Click here to Skip to main content
15,883,705 members
Articles / Programming Languages / C#
Article

DirectX 9 - Get Ambient With DirectSound

Rate me:
Please Sign up or sign in to vote.
4.87/5 (29 votes)
30 Nov 20033 min read 121.8K   1.9K   48   14
Create an ambient environment with DirectSound.

Ambient Sounds

Introduction

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!

Background

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.

Code Review

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 controls.
  • 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 ApplicationManager class.

C#
// Create a new DirectSound manager.
private Microsoft.DirectX.DirectSound.Device    m_SoundDevice;

The 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.

C#
// Instantiate the DirectSound manager.
m_SoundDevice = new Device();
m_SoundDevice.SetCooperativeLevel(m_Interface, CooperativeLevel.Priority);

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.

C#
// 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 Device and a buffer setup, playing the wav is extremely simple. You have a couple of options with the BufferPlayFlags; Default, which plays the wav once and ends, or Looping, which will continually play the wav file over and over.

C#
m_SoundBuffer.Play(0, BufferPlayFlags.Default);
//or
m_SoundBuffer.Play(0, BufferPlayFlags.Looping);

To stop the wav from playing in either the Default mode or Looping, simply call Stop().

C#
m_SoundBuffer.Stop();

Changing the volume and pan is also very straight-forward.

C#
// 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 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat about MP3Files? Pin
rkelm_21227-Oct-04 5:09
rkelm_21227-Oct-04 5:09 
AnswerRe: What about MP3Files? Pin
fx3000se16-Aug-05 8:26
fx3000se16-Aug-05 8:26 
GeneralRe: What about MP3Files? Pin
Luca Crisi, MCP17-Sep-07 2:29
Luca Crisi, MCP17-Sep-07 2:29 
GeneralRe: What about MP3Files? Pin
Ibrahim Dwaikat7-Nov-07 9:26
Ibrahim Dwaikat7-Nov-07 9:26 
AnswerRe: What about MP3Files? Pin
Luca Crisi, MCP8-Nov-07 8:35
Luca Crisi, MCP8-Nov-07 8:35 
GeneralRe: What about MP3Files? Pin
Ibrahim Dwaikat8-Nov-07 10:07
Ibrahim Dwaikat8-Nov-07 10:07 
GeneralDirectX 9 Pin
Alexander M.,9-Dec-03 4:13
Alexander M.,9-Dec-03 4:13 
Question5.1 3D Sound? Pin
_crog_8-Dec-03 22:15
suss_crog_8-Dec-03 22:15 
AnswerRe: 5.1 3D Sound? Pin
Alexander M.,9-Dec-03 4:11
Alexander M.,9-Dec-03 4:11 
Question5.1 3D Sound? Pin
crog_8-Dec-03 22:14
susscrog_8-Dec-03 22:14 
Generalcongratz Pin
BillWoodruff3-Dec-03 7:12
professionalBillWoodruff3-Dec-03 7:12 
GeneralGreat, but... Pin
Member 2611883-Dec-03 0:42
Member 2611883-Dec-03 0:42 
GeneralGreat job!!! Pin
glocke3-Dec-03 0:11
glocke3-Dec-03 0:11 
GeneralCompliments Pin
Ward2-Dec-03 19:31
Ward2-Dec-03 19:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.