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.
inputParameters.hostApiSpecificStreamInfo = NULL;
outputParameters.hostApiSpecificStreamInfo = NULL;
&stream,
&inputParameters,
&outputParameters,
NULL,
NULL );
printf("Wire on. Will run one minute.\n"); fflush(stdout);
{
if( err ) goto xrun;
if( err ) goto xrun;
}
return 0;
#define PA_SAMPLE_TYPE
Definition pa_fuzz.c:56
#define SAMPLE_RATE
Definition pa_fuzz.c:55
#define FRAMES_PER_BUFFER
Definition pa_fuzz.c:57
#define NUM_CHANNELS
Definition paex_record.c:52
PaError Pa_WriteStream(PaStream *stream, const void *buffer, unsigned long frames)
Definition pa_front.c:1691
PaError Pa_ReadStream(PaStream *stream, void *buffer, unsigned long frames)
Definition pa_front.c:1651
PaError Pa_Terminate(void)
Definition pa_front.c:398
@ paNoError
Definition portaudio.h:125
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
PaError Pa_StartStream(PaStream *stream)
Definition pa_front.c:1445
#define paClipOff
Definition portaudio.h:670
PaError Pa_CloseStream(PaStream *stream)
Definition pa_front.c:1382
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
Definition pa_front.c:763
PaError Pa_Initialize(void)
Definition pa_front.c:353
PaDeviceIndex Pa_GetDefaultInputDevice(void)
Definition pa_front.c:717
PaDeviceIndex Pa_GetDefaultOutputDevice(void)
Definition pa_front.c:740
PaError Pa_StopStream(PaStream *stream)
Definition pa_front.c:1471
PaTime defaultHighOutputLatency
Definition portaudio.h:523
PaTime defaultHighInputLatency
Definition portaudio.h:522
Previous: Enumerating and Querying PortAudio Devices | Next: Exploring PortAudio