mirror of
https://github.com/thestk/stk
synced 2026-01-12 12:31:53 +00:00
112 lines
9.0 KiB
HTML
112 lines
9.0 KiB
HTML
<HTML>
|
|
<HEAD>
|
|
<TITLE>The Synthesis ToolKit in C++ (STK)</TITLE>
|
|
<LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">
|
|
</HEAD>
|
|
<BODY BGCOLOR="#FFFFFF">
|
|
<CENTER>
|
|
<img src="princeton.gif"> <img src="ccrma.gif"> <img src="mcgill.gif"><P>
|
|
<a class="qindex" href="index.html">Home</a> <a class="qindex" href="information.html">Information</a> <a class="qindex" href="classes.html">Classes</a> <a class="qindex" href="download.html">Download</a> <a class="qindex" href="usage.html">Usage</a> <a class="qindex" href="maillist.html">Mail List</a> <a class="qindex" href="system.html">Requirements</a> <a class="qindex" href="links.html">Links</a> <a class="qindex" href="faq.html">FAQ</a> <a class="qindex" href="tutorial.html">Tutorial</a></CENTER>
|
|
<HR>
|
|
<!-- Generated by Doxygen 1.5.8 -->
|
|
<div class="contents">
|
|
<h1><a class="anchor" name="hello">Hello Sine! </a></h1>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, <a class="el" href="classstk_1_1FileLoop.html" title="STK file looping / oscillator class.">stk::FileLoop</a>, 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 <a class="el" href="classstk_1_1FileWvOut.html" title="STK audio file output class.">stk::FileWvOut</a> to write the result to a 16-bit, WAV formatted audio file.<p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">// sineosc.cpp</span>
|
|
|
|
<span class="preprocessor">#include "FileLoop.h"</span>
|
|
<span class="preprocessor">#include "FileWvOut.h"</span>
|
|
<span class="keyword">using namespace </span>stk;
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
<span class="comment">// Set the global sample rate before creating class instances.</span>
|
|
Stk::setSampleRate( 44100.0 );
|
|
|
|
FileLoop input;
|
|
FileWvOut output;
|
|
|
|
<span class="comment">// Load the sine wave file.</span>
|
|
input.openFile( <span class="stringliteral">"rawwaves/sinewave.raw"</span>, <span class="keyword">true</span> );
|
|
|
|
<span class="comment">// Open a 16-bit, one-channel WAV formatted output file</span>
|
|
output.openFile( <span class="stringliteral">"hellosine.wav"</span>, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
|
|
|
|
input.setFrequency( 440.0 );
|
|
|
|
<span class="comment">// Run the oscillator for 40000 samples, writing to the output file</span>
|
|
<span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<40000; i++ )
|
|
output.tick( input.tick() );
|
|
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
</pre></div><p>
|
|
<a class="el" href="classstk_1_1FileLoop.html" title="STK file looping / oscillator class.">stk::FileLoop</a> is a subclass of <a class="el" href="classstk_1_1FileWvIn.html" title="STK audio file input class.">stk::FileWvIn</a>, 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. <a class="el" href="classstk_1_1FileWvIn.html" title="STK audio file input class.">stk::FileWvIn</a> provides interpolating, read-once ("oneshot") functionality, as well as methods for setting the read rate and read position.<p>
|
|
<a class="el" href="classstk_1_1FileWvIn.html" title="STK audio file input class.">stk::FileWvIn</a> provides a "tick level" and interpolating interface to the <a class="el" href="classstk_1_1FileRead.html" title="STK audio file input class.">stk::FileRead</a> class. Likewise, <a class="el" href="classstk_1_1FileWvOut.html" title="STK audio file output class.">stk::FileWvOut</a> provides a "tick level" interface to the <a class="el" href="classstk_1_1FileWrite.html" title="STK audio file output class.">stk::FileWrite</a> class. <a class="el" href="classstk_1_1FileRead.html" title="STK audio file input class.">stk::FileRead</a> 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. <a class="el" href="classstk_1_1FileWvOut.html" title="STK audio file output class.">stk::FileWvOut</a> does not currently offer data interpolation functionality.<p>
|
|
A number of STK parent classes, including <a class="el" href="classstk_1_1WvIn.html" title="STK audio input abstract base class.">stk::WvIn</a>, <a class="el" href="classstk_1_1WvOut.html" title="STK audio output abstract base class.">stk::WvOut</a>, <a class="el" href="classstk_1_1Instrmnt.html" title="STK instrument abstract base class.">stk::Instrmnt</a>, <a class="el" href="classstk_1_1Generator.html" title="STK abstract unit generator parent class.">stk::Generator</a>, and <a class="el" href="classstk_1_1Effect.html" title="STK abstract effects parent class.">stk::Effect</a>, (and some or all of their subclasses) support multi-channel sample frames. If a single-sample version of the <code>tick()</code> 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 <code>tick()</code> function is called for subclasses of <a class="el" href="classstk_1_1WvOut.html" title="STK audio output abstract base class.">stk::WvOut</a>, the sample argument is written to all channels in the one computed frame. For classes returning values, an optional <code>channel</code> 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 <code>tick()</code> functions taking StkFrames reference arguments should be used.<p>
|
|
Nearly all STK classes inherit from the <a class="el" href="classstk_1_1Stk.html" title="STK base class.">stk::Stk</a> 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.<h2><a class="anchor" name="error">
|
|
Error Handling</a></h2>
|
|
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.<p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">// sineosc.cpp STK tutorial program</span>
|
|
|
|
<span class="preprocessor">#include "FileLoop.h"</span>
|
|
<span class="preprocessor">#include "FileWvOut.h"</span>
|
|
<span class="keyword">using namespace </span>stk;
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
<span class="comment">// Set the global sample rate before creating class instances.</span>
|
|
Stk::setSampleRate( 44100.0 );
|
|
|
|
<span class="keywordtype">int</span> nFrames = 100000;
|
|
FileLoop input;
|
|
FileWvOut output;
|
|
|
|
<span class="keywordflow">try</span> {
|
|
<span class="comment">// Load the sine wave file.</span>
|
|
input.openFile( <span class="stringliteral">"rawwaves/sinewave.raw"</span>, <span class="keyword">true</span> );
|
|
|
|
<span class="comment">// Open a 16-bit, one-channel WAV formatted output file</span>
|
|
output.openFile( <span class="stringliteral">"hellosine.wav"</span>, 1, FileWrite::FILE_WAV, Stk::STK_SINT16 );
|
|
}
|
|
<span class="keywordflow">catch</span> ( StkError & ) {
|
|
exit( 1 );
|
|
}
|
|
|
|
input.setFrequency( 440.0 );
|
|
|
|
<span class="comment">// Option 1: Use StkFrames</span>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> StkFrames frames( nFrames, 1 );</span>
|
|
<span class="comment"> try {</span>
|
|
<span class="comment"> output.tick( input.tick( frames ) );</span>
|
|
<span class="comment"> }</span>
|
|
<span class="comment"> catch ( StkError & ) {</span>
|
|
<span class="comment"> exit( 1 );</span>
|
|
<span class="comment"> }</span>
|
|
<span class="comment"> */</span>
|
|
|
|
<span class="comment">// Option 2: Single-sample computations</span>
|
|
<span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<nFrames; i++ ) {
|
|
<span class="keywordflow">try</span> {
|
|
output.tick( input.tick() );
|
|
}
|
|
<span class="keywordflow">catch</span> ( StkError & ) {
|
|
exit( 1 );
|
|
}
|
|
}
|
|
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
</pre></div><p>
|
|
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 <a class="el" href="classes.html">Class Documentation</a> to determine which constructors and functions can throw an error.<p>
|
|
[<a href="fundamentals.html">Main tutorial page</a>] [<a href="compile.html">Next tutorial</a>] </div>
|
|
<HR>
|
|
|
|
<table>
|
|
<tr><td><A HREF="http://ccrma.stanford.edu/software/stk/"><I>The Synthesis ToolKit in C++ (STK)</I></A></td></tr>
|
|
<tr><td>©1995-2009 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
|
|
</table>
|
|
|
|
</BODY>
|
|
</HTML>
|