Querying Devices
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 pa_devs.c 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:
int numDevices;
if( numDevices < 0 )
{
printf( "ERROR: Pa_CountDevices returned 0x%x\n", numDevices );
err = numDevices;
goto error;
}
PaDeviceIndex Pa_GetDeviceCount(void)
Definition pa_front.c:696
If you want to get information about each device, simply loop through as follows:
for( i=0; i<numDevices; i++ )
{
...
}
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
Definition pa_front.c:763
Definition portaudio.h:506
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:
int structVersion
const char * name
int maxInputChannels
int maxOutputChannels
PaTime defaultLowOutputLatency
PaTime defaultHighInputLatency
PaTime defaultHighOutputLatency
double defaultSampleRate
int PaHostApiIndex
Definition portaudio.h:240
double PaTime
Definition portaudio.h:465
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:
double desiredSampleRate;
...
PaError err;
{
printf( "Hooray!\n");
}
else
{
printf("Too Bad.\n");
}
#define paFormatIsSupported
Definition portaudio.h:594
PaError Pa_IsFormatSupported(const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate)
Definition pa_front.c:1048
Definition portaudio.h:548
Filling in the inputParameters and outputParameters fields is shown in a moment.
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 PaStreamParameters structures, and calling Pa_OpenStream:
double srate = ... ;
unsigned long framesPerBuffer = ... ;
bzero( &inputParameters, sizeof( inputParameters ) );
inputParameters.
device = inDevNum;
bzero( &outputParameters, sizeof( outputParameters ) );
outputParameters.
device = outDevNum;
&stream,
&inputParameters,
&outputParameters,
srate,
framesPerBuffer,
portAudioCallback,
(void *)this );
void PaStream
Definition portaudio.h:644
#define paFloat32
Definition portaudio.h:492
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
Definition pa_front.c:1152
#define paNoFlag
Definition portaudio.h:665
PaTime defaultLowOutputLatency
Definition portaudio.h:520
PaTime defaultLowInputLatency
Definition portaudio.h:519
int channelCount
Definition portaudio.h:562
PaTime suggestedLatency
Definition portaudio.h:581
PaSampleFormat sampleFormat
Definition portaudio.h:568
PaDeviceIndex device
Definition portaudio.h:555
void * hostApiSpecificStreamInfo
Definition portaudio.h:588
Previous: Utility Functions | Next: Blocking Read/Write Functions