Home   Class/Enum List   File List   Compound Members  

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;
if ( adc.getDeviceCount() < 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
try {
adc.openStream( NULL, &parameters, RTAUDIO_SINT16,
sampleRate, &bufferFrames, &record );
adc.startStream();
}
catch ( RtAudioError& e ) {
exit( 0 );
}
char input;
std::cout << "\nRecording ... press <enter> to quit.\n";
std::cin.get( input );
try {
// Stop the stream
adc.stopStream();
}
catch (RtAudioError& e) {
}
if ( adc.isStreamOpen() ) adc.closeStream();
return 0;
}

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.

RtAudio::getDeviceCount
unsigned int getDeviceCount(void)
A public function that queries for the number of audio devices available.
Definition: RtAudio.h:830
RtAudio::isStreamOpen
bool isStreamOpen(void) const
Returns true if a stream is open and false if not.
Definition: RtAudio.h:838
RtAudioError
Exception handling class for RtAudio.
Definition: RtAudio.h:204
RtAudio::openStream
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.
RtAudioError::printMessage
virtual void printMessage(void) const
Prints thrown error message to stderr.
Definition: RtAudio.h:227
RtAudio::StreamParameters::firstChannel
unsigned int firstChannel
Definition: RtAudio.h:304
RtAudio::stopStream
void stopStream(void)
Stop a stream, allowing any samples remaining in the output queue to be played.
Definition: RtAudio.h:836
RtAudio::closeStream
void closeStream(void)
A function that closes a stream and frees any associated stream memory.
Definition: RtAudio.h:834
RtAudio::StreamParameters::nChannels
unsigned int nChannels
Definition: RtAudio.h:303
RtAudio::startStream
void startStream(void)
A function that starts a stream.
Definition: RtAudio.h:835
RtAudio::getDefaultInputDevice
unsigned int getDefaultInputDevice(void)
A function that returns the index of the default input device.
Definition: RtAudio.h:832
RtAudioStreamStatus
unsigned int RtAudioStreamStatus
RtAudio stream status (over- or underflow) flags.
Definition: RtAudio.h:144
RtAudio
Realtime audio i/o C++ classes.
Definition: RtAudio.h:264
RtAudio::StreamParameters
The structure for specifying input or ouput stream parameters.
Definition: RtAudio.h:301
RtAudio::StreamParameters::deviceId
unsigned int deviceId
Definition: RtAudio.h:302
RtAudio.h

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