如何解决OpenAL播放Windows,C ++中的固有延迟高于预期
TL / DR:一个简单的回声程序可以立即记录并播放音频,显示出的延迟高于预期。
我正在研究实时音频广播应用程序。我决定使用OpenAL来捕获和回放音频样本。我正在计划通过LAN网络发送带有原始PCM数据的UDP数据包。一台机器上录制到另一台机器上播放之间的理想延迟时间是30ms。 (一个崇高的目标)。
作为测试,我制作了一个小程序,该程序记录了麦克风中的样本,并立即将其播放回主扬声器。我这样做是为了测试基线延迟。 但是,从简单的录制音频和播放音频中,我看到固有的延迟约为65-70 ms。 我已将openAL使用的缓冲区大小减小为每秒44100个样本的100个样本。理想情况下,这将产生2-3 ms的延迟。
我尚未在其他平台(MacOS / Linux)上尝试此操作,以确定这是OpenAL问题还是Windows问题。
这是代码:
using std::list;
#define FREQ 44100 // Sample rate
#define CAP_SIZE 100 // How much to capture at a time (affects latency)
#define NUM_BUFFERS 10
int main(int argC,char* argV[])
{
list<ALuint> bufferQueue; // A quick and dirty queue of buffer objects
ALuint helloBuffer[NUM_BUFFERS],helloSource;
ALCdevice* audioDevice = alcopenDevice(NULL); // Request default audio device
ALCcontext* audioContext = alcCreateContext(audioDevice,NULL); // Create the audio context
alcMakeContextCurrent(audioContext);
// Request the default capture device with a ~2ms buffer
ALCdevice* inputDevice = alcCaptureOpenDevice(NULL,FREQ,AL_FORMAT_MONO16,CAP_SIZE);
alGenBuffers(NUM_BUFFERS,&helloBuffer[0]); // Create some buffer-objects
// Queue our buffers onto an STL list
for (int ii = 0; ii < NUM_BUFFERS; ++ii) {
bufferQueue.push_back(helloBuffer[ii]);
}
alGenSources(1,&helloSource); // Create a sound source
short* buffer = new short[CAP_SIZE]; // A buffer to hold captured audio
ALCint samplesIn = 0; // How many samples are captured
ALint availBuffers = 0; // Buffers to be recovered
ALuint myBuff; // The buffer we're using
ALuint buffHolder[NUM_BUFFERS]; // An array to hold catch the unqueued buffers
alcCaptureStart(inputDevice); // Begin capturing
bool done = false;
while (!done) { // Main loop
// Poll for recoverable buffers
alGetSourcei(helloSource,AL_BUFFERS_PROCESSED,&availBuffers);
if (availBuffers > 0) {
alSourceUnqueueBuffers(helloSource,availBuffers,buffHolder);
for (int ii = 0; ii < availBuffers; ++ii) {
// Push the recovered buffers back on the queue
bufferQueue.push_back(buffHolder[ii]);
}
}
// Poll for captured audio
alcGetIntegerv(inputDevice,ALC_CAPTURE_SAMPLES,1,&samplesIn);
if (samplesIn > CAP_SIZE) {
// Grab the sound
alcCaptureSamples(inputDevice,buffer,samplesIn);
// Stuff the captured data in a buffer-object
if (!bufferQueue.empty()) { // We just drop the data if no buffers are available
myBuff = bufferQueue.front(); bufferQueue.pop_front();
alBufferData(myBuff,samplesIn * sizeof(short),FREQ);
// Queue the buffer
alSourceQueueBuffers(helloSource,&myBuff);
// Restart the source if needed
// (if we take too long and the queue dries up,// the source stops playing).
ALint sstate = 0;
alGetSourcei(helloSource,AL_SOURCE_STATE,&sstate);
if (sstate != AL_PLAYING) {
alSourcePlay(helloSource);
}
}
}
}
// Stop capture
alcCaptureStop(inputDevice);
alcCaptureCloseDevice(inputDevice);
// Stop the sources
alSourceStopv(1,&helloSource);
alSourcei(helloSource,AL_BUFFER,0);
// Clean-up
alDeleteSources(1,&helloSource);
alDeleteBuffers(NUM_BUFFERS,&helloBuffer[0]);
alcMakeContextCurrent(NULL);
alcDestroyContext(audioContext);
alccloseDevice(audioDevice);
return 0;
}
这是波形图,显示了输入声音的延迟和产生的回声。此示例显示了大约70ms的延迟。
系统规格:
英特尔酷睿i7-9750H 24 GB内存 Windows 10 Home:V 2004-内部版本19041.508 声音驱动程序:Realtek Audio(驱动程序版本10.0.19041.1) 输入设备:Logitech G330耳机
问题可以在其他Windows系统上重现。
编辑:
我尝试使用PortAudio做类似的事情,并获得了类似的结果。我确定这是由于Windows音频驱动程序引起的。 我仅使用ASIO音频重建了PortAudio并安装了ASIO4ALL音频驱动程序。这样可以实现小于10ms的可接受延迟。
解决方法
我最终通过放弃OpenAL转向PortAudio和Steinberg ASIO解决了这个问题。我安装了ASIO4ALL并重建了PortAudio以仅接受ASIO设备驱动程序。我需要使用Steinberg中的ASIO SDK来执行此操作。 (遵循指南here)。这使我获得了5到10毫秒之间的延迟。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。