Document directory
- Get 3D buffer objects
- Min and max distance
- Processing mode
- Buffer location and speed
- Sound cone
- 3D buffer Parameters
- Get 3D listeners
- Listener space parameters
- Distance factor
- Doppler Effect
- Attenuation Factor
- Parameters of 3D listeners
- Delay settings
Transferred from:
Http://blog.sina.com.cn/s/blog_4de8abbb01000atp.html3D sound
Dsound allows you to create a 3D world and 3D graphics for sound. The location of sound resources and listeners can be located in the space, and the Doppler transform can be used to move the sound and control physical parameters, such as the frequency at which the sound is weakened at a distance.
Dsound allows you to create a virtual 3D effect on two speakers or headphones. If you have selected a surround sound speaker in the control panel, you can also use the WDM driver's multi-channel. For more information, see dsbufferdesc.
3D space coordinates
The position, speed, and direction of the sound resource and the listener are represented in the Cartesian coordinate system in 3D space. Coordinates are related to the viewpoint set up by the program. The X axis is from left to right, the Y axis is from small to upper, And the Z axis is from near to far.
The d3dvector structure contains values describing sound, rate, and direction in a 3-way coordinate system.
Conveniently, the vector is represented as follows (x, y, z ).
LocationThe default unit is meter. If the 3D graph of the program does not use meters as the unit, you can set a distance factor to indicate the number of meters per unit. For example, if the program uses a ruler as the unit, the distance factor can be set to. 3048, that is,. 3048 is a ruler. For more information, see distance factor.
SpeedIs a vector that describes the rate at which each second moves along each coordinate axis. Similarly, the default unit is meter, but can be changed by the program.
DirectionCan be any unit, because they are all proportional. If the basic 3D coordinate system is oriented to the north by default and the listener is (-, 1), The Listener is oriented to the north and west. This direction can also be expressed as (-5, 0, 5) or (-. 25, 0,. 25 ).
You can draw them on paper to observe how vectors work in 2D spaces. Painting from (0, 0) to () and from (0, 0) to (5, 5) have the same direction, but the latter represents a longer distance or faster speed.
Understanding of sound position
In the real world, the understanding of sound positions in space is influenced by several factors. Not all of these factors are acoustic, And the other focus is on vision. The clues of The Voice itself include the following:
1) Voice weakened. When a sound resource is far away from the listener, the perceived sound decreases at a fixed rate.
2) Strong differences between ears. From the listener's right side, the sound in the right ear is larger than that in the left ear.
3) time difference between ears. The time for the source to arrive at the right ear of the receiver when it is sent to the right ear is a little earlier than the time for reaching the left ear, which is about 1 ms.
4) occlusion. The shape and position of the ears ensure that the sound coming from the back of the listener is blocked compared with the sound coming from the front. In addition, if a sound is sent from the right side, it will be blocked by the recipient's head when it reaches the left ear.
5) earlobe effect. The ears and its folds cause nuances of sound from different directions. In mathematics, this effect is considered as a head-Response Transfer Function (Head-related transfer function (HRTF ))
Head-related transfer function (HRTF ))
HRTF: it is a sound effect locating algorithm. Its actual function is to cheat our ears. Simply put, this is a head-Response Transfer Function ). The specific points can be divided into several main steps to describe their functions. Step 1: Create a head model and install a microphone to the ear mask; Step 2: Make some sound from a fixed position; Step 3: analyze the sound obtained from the microphone and obtain the specific data changed by the model. Step 4: design an audio filter to simulate the effect. Step 5: when you need to imitate the sound from a location, use the above filter to imitate it. The response of the filter is considered as an HRTF. You need to set an HRTF for each location where there may be a sound source. In fact, we do not need an infinite number of HRTF. The reason is also very simple, and our brain cannot be so precise. It is sufficient to have about 1000 such functions distributed on the semi-spherical surface with the origin of our head, and the other half should be symmetric. As for the sense of distance, it should be achieved by data changes such as ECHO and response.
Dsound 3D Buffer
Sound resources in every 3D environment are described by the idirectsound3dbuffer8 interface. This interface is only supported by the sound buffer created using the dsbcaps_ctrl3d flag.
When using the 3D compatibility of dsound, the program must provide single-sound resources. If you try to use the dsbcaps_ctrl3d flag to create a buffer and the WAV format contains more than one audio channel, an error will occur. The following section describes how to obtain and manage 3D buffer objects.
Get 3D buffer objects
The idirectsound3dbuffer8 interface is obtained from a buffer created by the dsbcaps_ctrl3d flag. You can call QueryInterface to obtain the idirectsound3dbuffer8 interface on the idirectsoundbuffer8 interface:
LPDIRECTSOUND3DBUFFER8 lpDs3dBuffer;
HRESULT hr = lpDsbSecondary->QueryInterface(IID_IDirectSound3DBuffer8,
(LPVOID *)&lpDs3dBuffer);
Min and max distance
When a listener approaches a sound source, the sound increases. When the distance is halved, the sound doubles. Transmits a specific pointer, but the volume enhancement is not so regular.
The minimum distance determines how fast the sound disappears. For example, a jet may set this value to 100, but a bee to 2 cm. According to these settings, the volume of a jet aircraft is halved when it is 200 meters away from the listener, while that of a bee is only 4 cm.
The figure in the SDK help document shows how the minimum distance and maximum distance affect the volume of jet planes and bees as the distance increases.
Minimum distance:The default minimum distance of the sound buffer is ds3d_defaultmindistance, which is defined as 1 unit or 1 meter when the default distance factor is used. Unless this value is changed, the maximum volume of the sound heard when the sound is 1 meter away from the listener, halved when the sound is 2 meters away, and changed to 1/4 when the sound is 4 meters away. For most sounds, you may need to set a larger minimum distance so that the sound will not fade too quickly when it is far away.
Maximum Distance:The maximum distance of a sound source means that the sound will not fade when the distance is exceeded. The default maximum distance is 1 billion, which means that the attenuation of the sound outside the Auditory range will also be calculated. To avoid unnecessary processing, in The VxD-driven software buffer, the program can set an appropriate maximum distance and contain the dsbcaps_mute3datmaxdistance flag when creating the buffer (in dmusic, this flag is automatically set in the 3D buffer ).
The maximum distance can also be used to organize the disappearance of sound. For example, if you set the minimum distance of 100, you may not be able to hear it after 1000. By setting 800 to the maximum distance, this ensures that the maximum volume is 1/8 at a further distance. In this case, you cannot set the dsbcaps_mute3datmaxdistance flag.
By default, the distance value is described in meters. If you want to adjust the effect of distance, you can change the distance attenuation factor.
Processing mode
Sound buffer has three processing modes: Normal, header-related, and disabled
Normal Mode (ds3dmode_normal)The location and direction of the sound source depend entirely on the world space. This is the default mode. It is used when the sound source does not move or rotate relative to the listener.
Header-related mode (ds3dmode_headrelative)The 3D attribute of the sound source is related to the location, orientation, and speed of the current listener. When a listener moves or rotates, It is reconfigured in The World Space buffer zone. The header correlation can be used to create the effect of surrounding the listener's head. However, most sounds do not need to be 3D.
Disable mode (ds3dmode_disable)3D sound processing is forbidden, and the sound seems to be sent from the center of the recipient's head.
Buffer location and speed
When a sound source moves, the Program specifies a value for its location and speed.
The location is represented by the Distance Units of the three coordinate axes. It is related to the World Space and the listener, and is determined by the processing mode.
The speed is measured in units of vectors that move on three coordinate axes per second. By default, the distance is in meters. The rate is used only during Doppler transformation.
Sound cone
A sound with no direction has the same amplitude in all directions. A directional voice is the most audible in the orientation. A model describing the volume of a directed sound is called a Sound cone. A sound cone consists of an inner cone and an external cone. The external cone angle must be equal to or greater than the internal cone angle.
At any angle within the inner cone, the volume is not the volume of the cone after considering the basic volume, the distance of the listener, the direction of the listener, and other factors.
Normal volume is reduced by a factor set by the program at any angle other than the external cone. The volume of the external cone is represented by a percentile and is negative because the sound degrades from 0. The volume is equal to the internal cone volume + the defined external cone volume.
Between the inner and the outer cone is the painting process of the sound from the inner cone to the outer cone. The volume decreases as the angle increases.
Each 3D sound buffer has a sound cone, but the default buffer is a full range of sound sources, because the sound outside is not attenuated, and the angle of the inner and outer cones is 360 degrees. Unless the program changes these values, the sound surface does not sound any direction.
The proper Sound cone design can increase the dynamic effect for the program. For example, you can place a sound source in the middle of the room and set it to the open door toward a corridor. Then set the inner cone angle so that it can be extended to make the external cone angle a little more, and set the sound of the external cone to be inaudible. A listener walking along the corridor will only hear the sound when it is close to the door, and the sound will be the biggest when the listener opens the door.
3D buffer Parameters
The program can obtain or set parameters of the 3D sound buffer individually or in batches. If the sound source is moving, you may need to immediately set the location, orientation, speed, and many other parameters. You can call idirectsound3dbuffer8: getallparameters to obtain the current parameter, change the value of the ds3dbuffer structure member, and then pass the modified value to idirectsound3dbuffer8: setallparameters.
Parameter changes can also improve the efficiency by marking them as delayed and executed together later.
Dsound3d listener
In a virtual 3D environment, like in the real world, the sound is only related to the receiving point. In the DX program, the 3D sound effect is not only affected by the location, direction, and speed of the sound source, but also by the location, direction, and speed of the virtual listener.
By default, the listener is located at the origin, and the nose is directed to the Z axis. The head is directed to the Y axis. The program can change these values to influence users' movement and orientation. The listener also controls general parameters in the acoustic environment, such as Doppler transform and sound attenuation rate.
This section describes how to obtain and manage a global 3D sound parameter.
Get 3D listeners
The Global Sound parameters are set and obtained from the idirectsound3dlistener8 surface of the master buffer zone. In a program, there is only one primary buffer zone and one listener.
To get an excuse for listening, you must first use idirectsound8: createsoundbuffer to create a primary buffer and define the dsbcaps_ctrl3d and dsbcaps_primarybuffer flag bits. Call the QueryInterface method to obtain a pointer to the idirectsound3dlistener8 interface.
The following example obtains a listener pointer:
GetListener(LPDIRECTSOUND8 lpds, LPDIRECTSOUND3DLISTENER8* ppListener)
{
DSBUFFERDESC dsbd;
LPDIRECTSOUNDBUFFER lpdsbPrimary; // Cannot be IDirectSoundBuffer8.
LPDIRECTSOUND3DLISTENER8 lp3DListener = NULL;
HRESULT hr;
ZeroMemory(&dsbd, sizeof(DSBUFFERDESC));
dsbd.dwSize = sizeof(DSBUFFERDESC);
dsbd.dwFlags = DSBCAPS_CTRL3D | DSBCAPS_PRIMARYBUFFER;
if (SUCCEEDED(hr = lpds->CreateSoundBuffer(&dsbd, &lpdsbPrimary, NULL)))
{
hr = lpdsbPrimary->QueryInterface(IID_IDirectSound3DListener8,
(LPVOID *)ppListener);
lpdsbPrimary->Release();
}
return hr;
}
Listener space parameters
Listener VectorBy default, the forward vector is (0.0, 0.0, 1.0), and the top vector is (0.0, 1.0, 0.0 ). The two vectors must always have a right angle to each other. If necessary, dsound adjusts the forward vector to have a right angle with the above vector.
The position is measured by the distance unit of the vector.
The speed is measured by the speed along the vector. The default unit is meter. Rate is only useful for Doppler transformation.
Distance factor
The distance factor represents the number of meters in a vector unit. The default distance factor is 1.0. If the speed of a buffer zone is (2.0, 0.0, 0.0), the sound source is considered to move 2 meters per second along the X axis. Programs that use different measurement units for 3D images may change accordingly.
Suppose, for example, the basic measurement unit is feet or. 3048 meters. Set the distance factor to. 3048. From then on, feet are used as units and they are automatically converted to meters.
By changing the actual speed described by N measurement units per second, the distance factor can affect Doppler transformation. It does not directly affect the attenuation, because the attenuation rate of distance is based on the minimum distance. If the minimum distance is set to 2 measurement units, the volume is half of the 4 measurement units, regardless of the unit of measurement is feet, meters, or other measurements. For more information, see minimum and maximum distances.
Doppler Effect
Dsound automatically creates a Doppler transform effect for a buffer or receiver with a speed. The effect is cumulative: If the listener and the buffer are moving, the system automatically calculates the relevant speed to adjust the Doppler effect.
In order to have a real Doppler transformation effect in the program, the speed of any moving object must be calculated, and a proper speed must be set for the sound source or receiver. In special cases, we can increase or decrease this value freely to establish a special effect. We can also increase or decrease the Doppler effect by changing the Doppler factor.
Doppler effects can be defined from ds3d_mindopplerfactor to ds3d_maxdopplerfactor as 0.0 and 10.0 respectively. 0 means that the sound has no Doppler transformation. The number of times the Doppler transformation is represented by other values.
Attenuation Factor
The attenuation factor is the amount of attenuation acting on the sound, based on the distance between the listener and the sound source. Dsound can be ignored, increasing attenuation or using the same effect as the real world depends on a global attenuation factor.
The attenuation factor can be defined as 0.0 and 10.0 respectively from ds3d_minrolofffactor to ds3d_maxrolofffactor. Ds3d_minrolofffactor indicates that no matter how far the sound is from the listener, it will be the biggest sound. Any other value indicates the attenuation of the real world.
The attenuation factor is global. To change the distance effect for each individual sound buffer, you can set the minimum distance for the buffer.
Parameters of 3D listeners
The program can obtain and set parameters of a 3D listener object in batches. If the listener is moving, you may want to set a large number of parameters, such as location, direction, and speed. You can call idirectsound3dlistener8: getallparameters to obtain the current parameter. Change the member variable in the ds3dlistener structure and pass it to idirectsound3dlistener8: setallparameters .. Parameter changes can be improved by marking them as deferred and then executing them together. For more information, see deferred settings
Delay settings
Every change in 3D sound will result in re-mixing during CPU consumption. To minimize the impact of changing 3D settings on execution performance, set the ds3d_deferred flag when you call the methods in idirectsound3dlistener8 or idirectsound3dbuffer8 to change the 3D sound settings. Then call the idirectsound3dlistener8: commitdeferredsettings method to immediately execute all delayed commands.
The delay setting will be overwritten by the immediate mode. For example, if you use ds3d_deferred to set the speed of the listener to (1.0, 2.0) and then use ds3d_immediate to set the speed of the listener to (0.0, 0.0, 2.0), the speed of the listener will change, 0.0, 0.0). It will not be changed when commitdeferredsettings is called.