Debian Codegraph @

examples/book/digital-artists/chapter3/Listing3.5.ck

raw
1 // Listing 3.5 Playing a melody stored in an array
2 // Like Listing 3.4, but use logic for durations
3
4 // Let's Twinkle with a square wave
5 SqrOsc s => dac; // (1) Square wave oscillator for melody
6
7 // gains to separate our notes
8 0.7 => float onGain; // (2) Note on/off gains
9 0.0 => float offGain;
10
11 // (3) declare and initialize array of MIDI notes (int) for melody
12 [57,57,64,64,66,66,64,62,62,61,61,59,59,57] @=> int a[];
13
14 // loop for length of array
15 for (0 => int i; i < a.cap(); i++)
16 {
17 <<< i, a[i] >>>; // (4) Prints index and array note
18
19 // set frequency and gain to turn on our note
20 Std.mtof(a[i]) => s.freq; // (5) Sets pitch for melody notes
21 onGain => s.gain; // (6) Note on
22
23 if (i==6 || i==13)
24 {
25 0.8::second => now; // (3.5.1) Some notes are longer
26 }
27 else
28 {
29 0.3::second => now; // (3.5.1) The rest are shorter
30 }
31
32 // turn off our note to separate from the next
33 offGain => s.gain; // (8) Note off
34 0.2::second => now;
35 }