Home   Class/Enum List   File List   Compound Members  

Duplex Mode

Finally, it is easy to use RtAudio for simultaneous audio input/output, or duplex operation. In this example, we simply pass the input data back to the output.

#include "RtAudio.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
// Pass-through function.
int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *data )
{
// Since the number of input and output channels is equal, we can do
// a simple buffer copy operation here.
if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
unsigned long *bytes = (unsigned long *) data;
memcpy( outputBuffer, inputBuffer, *bytes );
return 0;
}
int main()
{
RtAudio adac;
if ( adac.getDeviceCount() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 0 );
}
// Set the same number of channels for both input and output.
unsigned int bufferBytes, bufferFrames = 512;
RtAudio::StreamParameters iParams, oParams;
iParams.deviceId = 0; // first available device
iParams.nChannels = 2;
oParams.deviceId = 0; // first available device
oParams.nChannels = 2;
try {
adac.openStream( &oParams, &iParams, RTAUDIO_SINT32, 44100, &bufferFrames, &inout, (void *)&bufferBytes );
}
catch ( RtAudioError& e ) {
exit( 0 );
}
bufferBytes = bufferFrames * 2 * 4;
try {
adac.startStream();
char input;
std::cout << "\nRunning ... press <enter> to quit.\n";
std::cin.get(input);
// Stop the stream.
adac.stopStream();
}
catch ( RtAudioError& e ) {
goto cleanup;
}
cleanup:
if ( adac.isStreamOpen() ) adac.closeStream();
return 0;
}
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition: RtAudio.h:144
Exception handling class for RtAudio.
Definition: RtAudio.h:204
virtual void printMessage(void) const
Prints thrown error message to stderr.
Definition: RtAudio.h:227
Realtime audio i/o C++ classes.
Definition: RtAudio.h:264
void openStream(RtAudio::StreamParameters *outputParameters, RtAudio::StreamParameters *inputParameters, RtAudioFormat format, unsigned int sampleRate, unsigned int *bufferFrames, RtAudioCallback callback, void *userData=NULL, RtAudio::StreamOptions *options=NULL, RtAudioErrorCallback errorCallback=NULL)
A public function for opening a stream with the specified parameters.
unsigned int getDeviceCount(void)
A public function that queries for the number of audio devices available.
Definition: RtAudio.h:830
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition: RtAudio.h:834
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition: RtAudio.h:838
void startStream(void)
A function that starts a stream.
Definition: RtAudio.h:835
void stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition: RtAudio.h:836
The structure for specifying input or ouput stream parameters.
Definition: RtAudio.h:301
unsigned int nChannels
Definition: RtAudio.h:303
unsigned int deviceId
Definition: RtAudio.h:302

In this example, audio recorded by the stream input will be played out during the next round of audio processing.

Note that a duplex stream can make use of two different devices (except when using the Linux Jack and Windows ASIO APIs). However, this may cause timing problems due to possible device clock variations, unless a common external "sync" is provided.


©2001-2017 Gary P. Scavone, McGill University. All Rights Reserved.
Maintained by Gary P. Scavone.