mirror of
https://github.com/thestk/stk
synced 2026-01-13 13:01:52 +00:00
99 lines
7.9 KiB
HTML
99 lines
7.9 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.4.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 provides two different classes for sine-wave generation. We will first look at a generic waveform oscillator class, <a class="el" href="classWaveLoop.html">WaveLoop</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="classFileWvOut.html">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 "WaveLoop.h"</span>
|
|
<span class="preprocessor">#include "FileWvOut.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;
|
|
<a class="code" href="classFileWvOut.html">FileWvOut</a> output;
|
|
|
|
<span class="comment">// Load the sine wave file.</span>
|
|
input.<a class="code" href="classWaveLoop.html#a3">openFile</a>( <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.<a class="code" href="classFileWvOut.html#a3">openFile</a>( <span class="stringliteral">"hellosine.wav"</span>, 1, <a class="code" href="classFileWrite.html#s1">FileWrite::FILE_WAV</a>, <a class="code" href="classStk.html#s1">Stk::STK_SINT16</a> );
|
|
|
|
input.<a class="code" href="classWaveLoop.html#a5">setFrequency</a>( 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.<a class="code" href="classWvOut.html#a6">tick</a>( input.<a class="code" href="classWvIn.html#a5">tick</a>() );
|
|
|
|
<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="classFileWvIn.html">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="classFileWvIn.html">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="classFileWvIn.html">FileWvIn</a> provides a "tick level" and interpolating interface to the <a class="el" href="classFileRead.html">FileRead</a> class. Likewise, <a class="el" href="classFileWvOut.html">FileWvOut</a> provides a "tick level" interface to the <a class="el" href="classFileWrite.html">FileWrite</a> class. <a class="el" href="classFileRead.html">FileRead</a> and <a class="el" href="classFileWrite.html">FileWrite</a> 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="classFileWvOut.html">FileWvOut</a> does not currently offer data interpolation functionality.<p>
|
|
The <a class="el" href="classWvIn.html">WvIn</a> and <a class="el" href="classWvOut.html">WvOut</a> 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 <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 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 "WaveLoop.h"</span>
|
|
<span class="preprocessor">#include "FileWvOut.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;
|
|
<a class="code" href="classFileWvOut.html">FileWvOut</a> output;
|
|
|
|
<span class="keywordflow">try</span> {
|
|
<span class="comment">// Load the sine wave file.</span>
|
|
input.<a class="code" href="classWaveLoop.html#a3">openFile</a>( <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.<a class="code" href="classFileWvOut.html#a3">openFile</a>( <span class="stringliteral">"hellosine.wav"</span>, 1, <a class="code" href="classFileWrite.html#s1">FileWrite::FILE_WAV</a>, <a class="code" href="classStk.html#s1">Stk::STK_SINT16</a> );
|
|
}
|
|
<span class="keywordflow">catch</span> ( <a class="code" href="classStkError.html">StkError</a> & ) {
|
|
exit(0);
|
|
}
|
|
|
|
input.<a class="code" href="classWaveLoop.html#a5">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#a6">tick</a>( input.<a class="code" href="classWvIn.html#a5">tick</a>() );
|
|
}
|
|
<span class="keywordflow">catch</span> ( <a class="code" href="classStkError.html">StkError</a> & ) {
|
|
exit(0);
|
|
}
|
|
|
|
}
|
|
|
|
<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>] <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-2005 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
|
|
</table>
|
|
|
|
</BODY>
|
|
</HTML>
|