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

86 lines
6.1 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="realtime"><h2>Realtime Audio</h2></a>
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 system.
<p>
<div class="fragment"><pre><font class="comment">// rtsine.cpp</font>
<font class="preprocessor">#include "WaveLoop.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="classWaveLoop.html">WaveLoop</a> *input = 0;
<a class="code" href="classRtWvOut.html">RtWvOut</a> *output = 0;
<font class="keywordflow">try</font> {
<font class="comment">// Define and load the sine wave file</font>
input = <font class="keyword">new</font> WaveLoop( <font class="stringliteral">"rawwaves/sinewave.raw"</font>, TRUE );
<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;
}
input-&gt;<a class="code" href="classWaveLoop.html#a2">setFrequency</a>(440.0);
<font class="comment">// Play the oscillator for 40000 samples</font>
<font class="keywordtype">int</font> i;
<font class="keywordflow">for</font> ( i=0; i&lt;40000; i++ ) {
<font class="keywordflow">try</font> {
output-&gt;<a class="code" href="classRtWvOut.html#a6">tick</a>(input-&gt;<a class="code" href="classWvIn.html#a16">tick</a>());
}
<font class="keywordflow">catch</font> (<a class="code" href="classStkError.html">StkError</a> &amp;) {
<font class="keywordflow">goto</font> cleanup;
}
}
cleanup:
<font class="keyword">delete</font> input;
<font class="keyword">delete</font> output;
<font class="keywordflow">return</font> 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.
<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 is 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/MIDI 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 <a class="code" href="classRtAudio.html">RtAudio</a>.cpp rtsine.cpp -lpthread -lasound -lstk</pre></div>
<p>
On a Macintosh OS X system, the syntax would be:
<p>
<div class="fragment"><pre>CC -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 <a class="code" href="classRtAudio.html">RtAudio</a>.cpp \
rtsine.cpp -lpthread -lstdc++ -lstk -framework CoreAudio -framework CoreMIDI -framework CoreFoundation</pre></div>
<p>
[<a href="instruments.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>