c语言 - 不调用消息"MM_WOM_DONE"



我有一个问题:音频块播放完成后,我不会收到"MM_WOM_DONE"。

与声音相关的代码:

void initSound(HWND hwnd) {
HWAVEOUT hWaveOut;
WAVEFORMATEX wfx;
wfx.nSamplesPerSec = 44100; /* sample rate */
wfx.wBitsPerSample = 8; /* sample size */
wfx.nChannels = 1; /* channels*/
wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, (DWORD)hwnd, 0, CALLBACK_WINDOW) != MMSYSERR_NOERROR) {
printf("Wave out can't be opened!n");
ExitProcess(1);
} else {
printf("Wave out is now open!n");
}
/* Building a block */
DWORD size = 8192;
int frequency = 523; // C note
int volume = 20;
float sinMul = 3.1415 * frequency / (wfx.nSamplesPerSec);
unsigned char blockArray[size];
for (int i = 0; i < size; i++) {
blockArray[i] = 128 + (signed char)(sin(i * sinMul) * volume);
}
WAVEHDR header;
ZeroMemory(&header, sizeof(WAVEHDR));
header.dwBufferLength = size;
header.lpData = (LPSTR)&blockArray;
printf("Block was created!n");
/* Prepearing header */
printf("Prepearing header for wave out...n");
waveOutPrepareHeader(hWaveOut, &header, sizeof(WAVEHDR));
/* Playing a block */
printf("Writing sound to wave out...n");
waveOutWrite(hWaveOut, &header, sizeof(WAVEHDR));
printf("Waiting sound for finishing playing...n");
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
initSound(hwnd);
break;
case MM_WOM_DONE:
printf("Sine wave stopped playing!");
waveOutUnprepareHeader((HWAVEOUT)wParam, (LPWAVEHDR)lParam, sizeof(WAVEHDR));
waveOutClose((HWAVEOUT)wParam);
break;
}
}

控制台输出:

Wave out is now open!
Block was created!
Prepearing header for wave out...
Writing sound to wave out...
Waiting sound for finishing playing...

正弦波播放良好且正确,但一旦完成播放,一些短的白噪声就会开始播放短时间(近 100-200 毫秒(。此外,"正弦波停止播放!"消息永远不会出现在控制台中,这意味着WndProc函数永远不会被"MM_WOM_DONE"消息调用。如果我在"initSound"函数中处理所有内容,即使写入和关闭waveOut之间的延迟要大得多(超过一秒(,正弦波最终也不会播放白噪声。

感谢Remy Lebeau解决这个问题。我犯的错误是将波头和波出头存储在堆栈中而不是堆中。这会导致出现噪声,因为数据被覆盖并且声卡开始读取无效数据。挥出标题也可能丢失或/和覆盖,因此我无法正确调用MM_WOM_DONE消息。所有这些都只是为了在函数外部声明变量而解决的。就像这样:

HWAVEOUT hWaveOut;
WAVEFORMATEX wfx;
DWORD size = 8192;
WAVEHDR header;
void initSound(HWND hwnd) {
/* Body of function */
}

因此,如果您遇到此问题,请确保与 waveout 相关的变量存活足够长的时间。

相关内容

  • 没有找到相关文章

最新更新