There's nothing better than coding away with your favorite tunes, but sometimes it's a bit distracting. This application is perfect for creating a sound environment to encapsulate your mood. Have company coming over? Turn on your virtual Hawaii, they'll love it. Creating a sound environment couldn't be any easier; add your own sound files, control the occurrence of each individual sound, the volume and the panning. Ready to switch to another environment? Save your current playlist and load up any other existing playlists you've created. Some of my environments I've been able to create include, Field and Stream, Oceanside Pagoda, Midnight Thunder Storm, Downtown New York, Barn Yard Animals (why I don't know), Dungeon Crawl, and Day at NASCAR. Enjoy!
Recently I'd started developing a kids RTS game using the new DirectX 9 and came face to face, once again, with the unhealthy lack of documentation plaguing most new Microsoft technologies. It was tough pulling together enough information on DirectSound to get an app working, but I did it, so here is the project I cut my teeth on. I originally got this idea from an old app called 'Environ', or something like that, and was fairly cool, but seriously lacked extendibility. Though I don't get into 3D buffer control capabilities, I do cover the basics. I hope that most of you find what you're looking for here.
Though I'll only be reviewing the DirectSound functionality, the project is commented well and you should have no problems figuring out what's going on and how to extend it; or pull it apart.
Here's a brief description about the classes and general flow of the application.
ApplicationManager - A manager class, holding the Main Entry Point to the application, and serving as the go-between for user interaction and application methods.
Interface - A view class, handling user interaction and rendering of the collection of
AmbientSounds - A view/modal class, describing the structure of a single sound, and handling user interaction.
Let's get started. (Steps to ripping DirectSound out of
AmbientSounds and putting it into your own code.)
You'll need the main DirectSound
Device declared in the
private Microsoft.DirectX.DirectSound.Device m_SoundDevice;
Device needs to be initialized before using, this is done in
ApplicationManager.Initialize(). After we initialize the
Device, we need to assign it to a control and set it's
CooperativeLevel. Because the DirectX driver for DirectSound could be utilized by a different app running at the same time, this is unavoidable. More Info on CooperativeLevels.
m_SoundDevice = new Device();
Now that our Device is ready to
rock, we need to create a sound buffer that will hold our wav file. We'll take a jump over to the
AmbientSound control class. Modifying buffer settings is done through a
BufferDescription and passed into the buffer instantiation.
private Microsoft.DirectX.DirectSound.Buffer m_SoundBuffer;
BufferDescription bufferDescription = new BufferDescription();
bufferDescription.ControlVolume = true;
bufferDescription.ControlPan = true;
bufferDescription.ControlFrequency = true;
bufferDescription.GlobalFocus = true;
m_SoundBuffer = new SecondaryBuffer(m_FilePath,
Now that we have the
Device and a buffer setup, playing the wav is extremely simple. You have a couple of options with the
Default, which plays the wav once and ends, or
Looping, which will continually play the wav file over and over.
To stop the wav from playing in either the
Default mode or
Looping, simply call
Changing the volume and pan is also very straight-forward.
m_SoundBuffer.Volume = value;
m_SoundBuffer.Pan = value;
Extending the Application
Obviously, I've gone over the most basic part of DirectSound. Microsoft has done a good job of shielding us from the complex to get something up and running, while still providing some more advanced implementations through the primary buffers and the use of 3D controls.
A few features I thought would compliment the app nicely...
- The ability to save a playlist as a soundpack; including all sound files used and the playlist file. This would allow the sharing of Environments.
- The ability to load an MP3 sound file.
Feel free to contact me and let me know how it goes. Visit Southern Oregon .NET Group.