Debian Codegraph @

examples/hanoi++.ck

raw
1 // towers of annoy
2 // demonstrates: recursion, arrays, stereo, sndbuf,
3 // questionable sonification
4
5 // number of disks
6 10 => int disks;
7 // min duration for each move
8 200::ms => dur wait => dur the_wait;
9 // step number
10 0 => int STEPS;
11
12 // gain to pan to dac
13 Gain g => Pan2 pan;
14 // stereo from here
15 pan.left => NRev r1 => dac.left;
16 pan.right => NRev r2 => dac.right;
17 // set gain
18 .5 => g.gain;
19 // set mix
20 .1 => r1.mix => r2.mix;
21
22 // the pegs (0 not used - for easy indexing)
23 SndBuf pegs[4];
24
25 // load files
26 me.dir() + "/data/snare-chili.wav" => pegs[1].read;
27 me.dir() + "/data/kick.wav" => pegs[2].read;
28 me.dir() + "/data/snare-hop.wav" => pegs[3].read;
29
30 // connect to gain
31 for( 1 => int i; i < pegs.size(); i++ )
32 pegs[i] => g;
33
34 // the hanoi
35 fun void hanoi( int num, int src, int dest, int other )
36 {
37 // move all except the biggest
38 if( num > 1 ) hanoi( num - 1, src, other, dest );
39
40 // increment steps
41 STEPS++;
42 // move the biggest
43 <<< "step", STEPS, " | move disk from peg", src, " -> ", "peg", dest >>>;
44 // sonify
45 0 => pegs[dest].pos;
46 // gain
47 Math.random2f( .2, .9 ) => pegs[dest].gain;
48 // pan
49 .8 * (dest - 2) => pan.pan;
50 // advance time
51 the_wait => now;
52
53 // move onto the biggest
54 if( num > 1 ) hanoi( num - 1, other, dest, src );
55 }
56
57 // start it
58 hanoi( disks, 1, 3, 2 );
59
60 <<<"done!">>>;
61
62 // let time pass for reverb to go...
63 2::second => now;
64
65