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.

84 lines
7.5KB

  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.8.2 -->
  12. </div><!-- top -->
  13. <div class="header">
  14. <div class="headertitle">
  15. <div class="title">Recording </div> </div>
  16. </div><!--header-->
  17. <div class="contents">
  18. <div class="textblock"><p>Using <a class="el" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> for audio input is almost identical to the way it is used for playback. Here's the blocking playback example rewritten for recording:</p>
  19. <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="RtAudio_8h.html">RtAudio.h</a>&quot;</span></div>
  20. <div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
  21. <div class="line"><span class="preprocessor">#include &lt;cstdlib&gt;</span></div>
  22. <div class="line"><span class="preprocessor">#include &lt;cstring&gt;</span></div>
  23. <div class="line"></div>
  24. <div class="line"><span class="keywordtype">int</span> record( <span class="keywordtype">void</span> *outputBuffer, <span class="keywordtype">void</span> *inputBuffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nBufferFrames,</div>
  25. <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>
  26. <div class="line">{</div>
  27. <div class="line"> <span class="keywordflow">if</span> ( status )</div>
  28. <div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;Stream overflow detected!&quot;</span> &lt;&lt; std::endl;</div>
  29. <div class="line"></div>
  30. <div class="line"> <span class="comment">// Do something with the data in the &quot;inputBuffer&quot; buffer.</span></div>
  31. <div class="line"></div>
  32. <div class="line"> <span class="keywordflow">return</span> 0;</div>
  33. <div class="line">}</div>
  34. <div class="line"></div>
  35. <div class="line"><span class="keywordtype">int</span> main()</div>
  36. <div class="line">{</div>
  37. <div class="line"> <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> adc;</div>
  38. <div class="line"> <span class="keywordflow">if</span> ( adc.<a class="code" href="classRtAudio.html#a747ce2d73803641bbb66d6e78092aa1a" title="A public function that queries for the number of audio devices available.">getDeviceCount</a>() &lt; 1 ) {</div>
  39. <div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;\nNo audio devices found!\n&quot;</span>;</div>
  40. <div class="line"> exit( 0 );</div>
  41. <div class="line"> }</div>
  42. <div class="line"></div>
  43. <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>
  44. <div class="line"> parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#ab3c72bcf3ef12149ae9a4fb597cc5489">deviceId</a> = adc.<a class="code" href="classRtAudio.html#aad8b94edd3cd379ee300b125750ac6ce" title="A function that returns the index of the default input device.">getDefaultInputDevice</a>();</div>
  45. <div class="line"> parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#a88a10091b6e284e21235cc6f25332ebd">nChannels</a> = 2;</div>
  46. <div class="line"> parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#ad4b4503782653ec93c83328c46abe50c">firstChannel</a> = 0;</div>
  47. <div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sampleRate = 44100;</div>
  48. <div class="line"> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferFrames = 256; <span class="comment">// 256 sample frames</span></div>
  49. <div class="line"></div>
  50. <div class="line"> <span class="keywordflow">try</span> {</div>
  51. <div class="line"> adc.<a class="code" href="classRtAudio.html#a6907539d2527775df778ebce32ef1e3b" title="A public function for opening a stream with the specified parameters.">openStream</a>( NULL, &amp;parameters, RTAUDIO_SINT16,</div>
  52. <div class="line"> sampleRate, &amp;bufferFrames, &amp;record );</div>
  53. <div class="line"> adc.<a class="code" href="classRtAudio.html#aec017a89629ccef66a90b60be22a2f80" title="A function that starts a stream.">startStream</a>();</div>
  54. <div class="line"> }</div>
  55. <div class="line"> <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp; RtMidi.">RtError</a>&amp; e ) {</div>
  56. <div class="line"> e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();</div>
  57. <div class="line"> exit( 0 );</div>
  58. <div class="line"> }</div>
  59. <div class="line"> </div>
  60. <div class="line"> <span class="keywordtype">char</span> input;</div>
  61. <div class="line"> std::cout &lt;&lt; <span class="stringliteral">&quot;\nRecording ... press &lt;enter&gt; to quit.\n&quot;</span>;</div>
  62. <div class="line"> std::cin.get( input );</div>
  63. <div class="line"></div>
  64. <div class="line"> <span class="keywordflow">try</span> {</div>
  65. <div class="line"> <span class="comment">// Stop the stream</span></div>
  66. <div class="line"> adc.<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>
  67. <div class="line"> }</div>
  68. <div class="line"> <span class="keywordflow">catch</span> (<a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp; RtMidi.">RtError</a>&amp; e) {</div>
  69. <div class="line"> e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();</div>
  70. <div class="line"> }</div>
  71. <div class="line"></div>
  72. <div class="line"> <span class="keywordflow">if</span> ( adc.<a class="code" href="classRtAudio.html#a3863e45ff81dbe97176de0ee7545917f" title="Returns true if a stream is open and false if not.">isStreamOpen</a>() ) adc.<a class="code" href="classRtAudio.html#a90d599002ad32cf250a4cb866f2cc93a" title="A function that closes a stream and frees any associated stream memory.">closeStream</a>();</div>
  73. <div class="line"></div>
  74. <div class="line"> <span class="keywordflow">return</span> 0;</div>
  75. <div class="line">}</div>
  76. </div><!-- fragment --><p>In this example, we pass the address of the stream parameter structure as the second argument of the <a class="el" href="classRtAudio.html#a6907539d2527775df778ebce32ef1e3b" title="A public function for opening a stream with the specified parameters.">RtAudio::openStream()</a> function and pass a NULL value for the output stream parameters. In this example, the <em>record()</em> callback function performs no specific operations. </p>
  77. </div></div><!-- contents -->
  78. <HR>
  79. <table><tr><td><img src="../images/mcgill.gif" width=165></td>
  80. <td>&copy;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>
  81. </table>
  82. </BODY>
  83. </HTML>