Partially handle the irom sub-chunk
[openal-soft.git] / examples / alreverb.c
blob420b1c5520c2023c75e164240413f805d07f152b
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 "AL/al.h"
31 #include "AL/alc.h"
32 #include "AL/alext.h"
33 #include "AL/efx-presets.h"
35 #include "common/alhelpers.h"
36 #include "common/sdl_sound.h"
39 static LPALBUFFERSAMPLESSOFT alBufferSamplesSOFT = wrap_BufferSamples;
40 static LPALISBUFFERFORMATSUPPORTEDSOFT alIsBufferFormatSupportedSOFT;
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. */
149 static ALuint LoadSound(const char *filename)
151 ALenum err, format, type, channels;
152 ALuint rate, buffer;
153 size_t datalen;
154 void *data;
155 FilePtr sound;
157 /* Open the file and get the first stream from it */
158 sound = openAudioFile(filename, 1000);
159 if(!sound)
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(getAudioInfo(sound, &rate, &channels, &type) != 0)
168 fprintf(stderr, "Error getting audio info for %s\n", filename);
169 closeAudioFile(sound);
170 return 0;
173 format = GetFormat(channels, type, alIsBufferFormatSupportedSOFT);
174 if(format == AL_NONE)
176 fprintf(stderr, "Unsupported format (%s, %s) for %s\n",
177 ChannelsName(channels), TypeName(type), filename);
178 closeAudioFile(sound);
179 return 0;
182 /* Decode the whole audio stream to a buffer. */
183 data = decodeAudioStream(sound, &datalen);
184 if(!data)
186 fprintf(stderr, "Failed to read audio from %s\n", filename);
187 closeAudioFile(sound);
188 return 0;
191 /* Buffer the audio data into a new buffer object, then free the data and
192 * close the file. */
193 buffer = 0;
194 alGenBuffers(1, &buffer);
195 alBufferSamplesSOFT(buffer, rate, format, BytesToFrames(datalen, channels, type),
196 channels, type, data);
197 free(data);
198 closeAudioFile(sound);
200 /* Check if an error occured, and clean up if so. */
201 err = alGetError();
202 if(err != AL_NO_ERROR)
204 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
205 if(alIsBuffer(buffer))
206 alDeleteBuffers(1, &buffer);
207 return 0;
210 return buffer;
214 int main(int argc, char **argv)
216 EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
217 ALuint source, buffer, effect, slot;
218 ALenum state;
220 /* Print out usage if no file was specified */
221 if(argc < 2)
223 fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
224 return 1;
227 /* Initialize OpenAL with the default device, and check for EFX support. */
228 if(InitAL() != 0)
229 return 1;
231 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
233 fprintf(stderr, "Error: EFX not supported\n");
234 CloseAL();
235 return 1;
238 /* Define a macro to help load the function pointers. */
239 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
240 LOAD_PROC(alGenEffects);
241 LOAD_PROC(alDeleteEffects);
242 LOAD_PROC(alIsEffect);
243 LOAD_PROC(alEffecti);
244 LOAD_PROC(alEffectiv);
245 LOAD_PROC(alEffectf);
246 LOAD_PROC(alEffectfv);
247 LOAD_PROC(alGetEffecti);
248 LOAD_PROC(alGetEffectiv);
249 LOAD_PROC(alGetEffectf);
250 LOAD_PROC(alGetEffectfv);
252 LOAD_PROC(alGenAuxiliaryEffectSlots);
253 LOAD_PROC(alDeleteAuxiliaryEffectSlots);
254 LOAD_PROC(alIsAuxiliaryEffectSlot);
255 LOAD_PROC(alAuxiliaryEffectSloti);
256 LOAD_PROC(alAuxiliaryEffectSlotiv);
257 LOAD_PROC(alAuxiliaryEffectSlotf);
258 LOAD_PROC(alAuxiliaryEffectSlotfv);
259 LOAD_PROC(alGetAuxiliaryEffectSloti);
260 LOAD_PROC(alGetAuxiliaryEffectSlotiv);
261 LOAD_PROC(alGetAuxiliaryEffectSlotf);
262 LOAD_PROC(alGetAuxiliaryEffectSlotfv);
264 if(alIsExtensionPresent("AL_SOFT_buffer_samples"))
266 LOAD_PROC(alBufferSamplesSOFT);
267 LOAD_PROC(alIsBufferFormatSupportedSOFT);
269 #undef LOAD_PROC
271 /* Load the sound into a buffer. */
272 buffer = LoadSound(argv[1]);
273 if(!buffer)
275 CloseAL();
276 return 1;
279 /* Load the reverb into an effect. */
280 effect = LoadEffect(&reverb);
281 if(!effect)
283 alDeleteBuffers(1, &buffer);
284 CloseAL();
285 return 1;
288 /* Create the effect slot object. This is what "plays" an effect on sources
289 * that connect to it. */
290 slot = 0;
291 alGenAuxiliaryEffectSlots(1, &slot);
293 /* Tell the effect slot to use the loaded effect object. Note that the this
294 * effectively copies the effect properties. You can modify or delete the
295 * effect object afterward without affecting the effect slot.
297 alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, effect);
298 assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
300 /* Create the source to play the sound with. */
301 source = 0;
302 alGenSources(1, &source);
303 alSourcei(source, AL_BUFFER, buffer);
305 /* Connect the source to the effect slot. This tells the source to use the
306 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
308 alSource3i(source, AL_AUXILIARY_SEND_FILTER, slot, 0, AL_FILTER_NULL);
309 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
311 /* Play the sound until it finishes. */
312 alSourcePlay(source);
313 do {
314 Sleep(10);
315 alGetSourcei(source, AL_SOURCE_STATE, &state);
316 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
318 /* All done. Delete resources, and close OpenAL. */
319 alDeleteSources(1, &source);
320 alDeleteAuxiliaryEffectSlots(1, &slot);
321 alDeleteEffects(1, &effect);
322 alDeleteBuffers(1, &buffer);
324 CloseAL();
326 return 0;