Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   Tutorial


Using 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.

// beethree.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;

  try {
    // Define and load the sine wave file
    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
  for (int i=0; i<80000; i++) {
    try {
      output->tick( instrument->tick() );
    }
    catch (StkError &) {
      goto cleanup;
    }

    if ( i % 2000 == 0 ) {
      scaler += 0.025;
      instrument->setFrequency( frequency * scaler );
    }
  }

 cleanup:
  delete instrument;
  delete output;

  return 0;
}

By using an Instrmnt pointer above, it is possible 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 (Shakers, Drummer).


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