Avoid duplicating code using a macro
[openal-soft.git] / examples / alreverb.c
blobec71f3549407f0f0f264c81122e30c7bfd5af093
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 arguments were specified */
221 if(argc < 2)
223 fprintf(stderr, "Usage: %s [-device <name] <filename>\n", argv[0]);
224 return 1;
227 /* Initialize OpenAL, and check for EFX support. */
228 argv++; argc--;
229 if(InitAL(&argv, &argc) != 0)
230 return 1;
232 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
234 fprintf(stderr, "Error: EFX not supported\n");
235 CloseAL();
236 return 1;
239 /* Define a macro to help load the function pointers. */
240 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
241 LOAD_PROC(alGenEffects);
242 LOAD_PROC(alDeleteEffects);
243 LOAD_PROC(alIsEffect);
244 LOAD_PROC(alEffecti);
245 LOAD_PROC(alEffectiv);
246 LOAD_PROC(alEffectf);
247 LOAD_PROC(alEffectfv);
248 LOAD_PROC(alGetEffecti);
249 LOAD_PROC(alGetEffectiv);
250 LOAD_PROC(alGetEffectf);
251 LOAD_PROC(alGetEffectfv);
253 LOAD_PROC(alGenAuxiliaryEffectSlots);
254 LOAD_PROC(alDeleteAuxiliaryEffectSlots);
255 LOAD_PROC(alIsAuxiliaryEffectSlot);
256 LOAD_PROC(alAuxiliaryEffectSloti);
257 LOAD_PROC(alAuxiliaryEffectSlotiv);
258 LOAD_PROC(alAuxiliaryEffectSlotf);
259 LOAD_PROC(alAuxiliaryEffectSlotfv);
260 LOAD_PROC(alGetAuxiliaryEffectSloti);
261 LOAD_PROC(alGetAuxiliaryEffectSlotiv);
262 LOAD_PROC(alGetAuxiliaryEffectSlotf);
263 LOAD_PROC(alGetAuxiliaryEffectSlotfv);
265 if(alIsExtensionPresent("AL_SOFT_buffer_samples"))
267 LOAD_PROC(alBufferSamplesSOFT);
268 LOAD_PROC(alIsBufferFormatSupportedSOFT);
270 #undef LOAD_PROC
272 /* Load the sound into a buffer. */
273 buffer = LoadSound(argv[0]);
274 if(!buffer)
276 CloseAL();
277 return 1;
280 /* Load the reverb into an effect. */
281 effect = LoadEffect(&reverb);
282 if(!effect)
284 alDeleteBuffers(1, &buffer);
285 CloseAL();
286 return 1;
289 /* Create the effect slot object. This is what "plays" an effect on sources
290 * that connect to it. */
291 slot = 0;
292 alGenAuxiliaryEffectSlots(1, &slot);
294 /* Tell the effect slot to use the loaded effect object. Note that the this
295 * effectively copies the effect properties. You can modify or delete the
296 * effect object afterward without affecting the effect slot.
298 alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, effect);
299 assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
301 /* Create the source to play the sound with. */
302 source = 0;
303 alGenSources(1, &source);
304 alSourcei(source, AL_BUFFER, buffer);
306 /* Connect the source to the effect slot. This tells the source to use the
307 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
309 alSource3i(source, AL_AUXILIARY_SEND_FILTER, slot, 0, AL_FILTER_NULL);
310 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
312 /* Play the sound until it finishes. */
313 alSourcePlay(source);
314 do {
315 al_nssleep(10000000);
316 alGetSourcei(source, AL_SOURCE_STATE, &state);
317 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
319 /* All done. Delete resources, and close OpenAL. */
320 alDeleteSources(1, &source);
321 alDeleteAuxiliaryEffectSlots(1, &slot);
322 alDeleteEffects(1, &effect);
323 alDeleteBuffers(1, &buffer);
325 CloseAL();
327 return 0;