Skip to content

Building your own Instrument

Phil Schatzmann edited this page Jul 7, 2024 · 15 revisions

It is very easy to build your own instrument: just create a new subclass of Instrmnt that provides the following methods:

  • noteOn
  • noteOff
  • tick

The tick() can be created from Generators combined with some Effects and/or Filters.

To handle the noteOn and noteOff you could simply set the amplitude to 1.0, if it is on or 0.0, if it is off. It is better however to handle this dynamically with the help of some Envelope Generators: ADSR, Asymp, Envelope.

Implementation

class MyFirstInstrument public Instrmnt  {
  public:
    MyFirstInstrument() {
      adsr.setAllTimes( 0.005, 0.01, 0.8, 0.010 );
      echo.setDelay(1024);
    }

     //! Start a note with the given frequency and amplitude.
    void noteOn( StkFloat frequency, StkFloat amplitude ) {
       wave.setFrequency(frequency);
       adsr.keyOn();
    }

     //! Stop a note with the given amplitude (speed of decay).
    void noteOff( StkFloat amplitude ){
       adsr.keyOff();
    }

    float tick(unsigned int channel = 0) {
       return wave.tick()*adsr.tick();
    }

  protected:
    SineWave wave;
    ADSR adsr;
};

Example

An example can be found on Github

Clone this wiki locally