Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

playback.html 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <HTML>
  2. <HEAD>
  3. <TITLE>The RtAudio Home Page</TITLE>
  4. <LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">
  5. <LINK REL="SHORTCUT ICON" HREF="http://www.music.mcgill.ca/~gary/favicon.ico">
  6. </HEAD>
  7. <BODY BGCOLOR="#FFFFFF">
  8. <CENTER>
  9. <a class="qindex" href="index.html">Home</a> &nbsp; <a class="qindex" href="annotated.html">Class/Enum List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </CENTER>
  10. <HR>
  11. <!-- Generated by Doxygen 1.6.2 -->
  12. <div class="contents">
  13. <h1><a class="anchor" id="playback">Playback </a></h1><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>
  14. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &quot;<a class="code" href="RtAudio_8h.html">RtAudio.h</a>&quot;</span>
  15. <span class="preprocessor">#include &lt;iostream&gt;</span>
  16. <span class="preprocessor">#include &lt;cstdlib&gt;</span>
  17. <span class="comment">// Two-channel sawtooth wave generator.</span>
  18. <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,
  19. <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 )
  20. {
  21. <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i, j;
  22. <span class="keywordtype">double</span> *buffer = (<span class="keywordtype">double</span> *) outputBuffer;
  23. <span class="keywordtype">double</span> *lastValues = (<span class="keywordtype">double</span> *) userData;
  24. <span class="keywordflow">if</span> ( status )
  25. std::cout &lt;&lt; <span class="stringliteral">&quot;Stream underflow detected!&quot;</span> &lt;&lt; std::endl;
  26. <span class="comment">// Write interleaved audio data.</span>
  27. <span class="keywordflow">for</span> ( i=0; i&lt;nBufferFrames; i++ ) {
  28. <span class="keywordflow">for</span> ( j=0; j&lt;2; j++ ) {
  29. *buffer++ = lastValues[j];
  30. lastValues[j] += 0.005 * (j+1+(j*0.1));
  31. <span class="keywordflow">if</span> ( lastValues[j] &gt;= 1.0 ) lastValues[j] -= 2.0;
  32. }
  33. }
  34. <span class="keywordflow">return</span> 0;
  35. }
  36. <span class="keywordtype">int</span> main()
  37. {
  38. <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> dac;
  39. <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>() &lt; 1 ) {
  40. std::cout &lt;&lt; <span class="stringliteral">&quot;\nNo audio devices found!\n&quot;</span>;
  41. exit( 0 );
  42. }
  43. <a class="code" href="structRtAudio_1_1StreamParameters.html" title="The structure for specifying input or ouput stream parameters.">RtAudio::StreamParameters</a> parameters;
  44. 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>();
  45. parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#a88a10091b6e284e21235cc6f25332ebd">nChannels</a> = 2;
  46. parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#ad4b4503782653ec93c83328c46abe50c">firstChannel</a> = 0;
  47. <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sampleRate = 44100;
  48. <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferFrames = 256; <span class="comment">// 256 sample frames</span>
  49. <span class="keywordtype">double</span> data[2];
  50. <span class="keywordflow">try</span> {
  51. dac.<a class="code" href="classRtAudio.html#afacc99740fa4c5606fb35467cdea6da8" title="A public function for opening a stream with the specified parameters.">openStream</a>( &amp;parameters, NULL, RTAUDIO_FLOAT64,
  52. sampleRate, &amp;bufferFrames, &amp;saw, (<span class="keywordtype">void</span> *)&amp;data );
  53. dac.<a class="code" href="classRtAudio.html#aec017a89629ccef66a90b60be22a2f80" title="A function that starts a stream.">startStream</a>();
  54. }
  55. <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp;amp; RtMidi.">RtError</a>&amp; e ) {
  56. e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();
  57. exit( 0 );
  58. }
  59. <span class="keywordtype">char</span> input;
  60. std::cout &lt;&lt; <span class="stringliteral">&quot;\nPlaying ... press &lt;enter&gt; to quit.\n&quot;</span>;
  61. std::cin.get( input );
  62. <span class="keywordflow">try</span> {
  63. <span class="comment">// Stop the stream</span>
  64. 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>();
  65. }
  66. <span class="keywordflow">catch</span> (<a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp;amp; RtMidi.">RtError</a>&amp; e) {
  67. e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();
  68. }
  69. <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>();
  70. <span class="keywordflow">return</span> 0;
  71. }
  72. </pre></div><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#afacc99740fa4c5606fb35467cdea6da8" 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>
  73. <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>
  74. </div>
  75. <HR>
  76. <table><tr><td><img src="../images/mcgill.gif" width=165></td>
  77. <td>&copy;2001-2012 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>
  78. </table>
  79. </BODY>
  80. </HTML>