我正在尝试在 Linux 中使用 RtAudio。首先,我在启用插孔的情况下对其进行了编译:
$ ./configure --with-alsa --with-jack
$ make
$ make install
然后我找到了一个小例子来测试 RtAudio:
#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;
}
这个例子没有做任何特别的事情。它只会尝试录制一些音频,甚至不会等待它捕获任何内容,它会立即退出。我第一次运行此代码时,它运行并退出,没有任何错误。但是第二次,它会出错:
$ ./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.
如果我想摆脱这个错误,我必须重新启动机器。
我必须承认,有时它会在重新启动后再次起作用。但在大多数情况下,它会出错。正如我一直试图了解问题所在,在 Linux 中似乎jack
可以用来确保没有软件会保留音频资源,并且多个进程可以同时使用音频资源。如果是这种情况,考虑到我已经在启用插孔的情况下编译了 RtAudio,为什么我仍然面临这个错误,我该如何解决?
顺便说一句,即使我的代码遇到此错误,arecord
也可以毫无问题地录制声音。