| 1 |
// Listing 4.5 Playing different sound files (method 2) using multiple SndBufs |
| 2 |
|
| 3 |
// Playing multiple sound files (method 2) |
| 4 |
// by ChucK Programmer, August, 2023 |
| 5 |
SndBuf snare[3]; // (1) Declares an array of three SndBufs... |
| 6 |
|
| 7 |
// for fun, let's pan them to left, right, center |
| 8 |
snare[0] => dac.left; |
| 9 |
snare[1] => dac; |
| 10 |
snare[2] => dac.right; |
| 11 |
|
| 12 |
// (2) pre-load three different sound files at the beginning |
| 13 |
me.dir()+"/audio/snare_01.wav" => snare[0].read; |
| 14 |
me.dir()+"/audio/snare_02.wav" => snare[1].read; |
| 15 |
me.dir()+"/audio/snare_03.wav" => snare[2].read; |
| 16 |
|
| 17 |
// infinite loop |
| 18 |
while (true) |
| 19 |
{ |
| 20 |
// (2) pick a random number between 0 and #files |
| 21 |
Math.random2(0,snare.cap()-1) => int which; |
| 22 |
|
| 23 |
// play that sample |
| 24 |
0 => snare[which].pos; |
| 25 |
|
| 26 |
// let it play |
| 27 |
0.5 :: second => now; |
| 28 |
} |