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. */
30 #define WIN32_LEAN_AND_MEAN
34 #define Sleep(x) usleep((x)*1000)
40 #include "AL/efx-presets.h"
42 #include "common/alhelpers.h"
43 #include "common/alffmpeg.h"
46 LPALBUFFERSAMPLESSOFT alBufferSamplesSOFT
= wrap_BufferSamples
;
47 LPALISBUFFERFORMATSUPPORTEDSOFT alIsBufferFormatSupportedSOFT
;
49 /* Effect object functions */
50 LPALGENEFFECTS alGenEffects
;
51 LPALDELETEEFFECTS alDeleteEffects
;
52 LPALISEFFECT alIsEffect
;
53 LPALEFFECTI alEffecti
;
54 LPALEFFECTIV alEffectiv
;
55 LPALEFFECTF alEffectf
;
56 LPALEFFECTFV alEffectfv
;
57 LPALGETEFFECTI alGetEffecti
;
58 LPALGETEFFECTIV alGetEffectiv
;
59 LPALGETEFFECTF alGetEffectf
;
60 LPALGETEFFECTFV alGetEffectfv
;
62 /* Auxiliary Effect Slot object functions */
63 LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
;
64 LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
;
65 LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot
;
66 LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
;
67 LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv
;
68 LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
;
69 LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
;
70 LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti
;
71 LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv
;
72 LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf
;
73 LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv
;
76 /* LoadEffect loads the given reverb properties into a new OpenAL effect
77 * object, and returns the new effect ID. */
78 static ALuint
LoadEffect(const EFXEAXREVERBPROPERTIES
*reverb
)
83 /* Create the effect object and check if we can do EAX reverb. */
84 alGenEffects(1, &effect
);
85 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
87 printf("Using EAX Reverb\n");
89 /* EAX Reverb is available. Set the EAX effect type then load the
90 * reverb properties. */
91 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_EAXREVERB
);
93 alEffectf(effect
, AL_EAXREVERB_DENSITY
, reverb
->flDensity
);
94 alEffectf(effect
, AL_EAXREVERB_DIFFUSION
, reverb
->flDiffusion
);
95 alEffectf(effect
, AL_EAXREVERB_GAIN
, reverb
->flGain
);
96 alEffectf(effect
, AL_EAXREVERB_GAINHF
, reverb
->flGainHF
);
97 alEffectf(effect
, AL_EAXREVERB_GAINLF
, reverb
->flGainLF
);
98 alEffectf(effect
, AL_EAXREVERB_DECAY_TIME
, reverb
->flDecayTime
);
99 alEffectf(effect
, AL_EAXREVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
100 alEffectf(effect
, AL_EAXREVERB_DECAY_LFRATIO
, reverb
->flDecayLFRatio
);
101 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
102 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
103 alEffectfv(effect
, AL_EAXREVERB_REFLECTIONS_PAN
, reverb
->flReflectionsPan
);
104 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
105 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
106 alEffectfv(effect
, AL_EAXREVERB_LATE_REVERB_PAN
, reverb
->flLateReverbPan
);
107 alEffectf(effect
, AL_EAXREVERB_ECHO_TIME
, reverb
->flEchoTime
);
108 alEffectf(effect
, AL_EAXREVERB_ECHO_DEPTH
, reverb
->flEchoDepth
);
109 alEffectf(effect
, AL_EAXREVERB_MODULATION_TIME
, reverb
->flModulationTime
);
110 alEffectf(effect
, AL_EAXREVERB_MODULATION_DEPTH
, reverb
->flModulationDepth
);
111 alEffectf(effect
, AL_EAXREVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
112 alEffectf(effect
, AL_EAXREVERB_HFREFERENCE
, reverb
->flHFReference
);
113 alEffectf(effect
, AL_EAXREVERB_LFREFERENCE
, reverb
->flLFReference
);
114 alEffectf(effect
, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
115 alEffecti(effect
, AL_EAXREVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
119 printf("Using Standard Reverb\n");
121 /* No EAX Reverb. Set the standard reverb effect type then load the
122 * available reverb properties. */
123 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_REVERB
);
125 alEffectf(effect
, AL_REVERB_DENSITY
, reverb
->flDensity
);
126 alEffectf(effect
, AL_REVERB_DIFFUSION
, reverb
->flDiffusion
);
127 alEffectf(effect
, AL_REVERB_GAIN
, reverb
->flGain
);
128 alEffectf(effect
, AL_REVERB_GAINHF
, reverb
->flGainHF
);
129 alEffectf(effect
, AL_REVERB_DECAY_TIME
, reverb
->flDecayTime
);
130 alEffectf(effect
, AL_REVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
131 alEffectf(effect
, AL_REVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
132 alEffectf(effect
, AL_REVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
133 alEffectf(effect
, AL_REVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
134 alEffectf(effect
, AL_REVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
135 alEffectf(effect
, AL_REVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
136 alEffectf(effect
, AL_REVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
137 alEffecti(effect
, AL_REVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
140 /* Check if an error occured, and clean up if so. */
142 if(err
!= AL_NO_ERROR
)
144 fprintf(stderr
, "OpenAL error: %s\n", alGetString(err
));
145 if(alIsEffect(effect
))
146 alDeleteEffects(1, &effect
);
154 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
155 * returns the new buffer ID. */
156 static ALuint
LoadSound(const char *filename
)
158 ALenum err
, format
, type
, channels
;
165 /* Open the file and get the first stream from it */
166 audiofile
= openAVFile(filename
);
167 sound
= getAVAudioStream(audiofile
, 0);
170 fprintf(stderr
, "Could not open audio in %s\n", filename
);
171 closeAVFile(audiofile
);
175 /* Get the sound format, and figure out the OpenAL format */
176 if(getAVAudioInfo(sound
, &rate
, &channels
, &type
) != 0)
178 fprintf(stderr
, "Error getting audio info for %s\n", filename
);
179 closeAVFile(audiofile
);
183 format
= GetFormat(channels
, type
, alIsBufferFormatSupportedSOFT
);
184 if(format
== AL_NONE
)
186 fprintf(stderr
, "Unsupported format (%s, %s) for %s\n",
187 ChannelsName(channels
), TypeName(type
), filename
);
188 closeAVFile(audiofile
);
192 /* Decode the whole audio stream to a buffer. */
193 data
= decodeAVAudioStream(sound
, &datalen
);
196 fprintf(stderr
, "Failed to read audio from %s\n", filename
);
197 closeAVFile(audiofile
);
201 /* Buffer the audio data into a new buffer object, then free the data and
204 alGenBuffers(1, &buffer
);
205 alBufferSamplesSOFT(buffer
, rate
, format
, BytesToFrames(datalen
, channels
, type
),
206 channels
, type
, data
);
208 closeAVFile(audiofile
);
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(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 file was specified */
233 fprintf(stderr
, "Usage: %s <filename>\n", argv
[0]);
237 /* Initialize OpenAL with the default device, and check for EFX support. */
241 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
243 fprintf(stderr
, "Error: EFX not supported\n");
248 /* Define a macro to help load the function pointers. */
249 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
250 LOAD_PROC(alGenEffects
);
251 LOAD_PROC(alDeleteEffects
);
252 LOAD_PROC(alIsEffect
);
253 LOAD_PROC(alEffecti
);
254 LOAD_PROC(alEffectiv
);
255 LOAD_PROC(alEffectf
);
256 LOAD_PROC(alEffectfv
);
257 LOAD_PROC(alGetEffecti
);
258 LOAD_PROC(alGetEffectiv
);
259 LOAD_PROC(alGetEffectf
);
260 LOAD_PROC(alGetEffectfv
);
262 LOAD_PROC(alGenAuxiliaryEffectSlots
);
263 LOAD_PROC(alDeleteAuxiliaryEffectSlots
);
264 LOAD_PROC(alIsAuxiliaryEffectSlot
);
265 LOAD_PROC(alAuxiliaryEffectSloti
);
266 LOAD_PROC(alAuxiliaryEffectSlotiv
);
267 LOAD_PROC(alAuxiliaryEffectSlotf
);
268 LOAD_PROC(alAuxiliaryEffectSlotfv
);
269 LOAD_PROC(alGetAuxiliaryEffectSloti
);
270 LOAD_PROC(alGetAuxiliaryEffectSlotiv
);
271 LOAD_PROC(alGetAuxiliaryEffectSlotf
);
272 LOAD_PROC(alGetAuxiliaryEffectSlotfv
);
274 if(alIsExtensionPresent("AL_SOFT_buffer_samples"))
276 LOAD_PROC(alBufferSamplesSOFT
);
277 LOAD_PROC(alIsBufferFormatSupportedSOFT
);
281 /* Load the sound into a buffer. */
282 buffer
= LoadSound(argv
[1]);
289 /* Load the reverb into an effect. */
290 effect
= LoadEffect(&reverb
);
293 alDeleteBuffers(1, &buffer
);
298 /* Create the effect slot object. This is what "plays" an effect on sources
299 * that connect to it. */
301 alGenAuxiliaryEffectSlots(1, &slot
);
303 /* Tell the effect slot to use the loaded effect object. Note that the this
304 * effectively copies the effect properties. You can modify or delete the
305 * effect object afterward without affecting the effect slot.
307 alAuxiliaryEffectSloti(slot
, AL_EFFECTSLOT_EFFECT
, effect
);
308 assert(alGetError()==AL_NO_ERROR
&& "Failed to set effect slot");
310 /* Create the source to play the sound with. */
312 alGenSources(1, &source
);
313 alSourcei(source
, AL_BUFFER
, buffer
);
315 /* Connect the source to the effect slot. This tells the source to use the
316 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
318 alSource3i(source
, AL_AUXILIARY_SEND_FILTER
, slot
, 0, AL_FILTER_NULL
);
319 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
321 /* Play the sound until it finishes. */
322 alSourcePlay(source
);
325 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
326 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
);
328 /* All done. Delete resources, and close OpenAL. */
329 alDeleteSources(1, &source
);
330 alDeleteAuxiliaryEffectSlots(1, &slot
);
331 alDeleteEffects(1, &effect
);
332 alDeleteBuffers(1, &buffer
);