I'm trying to use RtAudio in Linux. To start, I've compiled it with jack enabled:
$ ./configure --with-alsa --with-jack
$ make
$ make install
Then I found a small example to test RtAudio out:
#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! size:" << nBufferFrames << std::endl;
// Do something with the data in the "inputBuffer" buffer.
return 0;
}
int main( int argc, char* argv[] )
{
RtAudio adc;
if ( adc.getDeviceCount() < 1 ) {
std::cout << "\nNo audio devices found!\n";
exit( 1 );
}
adc.showWarnings( true );
RtAudio::StreamParameters parameters;
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, ¶meters, RTAUDIO_SINT16,
sampleRate, &bufferFrames, &record );
adc.startStream();
}
catch ( RtAudioError& e ) {
e.printMessage();
if ( adc.isStreamOpen() ) adc.closeStream();
exit( 1 );
}
char input;
std::cout << "\nRecording ... press <enter> to quit.\n";
try {
// Stop the stream
adc.stopStream();
}
catch (RtAudioError& e) {
e.printMessage();
if ( adc.isStreamOpen() ) adc.closeStream();
}
return 0;
}
This example doesn't do anything special. It will just try to record some audio and it doesn't even wait for it to capture anything and it will quit right away. For the first time that I run this code, it runs and quits without any errors. But the second time forth, it will error out:
$ ./test
RtApiAlsa::getDeviceInfo: snd_pcm_open error for device (hw:0,3), Device or resource busy.
RtApiAlsa::getDeviceInfo: snd_pcm_open error for device (hw:2,0), Device or resource busy.
Recording ... press <enter> to quit.
If I want to get rid of this error, I have to restart the machine.
I must confess that sometimes, it works again with a restart. But for the most part, it errors out. As I've been trying to understand what the problem is, it seems like in Linux jack
could be used to make sure that no software will hold on to the audio resource and multiple processes could use the audio resource at the same time. If that's the case, considering the fact that I've compiled the RtAudio with jack enabled, why I'm still facing this error and how can I fix it?
BTW, even when my code is facing this error, arecord
can record sound without any issues.
For anyone else who might be dealing with the same problem, here is how I fixed it:
$ ./configure --with-alsa --with-jack --with-pulse
$ make
$ make install