Skip to main content
GameDev.net gamedev.net
🔒 Locked

DSound / CSoundManager?

Started by Daaark Oct 13, 2005 at 3:16 AM 5 replies 3.5k views
Original Post
Daaark
Daaark
I'm trying to get into DirectSound. But everywhere I have looked, the tutorials are just for DirectMusic. The samples that come with the sdk are using a class called CSoundManager to wrap it all up (how is that supposed to help me learn anything?). I've tried to put it together myself by looking through various things, and I've come up with this to init dsound, is it even correct? I'm thinking now I need to make a DirectSoundBuffer for each wav I need to load? Does anyone know a good tutorial source that shows how to actually use direct sound directly? [grin]
//attempt to init direct sound
bool SoundInit(void)
{    
    if (DirectSoundCreate8(NULL,&lpDS,NULL) != S_OK)
    {
        MessageBox(GetHWND(),"SoundInit(): Failed to init sound device.",
        APP_TITLE,MB_OK|MB_ICONERROR);
        return false;
    }

    if (lpDS->SetCooperativeLevel(GetHWND(),DSSCL_EXCLUSIVE) != S_OK)
    {
        MessageBox(GetHWND(),"SoundInit(): Failed to set cooperation level.",
        APP_TITLE,MB_OK|MB_ICONERROR);
        return false;
    }


    return true;
}


void SoundShutdown(void)
{
    lpDS->Release();
    return;
}
d000hg
d000hg
I on't have time right now to look through it, but you need a sound buffer (IDirectSound8/9) for each sample currently being played. So if you play the same sound twice at once you need to buffers for it.
I find it easiest to load each sound into a DSound buffer so I can just copy it as needed.

I wrote a simple DSound library which supports stereo and 3D functionality - would you like to take a look at the code? Basically there's a Manager class, and each sound buffer is encapsualted in a Sound/Sound3D object. There's really not much to it and its some of my better-written code... unless I already showed it to you because a few people have seen it now?

email the address in my profile if you're interested...
jasonsa
jasonsa
Quote:
Original post by Vampyre_Dark
The samples that come with the sdk are using a class called CSoundManager to wrap it all up (how is that supposed to help me learn anything?).


Did you look at the source to CSoundManager? It should help you out a little -- it has the init code and has code on how to read a wave file, but its by no means the end all solution. The source is at:

\DXSDK\Samples\C++\Common\DXUTSound.cpp
Daaark
Daaark
Quote:
Original post by jasonsa
\DXSDK\Samples\C++\Common\DXUTSound.cpp
There we go. :) I was looking for a CSoundManager.cpp in the dsound samples folder.

So I have to copy all my sound data to a new buffer everytime I need a new copy of it playing? Why can't I just play from the same buffer twice?

At any given time in my app I might need to call my explosion sound at least 3 times, maybe more, in given any frame. I'm going to have to be making a lot of temp buffers, possibly up to 100. What do you do in a situation like this? Make the max amount of static buffers, and then copy the data as needed?


Mezz
Mezz
Quote:
Original post by Vampyre_Dark
So I have to copy all my sound data to a new buffer everytime I need a new copy of it playing? Why can't I just play from the same buffer twice?


Yeah, it's a bit crap, isn't it. You can use DuplicateSoundBuffer() though, which might help ease your pain.

-Mezz
matthughson
matthughson
What I did (before seeing the light) was have 2 objects: CSoundManager and CSound. CSound handled induvidal sound effects (ie. contained a buffer, position (for 3d sound), etc). CSoundManager was a singleton that abstracted the interface to the CSound objects. So I could say something like CSoundManager::LoadSound(...) and it would find an empty CSound object, load the sound, and return an ID so the sound could be manipulated (play, stop, etc) later on (once again through the CSoundManager). Once that was implemented, actually using DirectX Audio wasn't such a headache.

Good luck,
Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
d000hg
d000hg
It's common to have maybe 8 sound buffers, and then the 8 highest-priority sounds get copied into these as needed. So you have one copy of each .wav file, and then only play those 8 buffers. You can set up timers to let you copy chunks of a large sample into a small buffer as needed, as well.

Or you can reuse your buffers. Definitely store them to re-use for the same sound so you don't create a buffer every time. You might also want to allow the possiblity to find the smallest unused buffer large enough to contain your sample which would reduce the total number of buffers you get lying around. I was advised to make my samples the same length to facilitate such a design.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.