Debian Codegraph @

examples/hanoi2.ck

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