| 1 |
// Listing 9.2 Simple resonant pop class wraps UGens and functions for accessing them |
| 2 |
|
| 3 |
// Simple example of a resonant pop sound-making class |
| 4 |
public class Simple { // (1) Declares a new class called Simple |
| 5 |
// our Simple instrument patch |
| 6 |
Impulse imp => ResonZ filt => dac; // (2) Impulse through resonant filter to dac |
| 7 |
// some default settings |
| 8 |
100.0 => filt.Q => filt.gain; |
| 9 |
1000.0 => filt.freq; |
| 10 |
|
| 11 |
// set freq as we would any instrument |
| 12 |
fun void freq(float freq) // (3) Frequency setting method |
| 13 |
{ |
| 14 |
freq => filt.freq; |
| 15 |
} |
| 16 |
|
| 17 |
// method to allow setting Q |
| 18 |
fun void setQ(float Q) // (4) Resonance (Q) setting method |
| 19 |
{ |
| 20 |
Q => filt.Q; |
| 21 |
} |
| 22 |
|
| 23 |
// method to allow setting gain |
| 24 |
fun void setGain(float gain) // (5) Gain setting method |
| 25 |
{ |
| 26 |
filt.Q() * gain => imp.gain; |
| 27 |
} |
| 28 |
|
| 29 |
// play a note by firing impulse |
| 30 |
fun void noteOn(float volume) // (6) noteOn sets impulse to make a click |
| 31 |
{ |
| 32 |
volume => imp.next; |
| 33 |
} |
| 34 |
} |
| 35 |
// Make an instance of (declare) one of our Simples |
| 36 |
Simple s; // (7) Declares a new Simple instrument |
| 37 |
|
| 38 |
while (1) { // (8) Infinite loop |
| 39 |
// random frequency |
| 40 |
Std.rand2f(1100.0,1200.0) => s.freq; // (9) Random pitch frequency for pop |
| 41 |
|
| 42 |
// play a note and wait a bit |
| 43 |
1 => s.noteOn; // (10) Triggers a simple pop sound |
| 44 |
0.1 :: second => now; |
| 45 |
} |
| 46 |
|