| 1 |
// Listing 4.9 Improving the while loop of your drum machine |
| 2 |
|
| 3 |
// Drum Machine, version 2.0 |
| 4 |
// by block-rockin programmer, Dec 32, 1999 |
| 5 |
|
| 6 |
// SndBufs for kick (bass) and snare drums |
| 7 |
SndBuf kick => Gain master => dac; // (1) Kick to master mixer Gain to dac. |
| 8 |
SndBuf snare => master; // (2) Snare also to master mixer. |
| 9 |
|
| 10 |
// load up some samples of those // (3) Loads your kick and snare drum sound files. |
| 11 |
me.dir()+"/audio/kick_01.wav" => kick.read; |
| 12 |
me.dir()+"/audio/snare_01.wav" => snare.read; |
| 13 |
|
| 14 |
// declare a new tempo variable |
| 15 |
0.15 :: second => dur tempo; // (4) Tempo duration is time between beats. |
| 16 |
|
| 17 |
while (true) |
| 18 |
{ |
| 19 |
for (0 => int beat; beat < 16; beat++) // (5) Loops over a "measure" of 16 beats. |
| 20 |
{ |
| 21 |
// play kick drum on beat 0, 4, 8, and 12 |
| 22 |
if (beat==0 || beat==4 || beat==8 || beat==12) // (6) Plays kick only on specific beats. |
| 23 |
{ |
| 24 |
0 => kick.pos; |
| 25 |
} |
| 26 |
|
| 27 |
// play snare drum on beat 4, 10, 13, and 14 |
| 28 |
if (beat==4 || beat==10 || beat==13 || beat==14) // (7) Plays snare only on other specific beats. |
| 29 |
{ |
| 30 |
0 => snare.pos; |
| 31 |
} |
| 32 |
tempo => now; |
| 33 |
} |
| 34 |
} |