Files
stk/doc/html/realtime.html
2013-09-29 23:39:37 +02:00

77 lines
6.8 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="realtime">Realtime Audio (blocking)</a></h1>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.<p>
<div class="fragment"><pre><span class="comment">// rtsine.cpp STK tutorial program</span>
<span class="preprocessor">#include "WaveLoop.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="classWaveLoop.html">WaveLoop</a> *sine = 0;
<a class="code" href="classRtWvOut.html">RtWvOut</a> *dac = 0;
<span class="keywordflow">try</span> {
<span class="comment">// Define and load the sine wave file</span>
sine = <span class="keyword">new</span> <a class="code" href="classWaveLoop.html">WaveLoop</a>( <span class="stringliteral">"rawwaves/sinewave.raw"</span>, <span class="keyword">true</span> );
<span class="comment">// Define and open the default realtime output device for one-channel playback</span>
dac = <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;
}
sine-&gt;<a class="code" href="classWaveLoop.html#a2">setFrequency</a>(440.0);
<span class="comment">// Play the oscillator for 40000 samples</span>
<span class="keywordtype">int</span> i;
<span class="keywordflow">for</span> ( i=0; i&lt;40000; i++ ) {
<span class="keywordflow">try</span> {
dac-&gt;<a class="code" href="classRtWvOut.html#a6">tick</a>( sine-&gt;<a class="code" href="classWvIn.html#a16">tick</a>() );
}
<span class="keywordflow">catch</span> (<a class="code" href="classStkError.html">StkError</a> &amp;) {
<span class="keywordflow">goto</span> cleanup;
}
}
cleanup:
<span class="keyword">delete</span> sine;
<span class="keyword">delete</span> dac;
<span class="keywordflow">return</span> 0;
}
</pre></div><p>
The class <a class="el" href="classRtWvOut.html">RtWvOut</a> is a protected subclass of <a class="el" href="classWvOut.html">WvOut</a>. A number of optional constructor arguments can be used to fine tune its performance for a given system. <a class="el" href="classRtWvOut.html">RtWvOut</a> provides a "single-sample" interface to the <a class="el" href="classRtAudio.html">RtAudio</a> class. Note that <a class="el" href="classRtWvOut.html">RtWvOut</a> (as well as the <a class="el" href="classRtWvIn.html">RtWvIn</a> and <a class="el" href="classRtDuplex.html">RtDuplex</a> classes described below) make use of RtAudio's blocking input/output functionality. On systems which implement an inherently callback-based audio API, this blocking functionality will be less robust. An example of audio output using a callback scheme will be discussed in a subsequent tutorial section.<p>
Though not used here, an <a class="el" href="classRtWvIn.html">RtWvIn</a> class exists as well which 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>
It may be possible to use an instance of <a class="el" href="classRtWvOut.html">RtWvOut</a> and an instance of <a class="el" href="classRtWvIn.html">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="classRtDuplex.html">RtDuplex</a> to achieve this behavior, in that it guarantees better synchronization between the input and output data. See the <code>effects</code> project or the <code>io.cpp</code> example program in the <code>examples</code> project for more information.<p>
When using any realtime STK class (<a class="el" href="classRtAudio.html">RtAudio</a>, <a class="el" href="classRtWvOut.html">RtWvOut</a>, <a class="el" href="classRtWvIn.html">RtWvIn</a>, <a class="el" href="classRtDuplex.html">RtDuplex</a>, <a class="el" href="classRtMidi.html">RtMidi</a>, <a class="el" href="classTcpWvIn.html">TcpWvIn</a>, <a class="el" href="classTcpWvOut.html">TcpWvOut</a>, <a class="el" href="classSocket.html">Socket</a>, and <a class="el" href="classThread.html">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>g++ -Wall -D__LINUX_ALSA__ -D__LITTLE_ENDIAN__ -o rtsine <a class="code" href="classStk.html">Stk</a>.cpp <a class="code" href="classWvIn.html">WvIn</a>.cpp <a class="code" href="classWaveLoop.html">WaveLoop</a>.cpp <a class="code" href="classWvOut.html">WvOut</a>.cpp \
<a class="code" href="classRtWvOut.html">RtWvOut</a>.cpp RtAudio.cpp rtsine.cpp -lpthread -lasound
</pre></div><p>
On a Macintosh OS X system, the syntax would be:<p>
<div class="fragment"><pre>g++ -Wall -D__MACOSX_CORE__ -o rtsine <a class="code" href="classStk.html">Stk</a>.cpp <a class="code" href="classWvIn.html">WvIn</a>.cpp <a class="code" href="classWaveLoop.html">WaveLoop</a>.cpp <a class="code" href="classWvOut.html">WvOut</a>.cpp <a class="code" href="classRtWvOut.html">RtWvOut</a>.cpp RtAudio.cpp \
rtsine.cpp -lpthread -framework CoreAudio -framework CoreMIDI -framework CoreFoundation
</pre></div><p>
[<a href="crealtime.html">Next tutorial</a>] &nbsp; [<a href="tutorial.html">Main tutorial page</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>&copy;1995-2004 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
</table>
</BODY>
</HTML>