| 1 |
//----------------------------------- (fail-correct) |
| 2 |
// name: polyfony.ck |
| 3 |
// desc: polyfonic midi |
| 4 |
// |
| 5 |
// by: Ananya Misra and Ge Wang |
| 6 |
// send all complaints to [email protected] |
| 7 |
//-------------------------------------------- |
| 8 |
|
| 9 |
MidiIn min; |
| 10 |
MidiMsg msg; |
| 11 |
|
| 12 |
if( !min.open( 0 ) ) |
| 13 |
me.exit(); |
| 14 |
|
| 15 |
class NoteEvent extends Event |
| 16 |
{ |
| 17 |
int note; |
| 18 |
int velocity; |
| 19 |
} |
| 20 |
|
| 21 |
// the event |
| 22 |
NoteEvent on; |
| 23 |
// array of ugen's handling each note |
| 24 |
Event us[128]; |
| 25 |
|
| 26 |
gain g => dac; |
| 27 |
.1 => g.gain; |
| 28 |
|
| 29 |
fun void handler() |
| 30 |
{ |
| 31 |
// don't connect to dac until we need it |
| 32 |
Clarinet c; |
| 33 |
PRCRev r => dac; |
| 34 |
.2 => r.mix; |
| 35 |
40 => c.rate; |
| 36 |
Event off; |
| 37 |
int note; |
| 38 |
|
| 39 |
while( true ) |
| 40 |
{ |
| 41 |
on => now; |
| 42 |
on.note => note; |
| 43 |
// dynamically repatch |
| 44 |
c => r; |
| 45 |
std.mtof( note ) => c.freq; |
| 46 |
.5 + on.velocity / 256.0 => c.startBlowing; |
| 47 |
off @=> us[note]; |
| 48 |
|
| 49 |
off => now; |
| 50 |
c.stopBlowing( 10.0 ); |
| 51 |
null @=> us[note]; |
| 52 |
100::ms => now; |
| 53 |
c =< r; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// spork handlers |
| 58 |
for( 0 => int i; i < 10; i++ ) spork ~ handler(); |
| 59 |
|
| 60 |
MidiMsgOut mrw; |
| 61 |
|
| 62 |
mrw.open( "zz.txt" ); |
| 63 |
|
| 64 |
while( true ) |
| 65 |
{ |
| 66 |
// wait on midi event |
| 67 |
min => now; |
| 68 |
|
| 69 |
// get the midimsg |
| 70 |
while( min.recv( msg ) ) |
| 71 |
{ |
| 72 |
if( msg.data1 != 144 ) // !noteon |
| 73 |
continue; |
| 74 |
|
| 75 |
if( msg.data3 > 0 ) |
| 76 |
{ |
| 77 |
// store midi note number |
| 78 |
msg.data2 => on.note; |
| 79 |
// store velocity |
| 80 |
msg.data3 => on.velocity; |
| 81 |
// signal the event |
| 82 |
on.signal(); |
| 83 |
// yield without advancing time to allow shred to run |
| 84 |
me.yield(); |
| 85 |
} |
| 86 |
else |
| 87 |
{ |
| 88 |
if( us[msg.data2] != null ) us[msg.data2].signal(); |
| 89 |
} |
| 90 |
|
| 91 |
mrw.write( msg, now ); |
| 92 |
} |
| 93 |
} |