2 * OpenAL Multi-Zone Reverb Example
4 * Copyright (c) 2018 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
25 /* This file contains an example for controlling multiple reverb zones to
26 * smoothly transition between reverb environments. The general concept is to
27 * extend single-reverb by also tracking the closest adjacent environment, and
28 * utilize EAX Reverb's panning vectors to position them relative to the
46 #include "AL/efx-presets.h"
48 #include "common/alhelpers.h"
52 #define M_PI 3.14159265358979323846
56 /* Filter object functions */
57 static LPALGENFILTERS alGenFilters
;
58 static LPALDELETEFILTERS alDeleteFilters
;
59 static LPALISFILTER alIsFilter
;
60 static LPALFILTERI alFilteri
;
61 static LPALFILTERIV alFilteriv
;
62 static LPALFILTERF alFilterf
;
63 static LPALFILTERFV alFilterfv
;
64 static LPALGETFILTERI alGetFilteri
;
65 static LPALGETFILTERIV alGetFilteriv
;
66 static LPALGETFILTERF alGetFilterf
;
67 static LPALGETFILTERFV alGetFilterfv
;
69 /* Effect object functions */
70 static LPALGENEFFECTS alGenEffects
;
71 static LPALDELETEEFFECTS alDeleteEffects
;
72 static LPALISEFFECT alIsEffect
;
73 static LPALEFFECTI alEffecti
;
74 static LPALEFFECTIV alEffectiv
;
75 static LPALEFFECTF alEffectf
;
76 static LPALEFFECTFV alEffectfv
;
77 static LPALGETEFFECTI alGetEffecti
;
78 static LPALGETEFFECTIV alGetEffectiv
;
79 static LPALGETEFFECTF alGetEffectf
;
80 static LPALGETEFFECTFV alGetEffectfv
;
82 /* Auxiliary Effect Slot object functions */
83 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
;
84 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
;
85 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot
;
86 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
;
87 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv
;
88 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
;
89 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
;
90 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti
;
91 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv
;
92 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf
;
93 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv
;
95 /* C doesn't allow casting between function and non-function pointer types, so
96 * with C99 we need to use a union to reinterpret the pointer type. Pre-C99
97 * still needs to use a normal cast and live with the warning (C++ is fine with
98 * a regular reinterpret_cast).
100 #if __STDC_VERSION__ >= 199901L
101 #define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f
103 #define FUNCTION_CAST(T, ptr) (T)(ptr)
107 /* LoadEffect loads the given initial reverb properties into the given OpenAL
108 * effect object, and returns non-zero on success.
110 static int LoadEffect(ALuint effect
, const EFXEAXREVERBPROPERTIES
*reverb
)
116 /* Prepare the effect for EAX Reverb (standard reverb doesn't contain
117 * the needed panning vectors).
119 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_EAXREVERB
);
120 if((err
=alGetError()) != AL_NO_ERROR
)
122 fprintf(stderr
, "Failed to set EAX Reverb: %s (0x%04x)\n", alGetString(err
), err
);
126 /* Load the reverb properties. */
127 alEffectf(effect
, AL_EAXREVERB_DENSITY
, reverb
->flDensity
);
128 alEffectf(effect
, AL_EAXREVERB_DIFFUSION
, reverb
->flDiffusion
);
129 alEffectf(effect
, AL_EAXREVERB_GAIN
, reverb
->flGain
);
130 alEffectf(effect
, AL_EAXREVERB_GAINHF
, reverb
->flGainHF
);
131 alEffectf(effect
, AL_EAXREVERB_GAINLF
, reverb
->flGainLF
);
132 alEffectf(effect
, AL_EAXREVERB_DECAY_TIME
, reverb
->flDecayTime
);
133 alEffectf(effect
, AL_EAXREVERB_DECAY_HFRATIO
, reverb
->flDecayHFRatio
);
134 alEffectf(effect
, AL_EAXREVERB_DECAY_LFRATIO
, reverb
->flDecayLFRatio
);
135 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, reverb
->flReflectionsGain
);
136 alEffectf(effect
, AL_EAXREVERB_REFLECTIONS_DELAY
, reverb
->flReflectionsDelay
);
137 alEffectfv(effect
, AL_EAXREVERB_REFLECTIONS_PAN
, reverb
->flReflectionsPan
);
138 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, reverb
->flLateReverbGain
);
139 alEffectf(effect
, AL_EAXREVERB_LATE_REVERB_DELAY
, reverb
->flLateReverbDelay
);
140 alEffectfv(effect
, AL_EAXREVERB_LATE_REVERB_PAN
, reverb
->flLateReverbPan
);
141 alEffectf(effect
, AL_EAXREVERB_ECHO_TIME
, reverb
->flEchoTime
);
142 alEffectf(effect
, AL_EAXREVERB_ECHO_DEPTH
, reverb
->flEchoDepth
);
143 alEffectf(effect
, AL_EAXREVERB_MODULATION_TIME
, reverb
->flModulationTime
);
144 alEffectf(effect
, AL_EAXREVERB_MODULATION_DEPTH
, reverb
->flModulationDepth
);
145 alEffectf(effect
, AL_EAXREVERB_AIR_ABSORPTION_GAINHF
, reverb
->flAirAbsorptionGainHF
);
146 alEffectf(effect
, AL_EAXREVERB_HFREFERENCE
, reverb
->flHFReference
);
147 alEffectf(effect
, AL_EAXREVERB_LFREFERENCE
, reverb
->flLFReference
);
148 alEffectf(effect
, AL_EAXREVERB_ROOM_ROLLOFF_FACTOR
, reverb
->flRoomRolloffFactor
);
149 alEffecti(effect
, AL_EAXREVERB_DECAY_HFLIMIT
, reverb
->iDecayHFLimit
);
151 /* Check if an error occured, and return failure if so. */
152 if((err
=alGetError()) != AL_NO_ERROR
)
154 fprintf(stderr
, "Error setting up reverb: %s\n", alGetString(err
));
162 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
163 * returns the new buffer ID.
165 static ALuint
LoadSound(const char *filename
)
172 sf_count_t num_frames
;
175 /* Open the audio file and check that it's usable. */
176 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
179 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
182 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(short))/sfinfo
.channels
)
184 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
189 /* Get the sound format, and figure out the OpenAL format */
190 if(sfinfo
.channels
== 1)
191 format
= AL_FORMAT_MONO16
;
192 else if(sfinfo
.channels
== 2)
193 format
= AL_FORMAT_STEREO16
;
196 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
201 /* Decode the whole audio file to a buffer. */
202 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(short));
204 num_frames
= sf_readf_short(sndfile
, membuf
, sfinfo
.frames
);
209 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
212 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(short);
214 /* Buffer the audio data into a new buffer object, then free the data and
218 alGenBuffers(1, &buffer
);
219 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
224 /* Check if an error occured, and clean up if so. */
226 if(err
!= AL_NO_ERROR
)
228 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
229 if(buffer
&& alIsBuffer(buffer
))
230 alDeleteBuffers(1, &buffer
);
238 /* Helper to calculate the dot-product of the two given vectors. */
239 static ALfloat
dot_product(const ALfloat vec0
[3], const ALfloat vec1
[3])
241 return vec0
[0]*vec1
[0] + vec0
[1]*vec1
[1] + vec0
[2]*vec1
[2];
244 /* Helper to normalize a given vector. */
245 static void normalize(ALfloat vec
[3])
247 ALfloat mag
= sqrtf(dot_product(vec
, vec
));
263 /* The main update function to update the listener and environment effects. */
264 static void UpdateListenerAndEffects(float timediff
, const ALuint slots
[2], const ALuint effects
[2], const EFXEAXREVERBPROPERTIES reverbs
[2])
266 static const ALfloat listener_move_scale
= 10.0f
;
267 /* Individual reverb zones are connected via "portals". Each portal has a
268 * position (center point of the connecting area), a normal (facing
269 * direction), and a radius (approximate size of the connecting area).
271 const ALfloat portal_pos
[3] = { 0.0f
, 0.0f
, 0.0f
};
272 const ALfloat portal_norm
[3] = { sqrtf(0.5f
), 0.0f
, -sqrtf(0.5f
) };
273 const ALfloat portal_radius
= 2.5f
;
274 ALfloat other_dir
[3], this_dir
[3];
275 ALfloat listener_pos
[3];
276 ALfloat local_norm
[3];
277 ALfloat local_dir
[3];
278 ALfloat near_edge
[3];
282 /* Update the listener position for the amount of time passed. This uses a
283 * simple triangular LFO to offset the position (moves along the X axis
284 * between -listener_move_scale and +listener_move_scale for each
287 listener_pos
[0] = (fabsf(2.0f
- timediff
/2.0f
) - 1.0f
) * listener_move_scale
;
288 listener_pos
[1] = 0.0f
;
289 listener_pos
[2] = 0.0f
;
290 alListenerfv(AL_POSITION
, listener_pos
);
292 /* Calculate local_dir, which represents the listener-relative point to the
293 * adjacent zone (should also include orientation). Because EAX Reverb uses
294 * left-handed coordinates instead of right-handed like the rest of OpenAL,
295 * negate Z for the local values.
297 local_dir
[0] = portal_pos
[0] - listener_pos
[0];
298 local_dir
[1] = portal_pos
[1] - listener_pos
[1];
299 local_dir
[2] = -(portal_pos
[2] - listener_pos
[2]);
300 /* A normal application would also rotate the portal's normal given the
301 * listener orientation, to get the listener-relative normal.
303 local_norm
[0] = portal_norm
[0];
304 local_norm
[1] = portal_norm
[1];
305 local_norm
[2] = -portal_norm
[2];
307 /* Calculate the distance from the listener to the portal, and ensure it's
308 * far enough away to not suffer severe floating-point precision issues.
310 dist
= sqrtf(dot_product(local_dir
, local_dir
));
313 const EFXEAXREVERBPROPERTIES
*other_reverb
, *this_reverb
;
314 ALuint other_effect
, this_effect
;
315 ALfloat magnitude
, dir_dot_norm
;
317 /* Normalize the direction to the portal. */
318 local_dir
[0] /= dist
;
319 local_dir
[1] /= dist
;
320 local_dir
[2] /= dist
;
322 /* Calculate the dot product of the portal's local direction and local
323 * normal, which is used for angular and side checks later on.
325 dir_dot_norm
= dot_product(local_dir
, local_norm
);
327 /* Figure out which zone we're in. */
328 if(dir_dot_norm
<= 0.0f
)
330 /* We're in front of the portal, so we're in Zone 0. */
331 this_effect
= effects
[0];
332 other_effect
= effects
[1];
333 this_reverb
= &reverbs
[0];
334 other_reverb
= &reverbs
[1];
338 /* We're behind the portal, so we're in Zone 1. */
339 this_effect
= effects
[1];
340 other_effect
= effects
[0];
341 this_reverb
= &reverbs
[1];
342 other_reverb
= &reverbs
[0];
345 /* Calculate the listener-relative extents of the portal. */
346 /* First, project the listener-to-portal vector onto the portal's plane
347 * to get the portal-relative direction along the plane that goes away
348 * from the listener (toward the farthest edge of the portal).
350 far_edge
[0] = local_dir
[0] - local_norm
[0]*dir_dot_norm
;
351 far_edge
[1] = local_dir
[1] - local_norm
[1]*dir_dot_norm
;
352 far_edge
[2] = local_dir
[2] - local_norm
[2]*dir_dot_norm
;
354 edist
= sqrtf(dot_product(far_edge
, far_edge
));
357 /* Rescale the portal-relative vector to be at the radius edge. */
358 ALfloat mag
= portal_radius
/ edist
;
363 /* Calculate the closest edge of the portal by negating the
364 * farthest, and add an offset to make them both relative to the
367 near_edge
[0] = local_dir
[0]*dist
- far_edge
[0];
368 near_edge
[1] = local_dir
[1]*dist
- far_edge
[1];
369 near_edge
[2] = local_dir
[2]*dist
- far_edge
[2];
370 far_edge
[0] += local_dir
[0]*dist
;
371 far_edge
[1] += local_dir
[1]*dist
;
372 far_edge
[2] += local_dir
[2]*dist
;
374 /* Normalize the listener-relative extents of the portal, then
375 * calculate the panning magnitude for the other zone given the
376 * apparent size of the opening. The panning magnitude affects the
377 * envelopment of the environment, with 1 being a point, 0.5 being
378 * half coverage around the listener, and 0 being full coverage.
381 normalize(near_edge
);
382 magnitude
= 1.0f
- acosf(dot_product(far_edge
, near_edge
))/(float)(M_PI
*2.0);
384 /* Recalculate the panning direction, to be directly between the
385 * direction of the two extents.
387 local_dir
[0] = far_edge
[0] + near_edge
[0];
388 local_dir
[1] = far_edge
[1] + near_edge
[1];
389 local_dir
[2] = far_edge
[2] + near_edge
[2];
390 normalize(local_dir
);
394 /* If we get here, the listener is directly in front of or behind
395 * the center of the portal, making all aperture edges effectively
396 * equidistant. Calculating the panning magnitude is simplified,
397 * using the arctangent of the radius and distance.
399 magnitude
= 1.0f
- (atan2f(portal_radius
, dist
) / (float)M_PI
);
402 /* Scale the other zone's panning vector. */
403 other_dir
[0] = local_dir
[0] * magnitude
;
404 other_dir
[1] = local_dir
[1] * magnitude
;
405 other_dir
[2] = local_dir
[2] * magnitude
;
406 /* Pan the current zone to the opposite direction of the portal, and
407 * take the remaining percentage of the portal's magnitude.
409 this_dir
[0] = local_dir
[0] * (magnitude
-1.0f
);
410 this_dir
[1] = local_dir
[1] * (magnitude
-1.0f
);
411 this_dir
[2] = local_dir
[2] * (magnitude
-1.0f
);
413 /* Now set the effects' panning vectors and gain. Energy is shared
414 * between environments, so attenuate according to each zone's
415 * contribution (note: gain^2 = energy).
417 alEffectf(this_effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, this_reverb
->flReflectionsGain
* sqrtf(magnitude
));
418 alEffectf(this_effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, this_reverb
->flLateReverbGain
* sqrtf(magnitude
));
419 alEffectfv(this_effect
, AL_EAXREVERB_REFLECTIONS_PAN
, this_dir
);
420 alEffectfv(this_effect
, AL_EAXREVERB_LATE_REVERB_PAN
, this_dir
);
422 alEffectf(other_effect
, AL_EAXREVERB_REFLECTIONS_GAIN
, other_reverb
->flReflectionsGain
* sqrtf(1.0f
-magnitude
));
423 alEffectf(other_effect
, AL_EAXREVERB_LATE_REVERB_GAIN
, other_reverb
->flLateReverbGain
* sqrtf(1.0f
-magnitude
));
424 alEffectfv(other_effect
, AL_EAXREVERB_REFLECTIONS_PAN
, other_dir
);
425 alEffectfv(other_effect
, AL_EAXREVERB_LATE_REVERB_PAN
, other_dir
);
429 /* We're practically in the center of the portal. Give the panning
430 * vectors a 50/50 split, with Zone 0 covering the half in front of
431 * the normal, and Zone 1 covering the half behind.
433 this_dir
[0] = local_norm
[0] / 2.0f
;
434 this_dir
[1] = local_norm
[1] / 2.0f
;
435 this_dir
[2] = local_norm
[2] / 2.0f
;
437 other_dir
[0] = local_norm
[0] / -2.0f
;
438 other_dir
[1] = local_norm
[1] / -2.0f
;
439 other_dir
[2] = local_norm
[2] / -2.0f
;
441 alEffectf(effects
[0], AL_EAXREVERB_REFLECTIONS_GAIN
, reverbs
[0].flReflectionsGain
* sqrtf(0.5f
));
442 alEffectf(effects
[0], AL_EAXREVERB_LATE_REVERB_GAIN
, reverbs
[0].flLateReverbGain
* sqrtf(0.5f
));
443 alEffectfv(effects
[0], AL_EAXREVERB_REFLECTIONS_PAN
, this_dir
);
444 alEffectfv(effects
[0], AL_EAXREVERB_LATE_REVERB_PAN
, this_dir
);
446 alEffectf(effects
[1], AL_EAXREVERB_REFLECTIONS_GAIN
, reverbs
[1].flReflectionsGain
* sqrtf(0.5f
));
447 alEffectf(effects
[1], AL_EAXREVERB_LATE_REVERB_GAIN
, reverbs
[1].flLateReverbGain
* sqrtf(0.5f
));
448 alEffectfv(effects
[1], AL_EAXREVERB_REFLECTIONS_PAN
, other_dir
);
449 alEffectfv(effects
[1], AL_EAXREVERB_LATE_REVERB_PAN
, other_dir
);
452 /* Finally, update the effect slots with the updated effect parameters. */
453 alAuxiliaryEffectSloti(slots
[0], AL_EFFECTSLOT_EFFECT
, (ALint
)effects
[0]);
454 alAuxiliaryEffectSloti(slots
[1], AL_EFFECTSLOT_EFFECT
, (ALint
)effects
[1]);
458 int main(int argc
, char **argv
)
460 static const int MaxTransitions
= 8;
461 EFXEAXREVERBPROPERTIES reverbs
[2] = {
462 EFX_REVERB_PRESET_CARPETEDHALLWAY
,
463 EFX_REVERB_PRESET_BATHROOM
465 ALCdevice
*device
= NULL
;
466 ALCcontext
*context
= NULL
;
467 ALuint effects
[2] = { 0, 0 };
468 ALuint slots
[2] = { 0, 0 };
469 ALuint direct_filter
= 0;
472 ALCint num_sends
= 0;
473 ALenum state
= AL_INITIAL
;
474 ALfloat direct_gain
= 1.0f
;
478 /* Print out usage if no arguments were specified */
481 fprintf(stderr
, "Usage: %s [-device <name>] [options] <filename>\n\n"
483 "\t-nodirect\tSilence direct path output (easier to hear reverb)\n\n",
488 /* Initialize OpenAL, and check for EFX support with at least 2 auxiliary
489 * sends (if multiple sends are supported, 2 are provided by default; if
490 * you want more, you have to request it through alcCreateContext).
493 if(InitAL(&argv
, &argc
) != 0)
498 if(strcmp(argv
[0], "-nodirect") == 0)
507 fprintf(stderr
, "No filename spacified.\n");
512 context
= alcGetCurrentContext();
513 device
= alcGetContextsDevice(context
);
515 if(!alcIsExtensionPresent(device
, "ALC_EXT_EFX"))
517 fprintf(stderr
, "Error: EFX not supported\n");
523 alcGetIntegerv(device
, ALC_MAX_AUXILIARY_SENDS
, 1, &num_sends
);
524 if(alcGetError(device
) != ALC_NO_ERROR
|| num_sends
< 2)
526 fprintf(stderr
, "Error: Device does not support multiple sends (got %d, need 2)\n",
532 /* Define a macro to help load the function pointers. */
533 #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alGetProcAddress(#x)))
534 LOAD_PROC(LPALGENFILTERS
, alGenFilters
);
535 LOAD_PROC(LPALDELETEFILTERS
, alDeleteFilters
);
536 LOAD_PROC(LPALISFILTER
, alIsFilter
);
537 LOAD_PROC(LPALFILTERI
, alFilteri
);
538 LOAD_PROC(LPALFILTERIV
, alFilteriv
);
539 LOAD_PROC(LPALFILTERF
, alFilterf
);
540 LOAD_PROC(LPALFILTERFV
, alFilterfv
);
541 LOAD_PROC(LPALGETFILTERI
, alGetFilteri
);
542 LOAD_PROC(LPALGETFILTERIV
, alGetFilteriv
);
543 LOAD_PROC(LPALGETFILTERF
, alGetFilterf
);
544 LOAD_PROC(LPALGETFILTERFV
, alGetFilterfv
);
546 LOAD_PROC(LPALGENEFFECTS
, alGenEffects
);
547 LOAD_PROC(LPALDELETEEFFECTS
, alDeleteEffects
);
548 LOAD_PROC(LPALISEFFECT
, alIsEffect
);
549 LOAD_PROC(LPALEFFECTI
, alEffecti
);
550 LOAD_PROC(LPALEFFECTIV
, alEffectiv
);
551 LOAD_PROC(LPALEFFECTF
, alEffectf
);
552 LOAD_PROC(LPALEFFECTFV
, alEffectfv
);
553 LOAD_PROC(LPALGETEFFECTI
, alGetEffecti
);
554 LOAD_PROC(LPALGETEFFECTIV
, alGetEffectiv
);
555 LOAD_PROC(LPALGETEFFECTF
, alGetEffectf
);
556 LOAD_PROC(LPALGETEFFECTFV
, alGetEffectfv
);
558 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS
, alGenAuxiliaryEffectSlots
);
559 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS
, alDeleteAuxiliaryEffectSlots
);
560 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT
, alIsAuxiliaryEffectSlot
);
561 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI
, alAuxiliaryEffectSloti
);
562 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV
, alAuxiliaryEffectSlotiv
);
563 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF
, alAuxiliaryEffectSlotf
);
564 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV
, alAuxiliaryEffectSlotfv
);
565 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI
, alGetAuxiliaryEffectSloti
);
566 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV
, alGetAuxiliaryEffectSlotiv
);
567 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF
, alGetAuxiliaryEffectSlotf
);
568 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV
, alGetAuxiliaryEffectSlotfv
);
571 /* Load the sound into a buffer. */
572 buffer
= LoadSound(argv
[0]);
579 /* Generate two effects for two "zones", and load a reverb into each one.
580 * Note that unlike single-zone reverb, where you can store one effect per
581 * preset, for multi-zone reverb you should have one effect per environment
582 * instance, or one per audible zone. This is because we'll be changing the
583 * effects' properties in real-time based on the environment instance
584 * relative to the listener.
586 alGenEffects(2, effects
);
587 if(!LoadEffect(effects
[0], &reverbs
[0]) || !LoadEffect(effects
[1], &reverbs
[1]))
589 alDeleteEffects(2, effects
);
590 alDeleteBuffers(1, &buffer
);
595 /* Create the effect slot objects, one for each "active" effect. */
596 alGenAuxiliaryEffectSlots(2, slots
);
598 /* Tell the effect slots to use the loaded effect objects, with slot 0 for
599 * Zone 0 and slot 1 for Zone 1. Note that this effectively copies the
600 * effect properties. Modifying or deleting the effect object afterward
601 * won't directly affect the effect slot until they're reapplied like this.
603 alAuxiliaryEffectSloti(slots
[0], AL_EFFECTSLOT_EFFECT
, (ALint
)effects
[0]);
604 alAuxiliaryEffectSloti(slots
[1], AL_EFFECTSLOT_EFFECT
, (ALint
)effects
[1]);
605 assert(alGetError()==AL_NO_ERROR
&& "Failed to set effect slot");
607 /* For the purposes of this example, prepare a filter that optionally
608 * silences the direct path which allows us to hear just the reverberation.
609 * A filter like this is normally used for obstruction, where the path
610 * directly between the listener and source is blocked (the exact
611 * properties depending on the type and thickness of the obstructing
614 alGenFilters(1, &direct_filter
);
615 alFilteri(direct_filter
, AL_FILTER_TYPE
, AL_FILTER_LOWPASS
);
616 alFilterf(direct_filter
, AL_LOWPASS_GAIN
, direct_gain
);
617 assert(alGetError()==AL_NO_ERROR
&& "Failed to set direct filter");
619 /* Create the source to play the sound with, place it in front of the
620 * listener's path in the left zone.
623 alGenSources(1, &source
);
624 alSourcei(source
, AL_LOOPING
, AL_TRUE
);
625 alSource3f(source
, AL_POSITION
, -5.0f
, 0.0f
, -2.0f
);
626 alSourcei(source
, AL_DIRECT_FILTER
, (ALint
)direct_filter
);
627 alSourcei(source
, AL_BUFFER
, (ALint
)buffer
);
629 /* Connect the source to the effect slots. Here, we connect source send 0
630 * to Zone 0's slot, and send 1 to Zone 1's slot. Filters can be specified
631 * to occlude the source from each zone by varying amounts; for example, a
632 * source within a particular zone would be unfiltered, while a source that
633 * can only see a zone through a window or thin wall may be attenuated for
636 alSource3i(source
, AL_AUXILIARY_SEND_FILTER
, (ALint
)slots
[0], 0, AL_FILTER_NULL
);
637 alSource3i(source
, AL_AUXILIARY_SEND_FILTER
, (ALint
)slots
[1], 1, AL_FILTER_NULL
);
638 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
640 /* Get the current time as the base for timing in the main loop. */
641 basetime
= altime_get();
643 printf("Transition %d of %d...\n", loops
+1, MaxTransitions
);
645 /* Play the sound for a while. */
646 alSourcePlay(source
);
651 /* Start a batch update, to ensure all changes apply simultaneously. */
652 alcSuspendContext(context
);
654 /* Get the current time to track the amount of time that passed.
655 * Convert the difference to seconds.
657 curtime
= altime_get();
658 timediff
= (float)(curtime
- basetime
) / 1000.0f
;
660 /* Avoid negative time deltas, in case of non-monotonic clocks. */
663 else while(timediff
>= 4.0f
*(float)((loops
&1)+1))
665 /* For this example, each transition occurs over 4 seconds, and
666 * there's 2 transitions per cycle.
668 if(++loops
< MaxTransitions
)
669 printf("Transition %d of %d...\n", loops
+1, MaxTransitions
);
672 /* Cycle completed. Decrease the delta and increase the base
673 * time to start a new cycle.
680 /* Update the listener and effects, and finish the batch. */
681 UpdateListenerAndEffects(timediff
, slots
, effects
, reverbs
);
682 alcProcessContext(context
);
684 al_nssleep(10000000);
686 alGetSourcei(source
, AL_SOURCE_STATE
, &state
);
687 } while(alGetError() == AL_NO_ERROR
&& state
== AL_PLAYING
&& loops
< MaxTransitions
);
689 /* All done. Delete resources, and close down OpenAL. */
690 alDeleteSources(1, &source
);
691 alDeleteAuxiliaryEffectSlots(2, slots
);
692 alDeleteEffects(2, effects
);
693 alDeleteFilters(1, &direct_filter
);
694 alDeleteBuffers(1, &buffer
);