| 1 |
// Listing 9.18 A smarter MyClarinet that Inherits from Clarinet |
| 2 |
// A clarinet that understands MIDI note numbers in noteOn!! |
| 3 |
public class MyClarinet extends Clarinet // (1) Subclass of Clarinet UGen. |
| 4 |
{ |
| 5 |
// here we define one new function |
| 6 |
fun float noteOn(int noteNum, float volume) { // (2) New noteOn function with two arguments. |
| 7 |
// we set frequency using MIDI note Number |
| 8 |
Std.mtof(noteNum) => this.freq; // (3) Clarinet's built in .freq method... |
| 9 |
// then call the built-in inherited noteOn |
| 10 |
volume => this.noteOn; // (4) ...and uses Clarinet's noteOn to set volume // |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
// make a new instance of our special clarinet |
| 16 |
MyClarinet myclar => dac; |
| 17 |
|
| 18 |
// test our new noteOn function |
| 19 |
myclar.noteOn(60, 1.0); // (5) Tests it all out. |
| 20 |
|
| 21 |
second => now; |
| 22 |
1 => myclar.noteOff; |
| 23 |
second => now; |
| 24 |
|
| 25 |
// test our old noteOn function |
| 26 |
myclar.noteOn(1.0); // (6) The old Clarinet noteOn still works too! |
| 27 |
second => now; |