gpt4 book ai didi

c++ - WaveOutWrite 回调创建断断续续的音频

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:59:03 25 4
gpt4 key购买 nike

我有四个缓冲区用于合成器中的音频播放。我最初提交两个缓冲区,然后在回调例程中将数据写入下一个缓冲区,然后提交该缓冲区。

当我生成每个缓冲区时,我只是将一个正弦波放入其中,其周期是缓冲区长度的倍数。

当我执行时,我听到每个缓冲区之间有短暂的停顿。我已将缓冲区大小增加到 44100 Hz 时的 16K 样本,因此我可以清楚地听到整个缓冲区正在播放,但每个缓冲区之间有一个中断。

我认为正在发生的事情是回调函数仅在所有已写入的缓冲区都完成时才被调用。我需要合成保持在回放之前,所以我需要在每个缓冲区完成时进行回调。

人们通常如何解决这个问题?

更新:我被要求添加代码。这是我拥有的:

首先我连接到 WaveOut 设备:

// Always grab the mapped wav device.
UINT deviceId = WAVE_MAPPER;

// This is an excelent tutorial:
// http://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4422&lngWId=3

WAVEFORMATEX wfx;
wfx.nSamplesPerSec = 44100;
wfx.wBitsPerSample = 16;
wfx.nChannels = 1;
wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

_waveChangeEventHandle = CreateMutex(NULL,false,NULL);

MMRESULT res;
res = waveOutOpen(&_wo, deviceId, &wfx, (DWORD_PTR)WavCallback,
(DWORD_PTR)this, CALLBACK_FUNCTION);

我初始化我将要使用的四个框架:

for (int i=0; i<_numFrames; ++i)
{
WAVEHDR *header = _outputFrames+i;
ZeroMemory(header, sizeof(WAVEHDR));
// Block size is in bytes. We have 2 bytes per sample.
header->dwBufferLength = _codeSpec->OutputNumSamples*2;
header->lpData = (LPSTR)malloc(2 * _codeSpec->OutputNumSamples);
ZeroMemory(header->lpData, 2*_codeSpec->OutputNumSamples);
res = waveOutPrepareHeader(_wo, header, sizeof(WAVEHDR));
if (res != MMSYSERR_NOERROR)
{
printf("Error preparing header: %d\n", res - MMSYSERR_BASE);
}
}
SubmitBuffer();
SubmitBuffer();

这是 SubmitBuffer 代码:

void Vodec::SubmitBuffer()
{
WAVEHDR *header = _outputFrames+_curFrame;
MMRESULT res;
res = waveOutWrite(_wo, header, sizeof(WAVEHDR));
if (res != MMSYSERR_NOERROR)
{
if (res = WAVERR_STILLPLAYING)
{
printf("Cannot write when still playing.");
}
else
{
printf("Error calling waveOutWrite: %d\n", res-WAVERR_BASE);
}
}

_curFrame = (_curFrame+1)&0x3;

if (_pointQueue != NULL)
{
RenderQueue();
_nextFrame = (_nextFrame + 1) & 0x3;
}
}

这是我的回调代码:

void CALLBACK Vodec::WavCallback(HWAVEOUT hWaveOut, 
UINT uMsg,
DWORD dwInstance,
DWORD dwParam1,
DWORD dwParam2 )
{
// Only listen for end of block messages.
if(uMsg != WOM_DONE) return;

Vodec *instance = (Vodec *)dwInstance;
instance->SubmitBuffer();
}

RenderQueue 代码非常简单 - 只需将模板缓冲区的一部分复制到输出缓冲区即可:

void Vodec::RenderQueue()
{
double white = _pointQueue->White;
white = 10.0; // For now just override with a constant value
int numSamples = _codeSpec->OutputNumSamples;
signed short int *data = (signed short int *)_outputFrames[_nextFrame].lpData;
for (int i=0; i<numSamples; ++i)
{
Sample x = white * _noise->Samples[i];
data[i] = (signed short int)(x);
}
_sampleOffset += numSamples;
if (_sampleOffset >= _pointQueue->DurationInSamples)
{
_sampleOffset = 0;
_pointQueue = _pointQueue->next;
}
}

更新:基本上解决了这个问题。我需要增加 _nextFrame 和 _curFrame(不是有条件的)。播放缓冲区领先于写入缓冲区。

但是,当我将播放缓冲区减少到 1024 个样本时,它又变得不稳定了。在 2048 个样本中很明显。调试和发布版本都会发生这种情况。

最佳答案

1024 个样本大约是 23 毫秒的音频数据。 wav 是从 Windows Vista 开始的相当高级的 API。如果你想要低延迟音频播放,你应该使用 CoreAudio .您可以在共享模式下将延迟降至 10 毫秒,在独占模式下将延迟降至 3 毫秒。此外,音频取决于您系统上当前运行的进程。换句话说,这取决于您的音频线程运行以获取数据的频率。你还应该看看 MultiMedia Class Scheduler ServiceAvSetMmThreadCharacteristics功能。

关于c++ - WaveOutWrite 回调创建断断续续的音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12754891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com