| 1 |
// name: playmidi.ck |
| 2 |
// desc: example to render a MIDI file using ChucK |
| 3 |
|
| 4 |
// path |
| 5 |
NRev reverb => dac; |
| 6 |
// just a bit of reverb |
| 7 |
0.025 => reverb.mix; |
| 8 |
|
| 9 |
// the MIDI file in object |
| 10 |
MidiFileIn min; |
| 11 |
// the MIDI message shuttle |
| 12 |
MidiMsg msg; |
| 13 |
|
| 14 |
// the filename |
| 15 |
string filename; |
| 16 |
// if no command line arguments provided |
| 17 |
if( me.args() == 0 ) me.dir() + "bwv772.mid" => filename; |
| 18 |
// else use that filename |
| 19 |
else me.arg(0) => filename; |
| 20 |
|
| 21 |
// open the file; exit on error |
| 22 |
if( !min.open(filename) ) me.exit(); |
| 23 |
|
| 24 |
// print out |
| 25 |
cherr <= "----------" <= IO.newline(); |
| 26 |
cherr <= "MIDI file: " <= IO.newline() |
| 27 |
<= " |- " <= filename <= IO.newline() |
| 28 |
<= " |- contains " <= min.numTracks() <= " tracks" <= IO.newline() |
| 29 |
<= " |- TPQ: " <= min.tpq() <= IO.newline() |
| 30 |
<= " |- BPM: " <= min.bpm() <= IO.newline(); |
| 31 |
// print |
| 32 |
cherr <= "----------" <= IO.newline(); |
| 33 |
cherr <= "playing..." <= IO.newline(); |
| 34 |
cherr <= "----------" <= IO.newline(); |
| 35 |
|
| 36 |
// flag |
| 37 |
int done; |
| 38 |
|
| 39 |
// for each track, spork a separate shred with the track number |
| 40 |
for( int t; t < min.numTracks(); t++ ) |
| 41 |
spork ~ doTrack( t, 1 ); |
| 42 |
|
| 43 |
// keeping track of how many tracks are done |
| 44 |
while( done < min.numTracks() ) |
| 45 |
1::second => now; |
| 46 |
|
| 47 |
// done |
| 48 |
cherr <= "done; cleaning up..." <= IO.newline(); |
| 49 |
|
| 50 |
// close the file |
| 51 |
min.close(); |
| 52 |
|
| 53 |
// entry point for each shred assigned to a track |
| 54 |
fun void doTrack( int track, float speed ) |
| 55 |
{ |
| 56 |
// hack polyphony |
| 57 |
Wurley s[4]; |
| 58 |
// for each voice |
| 59 |
for( int i; i < s.size(); i++ ) |
| 60 |
{ |
| 61 |
// set gain |
| 62 |
.5 => s[i].gain; |
| 63 |
// connect to output |
| 64 |
s[i] => reverb; |
| 65 |
} |
| 66 |
// voice number for quick polyphony |
| 67 |
int v; |
| 68 |
|
| 69 |
// read through all MIDI messages on track |
| 70 |
while( min.read( msg, track ) ) |
| 71 |
{ |
| 72 |
// this means no more MIDI events at current time; advance time |
| 73 |
if( msg.when > 0::second ) |
| 74 |
msg.when * speed => now; // speed of 1 is nominal |
| 75 |
|
| 76 |
// catch NOTEON messages (lower nibble == 0x90) |
| 77 |
if( (msg.data1 & 0xF0) == 0x90 && msg.data2 > 0 && msg.data3 > 0 ) |
| 78 |
{ |
| 79 |
// get the pitch and convert to frequencey; set |
| 80 |
msg.data2 => Std.mtof => s[v].freq; |
| 81 |
// velocity data; note on |
| 82 |
msg.data3/127.0 => s[v].noteOn; |
| 83 |
// cycle the voices |
| 84 |
(v+1)%s.size() => v; |
| 85 |
|
| 86 |
// log |
| 87 |
cherr <= "NOTE ON track:" <= track <= " pitch:" <= msg.data2 <=" velocity:" <= msg.data3 <= " when: " <= msg.when/ms <= IO.newline(); |
| 88 |
} |
| 89 |
// other messages |
| 90 |
else |
| 91 |
{ |
| 92 |
// log |
| 93 |
// cherr <= "----EVENT (unhandled) track:" <= track <= " type:" <= (msg.data1&0xF0) |
| 94 |
// <= " data2:" <= msg.data2 <= " data3:" <= msg.data3 <= IO.newline(); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// done with track |
| 99 |
done++; |
| 100 |
} |