| 1 |
// Listing 3.6 Storing durations in an array |
| 2 |
|
| 3 |
// Let's Twinkle with a square wave |
| 4 |
SqrOsc s => dac; // (1) Square wave oscillator for melody |
| 5 |
|
| 6 |
// gains to separate our notes |
| 7 |
0.7 => float onGain; // (2) Note on/off gains |
| 8 |
0.0 => float offGain; |
| 9 |
|
| 10 |
// (3) declare and initialize array of MIDI notes (int) for melody |
| 11 |
[57,57,64,64,66,66,64,62,62,61,61,59,59,57] @=> int myNotes[]; |
| 12 |
|
| 13 |
// quarter note and half note durations |
| 14 |
0.3 :: second => dur q; // (4) Duration for quarter notes |
| 15 |
0.8 :: second => dur h; // (5) Duration for half notes |
| 16 |
// (6) Array of durations for melody notes |
| 17 |
[q, q, q, q, q, q, h, q, q, q, q, q, q, h] @=> dur myDurs[]; |
| 18 |
|
| 19 |
// loop for length of array |
| 20 |
for (0 => int i; i < myNotes.cap(); i++) // (7) For loop iterates |
| 21 |
{ // over length of note array |
| 22 |
// set frequency and gain to turn on our note |
| 23 |
Std.mtof(myNotes[i]) => s.freq; // (8) Sets pitch for melody notes |
| 24 |
onGain => s.gain; // (9) Note on |
| 25 |
myDurs[i] => now; // (10) For duration stored in array for that note |
| 26 |
|
| 27 |
// turn off our note to separate from the next |
| 28 |
offGain => s.gain; // (11) Note off |
| 29 |
0.2::second => now; |
| 30 |
} |