Jump to content
  • Advertisement
Sign in to follow this  
Barry79

Can't play sine wave with dsound

This topic is 4451 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to play a sine wave through my sound card, but I haven't been sucessful. My code is below and I'm not getting any DS_ERRORS. Having said that, if I change DSBPLAY_LOOPING in the Play routine to 0, I get - HRESULT: 0x00000057 (87) Name: ERROR_INVALID_PARAMETER not sure why. Also, after I run the program below, I've noticed that if I try to adjust the volume in the volume control windows applet, my computer becomes really slow and I have to restart it. I guess I'm writing to somewhere I shouldn't be and disrupting the sound card. Any ideas what I'm doing wrong in my code? Any bugging suggestions? Thanks very much for your help, Barry.
#define MY_BUFSIZE 1024 // Buffer size for console window titles.

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "dsound.h"


int main()
{

	HWND hwndFound;         // This is what is returned to the caller.
	char pszWindowTitle[MY_BUFSIZE]; // Contains WindowTitle.
       // Fetch current window title.
	GetConsoleTitle(pszWindowTitle, MY_BUFSIZE);
  	hwndFound=FindWindow(NULL, pszWindowTitle);

	LPDIRECTSOUND		lpds;
	LPDIRECTSOUNDBUFFER 	lpdsBuffer;
	DSBUFFERDESC 		m_BufferDesc;

	DirectSoundCreate(NULL, &lpds, NULL);
	if(lpds->SetCooperativeLevel(hwndFound, DSSCL_WRITEPRIMARY )!=DS_OK)
	{
		printf("NOT OK");
		return 0;
	}

	DSBUFFERDESC		dsbd;
    	ZeroMemory( &dsbd, sizeof(DSBUFFERDESC) );
    	dsbd.dwSize        = sizeof(DSBUFFERDESC);
    	dsbd.dwFlags       = DSBCAPS_PRIMARYBUFFER;
    	dsbd.dwBufferBytes = 0;
    	dsbd.dwReserved = 0;
    	dsbd.lpwfxFormat = 0;

	if(lpds->CreateSoundBuffer( &dsbd, &lpdsBuffer, NULL )!=DS_OK)
	{
		printf("NOT OK");
		return 0;
	}

	WAVEFORMATEX		wfx;

	ZeroMemory( &wfx, sizeof(WAVEFORMATEX) );
    	wfx.wFormatTag      = WAVE_FORMAT_PCM;
    	wfx.nSamplesPerSec =  44100;
    	wfx.wBitsPerSample = 16;
    	wfx.nChannels = 2;
    	wfx.nBlockAlign = wfx.nChannels * ( wfx.wBitsPerSample / 8 );
    	wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;

	if(lpdsBuffer->SetFormat(&wfx)!=DS_OK)
	{
		printf("NOT OK");
		return 0;
	}

	LPVOID lpvWrite = NULL;
	DWORD  dwLength;
	short *pbData;
	pbData = new short[30000];
	for(int x = 0; x < 30000; x++)
     		pbData[x] = sin((x/180.0f*M_PI)*4) *0x3fff; 

	if (DS_OK == lpdsBuffer->Lock(
      		0,          // Offset at which to start lock.
      		0,          // Size of lock; ignored because of flag.
      		&lpvWrite,  // Gets address of first part of lock.
      		&dwLength,  // Gets size of first part of lock.
      		NULL,       // Address of wraparound not needed.
      		NULL,       // Size of wraparound not needed.
      		DSBLOCK_ENTIREBUFFER))  // Flag.
	{
 		printf("%d",lpvWrite);
		printf("\n%d",dwLength);
		memcpy(lpvWrite, pbData,dwLength);

  		lpdsBuffer->Unlock(
      			lpvWrite,   // Address of lock start.
      			dwLength,   // Size of lock.
      			NULL,       // No wraparound portion.
      			0);         // No wraparound size.
	}
	else
	{
		printf("NOT OK");
		return 0;
	}

	lpdsBuffer->SetCurrentPosition(0);

	
	if (DS_OK != lpdsBuffer->Play(
    		0,  // Unused.
    		0,  // Priority for voice management.
    		DSBPLAY_LOOPING)) // Flags.
	{
  		printf("NOT OK");
		return 0;
	}

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
I'm still haivng no luck with this. Can anyone suggest possible problems with my code? Is it even valid to write directly to the primary buffer as I do? Most of the tutorials I have found stop once CreateSoundBuffer has been issued (http://bdn.borland.com/article/20941 for example), so I'm unsure if the code following this point is correct.

Thanks for your help,

Barry.

Share this post


Link to post
Share on other sites
I removed lpdsBuffer->SetCurrentPosition(0); from my code because I notited in the docs that it shouldn't be called on primary buffers. Now I get a short beep - but no looping. At least I fell like I'm getting somewhere now...

Share this post


Link to post
Share on other sites
I compiled your code and it appears that the program just ends. If you put a Sleep() at the end of the program or wait for user input, the sound plays fine. I also get a warning about m_BufferDesc being unused. Shouldn't you do something with that?

Share this post


Link to post
Share on other sites
Quote:
Original post by paradoxnj
Your cooperative level is suspect. Try using DSSCL_PRIORITY or DSSCL_EXCLUSIVE.
You need that priority level to be able to lock the primary buffer.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!