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. */
38 #include "AL/efx-presets.h"
40 #include "common/alhelpers.h"
43 /* Effect object functions */
44 static LPALGENEFFECTS alGenEffects
;
45 static LPALDELETEEFFECTS alDeleteEffects
;
46 static LPALISEFFECT alIsEffect
;
47 static LPALEFFECTI alEffecti
;
48 static LPALEFFECTIV alEffectiv
;
49 static LPALEFFECTF alEffectf
;
50 static LPALEFFECTFV alEffectfv
;
51 static LPALGETEFFECTI alGetEffecti
;
52 static LPALGETEFFECTIV alGetEffectiv
;
53 static LPALGETEFFECTF alGetEffectf
;
54 static LPALGETEFFECTFV alGetEffectfv
;
56 /* Auxiliary Effect Slot object functions */
57 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
;
58 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
;
59 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot
;
60 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
;
61 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv
;
62 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
;
63 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
;
64 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti
;
65 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv
;
66 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf
;
67 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv
;
70 /* LoadEffect loads the given reverb properties into a new OpenAL effect
71 * object, and returns the new effect ID. */
72 static ALuint
LoadEffect(const EFXEAXREVERBPROPERTIES
*reverb
)
77 /* Create the effect object and check if we can do EAX reverb. */
78 alGenEffects(1, &effect
);
79 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
81 printf("Using EAX Reverb\n");
83 /* EAX Reverb is available. Set the EAX effect type then load the
84 * reverb properties. */
85 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_EAXREVERB
);
87 alEffectf(effect
, AL_EAXREVERB_DENSITY
, reverb
->flDensity
);
88 alEffectf(effect
, AL_EAXREVERB_DIFFUSION
, reverb
->flDiffusion
);
89 alEffectf(effect
, AL_EAXREVERB_GAIN
, reverb
->flGain
);
90 alEffectf(effect
, AL_EAXREVERB_GAINHF
, reverb
->flGainHF
);
91 alEffectf(effect
, AL_EAXREVERB_GAINLF
, reverb
->flGainLF
);
92 alEffectf(effect
, AL_EAXREVERB_DECAY_TIME
, reverb
->flDecayTime
);
93 alEffectf(effect
, AL_EAXREVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
94 alEffectf(effect
, AL_EAXREVERB_DECAY_LFRATIO
, reverb
->flDecayLFRatio
);
95 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
96 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
97 alEffectfv(effect
, AL_EAXREVERB_REFLECTIONS_PAN
, reverb
->flReflectionsPan
);
98 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
99 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
100 alEffectfv(effect
, AL_EAXREVERB_LATE_REVERB_PAN
, reverb
->flLateReverbPan
);
101 alEffectf(effect
, AL_EAXREVERB_ECHO_TIME
, reverb
->flEchoTime
);
102 alEffectf(effect
, AL_EAXREVERB_ECHO_DEPTH
, reverb
->flEchoDepth
);
103 alEffectf(effect
, AL_EAXREVERB_MODULATION_TIME
, reverb
->flModulationTime
);
104 alEffectf(effect
, AL_EAXREVERB_MODULATION_DEPTH
, reverb
->flModulationDepth
);
105 alEffectf(effect
, AL_EAXREVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
106 alEffectf(effect
, AL_EAXREVERB_HFREFERENCE
, reverb
->flHFReference
);
107 alEffectf(effect
, AL_EAXREVERB_LFREFERENCE
, reverb
->flLFReference
);
108 alEffectf(effect
, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
109 alEffecti(effect
, AL_EAXREVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
113 printf("Using Standard Reverb\n");
115 /* No EAX Reverb. Set the standard reverb effect type then load the
116 * available reverb properties. */
117 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_REVERB
);
119 alEffectf(effect
, AL_REVERB_DENSITY
, reverb
->flDensity
);
120 alEffectf(effect
, AL_REVERB_DIFFUSION
, reverb
->flDiffusion
);
121 alEffectf(effect
, AL_REVERB_GAIN
, reverb
->flGain
);
122 alEffectf(effect
, AL_REVERB_GAINHF
, reverb
->flGainHF
);
123 alEffectf(effect
, AL_REVERB_DECAY_TIME
, reverb
->flDecayTime
);
124 alEffectf(effect
, AL_REVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
125 alEffectf(effect
, AL_REVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
126 alEffectf(effect
, AL_REVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
127 alEffectf(effect
, AL_REVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
128 alEffectf(effect
, AL_REVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
129 alEffectf(effect
, AL_REVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
130 alEffectf(effect
, AL_REVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
131 alEffecti(effect
, AL_REVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
134 /* Check if an error occured, and clean up if so. */
136 if(err
!= AL_NO_ERROR
)
138 fprintf(stderr
, "OpenAL error: %s\n", alGetString(err
));
139 if(alIsEffect(effect
))
140 alDeleteEffects(1, &effect
);
148 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
149 * returns the new buffer ID.
151 static ALuint
LoadSound(const char *filename
)
158 sf_count_t num_frames
;
161 /* Open the audio file and check that it's usable. */
162 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
165 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
168 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(short))/sfinfo
.channels
)
170 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
175 /* Get the sound format, and figure out the OpenAL format */
176 if(sfinfo
.channels
== 1)
177 format
= AL_FORMAT_MONO16
;
178 else if(sfinfo
.channels
== 2)
179 format
= AL_FORMAT_STEREO16
;
182 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
187 /* Decode the whole audio file to a buffer. */
188 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(short));
190 num_frames
= sf_readf_short(sndfile
, membuf
, sfinfo
.frames
);
195 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
198 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(short);
200 /* Buffer the audio data into a new buffer object, then free the data and
204 alGenBuffers(1, &buffer
);
205 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
210 /* Check if an error occured, and clean up if so. */
212 if(err
!= AL_NO_ERROR
)
214 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
215 if(buffer
&& alIsBuffer(buffer
))
216 alDeleteBuffers(1, &buffer
);
224 int main(int argc
, char **argv
)
226 EFXEAXREVERBPROPERTIES reverb
= EFX_REVERB_PRESET_GENERIC
;
227 ALuint source
, buffer
, effect
, slot
;
230 /* Print out usage if no arguments were specified */
233 fprintf(stderr
, "Usage: %s [-device <name] <filename>\n", argv
[0]);
237 /* Initialize OpenAL, and check for EFX support. */
239 if(InitAL(&argv
, &argc
) != 0)
242 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
244 fprintf(stderr
, "Error: EFX not supported\n");
249 /* Define a macro to help load the function pointers. */
250 #define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x))
251 LOAD_PROC(LPALGENEFFECTS
, alGenEffects
);
252 LOAD_PROC(LPALDELETEEFFECTS
, alDeleteEffects
);
253 LOAD_PROC(LPALISEFFECT
, alIsEffect
);
254 LOAD_PROC(LPALEFFECTI
, alEffecti
);
255 LOAD_PROC(LPALEFFECTIV
, alEffectiv
);
256 LOAD_PROC(LPALEFFECTF
, alEffectf
);
257 LOAD_PROC(LPALEFFECTFV
, alEffectfv
);
258 LOAD_PROC(LPALGETEFFECTI
, alGetEffecti
);
259 LOAD_PROC(LPALGETEFFECTIV
, alGetEffectiv
);
260 LOAD_PROC(LPALGETEFFECTF
, alGetEffectf
);
261 LOAD_PROC(LPALGETEFFECTFV
, alGetEffectfv
);
263 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS
, alGenAuxiliaryEffectSlots
);
264 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS
, alDeleteAuxiliaryEffectSlots
);
265 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT
, alIsAuxiliaryEffectSlot
);
266 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI
, alAuxiliaryEffectSloti
);
267 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV
, alAuxiliaryEffectSlotiv
);
268 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF
, alAuxiliaryEffectSlotf
);
269 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV
, alAuxiliaryEffectSlotfv
);
270 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI
, alGetAuxiliaryEffectSloti
);
271 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV
, alGetAuxiliaryEffectSlotiv
);
272 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF
, alGetAuxiliaryEffectSlotf
);
273 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV
, alGetAuxiliaryEffectSlotfv
);
276 /* Load the sound into a buffer. */
277 buffer
= LoadSound(argv
[0]);
284 /* Load the reverb into an effect. */
285 effect
= LoadEffect(&reverb
);
288 alDeleteBuffers(1, &buffer
);
293 /* Create the effect slot object. This is what "plays" an effect on sources
294 * that connect to it. */
296 alGenAuxiliaryEffectSlots(1, &slot
);
298 /* Tell the effect slot to use the loaded effect object. Note that the this
299 * effectively copies the effect properties. You can modify or delete the
300 * effect object afterward without affecting the effect slot.
302 alAuxiliaryEffectSloti(slot
, AL_EFFECTSLOT_EFFECT
, (ALint
)effect
);
303 assert(alGetError()==AL_NO_ERROR
&& "Failed to set effect slot");
305 /* Create the source to play the sound with. */
307 alGenSources(1, &source
);
308 alSourcei(source
, AL_BUFFER
, (ALint
)buffer
);
310 /* Connect the source to the effect slot. This tells the source to use the
311 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
313 alSource3i(source
, AL_AUXILIARY_SEND_FILTER
, (ALint
)slot
, 0, AL_FILTER_NULL
);
314 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
316 /* Play the sound until it finishes. */
317 alSourcePlay(source
);
319 al_nssleep(10000000);
320 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
321 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
323 /* All done. Delete resources, and close down OpenAL. */
324 alDeleteSources(1, &source
);
325 alDeleteAuxiliaryEffectSlots(1, &slot
);
326 alDeleteEffects(1, &effect
);
327 alDeleteBuffers(1, &buffer
);