Report the number of IRs used from SOFA files
[openal-soft.git] / examples / alreverb.c
blobd789dffe311b43ff6b3be05a791b64f0b323fd16
1 /*
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
22 * THE SOFTWARE.
25 /* This file contains an example for applying reverb to a sound. */
27 #include <stdio.h>
28 #include <assert.h>
30 #include "SDL_sound.h"
31 #include "SDL_audio.h"
32 #include "SDL_stdinc.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
36 #include "AL/efx.h"
37 #include "AL/efx-presets.h"
39 #include "common/alhelpers.h"
42 /* Effect object functions */
43 static LPALGENEFFECTS alGenEffects;
44 static LPALDELETEEFFECTS alDeleteEffects;
45 static LPALISEFFECT alIsEffect;
46 static LPALEFFECTI alEffecti;
47 static LPALEFFECTIV alEffectiv;
48 static LPALEFFECTF alEffectf;
49 static LPALEFFECTFV alEffectfv;
50 static LPALGETEFFECTI alGetEffecti;
51 static LPALGETEFFECTIV alGetEffectiv;
52 static LPALGETEFFECTF alGetEffectf;
53 static LPALGETEFFECTFV alGetEffectfv;
55 /* Auxiliary Effect Slot object functions */
56 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots;
57 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots;
58 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot;
59 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti;
60 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv;
61 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf;
62 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv;
63 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti;
64 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv;
65 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf;
66 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
69 /* LoadEffect loads the given reverb properties into a new OpenAL effect
70 * object, and returns the new effect ID. */
71 static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
73 ALuint effect = 0;
74 ALenum err;
76 /* Create the effect object and check if we can do EAX reverb. */
77 alGenEffects(1, &effect);
78 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
80 printf("Using EAX Reverb\n");
82 /* EAX Reverb is available. Set the EAX effect type then load the
83 * reverb properties. */
84 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
86 alEffectf(effect, AL_EAXREVERB_DENSITY, reverb->flDensity);
87 alEffectf(effect, AL_EAXREVERB_DIFFUSION, reverb->flDiffusion);
88 alEffectf(effect, AL_EAXREVERB_GAIN, reverb->flGain);
89 alEffectf(effect, AL_EAXREVERB_GAINHF, reverb->flGainHF);
90 alEffectf(effect, AL_EAXREVERB_GAINLF, reverb->flGainLF);
91 alEffectf(effect, AL_EAXREVERB_DECAY_TIME, reverb->flDecayTime);
92 alEffectf(effect, AL_EAXREVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
93 alEffectf(effect, AL_EAXREVERB_DECAY_LFRATIO, reverb->flDecayLFRatio);
94 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
95 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
96 alEffectfv(effect, AL_EAXREVERB_REFLECTIONS_PAN, reverb->flReflectionsPan);
97 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
98 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
99 alEffectfv(effect, AL_EAXREVERB_LATE_REVERB_PAN, reverb->flLateReverbPan);
100 alEffectf(effect, AL_EAXREVERB_ECHO_TIME, reverb->flEchoTime);
101 alEffectf(effect, AL_EAXREVERB_ECHO_DEPTH, reverb->flEchoDepth);
102 alEffectf(effect, AL_EAXREVERB_MODULATION_TIME, reverb->flModulationTime);
103 alEffectf(effect, AL_EAXREVERB_MODULATION_DEPTH, reverb->flModulationDepth);
104 alEffectf(effect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
105 alEffectf(effect, AL_EAXREVERB_HFREFERENCE, reverb->flHFReference);
106 alEffectf(effect, AL_EAXREVERB_LFREFERENCE, reverb->flLFReference);
107 alEffectf(effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
108 alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
110 else
112 printf("Using Standard Reverb\n");
114 /* No EAX Reverb. Set the standard reverb effect type then load the
115 * available reverb properties. */
116 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
118 alEffectf(effect, AL_REVERB_DENSITY, reverb->flDensity);
119 alEffectf(effect, AL_REVERB_DIFFUSION, reverb->flDiffusion);
120 alEffectf(effect, AL_REVERB_GAIN, reverb->flGain);
121 alEffectf(effect, AL_REVERB_GAINHF, reverb->flGainHF);
122 alEffectf(effect, AL_REVERB_DECAY_TIME, reverb->flDecayTime);
123 alEffectf(effect, AL_REVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
124 alEffectf(effect, AL_REVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
125 alEffectf(effect, AL_REVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
126 alEffectf(effect, AL_REVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
127 alEffectf(effect, AL_REVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
128 alEffectf(effect, AL_REVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
129 alEffectf(effect, AL_REVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
130 alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
133 /* Check if an error occured, and clean up if so. */
134 err = alGetError();
135 if(err != AL_NO_ERROR)
137 fprintf(stderr, "OpenAL error: %s\n", alGetString(err));
138 if(alIsEffect(effect))
139 alDeleteEffects(1, &effect);
140 return 0;
143 return effect;
147 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
148 * returns the new buffer ID.
150 static ALuint LoadSound(const char *filename)
152 Sound_Sample *sample;
153 ALenum err, format;
154 ALuint buffer;
155 Uint32 slen;
157 /* Open the audio file */
158 sample = Sound_NewSampleFromFile(filename, NULL, 65536);
159 if(!sample)
161 fprintf(stderr, "Could not open audio in %s\n", filename);
162 return 0;
165 /* Get the sound format, and figure out the OpenAL format */
166 if(sample->actual.channels == 1)
168 if(sample->actual.format == AUDIO_U8)
169 format = AL_FORMAT_MONO8;
170 else if(sample->actual.format == AUDIO_S16SYS)
171 format = AL_FORMAT_MONO16;
172 else
174 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
175 Sound_FreeSample(sample);
176 return 0;
179 else if(sample->actual.channels == 2)
181 if(sample->actual.format == AUDIO_U8)
182 format = AL_FORMAT_STEREO8;
183 else if(sample->actual.format == AUDIO_S16SYS)
184 format = AL_FORMAT_STEREO16;
185 else
187 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
188 Sound_FreeSample(sample);
189 return 0;
192 else
194 fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
195 Sound_FreeSample(sample);
196 return 0;
199 /* Decode the whole audio stream to a buffer. */
200 slen = Sound_DecodeAll(sample);
201 if(!sample->buffer || slen == 0)
203 fprintf(stderr, "Failed to read audio from %s\n", filename);
204 Sound_FreeSample(sample);
205 return 0;
208 /* Buffer the audio data into a new buffer object, then free the data and
209 * close the file. */
210 buffer = 0;
211 alGenBuffers(1, &buffer);
212 alBufferData(buffer, format, sample->buffer, (ALsizei)slen, (ALsizei)sample->actual.rate);
213 Sound_FreeSample(sample);
215 /* Check if an error occured, and clean up if so. */
216 err = alGetError();
217 if(err != AL_NO_ERROR)
219 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
220 if(buffer && alIsBuffer(buffer))
221 alDeleteBuffers(1, &buffer);
222 return 0;
225 return buffer;
229 int main(int argc, char **argv)
231 EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
232 ALuint source, buffer, effect, slot;
233 ALenum state;
235 /* Print out usage if no arguments were specified */
236 if(argc < 2)
238 fprintf(stderr, "Usage: %s [-device <name] <filename>\n", argv[0]);
239 return 1;
242 /* Initialize OpenAL, and check for EFX support. */
243 argv++; argc--;
244 if(InitAL(&argv, &argc) != 0)
245 return 1;
247 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
249 fprintf(stderr, "Error: EFX not supported\n");
250 CloseAL();
251 return 1;
254 /* Define a macro to help load the function pointers. */
255 #define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x))
256 LOAD_PROC(LPALGENEFFECTS, alGenEffects);
257 LOAD_PROC(LPALDELETEEFFECTS, alDeleteEffects);
258 LOAD_PROC(LPALISEFFECT, alIsEffect);
259 LOAD_PROC(LPALEFFECTI, alEffecti);
260 LOAD_PROC(LPALEFFECTIV, alEffectiv);
261 LOAD_PROC(LPALEFFECTF, alEffectf);
262 LOAD_PROC(LPALEFFECTFV, alEffectfv);
263 LOAD_PROC(LPALGETEFFECTI, alGetEffecti);
264 LOAD_PROC(LPALGETEFFECTIV, alGetEffectiv);
265 LOAD_PROC(LPALGETEFFECTF, alGetEffectf);
266 LOAD_PROC(LPALGETEFFECTFV, alGetEffectfv);
268 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS, alGenAuxiliaryEffectSlots);
269 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS, alDeleteAuxiliaryEffectSlots);
270 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT, alIsAuxiliaryEffectSlot);
271 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI, alAuxiliaryEffectSloti);
272 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV, alAuxiliaryEffectSlotiv);
273 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF, alAuxiliaryEffectSlotf);
274 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV, alAuxiliaryEffectSlotfv);
275 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI, alGetAuxiliaryEffectSloti);
276 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV, alGetAuxiliaryEffectSlotiv);
277 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF, alGetAuxiliaryEffectSlotf);
278 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
279 #undef LOAD_PROC
281 /* Initialize SDL_sound. */
282 Sound_Init();
284 /* Load the sound into a buffer. */
285 buffer = LoadSound(argv[0]);
286 if(!buffer)
288 CloseAL();
289 Sound_Quit();
290 return 1;
293 /* Load the reverb into an effect. */
294 effect = LoadEffect(&reverb);
295 if(!effect)
297 alDeleteBuffers(1, &buffer);
298 Sound_Quit();
299 CloseAL();
300 return 1;
303 /* Create the effect slot object. This is what "plays" an effect on sources
304 * that connect to it. */
305 slot = 0;
306 alGenAuxiliaryEffectSlots(1, &slot);
308 /* Tell the effect slot to use the loaded effect object. Note that the this
309 * effectively copies the effect properties. You can modify or delete the
310 * effect object afterward without affecting the effect slot.
312 alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, (ALint)effect);
313 assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
315 /* Create the source to play the sound with. */
316 source = 0;
317 alGenSources(1, &source);
318 alSourcei(source, AL_BUFFER, (ALint)buffer);
320 /* Connect the source to the effect slot. This tells the source to use the
321 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
323 alSource3i(source, AL_AUXILIARY_SEND_FILTER, (ALint)slot, 0, AL_FILTER_NULL);
324 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
326 /* Play the sound until it finishes. */
327 alSourcePlay(source);
328 do {
329 al_nssleep(10000000);
330 alGetSourcei(source, AL_SOURCE_STATE, &state);
331 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
333 /* All done. Delete resources, and close down SDL_sound and OpenAL. */
334 alDeleteSources(1, &source);
335 alDeleteAuxiliaryEffectSlots(1, &slot);
336 alDeleteEffects(1, &effect);
337 alDeleteBuffers(1, &buffer);
339 Sound_Quit();
340 CloseAL();
342 return 0;