首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenAL播放的固有延迟高于预期(Windows、C++)

OpenAL播放的固有延迟高于预期(Windows、C++)
EN

Stack Overflow用户
提问于 2020-10-15 14:04:10
回答 1查看 263关注 0票数 0

TL/DR:一个简单的echo程序,可以立即记录和播放音频,但显示出比预期更高的延迟。

我正在开发一个实时音频广播应用程序。我决定使用OpenAL来捕获和回放音频样本。我计划通过LAN网络发送包含原始PCM数据的UDP数据包。在一台机器上录制和在另一台机器上播放之间的理想延迟是30ms。(一个远大的目标)

作为测试,我编写了一个小程序,将麦克风中的样本录制下来,并立即将它们回放给主持人。我这样做是为了测试基准延迟。然而,我发现简单地录制和回放音频的固有延迟约为65 - 70毫秒。我已经将openAL使用的缓冲区大小减少到每秒44100个样本的100个样本。理想情况下,这将产生2-3毫秒的延迟。

我还没有在另一个平台(MacOS / Linux)上尝试过,以确定这是一个OpenAL问题还是一个Windows问题。

代码如下:

代码语言:javascript
复制
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, AL_FORMAT_MONO16, buffer, samplesIn * sizeof(short), FREQ);

                // Queue the buffer
                alSourceQueueBuffers(helloSource, 1, &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。

Waveform with 70ms echo delay

系统规格:

英特尔酷睿i7-9750H 24 GB Ram Windows 10 Home: v2004- Build 19041.508声卡驱动程序: Realtek Audio (驱动程序版本10.0.19041.1)输入设备:罗技G330耳机

此问题可在其它Windows系统上重现。

编辑:

我尝试使用PortAudio来做类似的事情,并获得了类似的结果。我已经确定这是由于Windows音频驱动程序造成的。我只用ASIO音频重建了PortAudio,并安装了ASIO4ALL音频驱动。这已经实现了<10ms的可接受延迟。

EN

回答 1

Stack Overflow用户

发布于 2020-10-16 07:28:07

我最终解决了这个问题,放弃了OpenAL,转而支持PortAudio和Steinberg ASIO。我安装了ASIO4ALL并重新构建了PortAudio,只接受ASIO设备驱动程序。为此,我需要使用Steinberg的ASIO。(遵循指南here)。这使我可以实现5到10毫秒的延迟。

这篇文章帮助很大:input delay with PortAudio callback and ASIO sdk

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64365721

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档