mirror of
https://github.com/thestk/stk
synced 2026-01-14 05:21:53 +00:00
Release 4.2.0 tarball
This commit is contained in:
committed by
Stephen Sinclair
parent
fe20fe92a2
commit
de344668dd
@@ -9,61 +9,104 @@
|
||||
<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="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>
|
||||
<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 its frequency can be modified over time.<p>
|
||||
<div class="fragment"><pre><span class="comment">// bethree.cpp STK tutorial program</span>
|
||||
|
||||
<span class="preprocessor">#include "BeeThree.h"</span>
|
||||
<span class="preprocessor">#include "RtWvOut.h"</span>
|
||||
<span class="preprocessor">#include "RtAudio.h"</span>
|
||||
|
||||
<span class="comment">// The TickData structure holds all the class instances and data that</span>
|
||||
<span class="comment">// are shared by the various processing functions.</span>
|
||||
<span class="keyword">struct </span>TickData {
|
||||
<a class="code" href="classInstrmnt.html">Instrmnt</a> *instrument;
|
||||
StkFloat frequency;
|
||||
StkFloat scaler;
|
||||
<span class="keywordtype">long</span> counter;
|
||||
<span class="keywordtype">bool</span> done;
|
||||
|
||||
<span class="comment">// Default constructor.</span>
|
||||
TickData()
|
||||
: instrument(0), scaler(1.0), counter(0), done( false ) {}
|
||||
};
|
||||
|
||||
<span class="comment">// This tick() function handles sample computation only. It will be</span>
|
||||
<span class="comment">// called automatically when the system needs a new buffer of audio</span>
|
||||
<span class="comment">// samples.</span>
|
||||
<span class="keywordtype">int</span> tick(<span class="keywordtype">char</span> *buffer, <span class="keywordtype">int</span> bufferSize, <span class="keywordtype">void</span> *dataPointer)
|
||||
{
|
||||
TickData *data = (TickData *) dataPointer;
|
||||
<span class="keyword">register</span> StkFloat *samples = (StkFloat *) buffer;
|
||||
|
||||
<span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<bufferSize; i++ ) {
|
||||
*samples++ = data->instrument->tick();
|
||||
<span class="keywordflow">if</span> ( ++data->counter % 2000 == 0 ) {
|
||||
data->scaler += 0.025;
|
||||
data->instrument->setFrequency( data->frequency * data->scaler );
|
||||
}
|
||||
}
|
||||
|
||||
<span class="keywordflow">if</span> ( data->counter > 80000 )
|
||||
data->done = <span class="keyword">true</span>;
|
||||
|
||||
<span class="keywordflow">return</span> 0;
|
||||
}
|
||||
|
||||
<span class="keywordtype">int</span> main()
|
||||
{
|
||||
<span class="comment">// Set the global sample rate before creating class instances.</span>
|
||||
<span class="comment">// Set the global sample rate and rawwave path before creating class instances.</span>
|
||||
<a class="code" href="classStk.html#e1">Stk::setSampleRate</a>( 44100.0 );
|
||||
<a class="code" href="classStk.html#e3">Stk::setRawwavePath</a>( <span class="stringliteral">"../../rawwaves/"</span> );
|
||||
|
||||
<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;
|
||||
TickData data;
|
||||
RtAudio *dac = 0;
|
||||
|
||||
<span class="comment">// Figure out how many bytes in an StkFloat and setup the RtAudio object.</span>
|
||||
RtAudioFormat format = ( <span class="keyword">sizeof</span>(StkFloat) == 8 ) ? RTAUDIO_FLOAT64 : RTAUDIO_FLOAT32;
|
||||
<span class="keywordtype">int</span> bufferSize = RT_BUFFER_SIZE;
|
||||
<span class="keywordflow">try</span> {
|
||||
dac = <span class="keyword">new</span> RtAudio(0, 1, 0, 0, format, (<span class="keywordtype">int</span>)Stk::sampleRate(), &bufferSize, 4);
|
||||
}
|
||||
<span class="keywordflow">catch</span> (<a class="code" href="classRtError.html">RtError</a>& error) {
|
||||
error.printMessage();
|
||||
<span class="keywordflow">goto</span> cleanup;
|
||||
}
|
||||
|
||||
<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);
|
||||
data.instrument = <span class="keyword">new</span> <a class="code" href="classBeeThree.html">BeeThree</a>();
|
||||
}
|
||||
<span class="keywordflow">catch</span> (<a class="code" href="classStkError.html">StkError</a> &) {
|
||||
<span class="keywordflow">goto</span> cleanup;
|
||||
}
|
||||
|
||||
scaler = 1.0;
|
||||
frequency = 220.0;
|
||||
amplitude = 0.5;
|
||||
instrument-><a class="code" href="classInstrmnt.html#a2">noteOn</a>( frequency, amplitude );
|
||||
data.frequency = 220.0;
|
||||
data.instrument->noteOn( data.frequency, 0.5 );
|
||||
|
||||
<span class="comment">// Play the instrument for 80000 samples, changing the frequency every 2000 samples</span>
|
||||
counter = 0;
|
||||
<span class="keywordflow">while</span> ( counter < 80000 ) {
|
||||
<span class="keywordflow">for</span> ( i=0; i<2000; i++ ) {
|
||||
<span class="keywordflow">try</span> {
|
||||
output-><a class="code" href="classRtWvOut.html#a6">tick</a>( instrument-><a class="code" href="classInstrmnt.html#a8">tick</a>() );
|
||||
}
|
||||
<span class="keywordflow">catch</span> (<a class="code" href="classStkError.html">StkError</a> &) {
|
||||
<span class="keywordflow">goto</span> cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
counter += 2000;
|
||||
scaler += 0.025;
|
||||
instrument-><a class="code" href="classInstrmnt.html#a4">setFrequency</a>( frequency * scaler );
|
||||
<span class="keywordflow">try</span> {
|
||||
dac-><a class="code" href="classRtAudio.html#a6">setStreamCallback</a>(&tick, (<span class="keywordtype">void</span> *)&data);
|
||||
dac-><a class="code" href="classRtAudio.html#a13">startStream</a>();
|
||||
}
|
||||
<span class="keywordflow">catch</span> (<a class="code" href="classRtError.html">RtError</a> &error) {
|
||||
error.printMessage();
|
||||
<span class="keywordflow">goto</span> cleanup;
|
||||
}
|
||||
|
||||
<span class="comment">// Turn the instrument off with maximum decay envelope.</span>
|
||||
instrument-><a class="code" href="classInstrmnt.html#a3">noteOff</a>( 1.0 );
|
||||
<span class="comment">// Block waiting until callback signals done.</span>
|
||||
<span class="keywordflow">while</span> ( !data.done )
|
||||
<a class="code" href="classStk.html#e7">Stk::sleep</a>( 100 );
|
||||
|
||||
<span class="comment">// Shut down the callback and output stream.</span>
|
||||
<span class="keywordflow">try</span> {
|
||||
dac-><a class="code" href="classRtAudio.html#a7">cancelStreamCallback</a>();
|
||||
dac-><a class="code" href="classRtAudio.html#a12">closeStream</a>();
|
||||
}
|
||||
<span class="keywordflow">catch</span> (<a class="code" href="classRtError.html">RtError</a> &error) {
|
||||
error.printMessage();
|
||||
}
|
||||
|
||||
cleanup:
|
||||
<span class="keyword">delete</span> instrument;
|
||||
<span class="keyword">delete</span> output;
|
||||
<span class="keyword">delete</span> data.instrument;
|
||||
<span class="keyword">delete</span> dac;
|
||||
|
||||
<span class="keywordflow">return</span> 0;
|
||||
}
|
||||
@@ -74,7 +117,7 @@ Instrument parameters can be precisely controlled as demonstrated above. A more
|
||||
[<a href="controlin.html">Next tutorial</a>] [<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><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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user