| 1 |
// Listing 1.14 if/else code example |
| 2 |
|
| 3 |
// sound chain |
| 4 |
SinOsc s => dac; // Sine to dac, set frequency and gain. |
| 5 |
|
| 6 |
// set frequency |
| 7 |
220.0 => s.freq; |
| 8 |
// set volume |
| 9 |
0.5 => s.gain; |
| 10 |
|
| 11 |
// chance logical variable // (1) New integer named chance, |
| 12 |
3 => int chance; // initialized to value of 3. |
| 13 |
|
| 14 |
if (chance == 1) // (2) If chance is equal to 1... |
| 15 |
{ |
| 16 |
// play first "twinkle" note if chance == 1 |
| 17 |
1 :: second => now; // (3) ...do this block... |
| 18 |
} |
| 19 |
else // (4) ...otherwise (else), set sine frequency to something |
| 20 |
{ // different and play for a longer time. |
| 21 |
// otherwise, play other, higher twinkle note |
| 22 |
330.0 => s.freq; |
| 23 |
// and play it for a much longer time |
| 24 |
3::second => now; |
| 25 |
} |
| 26 |
|
| 27 |
// set and play one note, an octave above "twinkle" |
| 28 |
440.0 => s.freq; // After the if/else, play another, much higher note |
| 29 |
1 :: second => now; |