The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

Hello Sine!

We'll continue our introduction to the Synthesis ToolKit with a simple sine-wave oscillator program. STK provides two different classes for sine-wave generation. We will first look at a generic waveform oscillator class, stk::FileLoop, that can load a variety of common file types. In this example, we load a sine "table" from an STK RAW file (defined as monophonic, 16-bit, big-endian data). We use the class stk::FileWvOut to write the result to a 16-bit, WAV formatted audio file.

// sineosc.cpp
#include "FileLoop.h"
#include "FileWvOut.h"
using namespace stk;
int main()
{
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
FileLoop input;
FileWvOut output;
// Load the sine wave file.
input.openFile( "rawwaves/sinewave.raw", true );
// Open a 16-bit, one-channel WAV formatted output file
output.openFile( "hellosine.wav", 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
input.setFrequency( 440.0 );
// Run the oscillator for 40000 samples, writing to the output file
for ( int i=0; i<40000; i++ )
output.tick( input.tick() );
return 0;
}

stk::FileLoop is a subclass of stk::FileWvIn, which supports WAV, SND (AU), AIFF, MAT-file (Matlab), and RAW file formats with 8-, 16-, and 32-bit integer and 32- and 64-bit floating-point data types. stk::FileWvIn provides interpolating, read-once ("oneshot") functionality, as well as methods for setting the read rate and read position.

stk::FileWvIn provides a "tick level" and interpolating interface to the stk::FileRead class. Likewise, stk::FileWvOut provides a "tick level" interface to the stk::FileWrite class. stk::FileRead and FileWrite both support WAV, SND(AU), AIFF, MAT-file (Matlab), and RAW file formats with 8-, 16-, and 32-bit integer and 32- and 64-bit floating-point data types. stk::FileWvOut does not currently offer data interpolation functionality.

A number of STK parent classes, including stk::WvIn, stk::WvOut, stk::Instrmnt, stk::Generator, and stk::Effect, (and some or all of their subclasses) support multi-channel sample frames. If a single-sample version of the `tick()` function is called for these classes, a full sample frame is computed but only a single value is either input and/or output. For example, if the single-sample `tick()` function is called for subclasses of stk::WvOut, the sample argument is written to all channels in the one computed frame. For classes returning values, an optional `channel` argument specifies which channel value is returned from the computed frame (the default is always channel 0). To input and/or output multichannel data to these classes, the overloaded `tick()` functions taking StkFrames reference arguments should be used.

Nearly all STK classes inherit from the stk::Stk base class. Stk provides a static sample rate that is queried by subclasses as needed. Because many classes use the current sample rate value during instantiation, it is important that the desired value be set at the beginning of a program. The default STK sample rate is 44100 Hz.

# Error Handling

The ToolKit has some basic C++ error handling functionality built in. Classes that access files and/or hardware are most prone to runtime errors. To properly "catch" such errors, the above example should be rewritten as shown below.

// sineosc.cpp STK tutorial program
#include "FileLoop.h"
#include "FileWvOut.h"
#include <cstdlib>
using namespace stk;
int main()
{
// Set the global sample rate before creating class instances.
Stk::setSampleRate( 44100.0 );
int nFrames = 100000;
FileLoop input;
FileWvOut output;
try {
// Load the sine wave file.
input.openFile( "rawwaves/sinewave.raw", true );
// Open a 16-bit, one-channel WAV formatted output file
output.openFile( "hellosine.wav", 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
}
catch ( StkError & ) {
exit( 1 );
}
input.setFrequency( 440.0 );
// Option 1: Use StkFrames
/*
StkFrames frames( nFrames, 1 );
try {
output.tick( input.tick( frames ) );
}
catch ( StkError & ) {
exit( 1 );
}
*/
// Option 2: Single-sample computations
for ( int i=0; i<nFrames; i++ ) {
try {
output.tick( input.tick() );
}
catch ( StkError & ) {
exit( 1 );
}
}
return 0;
}

In this particular case, we simply exit the program if an error occurs (an error message is automatically printed to stderr). A more refined program might attempt to recover from or fix a particular problem and, if successful, continue processing. See the Class Documentation to determine which constructors and functions can throw an error.

[Main tutorial page] [Next tutorial]

stk::FileWvOut::tick
void tick(const StkFloat sample)
Output a single sample to all channels in a sample frame.
stk::FileLoop::tick
StkFloat tick(unsigned int channel=0)
Compute a sample frame and return the specified channel value.
stk::Stk::setSampleRate
static void setSampleRate(StkFloat rate)
Static method that sets the STK sample rate.
stk::Stk::STK_SINT16
static const StkFormat STK_SINT16
Definition: Stk.h:138
stk::FileLoop
STK file looping / oscillator class.
Definition: FileLoop.h:26
stk::FileLoop::setFrequency
void setFrequency(StkFloat frequency)
Set the data interpolation rate based on a looping frequency.
Definition: FileLoop.h:101
stk::FileWvOut
STK audio file output class.
Definition: FileWvOut.h:32
stk::FileWrite::FILE_WAV
static const FILE_TYPE FILE_WAV
Definition: FileWrite.h:38
stk::FileLoop::openFile
void openFile(std::string fileName, bool raw=false, bool doNormalize=true, bool doInt2FloatScaling=true)
Open the specified file and load its data.
stk
The STK namespace.
Definition: ADSR.h:6
stk::FileWvOut::openFile
void openFile(std::string fileName, unsigned int nChannels, FileWrite::FILE_TYPE type, Stk::StkFormat format)
Open a new file with the specified parameters.
stk::StkError
STK error handling class.
Definition: Stk.h:85

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