<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <meta http-equiv="X-UA-Compatible" content="IE=9"/> <meta name="generator" content="Doxygen 1.8.3.1"/> <title>PortAudio: Blocking Read/Write Functions</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="dynsections.js"></script> <link href="doxygen.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="top"><!-- do not remove this div, it is closed by doxygen! --> <div id="titlearea"> <table cellspacing="0" cellpadding="0"> <tbody> <tr style="height: 56px;"> <td style="padding-left: 0.5em;"> <div id="projectname">PortAudio  <span id="projectnumber">2.0</span> </div> </td> </tr> </tbody> </table> </div> <!-- end header part --> <!-- Generated by Doxygen 1.8.3.1 --> <div id="navrow1" class="tabs"> <ul class="tablist"> <li><a href="index.html"><span>Main Page</span></a></li> <li class="current"><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul> </div> </div><!-- top --> <div class="header"> <div class="headertitle"> <div class="title">Blocking Read/Write Functions </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><p>PortAudio V19 adds a huge advance over previous versions with a feature called Blocking I/O. Although it may have lower performance that the callback method described earlier in this tutorial, blocking I/O is easier to understand and is, in some cases, more compatible with third party systems than the callback method. Most people starting audio programming also find Blocking I/O easier to learn.</p> <p>Blocking I/O works in much the same way as the callback method except that instead of providing a function to provide (or consume) audio data, you must feed data to (or consume data from) PortAudio at regular intervals, usually inside a loop. The example below, excepted from patest_read_write_wire.c, shows how to open the default device, and pass data from its input to its output for a set period of time. Note that we use the default high latency values to help avoid underruns since we are usually reading and writing audio data from a relatively low priority thread, and there is usually extra buffering required to make blocking I/O work.</p> <p>Note that not all API's implement Blocking I/O at this point, so for maximum portability or performance, you'll still want to use callbacks.</p> <div class="fragment"><div class="line"><span class="comment">/* -- initialize PortAudio -- */</span></div> <div class="line">err = <a class="code" href="portaudio_8h.html#abed859482d156622d9332dff9b2d89da">Pa_Initialize</a>();</div> <div class="line"><span class="keywordflow">if</span>( err != paNoError ) <span class="keywordflow">goto</span> error;</div> <div class="line"></div> <div class="line"><span class="comment">/* -- setup input and output -- */</span></div> <div class="line">inputParameters.device = <a class="code" href="portaudio_8h.html#abf9f2f82da95553d5adb929af670f74b">Pa_GetDefaultInputDevice</a>(); <span class="comment">/* default input device */</span></div> <div class="line">inputParameters.channelCount = NUM_CHANNELS;</div> <div class="line">inputParameters.sampleFormat = PA_SAMPLE_TYPE;</div> <div class="line">inputParameters.suggestedLatency = <a class="code" href="portaudio_8h.html#ac7d8e091ffc1d1d4a035704660e117eb">Pa_GetDeviceInfo</a>( inputParameters.device )-><a class="code" href="structPaDeviceInfo.html#a4214826038fcaf374beb9816024e6c9f">defaultHighInputLatency</a> ;</div> <div class="line">inputParameters.hostApiSpecificStreamInfo = NULL;</div> <div class="line"></div> <div class="line">outputParameters.device = <a class="code" href="portaudio_8h.html#adc955dfab007624000695c48d4f876dc">Pa_GetDefaultOutputDevice</a>(); <span class="comment">/* default output device */</span></div> <div class="line">outputParameters.channelCount = NUM_CHANNELS;</div> <div class="line">outputParameters.sampleFormat = PA_SAMPLE_TYPE;</div> <div class="line">outputParameters.suggestedLatency = <a class="code" href="portaudio_8h.html#ac7d8e091ffc1d1d4a035704660e117eb">Pa_GetDeviceInfo</a>( outputParameters.device )->defaultHighOutputLatency;</div> <div class="line">outputParameters.hostApiSpecificStreamInfo = NULL;</div> <div class="line"></div> <div class="line"><span class="comment">/* -- setup stream -- */</span></div> <div class="line">err = <a class="code" href="portaudio_8h.html#a443ad16338191af364e3be988014cbbe">Pa_OpenStream</a>(</div> <div class="line"> &stream,</div> <div class="line"> &inputParameters,</div> <div class="line"> &outputParameters,</div> <div class="line"> SAMPLE_RATE,</div> <div class="line"> FRAMES_PER_BUFFER,</div> <div class="line"> <a class="code" href="portaudio_8h.html#a837b8a81be3f7e36c96003e0d8dcec12">paClipOff</a>, <span class="comment">/* we won't output out of range samples so don't bother clipping them */</span></div> <div class="line"> NULL, <span class="comment">/* no callback, use blocking API */</span></div> <div class="line"> NULL ); <span class="comment">/* no callback, so no callback userData */</span></div> <div class="line"><span class="keywordflow">if</span>( err != paNoError ) <span class="keywordflow">goto</span> error;</div> <div class="line"></div> <div class="line"><span class="comment">/* -- start stream -- */</span></div> <div class="line">err = <a class="code" href="portaudio_8h.html#a7432aadd26c40452da12fa99fc1a047b">Pa_StartStream</a>( stream );</div> <div class="line"><span class="keywordflow">if</span>( err != paNoError ) <span class="keywordflow">goto</span> error;</div> <div class="line">printf(<span class="stringliteral">"Wire on. Will run one minute.\n"</span>); fflush(stdout);</div> <div class="line"></div> <div class="line"><span class="comment">/* -- Here's the loop where we pass data from input to output -- */</span></div> <div class="line"><span class="keywordflow">for</span>( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )</div> <div class="line">{</div> <div class="line"> err = <a class="code" href="portaudio_8h.html#a075a6efb503a728213bdae24347ed27d">Pa_WriteStream</a>( stream, sampleBlock, FRAMES_PER_BUFFER );</div> <div class="line"> <span class="keywordflow">if</span>( err ) <span class="keywordflow">goto</span> xrun;</div> <div class="line"> err = <a class="code" href="portaudio_8h.html#a0b62d4b74b5d3d88368e9e4c0b8b2dc7">Pa_ReadStream</a>( stream, sampleBlock, FRAMES_PER_BUFFER );</div> <div class="line"> <span class="keywordflow">if</span>( err ) <span class="keywordflow">goto</span> xrun;</div> <div class="line">}</div> <div class="line"><span class="comment">/* -- Now we stop the stream -- */</span></div> <div class="line">err = <a class="code" href="portaudio_8h.html#af18dd60220251286c337631a855e38a0">Pa_StopStream</a>( stream );</div> <div class="line"><span class="keywordflow">if</span>( err != paNoError ) <span class="keywordflow">goto</span> error;</div> <div class="line"></div> <div class="line"><span class="comment">/* -- don't forget to cleanup! -- */</span></div> <div class="line">err = <a class="code" href="portaudio_8h.html#a92f56f88cbd14da0e8e03077e835d104">Pa_CloseStream</a>( stream );</div> <div class="line"><span class="keywordflow">if</span>( err != paNoError ) <span class="keywordflow">goto</span> error;</div> <div class="line"></div> <div class="line"><a class="code" href="portaudio_8h.html#a0db317604e916e8bd6098e60e6237221">Pa_Terminate</a>();</div> <div class="line"><span class="keywordflow">return</span> 0;</div> </div><!-- fragment --><p>Previous: <a class="el" href="querying_devices.html">Enumerating and Querying PortAudio Devices</a> | Next: <a class="el" href="exploring.html">Exploring PortAudio</a> </p> </div></div><!-- contents --> <hr size="1"><address style="text-align: right;"><small>Generated for PortAudio by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a>1.8.3.1</small></address> </body> </html>