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.
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.
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.
if( err != paNoError ) goto error;
inputParameters.channelCount = NUM_CHANNELS;
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
inputParameters.hostApiSpecificStreamInfo = NULL;
outputParameters.channelCount = NUM_CHANNELS;
outputParameters.sampleFormat = PA_SAMPLE_TYPE;
outputParameters.suggestedLatency =
Pa_GetDeviceInfo( outputParameters.device )->defaultHighOutputLatency;
outputParameters.hostApiSpecificStreamInfo = NULL;
&stream,
&inputParameters,
&outputParameters,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
NULL,
NULL );
if( err != paNoError ) goto error;
if( err != paNoError ) goto error;
printf("Wire on. Will run one minute.\n"); fflush(stdout);
for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i )
{
if( err ) goto xrun;
if( err ) goto xrun;
}
if( err != paNoError ) goto error;
if( err != paNoError ) goto error;
return 0;
PaError Pa_WriteStream(PaStream *stream, const void *buffer, unsigned long frames)
PaError Pa_ReadStream(PaStream *stream, void *buffer, unsigned long frames)
PaError Pa_Terminate(void)
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaError Pa_StartStream(PaStream *stream)
PaError Pa_CloseStream(PaStream *stream)
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaError Pa_Initialize(void)
PaDeviceIndex Pa_GetDefaultInputDevice(void)
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
PaError Pa_StopStream(PaStream *stream)
PaTime defaultHighInputLatency
Previous: Enumerating and Querying PortAudio Devices | Next: Exploring PortAudio