| 1 |
// Listing 5.15 Recursive (function that calls itself) scale-playing function |
| 2 |
|
| 3 |
// sound chain, mandolin to audio out |
| 4 |
Mandolin mand => dac; // (1) Mandolin instrument |
| 5 |
|
| 6 |
// recursive scale player |
| 7 |
fun int recurScale(int note, dur rate) { // (2) recurScale function definition |
| 8 |
Std.mtof(note) => mand.freq; // (3) Sets frequency of mandolin note |
| 9 |
1 => mand.noteOn; // (4) Plays note using noteOn |
| 10 |
rate => now; // (5) Waits for duration rate |
| 11 |
|
| 12 |
// only do it until some limit is reached |
| 13 |
if (note > 40) // (6) Limit for recursion |
| 14 |
{ |
| 15 |
// here's the recursion, it calls itself! |
| 16 |
recurScale(note-1, 0.9*rate); // (7) recurScale can call recurScale! |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
// now play a couple of scales |
| 21 |
recurScale(60, 0.5 :: second); |
| 22 |
recurScale(67, 1.0 :: second); |