<!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: Enumerating and Querying PortAudio Devices</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">Enumerating and Querying PortAudio Devices </div> </div> </div><!--header--> <div class="contents"> <div class="textblock"><h1><a class="anchor" id="tut_query1"></a> Querying Devices</h1> <p>It is often fine to use the default device as we did previously in this tutorial, but there are times when you'll want to explicitly choose the device from a list of available devices on the system. To see a working example of this, check out <a class="el" href="pa__devs_8c.html" title="List available devices, including device information.">pa_devs.c</a> in the tests/ directory of the PortAudio source code. To do so, you'll need to first initialize PortAudio and Query for the number of Devices:</p> <div class="fragment"><div class="line"><span class="keywordtype">int</span> numDevices;</div> <div class="line"></div> <div class="line">numDevices = <a class="code" href="portaudio_8h.html#acfe4d3c5ec1a343f459981bfa2057f8d">Pa_GetDeviceCount</a>();</div> <div class="line"><span class="keywordflow">if</span>( numDevices < 0 )</div> <div class="line">{</div> <div class="line"> printf( <span class="stringliteral">"ERROR: Pa_CountDevices returned 0x%x\n"</span>, numDevices );</div> <div class="line"> err = numDevices;</div> <div class="line"> <span class="keywordflow">goto</span> error;</div> <div class="line">}</div> </div><!-- fragment --><p>If you want to get information about each device, simply loop through as follows:</p> <div class="fragment"><div class="line"><span class="keyword">const</span> <a class="code" href="structPaDeviceInfo.html">PaDeviceInfo</a> *deviceInfo;</div> <div class="line"></div> <div class="line"><span class="keywordflow">for</span>( i=0; i<numDevices; i++ )</div> <div class="line">{</div> <div class="line"> deviceInfo = <a class="code" href="portaudio_8h.html#ac7d8e091ffc1d1d4a035704660e117eb">Pa_GetDeviceInfo</a>( i );</div> <div class="line"> ...</div> <div class="line">}</div> </div><!-- fragment --><p>The Pa_DeviceInfo structure contains a wealth of information such as the name of the devices, the default latency associated with the devices and more. The structure has the following fields:</p> <div class="fragment"><div class="line"><span class="keywordtype">int</span> structVersion</div> <div class="line"><span class="keyword">const</span> <span class="keywordtype">char</span> * name</div> <div class="line"><a class="code" href="portaudio_8h.html#aeef6da084c57c70aa94be97411e19930">PaHostApiIndex</a> hostApi</div> <div class="line"><span class="keywordtype">int</span> maxInputChannels</div> <div class="line"><span class="keywordtype">int</span> maxOutputChannels</div> <div class="line"><a class="code" href="portaudio_8h.html#af17a7e6d0471a23071acf8dbd7bbe4bd">PaTime</a> defaultLowInputLatency</div> <div class="line"><a class="code" href="portaudio_8h.html#af17a7e6d0471a23071acf8dbd7bbe4bd">PaTime</a> defaultLowOutputLatency</div> <div class="line"><a class="code" href="portaudio_8h.html#af17a7e6d0471a23071acf8dbd7bbe4bd">PaTime</a> defaultHighInputLatency</div> <div class="line"><a class="code" href="portaudio_8h.html#af17a7e6d0471a23071acf8dbd7bbe4bd">PaTime</a> defaultHighOutputLatency</div> <div class="line"><span class="keywordtype">double</span> defaultSampleRate</div> </div><!-- fragment --><p>You may notice that you can't determine, from this information alone, whether or not a particular sample rate is supported. This is because some devices support ranges of sample rates, others support, a list of sample rates, and still others support some sample rates and number of channels combinations but not others. To get around this, PortAudio offers a function for testing a particular device with a given format:</p> <div class="fragment"><div class="line"><span class="keyword">const</span> <a class="code" href="structPaStreamParameters.html">PaStreamParameters</a> *inputParameters;</div> <div class="line"><span class="keyword">const</span> <a class="code" href="structPaStreamParameters.html">PaStreamParameters</a> *outputParameters;</div> <div class="line"><span class="keywordtype">double</span> desiredSampleRate;</div> <div class="line">...</div> <div class="line">PaError err;</div> <div class="line"></div> <div class="line">err = <a class="code" href="portaudio_8h.html#abdb313743d6efef26cecdae787a2bd3d">Pa_IsFormatSupported</a>( inputParameters, outputParameters, desiredSampleRate );</div> <div class="line"><span class="keywordflow">if</span>( err == <a class="code" href="portaudio_8h.html#a400df642339bf4112333060af6a43c0f">paFormatIsSupported</a> )</div> <div class="line">{</div> <div class="line"> printf( <span class="stringliteral">"Hooray!\n"</span>);</div> <div class="line">}</div> <div class="line"><span class="keywordflow">else</span></div> <div class="line">{</div> <div class="line"> printf(<span class="stringliteral">"Too Bad.\n"</span>);</div> <div class="line">}</div> </div><!-- fragment --><p>Filling in the inputParameters and outputParameters fields is shown in a moment.</p> <p>Once you've found a configuration you like, or one you'd like to go ahead and try, you can open the stream by filling in the <a class="el" href="structPaStreamParameters.html">PaStreamParameters</a> structures, and calling Pa_OpenStream:</p> <div class="fragment"><div class="line"><span class="keywordtype">double</span> srate = ... ;</div> <div class="line"><a class="code" href="portaudio_8h.html#a19874734f89958fccf86785490d53b4c">PaStream</a> *stream;</div> <div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> framesPerBuffer = ... ; <span class="comment">//could be paFramesPerBufferUnspecified, in which case PortAudio will do its best to manage it for you, but, on some platforms, the framesPerBuffer will change in each call to the callback</span></div> <div class="line"><a class="code" href="structPaStreamParameters.html">PaStreamParameters</a> outputParameters;</div> <div class="line"><a class="code" href="structPaStreamParameters.html">PaStreamParameters</a> inputParameters;</div> <div class="line"></div> <div class="line">bzero( &inputParameters, <span class="keyword">sizeof</span>( inputParameters ) ); <span class="comment">//not necessary if you are filling in all the fields</span></div> <div class="line">inputParameters.<a class="code" href="structPaStreamParameters.html#a861ff361da71fc2572dd356c9c9878ca">channelCount</a> = inChan;</div> <div class="line">inputParameters.<a class="code" href="structPaStreamParameters.html#aebaf648b4d11dd1252a747b76b8da084">device</a> = inDevNum;</div> <div class="line">inputParameters.<a class="code" href="structPaStreamParameters.html#aff01b9fa0710ad1654471e97665c06a9">hostApiSpecificStreamInfo</a> = NULL;</div> <div class="line">inputParameters.<a class="code" href="structPaStreamParameters.html#ad8d2d3063757b812f9e5f8709f41052b">sampleFormat</a> = <a class="code" href="portaudio_8h.html#a2f16d29916725b8791eae60ab9e0b081">paFloat32</a>;</div> <div class="line">inputParameters.<a class="code" href="structPaStreamParameters.html#aa1e80ac0551162fd091db8936ccbe9a0">suggestedLatency</a> = <a class="code" href="portaudio_8h.html#ac7d8e091ffc1d1d4a035704660e117eb">Pa_GetDeviceInfo</a>(inDevNum)-><a class="code" href="structPaDeviceInfo.html#aad6629064b8c7cf043d237ea0a5cc534">defaultLowInputLatency</a> ;</div> <div class="line">inputParameters.<a class="code" href="structPaStreamParameters.html#aff01b9fa0710ad1654471e97665c06a9">hostApiSpecificStreamInfo</a> = NULL; <span class="comment">//See you specific host's API docs for info on using this field</span></div> <div class="line"></div> <div class="line"></div> <div class="line">bzero( &outputParameters, <span class="keyword">sizeof</span>( outputParameters ) ); <span class="comment">//not necessary if you are filling in all the fields</span></div> <div class="line">outputParameters.<a class="code" href="structPaStreamParameters.html#a861ff361da71fc2572dd356c9c9878ca">channelCount</a> = outChan;</div> <div class="line">outputParameters.<a class="code" href="structPaStreamParameters.html#aebaf648b4d11dd1252a747b76b8da084">device</a> = outDevNum;</div> <div class="line">outputParameters.<a class="code" href="structPaStreamParameters.html#aff01b9fa0710ad1654471e97665c06a9">hostApiSpecificStreamInfo</a> = NULL;</div> <div class="line">outputParameters.<a class="code" href="structPaStreamParameters.html#ad8d2d3063757b812f9e5f8709f41052b">sampleFormat</a> = <a class="code" href="portaudio_8h.html#a2f16d29916725b8791eae60ab9e0b081">paFloat32</a>;</div> <div class="line">outputParameters.<a class="code" href="structPaStreamParameters.html#aa1e80ac0551162fd091db8936ccbe9a0">suggestedLatency</a> = <a class="code" href="portaudio_8h.html#ac7d8e091ffc1d1d4a035704660e117eb">Pa_GetDeviceInfo</a>(outDevNum)->defaultLowOutputLatency ;</div> <div class="line">outputParameters.<a class="code" href="structPaStreamParameters.html#aff01b9fa0710ad1654471e97665c06a9">hostApiSpecificStreamInfo</a> = NULL; <span class="comment">//See you specific host's API docs for info on using this field</span></div> <div class="line"></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"> srate,</div> <div class="line"> framesPerBuffer,</div> <div class="line"> <a class="code" href="portaudio_8h.html#ad33384abe3754a39f4773f2561773595">paNoFlag</a>, <span class="comment">//flags that can be used to define dither, clip settings and more</span></div> <div class="line"> portAudioCallback, <span class="comment">//your callback function</span></div> <div class="line"> (<span class="keywordtype">void</span> *)<span class="keyword">this</span> ); <span class="comment">//data to be passed to callback. In C++, it is frequently (void *)this</span></div> <div class="line"><span class="comment">//don't forget to check errors!</span></div> </div><!-- fragment --><p>Previous: <a class="el" href="utility_functions.html">Utility Functions</a> | Next: <a class="el" href="blocking_read_write.html">Blocking Read/Write Functions</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>