mirror of
https://github.com/thestk/stk
synced 2026-01-13 04:51:53 +00:00
90 lines
8.2 KiB
HTML
90 lines
8.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"> <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="faq.html">FAQ</a> <a class="qindex" href="tutorial.html">Tutorial</a></CENTER>
|
|
<HR>
|
|
<!-- Generated by Doxygen 1.6.2 -->
|
|
<div class="contents">
|
|
|
|
|
|
<h1><a class="anchor" id="realtime">Realtime Audio (blocking) </a></h1><p>In this section, we modify the <code>sineosc.cpp</code> program in order to send the output to the default audio playback device on your computer system. We also make use of the <a class="el" href="classstk_1_1SineWave.html" title="STK sinusoid oscillator class.">stk::SineWave</a> class as a sine-wave oscillator. <a class="el" href="classstk_1_1SineWave.html" title="STK sinusoid oscillator class.">stk::SineWave</a> computes an internal, static sine-wave table when its first instance is created. Subsequent instances make use of the same table. The default table length, specified in <a class="el" href="SineWave_8h_source.html">SineWave.h</a>, is 2048 samples.</p>
|
|
<div class="fragment"><pre class="fragment"><span class="comment">// rtsine.cpp STK tutorial program</span>
|
|
|
|
<span class="preprocessor">#include "SineWave.h"</span>
|
|
<span class="preprocessor">#include "RtWvOut.h"</span>
|
|
<span class="preprocessor">#include <cstdlib></span>
|
|
|
|
<span class="keyword">using namespace </span>stk;
|
|
|
|
<span class="keywordtype">int</span> main()
|
|
{
|
|
<span class="comment">// Set the global sample rate before creating class instances.</span>
|
|
Stk::setSampleRate( 44100.0 );
|
|
Stk::showWarnings( <span class="keyword">true</span> );
|
|
|
|
<span class="keywordtype">int</span> nFrames = 100000;
|
|
SineWave sine;
|
|
RtWvOut *dac = 0;
|
|
|
|
<span class="keywordflow">try</span> {
|
|
<span class="comment">// Define and open the default realtime output device for one-channel playback</span>
|
|
dac = <span class="keyword">new</span> RtWvOut( 1 );
|
|
}
|
|
<span class="keywordflow">catch</span> ( StkError & ) {
|
|
exit( 1 );
|
|
}
|
|
|
|
sine.setFrequency( 441.0 );
|
|
|
|
<span class="comment">// Option 1: Use StkFrames</span>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> StkFrames frames( nFrames, 1 );</span>
|
|
<span class="comment"> try {</span>
|
|
<span class="comment"> dac->tick( sine.tick( frames ) );</span>
|
|
<span class="comment"> }</span>
|
|
<span class="comment"> catch ( StkError & ) {</span>
|
|
<span class="comment"> goto cleanup;</span>
|
|
<span class="comment"> }</span>
|
|
<span class="comment"> */</span>
|
|
|
|
<span class="comment">// Option 2: Single-sample computations</span>
|
|
<span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<nFrames; i++ ) {
|
|
<span class="keywordflow">try</span> {
|
|
dac->tick( sine.tick() );
|
|
}
|
|
<span class="keywordflow">catch</span> ( StkError & ) {
|
|
<span class="keywordflow">goto</span> cleanup;
|
|
}
|
|
}
|
|
|
|
cleanup:
|
|
<span class="keyword">delete</span> dac;
|
|
|
|
<span class="keywordflow">return</span> 0;
|
|
}
|
|
</pre></div><p>The class <a class="el" href="classstk_1_1RtWvOut.html" title="STK realtime audio (blocking) output class.">stk::RtWvOut</a> is a protected subclass of <a class="el" href="classstk_1_1WvOut.html" title="STK audio output abstract base class.">stk::WvOut</a>. A number of optional constructor arguments can be used to fine tune its performance for a given system. <a class="el" href="classstk_1_1RtWvOut.html" title="STK realtime audio (blocking) output class.">stk::RtWvOut</a> provides a "single-sample", blocking interface to the <a class="el" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> class. Note that <a class="el" href="classstk_1_1RtWvOut.html" title="STK realtime audio (blocking) output class.">stk::RtWvOut</a> (as well as the <a class="el" href="classstk_1_1RtWvIn.html" title="STK realtime audio (blocking) input class.">stk::RtWvIn</a> class described below) makes use of RtAudio's callback input/output functionality by creating a large ring-buffer into which data is written. These classes should not be used when low-latency and robust performance is necessary</p>
|
|
<p>Though not used here, an <a class="el" href="classstk_1_1RtWvIn.html" title="STK realtime audio (blocking) input class.">stk::RtWvIn</a> class exists as well that can be used to read realtime audio data from an input device. See the <code>record.cpp</code> example program in the <code>examples</code> project for more information.</p>
|
|
<p>It may be possible to use an instance of <a class="el" href="classstk_1_1RtWvOut.html" title="STK realtime audio (blocking) output class.">stk::RtWvOut</a> and an instance of <a class="el" href="classstk_1_1RtWvIn.html" title="STK realtime audio (blocking) input class.">stk::RtWvIn</a> to simultaneously read and write realtime audio to and from a hardware device or devices. However, it is recommended to instead use a single instance of <a class="el" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> to achieve this behavior, as described in the next section. See the <code>effects</code> project or the <code>duplex.cpp</code> example program in the <code>examples</code> project for more information.</p>
|
|
<p>When using any realtime STK class (<a class="el" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a>, <a class="el" href="classstk_1_1RtWvOut.html" title="STK realtime audio (blocking) output class.">stk::RtWvOut</a>, <a class="el" href="classstk_1_1RtWvIn.html" title="STK realtime audio (blocking) input class.">stk::RtWvIn</a>, <a class="el" href="classRtMidi.html" title="An abstract base class for realtime MIDI input/output.">RtMidi</a>, <a class="el" href="classstk_1_1InetWvIn.html" title="STK internet streaming input class.">stk::InetWvIn</a>, <a class="el" href="classstk_1_1InetWvOut.html" title="STK internet streaming output class.">stk::InetWvOut</a>, <a class="el" href="classstk_1_1Socket.html" title="STK internet socket abstract base class.">stk::Socket</a>, <a class="el" href="classstk_1_1UdpSocket.html" title="STK UDP socket server/client class.">stk::UdpSocket</a>, <a class="el" href="classstk_1_1TcpServer.html" title="STK TCP socket server class.">stk::TcpServer</a>, <a class="el" href="classstk_1_1TcpClient.html" title="STK TCP socket client class.">stk::TcpClient</a>, and <a class="el" href="classstk_1_1Thread.html" title="STK thread class.">stk::Thread</a>), it is necessary to specify an audio/MIDI API preprocessor definition and link with the appropriate libraries or frameworks. For example, the above program could be compiled on a Linux system using the GNU g++ compiler and the ALSA audio API as follows (assuming all necessary files exist in the project directory):</p>
|
|
<div class="fragment"><pre class="fragment">g++ -Wall -D__LINUX_ALSA__ -D__LITTLE_ENDIAN__ -o rtsine Stk.cpp Generator.cpp SineWave.cpp WvOut.cpp \
|
|
RtWvOut.cpp <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a>.cpp rtsine.cpp -lpthread -lasound
|
|
</pre></div><p>On a Macintosh OS X system, the syntax would be:</p>
|
|
<div class="fragment"><pre class="fragment">g++ -Wall -D__MACOSX_CORE__ -o rtsine Stk.cpp Generator.cpp SineWave.cpp WvOut.cpp RtWvOut.cpp <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a>.cpp \
|
|
rtsine.cpp -lpthread -framework CoreAudio -framework CoreMIDI -framework CoreFoundation
|
|
</pre></div><p>[<a href="tutorial.html">Main tutorial page</a>] [<a href="crealtime.html">Next tutorial</a>] </p>
|
|
</div>
|
|
<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-2010 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
|
|
</table>
|
|
|
|
</BODY>
|
|
</HTML>
|