Debian Codegraph @

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

raw
1 // Listing 3.8 "Twinkle" with melody, harmony, and lyrics!
2
3 // by ChucK Team, July 2050
4
5 // two oscillators, melody and harmony
6 SinOsc s => Pan2 mpan => dac; // (1) SinOsc through Pan2 for melody
7 TriOsc t => dac; // (2) TriOsc fixed at center for harmony
8
9 // we will use these to separate notes later
10 0.5 => float onGain; // (3) Note on/off gains
11 0.0 => float offGain;
12
13 // declare and initialize our arrays of MIDI note #s
14 [57, 57, 64, 64, 66, 66, 64, // (4) Melody (int) MIDI note array
15 62, 62, 61, 61, 59, 59, 57] @=> int melNotes[];
16 [61, 61, 57, 61, 62, 62, 61, // (5) Harmony (int) MIDI note array
17 59, 56, 57, 52, 52, 68, 69] @=> int harmNotes[];
18
19 // quarter note and half note (6) Duration (dur) array
20 0.5 :: second => dur q;
21 1.0 :: second => dur h;
22 [ q, q, q, q, q, q, h, q, q, q, q, q, q, h] @=> dur myDurs[];
23
24 // make one more array to hold the words
25 ["Twin","kle","twin","kle","lit","tle","star,", // (7) Lyrics (string) array
26 "how", "I","won","der","what","you","are."] @=> string words[];
27
28 // loop over all the arrays
29 // (make sure they're the same length!!)
30 for (0 => int i; i < melNotes.cap(); i++) // (8) Plays through all
31 { // notes in array
32 // (9) print out index, MIDI notes, and words from arrays
33 <<< i, melNotes[i], harmNotes[i], words[i] >>>;
34
35 // set melody and harmony from arrays
36 Std.mtof(harmNotes[i]) => s.freq; // (10) Sets frequencies from
37 Std.mtof(melNotes[i]) => t.freq; // array MIDI notes
38
39 // melody has a random pan for each note
40 Math.random2f(-1.0,1.0) => mpan.pan; // (11) Random pan for melody oscillator
41
42 // notes are on for 70% of duration from array
43 onGain => s.gain => t.gain; // (12) Turns on both oscillators
44 0.7*myDurs[i] => now; // (13) 70% of array duration is note on time
45
46 // space between notes is 30% of array duration
47 offGain => s.gain => t.gain;
48 0.3*myDurs[i] => now; // (14) 30% of array duration is off time
49 }