c2ba3041c3c20d373e9365e475677ad7f48af460
[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.6.2 -->
12 <div class="contents">
13
14
15 <h1><a class="anchor" id="duplex">Duplex Mode </a></h1><p>Finally, it is easy to use <a class="el" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> for simultaneous audio input/output, or duplex operation. In this example, we simply pass the input data back to the output.</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="comment">// Pass-through function.</span>
22 <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,
23            <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> *data )
24 {
25   <span class="comment">// Since the number of input and output channels is equal, we can do</span>
26   <span class="comment">// a simple buffer copy operation here.</span>
27   <span class="keywordflow">if</span> ( status ) std::cout &lt;&lt; <span class="stringliteral">&quot;Stream over/underflow detected.&quot;</span> &lt;&lt; std::endl;
28
29   <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *bytes = (<span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> *) data;
30   memcpy( outputBuffer, inputBuffer, *bytes );
31   <span class="keywordflow">return</span> 0;
32 }
33
34 <span class="keywordtype">int</span> main()
35 {
36  <a class="code" href="classRtAudio.html" title="Realtime audio i/o C++ classes.">RtAudio</a> adac;
37   <span class="keywordflow">if</span> ( adac.<a class="code" href="classRtAudio.html#a747ce2d73803641bbb66d6e78092aa1a" title="A public function that queries for the number of audio devices available.">getDeviceCount</a>() &lt; 1 ) {
38     std::cout &lt;&lt; <span class="stringliteral">&quot;\nNo audio devices found!\n&quot;</span>;
39     exit( 0 );
40   }
41
42   <span class="comment">// Set the same number of channels for both input and output.</span>
43   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferBytes, bufferFrames = 512;
44   <a class="code" href="structRtAudio_1_1StreamParameters.html" title="The structure for specifying input or ouput stream parameters.">RtAudio::StreamParameters</a> iParams, oParams;
45   iParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#ab3c72bcf3ef12149ae9a4fb597cc5489">deviceId</a> = 0; <span class="comment">// first available device</span>
46   iParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#a88a10091b6e284e21235cc6f25332ebd">nChannels</a> = 2;
47   oParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#ab3c72bcf3ef12149ae9a4fb597cc5489">deviceId</a> = 0; <span class="comment">// first available device</span>
48   oParams.<a class="code" href="structRtAudio_1_1StreamParameters.html#a88a10091b6e284e21235cc6f25332ebd">nChannels</a> = 2;
49
50   <span class="keywordflow">try</span> {
51     adac.<a class="code" href="classRtAudio.html#afacc99740fa4c5606fb35467cdea6da8" title="A public function for opening a stream with the specified parameters.">openStream</a>( &amp;oParams, &amp;iParams, RTAUDIO_SINT32, 44100, &amp;bufferFrames, &amp;inout, (<span class="keywordtype">void</span> *)&amp;bufferBytes );
52   }
53   <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp;amp; RtMidi.">RtError</a>&amp; e ) {
54     e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();
55     exit( 0 );
56   }
57
58   bufferBytes = bufferFrames * 2 * 4;
59
60   <span class="keywordflow">try</span> {
61     adac.<a class="code" href="classRtAudio.html#aec017a89629ccef66a90b60be22a2f80" title="A function that starts a stream.">startStream</a>();
62
63     <span class="keywordtype">char</span> input;
64     std::cout &lt;&lt; <span class="stringliteral">&quot;\nRunning ... press &lt;enter&gt; to quit.\n&quot;</span>;
65     std::cin.get(input);
66
67     <span class="comment">// Stop the stream.</span>
68     adac.<a class="code" href="classRtAudio.html#af4c241ff86936ecc8108f0d9dfe3efdd" title="Stop a stream, allowing any samples remaining in the output queue to be played.">stopStream</a>();
69   }
70   <span class="keywordflow">catch</span> ( <a class="code" href="classRtError.html" title="Exception handling class for RtAudio &amp;amp; RtMidi.">RtError</a>&amp; e ) {
71     e.<a class="code" href="classRtError.html#a251dcdac396c998c91706dd2dd3b8bfc" title="Prints thrown error message to stderr.">printMessage</a>();
72     <span class="keywordflow">goto</span> cleanup;
73   }
74
75  cleanup:
76   <span class="keywordflow">if</span> ( adac.<a class="code" href="classRtAudio.html#a3863e45ff81dbe97176de0ee7545917f" title="Returns true if a stream is open and false if not.">isStreamOpen</a>() ) adac.<a class="code" href="classRtAudio.html#a90d599002ad32cf250a4cb866f2cc93a" title="A function that closes a stream and frees any associated stream memory.">closeStream</a>();
77
78   <span class="keywordflow">return</span> 0;
79 }
80 </pre></div><p>In this example, audio recorded by the stream input will be played out during the next round of audio processing.</p>
81 <p>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. </p>
82 </div>
83 <HR>
84
85 <table><tr><td><img src="../images/mcgill.gif" width=165></td>
86   <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>
87 </table>
88
89 </BODY>
90 </HTML>