| 1 |
//------------------------------------------------ |
| 2 |
// name: polyfony.ck |
| 3 |
// desc: polyfonic mandolin model |
| 4 |
// |
| 5 |
// authors: Ananya Misra and Ge Wang |
| 6 |
// send all complaints to [email protected] |
| 7 |
//-------------------------------------------- |
| 8 |
|
| 9 |
// device to open (see: chuck --probe) |
| 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 for a single voice |
| 42 |
fun void handler() |
| 43 |
{ |
| 44 |
// don't connect to dac until we need it |
| 45 |
Mandolin m; |
| 46 |
Event off; |
| 47 |
int note; |
| 48 |
|
| 49 |
while( true ) |
| 50 |
{ |
| 51 |
on => now; |
| 52 |
on.note => note; |
| 53 |
// dynamically repatch |
| 54 |
m => g; |
| 55 |
Std.mtof( note ) => m.freq; |
| 56 |
Math.random2f( .6, .8 ) => m.pluckPos; |
| 57 |
on.velocity / 128.0 => m.pluck; |
| 58 |
off @=> us[note]; |
| 59 |
|
| 60 |
off => now; |
| 61 |
null @=> us[note]; |
| 62 |
m =< g; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
// spork handlers, one for each voice |
| 67 |
for( 0 => int i; i < 20; i++ ) spork ~ handler(); |
| 68 |
|
| 69 |
// infinite time-loop |
| 70 |
while( true ) |
| 71 |
{ |
| 72 |
// wait on midi event |
| 73 |
min => now; |
| 74 |
|
| 75 |
// get the midimsg |
| 76 |
while( min.recv( msg ) ) |
| 77 |
{ |
| 78 |
// catch only noteon |
| 79 |
if( (msg.data1 & 0xf0) == 0x90 ) |
| 80 |
{ |
| 81 |
// check velocity |
| 82 |
if( msg.data3 > 0 ) |
| 83 |
{ |
| 84 |
// store midi note number |
| 85 |
msg.data2 => on.note; |
| 86 |
// store velocity |
| 87 |
msg.data3 => on.velocity; |
| 88 |
// signal the event |
| 89 |
on.signal(); |
| 90 |
// yield without advancing time to allow shred to run |
| 91 |
me.yield(); |
| 92 |
} |
| 93 |
else |
| 94 |
{ |
| 95 |
if( us[msg.data2] != null ) us[msg.data2].signal(); |
| 96 |
} |
| 97 |
} |
| 98 |
else if( (msg.data1 & 0xf0) == 0x80 ) |
| 99 |
{ |
| 100 |
us[msg.data2].signal(); |
| 101 |
} |
| 102 |
} |
| 103 |
} |