Jump to content
  • Advertisement
Sign in to follow this  
d3x

Playing sounds in DirectSound

This topic is 4849 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 guys I have a strange problem so let me explain class DirectSound { //initialize DirectSound8 interface } class DirectSound2D : public DirectSound { bool Play(char* FileName); } what method play does is: -reads header information from wav file that i want to play and gets all information needed to create secondary sound buffer like channels, sample rate... -creates secondary buffer for that wav file -fills the secondary buffer with sound data -plays the buffer -Releases the secondary buffer Its not problem to play a wav file at all, BUT buffer is released before the sound file is played because as soon as te buffer starts to play program goes to the nest statement that is Release(); so buffer is released before its played so i deleted this last statement, Release(); and the buffer plays nice, but memory is not freed after the playing is complete. is there any way to notify the program that the buffer is finished playing and is ready to be released from memory? so when its finished playing, release it! what i want is to play sounds like this: DirectSound2D dSound; dSound.Play("C:\\sound.wav"); //continue with program //when its finished playing release the play buffer dSound.Play("C:\\anotherSound.wav"); So every time i run play function i want it to create buffer for every sound in the game, and release it after its stopped playing. Im really sorry for the long post. hope someone has experience with sounds in game.

Share this post


Link to post
Share on other sites
Advertisement
Hi

I don't know directsound, but seeing that you are using classes you might want to put the release of your buffer in your class destructor, thus it is only released when your class is destroyed (IE when program ends or if you use new, when you call delete)

If your not sure what I am talking about look up on how class destructors work.

Hope this helps

Share this post


Link to post
Share on other sites
when i do this

DirectSound2D dSound();

it initializes only DirectSound8 interface, so i only have to use one object of the DirectSound2D because i dont want more than one interface

so when i want to play two sounds at the same time, i do this

dSound.Play("first.wav");
dSound.Play("second.wav");

so in this example the sound buffer is created in method play so there is one object(dSound), and two sound buffers(in two Play functions) and if i now want to destruct dSound it would delete all my sound buffers and thats great, but what if i play 10 different sounds (thats ten sound buffers) and those 10 buffers are not released until the end of the program and they accumulate in the programs memory and they take resources that i dont use.

i want to release the buffer after the sound has stopped playing, that way the memory is released immediately.

Has anyone done sounds for games before to show me how they implemented playing multiple sounds?

Share this post


Link to post
Share on other sites
Do you have to use DirectSound? I ask because it is overly-complicated if you just want to play simple WAV files. I would suggest looking at FMOD.

Share this post


Link to post
Share on other sites
First off, know that DirectSound is meant to be a fairly powerful API,
it gives you lots of freedom in how you implement your sound system.

This comes at the cost of simplicity though, for instance there is no way to simply 'play a wave file', or more importantly 'stream a wave file from disk'.

those are features you will need to implement yourself,

in short, DirectSound is an API for playing simultanius buffers of PCM wave data, and potentially taking advantage of hardware mixing.




If you do use DSound, fyou will need to build some mechanisim for detecting that a sound is done playing, and if it is, destory the buffer.

this can be done by periodically querying the Playing flag of the buffer.

a typical system might put all playing buffers into a list, then every second, go through the list checking for buffers that are not playing.

Any buffer that is not playing can be removed from the list and the buffer can be destroyed.

There IS also a directsound notification framework, but that ususaly requires the use of Threads, which can seriously complicate matters, especially if you are not a seasoned thread user =D.

Share this post


Link to post
Share on other sites
There is a class written by microsoft its called CSoundManager its used to play wav files but i dont wanna use someone else's code, its better to to it on my own :)

Ive come up with an idea to use linked list that will contain secondary buffers for every sound. and every time the buffer is created its stored in a linked list.
ill probably have one more function that releases those buffers.

check buffer state every 1second
-if the buffer is not playing
- release it :)
-if the buffer is playing
- continue to check another buffer in the linked list

i dont really know how to implement checking every x second. i have heard of timers, maybe they will help me with it. gonna read a few chapters and come back :))))

Share this post


Link to post
Share on other sites
Okay there is a few ways to get release the buffers... one would be to put it your destructor or maybe a free method. Then you could obviously delete it whenever you want.

Another could be to use timers or a while loop and check to see if the sound is currently playing. You do that by creating a soundnotification.

Some code from the documentation...

HRESULT SetStopNotification(HANDLE hMyEvent,
LPDIRECTSOUNDBUFFER8 lpDsbSecondary)
{
LPDIRECTSOUNDNOTIFY8 lpDsNotify;
DSBPOSITIONNOTIFY PositionNotify;
HRESULT hr;

if (SUCCEEDED(
hr = lpDsbSecondary->QueryInterface(IID_IDirectSoundNotify8,
(LPVOID*)&lpDsNotify)))
{
PositionNotify.dwOffset = DSBPN_OFFSETSTOP;
PositionNotify.hEventNotify = hMyEvent;
hr = lpDsNotify->SetNotificationPositions(1, &PositionNotify);
lpDsNotify->Release();
}
return hr;

Share this post


Link to post
Share on other sites
Quote:
Original post by d3x
There is a class written by microsoft its called CSoundManager its used to play wav files but i dont wanna use someone else's code, its better to to it on my own :)


There's nothing wrong with using others code like that. I mean all of DirectX is code written by Microsoft!

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!