What's wrong with this?

I'm trying to make a .wav loader/player, and I based the part that plays it off of some code I found on the internet designed to create the sound in memory and play it (which is probably why there's an error). So, here's my code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool play_sound(sound& snd){
	HWAVEOUT out;
	HANDLE done=CreateEvent(0,false,false,0);
	//IT CRASHES HERE vvv
	if(waveOutOpen(&out,0,&snd.format,(DWORD)done,0,CALLBACK_EVENT)!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to open soundcard","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	//IT CRASHES THERE ^^^
	if (waveOutPrepareHeader(out,&snd.data,sizeof(WAVEHDR))!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to prepare header","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	ResetEvent(done);
	if(waveOutWrite(out,&snd.data,sizeof(WAVEHDR))!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to write to soundcard","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	if(WaitForSingleObject(done,INFINITE)!=WAIT_OBJECT_0){
		MessageBox(0,"ERROR: Failed to wait for sound to finish","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	if(waveOutUnprepareHeader(out,&snd.data,sizeof(WAVEHDR))!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to unprepare header","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	if(waveOutClose(out)!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to close soundcard","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	CloseHandle(done);
	return true;}


I got the code from here: http://www.tmsoft.com/tutorial-sound.html

If you need the sound struct, or the loading function, I'll provide it.
Last edited on
Ok, with a little experimenting, I found two logical errors that I didn't notice before (I was thinking that memcmp would return true if they're the same, instead of 0, and I was reading the entire size of WAVEFORMATEX, when its cbSize member isn't in the file)

Now, I actually get a messagebox that shows an error. It fails on the waveOutPrepareHeader function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
bool play_sound(sound& snd){
	HWAVEOUT out;
	HANDLE done=CreateEvent(0,false,false,0);
	if(waveOutOpen(&out,0,&snd.format,(DWORD)done,0,CALLBACK_EVENT)!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to open soundcard","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	//now it fails here vvv
	if (waveOutPrepareHeader(out,&snd.data,sizeof(WAVEHDR))!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to prepare header","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	//now it fails there ^^^
	ResetEvent(done);
	if(waveOutWrite(out,&snd.data,sizeof(WAVEHDR))!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to write to soundcard","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	if(WaitForSingleObject(done,INFINITE)!=WAIT_OBJECT_0){
		MessageBox(0,"ERROR: Failed to wait for sound to finish","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	if(waveOutUnprepareHeader(out,&snd.data,sizeof(WAVEHDR))!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to unprepare header","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	if(waveOutClose(out)!=MMSYSERR_NOERROR){
		MessageBox(0,"ERROR: Failed to close soundcard","ERROR",MB_OK|MB_ICONEXCLAMATION);
		return false;}
	CloseHandle(done);
	return true;}


Any idea what's causing this? O.o
Last edited on
Now, by fleshing out the error-checking, I've figured out that waveOutPrepareHeader is returning MMSYSERR_INVALPARAM which according to Windows documentation, means:

The buffer's base address is not aligned with the sample size.


I looked at my loader code, and realized that I forgot to set the dwBufferLength member, but when I fixed that, nothing changed... Any ideas?
Well now I feel stupid. I forgot to set the dwFlags member! Do'oh. Thanks for your help guys!
Topic archived. No new replies allowed.