Debian Codegraph @

examples/book/digital-artists/chapter1/Listing1.20.ck

raw
1 // Listing 1.20a Putting "Twinkle" all together, with two waves!
2
3 // Twinkle, with two oscillators!
4 SinOsc s => dac; // (1) Sine wave oscillator.
5 TriOsc t => dac; // (2) Another oscillator (triangle wave).
6
7 // our main pitch variable
8 110.0 => float melody; // (3) Sets initial pitch.
9
10 // gain control for our triangle wave melody
11 0.3 => float onGain; // (4) Gains for note on.
12
13 // we'll use this for our on and off times
14 0.3 :: second => dur myDur; // (5) Notes duration.
15
16 // Listing 1.20b Putting "Twinkle" all together (part B, Sweeping Upward)
17
18 // only play t at first, sweeping pitch upward
19 onGain => t.gain; // (6) Turns on triangle oscillator.
20 0 => s.gain; // (7) Turns off sine osc.
21
22 while (melody < 220.0) { // (8) Loops until pitch reaches 220.
23 melody => t.freq;
24 1.0 +=> melody; // (9) Steps up pitch by 1 Hz.
25 0.01 :: second => now; // (10) Every 1/100 of a second.
26 }
27
28 // Listing 1.20c Putting "Twinkle" all together, first "Twinkle"
29
30 // turn both on, set up the pitches
31 0.7 => s.gain; // (11) Now turn on sin osc too.
32 110 => s.freq; // (12) ...and initialize its pitch.
33
34 // play two notes, 1st "Twinkle"
35 for (0 => int i; i < 2; i++) { // (13) Use a for loop to play two notes.
36 onGain => t.gain; // (14) Turn on triangle.
37 myDur => now; // (15) Let note play.
38 0 => t.gain; // (16) Turn off triangle.
39 myDur => now; // (17) Silence to separate notes.
40 }
41
42 // Listing 1.20d Putting "Twinkle" all together , the second "twinkle"
43
44 // new pitches!
45 138.6 => s.freq; // (19) Sets up next "twinkle" frequency.
46 1.5*melody => t.freq;
47
48 // two more notes, 2nd "twinkle"
49 for (0 => int i; i < 2; i++) { // (20) Plays that twice (for loop).
50 onGain => t.gain;
51 myDur => now;
52 0 => t.gain;
53 myDur => now;
54 }
55
56 // Listing 1.20e Putting "Twinkle" all together, playing "little" and "star"
57
58 // pitches for "little"
59 146.8 => s.freq; // (20) Sets up next frequency for "little".
60 1.6837 * melody => t.freq;
61
62 // play 2 notes for "little"
63 for (0 => int i; i < 2; i++) { // (21) Plays that twice (for loop).
64 onGain => t.gain;
65 myDur => now;
66 0 => t.gain;
67 myDur => now;
68 }
69
70 // set up and play "star!"
71 138.6 => s.freq; // (22) Sets up next frequency for "star".
72 1.5*melody => t.freq;
73 onGain => t.gain; // (23) Plays that note...
74 second => now; // (24) ...for a second.
75
76 // Listing 1.20f Putting "Twinkle" all together
77
78 // end by sweeping both oscillators down to zero
79 for (330 => int i; i > 0; i--) { // (25) Uses a for loop to sweep down from 330 Hz.
80 i => t.freq;
81 i*1.333 => s.freq;
82 0.01 :: second => now; // (25) Uses a for loop to sweep down from 330 Hz.
83 }
84
85