Debian Codegraph @

examples/hanoi3.ck

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