Remove unused atomic_invflag
[openal-soft.git] / examples / alreverb.c
blob56acdd826cadc15f3eab4cddd84d918adacb0e0e
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 <assert.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <stdio.h>
31 #include <stdlib.h>
33 #include "sndfile.h"
35 #include "AL/al.h"
36 #include "AL/alc.h"
37 #include "AL/alext.h"
38 #include "AL/efx.h"
39 #include "AL/efx-presets.h"
41 #include "common/alhelpers.h"
44 /* Effect object functions */
45 static LPALGENEFFECTS alGenEffects;
46 static LPALDELETEEFFECTS alDeleteEffects;
47 static LPALISEFFECT alIsEffect;
48 static LPALEFFECTI alEffecti;
49 static LPALEFFECTIV alEffectiv;
50 static LPALEFFECTF alEffectf;
51 static LPALEFFECTFV alEffectfv;
52 static LPALGETEFFECTI alGetEffecti;
53 static LPALGETEFFECTIV alGetEffectiv;
54 static LPALGETEFFECTF alGetEffectf;
55 static LPALGETEFFECTFV alGetEffectfv;
57 /* Auxiliary Effect Slot object functions */
58 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots;
59 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots;
60 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot;
61 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti;
62 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv;
63 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf;
64 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv;
65 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti;
66 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv;
67 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf;
68 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
71 /* LoadEffect loads the given reverb properties into a new OpenAL effect
72 * object, and returns the new effect ID. */
73 static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
75 ALuint effect = 0;
76 ALenum err;
78 /* Create the effect object and check if we can do EAX reverb. */
79 alGenEffects(1, &effect);
80 if(alGetEnumValue("AL_EFFECT_EAXREVERB") != 0)
82 printf("Using EAX Reverb\n");
84 /* EAX Reverb is available. Set the EAX effect type then load the
85 * reverb properties. */
86 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
88 alEffectf(effect, AL_EAXREVERB_DENSITY, reverb->flDensity);
89 alEffectf(effect, AL_EAXREVERB_DIFFUSION, reverb->flDiffusion);
90 alEffectf(effect, AL_EAXREVERB_GAIN, reverb->flGain);
91 alEffectf(effect, AL_EAXREVERB_GAINHF, reverb->flGainHF);
92 alEffectf(effect, AL_EAXREVERB_GAINLF, reverb->flGainLF);
93 alEffectf(effect, AL_EAXREVERB_DECAY_TIME, reverb->flDecayTime);
94 alEffectf(effect, AL_EAXREVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
95 alEffectf(effect, AL_EAXREVERB_DECAY_LFRATIO, reverb->flDecayLFRatio);
96 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
97 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
98 alEffectfv(effect, AL_EAXREVERB_REFLECTIONS_PAN, reverb->flReflectionsPan);
99 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
100 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
101 alEffectfv(effect, AL_EAXREVERB_LATE_REVERB_PAN, reverb->flLateReverbPan);
102 alEffectf(effect, AL_EAXREVERB_ECHO_TIME, reverb->flEchoTime);
103 alEffectf(effect, AL_EAXREVERB_ECHO_DEPTH, reverb->flEchoDepth);
104 alEffectf(effect, AL_EAXREVERB_MODULATION_TIME, reverb->flModulationTime);
105 alEffectf(effect, AL_EAXREVERB_MODULATION_DEPTH, reverb->flModulationDepth);
106 alEffectf(effect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
107 alEffectf(effect, AL_EAXREVERB_HFREFERENCE, reverb->flHFReference);
108 alEffectf(effect, AL_EAXREVERB_LFREFERENCE, reverb->flLFReference);
109 alEffectf(effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
110 alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
112 else
114 printf("Using Standard Reverb\n");
116 /* No EAX Reverb. Set the standard reverb effect type then load the
117 * available reverb properties. */
118 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
120 alEffectf(effect, AL_REVERB_DENSITY, reverb->flDensity);
121 alEffectf(effect, AL_REVERB_DIFFUSION, reverb->flDiffusion);
122 alEffectf(effect, AL_REVERB_GAIN, reverb->flGain);
123 alEffectf(effect, AL_REVERB_GAINHF, reverb->flGainHF);
124 alEffectf(effect, AL_REVERB_DECAY_TIME, reverb->flDecayTime);
125 alEffectf(effect, AL_REVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
126 alEffectf(effect, AL_REVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
127 alEffectf(effect, AL_REVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
128 alEffectf(effect, AL_REVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
129 alEffectf(effect, AL_REVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
130 alEffectf(effect, AL_REVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
131 alEffectf(effect, AL_REVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
132 alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
135 /* Check if an error occured, and clean up if so. */
136 err = alGetError();
137 if(err != AL_NO_ERROR)
139 fprintf(stderr, "OpenAL error: %s\n", alGetString(err));
140 if(alIsEffect(effect))
141 alDeleteEffects(1, &effect);
142 return 0;
145 return effect;
149 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
150 * returns the new buffer ID.
152 static ALuint LoadSound(const char *filename)
154 ALenum err, format;
155 ALuint buffer;
156 SNDFILE *sndfile;
157 SF_INFO sfinfo;
158 short *membuf;
159 sf_count_t num_frames;
160 ALsizei num_bytes;
162 /* Open the audio file and check that it's usable. */
163 sndfile = sf_open(filename, SFM_READ, &sfinfo);
164 if(!sndfile)
166 fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
167 return 0;
169 if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
171 fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
172 sf_close(sndfile);
173 return 0;
176 /* Get the sound format, and figure out the OpenAL format */
177 format = AL_NONE;
178 if(sfinfo.channels == 1)
179 format = AL_FORMAT_MONO16;
180 else if(sfinfo.channels == 2)
181 format = AL_FORMAT_STEREO16;
182 else if(sfinfo.channels == 3)
184 if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
185 format = AL_FORMAT_BFORMAT2D_16;
187 else if(sfinfo.channels == 4)
189 if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
190 format = AL_FORMAT_BFORMAT3D_16;
192 if(!format)
194 fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
195 sf_close(sndfile);
196 return 0;
199 /* Decode the whole audio file to a buffer. */
200 membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
202 num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
203 if(num_frames < 1)
205 free(membuf);
206 sf_close(sndfile);
207 fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
208 return 0;
210 num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
212 /* Buffer the audio data into a new buffer object, then free the data and
213 * close the file.
215 buffer = 0;
216 alGenBuffers(1, &buffer);
217 alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
219 free(membuf);
220 sf_close(sndfile);
222 /* Check if an error occured, and clean up if so. */
223 err = alGetError();
224 if(err != AL_NO_ERROR)
226 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
227 if(buffer && alIsBuffer(buffer))
228 alDeleteBuffers(1, &buffer);
229 return 0;
232 return buffer;
236 int main(int argc, char **argv)
238 EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
239 ALuint source, buffer, effect, slot;
240 ALenum state;
242 /* Print out usage if no arguments were specified */
243 if(argc < 2)
245 fprintf(stderr, "Usage: %s [-device <name] <filename>\n", argv[0]);
246 return 1;
249 /* Initialize OpenAL, and check for EFX support. */
250 argv++; argc--;
251 if(InitAL(&argv, &argc) != 0)
252 return 1;
254 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
256 fprintf(stderr, "Error: EFX not supported\n");
257 CloseAL();
258 return 1;
261 /* Define a macro to help load the function pointers. */
262 #define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x))
263 LOAD_PROC(LPALGENEFFECTS, alGenEffects);
264 LOAD_PROC(LPALDELETEEFFECTS, alDeleteEffects);
265 LOAD_PROC(LPALISEFFECT, alIsEffect);
266 LOAD_PROC(LPALEFFECTI, alEffecti);
267 LOAD_PROC(LPALEFFECTIV, alEffectiv);
268 LOAD_PROC(LPALEFFECTF, alEffectf);
269 LOAD_PROC(LPALEFFECTFV, alEffectfv);
270 LOAD_PROC(LPALGETEFFECTI, alGetEffecti);
271 LOAD_PROC(LPALGETEFFECTIV, alGetEffectiv);
272 LOAD_PROC(LPALGETEFFECTF, alGetEffectf);
273 LOAD_PROC(LPALGETEFFECTFV, alGetEffectfv);
275 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS, alGenAuxiliaryEffectSlots);
276 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS, alDeleteAuxiliaryEffectSlots);
277 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT, alIsAuxiliaryEffectSlot);
278 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI, alAuxiliaryEffectSloti);
279 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV, alAuxiliaryEffectSlotiv);
280 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF, alAuxiliaryEffectSlotf);
281 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV, alAuxiliaryEffectSlotfv);
282 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI, alGetAuxiliaryEffectSloti);
283 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV, alGetAuxiliaryEffectSlotiv);
284 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF, alGetAuxiliaryEffectSlotf);
285 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
286 #undef LOAD_PROC
288 /* Load the sound into a buffer. */
289 buffer = LoadSound(argv[0]);
290 if(!buffer)
292 CloseAL();
293 return 1;
296 /* Load the reverb into an effect. */
297 effect = LoadEffect(&reverb);
298 if(!effect)
300 alDeleteBuffers(1, &buffer);
301 CloseAL();
302 return 1;
305 /* Create the effect slot object. This is what "plays" an effect on sources
306 * that connect to it. */
307 slot = 0;
308 alGenAuxiliaryEffectSlots(1, &slot);
310 /* Tell the effect slot to use the loaded effect object. Note that the this
311 * effectively copies the effect properties. You can modify or delete the
312 * effect object afterward without affecting the effect slot.
314 alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, (ALint)effect);
315 assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
317 /* Create the source to play the sound with. */
318 source = 0;
319 alGenSources(1, &source);
320 alSourcei(source, AL_BUFFER, (ALint)buffer);
322 /* Connect the source to the effect slot. This tells the source to use the
323 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
325 alSource3i(source, AL_AUXILIARY_SEND_FILTER, (ALint)slot, 0, AL_FILTER_NULL);
326 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
328 /* Play the sound until it finishes. */
329 alSourcePlay(source);
330 do {
331 al_nssleep(10000000);
332 alGetSourcei(source, AL_SOURCE_STATE, &state);
333 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
335 /* All done. Delete resources, and close down OpenAL. */
336 alDeleteSources(1, &source);
337 alDeleteAuxiliaryEffectSlots(1, &slot);
338 alDeleteEffects(1, &effect);
339 alDeleteBuffers(1, &buffer);
341 CloseAL();
343 return 0;