Files
stk/doc/html/instruments.html
2013-09-29 23:38:16 +02:00

83 lines
5.2 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"> &nbsp; <img src="ccrma.gif"> &nbsp; <img src="mcgill.gif"><P>
<a class="qindex" href="index.html">Home</a> &nbsp; <a class="qindex" href="information.html">Information</a> &nbsp; <a class="qindex" href="classes.html">Classes</a> &nbsp; <a class="qindex" href="download.html">Download</a> &nbsp; <a class="qindex" href="usage.html">Usage</a> &nbsp; <a class="qindex" href="maillist.html">Mail List</a> &nbsp; <a class="qindex" href="system.html">Requirements</a> &nbsp; <a class="qindex" href="links.html">Links</a> &nbsp; <a class="qindex" href="tutorial.html">Tutorial</a></CENTER>
<HR>
<!-- Generated by Doxygen 1.3.4 -->
<h1><a class="anchor" name="instruments">Instruments</a></h1>The ToolKit comes with a wide variety of synthesis algorithms, all of which inherit from the <a class="el" href="classInstrmnt.html">Instrmnt</a> class. In this example, we'll fire up an instance of the <a class="el" href="classBeeThree.html">BeeThree</a> <a class="el" href="classFM.html">FM</a> synthesis class and show how it's frequency can be modified over time.<p>
<div class="fragment"><pre><span class="comment">// bethree.cpp</span>
<span class="preprocessor">#include "BeeThree.h"</span>
<span class="preprocessor">#include "RtWvOut.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="classInstrmnt.html">Instrmnt</a> *instrument = 0;
<a class="code" href="classRtWvOut.html">RtWvOut</a> *output = 0;
MY_FLOAT frequency, amplitude, scaler;
<span class="keywordtype">long</span> counter, i;
<span class="keywordflow">try</span> {
<span class="comment">// Define and load the BeeThree instrument</span>
instrument = <span class="keyword">new</span> <a class="code" href="classBeeThree.html">BeeThree</a>();
<span class="comment">// Define and open the default realtime output device for one-channel playback</span>
output = <span class="keyword">new</span> <a class="code" href="classRtWvOut.html">RtWvOut</a>(1);
}
<span class="keywordflow">catch</span> (<a class="code" href="classStkError.html">StkError</a> &amp;) {
<span class="keywordflow">goto</span> cleanup;
}
scaler = 1.0;
frequency = 220.0;
amplitude = 0.5;
instrument-&gt;<a class="code" href="classInstrmnt.html#a2">noteOn</a>( frequency, amplitude );
<span class="comment">// Play the instrument for 80000 samples, changing the frequency every 2000 samples</span>
counter = 0;
<span class="keywordflow">while</span> ( counter &lt; 80000 ) {
<span class="keywordflow">for</span> ( i=0; i&lt;2000; i++ ) {
<span class="keywordflow">try</span> {
output-&gt;<a class="code" href="classRtWvOut.html#a6">tick</a>( instrument-&gt;<a class="code" href="classInstrmnt.html#a8">tick</a>() );
}
<span class="keywordflow">catch</span> (<a class="code" href="classStkError.html">StkError</a> &amp;) {
<span class="keywordflow">goto</span> cleanup;
}
}
counter += 2000;
scaler += 0.025;
instrument-&gt;<a class="code" href="classInstrmnt.html#a4">setFrequency</a>( frequency * scaler );
}
<span class="comment">// Turn the instrument off with maximum decay envelope.</span>
instrument-&gt;<a class="code" href="classInstrmnt.html#a3">noteOff</a>( 1.0 );
cleanup:
<span class="keyword">delete</span> instrument;
<span class="keyword">delete</span> output;
<span class="keywordflow">return</span> 0;
}
</pre></div><p>
We have used an <a class="el" href="classInstrmnt.html">Instrmnt</a> pointer when referencing the <a class="el" href="classBeeThree.html">BeeThree</a> instance above, so it would be simple to replace the <a class="el" href="classBeeThree.html">BeeThree</a> class with any other STK instrument class. It should be noted, however, that a few classes do not respond to the setFrequency() function (e.g., <a class="el" href="classShakers.html">Shakers</a>, <a class="el" href="classDrummer.html">Drummer</a>).<p>
The noteOn() function initiates an instrument attack. Instruments which are continuously excited (e.g., <a class="el" href="classClarinet.html">Clarinet</a>, <a class="el" href="classBeeThree.html">BeeThree</a>) will continue to sound until stopped with a noteOff(). Impulsively excited instrument sounds (e.g., <a class="el" href="classPlucked.html">Plucked</a>, <a class="el" href="classWurley.html">Wurley</a>) typically decay within a few seconds time, requiring subsequent noteOn() messages for re-attack.<p>
Instrument parameters can be precisely controlled as demonstrated above. A more flexible approach to instrument control, allowing arbitrary scorefile or realtime updates, is described in the next tutorial chapter.<p>
[<a href="controlin.html">Next tutorial</a>] &nbsp; [<a href="tutorial.html">Main tutorial page</a>] <HR>
<table>
<tr><td><A HREF="http://www-ccrma.stanford.edu/software/stk/"><I>The Synthesis ToolKit in C++ (STK)</I></A></td></tr>
<tr><td>&copy;1995-2004 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
</table>
</BODY>
</HTML>