Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Frequency and DirectSound

This topic is 5535 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

Hey, I was wondering why when I put SetFrequency(parms); it didn''t change the way it sounded? Heres my code for it:
int Game_Init(void *parms)
// this function is where you do all the initialization 

// for your game

// this example does everything: it sets up directsound

// creates a secondary buffer, loads it with a synthesizer

// sine wave and plays it

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

		*audio_ptr_2 = NULL;

DWORD	dsbstatus;              // status of sound buffer

DWORD	audio_length_1    = 0,  // length of locked memory

		audio_length_2    = 0,
		snd_buffer_length = 64000; // working buffer

// allocate memory for buffer

UCHAR *snd_buffer_ptr = (UCHAR *)malloc(snd_buffer_length);

// we need some data for the buffer, you could load a .VOC or .WAV

// but as an example, lets synthesize the data

// fill buffer with a synthesized 100hz sine wave

for (int index=0; index < (int)snd_buffer_length; index++)
	snd_buffer_ptr[index] = 127*sin(6.28*((float)(index%110))/(float)110);

// note the math, 127 is the scale or amplitude

// 6.28 is to convert to radians

// (index % 110) read below

// we are playing at 11025 hz or 11025 cycles/sec therefore, in 1 sec

// we want 100 cycles of our synthesized sound, thus 11025/100 is approx.

// 110, thus we want the waveform to repeat each 110 clicks of index, so

// normalize to 110

// create a directsound object

if (DirectSoundCreate(NULL, &lpds, NULL)!=DS_OK )

// set cooperation level

if (lpds->SetCooperativeLevel(main_window_handle,DSSCL_NORMAL)!=DS_OK)

// set up the format data structure

memset(&pcmwf, 0, 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;

// create the secondary buffer (no need for a primary)

dsbd.dwSize			= sizeof(DSBUFFERDESC);
dsbd.dwBufferBytes	= snd_buffer_length+1;
dsbd.lpwfxFormat	= &pcmwf;

if (lpds->CreateSoundBuffer(&dsbd,&lpdsbsecondary,NULL)!=DS_OK)

// copy data into sound buffer

if (lpdsbsecondary->Lock(0,					 

// copy first section of circular buffer

CopyMemory(audio_ptr_1, snd_buffer_ptr, audio_length_1);

// copy last section of circular buffer

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

// unlock the buffer

if (lpdsbsecondary->Unlock(audio_ptr_1, 

// play the sound in looping mode

if (lpdsbsecondary->Play(0,0,DSBPLAY_LOOPING )!=DS_OK)
if (FAILED(lpdsbsecondary->SetVolume(DSVOLUME_TO_DB(200))))
if (FAILED(lpdsbsecondary->SetFrequency(22050)))
// release the memory since DirectSound has made a copy of it


// return success


} // end Game_Init
The code for the SetFrequency is at the bottom of the code, but it still never hurst to display the whole Game_Init in case I messed up somewhere else. Thanks everyone, you''ve been a major help! -Sathenzar
The ultament game coming to your local stores soon, an awsome breath taking gameplay and an incredable story line. Watch as the ultament threat surfaces to the earth as a demon''s power is unleashed and all mortals will ethier unite as one to defend, or face to demon''s wraith as he makes his way to his quest in the Desingath''s Tower. Read more on October 24, 2003. http//:ww.demonrealmstudios.com/games/storyline.php/ (site temporarily unavailible.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!