| 1 |
// Listing 5.19 Playing chords on an array of SinOsc UGens, using a function |
| 2 |
|
| 3 |
// Sound Network |
| 4 |
SinOsc chord[3]; // (1) Three oscillators for a chord |
| 5 |
|
| 6 |
for (0 => int i; i < chord.cap(); i++) |
| 7 |
{ |
| 8 |
// connect each element of our array to dac |
| 9 |
chord[i] => dac; // (2) Connects them all to the dac... |
| 10 |
|
| 11 |
// adjust gain so we don't overload |
| 12 |
1.0/chord.cap() => chord[i].gain; // (3) ...and sets their gains so you don't overload. |
| 13 |
} |
| 14 |
|
| 15 |
fun void playChord(int root, string quality, dur howLong) |
| 16 |
{ |
| 17 |
// set root of chord |
| 18 |
Std.mtof(root) => chord[0].freq; // (4) Root of chord. |
| 19 |
|
| 20 |
// set fifth of chord |
| 21 |
Std.mtof(root+7) => chord[2].freq; // (5) Fifth of chord. |
| 22 |
|
| 23 |
// third sets quality, major or minor |
| 24 |
if (quality == "major") |
| 25 |
{ |
| 26 |
Std.mtof(root+4) => chord[1].freq; // (6) Major chord. |
| 27 |
} |
| 28 |
else if (quality == "minor") { |
| 29 |
Std.mtof(root+3) => chord[1].freq; // (7) Minor chord. |
| 30 |
} |
| 31 |
else |
| 32 |
{ |
| 33 |
<<< "You must specify major or minor!!" >>>; |
| 34 |
} |
| 35 |
howLong => now; |
| 36 |
} |
| 37 |
|
| 38 |
// Listing 5.20 Using playChord |
| 39 |
|
| 40 |
// Main program: now let's use playChord!! |
| 41 |
while (true) |
| 42 |
{ |
| 43 |
playChord(Std.rand2(70,82), "minor", second/2); // (1) Plays a minor chord on a random note |
| 44 |
playChord(60, "minor", second/2); // (2) Plays a C minor chord |
| 45 |
playChord(67, "major", second/2); // (3) Plays a G major chord |
| 46 |
} |