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

90 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"><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.2.8.1 -->
<a name="instruments"><h2>Instruments</h2></a>
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><font class="comment">// bethree.cpp</font>
<font class="preprocessor">#include "BeeThree.h"</font>
<font class="preprocessor">#include "RtWvOut.h"</font>
<font class="keywordtype">int</font> main()<font class="keyword"></font>
<font class="keyword"></font>{
<font class="comment">// Set the global sample rate before creating class instances.</font>
<a class="code" href="classStk.html#d1">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;
<font class="keywordtype">long</font> counter, i;
<font class="keywordflow">try</font> {
<font class="comment">// Define and load the BeeThree instrument</font>
instrument = <font class="keyword">new</font> BeeThree();
<font class="comment">// Define and open the default realtime output device for one-channel playback</font>
output = <font class="keyword">new</font> RtWvOut(1);
}
<font class="keywordflow">catch</font> (<a class="code" href="classStkError.html">StkError</a> &amp;) {
<font class="keywordflow">goto</font> cleanup;
}
scaler = 1.0;
frequency = 220.0;
amplitude = 0.5;
instrument-&gt;<a class="code" href="classInstrmnt.html#a2">noteOn</a>( frequency, amplitude );
<font class="comment">// Play the instrument for 80000 samples, changing the frequency every 2000 samples</font>
counter = 0;
<font class="keywordflow">while</font> ( counter &lt; 80000 ) {
<font class="keywordflow">for</font> ( i=0; i&lt;2000; i++ ) {
<font class="keywordflow">try</font> {
output-&gt;<a class="code" href="classRtWvOut.html#a6">tick</a>( instrument-&gt;<a class="code" href="classInstrmnt.html#a6">tick</a>() );
}
<font class="keywordflow">catch</font> (<a class="code" href="classStkError.html">StkError</a> &amp;) {
<font class="keywordflow">goto</font> cleanup;
}
}
counter += 2000;
scaler += 0.025;
instrument-&gt;<a class="code" href="classInstrmnt.html#a4">setFrequency</a>( frequency * scaler );
}
<font class="comment">// Turn the instrument off with maximum decay envelope.</font>
instrument-&gt;<a class="code" href="classInstrmnt.html#a3">noteOff</a>( 1.0 );
cleanup:
<font class="keyword">delete</font> instrument;
<font class="keyword">delete</font> output;
<font class="keywordflow">return</font> 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>]
<p>
<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-2002 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
</table>
</BODY>
</HTML>