diff options
author | David Runge <dave@sleepmap.de> | 2018-03-18 00:57:06 +0100 |
---|---|---|
committer | David Runge <dave@sleepmap.de> | 2018-03-18 00:57:06 +0100 |
commit | 6a213f643bcee208afe6f1808e7ce4b634e39852 (patch) | |
tree | 44f738a741ab1dd3fd4df01522455cff0facecf4 /classes | |
parent | 755cc2bc11e9c571c08df809935b68c5f4cf6652 (diff) | |
download | usbmidiism-6a213f643bcee208afe6f1808e7ce4b634e39852.tar.gz usbmidiism-6a213f643bcee208afe6f1808e7ce4b634e39852.tar.bz2 usbmidiism-6a213f643bcee208afe6f1808e7ce4b634e39852.tar.xz usbmidiism-6a213f643bcee208afe6f1808e7ce4b634e39852.zip |
Adding classes/MIDIState{,Manager}.sc, taking care of early stages of state management.
Making constructors more readable. Adding MIDIStateManager to USBMIDIIsm. Creating/recycling states for new and re-recognized MIDI devices.
Diffstat (limited to 'classes')
-rw-r--r-- | classes/MIDIDevice.sc | 60 | ||||
-rw-r--r-- | classes/MIDIState.sc | 21 | ||||
-rw-r--r-- | classes/MIDIStateManager.sc | 41 | ||||
-rw-r--r-- | classes/NanoKontrol2.sc | 5 | ||||
-rw-r--r-- | classes/PlatformMPlus.sc | 5 | ||||
-rw-r--r-- | classes/USBMIDIIsm.sc | 35 |
6 files changed, 154 insertions, 13 deletions
diff --git a/classes/MIDIDevice.sc b/classes/MIDIDevice.sc index 9e7f2b1..f35be83 100644 --- a/classes/MIDIDevice.sc +++ b/classes/MIDIDevice.sc @@ -5,6 +5,7 @@ MIDIDevice{ <inPortNum, <outPortNum, <verbose, + <>states, <inPort, <outPort, <faders, @@ -15,8 +16,31 @@ MIDIDevice{ <passThroughs; *new{ - arg name, uid, inPortNum, outPortNum, verbose=false, faderSize=0, potiSize=0, buttonSize=0, jogwheelSize=0, touchpadSize=0; - ^super.newCopyArgs(name, uid, inPortNum, outPortNum, verbose).initDevice(faderSize, potiSize, buttonSize, jogwheelSize, touchpadSize); + arg name, + uid, + inPortNum, + outPortNum, + verbose=false, + states, + faderSize=0, + potiSize=0, + buttonSize=0, + jogwheelSize=0, + touchpadSize=0; + ^super.newCopyArgs( + name, + uid, + inPortNum, + outPortNum, + verbose, + states + ).initDevice( + faderSize, + potiSize, + buttonSize, + jogwheelSize, + touchpadSize + ); } initDevice{ @@ -27,6 +51,7 @@ MIDIDevice{ jogwheels = Array.newClear(indexedSize: jogwheelSize); touchpads = Array.newClear(indexedSize: touchpadSize); passThroughs = Dictionary(); +// states = MIDIState(); this.connectPorts(); postln("Device '"++name++"' initialized."); } @@ -145,4 +170,35 @@ MIDIDevice{ }); }); } + + connectStates{ + if(verbose,{ + postln("Setting up states for "++name); + }); + states.keysValuesDo({|channel, type| + MIDIdef.new( + key: ("state_"++uid++"_"++channel).asSymbol, + func: {arg ...args; + if(verbose,{ + ("state_"++uid++"_"++channel++": "++args).postln; + }); + case + {type==\control}{outPort.control(chan: channel, val: args[0])} + {type==\bend}{outPort.bend(chan: channel, val: args[0])} + {type==\midiClock}{outPort.midiClock} + {type==\bend}{outPort.bend(chan: channel, veloc: args[0])} + {type==\noteOn}{outPort.noteOn(chan: channel, note: args[1], veloc: args[0])} + {type==\noteOff}{outPort.noteOn(chan: channel, note: args[1], veloc: args[0])} + {type==\polyTouch}{outPort.polyTouch(chan: channel, note: args[1], val: args[0])} + {type==\program}{outPort.program(chan: channel, num: args[0])} + {type==\touch}{outPort.touch(chan: channel, val: args[0])} + {type==\start}{outPort.start} + {type==\stop}{outPort.stop} + {type==\continue}{outPort.continue}; + }, + chan: channel, + msgType: type, + ); + }); + } } diff --git a/classes/MIDIState.sc b/classes/MIDIState.sc new file mode 100644 index 0000000..2b3da3c --- /dev/null +++ b/classes/MIDIState.sc @@ -0,0 +1,21 @@ +MIDIState{ + var <name, <>uid, <>inPortNum, <>outPortNum, <data, <>connected=false; + + *new{ + arg name, uid, inPortNum, outPortNum; + ^super.newCopyArgs( + name, + uid, + inPortNum, + outPortNum + ).initState(); + } + + initState{ + data = Dictionary(); + connected = true; + } + + + +} diff --git a/classes/MIDIStateManager.sc b/classes/MIDIStateManager.sc new file mode 100644 index 0000000..ec4b53c --- /dev/null +++ b/classes/MIDIStateManager.sc @@ -0,0 +1,41 @@ +MIDIStateManager{ + + var <deviceStates; + + *new{ + ^super.new.initManager(); + } + + initManager{ + deviceStates = Array(); + } + + // request a state + // if a stored and unconnected state is available return that, else return a + // new MIDIState + requestState{ + arg name, uid, inPortNum, outPortNum; + var devicePosition = -1, stateAvailable = false; + deviceStates.do({|storedDevice, i| + if((name == storedDevice.name) && (storedDevice.connected.not),{ + stateAvailable = true; + devicePosition = i; + }); + }); + if(stateAvailable && (devicePosition >= 0),{ + deviceStates[devicePosition].connected=true; + ^deviceStates[devicePosition]; + },{ + deviceStates = deviceStates.add( + MIDIState.new(name, uid, inPortNum, outPortNum) + ); + ^deviceStates[deviceStates.size-1]; + }); + } + + listStates{ + deviceStates.do({|state, i| + postln(i.asString++": ["++state.name++", "++state.uid++", "++state.inPortNum++", "++state.outPortNum++", "++state.connected++"]"); + }); + } +} diff --git a/classes/NanoKontrol2.sc b/classes/NanoKontrol2.sc index d0f79c9..43fafe0 100644 --- a/classes/NanoKontrol2.sc +++ b/classes/NanoKontrol2.sc @@ -1,11 +1,11 @@ NanoKontrol2 : MIDIDevice{ *new{ - arg name, uid, inPort, outPort, verbose; + arg name, uid, inPort, outPort, verbose, states; var faderSize = 8, potiSize = 8, buttonSize = 36; - ^super.new(name, uid, inPort, outPort, verbose, faderSize, potiSize, buttonSize).init; + ^super.new(name, uid, inPort, outPort, verbose, states, faderSize, potiSize, buttonSize).init; } init{ @@ -16,6 +16,7 @@ NanoKontrol2 : MIDIDevice{ cleanup{ super.disconnectPorts(); + states.connected=false; } addButtons{ diff --git a/classes/PlatformMPlus.sc b/classes/PlatformMPlus.sc index fc10aa1..999786d 100644 --- a/classes/PlatformMPlus.sc +++ b/classes/PlatformMPlus.sc @@ -1,12 +1,12 @@ PlatformMPlus : MIDIDevice{ *new{ - arg name, uid, inPort, outPort, verbose; + arg name, uid, inPort, outPort, verbose, states; var faderSize = 9, potiSize = 8, buttonSize = 48, jogwheelSize = 1; - ^super.new(name, uid, inPort, outPort, verbose, faderSize, potiSize, buttonSize, jogwheelSize).init; + ^super.new(name, uid, inPort, outPort, verbose, states, faderSize, potiSize, buttonSize, jogwheelSize).init; } init{ @@ -21,6 +21,7 @@ PlatformMPlus : MIDIDevice{ cleanup{ super.disconnectPorts(); super.disconnectPassThroughs(); + states.connected=false; } addPassThroughs{ diff --git a/classes/USBMIDIIsm.sc b/classes/USBMIDIIsm.sc index 3a7518a..52e421b 100644 --- a/classes/USBMIDIIsm.sc +++ b/classes/USBMIDIIsm.sc @@ -1,6 +1,6 @@ USBMIDIIsm{ - var <devices, verbose; + var <devices, <stateManager, verbose; *new{ arg setVerbose=false; @@ -10,6 +10,7 @@ USBMIDIIsm{ init{ arg setVerbose; devices = Array(); + stateManager = MIDIStateManager(); verbose = setVerbose; this.discoverDevices(); } @@ -23,6 +24,7 @@ USBMIDIIsm{ postln("Resetting list of MIDI input devices."); devices.do({|device,i| if(device.inPortNum.notNil || device.outPortNum.notNil,{ + // stateManager.store(device)? device.cleanup(); }); }); @@ -42,6 +44,7 @@ USBMIDIIsm{ inEndPoint.device.contains("Announce")).not, { // correlate with output devices MIDIClient.destinations.do({|outEndPoint, outPortNum| + var device, state; if((inEndPoint.device == outEndPoint.device && inEndPoint.uid == outEndPoint.uid), { // available devices @@ -49,11 +52,17 @@ USBMIDIIsm{ {inEndPoint.device.contains("Platform M+")} { devices = devices.add( PlatformMPlus.new( - inEndPoint.device, + name: inEndPoint.device, uid: inEndPoint.uid.asSymbol, inPort: inPortNum, outPort: outPortNum, - verbose: verbose + verbose: verbose, + states: stateManager.requestState( + inEndPoint.device, + inEndPoint.uid.asSymbol, + inPortNum, + outPortNum + ); ) ); } @@ -64,7 +73,13 @@ USBMIDIIsm{ uid: inEndPoint.uid.asSymbol, inPort: inPortNum, outPort: outPortNum, - verbose: verbose + verbose: verbose, + states: stateManager.requestState( + inEndPoint.device, + inEndPoint.uid.asSymbol, + inPortNum, + outPortNum + ); ) ); }; @@ -75,12 +90,18 @@ USBMIDIIsm{ } listDevices{ - postln("Name, uid, in, out"); - devices.do({|device| - postln(device.name++", "++device.uid++", "++device.inPortNum++", "++device.outPortNum); + devices.do({|device, i| + postln(i.asString++": ["++device.name++", "++device.uid++", "++device.inPortNum++", "++device.outPortNum++"]"); }); } + setStates{ + arg device; + + + } + + listen{ arg listen = true; if(listen, { |