| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// name: midi2osc-s.ck |
| 3 |
// desc: starter code for sending MIDI input out to OSC |
| 4 |
// |
| 5 |
// author: Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 6 |
// date: Spring 2024 |
| 7 |
//---------------------------------------------------------------------------- |
| 8 |
|
| 9 |
//---------------------------------------------------------------------------- |
| 10 |
// global values |
| 11 |
//---------------------------------------------------------------------------- |
| 12 |
// destination host | 224.0.0.1 == multicast |
| 13 |
"localhost" => string HOSTNAME; |
| 14 |
// destination port number |
| 15 |
6449 => int PORT; |
| 16 |
// accept MIDI input from up to this number of devices |
| 17 |
16 => int NUM_DEVICES; |
| 18 |
// OSC transmitter |
| 19 |
OscOut XMIT; |
| 20 |
|
| 21 |
|
| 22 |
//---------------------------------------------------------------------------- |
| 23 |
// initialization routines |
| 24 |
//---------------------------------------------------------------------------- |
| 25 |
// initialize MIDI |
| 26 |
if( !initMIDI( NUM_DEVICES ) ) me.exit(); |
| 27 |
// initialize OSC |
| 28 |
initOSC( XMIT, HOSTNAME, PORT ); |
| 29 |
//---------------------------------------------------------------------------- |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
//---------------------------------------------------------------------------- |
| 35 |
// infinite time loop |
| 36 |
//---------------------------------------------------------------------------- |
| 37 |
while( true ) 1::second => now; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
//---------------------------------------------------------------------------- |
| 43 |
// initialize OSC |
| 44 |
//---------------------------------------------------------------------------- |
| 45 |
fun void initOSC( OscOut xmit, string hostname, int port ) |
| 46 |
{ |
| 47 |
// aim the transmitter at destination |
| 48 |
xmit.dest( hostname, port ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
//---------------------------------------------------------------------------- |
| 55 |
// common OSC sends |
| 56 |
//---------------------------------------------------------------------------- |
| 57 |
// send message with an arbitrary number of floats |
| 58 |
fun void send( string address, float values[] ) |
| 59 |
{ |
| 60 |
// start the message... |
| 61 |
XMIT.start( address ); |
| 62 |
// loop and add float values |
| 63 |
for( float v : values ) { v => XMIT.add; } |
| 64 |
// send the message |
| 65 |
XMIT.send(); |
| 66 |
} |
| 67 |
// send message with int with an arbitrary number of floats |
| 68 |
fun void send( string address, int num, float values[] ) |
| 69 |
{ |
| 70 |
// start the message... |
| 71 |
XMIT.start( address ); |
| 72 |
// add int |
| 73 |
num => XMIT.add; |
| 74 |
// loop and add float values |
| 75 |
for( float v : values ) { v => XMIT.add; } |
| 76 |
// send the message |
| 77 |
XMIT.send(); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
//---------------------------------------------------------------------------- |
| 84 |
// initialize MIDI |
| 85 |
//---------------------------------------------------------------------------- |
| 86 |
fun int initMIDI( int numDevices ) |
| 87 |
{ |
| 88 |
// devices to open (try: chuck --probe) |
| 89 |
MidiIn midi[numDevices]; |
| 90 |
|
| 91 |
// opens MIDI input devices one by one, starting from 0, until it |
| 92 |
// reaches one it can't open. then waits for midi events on all open |
| 93 |
// devices and prints out the device, and contents of the MIDI message |
| 94 |
for( int i; i < midi.size(); i++ ) |
| 95 |
{ |
| 96 |
// no print err |
| 97 |
midi[i].printerr( 0 ); |
| 98 |
|
| 99 |
// open the device |
| 100 |
if( midi[i].open( i ) ) |
| 101 |
{ |
| 102 |
<<< "device", i, "->", midi[i].name(), "->", "open: SUCCESS" >>>; |
| 103 |
spork ~ midi_go( midi[i], i ); |
| 104 |
} |
| 105 |
else |
| 106 |
{ |
| 107 |
midi.size( i ); |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// check |
| 113 |
if( midi.size() == 0 ) |
| 114 |
{ |
| 115 |
<<< "(midi2osc-s.ck) no MIDI input devices found, exiting...", "" >>>; |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
//---------------------------------------------------------------------------- |
| 126 |
// MIDI handler (on per device) |
| 127 |
//---------------------------------------------------------------------------- |
| 128 |
fun void midi_go( MidiIn min, int id ) |
| 129 |
{ |
| 130 |
// the message |
| 131 |
MidiMsg msg; |
| 132 |
|
| 133 |
// infinite event loop |
| 134 |
while( true ) |
| 135 |
{ |
| 136 |
// wait on event |
| 137 |
min => now; |
| 138 |
|
| 139 |
// print message |
| 140 |
while( min.recv( msg ) ) |
| 141 |
{ |
| 142 |
// print MIDI message with device id (useful for debugging) |
| 143 |
// <<< "MIDI input on device", id, ":", msg.data1, msg.data2, msg.data3 >>>; |
| 144 |
|
| 145 |
// knobs |
| 146 |
if( msg.data1 == 185 ) |
| 147 |
{ |
| 148 |
send( "/foo/control/knob", msg.data2, [ msg.data3/127.0 ] ); |
| 149 |
} |
| 150 |
else if( msg.data1 == 153 ) |
| 151 |
{ |
| 152 |
send( "/foo/control/button", msg.data2, [ msg.data3/127.0 ] ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |