|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <HTML>
- <HEAD>
- <TITLE>The RtAudio Home Page</TITLE>
- <LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">
- <LINK REL="SHORTCUT ICON" HREF="http://www.music.mcgill.ca/~gary/favicon.ico">
- </HEAD>
- <BODY BGCOLOR="#FFFFFF">
- <CENTER>
- <a class="qindex" href="index.html">Home</a> <a class="qindex" href="annotated.html">Class/Enum List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> </CENTER>
- <HR>
- <!-- Generated by Doxygen 1.8.2 -->
- </div><!-- top -->
- <div class="header">
- <div class="headertitle">
- <div class="title">Playback </div> </div>
- </div><!--header-->
- <div class="contents">
- <div class="textblock"><p>In this example, we provide a complete program that demonstrates the use of <a class="el" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> for audio playback. Our program produces a two-channel sawtooth waveform for output.</p>
- <div class="fragment"><div class="line"><span class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</span></div>
- <div class="line"><span class="preprocessor">#include <iostream></span></div>
- <div class="line"><span class="preprocessor">#include <cstdlib></span></div>
- <div class="line"></div>
- <div class="line"><span class="comment">// Two-channel sawtooth wave generator.</span></div>
- <div class="line"><span class="keywordtype">int</span> saw( <span class="keywordtype">void</span> *outputBuffer, <span class="keywordtype">void</span> *inputBuffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nBufferFrames,</div>
- <div class="line"> <span class="keywordtype">double</span> streamTime, <a class="code" href="RtAudio_8h.html#a80e306d363583da3b0a1b65d9b57c806" title="RtAudio stream status (over- or underflow) flags.">RtAudioStreamStatus</a> status, <span class="keywordtype">void</span> *userData )</div>
- <div class="line">{</div>
- <div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i, j;</div>
- <div class="line"> <span class="keywordtype">double</span> *buffer = (<span class="keywordtype">double</span> *) outputBuffer;</div>
- <div class="line"> <span class="keywordtype">double</span> *lastValues = (<span class="keywordtype">double</span> *) userData;</div>
- <div class="line"></div>
- <div class="line"> <span class="keywordflow">if</span> ( status )</div>
- <div class="line"> std::cout << <span class="stringliteral">"Stream underflow detected!"</span> << std::endl;</div>
- <div class="line"></div>
- <div class="line"> <span class="comment">// Write interleaved audio data.</span></div>
- <div class="line"> <span class="keywordflow">for</span> ( i=0; i<nBufferFrames; i++ ) {</div>
- <div class="line"> <span class="keywordflow">for</span> ( j=0; j<2; j++ ) {</div>
- <div class="line"> *buffer++ = lastValues[j];</div>
- <div class="line"></div>
- <div class="line"> lastValues[j] += 0.005 * (j+1+(j*0.1));</div>
- <div class="line"> <span class="keywordflow">if</span> ( lastValues[j] >= 1.0 ) lastValues[j] -= 2.0;</div>
- <div class="line"> }</div>
- <div class="line"> }</div>
- <div class="line"></div>
- <div class="line"> <span class="keywordflow">return</span> 0;</div>
- <div class="line">}</div>
- <div class="line"></div>
- <div class="line"><span class="keywordtype">int</span> main()</div>
- <div class="line">{</div>
- <div class="line"> <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> dac;</div>
- <div class="line"> <span class="keywordflow">if</span> ( dac.<a class="code" href="classRtAudio.html#a747ce2d73803641bbb66d6e78092aa1a" title="A public function that queries for the number of audio devices available.">getDeviceCount</a>() < 1 ) {</div>
- <div class="line"> std::cout << <span class="stringliteral">"\nNo audio devices found!\n"</span>;</div>
- <div class="line"> exit( 0 );</div>
- <div class="line"> }</div>
- <div class="line"></div>
- <div class="line"> <a class="code" href="structRtAudio_1_1StreamParameters.html" title="The structure for specifying input or ouput stream parameters.">RtAudio::StreamParameters</a> parameters;</div>
- <div class="line"> parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#ab3c72bcf3ef12149ae9a4fb597cc5489">deviceId</a> = dac.<a class="code" href="classRtAudio.html#a3a3f3dbe13ea696b521e49cdaaa357bc" title="A function that returns the index of the default output device.">getDefaultOutputDevice</a>();</div>
- <div class="line"> parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#a88a10091b6e284e21235cc6f25332ebd">nChannels</a> = 2;</div>
- <div class="line"> parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#ad4b4503782653ec93c83328c46abe50c">firstChannel</a> = 0;</div>
- <div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sampleRate = 44100;</div>
- <div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferFrames = 256; <span class="comment">// 256 sample frames</span></div>
- <div class="line"> <span class="keywordtype">double</span> data[2];</div>
- <div class="line"></div>
- <div class="line"> <span class="keywordflow">try</span> {</div>
- <div class="line"> dac.<a class="code" href="classRtAudio.html#a6907539d2527775df778ebce32ef1e3b" title="A public function for opening a stream with the specified parameters.">openStream</a>( &parameters, NULL, RTAUDIO_FLOAT64,</div>
- <div class="line"> sampleRate, &bufferFrames, &saw, (<span class="keywordtype">void</span> *)&data );</div>
- <div class="line"> dac.<a class="code" href="classRtAudio.html#aec017a89629ccef66a90b60be22a2f80" title="A function that starts a stream.">startStream</a>();</div>
- <div class="line"> }</div>
- <div class="line"> <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html" title="Exception handling class for RtAudio & RtMidi.">RtError</a>& e ) {</div>
- <div class="line"> e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();</div>
- <div class="line"> exit( 0 );</div>
- <div class="line"> }</div>
- <div class="line"> </div>
- <div class="line"> <span class="keywordtype">char</span> input;</div>
- <div class="line"> std::cout << <span class="stringliteral">"\nPlaying ... press <enter> to quit.\n"</span>;</div>
- <div class="line"> std::cin.get( input );</div>
- <div class="line"></div>
- <div class="line"> <span class="keywordflow">try</span> {</div>
- <div class="line"> <span class="comment">// Stop the stream</span></div>
- <div class="line"> dac.<a class="code" href="classRtAudio.html#af4c241ff86936ecc8108f0d9dfe3efdd" title="Stop a stream, allowing any samples remaining in the output queue to be played.">stopStream</a>();</div>
- <div class="line"> }</div>
- <div class="line"> <span class="keywordflow">catch</span> (<a class="code" href="classRtError.html" title="Exception handling class for RtAudio & RtMidi.">RtError</a>& e) {</div>
- <div class="line"> e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();</div>
- <div class="line"> }</div>
- <div class="line"></div>
- <div class="line"> <span class="keywordflow">if</span> ( dac.<a class="code" href="classRtAudio.html#a3863e45ff81dbe97176de0ee7545917f" title="Returns true if a stream is open and false if not.">isStreamOpen</a>() ) dac.<a class="code" href="classRtAudio.html#a90d599002ad32cf250a4cb866f2cc93a" title="A function that closes a stream and frees any associated stream memory.">closeStream</a>();</div>
- <div class="line"></div>
- <div class="line"> <span class="keywordflow">return</span> 0;</div>
- <div class="line">}</div>
- </div><!-- fragment --><p>We open the stream in exactly the same way as the previous example (except with a data format change) and specify the address of our callback function <em>"saw()"</em>. The callback function will automatically be invoked when the underlying audio system needs data for output. Note that the callback function is called only when the stream is "running" (between calls to the <a class="el" href="classRtAudio.html#aec017a89629ccef66a90b60be22a2f80" title="A function that starts a stream.">RtAudio::startStream()</a> and <a class="el" href="classRtAudio.html#af4c241ff86936ecc8108f0d9dfe3efdd" title="Stop a stream, allowing any samples remaining in the output queue to be played.">RtAudio::stopStream()</a> functions). We can also pass a pointer value to the <a class="el" href="classRtAudio.html#a6907539d2527775df778ebce32ef1e3b" title="A public function for opening a stream with the specified parameters.">RtAudio::openStream()</a> function that is made available in the callback function. In this way, it is possible to gain access to arbitrary data created in our <em>main()</em> function from within the globally defined callback function.</p>
- <p>In this example, we stop the stream with an explicit call to <a class="el" href="classRtAudio.html#af4c241ff86936ecc8108f0d9dfe3efdd" title="Stop a stream, allowing any samples remaining in the output queue to be played.">RtAudio::stopStream()</a>. It is also possible to stop a stream by returning a non-zero value from the callback function. A return value of 1 will cause the stream to finish draining its internal buffers and then halt (equivalent to calling the <a class="el" href="classRtAudio.html#af4c241ff86936ecc8108f0d9dfe3efdd" title="Stop a stream, allowing any samples remaining in the output queue to be played.">RtAudio::stopStream()</a> function). A return value of 2 will cause the stream to stop immediately (equivalent to calling the <a class="el" href="classRtAudio.html#ad0586b47cd6bb9591a80b4052815991f" title="Stop a stream, discarding any samples remaining in the input/output queue.">RtAudio::abortStream()</a> function). </p>
- </div></div><!-- contents -->
- <HR>
- <table><tr><td><img src="../images/mcgill.gif" width=165></td>
- <td>©2001-2013 Gary P. Scavone, McGill University. All Rights Reserved.<br>Maintained by <a href="http://www.music.mcgill.ca/~gary/">Gary P. Scavone</a>.</td></tr>
- </table>
- </BODY>
- </HTML>
|