The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

Multi-Channel I/O

The ToolKit stk::WvIn and stk::WvOut classes (and their subclasses) support multi-channel audio data input and output. Several other abstract base classes, such as stk::Instrmnt, stk::Generator, and stk::Effect, also support multi-channel computations though not all of their subclasses produce or take multi-channel data. A set of interleaved audio samples representing a single time "slice" is referred to as a sample frame. At a sample rate of 44.1 kHz, a four-channel audio stream will have 44100 sample frames per second and a total of 176400 individual samples per second.

Most STK classes process single-sample data streams via their `tick()` function. For classes supporting multi-channel data, one must distinguish the `tick()` functions taking or producing single `StkFloat` arguments from those taking stk::StkFrames& arguments. 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 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.

Multi-channel support for realtime audio input and output is dependent on the audio device(s) available on your system.

The following example demonstrates the use of the stk::FileWvOut class for creating a four channel, 16-bit AIFF formatted audio file. We will use four sinewaves of different frequencies for the first two seconds and then a single sinewave for the last two seconds.

// foursine.cpp STK tutorial program
#include "SineWave.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 i;
FileWvOut output;
SineWave inputs[4];
// Set the sine wave frequencies.
for ( i=0; i<4; i++ )
inputs[i].setFrequency( 220.0 * (i+1) );
// Define and open a 16-bit, four-channel AIFF formatted output file
try {
output.openFile( "foursine.aif", 4, FileWrite::FILE_AIF, Stk::STK_SINT16 );
}
catch (StkError &) {
exit( 1 );
}
// Write two seconds of four sines to the output file
StkFrames frames( 88200, 4 );
for ( i=0; i<4; i++ )
inputs[i].tick( frames, i );
output.tick( frames );
// Now write the first sine to all four channels for two seconds
for ( i=0; i<88200; i++ ) {
output.tick( inputs[0].tick() );
}
return 0;
}
stk::FileWvOut
STK audio file output class.
Definition FileWvOut.h:33
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::FileWvOut::tick
void tick(const StkFloat sample)
Output a single sample to all channels in a sample frame.
stk::SineWave
STK sinusoid oscillator class.
Definition SineWave.h:26
stk::StkError
STK error handling class.
Definition Stk.h:87
stk::StkFrames
An STK class to handle vectorized audio data.
Definition Stk.h:279
stk
The STK namespace.
Definition ADSR.h:6

[Main tutorial page] [Next tutorial]

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