我试图在Linux中使用RtAudio。首先,我在启用jack的情况下编译了它:
$ ./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来确保没有软件能够保存音频资源,并且多个进程可以同时使用音频资源。如果是这样的话,考虑到我已经在启用jack的情况下编译了RtAudio,为什么我仍然面临这个错误,以及如何修复它?
顺便说一句,即使我的代码面临这个错误,arecord也可以记录声音,没有任何问题。
发布于 2022-02-17 22:27:05
对于其他可能正在处理同样问题的人,我是如何解决这个问题的:
$ ./configure --with-alsa --with-jack --with-pulse
$ make
$ make installhttps://stackoverflow.com/questions/71121321
复制相似问题