Don't allocate extraneous memory for dynamic arrays
[openal-soft.git] / examples / alreverb.c
blob5f68fe3a1890e454f697bee0a7e7d2eb1030f2cd
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"
43 #include "win_main_utf8.h"
46 /* Effect object functions */
47 static LPALGENEFFECTS alGenEffects;
48 static LPALDELETEEFFECTS alDeleteEffects;
49 static LPALISEFFECT alIsEffect;
50 static LPALEFFECTI alEffecti;
51 static LPALEFFECTIV alEffectiv;
52 static LPALEFFECTF alEffectf;
53 static LPALEFFECTFV alEffectfv;
54 static LPALGETEFFECTI alGetEffecti;
55 static LPALGETEFFECTIV alGetEffectiv;
56 static LPALGETEFFECTF alGetEffectf;
57 static LPALGETEFFECTFV alGetEffectfv;
59 /* Auxiliary Effect Slot object functions */
60 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots;
61 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots;
62 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot;
63 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti;
64 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv;
65 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf;
66 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv;
67 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti;
68 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv;
69 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf;
70 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv;
73 /* LoadEffect loads the given reverb properties into a new OpenAL effect
74 * object, and returns the new effect ID. */
75 static ALuint LoadEffect(const EFXEAXREVERBPROPERTIES *reverb)
77 ALuint effect = 0;
78 ALenum err;
80 /* Clear error state. */
81 alGetError();
83 /* Create the effect object and check if we can do EAX reverb. */
84 alGenEffects(1, &effect);
85 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_EAXREVERB);
86 err = alGetError();
87 if(err == AL_NO_ERROR)
89 printf("Using EAX Reverb\n");
91 alEffectf(effect, AL_EAXREVERB_DENSITY, reverb->flDensity);
92 alEffectf(effect, AL_EAXREVERB_DIFFUSION, reverb->flDiffusion);
93 alEffectf(effect, AL_EAXREVERB_GAIN, reverb->flGain);
94 alEffectf(effect, AL_EAXREVERB_GAINHF, reverb->flGainHF);
95 alEffectf(effect, AL_EAXREVERB_GAINLF, reverb->flGainLF);
96 alEffectf(effect, AL_EAXREVERB_DECAY_TIME, reverb->flDecayTime);
97 alEffectf(effect, AL_EAXREVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
98 alEffectf(effect, AL_EAXREVERB_DECAY_LFRATIO, reverb->flDecayLFRatio);
99 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
100 alEffectf(effect, AL_EAXREVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
101 alEffectfv(effect, AL_EAXREVERB_REFLECTIONS_PAN, reverb->flReflectionsPan);
102 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
103 alEffectf(effect, AL_EAXREVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
104 alEffectfv(effect, AL_EAXREVERB_LATE_REVERB_PAN, reverb->flLateReverbPan);
105 alEffectf(effect, AL_EAXREVERB_ECHO_TIME, reverb->flEchoTime);
106 alEffectf(effect, AL_EAXREVERB_ECHO_DEPTH, reverb->flEchoDepth);
107 alEffectf(effect, AL_EAXREVERB_MODULATION_TIME, reverb->flModulationTime);
108 alEffectf(effect, AL_EAXREVERB_MODULATION_DEPTH, reverb->flModulationDepth);
109 alEffectf(effect, AL_EAXREVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
110 alEffectf(effect, AL_EAXREVERB_HFREFERENCE, reverb->flHFReference);
111 alEffectf(effect, AL_EAXREVERB_LFREFERENCE, reverb->flLFReference);
112 alEffectf(effect, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
113 alEffecti(effect, AL_EAXREVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
115 else
117 printf("Using Standard Reverb\n");
119 /* No EAX Reverb. Set the standard reverb effect type then load the
120 * available reverb properties.
122 alEffecti(effect, AL_EFFECT_TYPE, AL_EFFECT_REVERB);
124 alEffectf(effect, AL_REVERB_DENSITY, reverb->flDensity);
125 alEffectf(effect, AL_REVERB_DIFFUSION, reverb->flDiffusion);
126 alEffectf(effect, AL_REVERB_GAIN, reverb->flGain);
127 alEffectf(effect, AL_REVERB_GAINHF, reverb->flGainHF);
128 alEffectf(effect, AL_REVERB_DECAY_TIME, reverb->flDecayTime);
129 alEffectf(effect, AL_REVERB_DECAY_HFRATIO, reverb->flDecayHFRatio);
130 alEffectf(effect, AL_REVERB_REFLECTIONS_GAIN, reverb->flReflectionsGain);
131 alEffectf(effect, AL_REVERB_REFLECTIONS_DELAY, reverb->flReflectionsDelay);
132 alEffectf(effect, AL_REVERB_LATE_REVERB_GAIN, reverb->flLateReverbGain);
133 alEffectf(effect, AL_REVERB_LATE_REVERB_DELAY, reverb->flLateReverbDelay);
134 alEffectf(effect, AL_REVERB_AIR_ABSORPTION_GAINHF, reverb->flAirAbsorptionGainHF);
135 alEffectf(effect, AL_REVERB_ROOM_ROLLOFF_FACTOR, reverb->flRoomRolloffFactor);
136 alEffecti(effect, AL_REVERB_DECAY_HFLIMIT, reverb->iDecayHFLimit);
139 /* Check if an error occurred, and clean up if so. */
140 err = alGetError();
141 if(err != AL_NO_ERROR)
143 fprintf(stderr, "OpenAL error: %s\n", alGetString(err));
144 if(alIsEffect(effect))
145 alDeleteEffects(1, &effect);
146 return 0;
149 return effect;
153 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
154 * returns the new buffer ID.
156 static ALuint LoadSound(const char *filename)
158 ALenum err, format;
159 ALuint buffer;
160 SNDFILE *sndfile;
161 SF_INFO sfinfo;
162 short *membuf;
163 sf_count_t num_frames;
164 ALsizei num_bytes;
166 /* Open the audio file and check that it's usable. */
167 sndfile = sf_open(filename, SFM_READ, &sfinfo);
168 if(!sndfile)
170 fprintf(stderr, "Could not open audio in %s: %s\n", filename, sf_strerror(sndfile));
171 return 0;
173 if(sfinfo.frames < 1 || sfinfo.frames > (sf_count_t)(INT_MAX/sizeof(short))/sfinfo.channels)
175 fprintf(stderr, "Bad sample count in %s (%" PRId64 ")\n", filename, sfinfo.frames);
176 sf_close(sndfile);
177 return 0;
180 /* Get the sound format, and figure out the OpenAL format */
181 format = AL_NONE;
182 if(sfinfo.channels == 1)
183 format = AL_FORMAT_MONO16;
184 else if(sfinfo.channels == 2)
185 format = AL_FORMAT_STEREO16;
186 else if(sfinfo.channels == 3)
188 if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
189 format = AL_FORMAT_BFORMAT2D_16;
191 else if(sfinfo.channels == 4)
193 if(sf_command(sndfile, SFC_WAVEX_GET_AMBISONIC, NULL, 0) == SF_AMBISONIC_B_FORMAT)
194 format = AL_FORMAT_BFORMAT3D_16;
196 if(!format)
198 fprintf(stderr, "Unsupported channel count: %d\n", sfinfo.channels);
199 sf_close(sndfile);
200 return 0;
203 /* Decode the whole audio file to a buffer. */
204 membuf = malloc((size_t)(sfinfo.frames * sfinfo.channels) * sizeof(short));
206 num_frames = sf_readf_short(sndfile, membuf, sfinfo.frames);
207 if(num_frames < 1)
209 free(membuf);
210 sf_close(sndfile);
211 fprintf(stderr, "Failed to read samples in %s (%" PRId64 ")\n", filename, num_frames);
212 return 0;
214 num_bytes = (ALsizei)(num_frames * sfinfo.channels) * (ALsizei)sizeof(short);
216 /* Buffer the audio data into a new buffer object, then free the data and
217 * close the file.
219 buffer = 0;
220 alGenBuffers(1, &buffer);
221 alBufferData(buffer, format, membuf, num_bytes, sfinfo.samplerate);
223 free(membuf);
224 sf_close(sndfile);
226 /* Check if an error occurred, and clean up if so. */
227 err = alGetError();
228 if(err != AL_NO_ERROR)
230 fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
231 if(buffer && alIsBuffer(buffer))
232 alDeleteBuffers(1, &buffer);
233 return 0;
236 return buffer;
240 int main(int argc, char **argv)
242 EFXEAXREVERBPROPERTIES reverb = EFX_REVERB_PRESET_GENERIC;
243 ALuint source, buffer, effect, slot;
244 ALenum state;
246 /* Print out usage if no arguments were specified */
247 if(argc < 2)
249 fprintf(stderr, "Usage: %s [-device <name] <filename>\n", argv[0]);
250 return 1;
253 /* Initialize OpenAL, and check for EFX support. */
254 argv++; argc--;
255 if(InitAL(&argv, &argc) != 0)
256 return 1;
258 if(!alcIsExtensionPresent(alcGetContextsDevice(alcGetCurrentContext()), "ALC_EXT_EFX"))
260 fprintf(stderr, "Error: EFX not supported\n");
261 CloseAL();
262 return 1;
265 /* Define a macro to help load the function pointers. */
266 #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
267 LOAD_PROC(LPALGENEFFECTS, alGenEffects);
268 LOAD_PROC(LPALDELETEEFFECTS, alDeleteEffects);
269 LOAD_PROC(LPALISEFFECT, alIsEffect);
270 LOAD_PROC(LPALEFFECTI, alEffecti);
271 LOAD_PROC(LPALEFFECTIV, alEffectiv);
272 LOAD_PROC(LPALEFFECTF, alEffectf);
273 LOAD_PROC(LPALEFFECTFV, alEffectfv);
274 LOAD_PROC(LPALGETEFFECTI, alGetEffecti);
275 LOAD_PROC(LPALGETEFFECTIV, alGetEffectiv);
276 LOAD_PROC(LPALGETEFFECTF, alGetEffectf);
277 LOAD_PROC(LPALGETEFFECTFV, alGetEffectfv);
279 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS, alGenAuxiliaryEffectSlots);
280 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS, alDeleteAuxiliaryEffectSlots);
281 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT, alIsAuxiliaryEffectSlot);
282 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI, alAuxiliaryEffectSloti);
283 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV, alAuxiliaryEffectSlotiv);
284 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF, alAuxiliaryEffectSlotf);
285 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV, alAuxiliaryEffectSlotfv);
286 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI, alGetAuxiliaryEffectSloti);
287 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV, alGetAuxiliaryEffectSlotiv);
288 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF, alGetAuxiliaryEffectSlotf);
289 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV, alGetAuxiliaryEffectSlotfv);
290 #undef LOAD_PROC
292 /* Load the sound into a buffer. */
293 buffer = LoadSound(argv[0]);
294 if(!buffer)
296 CloseAL();
297 return 1;
300 /* Load the reverb into an effect. */
301 effect = LoadEffect(&reverb);
302 if(!effect)
304 alDeleteBuffers(1, &buffer);
305 CloseAL();
306 return 1;
309 /* Create the effect slot object. This is what "plays" an effect on sources
310 * that connect to it. */
311 slot = 0;
312 alGenAuxiliaryEffectSlots(1, &slot);
314 /* Tell the effect slot to use the loaded effect object. Note that the this
315 * effectively copies the effect properties. You can modify or delete the
316 * effect object afterward without affecting the effect slot.
318 alAuxiliaryEffectSloti(slot, AL_EFFECTSLOT_EFFECT, (ALint)effect);
319 assert(alGetError()==AL_NO_ERROR && "Failed to set effect slot");
321 /* Create the source to play the sound with. */
322 source = 0;
323 alGenSources(1, &source);
324 alSourcei(source, AL_BUFFER, (ALint)buffer);
326 /* Connect the source to the effect slot. This tells the source to use the
327 * effect slot 'slot', on send #0 with the AL_FILTER_NULL filter object.
329 alSource3i(source, AL_AUXILIARY_SEND_FILTER, (ALint)slot, 0, AL_FILTER_NULL);
330 assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
332 /* Play the sound until it finishes. */
333 alSourcePlay(source);
334 do {
335 al_nssleep(10000000);
336 alGetSourcei(source, AL_SOURCE_STATE, &state);
337 } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
339 /* All done. Delete resources, and close down OpenAL. */
340 alDeleteSources(1, &source);
341 alDeleteAuxiliaryEffectSlots(1, &slot);
342 alDeleteEffects(1, &effect);
343 alDeleteBuffers(1, &buffer);
345 CloseAL();
347 return 0;