| 1 |
//------------------------------------------------ |
| 2 |
// name: polyfony2.ck |
| 3 |
// desc: polyfonic clarinet model with midi input |
| 4 |
// |
| 5 |
// authors: Ananya Misra and Ge Wang |
| 6 |
// send all complaints to [email protected] |
| 7 |
//-------------------------------------------- |
| 8 |
|
| 9 |
// device to open |
| 10 |
0 => int device; |
| 11 |
// get from command line |
| 12 |
if( me.args() ) me.arg(0) => Std.atoi => device; |
| 13 |
|
| 14 |
MidiIn min; |
| 15 |
MidiMsg msg; |
| 16 |
|
| 17 |
// try to open MIDI port (see chuck --probe for available devices) |
| 18 |
if( !min.open( device ) ) me.exit(); |
| 19 |
|
| 20 |
// print out device that was opened |
| 21 |
<<< "MIDI device:", min.num(), " -> ", min.name() >>>; |
| 22 |
|
| 23 |
|
| 24 |
// make our own event |
| 25 |
class NoteEvent extends Event |
| 26 |
{ |
| 27 |
int note; |
| 28 |
int velocity; |
| 29 |
} |
| 30 |
|
| 31 |
// the event |
| 32 |
NoteEvent on; |
| 33 |
// array of ugen's handling each note |
| 34 |
Event @ us[128]; |
| 35 |
|
| 36 |
// the base patch |
| 37 |
Gain g => JCRev r => dac; |
| 38 |
.1 => g.gain; |
| 39 |
.2 => r.mix; |
| 40 |
|
| 41 |
// handler shred for a single voice |
| 42 |
fun void handler() |
| 43 |
{ |
| 44 |
// don't connect to dac until we need it |
| 45 |
Clarinet c; |
| 46 |
Event off; |
| 47 |
int note; |
| 48 |
|
| 49 |
// inifinite time loop |
| 50 |
while( true ) |
| 51 |
{ |
| 52 |
on => now; |
| 53 |
on.note => note; |
| 54 |
// dynamically repatch |
| 55 |
c => g; |
| 56 |
Std.mtof( note ) => c.freq; |
| 57 |
.4 + on.velocity / 128.0 * .6 => c.startBlowing; |
| 58 |
off @=> us[note]; |
| 59 |
|
| 60 |
off => now; |
| 61 |
c.stopBlowing( 10.0 ); |
| 62 |
null @=> us[note]; |
| 63 |
100::ms => now; |
| 64 |
c =< g; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// spork handlers, one for each voice |
| 69 |
for( 0 => int i; i < 20; i++ ) spork ~ handler(); |
| 70 |
|
| 71 |
// infinite time loop |
| 72 |
while( true ) |
| 73 |
{ |
| 74 |
// wait on midi event |
| 75 |
min => now; |
| 76 |
|
| 77 |
// get the midimsg |
| 78 |
while( min.recv( msg ) ) |
| 79 |
{ |
| 80 |
// catch only noteon |
| 81 |
if( (msg.data1 & 0xf0) == 0x90 ) |
| 82 |
{ |
| 83 |
// check velocity |
| 84 |
if( msg.data3 > 0 ) |
| 85 |
{ |
| 86 |
// store midi note number |
| 87 |
msg.data2 => on.note; |
| 88 |
// store velocity |
| 89 |
msg.data3 => on.velocity; |
| 90 |
// signal the event |
| 91 |
on.signal(); |
| 92 |
// yield without advancing time to allow shred to run |
| 93 |
me.yield(); |
| 94 |
} |
| 95 |
else |
| 96 |
{ |
| 97 |
if( us[msg.data2] != null ) us[msg.data2].signal(); |
| 98 |
} |
| 99 |
} |
| 100 |
else if( (msg.data1 & 0xf0) == 0x80 ) |
| 101 |
{ |
| 102 |
us[msg.data2].signal(); |
| 103 |
} |
| 104 |
} |
| 105 |
} |