mirror of
https://github.com/thestk/stk
synced 2026-01-12 12:31:53 +00:00
105 lines
7.8 KiB
HTML
105 lines
7.8 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="tutorial.html">Tutorial</a></CENTER>
|
|
<HR>
|
|
<!-- Generated by Doxygen 1.3.4 -->
|
|
<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 does not provide a specific oscillator for sine waves. Instead, it provides a generic waveform oscillator class, <a class="el" href="classWaveLoop.html">WaveLoop</a>, which 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="classWvOut.html">WvOut</a> to write the result to a 16-bit, WAV formatted audio file.<p>
|
|
<div class="fragment"><pre><span class="comment">// sineosc.cpp</span>
|
|
|
|
<span class="preprocessor">#include "WaveLoop.h"</span>
|
|
<span class="preprocessor">#include "WvOut.h"</span>
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
<span class="comment">// Set the global sample rate before creating class instances.</span>
|
|
<a class="code" href="classStk.html#e1">Stk::setSampleRate</a>( 44100.0 );
|
|
|
|
<span class="comment">// Define and load the sine wave file</span>
|
|
<a class="code" href="classWaveLoop.html">WaveLoop</a>* input = <span class="keyword">new</span> <a class="code" href="classWaveLoop.html">WaveLoop</a>( <span class="stringliteral">"rawwaves/sinewave.raw"</span>, <span class="keyword">true</span> );
|
|
input-><a class="code" href="classWaveLoop.html#a2">setFrequency</a>( 440.0 );
|
|
|
|
<span class="comment">// Define and open a 16-bit, one-channel WAV formatted output file</span>
|
|
<a class="code" href="classWvOut.html">WvOut</a>* output = <span class="keyword">new</span> <a class="code" href="classWvOut.html">WvOut</a>( <span class="stringliteral">"hellosine.wav"</span>, 1, WvOut::WVOUT_WAV, Stk::STK_SINT16 );
|
|
|
|
<span class="comment">// Run the oscillator for 40000 samples, writing to the output file</span>
|
|
<span class="keywordtype">int</span> i;
|
|
<span class="keywordflow">for</span> ( i=0; i<40000; i++ ) {
|
|
output-><a class="code" href="classWvOut.html#a9">tick</a>( input-><a class="code" href="classWvIn.html#a16">tick</a>() );
|
|
}
|
|
|
|
<span class="comment">// Clean up</span>
|
|
<span class="keyword">delete</span> input;
|
|
<span class="keyword">delete</span> output;
|
|
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
</pre></div><p>
|
|
<a class="el" href="classWaveLoop.html">WaveLoop</a> is a subclass of <a class="el" href="classWvIn.html">WvIn</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="classWvIn.html">WvIn</a> provides interpolating, read once ("oneshot") functionality, as well as methods for setting the read rate and read position.<p>
|
|
The <a class="el" href="classWvIn.html">WvIn</a> and <a class="el" href="classWvOut.html">WvOut</a> classes are complementary, both supporting 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. However, <a class="el" href="classWvOut.html">WvOut</a> does not perform data interpolation.<p>
|
|
The <a class="el" href="classWvIn.html">WvIn</a> and <a class="el" href="classWvOut.html">WvOut</a> classes support multi-channel sample frames. To distinguish single-sample frame operations from multi-channel frame operations, these classes also implement <code>tickFrame()</code> functions. When a <code>tick()</code> method is called for multi-channel data, frame averages are returned or the input sample is distributed across all channels of a sample frame.<p>
|
|
Nearly all STK classes inherit from the <a class="el" href="classStk.html">Stk</a> base class. <a class="el" href="classStk.html">Stk</a> provides a static sample rate which 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 which 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><span class="comment">// sineosc.cpp STK tutorial program</span>
|
|
|
|
<span class="preprocessor">#include "WaveLoop.h"</span>
|
|
<span class="preprocessor">#include "WvOut.h"</span>
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
<span class="comment">// Set the global sample rate before creating class instances.</span>
|
|
<a class="code" href="classStk.html#e1">Stk::setSampleRate</a>( 44100.0 );
|
|
|
|
<a class="code" href="classWaveLoop.html">WaveLoop</a> *input = 0;
|
|
<a class="code" href="classWvOut.html">WvOut</a> *output = 0;
|
|
|
|
<span class="keywordflow">try</span> {
|
|
<span class="comment">// Define and load the sine wave file</span>
|
|
input = <span class="keyword">new</span> <a class="code" href="classWaveLoop.html">WaveLoop</a>( <span class="stringliteral">"rawwaves/sinewave.raw"</span>, <span class="keyword">true</span> );
|
|
|
|
<span class="comment">// Define and open a 16-bit, one-channel WAV formatted output file</span>
|
|
output = <span class="keyword">new</span> <a class="code" href="classWvOut.html">WvOut</a>( <span class="stringliteral">"hellosine.wav"</span>, 1, WvOut::WVOUT_WAV, Stk::STK_SINT16 );
|
|
}
|
|
<span class="keywordflow">catch</span> ( <a class="code" href="classStkError.html">StkError</a> & ) {
|
|
<span class="keywordflow">goto</span> cleanup;
|
|
}
|
|
|
|
input-><a class="code" href="classWaveLoop.html#a2">setFrequency</a>( 440.0 );
|
|
|
|
<span class="comment">// Run the oscillator for 40000 samples, writing to the output file</span>
|
|
<span class="keywordtype">int</span> i;
|
|
<span class="keywordflow">for</span> ( i=0; i<40000; i++ ) {
|
|
|
|
<span class="keywordflow">try</span> {
|
|
output-><a class="code" href="classWvOut.html#a9">tick</a>( input-><a class="code" href="classWvIn.html#a16">tick</a>() );
|
|
}
|
|
<span class="keywordflow">catch</span> ( <a class="code" href="classStkError.html">StkError</a> & ) {
|
|
<span class="keywordflow">goto</span> cleanup;
|
|
}
|
|
|
|
}
|
|
|
|
cleanup:
|
|
<span class="keyword">delete</span> input;
|
|
<span class="keyword">delete</span> output;
|
|
|
|
<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="compile.html">Next tutorial</a>] [<a href="fundamentals.html">Main tutorial page</a>] <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-2004 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
|
|
</table>
|
|
|
|
</BODY>
|
|
</HTML>
|