Chapter 11, you should stop reading this and go back and learn those concepts. OpenAL shares 90 percent of the concepts used by DirectX Audio's 3D sound implementation.
Sources, Buffers, and Listeners
The OpenAL functionality revolves around three main kinds of objects: sources, buffers, and listeners. Listeners in OpenAL and DirectX Audio are conceptually the same thing; in OpenAL audio, there's still only one listener, which represents the ears of the in-game player.However, OpenAL has a new object—a source—as well as a different meaning for the term "buffer." An OpenAL buffer is the PCM sampled data itself, whereas a source describes an instance of a buffer, a position from which a certain buffer emanates (see Figure 14.1).

Figure 14.1: Multiple sources can reference the same buffer.
OpenAL uses these constructs so that if, for example, your game has three different helicopters, you don't need to load three copies of the helicopter WAV file into memory. You load the WAV into a buffer, and then create three sources that reference that same buffer.From a different angle, both APIs have this capability. In OpenAL, you have multiple sources referencing a single buffer, which contains the actual sound data. In DirectX Audio, you have audiopaths; you could say that an OpenAL buffer is equivalent to a DirectMusic segment, and that an OpenAL source is equivalent to an audiopath off that segment.
It Isn't COM Anymore
The DirectMusic API is driven by COM, which means by now you're used to dealing with acquiring and releasing interfaces. OpenAL is not COM. Since it's cross platform, it relies on the triedand-true method of API interfaces—a big list of functions, enums, and structures.OpenAL (and OpenGL too, for that matter) adheres to a strict naming convention for its functions. If you look in the API reference, you'll see several functions whose names are almost identical—except one will end in f, another will end in fv, and a third might end in i. These correspond to the type of data the function takes as arguments.For example, there are four functions with names alListenerf, alListener3f, alListenerfv, and alListeneri (by the way, all OpenAL functions start with al). All four of these functions do the same thing—they set the properties of a listener. However, alListenerf takes one floating-point argument, alListener3f takes 3 floats as arguments (a vector), alListenerfv takes an array of floats (a vector in the C++ sense of the word), and alListeneri takes an integer. This seems weird, but it becomes second nature once you get the hang of it, and makes it more difficult to accidentally get your data types mucked up.
Buffer Properties
The available properties for a buffer are summarized in Table 14.1. As you can see, they all relate to the properties of the wave file originally loaded into the buffer.
Property | Description |
---|---|
AL_FREQUENCY | Frequency of the WAV file. |
AL_BITS | Bits per sample of the WAV file. |
AL_CHANNELS | Number of channels (1=mono, 2=stereo). Note that if this is greater than one, the buffer will not be positioned aurally when played (in other words, if you want your sound to appear to come from the right place, use mono samples). |
AL_SIZE | Size (in bytes) of the buffer. |
AL_DATA | Memory address data was originally loaded from (not particularly useful since you probably freed the data once it was copied into the buffer). |
All of these values are read-only, set when you call alBufferData to pump a buffer full of sample data. Since all of these properties are integers, you can read them by calling the OpenAL function alGetBufferi.
Source Properties
OpenAL sports several properties for its sources (see Table 14.2). Most of these you're already familiar with from the last chapter, but there are some new ones, too.
Property | Description |
---|---|
AL_PITCH | A pitch multiplier for this source (always greater than zero). Values between zero and one will lower the pitch; values above one will raise it. |
AL_GAIN | Volume of the source. Values between zero and one will lower the volume; values above one will raise it. |
AL_MAX_DISTANCE | Distance at which the listener can no longer hear this source. |
AL_ROLLOFF_FACTOR | Rolloff multiplier for this source. Usually one, but can be lowered to values between zero and one for longer rolloff, or raised above one for shorter rolloff. |
AL_REFERENCE_DISTANCE | The distance at which the source would lose half its volume (not counting the rolloff factor). |
AL_MIN_GAIN | The minimum gain (volume) of this source. |
AL_MAX_GAIN | The maximum gain (volume) of this source. |
AL_CONE_OUTER_GAIN | Volume when the listener is outside of the source's cone. |
AL_CONE_INNER_GAIN | Volume when the listener is inside the source's cone. |
AL_CONE_OUTER_ANGLE | Angle of the sound cone. The default is 360, which removes the cone altogether. |
AL_POSITION | 3-float vector specifying the position of the source. |
AL_VELOCITY | 3-float vector specifying the velocity of the source (used for Doppler effects). |
AL_DIRECTION | 3-float vector specifying the direction the cone of the source is pointed. |
AL_SOURCE_RELATIVE | If set to AL_TRUE, the position of the source is relative to the listener (like DirectX Audio's head-relative mode). |
AL_LOOPING | If set to AL_TRUE, the source loops continuously. |
AL_BUFFER | The ID of the buffer the source plays. |
AL_SOURCE_STATE | Current state of the source (AL_INITIAL, AL_STOPPED, AL_PLAYING, or AL_PAUSED). |
AL_BUFFERS_QUEUED | The number of buffers queued for playback on this source (this property is read only—you can set it indirectly by calling alSourceQueueBuffers). |
AL_BUFFERS_PROCESSED | The number of queued buffers that have already been played (again, this is a read-only property). |
You can set these source properties by using the OpenAL functions alSourcef, alSourcefv, alSource3f, and alSourcei. Retrieve them by calling alGetSourcef, alGetSourcefv, alGetSource3f, or alGetSourcei, depending on the data type of the property.
Tip | Be careful—OpenAL uses a left-handed coordinate system, whereas Direct3D is right-handed. Put another way, in OpenAL, negative z values descend into the screen, whereas in Direct3D, negative z values go out of the screen. |
Listener Properties
The OpenAL listener also has a handful of properties, summarized in Table 14.3. You can set these properties by calling the alListener family of functions (alListenerf, alListener3f, alListenerfv, and alListeneri). Retrieve their values by calling alGetListenerf, alGetListener3f, alGetListenerfv, or alGetListeneri.
Property | Description |
---|---|
AL_GAIN | Master gain value (this is sort of like a volume knob; raise it above one, and all sound will be amplified, set it to a value between zero and one and all sound volumes will be lowered). |
AL_POSITION | 3-float vector specifying the listener's absolute position in the 3D world. |
AL_VELOCITY | 3-float vector specifying the velocity of the listener for Doppler effects. |
AL_ORIENTATION | A two vector, 6-float property specifying the orientation of the listener. The first three floats form the lookat vector, the last three form the up vector. |