Many multimedia computer systems nowadays have high-end speaker systems attached to high-end audio cards. It isn't uncommon for a system to have the ability to use full surround sound, and it won't be long before 5.1 systems are the norm for computers, particularly for the gamers. The DirectSound API already has the ability to play sounds in 3D space, and can use these features if the computer supports them today. The options are controlled with the Buffer3D object (for controlling the actual source of the sound) and the Listener3D object (for controlling the position and orientation of the listener). You'll notice that the constructors for these objects each take in a buffer that is used to create the 3D version of the audio. These buffers must be created with the Control3D flag set to true. So what benefits do you gain from implementing 3D sound into your application? Naturally you get a richer experience. When you go to the movie theater nowadays, the sound is normally played digitally, and they always have a surround sound system. Imagine if you were playing a game, and a door opened behind you, if there were speakers behind you, you would expect the sound to come from those speakers, not the two in front of you. Games today require the full experience, and this includes 3D sound. There are two items you can deal with when you want to manipulate the sounds in 3D. You can manipulate the actual source of the sound, or you can manipulate the "ears" that are hearing the sound. First, you can look at moving around the sound itself, by using the Buffer3D object. This object is only designed to control the 3D settings for the buffer. All of the methods for playing the buffer, and so on, still reside on the original buffer object. The properties you can update with this object are located in Table 14.2.
There is also another property that allows you to modify all of these parameters at once. It is called AllParameters, and you can update all of the options at once using this property. It should be a simple matter to update the last example using the pan property to use 3D processing instead, so you can try that now. The audio file you've used thus far happens to be a stereo file; it contains two separate channels for the left and right speakers. In order to use 3D processing, you will need to use a mono audio file (or one with only one channel). The code here (and on the included CD) will use a different file from the DirectX SDK that is a mono file instead for this example. Make sure you copy that file to your source code location. Add a reference to the Buffer3D object you will use to control the 3D buffer: private Buffer3D buffer = null; You should update the InitializeSound method to use 3D processing rather than the pan control you used before: public void InitializeSound() { device = new Device(); device.SetCooperativeLevel(this, CooperativeLevel.Normal); BufferDescription desc = new BufferDescription(); desc.Control3D = true; desc.GlobalFocus = true; sound = new SecondaryBuffer(@"..\..\drumpad-bass_drum.wav", desc, device); buffer = new Buffer3D(sound); sound.Play(0, BufferPlayFlags.Looping); buffer.Position = new Vector3(-0.1f, 0.0f, 0.0f); timer1.Enabled = true; } As you can see, you replaced the pan control with the 3D control, as well as changed the audio file to one that was mono. You then created the Buffer3D object from the already created sound buffer. After you play the buffer (ensuring it will loop continuously), you set the position just to the left of the listener (which defaults to 0, 0, 0). Finally, you should update the timer code. Rather than just move back and forth, though, you should really make this buffer move around. Replace the timer code with this: private void timer1_Tick(object sender, System.EventArgs e) { // Adjust the position buffer.Position *= mover; if ((Math.Abs(buffer.Position.X) > MoverMax) && (mover == MoverUp)) { mover = MoverDown; } if ((Math.Abs(buffer.Position.X) < MoverMin) && (mover == MoverDown)) { mover = MoverUp; } } This code uses a few new variables and constants here that are used to make the position move around. Add them to your class so that you can compile: private const float MoverMax = 35.0f; private const float MoverMin = 0.5f; private const float MoverUp = -1.05f; private const float MoverDown = -0.95f; private float mover = MoverUp; When the application first starts up, you'll notice the sounds moving back and forth quickly between your speakers, then it slowly sounds like the sounds get far away, and just when it sounds like they may move out of listening range, they start coming back toward you. The application will continue doing this until you quit it. |