| 1 |
// Listing 5.8 Main program uses swell to expressively play a melody |
| 2 |
|
| 3 |
// swell function, operates on any type of UGen |
| 4 |
fun void swell(UGen osc, float begin, float end, float step) |
| 5 |
{ // (1) swell function definition |
| 6 |
float val; |
| 7 |
|
| 8 |
// swell up volume |
| 9 |
for (begin => val; val < end; step +=> val) // (2) for loop to ramp up volume |
| 10 |
{ |
| 11 |
val => osc.gain; |
| 12 |
0.01 :: second => now; |
| 13 |
} |
| 14 |
|
| 15 |
// swell down volume |
| 16 |
while (val > begin) // (3) while loop to ramp it back down |
| 17 |
{ |
| 18 |
val => osc.gain; |
| 19 |
step -=> val; |
| 20 |
0.01:: second => now; |
| 21 |
} |
| 22 |
} |
| 23 |
|
| 24 |
// Main program |
| 25 |
// our sound "patch" |
| 26 |
TriOsc tri => dac; // (1) Triangle oscillator to test swell function |
| 27 |
|
| 28 |
// global (2) Array of notes to play |
| 29 |
[60,62,63,65,63,64,65,58,57,56] @=> int notes[]; |
| 30 |
|
| 31 |
// Swell through all notes |
| 32 |
for (0 => int i; i < notes.cap(); i++) { // (3) Iterates through notes array |
| 33 |
Std.mtof(notes[i]) => tri.freq; // (4) Sets frequency |
| 34 |
swell(tri,0.2,1.0,0.01); // (5) Calls swell function |
| 35 |
} |