| 1 |
// Listing 5.14 Drum machine using patterns stored in arrays |
| 2 |
|
| 3 |
// sound chain: two drums |
| 4 |
SndBuf kick => dac; // (1) SndBufs for kick and snare drum sounds |
| 5 |
SndBuf snare => dac; |
| 6 |
|
| 7 |
// (2) load the sound files for our kick and snare drums |
| 8 |
me.dir() + "/audio/kick_01.wav" => kick.read; |
| 9 |
me.dir() + "/audio/snare_03.wav" => snare.read; |
| 10 |
|
| 11 |
// (3) set their pointers to end, to make no sound |
| 12 |
kick.samples() => kick.pos; |
| 13 |
snare.samples() => snare.pos; |
| 14 |
// drum patterns as logical variables, // (4) Arrays to hold logical values, play=1/not play=0 |
| 15 |
[1,0,0,0,1,0,0,0] @=> int kickPattern1[]; |
| 16 |
[0,0,1,0,0,0,1,0] @=> int kickPattern2[]; |
| 17 |
[1,0,1,0,1,0,1,0] @=> int snarePattern1[]; |
| 18 |
[1,1,1,1,0,1,1,1] @=> int snarePattern2[]; |
| 19 |
|
| 20 |
// function to play pattern arrays |
| 21 |
fun void playSection(int kickA[], int snareA[], float beattime) |
| 22 |
{ // (5) playSection function, arrayarguments control patterns |
| 23 |
for (0 => int i; i < kickA.cap(); i++) |
| 24 |
{ |
| 25 |
if (kickA[i]) |
| 26 |
{ |
| 27 |
0 => kick.pos; |
| 28 |
} |
| 29 |
if (snareA[i]) |
| 30 |
{ |
| 31 |
0 => snare.pos; |
| 32 |
} |
| 33 |
beattime::second => now; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// Main program, infinite loop |
| 38 |
while (true) // (6) Infinite test loop |
| 39 |
{ // (7) Call playSection with different patterns |
| 40 |
playSection(kickPattern1,snarePattern2,0.2); |
| 41 |
playSection(kickPattern2,snarePattern2,0.2); |
| 42 |
playSection(kickPattern1,snarePattern2,0.2); |
| 43 |
playSection(kickPattern2,snarePattern1,0.2); |
| 44 |
} |