2 * OpenAL Convolution Reverb Example
4 * Copyright (c) 2020 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 applying convolution reverb to a source. */
39 #include "common/alhelpers.h"
42 #ifndef AL_SOFT_convolution_reverb
43 #define AL_SOFT_convolution_reverb
44 #define AL_EFFECT_CONVOLUTION_REVERB_SOFT 0xA000
48 /* Filter object functions */
49 static LPALGENFILTERS alGenFilters
;
50 static LPALDELETEFILTERS alDeleteFilters
;
51 static LPALISFILTER alIsFilter
;
52 static LPALFILTERI alFilteri
;
53 static LPALFILTERIV alFilteriv
;
54 static LPALFILTERF alFilterf
;
55 static LPALFILTERFV alFilterfv
;
56 static LPALGETFILTERI alGetFilteri
;
57 static LPALGETFILTERIV alGetFilteriv
;
58 static LPALGETFILTERF alGetFilterf
;
59 static LPALGETFILTERFV alGetFilterfv
;
61 /* Effect object functions */
62 static LPALGENEFFECTS alGenEffects
;
63 static LPALDELETEEFFECTS alDeleteEffects
;
64 static LPALISEFFECT alIsEffect
;
65 static LPALEFFECTI alEffecti
;
66 static LPALEFFECTIV alEffectiv
;
67 static LPALEFFECTF alEffectf
;
68 static LPALEFFECTFV alEffectfv
;
69 static LPALGETEFFECTI alGetEffecti
;
70 static LPALGETEFFECTIV alGetEffectiv
;
71 static LPALGETEFFECTF alGetEffectf
;
72 static LPALGETEFFECTFV alGetEffectfv
;
74 /* Auxiliary Effect Slot object functions */
75 static LPALGENAUXILIARYEFFECTSLOTS alGenAuxiliaryEffectSlots
;
76 static LPALDELETEAUXILIARYEFFECTSLOTS alDeleteAuxiliaryEffectSlots
;
77 static LPALISAUXILIARYEFFECTSLOT alIsAuxiliaryEffectSlot
;
78 static LPALAUXILIARYEFFECTSLOTI alAuxiliaryEffectSloti
;
79 static LPALAUXILIARYEFFECTSLOTIV alAuxiliaryEffectSlotiv
;
80 static LPALAUXILIARYEFFECTSLOTF alAuxiliaryEffectSlotf
;
81 static LPALAUXILIARYEFFECTSLOTFV alAuxiliaryEffectSlotfv
;
82 static LPALGETAUXILIARYEFFECTSLOTI alGetAuxiliaryEffectSloti
;
83 static LPALGETAUXILIARYEFFECTSLOTIV alGetAuxiliaryEffectSlotiv
;
84 static LPALGETAUXILIARYEFFECTSLOTF alGetAuxiliaryEffectSlotf
;
85 static LPALGETAUXILIARYEFFECTSLOTFV alGetAuxiliaryEffectSlotfv
;
88 /* This stuff defines a simple streaming player object, the same as alstream.c.
89 * Comments are removed for brevity, see alstream.c for more details.
92 #define BUFFER_SAMPLES 8192
94 typedef struct StreamPlayer
{
95 ALuint buffers
[NUM_BUFFERS
];
105 static StreamPlayer
*NewPlayer(void)
107 StreamPlayer
*player
;
109 player
= calloc(1, sizeof(*player
));
110 assert(player
!= NULL
);
112 alGenBuffers(NUM_BUFFERS
, player
->buffers
);
113 assert(alGetError() == AL_NO_ERROR
&& "Could not create buffers");
115 alGenSources(1, &player
->source
);
116 assert(alGetError() == AL_NO_ERROR
&& "Could not create source");
118 alSource3i(player
->source
, AL_POSITION
, 0, 0, -1);
119 alSourcei(player
->source
, AL_SOURCE_RELATIVE
, AL_TRUE
);
120 alSourcei(player
->source
, AL_ROLLOFF_FACTOR
, 0);
121 assert(alGetError() == AL_NO_ERROR
&& "Could not set source parameters");
126 static void ClosePlayerFile(StreamPlayer
*player
)
129 sf_close(player
->sndfile
);
130 player
->sndfile
= NULL
;
132 free(player
->membuf
);
133 player
->membuf
= NULL
;
136 static void DeletePlayer(StreamPlayer
*player
)
138 ClosePlayerFile(player
);
140 alDeleteSources(1, &player
->source
);
141 alDeleteBuffers(NUM_BUFFERS
, player
->buffers
);
142 if(alGetError() != AL_NO_ERROR
)
143 fprintf(stderr
, "Failed to delete object IDs\n");
145 memset(player
, 0, sizeof(*player
));
149 static int OpenPlayerFile(StreamPlayer
*player
, const char *filename
)
153 ClosePlayerFile(player
);
155 player
->sndfile
= sf_open(filename
, SFM_READ
, &player
->sfinfo
);
158 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(NULL
));
162 player
->format
= AL_NONE
;
163 if(player
->sfinfo
.channels
== 1)
164 player
->format
= AL_FORMAT_MONO_FLOAT32
;
165 else if(player
->sfinfo
.channels
== 2)
166 player
->format
= AL_FORMAT_STEREO_FLOAT32
;
167 else if(player
->sfinfo
.channels
== 6)
168 player
->format
= AL_FORMAT_51CHN32
;
169 else if(player
->sfinfo
.channels
== 3)
171 if(sf_command(player
->sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
172 player
->format
= AL_FORMAT_BFORMAT2D_FLOAT32
;
174 else if(player
->sfinfo
.channels
== 4)
176 if(sf_command(player
->sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
177 player
->format
= AL_FORMAT_BFORMAT3D_FLOAT32
;
181 fprintf(stderr
, "Unsupported channel count: %d\n", player
->sfinfo
.channels
);
182 sf_close(player
->sndfile
);
183 player
->sndfile
= NULL
;
187 frame_size
= (size_t)(BUFFER_SAMPLES
* player
->sfinfo
.channels
) * sizeof(float);
188 player
->membuf
= malloc(frame_size
);
193 static int StartPlayer(StreamPlayer
*player
)
197 alSourceRewind(player
->source
);
198 alSourcei(player
->source
, AL_BUFFER
, 0);
200 for(i
= 0;i
< NUM_BUFFERS
;i
++)
202 sf_count_t slen
= sf_readf_float(player
->sndfile
, player
->membuf
, BUFFER_SAMPLES
);
205 slen
*= player
->sfinfo
.channels
* (sf_count_t
)sizeof(float);
206 alBufferData(player
->buffers
[i
], player
->format
, player
->membuf
, (ALsizei
)slen
,
207 player
->sfinfo
.samplerate
);
209 if(alGetError() != AL_NO_ERROR
)
211 fprintf(stderr
, "Error buffering for playback\n");
215 alSourceQueueBuffers(player
->source
, i
, player
->buffers
);
216 alSourcePlay(player
->source
);
217 if(alGetError() != AL_NO_ERROR
)
219 fprintf(stderr
, "Error starting playback\n");
226 static int UpdatePlayer(StreamPlayer
*player
)
228 ALint processed
, state
;
230 alGetSourcei(player
->source
, AL_SOURCE_STATE
, &state
);
231 alGetSourcei(player
->source
, AL_BUFFERS_PROCESSED
, &processed
);
232 if(alGetError() != AL_NO_ERROR
)
234 fprintf(stderr
, "Error checking source state\n");
243 alSourceUnqueueBuffers(player
->source
, 1, &bufid
);
246 slen
= sf_readf_float(player
->sndfile
, player
->membuf
, BUFFER_SAMPLES
);
249 slen
*= player
->sfinfo
.channels
* (sf_count_t
)sizeof(float);
250 alBufferData(bufid
, player
->format
, player
->membuf
, (ALsizei
)slen
,
251 player
->sfinfo
.samplerate
);
252 alSourceQueueBuffers(player
->source
, 1, &bufid
);
254 if(alGetError() != AL_NO_ERROR
)
256 fprintf(stderr
, "Error buffering data\n");
261 if(state
!= AL_PLAYING
&& state
!= AL_PAUSED
)
265 alGetSourcei(player
->source
, AL_BUFFERS_QUEUED
, &queued
);
269 alSourcePlay(player
->source
);
270 if(alGetError() != AL_NO_ERROR
)
272 fprintf(stderr
, "Error restarting playback\n");
281 /* CreateEffect creates a new OpenAL effect object with a convolution reverb
282 * type, and returns the new effect ID.
284 static ALuint
CreateEffect(void)
289 printf("Using Convolution Reverb\n");
291 /* Create the effect object and set the convolution reverb effect type. */
292 alGenEffects(1, &effect
);
293 alEffecti(effect
, AL_EFFECT_TYPE
, AL_EFFECT_CONVOLUTION_REVERB_SOFT
);
295 /* Check if an error occured, and clean up if so. */
297 if(err
!= AL_NO_ERROR
)
299 fprintf(stderr
, "OpenAL error: %s\n", alGetString(err
));
300 if(alIsEffect(effect
))
301 alDeleteEffects(1, &effect
);
308 /* LoadBuffer loads the named audio file into an OpenAL buffer object, and
309 * returns the new buffer ID.
311 static ALuint
LoadSound(const char *filename
)
313 const char *namepart
;
319 sf_count_t num_frames
;
322 /* Open the audio file and check that it's usable. */
323 sndfile
= sf_open(filename
, SFM_READ
, &sfinfo
);
326 fprintf(stderr
, "Could not open audio in %s: %s\n", filename
, sf_strerror(sndfile
));
329 if(sfinfo
.frames
< 1 || sfinfo
.frames
> (sf_count_t
)(INT_MAX
/sizeof(float))/sfinfo
.channels
)
331 fprintf(stderr
, "Bad sample count in %s (%" PRId64
")\n", filename
, sfinfo
.frames
);
336 /* Get the sound format, and figure out the OpenAL format. Use floats since
337 * impulse responses will usually have more than 16-bit precision.
340 if(sfinfo
.channels
== 1)
341 format
= AL_FORMAT_MONO_FLOAT32
;
342 else if(sfinfo
.channels
== 2)
343 format
= AL_FORMAT_STEREO_FLOAT32
;
344 else if(sfinfo
.channels
== 3)
346 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
347 format
= AL_FORMAT_BFORMAT2D_FLOAT32
;
349 else if(sfinfo
.channels
== 4)
351 if(sf_command(sndfile
, SFC_WAVEX_GET_AMBISONIC
, NULL
, 0) == SF_AMBISONIC_B_FORMAT
)
352 format
= AL_FORMAT_BFORMAT3D_FLOAT32
;
356 fprintf(stderr
, "Unsupported channel count: %d\n", sfinfo
.channels
);
361 namepart
= strrchr(filename
, '/');
362 if(namepart
|| (namepart
=strrchr(filename
, '\\')))
366 printf("Loading: %s (%s, %dhz, %" PRId64
" samples / %.2f seconds)\n", namepart
,
367 FormatName(format
), sfinfo
.samplerate
, sfinfo
.frames
,
368 (double)sfinfo
.frames
/ sfinfo
.samplerate
);
371 /* Decode the whole audio file to a buffer. */
372 membuf
= malloc((size_t)(sfinfo
.frames
* sfinfo
.channels
) * sizeof(float));
374 num_frames
= sf_readf_float(sndfile
, membuf
, sfinfo
.frames
);
379 fprintf(stderr
, "Failed to read samples in %s (%" PRId64
")\n", filename
, num_frames
);
382 num_bytes
= (ALsizei
)(num_frames
* sfinfo
.channels
) * (ALsizei
)sizeof(float);
384 /* Buffer the audio data into a new buffer object, then free the data and
388 alGenBuffers(1, &buffer
);
389 alBufferData(buffer
, format
, membuf
, num_bytes
, sfinfo
.samplerate
);
394 /* Check if an error occured, and clean up if so. */
396 if(err
!= AL_NO_ERROR
)
398 fprintf(stderr
, "OpenAL Error: %s\n", alGetString(err
));
399 if(buffer
&& alIsBuffer(buffer
))
400 alDeleteBuffers(1, &buffer
);
408 int main(int argc
, char **argv
)
410 ALuint ir_buffer
, filter
, effect
, slot
;
411 StreamPlayer
*player
;
414 /* Print out usage if no arguments were specified */
417 fprintf(stderr
, "Usage: %s [-device <name>] <impulse response file> "
418 "<[-dry | -nodry] filename>...\n", argv
[0]);
423 if(InitAL(&argv
, &argc
) != 0)
426 if(!alIsExtensionPresent("AL_SOFTX_convolution_reverb"))
429 fprintf(stderr
, "Error: Convolution revern not supported\n");
436 fprintf(stderr
, "Error: Missing impulse response or sound files\n");
440 /* Define a macro to help load the function pointers. */
441 #define LOAD_PROC(T, x) ((x) = (T)alGetProcAddress(#x))
442 LOAD_PROC(LPALGENFILTERS
, alGenFilters
);
443 LOAD_PROC(LPALDELETEFILTERS
, alDeleteFilters
);
444 LOAD_PROC(LPALISFILTER
, alIsFilter
);
445 LOAD_PROC(LPALFILTERI
, alFilteri
);
446 LOAD_PROC(LPALFILTERIV
, alFilteriv
);
447 LOAD_PROC(LPALFILTERF
, alFilterf
);
448 LOAD_PROC(LPALFILTERFV
, alFilterfv
);
449 LOAD_PROC(LPALGETFILTERI
, alGetFilteri
);
450 LOAD_PROC(LPALGETFILTERIV
, alGetFilteriv
);
451 LOAD_PROC(LPALGETFILTERF
, alGetFilterf
);
452 LOAD_PROC(LPALGETFILTERFV
, alGetFilterfv
);
454 LOAD_PROC(LPALGENEFFECTS
, alGenEffects
);
455 LOAD_PROC(LPALDELETEEFFECTS
, alDeleteEffects
);
456 LOAD_PROC(LPALISEFFECT
, alIsEffect
);
457 LOAD_PROC(LPALEFFECTI
, alEffecti
);
458 LOAD_PROC(LPALEFFECTIV
, alEffectiv
);
459 LOAD_PROC(LPALEFFECTF
, alEffectf
);
460 LOAD_PROC(LPALEFFECTFV
, alEffectfv
);
461 LOAD_PROC(LPALGETEFFECTI
, alGetEffecti
);
462 LOAD_PROC(LPALGETEFFECTIV
, alGetEffectiv
);
463 LOAD_PROC(LPALGETEFFECTF
, alGetEffectf
);
464 LOAD_PROC(LPALGETEFFECTFV
, alGetEffectfv
);
466 LOAD_PROC(LPALGENAUXILIARYEFFECTSLOTS
, alGenAuxiliaryEffectSlots
);
467 LOAD_PROC(LPALDELETEAUXILIARYEFFECTSLOTS
, alDeleteAuxiliaryEffectSlots
);
468 LOAD_PROC(LPALISAUXILIARYEFFECTSLOT
, alIsAuxiliaryEffectSlot
);
469 LOAD_PROC(LPALAUXILIARYEFFECTSLOTI
, alAuxiliaryEffectSloti
);
470 LOAD_PROC(LPALAUXILIARYEFFECTSLOTIV
, alAuxiliaryEffectSlotiv
);
471 LOAD_PROC(LPALAUXILIARYEFFECTSLOTF
, alAuxiliaryEffectSlotf
);
472 LOAD_PROC(LPALAUXILIARYEFFECTSLOTFV
, alAuxiliaryEffectSlotfv
);
473 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTI
, alGetAuxiliaryEffectSloti
);
474 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTIV
, alGetAuxiliaryEffectSlotiv
);
475 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTF
, alGetAuxiliaryEffectSlotf
);
476 LOAD_PROC(LPALGETAUXILIARYEFFECTSLOTFV
, alGetAuxiliaryEffectSlotfv
);
479 /* Load the reverb into an effect. */
480 effect
= CreateEffect();
487 /* Load the impulse response sound into a buffer. */
488 ir_buffer
= LoadSound(argv
[0]);
491 alDeleteEffects(1, &effect
);
496 /* Create the effect slot object. This is what "plays" an effect on sources
497 * that connect to it.
500 alGenAuxiliaryEffectSlots(1, &slot
);
502 /* Set the impulse response sound buffer on the effect slot. This allows
503 * effects to access it as needed. In this case, convolution reverb uses it
504 * as the filter source. NOTE: Unlike the effect object, the buffer *is*
505 * kept referenced and may not be changed or deleted as long as it's set,
506 * just like with a source. When another buffer is set, or the effect slot
507 * is deleted, the buffer reference is released.
509 * The effect slot's gain is reduced because the impulse responses I've
510 * tested with result in excessively loud reverb. Is that normal? Even with
511 * this, it seems a bit on the loud side.
513 * Also note: unlike standard or EAX reverb, there is no automatic
514 * attenuation of a source's reverb response with distance, so the reverb
515 * will remain full volume regardless of a given sound's distance from the
516 * listener. You can use a send filter to alter a given source's
517 * contribution to reverb.
519 alAuxiliaryEffectSloti(slot
, AL_BUFFER
, (ALint
)ir_buffer
);
520 alAuxiliaryEffectSlotf(slot
, AL_EFFECTSLOT_GAIN
, 1.0f
/ 16.0f
);
521 alAuxiliaryEffectSloti(slot
, AL_EFFECTSLOT_EFFECT
, (ALint
)effect
);
522 assert(alGetError()==AL_NO_ERROR
&& "Failed to set effect slot");
524 /* Create a filter that can silence the dry path. */
526 alGenFilters(1, &filter
);
527 alFilteri(filter
, AL_FILTER_TYPE
, AL_FILTER_LOWPASS
);
528 alFilterf(filter
, AL_LOWPASS_GAIN
, 0.0f
);
530 player
= NewPlayer();
531 /* Connect the player's source to the effect slot. */
532 alSource3i(player
->source
, AL_AUXILIARY_SEND_FILTER
, (ALint
)slot
, 0, AL_FILTER_NULL
);
533 assert(alGetError()==AL_NO_ERROR
&& "Failed to setup sound source");
535 /* Play each file listed on the command line */
536 for(i
= 1;i
< argc
;i
++)
538 const char *namepart
;
542 if(strcasecmp(argv
[i
], "-nodry") == 0)
544 alSourcei(player
->source
, AL_DIRECT_FILTER
, (ALint
)filter
);
547 else if(strcasecmp(argv
[i
], "-dry") == 0)
549 alSourcei(player
->source
, AL_DIRECT_FILTER
, AL_FILTER_NULL
);
554 if(!OpenPlayerFile(player
, argv
[i
]))
557 namepart
= strrchr(argv
[i
], '/');
558 if(namepart
|| (namepart
=strrchr(argv
[i
], '\\')))
563 printf("Playing: %s (%s, %dhz)\n", namepart
, FormatName(player
->format
),
564 player
->sfinfo
.samplerate
);
567 if(!StartPlayer(player
))
569 ClosePlayerFile(player
);
573 while(UpdatePlayer(player
))
574 al_nssleep(10000000);
576 ClosePlayerFile(player
);
580 /* All files done. Delete the player and effect resources, and close down
583 DeletePlayer(player
);
586 alDeleteAuxiliaryEffectSlots(1, &slot
);
587 alDeleteEffects(1, &effect
);
588 alDeleteFilters(1, &filter
);
589 alDeleteBuffers(1, &ir_buffer
);