OpenAL is a cross-platform 3D audio API modelled after OpenGL. It lets you place sound sources and a listener anywhere in 3D space, and the library handles distance attenuation, panning, and Doppler shift automatically. LWJGL 3 provides a complete Java binding to OpenAL and the ALC (context) layer. The recommended implementation is OpenAL Soft, which supports Windows, macOS, Linux, and a wide variety of audio hardware.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/LWJGL/lwjgl3/llms.txt
Use this file to discover all available pages before exploring further.
OpenAL Soft must be present on the target system. On Linux it is usually available via the package manager (
libopenal). On Windows and macOS you can bundle the native library with your application using the LWJGL build customiser.Open a device
The device represents a physical or virtual audio output. Pass You can also pass a specific device name string —
null to let OpenAL Soft choose the default device:ALUtil.getStringList(NULL, ALC_ALL_DEVICES_SPECIFIER) returns all available output device names.Create a context and make it current
A context holds all OpenAL state. An application normally creates one context per device:
AL.createCapabilities probes the context for supported AL extensions and populates the ALCapabilities object that LWJGL consults when you call AL functions.Generate sources and buffers
OpenAL separates audio data (buffers) from playback state (sources). A buffer holds raw PCM samples; a source has position, velocity, pitch, gain, and a reference to a buffer.Multiple sources can share the same buffer, allowing the same sound effect to play simultaneously from different positions with independent gain and pitch.
Load audio data into the buffer
alBufferData uploads raw PCM samples to the GPU-side buffer. The ALCDemo sample loads an Ogg Vorbis file using STB Vorbis and passes the decoded PCM directly:| Constant | Description |
|---|---|
AL_FORMAT_MONO8 | 8-bit mono |
AL_FORMAT_MONO16 | 16-bit mono (most common for decoded audio) |
AL_FORMAT_STEREO8 | 8-bit stereo |
AL_FORMAT_STEREO16 | 16-bit stereo |
3D positional effects (distance attenuation, panning) only work with mono sources. Stereo buffers are played back as-is without spatial processing.
Attach the buffer to the source
AL_BUFFER is a source property of integer type, set via alSourcei. After this call the source references the buffer but does not yet play it.Position the listener and source
OpenAL uses a right-handed coordinate system. The listener represents the player’s ears; all distance and panning calculations are relative to it.Relevant spatial constants:
| Constant | Used with |
|---|---|
AL_POSITION | alListener3f, alSource3f |
AL_VELOCITY | alListener3f, alSource3f (for Doppler) |
AL_ORIENTATION | alListenerfv (listener only) |
AL_GAIN | alSourcef, alListenerf |
Play the source
AL_SOURCE_STATE returns one of AL_INITIAL, AL_PLAYING, AL_PAUSED, or AL_STOPPED. You can pause, resume, stop, and rewind with alSourcePause, alSourcePlay, alSourceStop, and alSourceRewind.For looping sounds, set alSourcei(source, AL_LOOPING, AL_TRUE) before calling alSourcePlay.Streaming audio for long tracks
Loading an entire audio file into a single buffer works well for short sound effects, but for music or dialogue you should stream the data through a queue of rotating buffers:EFX — effects and reverb
OpenAL Soft supports the EFX extension (ALC_EXT_EFX), which adds environmental reverb, chorus, distortion, and other DSP effects. Check deviceCaps.ALC_EXT_EFX before using EFX functions. The EFXTest.java sample demonstrates reverb presets.