Debian Codegraph @

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

raw
1 // Listing 4.2 Using a loop to repeatedly play a sound file
2
3 // Play a sound file repeatedly in a loop
4 // by ChucK Programmer, January 12, 2017
5 SndBuf mySound => Pan2 pan => dac; // Connects a SndBuf through a
6 // Pan2 panner object to the DAC
7
8 // get file path and load file all in one line!
9 me.dir()+"/audio/cowbell_01.wav" => mySound.read;
10
11 // play our sound over and again in an infinite loop
12 while (true)
13 {
14 // random gain, rate (pitch), and pan each time
15 Math.random2f(0.1,1.0) => mySound.gain; // (1) Random gain for sound file
16 Math.random2f(-1.0,1.0) => pan.pan; // (2) Random pan position
17 Math.random2f(0.2,1.8) => mySound.rate; // (3) Random rate (speed and pitch)
18
19 // (re)start the sound by setting position to 0
20 0 => mySound.pos; // (4) Sets pos to zero to start it playing
21
22 // advance time so we can hear it
23 500.0 :: ms => now; // (5) Hangs out a bit while it plays
24 }