Merge 4.0.2 into releases
[rtaudio.git] / doc / html / duplex.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="duplex">Duplex Mode</a></h1>Finally, it is easy to use <a class="el" href="classRtAudio.html">RtAudio</a> for simultaneous audio input/output, or duplex operation. In this example, we simply pass the input data back to the output.<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="comment">// Pass-through function.</span>
17 <span class="keywordtype">int</span> inout( <span class="keywordtype">void</span> *outputBuffer, <span class="keywordtype">void</span> *inputBuffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nBufferFrames,
18            <span class="keywordtype">double</span> streamTime, <a class="code" href="RtAudio_8h.html#a12">RtAudioStreamStatus</a> status, <span class="keywordtype">void</span> *data )
19 {
20   <span class="comment">// Since the number of input and output channels is equal, we can do</span>
21   <span class="comment">// a simple buffer copy operation here.</span>
22   <span class="keywordflow">if</span> ( status ) std::cout &lt;&lt; <span class="stringliteral">"Stream over/underflow detected."</span> &lt;&lt; std::endl;
23
24   <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *bytes = (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *) data;
25   memcpy( outputBuffer, inputBuffer, *bytes );
26   <span class="keywordflow">return</span> 0;
27 }
28
29 <span class="keywordtype">int</span> main()
30 {
31  <a class="code" href="classRtAudio.html">RtAudio</a> adac;
32   <span class="keywordflow">if</span> ( adac.<a class="code" href="classRtAudio.html#a3">getDeviceCount</a>() &lt; 1 ) {
33     std::cout &lt;&lt; <span class="stringliteral">"\nNo audio devices found!\n"</span>;
34     exit( 0 );
35   }
36
37   <span class="comment">// Set the same number of channels for both input and output.</span>
38   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferBytes, bufferFrames = 512;
39   <a class="code" href="structRtAudio_1_1StreamParameters.html">RtAudio::StreamParameters</a> iParams, oParams;
40   iParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#o0">deviceId</a> = 0; <span class="comment">// first available device</span>
41   iParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#o1">nChannels</a> = 2;
42   oParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#o0">deviceId</a> = 0; <span class="comment">// first available device</span>
43   oParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#o1">nChannels</a> = 2;
44
45   <span class="keywordflow">try</span> {
46     adac.<a class="code" href="classRtAudio.html#a7">openStream</a>( &amp;oParams, &amp;iParams, RTAUDIO_SINT32, 44100, &amp;bufferFrames, &amp;inout, (<span class="keywordtype">void</span> *)&amp;bufferBytes );
47   }
48   <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html">RtError</a>&amp; e ) {
49     e.<a class="code" href="classRtError.html#a2">printMessage</a>();
50     exit( 0 );
51   }
52
53   bufferBytes = bufferFrames * 2 * 4;
54
55   <span class="keywordflow">try</span> {
56     adac.<a class="code" href="classRtAudio.html#a9">startStream</a>();
57
58     <span class="keywordtype">char</span> input;
59     std::cout &lt;&lt; <span class="stringliteral">"\nRunning ... press &lt;enter&gt; to quit.\n"</span>;
60     std::cin.get(input);
61
62     <span class="comment">// Stop the stream.</span>
63     adac.<a class="code" href="classRtAudio.html#a10">stopStream</a>();
64   }
65   <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html">RtError</a>&amp; e ) {
66     e.<a class="code" href="classRtError.html#a2">printMessage</a>();
67     <span class="keywordflow">goto</span> cleanup;
68   }
69
70  cleanup:
71   <span class="keywordflow">if</span> ( adac.<a class="code" href="classRtAudio.html#a12">isStreamOpen</a>() ) adac.<a class="code" href="classRtAudio.html#a8">closeStream</a>();
72
73   <span class="keywordflow">return</span> 0;
74 }
75 </pre></div><p>
76 In this example, audio recorded by the stream input will be played out during the next round of audio processing.<p>
77 Note that a duplex stream can make use of two different devices (except when using the Linux Jack and Windows ASIO APIs). However, this may cause timing problems due to possible device clock variations, unless a common external "sync" is provided. <HR>
78
79 <table><tr><td><img src="../images/mcgill.gif" width=165></td>
80   <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>
81 </table>
82
83 </BODY>
84 </HTML>