In a virtual 3-D environment as in the real world, sounds exist in relation to a point of reception. The 3-D sound effects in a Microsoft DirectSound application are affected not only by the position, orientation, and velocity values of the sound sources, but also by the position, orientation, and velocity of the virtual listener.
By default, the listener is stationary at the zero point of all vectors, oriented with the nose toward the positive z-axis and the top of the head toward the positive y-axis. By obtaining an object to represent the listener, the application can change all these values to reflect the movement and "facing" of the user within virtual space. The listener object also controls the general parameters of the acoustic environment, such as the amount of Doppler shift and the rate of volume attenuation over distance.
This section describes how your application can obtain a listener and manage global 3-D sound parameters. The following topics are discussed.
- Obtaining the 3-D Listener
- Batch Parameters for 3-D Listeners
- Deferred Settings
- Distance Factor
- Listener Orientation
- Listener Position and Velocity
- Doppler Factor
- Rolloff Factor
Global sound parameters are set and retrieved by using the Listener3D object, which is obtained from the primary buffer. Only one listener exists in an application.The following Microsoft Visual Basic function creates a Buffer object representing the primary buffer, which is created with the Control3D property. The function then creates and returns a Listener3D object.
[Visual Basic]Private Function getListener(ByVal dev As Device) As Listener3D Dim primaryBuffer As Buffer Dim desc As New BufferDescription() Dim caps As New BufferCaps() desc.PrimaryBuffer = True desc.Flags = caps.PrimaryBuffer OR caps.Control3D primaryBuffer = New Buffer(desc, dev) Dim listener = New Listener3D(primaryBuffer) Return listener End Function
Applications can retrieve or set a listener's parameters individually or in batches. To set individual values, your application can use the applicable Listener3D property. However, applications often must set or retrieve several values at once as the sound source moves about, changes velocity, and so on. You can do this by using the Listener3D.AllParameters property.
Parameter changes can also be made more efficiently by flagging them as deferred and then executing them all at once. For more information, see Deferred Settings.
Every change to 3-D sound buffer and listener settings causes remixing, at the expense of CPU cycles. To minimize the performance impact of changing 3-D settings, set the Deferred property on the Listener3D object and all Buffer3D objects. When this property is true, no other properties are changed until you call Listener3D.CommitDeferredSettings.
The distance factor is the number of meters in a vector unit. By default, the distance factor is 1.0. If the velocity of a buffer is (2.0, 0.0, 0.0), the sound source is considered to be moving along the positive x-axis at 2 meters per second. Applications that are using a different unit of measurement for 3-D graphics vectors might want to change the distance factor accordingly.
Suppose, for example, that the basic unit of measurement in your application is the foot, or 0.3048 meters. You set the Listener3D.DistanceFactor property to 0.3048. From then on, properties such as position and velocity are measured in feet.
The distance factor mainly affects Doppler shift, by changing the actual velocity represented by n units per second. It does not directly affect rolloff, because the rate of attenuation over distance is based on the minimum distance in unspecified units. If you set the minimum distance for a given sound at 2 units, the volume will be halved at a distance of 4 units, whether those units are in feet, meters, or any other measure.
Listener orientation is defined by the relationship between two vectors that share an origin at the center of the listener's head: the top and front vectors. The top vector points straight up through the top of the head, and the front vector points forward through the listener's face at right angles to the top vector, as in the following illustration.
By default, the front vector is (0.0, 0.0, 1.0), and the top vector is (0.0, 1.0, 0.0). The two vectors must always be at right angles to one another. If necessary, DirectSound will adjust the front vector so that it is at right angles to the top vector.
As the listener moves, it is up to the application to specify values for its position and velocity.
Position is measured in distance units along each of the three axes, relative to either world space or the listener, depending on the processing mode.
Velocity is measured in distance units per second along each of the three axes of a vector. By default, distance units are meters. To calculate Doppler shift, DirectSound combines the velocity of the sound source with the velocity of the listener.
DirectSound automatically creates Doppler shift effects for 3-D sound buffers based on their velocity relative to the listener.
In order to have realistic Doppler shift effects in your application, you must calculate the speed of any object that is moving, whether a sound source or a listener, and set the appropriate velocity. You are free to exaggerate or lessen this value in a particular case in order to create special effects. You can also globally increase or decrease Doppler shift by changing the Listener3D.DopplerFactor property.
The Doppler factor can range from 0.0 (DSoundHelper.MinDopplerFactor) to 10.0 (DSoundHelper.MaxDopplerFactor). A value of 0 means no Doppler shift is applied to a sound. Every other value represents a multiple of the real-world Doppler shift. In other words, a value of 1 (DSoundHelper.DefaultDopplerFactor) means the Doppler shift that would be experienced in the real world is applied to the sound, a value of 2 means twice the real-world Doppler shift, and so on.
Rolloff is the amount of attenuation that is applied to sounds, based on the listener's distance from the sound source. DirectSound can ignore rolloff, exaggerate it, or give it the same effect as in the real world, depending on the value of the Listener3D.RolloffFactor property.
The rolloff factor can range from 0.0 (DSoundHelper.MinRolloffFactor) to 10.0 (DSoundHelper.MaxRolloffFactor). A value of 0 means no rolloff is applied to a sound. Every other value represents a multiple of the real-world rolloff. In other words, a value of 1 (DSoundHelper.DefaultRolloffFactor) means the rolloff that would be experienced in the real world is applied to the sound, a value of 2 means twice the real-world rolloff, and so on.
To change the effect of distance for an individual sound buffer, you can set the minimum distance for the buffer. For more information, see Minimum and Maximum Distances.