Click here to Skip to main content
Click here to Skip to main content

DirectX 9 - Get Ambient With DirectSound

By , 30 Nov 2003
Rate this:
Please Sign up or sign in to vote.

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.

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

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

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

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().

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

About the Author

v3ct0r m4yh3m
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionWhat about MP3Files? Pinmemberrkelm_21227-Oct-04 5:09 
AnswerRe: What about MP3Files? Pinmemberfx3000se16-Aug-05 8:26 
GeneralRe: What about MP3Files? Pinmemberklaus@gubbernet.com17-Sep-07 2:29 
GeneralRe: What about MP3Files? PinmemberIbrahim Dwaikat7-Nov-07 9:26 
AnswerRe: What about MP3Files? Pinmemberklaus@gubbernet.com8-Nov-07 8:35 
GeneralRe: What about MP3Files? PinmemberIbrahim Dwaikat8-Nov-07 10:07 
GeneralDirectX 9 PinmemberAlexander M.9-Dec-03 4:13 
Question5.1 3D Sound? Pinsuss_crog_8-Dec-03 22:15 
AnswerRe: 5.1 3D Sound? PinmemberAlexander M.9-Dec-03 4:11 
Question5.1 3D Sound? Pinsusscrog_8-Dec-03 22:14 
Generalcongratz PinmemberBillWoodruff3-Dec-03 7:12 
GeneralGreat, but... PinmemberDaniël Pelsmaeker3-Dec-03 0:42 
GeneralGreat job!!! Pinmemberglocke3-Dec-03 0:11 
GeneralCompliments PinmemberWard2-Dec-03 19:31 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 1 Dec 2003
Article Copyright 2003 by v3ct0r m4yh3m
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid