2 * OpenAL Reverb Example
4 * Copyright (c) 2012 by Chris Robinson <chris.kcat@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /* This file contains an example for applying reverb to a sound. */
39 #include "AL/efx-presets.h"
41 #include "common/alhelpers.h"
44 /* Effect object functions */
45 static LPALGENEFFECTS alGenEffects
;
46 static LPALDELETEEFFECTS alDeleteEffects
;
47 static LPALISEFFECT alIsEffect
;
48 static LPALEFFECTI alEffecti
;
49 static LPALEFFECTIV alEffectiv
;
50 static LPALEFFECTF alEffectf
;
51 static LPALEFFECTFV alEffectfv
;
52 static LPALGETEFFECTI alGetEffecti
;
53 static LPALGETEFFECTIV alGetEffectiv
;
54 static LPALGETEFFECTF alGetEffectf
;
55 static LPALGETEFFECTFV alGetEffectfv
;
57 /* Auxiliary Effect Slot object functions */
58 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
;
59 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
;
60 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot
;
61 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
;
62 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv
;
63 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
;
64 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
;
65 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti
;
66 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv
;
67 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf
;
68 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv
;
71 /* LoadEffect loads the given reverb properties into a new OpenAL effect
72 * object, and returns the new effect ID. */
73 static ALuint
LoadEffect(const EFXEAXREVERBPROPERTIES
*reverb
)
78 /* Create the effect object and check if we can do EAX reverb. */
79 alGenEffects(1, &effect
);
80 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
82 printf("Using EAX Reverb\n");
84 /* EAX Reverb is available. Set the EAX effect type then load the
85 * reverb properties. */
86 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_EAXREVERB
);
88 alEffectf(effect
, AL_EAXREVERB_DENSITY
, reverb
->flDensity
);
89 alEffectf(effect
, AL_EAXREVERB_DIFFUSION
, reverb
->flDiffusion
);
90 alEffectf(effect
, AL_EAXREVERB_GAIN
, reverb
->flGain
);
91 alEffectf(effect
, AL_EAXREVERB_GAINHF
, reverb
->flGainHF
);
92 alEffectf(effect
, AL_EAXREVERB_GAINLF
, reverb
->flGainLF
);
93 alEffectf(effect
, AL_EAXREVERB_DECAY_TIME
, reverb
->flDecayTime
);
94 alEffectf(effect
, AL_EAXREVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
95 alEffectf(effect
, AL_EAXREVERB_DECAY_LFRATIO
, reverb
->flDecayLFRatio
);
96 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
97 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
98 alEffectfv(effect
, AL_EAXREVERB_REFLECTIONS_PAN
, reverb
->flReflectionsPan
);
99 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
100 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
101 alEffectfv(effect
, AL_EAXREVERB_LATE_REVERB_PAN
, reverb
->flLateReverbPan
);
102 alEffectf(effect
, AL_EAXREVERB_ECHO_TIME
, reverb
->flEchoTime
);
103 alEffectf(effect
, AL_EAXREVERB_ECHO_DEPTH
, reverb
->flEchoDepth
);
104 alEffectf(effect
, AL_EAXREVERB_MODULATION_TIME
, reverb
->flModulationTime
);
105 alEffectf(effect
, AL_EAXREVERB_MODULATION_DEPTH
, reverb
->flModulationDepth
);
106 alEffectf(effect
, AL_EAXREVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
107 alEffectf(effect
, AL_EAXREVERB_HFREFERENCE
, reverb
->flHFReference
);
108 alEffectf(effect
, AL_EAXREVERB_LFREFERENCE
, reverb
->flLFReference
);
109 alEffectf(effect
, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
110 alEffecti(effect
, AL_EAXREVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
114 printf("Using Standard Reverb\n");
116 /* No EAX Reverb. Set the standard reverb effect type then load the
117 * available reverb properties. */
118 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_REVERB
);
120 alEffectf(effect
, AL_REVERB_DENSITY
, reverb
->flDensity
);
121 alEffectf(effect
, AL_REVERB_DIFFUSION
, reverb
->flDiffusion
);
122 alEffectf(effect
, AL_REVERB_GAIN
, reverb
->flGain
);
123 alEffectf(effect
, AL_REVERB_GAINHF
, reverb
->flGainHF
);
124 alEffectf(effect
, AL_REVERB_DECAY_TIME
, reverb
->flDecayTime
);
125 alEffectf(effect
, AL_REVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
126 alEffectf(effect
, AL_REVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
127 alEffectf(effect
, AL_REVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
128 alEffectf(effect
, AL_REVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
129 alEffectf(effect
, AL_REVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
130 alEffectf(effect
, AL_REVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
131 alEffectf(effect
, AL_REVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
132 alEffecti(effect
, AL_REVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
135 /* Check if an error occured, and clean up if so. */
137 if(err
!= AL_NO_ERROR
)
139 fprintf(stderr
, "OpenAL error: %s\n", alGetString(err
));
140 if(alIsEffect(effect
))
141 alDeleteEffects(1, &effect
);
149 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
150 * returns the new buffer ID.
152 static ALuint
LoadSound(const char *filename
)
159 sf_count_t num_frames
;
162 /* Open the audio file and check that it's usable. */
163 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
166 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
169 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(short))/sfinfo
.channels
)
171 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
176 /* Get the sound format, and figure out the OpenAL format */
178 if(sfinfo
.channels
== 1)
179 format
= AL_FORMAT_MONO16
;
180 else if(sfinfo
.channels
== 2)
181 format
= AL_FORMAT_STEREO16
;
182 else if(sfinfo
.channels
== 3)
184 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
185 format
= AL_FORMAT_BFORMAT2D_16
;
187 else if(sfinfo
.channels
== 4)
189 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
190 format
= AL_FORMAT_BFORMAT3D_16
;
194 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
199 /* Decode the whole audio file to a buffer. */
200 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(short));
202 num_frames
= sf_readf_short(sndfile
, membuf
, sfinfo
.frames
);
207 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
210 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(short);
212 /* Buffer the audio data into a new buffer object, then free the data and
216 alGenBuffers(1, &buffer
);
217 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
222 /* Check if an error occured, and clean up if so. */
224 if(err
!= AL_NO_ERROR
)
226 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
227 if(buffer
&& alIsBuffer(buffer
))
228 alDeleteBuffers(1, &buffer
);
236 int main(int argc
, char **argv
)
238 EFXEAXREVERBPROPERTIES reverb
= EFX_REVERB_PRESET_GENERIC
;
239 ALuint source
, buffer
, effect
, slot
;
242 /* Print out usage if no arguments were specified */
245 fprintf(stderr
, "Usage: %s [-device <name] <filename>\n", argv
[0]);
249 /* Initialize OpenAL, and check for EFX support. */
251 if(InitAL(&argv
, &argc
) != 0)
254 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
256 fprintf(stderr
, "Error: EFX not supported\n");
261 /* Define a macro to help load the function pointers. */
262 #define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x))
263 LOAD_PROC(LPALGENEFFECTS
, alGenEffects
);
264 LOAD_PROC(LPALDELETEEFFECTS
, alDeleteEffects
);
265 LOAD_PROC(LPALISEFFECT
, alIsEffect
);
266 LOAD_PROC(LPALEFFECTI
, alEffecti
);
267 LOAD_PROC(LPALEFFECTIV
, alEffectiv
);
268 LOAD_PROC(LPALEFFECTF
, alEffectf
);
269 LOAD_PROC(LPALEFFECTFV
, alEffectfv
);
270 LOAD_PROC(LPALGETEFFECTI
, alGetEffecti
);
271 LOAD_PROC(LPALGETEFFECTIV
, alGetEffectiv
);
272 LOAD_PROC(LPALGETEFFECTF
, alGetEffectf
);
273 LOAD_PROC(LPALGETEFFECTFV
, alGetEffectfv
);
275 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS
, alGenAuxiliaryEffectSlots
);
276 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS
, alDeleteAuxiliaryEffectSlots
);
277 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT
, alIsAuxiliaryEffectSlot
);
278 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI
, alAuxiliaryEffectSloti
);
279 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV
, alAuxiliaryEffectSlotiv
);
280 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF
, alAuxiliaryEffectSlotf
);
281 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV
, alAuxiliaryEffectSlotfv
);
282 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI
, alGetAuxiliaryEffectSloti
);
283 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV
, alGetAuxiliaryEffectSlotiv
);
284 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF
, alGetAuxiliaryEffectSlotf
);
285 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV
, alGetAuxiliaryEffectSlotfv
);
288 /* Load the sound into a buffer. */
289 buffer
= LoadSound(argv
[0]);
296 /* Load the reverb into an effect. */
297 effect
= LoadEffect(&reverb
);
300 alDeleteBuffers(1, &buffer
);
305 /* Create the effect slot object. This is what "plays" an effect on sources
306 * that connect to it. */
308 alGenAuxiliaryEffectSlots(1, &slot
);
310 /* Tell the effect slot to use the loaded effect object. Note that the this
311 * effectively copies the effect properties. You can modify or delete the
312 * effect object afterward without affecting the effect slot.
314 alAuxiliaryEffectSloti(slot
, AL_EFFECTSLOT_EFFECT
, (ALint
)effect
);
315 assert(alGetError()==AL_NO_ERROR
&& "Failed to set effect slot");
317 /* Create the source to play the sound with. */
319 alGenSources(1, &source
);
320 alSourcei(source
, AL_BUFFER
, (ALint
)buffer
);
322 /* Connect the source to the effect slot. This tells the source to use the
323 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
325 alSource3i(source
, AL_AUXILIARY_SEND_FILTER
, (ALint
)slot
, 0, AL_FILTER_NULL
);
326 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
328 /* Play the sound until it finishes. */
329 alSourcePlay(source
);
331 al_nssleep(10000000);
332 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
333 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
335 /* All done. Delete resources, and close down OpenAL. */
336 alDeleteSources(1, &source
);
337 alDeleteAuxiliaryEffectSlots(1, &slot
);
338 alDeleteEffects(1, &effect
);
339 alDeleteBuffers(1, &buffer
);