Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial
// sineosc.cpp #include "WaveLoop.h" #include "FileWvOut.h" int main() { // Set the global sample rate before creating class instances. Stk::setSampleRate( 44100.0 ); WaveLoop 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; }
WaveLoop is a subclass of 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. FileWvIn provides interpolating, read-once ("oneshot") functionality, as well as methods for setting the read rate and read position.
FileWvIn provides a "tick level" and interpolating interface to the FileRead class. Likewise, FileWvOut provides a "tick level" interface to the FileWrite class. 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. FileWvOut does not currently offer data interpolation functionality.
The WvIn and WvOut parent classes and all subclasses support multi-channel sample frames. To distinguish single-sample frame operations from multi-channel frame operations, these classes also implement tickFrame()
functions. When a tick()
method is called for multi-channel data, frame averages are returned or the input sample is distributed across all channels of a sample frame.
Nearly all STK classes inherit from the 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.
// sineosc.cpp STK tutorial program #include "WaveLoop.h" #include "FileWvOut.h" int main() { // Set the global sample rate before creating class instances. Stk::setSampleRate( 44100.0 ); WaveLoop 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 ); // Run the oscillator for 40000 samples, writing to the output file int i; for ( i=0; i<40000; 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]
The Synthesis ToolKit in C++ (STK) |
©1995-2007 Perry R. Cook and Gary P. Scavone. All Rights Reserved. |