Debian Codegraph @

examples/book/digital-artists/chapter5/Listing5.17.ck

raw
1 // Listing 5.17 Sonifying the factorial() function
2
3 SinOsc s => dac; //(1) SinOsc so you can hear factorial
4 // our recursive factorial function
5 fun int factorial(int x) // (2) factorial function definition
6 {
7 sonify(x); // (3) Call the sonify function within factorial
8 if (x <=1) return 1;
9 else return (x*factorial(x-1));
10 }
11
12 // a function to sonify numbers
13 fun void sonify(int note) { // (4) Definition of the sonify function
14 // offset above middle C
15 Std.mtof(60+(0.5*note)) => s.freq; // (5) Sets frequency as function of note
16 1.0 => s.gain; // (6) Turns on Osc
17 300 :: ms => now;
18 0.0 => s.gain;
19 50 :: ms => now;
20 }
21
22 // try it out!
23 sonify(factorial(2));
24 second => now;
25
26 sonify(factorial(3));
27 second => now;
28
29 sonify(factorial(4));
30 second => now;
31
32 sonify(factorial(5));
33 second => now;
34