I run the following code:
void SoundOut()
{
HWAVEOUT wo;
WAVEHDR whdr;
whdr.dwBufferLength = std::vector<SampleType>::size() * m_format.nBlockAlign;
whdr.lpData = reinterpret_cast<LPSTR>(&(std::vector<SampleType>::front()));
whdr.dwFlags = 0;
cout << "waveOpen result " << waveOutOpen(&wo, WAVE_MAPPER, &m_format, 0, 0, CALLBACK_NULL) << endl;;
cout << "waveOutPrepareHeader" << waveOutPrepareHeader(wo, &whdr, sizeof(WAVEHDR)) << endl;;
cout << "waveOutWrite" << waveOutWrite(wo, &whdr, sizeof(WAVEHDR)) << endl;;
cout << "waveOutUnprepareHeader" << waveOutUnprepareHeader(wo, &whdr, sizeof(WAVEHDR)) << endl;;
cout << "waveOutClose" << waveOutClose(wo) << endl;;
};
The stdout looks like the following:
waveOpen result 0
waveOutPrepareHeader 0
waveOutWrite 0
waveOutUnprepareHeader 33
waveOutClose 33
According to SOUNDWIN.H file the code 33 means WAVERR_STILLPLAYING. But no any sound is heard and waveOutWrite returns immediatelly (writting 88000 samples to a sound card). Suggestly this immediate return implies code 33, but then why waveOutWrite reurns immediatelly and nothing is heard?
And generally, why waveOutWrite finished successfully and nothing sounds?
Where to look?
You are supposed to prepare buffers once and then unprepare them when playback is completed. The buffers can be reused in between but it is not generally a good idea to prepare/unprepare buffers along with playback.
The problem comes from the fact that when you write the data is queued for playback immediately (and remember that waveOut API is emulation/compatibility layer only), however the buffers are returned back later on a delayed callback. See WOM_DONE message on this. It is only allowed to unprepare when the buffer is no longer in use by the API.