|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe most exciting feature in DirectSound 9 is the possibility to move in the 3D space, the sounds and the points of listening. Audio is so important in applications such as videogames, that 3D audio is a godsend, especially because implementing 3D audio system with DirectSound 9 is very simple. RequirementsTo understand this tutorial, it is important to have familiarity with simple audio playback with Managed DirectSound 9 and with all the general concepts about Managed DirectX, so please read the previous tutorial about sound playback. In DirectSound 9 a sound source can be used in 3D only if it is mono. This is important because even a stereo file has ‘3D sound information’. If you try to use a non-mono file, DirectSound will raise an exception. Using 3D audio sources and listenersThe technique is so simple that I will use only a paragraph to explain it. The first step is to create a private Device dSound;
private SecondaryBuffer sound;
// New objects for 3D
private Buffer3D sound3D;
private Listener3D listener;
The dSound = new Device(); // Using default audio card
dSound.SetCooperativeLevel(this.Handle,
CooperativeLevel.Priority);
BufferDescription d = new BufferDescription();
d.ControlVolume = true;
d.ControlPan = true;
d.ControlFrequency = true;
d.ControlEffects = true;
d.Control3D = true; // Important to enable 3D audio!
// See note below!
d.Guid3DAlgorithm =
DSoundHelper.Guid3DAlgorithmHrtfFull;
sound = new SecondaryBuffer(filePath, d, dSound);
It is better to select, when possible, the most powerful audio card, so use the overloaded constructor of
The next step is to create the 3D buffer: sound3D = new Buffer3D(sound);
sound3D.Mode = Mode3D.HeadRelative;
The property
Now it’s time to set up the listener: Buffer b;
// Another primary buffer is needed
BufferDescription dp = new BufferDescription();
dp.PrimaryBuffer = true;
dp.Control3D = true;
b = new Buffer(dp, dSound);
// Create the Listener3D
listener = new Listener3D(b);
// Setup initial position and options
// for listener and sound3D
// Set the listener in the ‘zero’
// position of the space (origin)
listener.Position = new Vector3(0, 0, 0);
sound3D.Position = new Vector3(0, 0, 0);
// Make the listener ‘looking forward’ (see MSDN)
Listener3DOrientation o = new Listener3DOrientation();
o.Front = new Vector3(0, 0, 1);
o.Top = new Vector3(0, 1, 0);
listener.Orientation = o;
// Play the sound, if needed
sound.Play(0, BufferPlayFlags.Looping);
There are no more interesting features to describe. See the attached demo app, that shows a moving sound source and a fixed listener. You can move the source with your mouse or make it rotate around the listener. As usual, I suggest going through the MSDN documentation: after you understand the main techniques, extending them is very simple. Performances notesWe can say DirectSound is very fast. This statement is valid in most cases, but we need to describe some situations. For example if the audio card doesn’t support 3D sounds, DirectSound will emulate them via software. If the system (or the same app) does not have to perform other tasks, the user won’t notice the difference. But if you are developing a videogame, 3D sounds will load the CPU, slowing down the system. So make your choices right. In the demo app you may notice a little roughness of the source’s movements, even though the CPU load remains under 10%. ConclusionsNow you should be able to manage sounds inside a 3D space. Personally, I believe it is very simple. In the next tutorial I will explain how to record sounds with DirectSound’s
|
||||||||||||||||||||||