Add tracing capabilities to the router
[openal-soft.git] / examples / alreverb.c
blobe6c9e606f98f85e488916b5cd638a31dbb741e52
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>
32 #include "AL/al.h"
33 #include "AL/alc.h"
34 #include "AL/alext.h"
35 #include "AL/efx-presets.h"
37 #include "common/alhelpers.h"
40 /* Effect object functions */
41 static LPALGENEFFECTS alGenEffects;
42 static LPALDELETEEFFECTS alDeleteEffects;
43 static LPALISEFFECT alIsEffect;
44 static LPALEFFECTI alEffecti;
45 static LPALEFFECTIV alEffectiv;
46 static LPALEFFECTF alEffectf;
47 static LPALEFFECTFV alEffectfv;
48 static LPALGETEFFECTI alGetEffecti;
49 static LPALGETEFFECTIV alGetEffectiv;
50 static LPALGETEFFECTF alGetEffectf;
51 static LPALGETEFFECTFV alGetEffectfv;
53 /* Auxiliary Effect Slot object functions */
54 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots;
55 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots;
56 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot;
57 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti;
58 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv;
59 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf;
60 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv;
61 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti;
62 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv;
63 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf;
64 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
67 /* LoadEffect loads the given reverb properties into a new OpenAL effect
68 * object, and returns the new effect ID. */
69 static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
71 ALuint effect = 0;
72 ALenum err;
74 /* Create the effect object and check if we can do EAX reverb. */
75 alGenEffects(1, &effect);
76 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
78 printf("Using EAX Reverb\n");
80 /* EAX Reverb is available. Set the EAX effect type then load the
81 * reverb properties. */
82 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
84 alEffectf(effect, AL_EAXREVERB_DENSITY, reverb->flDensity);
85 alEffectf(effect, AL_EAXREVERB_DIFFUSION, reverb->flDiffusion);
86 alEffectf(effect, AL_EAXREVERB_GAIN, reverb->flGain);
87 alEffectf(effect, AL_EAXREVERB_GAINHF, reverb->flGainHF);
88 alEffectf(effect, AL_EAXREVERB_GAINLF, reverb->flGainLF);
89 alEffectf(effect, AL_EAXREVERB_DECAY_TIME, reverb->flDecayTime);
90 alEffectf(effect, AL_EAXREVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
91 alEffectf(effect, AL_EAXREVERB_DECAY_LFRATIO, reverb->flDecayLFRatio);
92 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
93 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
94 alEffectfv(effect, AL_EAXREVERB_REFLECTIONS_PAN, reverb->flReflectionsPan);
95 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
96 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
97 alEffectfv(effect, AL_EAXREVERB_LATE_REVERB_PAN, reverb->flLateReverbPan);
98 alEffectf(effect, AL_EAXREVERB_ECHO_TIME, reverb->flEchoTime);
99 alEffectf(effect, AL_EAXREVERB_ECHO_DEPTH, reverb->flEchoDepth);
100 alEffectf(effect, AL_EAXREVERB_MODULATION_TIME, reverb->flModulationTime);
101 alEffectf(effect, AL_EAXREVERB_MODULATION_DEPTH, reverb->flModulationDepth);
102 alEffectf(effect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
103 alEffectf(effect, AL_EAXREVERB_HFREFERENCE, reverb->flHFReference);
104 alEffectf(effect, AL_EAXREVERB_LFREFERENCE, reverb->flLFReference);
105 alEffectf(effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
106 alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
108 else
110 printf("Using Standard Reverb\n");
112 /* No EAX Reverb. Set the standard reverb effect type then load the
113 * available reverb properties. */
114 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
116 alEffectf(effect, AL_REVERB_DENSITY, reverb->flDensity);
117 alEffectf(effect, AL_REVERB_DIFFUSION, reverb->flDiffusion);
118 alEffectf(effect, AL_REVERB_GAIN, reverb->flGain);
119 alEffectf(effect, AL_REVERB_GAINHF, reverb->flGainHF);
120 alEffectf(effect, AL_REVERB_DECAY_TIME, reverb->flDecayTime);
121 alEffectf(effect, AL_REVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
122 alEffectf(effect, AL_REVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
123 alEffectf(effect, AL_REVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
124 alEffectf(effect, AL_REVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
125 alEffectf(effect, AL_REVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
126 alEffectf(effect, AL_REVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
127 alEffectf(effect, AL_REVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
128 alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
131 /* Check if an error occured, and clean up if so. */
132 err = alGetError();
133 if(err != AL_NO_ERROR)
135 fprintf(stderr, "OpenAL error: %s\n", alGetString(err));
136 if(alIsEffect(effect))
137 alDeleteEffects(1, &effect);
138 return 0;
141 return effect;
145 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
146 * returns the new buffer ID.
148 static ALuint LoadSound(const char *filename)
150 Sound_Sample *sample;
151 ALenum err, format;
152 ALuint buffer;
153 Uint32 slen;
155 /* Open the audio file */
156 sample = Sound_NewSampleFromFile(filename, NULL, 65536);
157 if(!sample)
159 fprintf(stderr, "Could not open audio in %s\n", filename);
160 return 0;
163 /* Get the sound format, and figure out the OpenAL format */
164 if(sample->actual.channels == 1)
166 if(sample->actual.format == AUDIO_U8)
167 format = AL_FORMAT_MONO8;
168 else if(sample->actual.format == AUDIO_S16SYS)
169 format = AL_FORMAT_MONO16;
170 else
172 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
173 Sound_FreeSample(sample);
174 return 0;
177 else if(sample->actual.channels == 2)
179 if(sample->actual.format == AUDIO_U8)
180 format = AL_FORMAT_STEREO8;
181 else if(sample->actual.format == AUDIO_S16SYS)
182 format = AL_FORMAT_STEREO16;
183 else
185 fprintf(stderr, "Unsupported sample format: 0x%04x\n", sample->actual.format);
186 Sound_FreeSample(sample);
187 return 0;
190 else
192 fprintf(stderr, "Unsupported channel count: %d\n", sample->actual.channels);
193 Sound_FreeSample(sample);
194 return 0;
197 /* Decode the whole audio stream to a buffer. */
198 slen = Sound_DecodeAll(sample);
199 if(!sample->buffer || slen == 0)
201 fprintf(stderr, "Failed to read audio from %s\n", filename);
202 Sound_FreeSample(sample);
203 return 0;
206 /* Buffer the audio data into a new buffer object, then free the data and
207 * close the file. */
208 buffer = 0;
209 alGenBuffers(1, &buffer);
210 alBufferData(buffer, format, sample->buffer, slen, sample->actual.rate);
211 Sound_FreeSample(sample);
213 /* Check if an error occured, and clean up if so. */
214 err = alGetError();
215 if(err != AL_NO_ERROR)
217 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
218 if(buffer && alIsBuffer(buffer))
219 alDeleteBuffers(1, &buffer);
220 return 0;
223 return buffer;
227 int main(int argc, char **argv)
229 EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
230 ALuint source, buffer, effect, slot;
231 ALenum state;
233 /* Print out usage if no arguments were specified */
234 if(argc < 2)
236 fprintf(stderr, "Usage: %s [-device <name] <filename>\n", argv[0]);
237 return 1;
240 /* Initialize OpenAL, and check for EFX support. */
241 argv++; argc--;
242 if(InitAL(&argv, &argc) != 0)
243 return 1;
245 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
247 fprintf(stderr, "Error: EFX not supported\n");
248 CloseAL();
249 return 1;
252 /* Define a macro to help load the function pointers. */
253 #define LOAD_PROC(x) ((x) = alGetProcAddress(#x))
254 LOAD_PROC(alGenEffects);
255 LOAD_PROC(alDeleteEffects);
256 LOAD_PROC(alIsEffect);
257 LOAD_PROC(alEffecti);
258 LOAD_PROC(alEffectiv);
259 LOAD_PROC(alEffectf);
260 LOAD_PROC(alEffectfv);
261 LOAD_PROC(alGetEffecti);
262 LOAD_PROC(alGetEffectiv);
263 LOAD_PROC(alGetEffectf);
264 LOAD_PROC(alGetEffectfv);
266 LOAD_PROC(alGenAuxiliaryEffectSlots);
267 LOAD_PROC(alDeleteAuxiliaryEffectSlots);
268 LOAD_PROC(alIsAuxiliaryEffectSlot);
269 LOAD_PROC(alAuxiliaryEffectSloti);
270 LOAD_PROC(alAuxiliaryEffectSlotiv);
271 LOAD_PROC(alAuxiliaryEffectSlotf);
272 LOAD_PROC(alAuxiliaryEffectSlotfv);
273 LOAD_PROC(alGetAuxiliaryEffectSloti);
274 LOAD_PROC(alGetAuxiliaryEffectSlotiv);
275 LOAD_PROC(alGetAuxiliaryEffectSlotf);
276 LOAD_PROC(alGetAuxiliaryEffectSlotfv);
277 #undef LOAD_PROC
279 /* Initialize SDL_sound. */
280 Sound_Init();
282 /* Load the sound into a buffer. */
283 buffer = LoadSound(argv[0]);
284 if(!buffer)
286 CloseAL();
287 Sound_Quit();
288 return 1;
291 /* Load the reverb into an effect. */
292 effect = LoadEffect(&reverb);
293 if(!effect)
295 alDeleteBuffers(1, &buffer);
296 Sound_Quit();
297 CloseAL();
298 return 1;
301 /* Create the effect slot object. This is what "plays" an effect on sources
302 * that connect to it. */
303 slot = 0;
304 alGenAuxiliaryEffectSlots(1, &slot);
306 /* Tell the effect slot to use the loaded effect object. Note that the this
307 * effectively copies the effect properties. You can modify or delete the
308 * effect object afterward without affecting the effect slot.
310 alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, effect);
311 assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
313 /* Create the source to play the sound with. */
314 source = 0;
315 alGenSources(1, &source);
316 alSourcei(source, AL_BUFFER, buffer);
318 /* Connect the source to the effect slot. This tells the source to use the
319 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
321 alSource3i(source, AL_AUXILIARY_SEND_FILTER, slot, 0, AL_FILTER_NULL);
322 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
324 /* Play the sound until it finishes. */
325 alSourcePlay(source);
326 do {
327 al_nssleep(10000000);
328 alGetSourcei(source, AL_SOURCE_STATE, &state);
329 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
331 /* All done. Delete resources, and close down SDL_sound and OpenAL. */
332 alDeleteSources(1, &source);
333 alDeleteAuxiliaryEffectSlots(1, &slot);
334 alDeleteEffects(1, &effect);
335 alDeleteBuffers(1, &buffer);
337 Sound_Quit();
338 CloseAL();
340 return 0;