一段时间后,音频音调会变尖,然后只会发出声音 这让我认为问题出在我填充缓冲区的方式上
int AudioThreadProc(AUDIOSTRUCT* audio) {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
HANDLE buffReady = CreateEvent(0, 0, 1, 0);
audio->client->lpVtbl->SetEventHandle(audio->client, buffReady);
unsigned int soundBuffSize;
audio->client->lpVtbl->GetBufferSize(audio->client, &soundBuffSize);
audio->client->lpVtbl->Start(audio.client);
static float time = 0;
float dtime = 1 / (float)44100;
for (;;) {
WaitForSingleObject(buffReady, INFINITE);
unsigned int soundFrames;
audio->client->lpVtbl->GetCurrentPadding(audio->client, &soundFrames);
unsigned int FramesToFill = soundBuffSize - soundFrames;
unsigned short int* soundBuff;
audio->render->lpVtbl->GetBuffer(audio.render, FramesToFill, &soundBuff);
for (unsigned int i = 0; i < FramesToFill; i++) {
unsigned short int soundWave = audio->callback(time);
time += dtime;
*soundBuff++ = soundWave;
*soundBuff++ = soundWave;
}
audio->render->lpVtbl->ReleaseBuffer(audio.render, FramesToFill, 0);
}
return 0;
}
回调函数仅是一个正弦波发生器
float callback(float time){
returns sin(pitch * PI2 * time) * 0xFFFF;
}
//...
audio.callback = callback;
答案 0 :(得分:0)
我认为这里的答案几乎等于几年前我给的this answer。随着time
的增加,计算pitch*2π*time
的浮点精度下降。调整代码,使time
重置为零或为,并在完成一整波数据后从零开始偏移。
类似这样的东西:
time += dtime;
if (time > 1.0) {
time -= 1.0;
}
此外,每个示例调用一个函数调用的总开销也可能是一个问题。将您的callback
转换为宏。你不会后悔的。