🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

DirectSound fun!!!

Started by
0 comments, last by Abort_Retry_Fail 22 years, 1 month ago
Does anyone know why this code doesn''t work? It''s practically right out of Windows GP for Dummies by Lamothe. The sound won''t play, and I keep getting the little message box that I set up at the end. There are no compiling errors.
  

//BEGIN DIRECT SOUND SETUP//////////////////////


	LPDIRECTSOUND lpds;
	if(DirectSoundCreate(NULL,&lpds,NULL)!=DS_OK) ErrorMsg("Error creating DirectSound interface!",hwnd);
	//Set the cooperative level

	lpds->SetCooperativeLevel(hwnd,DDSCL_NORMAL);

	void 	*audio_ptr_1 = NULL,   // used to lock memory

			*audio_ptr_2 = NULL;
	DWORD	dsbstatus;              // status of sound buffer

	DWORD	audio_length_1 = 0,audio_length_2 = 0; //two sections of circular buffer

	DWORD snd_buffer_length = 64000; //sound buffer length

	UCHAR * snd_buffer = (UCHAR *)malloc(snd_buffer_length);	//the sound buffer (to copied to the locked secondary buffer)


	// fill buffer with a synthesized 500hz sine wave

	for (int x=0; x < (int)snd_buffer_length; x++)
		snd_buffer[x] = 127*sin(6.28*(x%22));

	//A secondary sound buffer

	LPDIRECTSOUNDBUFFER lpdsbuffer;
	//Set up a sound format structure

	WAVEFORMATEX pcmwf;
	ZeroMemory(&pcmwf,sizeof(WAVEFORMATEX));
	pcmwf.wFormatTag	  = WAVE_FORMAT_PCM;
	pcmwf.nChannels		  = 1;
	pcmwf.nSamplesPerSec  = 11025;
	pcmwf.nBlockAlign	  = 1;
	pcmwf.nAvgBytesPerSec = pcmwf.nSamplesPerSec * pcmwf.nBlockAlign;
	pcmwf.wBitsPerSample  = 8;
	pcmwf.cbSize		  = 0;
	//Set a sound buffer description

	DSBUFFERDESC dsbd;
	ZeroMemory(&dsbd,sizeof(DSBUFFERDESC));
	dsbd.dwSize			= sizeof(DSBUFFERDESC);
	dsbd.dwFlags		= DSBCAPS_CTRLDEFAULT | DSBCAPS_STATIC | DSBCAPS_LOCSOFTWARE;
	dsbd.dwBufferBytes	= snd_buffer_length+1;
	dsbd.lpwfxFormat	= &pcmwf

	//Finally, create the sound buffer

	if(lpds->CreateSoundBuffer(&dsbd,&lpdsbuffer,NULL)!=DS_OK) ErrorMsg("Error with creating secondary sound buffer!",hwnd);

////////////////////////////////////////////////


// copy data into sound buffer

lpdsbuffer->Lock(0,snd_buffer_length,&audio_ptr_1,&audio_length_1,&audio_ptr_2,&audio_length_2,DSBLOCK_FROMWRITECURSOR);

// copy first section of circular buffer

CopyMemory(audio_ptr_1, snd_buffer, audio_length_1);
// copy last section of circular buffer

CopyMemory(audio_ptr_2, (snd_buffer+audio_length_1),audio_length_2);

// unlock the buffer

lpdsbuffer->Unlock(audio_ptr_1,audio_length_1,audio_ptr_2,audio_length_2);

// play the sound in looping mode

if (lpdsbuffer->Play(0,0,DSBPLAY_LOOPING)!=DS_OK) ErrorMsg("Unable to play sound",hwnd);

// release the memory since DirectSound has made a copy of it

free(snd_buffer);

  
Advertisement
Check your return values on every DirectSound call, and find out what they mean, not just whether they''re equal to DS_OK or not.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files ]

This topic is closed to new replies.

Advertisement