| 1 |
// Listing 3.4 Playing a melody stored 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 a[]; |
| 12 |
|
| 13 |
// loop for length of array |
| 14 |
for (0 => int i; i < a.cap(); i++) |
| 15 |
{ |
| 16 |
<<< i, a[i] >>>; // (4) Prints index and array note |
| 17 |
|
| 18 |
// set frequency and gain to turn on our note |
| 19 |
Std.mtof(a[i]) => s.freq; // (5) Sets pitch for melody notes |
| 20 |
onGain => s.gain; // (6) Note on |
| 21 |
0.3::second => now; // (7) Duration for note on |
| 22 |
|
| 23 |
// turn off our note to separate from the next |
| 24 |
offGain => s.gain; // (8) Note off |
| 25 |
0.2::second => now; |
| 26 |
} |