Home Information Classes Download Usage Mail List Requirements Links Tutorial

# Instruments

The ToolKit comes with a wide variety of synthesis algorithms, all of which inherit from the Instrmnt class. In this example, we'll fire up an instance of the BeeThree FM synthesis class and show how it's frequency can be modified over time.

    // bethree.cpp

    #include "BeeThree.h"
    #include "RtWvOut.h"

    int main()
    {
      // Set the global sample rate before creating class instances.
      Stk::setSampleRate( 44100.0 );

      Instrmnt *instrument = 0;
      RtWvOut *output = 0;
      MY_FLOAT frequency, amplitude, scaler;
      long counter, i;

      try {
        // Define and load the BeeThree instrument
        instrument = new BeeThree();

        // Define and open the default realtime output device for one-channel playback
        output = new RtWvOut(1);
      }
      catch (StkError &) {
        goto cleanup;
      }

      scaler = 1.0;
      frequency = 220.0;
      amplitude = 0.5;
      instrument->noteOn( frequency, amplitude );

      // Play the instrument for 80000 samples, changing the frequency every 2000 samples
      counter = 0;
      while ( counter < 80000 ) {
        for ( i=0; i<2000; i++ ) {
          try {
            output->tick( instrument->tick() );
          }
          catch (StkError &) {
            goto cleanup;
          }
        }

        counter += 2000;
        scaler += 0.025;
        instrument->setFrequency( frequency * scaler );
      }

      // Turn the instrument off with maximum decay envelope.
      instrument->noteOff( 1.0 );

     cleanup:
      delete instrument;
      delete output;

      return 0;
    }

We have used an Instrmnt pointer when referencing the BeeThree instance above, so it would be simple to replace the BeeThree class with any other STK instrument class. It should be noted, however, that a few classes do not respond to the setFrequency() function (e.g., Shakers, Drummer).

The noteOn() function initiates an instrument attack. Instruments which are continuously excited (e.g., Clarinet, BeeThree) will continue to sound until stopped with a noteOff(). Impulsively excited instrument sounds (e.g., Plucked, Wurley) typically decay within a few seconds time, requiring subsequent noteOn() messages for re-attack.

Instrument parameters can be precisely controlled as demonstrated above. A more flexible approach to instrument control, allowing arbitrary scorefile or realtime updates, is described in the next tutorial chapter.

[Next tutorial] [Main tutorial page]

The Synthesis ToolKit in C++ (STK)
©1995-2004 Perry R. Cook and Gary P. Scavone. All Rights Reserved.