Game Development Reference
// Get Source State
alGetSourcei( uiSource, AL_SOURCE_STATE, &iState);
} while (iState == AL_PLAYING);
// Clean up by deleting Source(s) and Buffer(s)
There's nothing fancy about this demonstration, and in fact there are no graphics. The
program runs in a console window. That's OK, however, since you really need your ears
and not your eyes to appreciate this demonstration. Be sure to use headphones if you
test it yourself. The 3D effect is much better with headphones.
The lines of code within main() all the way up to the comment Specify the location
of the Listener are just OpenAL initialization calls required to set up the framework
and associate a sound file with a sound buffer that will hold the sound data for later
The next line of code after the aforementioned comment sets the location of the listener.
We specify the listener's location at the origin. In a game, you would set the listener
location to the player's location as the player moves about your game world. In this
example, the listener stays put.
A source is then created and associated with the previously created sound buffer. Since
we want to include the Doppler effect, we set the Doppler factor to 10. The default is 1,
but we amped it up to enhance the effect.
Next we create six new local variables to store the source's x , y , and z coordinates and
the increments in position by which we'll move the source around. After initializing
those variables, we set a few properties of the source—namely, we specify that we want
the source sound to loop and then we set its initial position and velocity. The velocity
properties are important for the Doppler effect. If you forget to set the velocity prop‐
erties, you'll get no Doppler effect even if you move the source around by changing its
Next, the source is set to play, and a loop is entered to continuously update the source's
position every 100 milliseconds. The code within the loop simply adds the coordinate
increments to the current coordinates for the source and checks to be sure the source
remains within certain bounds. If the source gets too far away, attenuation will be such
that you won't hear it any longer, which just gets boring.