Debian Codegraph @

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

raw
1 // Listing 4.12a SndBufs and panning connections for your big drum machine
2
3 // Drum Machine, version 4.0
4 // by block-rockin programmer, Jan 1, 2099
5
6 // Here we'll use Modulo % and random to play drums
7 // Define array of master Gains for left, center, right
8 Gain master[3];
9 master[0] => dac.left; // Connects left ([0]) to dac.left.
10 master[1] => dac; // Connects center ([1]) to dac (both
11 // left and right automatically).
12 master[2] => dac.right; // Connects right ([2]) to dac.right.
13
14 // Declare SndBufs for lots of drums
15 // hook them up to pan positions
16 SndBuf kick => master[1]; // Connects kick drum SndBuf to center master gain.
17 SndBuf snare => master[1]; // Connects snare drum to center also.
18 SndBuf hihat => master[2]; // Connects hihat to right master gain.
19 SndBuf cowbell => master[0]; // Connects cowbell SndBuf to left master gain.
20
21 // Use a Pan2 for the hand claps,
22 // we'll use random panning later
23 SndBuf claps => Pan2 claPan; // Connects clap SndBuf to a Pan2 object.
24 claPan.chan(0) => master[0]; // Connects the left (0) channel of the Pan2 to master gain left.
25 claPan.chan(1) => master[2]; // Connects the right (1) channel of the Pan2 to master gain right.
26
27 // load up some samples of those // Loads all the sound files.
28 me.dir()+"/audio/kick_01.wav" => kick.read;
29 me.dir()+"/audio/snare_01.wav" => snare.read;
30 me.dir()+"/audio/hihat_01.wav" => hihat.read;
31 me.dir()+"/audio/cowbell_01.wav" => cowbell.read;
32 me.dir()+"/audio/clap_01.wav" => claps.read;
33
34 // Listing 4.12b Setting up variables for your big drum machine
35
36 // (1) Array to control cowbell strikes.
37 [1,0,1,0, 1,0,0,1, 0,1,0,1, 0,1,1,1] @=> int cowHits[];
38
39 // controls the overall length of our "measures"
40 // .cap() determines the maximum number of beats in your measure.
41 cowHits.cap() => int MAX_BEAT; // define using all caps, remember?
42
43 // modulo number for controlling kick and snare
44 4 => int MOD; // Constant for MOD operator--
45 // You'll use this to control
46 // kick and snare drum hits.
47
48 // overall speed control // Master speed control (tempo)--
49 0.15 :: second => dur tempo; // you'll use this to advance time each beat.
50
51 // counters: beat within measures, and measure
52 0 => int beat; // Two counters, one for beat
53 0 => int measure; // and one for measure number.
54
55 // Listing 4.12c Main loop to actually play drum patterns
56
57 // Main infinite drum loop
58 while (true)
59 {
60 // play kick drum on all main beats (0, 4, ...)
61 if (beat % 4 == 0) // (1) Uses MOD 4 to play
62 { // kick drum every fourth beat
63 0 => kick.pos;
64 }
65
66 // after a time, play snare on off beats (2, 6, ...)
67 if (beat % 4 == 2 && measure %2 == 1) // (2) Plays snare only on specific beats
68 {
69 0 => snare.pos;
70 }
71
72 // After a time, randomly play hihat or cowbell
73 if (measure > 1) { // (3) Plays cowbell and hihat only after measure 1
74 if (cowHits[beat]) // (4) Plays cowbell, controlled by array
75 {
76 0 => cowbell.pos;
77 }
78 else // (5) If not cowbell, then hi-hat
79 {
80 Math.random2f(0.0,1.0) => hihat.gain; // (6) Hi-hat has random gain
81 0 => hihat.pos;
82 }
83 }
84
85 // after a time, play randomly spaced claps at end of measure
86 if (beat > 11 && measure > 3) // (7) Plays claps only on certain measures and beats
87 {
88 Math.random2f(-1.0,1.0) => claPan.pan; // (8) Claps have random pan
89 0 => claps.pos;
90 }
91
92 tempo => now; // (9) Waits for one beat...
93
94 (beat + 1) % MAX_BEAT => beat; // (10) ...and then updates beat counter (MOD MAX)
95 if (beat==0)
96 { // (11) Increments measure counter at each new measure
97 measure++;
98 }
99 }