| 1 |
//---------------------------------------------------------------------------- |
| 2 |
// name: r-multimsg.ck |
| 3 |
// desc: OSC example: receiver for multiple message types |
| 4 |
// note: launch with s-multimsg.ck |
| 5 |
// |
| 6 |
// author: Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 7 |
// date: spring 2022 |
| 8 |
//---------------------------------------------------------------------------- |
| 9 |
|
| 10 |
// the patch |
| 11 |
BlitSaw s => JCRev r => dac; |
| 12 |
.5 => s.gain; |
| 13 |
.1 => r.mix; |
| 14 |
|
| 15 |
// spork the handlers, one for each message type |
| 16 |
spork ~ onNotes(); |
| 17 |
spork ~ onHarmonics(); |
| 18 |
|
| 19 |
// keep alive |
| 20 |
while( true ) 1::second => now; |
| 21 |
|
| 22 |
// handler for incoming OSC notes messages |
| 23 |
fun void onNotes() |
| 24 |
{ |
| 25 |
// create our OSC receiver |
| 26 |
OscIn oin; |
| 27 |
// create our OSC message |
| 28 |
OscMsg msg; |
| 29 |
// use port 6449 (or whatever) |
| 30 |
6449 => oin.port; |
| 31 |
// create an address in the receiver, expect an int and a float |
| 32 |
oin.addAddress( "/foo/notes, if" ); |
| 33 |
|
| 34 |
// infinite event loop |
| 35 |
while( true ) |
| 36 |
{ |
| 37 |
// wait for event to arrive |
| 38 |
oin => now; |
| 39 |
|
| 40 |
// grab the next message from the queue. |
| 41 |
while( oin.recv(msg) ) |
| 42 |
{ |
| 43 |
// expected datatypes (note: as indicated by "i f") |
| 44 |
int i; |
| 45 |
float f; |
| 46 |
|
| 47 |
// fetch the first data element as int |
| 48 |
msg.getInt(0) => i => Std.mtof => s.freq; |
| 49 |
// fetch the second data element as float |
| 50 |
msg.getFloat(1) => f => s.gain; |
| 51 |
|
| 52 |
// print |
| 53 |
<<< "notes (via OSC):", i, f >>>; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
// handler for incoming OSC harmonics messages |
| 59 |
fun void onHarmonics() |
| 60 |
{ |
| 61 |
// create our OSC receiver |
| 62 |
OscIn oin; |
| 63 |
// create our OSC message |
| 64 |
OscMsg msg; |
| 65 |
// use port 6449 (or whatever) |
| 66 |
6449 => oin.port; |
| 67 |
// create an address in the receiver, expect an int |
| 68 |
oin.addAddress( "/foo/harmonics, i" ); |
| 69 |
|
| 70 |
// infinite event loop |
| 71 |
while( true ) |
| 72 |
{ |
| 73 |
// wait for event to arrive |
| 74 |
oin => now; |
| 75 |
|
| 76 |
// grab the next message from the queue. |
| 77 |
while( oin.recv(msg) ) |
| 78 |
{ |
| 79 |
// expected datatypes (note: as indicated by "i") |
| 80 |
int i; |
| 81 |
|
| 82 |
// fetch the first data element as int |
| 83 |
msg.getInt(0) => i => s.harmonics; |
| 84 |
|
| 85 |
// print |
| 86 |
<<< "harmonics (via OSC):", i >>>; |
| 87 |
} |
| 88 |
} |
| 89 |
} |