| 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 |
MidiMsg msg; |
| 10 |
|
| 11 |
class NoteEvent extends Event |
| 12 |
{ |
| 13 |
int note; |
| 14 |
int velocity; |
| 15 |
} |
| 16 |
|
| 17 |
// the event |
| 18 |
NoteEvent on; |
| 19 |
// array of ugen's handling each note |
| 20 |
Event us[128]; |
| 21 |
|
| 22 |
gain g => dac; |
| 23 |
.1 => g.gain; |
| 24 |
|
| 25 |
fun void handler() |
| 26 |
{ |
| 27 |
// don't connect to dac until we need it |
| 28 |
Mandolin m; |
| 29 |
PRCRev r => dac; |
| 30 |
.2 => r.mix; |
| 31 |
Event off; |
| 32 |
int note; |
| 33 |
|
| 34 |
while( true ) |
| 35 |
{ |
| 36 |
on => now; |
| 37 |
on.note => note; |
| 38 |
// dynamically repatch |
| 39 |
m => r; |
| 40 |
std.mtof( note ) => m.freq; |
| 41 |
std.rand2f( .6, .8 ) => m.pluckPos; |
| 42 |
on.velocity / 128.0 => m.pluck; |
| 43 |
off @=> us[note]; |
| 44 |
|
| 45 |
off => now; |
| 46 |
null @=> us[note]; |
| 47 |
m =< r; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// spork handlers |
| 52 |
for( 0 => int i; i < 10; i++ ) spork ~ handler(); |
| 53 |
|
| 54 |
MidiMsgIn mrw; |
| 55 |
|
| 56 |
mrw.open( "zz.txt" ); |
| 57 |
|
| 58 |
1 => float scale; |
| 59 |
|
| 60 |
time t; |
| 61 |
now => t; |
| 62 |
|
| 63 |
// get the midimsg |
| 64 |
while( mrw.read( msg ) != 0 ) |
| 65 |
{ |
| 66 |
scale * (msg.when - t) => now; |
| 67 |
msg.when => t; |
| 68 |
|
| 69 |
if( msg.data1 != 144 ) // !noteon |
| 70 |
continue; |
| 71 |
|
| 72 |
if( msg.data3 > 0 ) |
| 73 |
{ |
| 74 |
// store midi note number |
| 75 |
msg.data2 => on.note; |
| 76 |
// store velocity |
| 77 |
msg.data3 => on.velocity; |
| 78 |
// signal the event |
| 79 |
on.signal(); |
| 80 |
// yield without advancing time to allow shred to run |
| 81 |
me.yield(); |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
if( us[msg.data2] != null ) us[msg.data2].signal(); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
mrw.close(); |