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
;
70 /* C doesn't allow casting between function and non-function pointer types, so
71 * with C99 we need to use a union to reinterpret the pointer type. Pre-C99
72 * still needs to use a normal cast and live with the warning (C++ is fine with
73 * a regular reinterpret_cast).
75 #if __STDC_VERSION__ >= 199901L
76 #define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f
78 #define FUNCTION_CAST(T, ptr) (T)(ptr)
82 /* LoadEffect loads the given reverb properties into a new OpenAL effect
83 * object, and returns the new effect ID. */
84 static ALuint
LoadEffect(const EFXEAXREVERBPROPERTIES
*reverb
)
89 /* Create the effect object and check if we can do EAX reverb. */
90 alGenEffects(1, &effect
);
91 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
93 printf("Using EAX Reverb\n");
95 /* EAX Reverb is available. Set the EAX effect type then load the
96 * reverb properties. */
97 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_EAXREVERB
);
99 alEffectf(effect
, AL_EAXREVERB_DENSITY
, reverb
->flDensity
);
100 alEffectf(effect
, AL_EAXREVERB_DIFFUSION
, reverb
->flDiffusion
);
101 alEffectf(effect
, AL_EAXREVERB_GAIN
, reverb
->flGain
);
102 alEffectf(effect
, AL_EAXREVERB_GAINHF
, reverb
->flGainHF
);
103 alEffectf(effect
, AL_EAXREVERB_GAINLF
, reverb
->flGainLF
);
104 alEffectf(effect
, AL_EAXREVERB_DECAY_TIME
, reverb
->flDecayTime
);
105 alEffectf(effect
, AL_EAXREVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
106 alEffectf(effect
, AL_EAXREVERB_DECAY_LFRATIO
, reverb
->flDecayLFRatio
);
107 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
108 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
109 alEffectfv(effect
, AL_EAXREVERB_REFLECTIONS_PAN
, reverb
->flReflectionsPan
);
110 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
111 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
112 alEffectfv(effect
, AL_EAXREVERB_LATE_REVERB_PAN
, reverb
->flLateReverbPan
);
113 alEffectf(effect
, AL_EAXREVERB_ECHO_TIME
, reverb
->flEchoTime
);
114 alEffectf(effect
, AL_EAXREVERB_ECHO_DEPTH
, reverb
->flEchoDepth
);
115 alEffectf(effect
, AL_EAXREVERB_MODULATION_TIME
, reverb
->flModulationTime
);
116 alEffectf(effect
, AL_EAXREVERB_MODULATION_DEPTH
, reverb
->flModulationDepth
);
117 alEffectf(effect
, AL_EAXREVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
118 alEffectf(effect
, AL_EAXREVERB_HFREFERENCE
, reverb
->flHFReference
);
119 alEffectf(effect
, AL_EAXREVERB_LFREFERENCE
, reverb
->flLFReference
);
120 alEffectf(effect
, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
121 alEffecti(effect
, AL_EAXREVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
125 printf("Using Standard Reverb\n");
127 /* No EAX Reverb. Set the standard reverb effect type then load the
128 * available reverb properties. */
129 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_REVERB
);
131 alEffectf(effect
, AL_REVERB_DENSITY
, reverb
->flDensity
);
132 alEffectf(effect
, AL_REVERB_DIFFUSION
, reverb
->flDiffusion
);
133 alEffectf(effect
, AL_REVERB_GAIN
, reverb
->flGain
);
134 alEffectf(effect
, AL_REVERB_GAINHF
, reverb
->flGainHF
);
135 alEffectf(effect
, AL_REVERB_DECAY_TIME
, reverb
->flDecayTime
);
136 alEffectf(effect
, AL_REVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
137 alEffectf(effect
, AL_REVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
138 alEffectf(effect
, AL_REVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
139 alEffectf(effect
, AL_REVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
140 alEffectf(effect
, AL_REVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
141 alEffectf(effect
, AL_REVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
142 alEffectf(effect
, AL_REVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
143 alEffecti(effect
, AL_REVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
146 /* Check if an error occured, and clean up if so. */
148 if(err
!= AL_NO_ERROR
)
150 fprintf(stderr
, "OpenAL error: %s\n", alGetString(err
));
151 if(alIsEffect(effect
))
152 alDeleteEffects(1, &effect
);
160 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
161 * returns the new buffer ID.
163 static ALuint
LoadSound(const char *filename
)
170 sf_count_t num_frames
;
173 /* Open the audio file and check that it's usable. */
174 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
177 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
180 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(short))/sfinfo
.channels
)
182 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
187 /* Get the sound format, and figure out the OpenAL format */
189 if(sfinfo
.channels
== 1)
190 format
= AL_FORMAT_MONO16
;
191 else if(sfinfo
.channels
== 2)
192 format
= AL_FORMAT_STEREO16
;
193 else if(sfinfo
.channels
== 3)
195 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
196 format
= AL_FORMAT_BFORMAT2D_16
;
198 else if(sfinfo
.channels
== 4)
200 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
201 format
= AL_FORMAT_BFORMAT3D_16
;
205 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
210 /* Decode the whole audio file to a buffer. */
211 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(short));
213 num_frames
= sf_readf_short(sndfile
, membuf
, sfinfo
.frames
);
218 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
221 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(short);
223 /* Buffer the audio data into a new buffer object, then free the data and
227 alGenBuffers(1, &buffer
);
228 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
233 /* Check if an error occured, and clean up if so. */
235 if(err
!= AL_NO_ERROR
)
237 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
238 if(buffer
&& alIsBuffer(buffer
))
239 alDeleteBuffers(1, &buffer
);
247 int main(int argc
, char **argv
)
249 EFXEAXREVERBPROPERTIES reverb
= EFX_REVERB_PRESET_GENERIC
;
250 ALuint source
, buffer
, effect
, slot
;
253 /* Print out usage if no arguments were specified */
256 fprintf(stderr
, "Usage: %s [-device <name] <filename>\n", argv
[0]);
260 /* Initialize OpenAL, and check for EFX support. */
262 if(InitAL(&argv
, &argc
) != 0)
265 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
267 fprintf(stderr
, "Error: EFX not supported\n");
272 /* Define a macro to help load the function pointers. */
273 #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
274 LOAD_PROC(LPALGENEFFECTS
, alGenEffects
);
275 LOAD_PROC(LPALDELETEEFFECTS
, alDeleteEffects
);
276 LOAD_PROC(LPALISEFFECT
, alIsEffect
);
277 LOAD_PROC(LPALEFFECTI
, alEffecti
);
278 LOAD_PROC(LPALEFFECTIV
, alEffectiv
);
279 LOAD_PROC(LPALEFFECTF
, alEffectf
);
280 LOAD_PROC(LPALEFFECTFV
, alEffectfv
);
281 LOAD_PROC(LPALGETEFFECTI
, alGetEffecti
);
282 LOAD_PROC(LPALGETEFFECTIV
, alGetEffectiv
);
283 LOAD_PROC(LPALGETEFFECTF
, alGetEffectf
);
284 LOAD_PROC(LPALGETEFFECTFV
, alGetEffectfv
);
286 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS
, alGenAuxiliaryEffectSlots
);
287 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS
, alDeleteAuxiliaryEffectSlots
);
288 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT
, alIsAuxiliaryEffectSlot
);
289 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI
, alAuxiliaryEffectSloti
);
290 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV
, alAuxiliaryEffectSlotiv
);
291 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF
, alAuxiliaryEffectSlotf
);
292 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV
, alAuxiliaryEffectSlotfv
);
293 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI
, alGetAuxiliaryEffectSloti
);
294 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV
, alGetAuxiliaryEffectSlotiv
);
295 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF
, alGetAuxiliaryEffectSlotf
);
296 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV
, alGetAuxiliaryEffectSlotfv
);
299 /* Load the sound into a buffer. */
300 buffer
= LoadSound(argv
[0]);
307 /* Load the reverb into an effect. */
308 effect
= LoadEffect(&reverb
);
311 alDeleteBuffers(1, &buffer
);
316 /* Create the effect slot object. This is what "plays" an effect on sources
317 * that connect to it. */
319 alGenAuxiliaryEffectSlots(1, &slot
);
321 /* Tell the effect slot to use the loaded effect object. Note that the this
322 * effectively copies the effect properties. You can modify or delete the
323 * effect object afterward without affecting the effect slot.
325 alAuxiliaryEffectSloti(slot
, AL_EFFECTSLOT_EFFECT
, (ALint
)effect
);
326 assert(alGetError()==AL_NO_ERROR
&& "Failed to set effect slot");
328 /* Create the source to play the sound with. */
330 alGenSources(1, &source
);
331 alSourcei(source
, AL_BUFFER
, (ALint
)buffer
);
333 /* Connect the source to the effect slot. This tells the source to use the
334 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
336 alSource3i(source
, AL_AUXILIARY_SEND_FILTER
, (ALint
)slot
, 0, AL_FILTER_NULL
);
337 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
339 /* Play the sound until it finishes. */
340 alSourcePlay(source
);
342 al_nssleep(10000000);
343 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
344 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
346 /* All done. Delete resources, and close down OpenAL. */
347 alDeleteSources(1, &source
);
348 alDeleteAuxiliaryEffectSlots(1, &slot
);
349 alDeleteEffects(1, &effect
);
350 alDeleteBuffers(1, &buffer
);