Merge 4.0.2 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.4.4 -->
12 <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>
13 <div class="fragment"><pre class="fragment"><span class="preprocessor">#include "<a class="code" href="RtAudio_8h.html">RtAudio.h</a>"</span>
14 <span class="preprocessor">#include &lt;iostream&gt;</span>
15
16 <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,
17          <span class="keywordtype">double</span> streamTime, <a class="code" href="RtAudio_8h.html#a12">RtAudioStreamStatus</a> status, <span class="keywordtype">void</span> *userData )
18 {
19   <span class="keywordflow">if</span> ( status )
20     std::cout &lt;&lt; <span class="stringliteral">"Stream overflow detected!"</span> &lt;&lt; std::endl;
21
22   <span class="comment">// Do something with the data in the "inputBuffer" buffer.</span>
23
24   <span class="keywordflow">return</span> 0;
25 }
26
27 <span class="keywordtype">int</span> main()
28 {
29   <a class="code" href="classRtAudio.html">RtAudio</a> adc;
30   <span class="keywordflow">if</span> ( adc.<a class="code" href="classRtAudio.html#a3">getDeviceCount</a>() &lt; 1 ) {
31     std::cout &lt;&lt; <span class="stringliteral">"\nNo audio devices found!\n"</span>;
32     exit( 0 );
33   }
34
35   <a class="code" href="structRtAudio_1_1StreamParameters.html">RtAudio::StreamParameters</a> parameters;
36   parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#o0">deviceId</a> = adc.<a class="code" href="classRtAudio.html#a6">getDefaultInputDevice</a>();
37   parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#o1">nChannels</a> = 2;
38   parameters.<a class="code" href="structRtAudio_1_1StreamParameters.html#o2">firstChannel</a> = 0;
39   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> sampleRate = 44100;
40   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferFrames = 256; <span class="comment">// 256 sample frames</span>
41
42   <span class="keywordflow">try</span> {
43     adc.<a class="code" href="classRtAudio.html#a7">openStream</a>( NULL, &amp;parameters, RTAUDIO_SINT16,
44                     sampleRate, &amp;bufferFrames, &amp;record );
45     adc.<a class="code" href="classRtAudio.html#a9">startStream</a>();
46   }
47   <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html">RtError</a>&amp; e ) {
48     e.<a class="code" href="classRtError.html#a2">printMessage</a>();
49     exit( 0 );
50   }
51   
52   <span class="keywordtype">char</span> input;
53   std::cout &lt;&lt; <span class="stringliteral">"\nRecording ... press &lt;enter&gt; to quit.\n"</span>;
54   std::cin.get( input );
55
56   <span class="keywordflow">try</span> {
57     <span class="comment">// Stop the stream</span>
58     adc.<a class="code" href="classRtAudio.html#a10">stopStream</a>();
59   }
60   <span class="keywordflow">catch</span> (<a class="code" href="classRtError.html">RtError</a>&amp; e) {
61     e.<a class="code" href="classRtError.html#a2">printMessage</a>();
62   }
63
64   <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>();
65
66   <span class="keywordflow">return</span> 0;
67 }
68 </pre></div><p>
69 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>
70
71 <table><tr><td><img src="../images/mcgill.gif" width=165></td>
72   <td>&copy;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>
73 </table>
74
75 </BODY>
76 </HTML>