Merge 4.0.9 into releases
[rtaudio.git] / doc / html / recording.html
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
14
15 <h1><a class="anchor" id="recording">Recording </a></h1><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>
16 <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &quot;<a class="code" href="RtAudio_8h.html">RtAudio.h</a>&quot;</span>
17 <span class="preprocessor">#include &lt;iostream&gt;</span>
18 <span class="preprocessor">#include &lt;cstdlib&gt;</span>
19 <span class="preprocessor">#include &lt;cstring&gt;</span>
20
21 <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,
22          <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 )
23 {
24   <span class="keywordflow">if</span> ( status )
25     std::cout &lt;&lt; <span class="stringliteral">&quot;Stream overflow detected!&quot;</span> &lt;&lt; std::endl;
26
27   <span class="comment">// Do something with the data in the &quot;inputBuffer&quot; buffer.</span>
28
29   <span class="keywordflow">return</span> 0;
30 }
31
32 <span class="keywordtype">int</span> main()
33 {
34   <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> adc;
35   <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 ) {
36     std::cout &lt;&lt; <span class="stringliteral">&quot;\nNo audio devices found!\n&quot;</span>;
37     exit( 0 );
38   }
39
40   <a class="code" href="structRtAudio_1_1StreamParameters.html" title="The structure for specifying input or ouput stream parameters.">RtAudio::StreamParameters</a> parameters;
41   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>();
42   parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#a88a10091b6e284e21235cc6f25332ebd">nChannels</a> = 2;
43   parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#ad4b4503782653ec93c83328c46abe50c">firstChannel</a> = 0;
44   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sampleRate = 44100;
45   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferFrames = 256; <span class="comment">// 256 sample frames</span>
46
47   <span class="keywordflow">try</span> {
48     adc.<a class="code" href="classRtAudio.html#afacc99740fa4c5606fb35467cdea6da8" title="A public function for opening a stream with the specified parameters.">openStream</a>( NULL, &amp;parameters, RTAUDIO_SINT16,
49                     sampleRate, &amp;bufferFrames, &amp;record );
50     adc.<a class="code" href="classRtAudio.html#aec017a89629ccef66a90b60be22a2f80" title="A function that starts a stream.">startStream</a>();
51   }
52   <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp;amp; RtMidi.">RtError</a>&amp; e ) {
53     e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();
54     exit( 0 );
55   }
56   
57   <span class="keywordtype">char</span> input;
58   std::cout &lt;&lt; <span class="stringliteral">&quot;\nRecording ... press &lt;enter&gt; to quit.\n&quot;</span>;
59   std::cin.get( input );
60
61   <span class="keywordflow">try</span> {
62     <span class="comment">// Stop the stream</span>
63     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>();
64   }
65   <span class="keywordflow">catch</span> (<a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp;amp; RtMidi.">RtError</a>&amp; e) {
66     e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();
67   }
68
69   <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>();
70
71   <span class="keywordflow">return</span> 0;
72 }
73 </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#afacc99740fa4c5606fb35467cdea6da8" 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>
74 </div>
75 <HR>
76
77 <table><tr><td><img src="../images/mcgill.gif" width=165></td>
78   <td>&copy;2001-2010 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>
79 </table>
80
81 </BODY>
82 </HTML>