Debian Codegraph @

examples/book/digital-artists/chapter4/Listing4.10.ck

raw
1 // Listing 4.10 Using arrays to further improve your drum machine
2
3 // Drum Machine, version 3.0
4 // by block-rockin programmer, Dec 31, 1999
5
6 // SndBufs for kick (bass) and snare drums
7 SndBuf kick => Gain master => dac; // (1) kick, snare, and hihat
8 SndBuf snare => master; // SndBufs into mixer to dac
9 SndBuf hihat => master;
10
11 // load up some samples of those
12 me.dir()+"/audio/kick_01.wav" => kick.read;
13 me.dir()+"/audio/snare_01.wav" => snare.read;
14 me.dir()+"/audio/hihat_01.wav" => hihat.read;
15
16 0.3 => hihat.gain;
17 0.15 :: second => dur tempo;
18
19 // scores (arrays) to tell drums when to play
20 [1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0] @=> int kickHits[]; // (1) Arrays to control when kick
21 [0,0,1,0,0,0,1,0,0,0,0,0,1,1,1,1] @=> int snareHits[]; // and snare play
22
23 while (true)
24 {
25 0 => int beat;
26 while (beat < kickHits.cap())
27 {
28 // play kick drum on based on array value
29 if (kickHits[beat]) // Checks array at location
30 { // beat to see if you should
31 0 => kick.pos; // play the kick drum now.
32 }
33
34 if (snareHits[beat]) // Checks array at location
35 { // beat to see if you should
36 0 => snare.pos; // play the snare drum now.
37 }
38
39 // always play hihat
40 1 => hihat.pos; // Play hihat on every beat for now.
41 tempo => now; // Advance time; lets sound synthesize.
42 beat++; // Increments to next beat, and goes back
43 } // and does the inner while loop again.
44 }