Home   Class/Enum List   File List   Compound Members   C interface  

Recording

Using RtAudio for audio input is almost identical to the way it is used for playback. Here's the blocking playback example rewritten for recording:

#include "RtAudio.h"
#include <iostream>
#include <cstdlib>
#include <cstring>
int record( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *userData )
{
if ( status )
std::cout << "Stream overflow detected!" << std::endl;
// Do something with the data in the "inputBuffer" buffer.
return 0;
}
int main()
{
RtAudio adc;
std::vector<unsigned int> deviceIds = adc.getDeviceIds();
if ( deviceIds.size() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 0 );
}
parameters.deviceId = adc.getDefaultInputDevice();
parameters.nChannels = 2;
parameters.firstChannel = 0;
unsigned int sampleRate = 44100;
unsigned int bufferFrames = 256; // 256 sample frames
if ( adc.openStream( NULL, &parameters, RTAUDIO_SINT16,
sampleRate, &bufferFrames, &record ) ) {
std::cout << '\n' << adc.getErrorText() << '\n' << std::endl;
exit( 0 ); // problem with device settings
}
// Stream is open ... now start it.
if ( adc.startStream() ) {
std::cout << adc.getErrorText() << std::endl;
goto cleanup;
}
char input;
std::cout << "\nRecording ... press <enter> to quit.\n";
std::cin.get( input );
// Block released ... stop the stream
if ( adc.isStreamRunning() )
adc.stopStream(); // or could call adc.abortStream();
cleanup:
if ( adc.isStreamOpen() ) adc.closeStream();
return 0;
}
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition RtAudio.h:178
Realtime audio i/o C++ classes.
Definition RtAudio.h:268
bool isStreamRunning(void) const
Returns true if the stream is running and false if it is stopped or not open.
Definition RtAudio.h:922
RtAudioErrorType stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition RtAudio.h:918
const std::string getErrorText(void)
Retrieve the error message corresponding to the last error or warning condition.
Definition RtAudio.h:920
unsigned int nChannels
Definition RtAudio.h:305
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition RtAudio.h:916
RtAudioErrorType openStream(RtAudio::StreamParameters *outputParameters, RtAudio::StreamParameters *inputParameters, RtAudioFormat format, unsigned int sampleRate, unsigned int *bufferFrames, RtAudioCallback callback, void *userData=NULL, RtAudio::StreamOptions *options=NULL)
A public function for opening a stream with the specified parameters.
RtAudioErrorType startStream(void)
A function that starts a stream.
Definition RtAudio.h:917
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition RtAudio.h:921
unsigned int getDefaultInputDevice(void)
A function that returns the ID of the default input device.
Definition RtAudio.h:914
std::vector< unsigned int > getDeviceIds(void)
A public function that returns a vector of audio device IDs.
Definition RtAudio.h:912
unsigned int deviceId
Definition RtAudio.h:304
unsigned int firstChannel
Definition RtAudio.h:306
The structure for specifying input or output stream parameters.
Definition RtAudio.h:302

In this example, we pass the address of the stream parameter structure as the second argument of the RtAudio::openStream() function and pass a NULL value for the output stream parameters. In this example, the record() callback function performs no specific operations.


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