Games and sophisticated applications require the use of a 3D Audio facility to place their gamers or users in a 3D Space. This results in a virtual world in which the user is immersed, resulting in an improved user response. Learn to use a technology by Creative Labs, the EAX or Environmental Audio Extension, that makes the virtual world richer with respect to 3D sound.
3D Audio and Environmental Audio Extension: Using AL's Support for 3D Audio and EAX
Games and sophisticated applications require the use of a 3D Audio facility to place their gamers or users in a 3D Space. This results in a virtual world in which the user is immersed, resulting in an improved user response. Added to this is a technology by Creative Labs, the EAX or Environmental Audio Extension, which makes the virtual world more rich with respect to 3D sound.
With this simple introduction, you experience the power of 3D sound and EAX and implement these by using the AL SDK.
- Download the SDK runtime and the SDK from http://streetx.freespaces.com.
- Install the SDK runtime on your system.
- Install the SDK as directed so that it can run with VC++ (6 or .NET) or BC++ (32 bit).
An Introduction to 3D Audio
Audio in 3D enables a feeling of reality in Sound. To bring about this reality to the sound, you require two attributes:
- Attribute of the Listener.
- Attribute of the Source.
Attribute of listener means vectors that specify the position of the listener (the gamer or the user) in 3D space along with the relative velocity of motion. Other attributes, such as Doppler shift, Roll off factor, Max and Min hearing distance, FRONT, and UP vector of the listener are also required.
Attribute of the source means the values that determine the characteristics of the sound source. The most important values for these are the vectors for position of source and the relative velocity. Other values can be the cone angle and orientation.
This article will discuss the placement of sound in 3D space with the position and velocity attribute of each. For a full discussion about the attributes, please refer to the help file for the SDK.
Relative Velocity: The Term
The relative velocity of an object may be defined as the (absolute) velocity of the object with respect to the observer, when the observer is stationary. The velocity of the listener and source can be independently specified, yet the term is relative velocity. This is because the effect achieved by setting the velocity of the source and listener independently also can be achieved by setting the velocity of listener to ZERO and the source to the relative velocity of source with respect to listener, and vice-versa. And, because nothing is absolute in space, the term relative provides a better approach.
Steps to Coding
- Initialize the AL system.
- Create an AL context and set the context for current use.
- Open a wave buffer with 3D support and, before using it, set it as the current audio source.
- Play the wave.
- Perform positioning of listener (it can be done anytime after setting a current context as it has nothing to do with opening of wave or the like).
- Position the source in the listener's space.
After youe are done using the program...
- Stop the wave.
- Close the wave.
- Delete the AL context. !!!Use the ALDelete() function!!!
- Uninitialize the AL system.
The source code of the program and a DEMO EXE are attached.
- Source Code Archive: CG_Audio3D.zip
- EXE Demo Archive: CG_Audio3D Demo.zip
This project implements functions from AL and a few functions from the GL library. This article will not discuss the functions from GL SDK but only those from AL SDK.
Excerpt from the Code
Because this article deals with 3D location of sound, I will assume that the reader understands the code dealing with initializating the AL system and de-initializating and creating of the AL context by downloading the souce.
The IDs used in the code are defined as follows:
- ID_WAVE: Points to the Wave buffer to be used.
- ID_FONT: Points to the Font resource to be used to display result on GLX Window.
- ID_TEXTURE: The 2D surface that is used to display 2D graphics on GLX Screen.
Opening Audio with 3D Support
if(!ALOpenWave( ID_WAVE, //Wave resource ID
// Wave file name (NULL terminated string)
// We need 3D support on the Wave Resource
// Wave data will be streamed ino wave buffer
// Wave will be audible even if the window loses focus
// We are not concerned about EAX now
/* TODO : FILE failed to open.
* Deal with error
Setting 3D Audio Attributes (Position and Velocity Vectors)
This function is very important. Whenever 3D parameters of the source and/or listener are changed, this function must be called for the changes in the 3D to take effect. It must be called after the listener and source 3D attributes are all set. It can be called after a batch of changes so that a more efficient program is created.
ALbool ALSet3DPosition( Vector *vec);
This function will take a vector that contains the position vector of the listener. By default, the listener is placed at Vector(0,0,0).
ALbool ALSet3DVelocity( Vector *vec);
This function will take a vector that contains the velocity vector of the listener. By default, the listener is stationary; in other words, Vector(0,0,0).
Now, the wave ID_WAVE is selected as the current wave buffer by calling ALSetWave(ID_WAVE). Then, afterward (because you know that your wave is 3D enabled), you can set the 3D Source attributes.
ALbool ALSetWave3DPosition(Vector *vec);
This function sets the position vector of the source in 3D space. By default, the source is set at the origin, Vector(0,0,0).
ALbool ALSetWave3DVelocity(Vector *vec);
This function sets the velocity vector of the source in 3D space. By default, the source is stationary; in other words, Vector(0,0,0).
Note: This is a general and simple 3D Audio Program; the source and demo can be viewed for results and understanding. Next, I will explain the Creative EAX effect on 3D Audio Resource. You should remember that the next program being discusses is an extension of the 3D Audio program; there is no change to whatever is discussed here, but only an addition of Creative EAX functionality.
Creative EAX Support of AL
EAX: An Introduction
A primary goal of computer gaming is to produce a realistic 3D world for the player. Creating the aural component of that world has always lagged behind the visual component. It's not uncommon to find a computer displaying sophisticated 3D graphics with texture mapping, shading, multiple light sources, haze, and one or two tinny speakers spits out monaural sound.
The Audio SDK keeps the nitty gritty of audio hardware at arm's length from the programmer. The programmer uses the relatively simple API to create sound sources, set their 3D positions and velocities (if moving), and take care of other decisions about the quality and placement of sounds in the aural world. The Audio kernel, through the audio driver installed in a computer, does the work of translating sound-source waveforms, positions, velocity, and more into a mix that ultimately comes out in a realistic 3D form through the player's speakers or headphones. Although the basic Audio SDK provides a number of sophisticated 3D aural effects such as Doppler effect, rolloff, and direction, they lack some very important environmental effects: reverberation, reflections, and sound occlusion or obstruction by intervening objects. Without these environmental effects, a listener can tell the direction of each sound source, but has no idea of the environment where the sources are located. The listener also has a more difficult time telling how far away the sources are.
Consider a sword clanked in a small padded cell. It should sound much different than the same sword clanked in a large cathedral, and it's reverberation that tells the story. Or, consider a scream coming from the next room. The occluded (muffled) quality of the scream tells you there's a wall between you and the screamer. Without environmental audio effects, sound sources are naked and lack warmth—the aural equivalent of a visual world without shadows, haze, and independent light sources.
EAX Parameter Path
The EAX parameter keeps two working profiles. The first profile, works on the listener's side, the attributes of which are common to all 3D wave buffers. The second profile is private to the 3D wave buffer that has EAX set on it. Its values affect only the current wave buffer (with EAX) and not all other buffers. For a more detailed discussion about EAX listener and source parameters, refer to the SDK help file. This article discusses the functions used in the EAX sample.
- Source Code: CG_EAXAudio
- EXE/DEMO Application: CG_EAXAudio Demo
EAX Parameters Used in EAX Sample
You have used four functions:
EAXIsEnabled, EAXSetReflections, and EAXSetEnvironment are listener-side parameters, whereas EAXWaveIsEnabled is a source-side parameter. Now, you can learn about these functions in detail. For the application, please read the source for more detail.
Opening Audio with 3D Support
if(!ALOpenWave( ID_WAVE, // Wave resource ID
filename, // Wave file name (NULL terminated string)
TRUE, // We need 3D support on the Wave Resource
TRUE, // Wave data will be streamed ino wave buffer
// Wave will be audible even if the window loses focus
// We need EAX support on our Wave resource
// (It will be disabled automatically if the Sound card does
// not support Creative EAX 2.0.
/* TODO : FILE failed to open.
Deal with error
Checking for EAX Support on the System
This function checks the Creative EAX 2 support on the device. If EAX 2.0 is detected, this function returns TRUE. (Optional, if you do not want to be informed if the host supports EAX or not.)
Checking for EAX Support on the Wave Source
First, set the wave resource as current. For the example, use ALSetWave(ID_WAVE).
This function checks the Creative EAX 2 support on the wave buffer. If EAX 2.0 is enabled on the wave buffer, this function returns TRUE. (Optional, if you do not want to be informed if the buffer had internally disabled support for EAX or not.) When a wave is opened with EAX support, 3D is automatically enabled, if not explicitly enabled when ALOpenWave() is called. But, if 3D support is not explicitly requested during the call to ALOpenWave() and the EAX 2.0 sound card is not detected, the Wave source may not support any 3D call on it.
Set Room Reflection Value
ALbool EAXSetReflections(ALlong v);
Sets the value of the reflection factor. Its value lies in the range -10000 to 1000 and is measured in seconds.
The Reflections property controls the overall amount of initial reflections relative to the Room property. (The Room property sets the overall amount of reflected sound: both initial reflections and later reverberation.) The value of Reflections ranges from a maximum of 10 dB to a minimum of -100 dB (no initial reflections at all), and is corrected by the value of the Room property. The Reflections property does not affect the subsequent reverberation decay. You can increase the amount of initial reflections to simulate a narrower space or closer walls; this is especially effective if you associate the initial reflection's increase with a reduction in reflection delays by lowering the value of the Reflection Delay property. To simulate open or semi-open environments, you can maintain the amount of early reflection while reducing the value of the Reverb property, which controls later reflections.
The Reflections Delay property is the amount of delay between the arrival time of the direct path from the source to the first reflection from the source. It ranges from 0 to 300 milliseconds. You can reduce or increase Reflections Delay to simulate closer or more distant reflective surfaces—and therefore control the perceived size of the room.
Both Reflections and Reflections Delay are low-level properties that may be controlled by the high-level listener property Environment Size. If so, their values are scaled according to the value set there. You can disable Environment Size's control over these properties by setting the appropriate flags in the listener property Flags.
Set the EAX Listener Environment
ALbool EAXSetEnvironment(ALdword v);
Sets the value of environment to use and hence modify the listener Room attributes. Its value lies within the range of 0 to 25, and its default value is 0 or EAX_ENV_GENERIC.
The possible environments are:
Value Units Integers that Each Specify a Specific Environment
Environment is the fundamental listener property. You typically set it first and then—if you want—modify it using other listener properties. When you set an environment, you choose the acoustic surroundings of the listener—the size of the virtual room around the listener and the reflective qualities of its walls. When you modify the environment using the other listener properties you can, in particular, modify the room size or adjust lower-level parameters such as the overall level and delay of the first reflections, the overall level of the subsequent reverberation, and the duration and tonal quality of the reverberation decay.
Here, the word Room is specified to emphasize that you are in a virtual 3D Room that you earlier considered as your 3D space. For the application, refer to the source. The detail discussion is here, but see it to believe it!!!