|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThere'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! BackgroundRecently 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. Code ReviewThough 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.
Let's get started. (Steps to ripping DirectSound out of You'll need the main DirectSound // Create a new DirectSound manager.
private Microsoft.DirectX.DirectSound.Device m_SoundDevice;
The // Instantiate the DirectSound manager.
m_SoundDevice = new Device();
m_SoundDevice.SetCooperativeLevel(m_Interface, CooperativeLevel.Priority);
Now that our Device is ready to // Holds a DirectSound SecondaryBuffer.
private Microsoft.DirectX.DirectSound.Buffer m_SoundBuffer;
// Describe the capabilities of this buffer.
BufferDescription bufferDescription = new BufferDescription();
bufferDescription.ControlVolume = true; // Let's us control the volume.
bufferDescription.ControlPan = true; // Let's us control the panning.
bufferDescription.ControlFrequency = true; // Let's us control the frequency.
// Tells the primary buffer to continue
// play this buffer, even if the app loses focus.
bufferDescription.GlobalFocus = true;
// Pass in the path to the wav file, the buffer description,
// and the Device we created above.
m_SoundBuffer = new SecondaryBuffer(m_FilePath,
bufferDescription, m_ApplicationManager.SoundDevice);
Now that we have the m_SoundBuffer.Play(0, BufferPlayFlags.Default);
//or
m_SoundBuffer.Play(0, BufferPlayFlags.Looping);
To stop the wav from playing in either the m_SoundBuffer.Stop();
Changing the volume and pan is also very straight-forward. // int between 0, as the loudest, and -3000 as silent.
// It will except smaller, but I couldn't here anything at this point.
m_SoundBuffer.Volume = value;
// int between -1000, as all left, and 1000 as all right,
// with 0 being in the middle.
m_SoundBuffer.Pan = value;
That's it! Extending the ApplicationObviously, 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...
Feel free to contact me and let me know how it goes. Visit Southern Oregon .NET Group.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||