|
- <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.4.4 -->
- <h1><a class="anchor" name="recording">Recording</a></h1>Using <a class="el" href="classRtAudio.html">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>
- <div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</span>
- <span class="preprocessor">#include <iostream></span>
-
- <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,
- <span class="keywordtype">double</span> streamTime, <a class="code" href="RtAudio_8h.html#a12">RtAudioStreamStatus</a> status, <span class="keywordtype">void</span> *userData )
- {
- <span class="keywordflow">if</span> ( status )
- std::cout << <span class="stringliteral">"Stream overflow detected!"</span> << std::endl;
-
- <span class="comment">// Do something with the data in the "inputBuffer" buffer.</span>
-
- <span class="keywordflow">return</span> 0;
- }
-
- <span class="keywordtype">int</span> main()
- {
- <a class="code" href="classRtAudio.html">RtAudio</a> adc;
- <span class="keywordflow">if</span> ( adc.<a class="code" href="classRtAudio.html#a3">getDeviceCount</a>() < 1 ) {
- std::cout << <span class="stringliteral">"\nNo audio devices found!\n"</span>;
- exit( 0 );
- }
-
- <a class="code" href="structRtAudio_1_1StreamParameters.html">RtAudio::StreamParameters</a> parameters;
- parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#o0">deviceId</a> = adc.<a class="code" href="classRtAudio.html#a6">getDefaultInputDevice</a>();
- parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#o1">nChannels</a> = 2;
- parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#o2">firstChannel</a> = 0;
- <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sampleRate = 44100;
- <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferFrames = 256; <span class="comment">// 256 sample frames</span>
-
- <span class="keywordflow">try</span> {
- adc.<a class="code" href="classRtAudio.html#a7">openStream</a>( NULL, &parameters, RTAUDIO_SINT16,
- sampleRate, &bufferFrames, &record );
- adc.<a class="code" href="classRtAudio.html#a9">startStream</a>();
- }
- <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html">RtError</a>& e ) {
- e.<a class="code" href="classRtError.html#a2">printMessage</a>();
- exit( 0 );
- }
-
- <span class="keywordtype">char</span> input;
- std::cout << <span class="stringliteral">"\nRecording ... press <enter> to quit.\n"</span>;
- std::cin.get( input );
-
- <span class="keywordflow">try</span> {
- <span class="comment">// Stop the stream</span>
- adc.<a class="code" href="classRtAudio.html#a10">stopStream</a>();
- }
- <span class="keywordflow">catch</span> (<a class="code" href="classRtError.html">RtError</a>& e) {
- e.<a class="code" href="classRtError.html#a2">printMessage</a>();
- }
-
- <span class="keywordflow">if</span> ( adc.<a class="code" href="classRtAudio.html#a12">isStreamOpen</a>() ) adc.<a class="code" href="classRtAudio.html#a8">closeStream</a>();
-
- <span class="keywordflow">return</span> 0;
- }
- </pre></div><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#a7">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. <HR>
-
- <table><tr><td><img src="../images/mcgill.gif" width=165></td>
- <td>©2001-2007 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>
|