Log in

View Full Version : Sound Engine?


BrewKnowC
07-09-2003, 10:59 AM
Hi, I'm having some second thoughts about the way I coded my sound engine, if thats what you would call it. Basically it just uses DirectSound to initialize everything in my init routine and load everything in my load routine. What I'm weary about is where to make the calls to play the sound? Is there a better way than to just put the call right when something happens (collision with item) because this way i'm gonna have a bunch of loose sound calls thrown within my code and it may be harder to debug later. Is this how everyone does it?
Thanks

kerchen
07-09-2003, 01:25 PM
I've faced the same dilemma: peppering your code with PlayThisSound( soundID ) (or whatever) is not very elegant but it's the most straight-forward way of playing sounds that I've been able to come up with.

If you want to get more fancy about it, you could create an "Event" system that defines game-specific events ("button pressed," "barrel exploded," "level completed," etc). Then, everywhere an "event" happens, you call EventHappened(), passing in specific information about the event. Then, in EventHappened(), you call PlayThisSound(), ShowThatAnimation(), StopAllMusic(), etc. That way, you keep your sound calls localized to one part of your code, which makes it easier to, say, selectively mute sounds, etc. And, if you have more than one effect triggered when an event occurs, you don't have to worry about making sure every occurrence of that event does everything it's supposed to do. Of course, that's more work than the simple "pepper" method, but it might be worth the effort if you have a lot of places in your code where you would otherwise call PlayThisSound().

BarrySlisk
07-09-2003, 11:14 PM
I don't have DSound calls in my game code. I have DX specific code in my Engine/wrapper.

I have a class CSample with a method Play(int volume, bool loop) if I remember correctly.

But if you are doing relatively small games and work alone then you shouldn't worry about such things. Just make it work and release it.

Pyabo
07-09-2003, 11:56 PM
I second what Barry said. Don't complicate something when a simple solution will suffice.

This is one of the methodologies of extreme pogramming... KISS -- Keep It Simple Stupid! :)

Punchey
07-10-2003, 06:38 AM
Hmmm... I think sometimes initial complexity yields greater simplicity in the end. For instance, if your game has various entities and you might potentially want to add more entities at a later time, usually you'd want each entity to have at least one sound that it plays in response to some kind of action. Whether it's a door, a monster, a brick that falls, whatever. So while it's more work intially, I think you'd save yourself a TON of work in the long-run if you define these entities in classes that keep track of and are responsible for producing their own sounds.

I've developed a system that is akin to the Windows message pump. It's actually an extremely simple system when you look at the code. Basically, you have a root event router. And each kind of entity (or even game triggers and other such abstract constructs) registers itself with the event router as a listener for a specific type of event. So if the player moves, a MOVE event is triggered and sent to the event router that then looks through its list of registered listeners for that event and calls their callbacks with the data that is part of that event (player speed, location, etc.).

So if you had a collision event defined, and you had a "wall" entity that was registered as a listener to the collision event, then when your player collides with a wall, the wall is notified and it plays its own "bonk" sound, for instance.

As you can see, a system like this automates quite a bit and makes it very easy to add functionality because it is flexible. And it can be used for more than just sound.

If, however, your game is a very simple kind that will never need additional entities or functionality (like Tetris or Checkers or something), then it might just be better to take the quick'n'simple route and hard-code your sound calls.

[edit:]
Oh yeah, and if you really like simplicity, check out fmod (fmod.org). It's what I use and I LOVE IT! It's extremely simple to setup and use. And as a bonus, it's cross-platform too! It also natively supports easy loading of various formats such as OGG, MP3, WMA, etc. (all usually with a single line of code). And you can get a multi-license for shareware products for a mere $250!

BrewKnowC
07-10-2003, 10:59 AM
Thanks guys... Punchey - thats pretty much the alternative I was looking to here. I think it would probably fit my project perfectly, but requires quite a bit of re-coding, so I may shove the calls in there now and make it more elegant later. Thanks

luggage
07-10-2003, 01:39 PM
Hello

We've used Audiere ( http://audiere.sourceforge.net/ ) on a couple of previous projects. It's extremely easy to use. Plays serveal different formats (we used ogg) and it works across Windows, Linux-i386, Cygwin, and IRIX.

And even better it's free for commercial use so long as you don't modify the original source code.

Scott