mirror of
https://github.com/thestk/stk
synced 2026-01-13 13:01:52 +00:00
73 lines
7.1 KiB
HTML
73 lines
7.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"> <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.4.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. We also make use of the <a class="el" href="classSineWave.html">SineWave</a> class as a sine-wave oscillator. <a class="el" href="classSineWave.html">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="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="classStk.html#e10">Stk::showWarnings</a>( <span class="keyword">true</span> );
|
|
|
|
<a class="code" href="classSineWave.html">SineWave</a> sine;
|
|
<a class="code" href="classRtWvOut.html">RtWvOut</a> *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> <a class="code" href="classRtWvOut.html">RtWvOut</a>( 1 );
|
|
}
|
|
<span class="keywordflow">catch</span> ( <a class="code" href="classStkError.html">StkError</a> & ) {
|
|
exit( 1 );
|
|
}
|
|
|
|
sine.<a class="code" href="classSineWave.html#a4">setFrequency</a>( 441.0 );
|
|
|
|
<span class="comment">// Play the oscillator for 40000 samples</span>
|
|
<span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i=0; i<40000; i++ ) {
|
|
<span class="keywordflow">try</span> {
|
|
dac-><a class="code" href="classWvOut.html#a6">tick</a>( sine.<a class="code" href="classGenerator.html#a3">tick</a>() );
|
|
}
|
|
<span class="keywordflow">catch</span> ( <a class="code" href="classStkError.html">StkError</a> & ) {
|
|
<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="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", blocking 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> 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>
|
|
Though not used here, an <a class="el" href="classRtWvIn.html">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>
|
|
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="classRtAudio.html">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>
|
|
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>, RtDuplex, <a class="el" href="classRtMidi.html">RtMidi</a>, <a class="el" href="classInetWvIn.html">InetWvIn</a>, <a class="el" href="classInetWvOut.html">InetWvOut</a>, <a class="el" href="classSocket.html">Socket</a>, <a class="el" href="classUdpSocket.html">UdpSocket</a>, <a class="el" href="classTcpServer.html">TcpServer</a>, <a class="el" href="classTcpClient.html">TcpClient</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 class="fragment">g++ -Wall -D__LINUX_ALSA__ -D__LITTLE_ENDIAN__ -o rtsine <a class="code" href="classStk.html">Stk</a>.cpp <a class="code" href="classGenerator.html">Generator</a>.cpp <a class="code" href="classSineWave.html">SineWave</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
|
|
</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 <a class="code" href="classStk.html">Stk</a>.cpp <a class="code" href="classGenerator.html">Generator</a>.cpp <a class="code" href="classSineWave.html">SineWave</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 -framework CoreAudio -framework CoreMIDI -framework CoreFoundation
|
|
</pre></div><p>
|
|
[<a href="tutorial.html">Main tutorial page</a>] [<a href="crealtime.html">Next tutorial</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>©1995-2007 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
|
|
</table>
|
|
|
|
</BODY>
|
|
</HTML>
|