Use a span for the band-splitter input
[openal-soft.git] / alc / effects / reverb.cpp
blob727c83528562d1f14a764ddb6d72f9a94808c331
1 /**
2 * Ambisonic reverb engine for the OpenAL cross platform audio library
3 * Copyright (C) 2008-2017 by Chris Robinson and Christopher Fitzgerald.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <cstdio>
24 #include <cstdlib>
25 #include <cmath>
27 #include <array>
28 #include <numeric>
29 #include <algorithm>
30 #include <functional>
32 #include "al/auxeffectslot.h"
33 #include "al/listener.h"
34 #include "alcmain.h"
35 #include "alcontext.h"
36 #include "alu.h"
37 #include "bformatdec.h"
38 #include "filters/biquad.h"
39 #include "vector.h"
40 #include "vecmat.h"
42 /* This is a user config option for modifying the overall output of the reverb
43 * effect.
45 ALfloat ReverbBoost = 1.0f;
47 namespace {
49 using namespace std::placeholders;
51 /* Max samples per process iteration. Used to limit the size needed for
52 * temporary buffers. Must be a multiple of 4 for SIMD alignment.
54 constexpr size_t MAX_UPDATE_SAMPLES{256};
56 /* The number of spatialized lines or channels to process. Four channels allows
57 * for a 3D A-Format response. NOTE: This can't be changed without taking care
58 * of the conversion matrices, and a few places where the length arrays are
59 * assumed to have 4 elements.
61 constexpr size_t NUM_LINES{4u};
64 /* The B-Format to A-Format conversion matrix. The arrangement of rows is
65 * deliberately chosen to align the resulting lines to their spatial opposites
66 * (0:above front left <-> 3:above back right, 1:below front right <-> 2:below
67 * back left). It's not quite opposite, since the A-Format results in a
68 * tetrahedron, but it's close enough. Should the model be extended to 8-lines
69 * in the future, true opposites can be used.
71 alignas(16) constexpr ALfloat B2A[NUM_LINES][MAX_AMBI_CHANNELS]{
72 { 0.288675134595f, 0.288675134595f, 0.288675134595f, 0.288675134595f },
73 { 0.288675134595f, -0.288675134595f, -0.288675134595f, 0.288675134595f },
74 { 0.288675134595f, 0.288675134595f, -0.288675134595f, -0.288675134595f },
75 { 0.288675134595f, -0.288675134595f, 0.288675134595f, -0.288675134595f }
78 /* Converts A-Format to B-Format. */
79 alignas(16) constexpr ALfloat A2B[NUM_LINES][NUM_LINES]{
80 { 0.866025403785f, 0.866025403785f, 0.866025403785f, 0.866025403785f },
81 { 0.866025403785f, -0.866025403785f, 0.866025403785f, -0.866025403785f },
82 { 0.866025403785f, -0.866025403785f, -0.866025403785f, 0.866025403785f },
83 { 0.866025403785f, 0.866025403785f, -0.866025403785f, -0.866025403785f }
87 /* The all-pass and delay lines have a variable length dependent on the
88 * effect's density parameter, which helps alter the perceived environment
89 * size. The size-to-density conversion is a cubed scale:
91 * density = min(1.0, pow(size, 3.0) / DENSITY_SCALE);
93 * The line lengths scale linearly with room size, so the inverse density
94 * conversion is needed, taking the cube root of the re-scaled density to
95 * calculate the line length multiplier:
97 * length_mult = max(5.0, cbrt(density*DENSITY_SCALE));
99 * The density scale below will result in a max line multiplier of 50, for an
100 * effective size range of 5m to 50m.
102 constexpr ALfloat DENSITY_SCALE{125000.0f};
104 /* All delay line lengths are specified in seconds.
106 * To approximate early reflections, we break them up into primary (those
107 * arriving from the same direction as the source) and secondary (those
108 * arriving from the opposite direction).
110 * The early taps decorrelate the 4-channel signal to approximate an average
111 * room response for the primary reflections after the initial early delay.
113 * Given an average room dimension (d_a) and the speed of sound (c) we can
114 * calculate the average reflection delay (r_a) regardless of listener and
115 * source positions as:
117 * r_a = d_a / c
118 * c = 343.3
120 * This can extended to finding the average difference (r_d) between the
121 * maximum (r_1) and minimum (r_0) reflection delays:
123 * r_0 = 2 / 3 r_a
124 * = r_a - r_d / 2
125 * = r_d
126 * r_1 = 4 / 3 r_a
127 * = r_a + r_d / 2
128 * = 2 r_d
129 * r_d = 2 / 3 r_a
130 * = r_1 - r_0
132 * As can be determined by integrating the 1D model with a source (s) and
133 * listener (l) positioned across the dimension of length (d_a):
135 * r_d = int_(l=0)^d_a (int_(s=0)^d_a |2 d_a - 2 (l + s)| ds) dl / c
137 * The initial taps (T_(i=0)^N) are then specified by taking a power series
138 * that ranges between r_0 and half of r_1 less r_0:
140 * R_i = 2^(i / (2 N - 1)) r_d
141 * = r_0 + (2^(i / (2 N - 1)) - 1) r_d
142 * = r_0 + T_i
143 * T_i = R_i - r_0
144 * = (2^(i / (2 N - 1)) - 1) r_d
146 * Assuming an average of 1m, we get the following taps:
148 constexpr std::array<ALfloat,NUM_LINES> EARLY_TAP_LENGTHS{{
149 0.0000000e+0f, 2.0213520e-4f, 4.2531060e-4f, 6.7171600e-4f
152 /* The early all-pass filter lengths are based on the early tap lengths:
154 * A_i = R_i / a
156 * Where a is the approximate maximum all-pass cycle limit (20).
158 constexpr std::array<ALfloat,NUM_LINES> EARLY_ALLPASS_LENGTHS{{
159 9.7096800e-5f, 1.0720356e-4f, 1.1836234e-4f, 1.3068260e-4f
162 /* The early delay lines are used to transform the primary reflections into
163 * the secondary reflections. The A-format is arranged in such a way that
164 * the channels/lines are spatially opposite:
166 * C_i is opposite C_(N-i-1)
168 * The delays of the two opposing reflections (R_i and O_i) from a source
169 * anywhere along a particular dimension always sum to twice its full delay:
171 * 2 r_a = R_i + O_i
173 * With that in mind we can determine the delay between the two reflections
174 * and thus specify our early line lengths (L_(i=0)^N) using:
176 * O_i = 2 r_a - R_(N-i-1)
177 * L_i = O_i - R_(N-i-1)
178 * = 2 (r_a - R_(N-i-1))
179 * = 2 (r_a - T_(N-i-1) - r_0)
180 * = 2 r_a (1 - (2 / 3) 2^((N - i - 1) / (2 N - 1)))
182 * Using an average dimension of 1m, we get:
184 constexpr std::array<ALfloat,NUM_LINES> EARLY_LINE_LENGTHS{{
185 5.9850400e-4f, 1.0913150e-3f, 1.5376658e-3f, 1.9419362e-3f
188 /* The late all-pass filter lengths are based on the late line lengths:
190 * A_i = (5 / 3) L_i / r_1
192 constexpr std::array<ALfloat,NUM_LINES> LATE_ALLPASS_LENGTHS{{
193 1.6182800e-4f, 2.0389060e-4f, 2.8159360e-4f, 3.2365600e-4f
196 /* The late lines are used to approximate the decaying cycle of recursive
197 * late reflections.
199 * Splitting the lines in half, we start with the shortest reflection paths
200 * (L_(i=0)^(N/2)):
202 * L_i = 2^(i / (N - 1)) r_d
204 * Then for the opposite (longest) reflection paths (L_(i=N/2)^N):
206 * L_i = 2 r_a - L_(i-N/2)
207 * = 2 r_a - 2^((i - N / 2) / (N - 1)) r_d
209 * For our 1m average room, we get:
211 constexpr std::array<ALfloat,NUM_LINES> LATE_LINE_LENGTHS{{
212 1.9419362e-3f, 2.4466860e-3f, 3.3791220e-3f, 3.8838720e-3f
216 using ReverbUpdateLine = std::array<float,MAX_UPDATE_SAMPLES>;
218 struct DelayLineI {
219 /* The delay lines use interleaved samples, with the lengths being powers
220 * of 2 to allow the use of bit-masking instead of a modulus for wrapping.
222 size_t Mask{0u};
223 union {
224 uintptr_t LineOffset{0u};
225 std::array<float,NUM_LINES> *Line;
228 /* Given the allocated sample buffer, this function updates each delay line
229 * offset.
231 void realizeLineOffset(std::array<float,NUM_LINES> *sampleBuffer) noexcept
232 { Line = sampleBuffer + LineOffset; }
234 /* Calculate the length of a delay line and store its mask and offset. */
235 ALuint calcLineLength(const ALfloat length, const uintptr_t offset, const ALfloat frequency,
236 const ALuint extra)
238 /* All line lengths are powers of 2, calculated from their lengths in
239 * seconds, rounded up.
241 ALuint samples{float2uint(std::ceil(length*frequency))};
242 samples = NextPowerOf2(samples + extra);
244 /* All lines share a single sample buffer. */
245 Mask = samples - 1;
246 LineOffset = offset;
248 /* Return the sample count for accumulation. */
249 return samples;
252 void write(size_t offset, const size_t c, const ALfloat *RESTRICT in, const size_t count) const noexcept
254 ASSUME(count > 0);
255 for(size_t i{0u};i < count;)
257 offset &= Mask;
258 size_t td{minz(Mask+1 - offset, count - i)};
259 do {
260 Line[offset++][c] = in[i++];
261 } while(--td);
266 struct VecAllpass {
267 DelayLineI Delay;
268 ALfloat Coeff{0.0f};
269 size_t Offset[NUM_LINES][2]{};
271 void processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
272 const ALfloat xCoeff, const ALfloat yCoeff, ALfloat fadeCount, const ALfloat fadeStep,
273 const size_t todo);
274 void processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
275 const ALfloat xCoeff, const ALfloat yCoeff, const size_t todo);
278 struct T60Filter {
279 /* Two filters are used to adjust the signal. One to control the low
280 * frequencies, and one to control the high frequencies.
282 ALfloat MidGain[2]{0.0f, 0.0f};
283 BiquadFilter HFFilter, LFFilter;
285 void calcCoeffs(const ALfloat length, const ALfloat lfDecayTime, const ALfloat mfDecayTime,
286 const ALfloat hfDecayTime, const ALfloat lf0norm, const ALfloat hf0norm);
288 /* Applies the two T60 damping filter sections. */
289 void process(const al::span<float> samples)
291 HFFilter.process(samples, samples.begin());
292 LFFilter.process(samples, samples.begin());
296 struct EarlyReflections {
297 /* A Gerzon vector all-pass filter is used to simulate initial diffusion.
298 * The spread from this filter also helps smooth out the reverb tail.
300 VecAllpass VecAp;
302 /* An echo line is used to complete the second half of the early
303 * reflections.
305 DelayLineI Delay;
306 size_t Offset[NUM_LINES][2]{};
307 ALfloat Coeff[NUM_LINES][2]{};
309 /* The gain for each output channel based on 3D panning. */
310 ALfloat CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
311 ALfloat PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
313 void updateLines(const ALfloat density, const ALfloat diffusion, const ALfloat decayTime,
314 const ALfloat frequency);
317 struct LateReverb {
318 /* A recursive delay line is used fill in the reverb tail. */
319 DelayLineI Delay;
320 size_t Offset[NUM_LINES][2]{};
322 /* Attenuation to compensate for the modal density and decay rate of the
323 * late lines.
325 ALfloat DensityGain[2]{0.0f, 0.0f};
327 /* T60 decay filters are used to simulate absorption. */
328 T60Filter T60[NUM_LINES];
330 /* A Gerzon vector all-pass filter is used to simulate diffusion. */
331 VecAllpass VecAp;
333 /* The gain for each output channel based on 3D panning. */
334 ALfloat CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
335 ALfloat PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
337 void updateLines(const ALfloat density, const ALfloat diffusion, const ALfloat lfDecayTime,
338 const ALfloat mfDecayTime, const ALfloat hfDecayTime, const ALfloat lf0norm,
339 const ALfloat hf0norm, const ALfloat frequency);
342 struct ReverbState final : public EffectState {
343 /* All delay lines are allocated as a single buffer to reduce memory
344 * fragmentation and management code.
346 al::vector<std::array<float,NUM_LINES>,16> mSampleBuffer;
348 struct {
349 /* Calculated parameters which indicate if cross-fading is needed after
350 * an update.
352 ALfloat Density{AL_EAXREVERB_DEFAULT_DENSITY};
353 ALfloat Diffusion{AL_EAXREVERB_DEFAULT_DIFFUSION};
354 ALfloat DecayTime{AL_EAXREVERB_DEFAULT_DECAY_TIME};
355 ALfloat HFDecayTime{AL_EAXREVERB_DEFAULT_DECAY_HFRATIO * AL_EAXREVERB_DEFAULT_DECAY_TIME};
356 ALfloat LFDecayTime{AL_EAXREVERB_DEFAULT_DECAY_LFRATIO * AL_EAXREVERB_DEFAULT_DECAY_TIME};
357 ALfloat HFReference{AL_EAXREVERB_DEFAULT_HFREFERENCE};
358 ALfloat LFReference{AL_EAXREVERB_DEFAULT_LFREFERENCE};
359 } mParams;
361 /* Master effect filters */
362 struct {
363 BiquadFilter Lp;
364 BiquadFilter Hp;
365 } mFilter[NUM_LINES];
367 /* Core delay line (early reflections and late reverb tap from this). */
368 DelayLineI mDelay;
370 /* Tap points for early reflection delay. */
371 size_t mEarlyDelayTap[NUM_LINES][2]{};
372 ALfloat mEarlyDelayCoeff[NUM_LINES][2]{};
374 /* Tap points for late reverb feed and delay. */
375 size_t mLateFeedTap{};
376 size_t mLateDelayTap[NUM_LINES][2]{};
378 /* Coefficients for the all-pass and line scattering matrices. */
379 ALfloat mMixX{0.0f};
380 ALfloat mMixY{0.0f};
382 EarlyReflections mEarly;
384 LateReverb mLate;
386 bool mDoFading{};
388 /* Maximum number of samples to process at once. */
389 size_t mMaxUpdate[2]{MAX_UPDATE_SAMPLES, MAX_UPDATE_SAMPLES};
391 /* The current write offset for all delay lines. */
392 size_t mOffset{};
394 /* Temporary storage used when processing. */
395 union {
396 alignas(16) FloatBufferLine mTempLine{};
397 alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mTempSamples;
399 alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mEarlySamples{};
400 alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mLateSamples{};
402 using MixOutT = void (ReverbState::*)(const al::span<FloatBufferLine> samplesOut,
403 const size_t counter, const size_t offset, const size_t todo);
405 MixOutT mMixOut{&ReverbState::MixOutPlain};
406 std::array<ALfloat,MAX_AMBI_ORDER+1> mOrderScales{};
407 std::array<std::array<BandSplitter,NUM_LINES>,2> mAmbiSplitter;
410 void MixOutPlain(const al::span<FloatBufferLine> samplesOut, const size_t counter,
411 const size_t offset, const size_t todo)
413 ASSUME(todo > 0);
415 /* Convert back to B-Format, and mix the results to output. */
416 const al::span<float> tmpspan{mTempLine.data(), todo};
417 for(size_t c{0u};c < NUM_LINES;c++)
419 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
420 MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mEarlySamples[0].data(),
421 mEarlySamples[0].size());
422 MixSamples(tmpspan, samplesOut, mEarly.CurrentGain[c], mEarly.PanGain[c], counter,
423 offset);
425 for(size_t c{0u};c < NUM_LINES;c++)
427 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
428 MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mLateSamples[0].data(),
429 mLateSamples[0].size());
430 MixSamples(tmpspan, samplesOut, mLate.CurrentGain[c], mLate.PanGain[c], counter,
431 offset);
435 void MixOutAmbiUp(const al::span<FloatBufferLine> samplesOut, const size_t counter,
436 const size_t offset, const size_t todo)
438 ASSUME(todo > 0);
440 const al::span<float> tmpspan{mTempLine.data(), todo};
441 for(size_t c{0u};c < NUM_LINES;c++)
443 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
444 MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mEarlySamples[0].data(),
445 mEarlySamples[0].size());
447 /* Apply scaling to the B-Format's HF response to "upsample" it to
448 * higher-order output.
450 const ALfloat hfscale{(c==0) ? mOrderScales[0] : mOrderScales[1]};
451 mAmbiSplitter[0][c].applyHfScale(tmpspan, hfscale);
453 MixSamples(tmpspan, samplesOut, mEarly.CurrentGain[c], mEarly.PanGain[c], counter,
454 offset);
456 for(size_t c{0u};c < NUM_LINES;c++)
458 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
459 MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mLateSamples[0].data(),
460 mLateSamples[0].size());
462 const ALfloat hfscale{(c==0) ? mOrderScales[0] : mOrderScales[1]};
463 mAmbiSplitter[1][c].applyHfScale(tmpspan, hfscale);
465 MixSamples(tmpspan, samplesOut, mLate.CurrentGain[c], mLate.PanGain[c], counter,
466 offset);
470 bool allocLines(const ALfloat frequency);
472 void updateDelayLine(const ALfloat earlyDelay, const ALfloat lateDelay, const ALfloat density,
473 const ALfloat decayTime, const ALfloat frequency);
474 void update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan,
475 const ALfloat earlyGain, const ALfloat lateGain, const EffectTarget &target);
477 void earlyUnfaded(const size_t offset, const size_t todo);
478 void earlyFaded(const size_t offset, const size_t todo, const ALfloat fade,
479 const ALfloat fadeStep);
481 void lateUnfaded(const size_t offset, const size_t todo);
482 void lateFaded(const size_t offset, const size_t todo, const ALfloat fade,
483 const ALfloat fadeStep);
485 ALboolean deviceUpdate(const ALCdevice *device) override;
486 void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
487 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
489 DEF_NEWDEL(ReverbState)
492 /**************************************
493 * Device Update *
494 **************************************/
496 inline ALfloat CalcDelayLengthMult(ALfloat density)
497 { return maxf(5.0f, std::cbrt(density*DENSITY_SCALE)); }
499 /* Calculates the delay line metrics and allocates the shared sample buffer
500 * for all lines given the sample rate (frequency). If an allocation failure
501 * occurs, it returns AL_FALSE.
503 bool ReverbState::allocLines(const ALfloat frequency)
505 /* All delay line lengths are calculated to accomodate the full range of
506 * lengths given their respective paramters.
508 size_t totalSamples{0u};
510 /* Multiplier for the maximum density value, i.e. density=1, which is
511 * actually the least density...
513 ALfloat multiplier{CalcDelayLengthMult(AL_EAXREVERB_MAX_DENSITY)};
515 /* The main delay length includes the maximum early reflection delay, the
516 * largest early tap width, the maximum late reverb delay, and the
517 * largest late tap width. Finally, it must also be extended by the
518 * update size (BUFFERSIZE) for block processing.
520 ALfloat length{AL_EAXREVERB_MAX_REFLECTIONS_DELAY + EARLY_TAP_LENGTHS.back()*multiplier +
521 AL_EAXREVERB_MAX_LATE_REVERB_DELAY +
522 (LATE_LINE_LENGTHS.back() - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*multiplier};
523 totalSamples += mDelay.calcLineLength(length, totalSamples, frequency, BUFFERSIZE);
525 /* The early vector all-pass line. */
526 length = EARLY_ALLPASS_LENGTHS.back() * multiplier;
527 totalSamples += mEarly.VecAp.Delay.calcLineLength(length, totalSamples, frequency, 0);
529 /* The early reflection line. */
530 length = EARLY_LINE_LENGTHS.back() * multiplier;
531 totalSamples += mEarly.Delay.calcLineLength(length, totalSamples, frequency, 0);
533 /* The late vector all-pass line. */
534 length = LATE_ALLPASS_LENGTHS.back() * multiplier;
535 totalSamples += mLate.VecAp.Delay.calcLineLength(length, totalSamples, frequency, 0);
537 /* The late delay lines are calculated from the largest maximum density
538 * line length.
540 length = LATE_LINE_LENGTHS.back() * multiplier;
541 totalSamples += mLate.Delay.calcLineLength(length, totalSamples, frequency, 0);
543 if(totalSamples != mSampleBuffer.size())
545 mSampleBuffer.resize(totalSamples);
546 mSampleBuffer.shrink_to_fit();
549 /* Clear the sample buffer. */
550 std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), std::array<float,NUM_LINES>{});
552 /* Update all delays to reflect the new sample buffer. */
553 mDelay.realizeLineOffset(mSampleBuffer.data());
554 mEarly.VecAp.Delay.realizeLineOffset(mSampleBuffer.data());
555 mEarly.Delay.realizeLineOffset(mSampleBuffer.data());
556 mLate.VecAp.Delay.realizeLineOffset(mSampleBuffer.data());
557 mLate.Delay.realizeLineOffset(mSampleBuffer.data());
559 return true;
562 ALboolean ReverbState::deviceUpdate(const ALCdevice *device)
564 const auto frequency = static_cast<ALfloat>(device->Frequency);
566 /* Allocate the delay lines. */
567 if(!allocLines(frequency))
568 return AL_FALSE;
570 const ALfloat multiplier{CalcDelayLengthMult(AL_EAXREVERB_MAX_DENSITY)};
572 /* The late feed taps are set a fixed position past the latest delay tap. */
573 mLateFeedTap = float2uint(
574 (AL_EAXREVERB_MAX_REFLECTIONS_DELAY + EARLY_TAP_LENGTHS.back()*multiplier) * frequency);
576 /* Clear filters and gain coefficients since the delay lines were all just
577 * cleared (if not reallocated).
579 for(auto &filter : mFilter)
581 filter.Lp.clear();
582 filter.Hp.clear();
585 for(auto &coeff : mEarlyDelayCoeff)
586 std::fill(std::begin(coeff), std::end(coeff), 0.0f);
587 for(auto &coeff : mEarly.Coeff)
588 std::fill(std::begin(coeff), std::end(coeff), 0.0f);
590 mLate.DensityGain[0] = 0.0f;
591 mLate.DensityGain[1] = 0.0f;
592 for(auto &t60 : mLate.T60)
594 t60.MidGain[0] = 0.0f;
595 t60.MidGain[1] = 0.0f;
596 t60.HFFilter.clear();
597 t60.LFFilter.clear();
600 for(auto &gains : mEarly.CurrentGain)
601 std::fill(std::begin(gains), std::end(gains), 0.0f);
602 for(auto &gains : mEarly.PanGain)
603 std::fill(std::begin(gains), std::end(gains), 0.0f);
604 for(auto &gains : mLate.CurrentGain)
605 std::fill(std::begin(gains), std::end(gains), 0.0f);
606 for(auto &gains : mLate.PanGain)
607 std::fill(std::begin(gains), std::end(gains), 0.0f);
609 /* Reset fading and offset base. */
610 mDoFading = true;
611 std::fill(std::begin(mMaxUpdate), std::end(mMaxUpdate), MAX_UPDATE_SAMPLES);
612 mOffset = 0;
614 if(device->mAmbiOrder > 1)
616 mMixOut = &ReverbState::MixOutAmbiUp;
617 mOrderScales = BFormatDec::GetHFOrderScales(1, device->mAmbiOrder);
619 else
621 mMixOut = &ReverbState::MixOutPlain;
622 mOrderScales.fill(1.0f);
624 mAmbiSplitter[0][0].init(400.0f / frequency);
625 std::fill(mAmbiSplitter[0].begin()+1, mAmbiSplitter[0].end(), mAmbiSplitter[0][0]);
626 std::fill(mAmbiSplitter[1].begin(), mAmbiSplitter[1].end(), mAmbiSplitter[0][0]);
628 return AL_TRUE;
631 /**************************************
632 * Effect Update *
633 **************************************/
635 /* Calculate a decay coefficient given the length of each cycle and the time
636 * until the decay reaches -60 dB.
638 inline ALfloat CalcDecayCoeff(const ALfloat length, const ALfloat decayTime)
639 { return std::pow(REVERB_DECAY_GAIN, length/decayTime); }
641 /* Calculate a decay length from a coefficient and the time until the decay
642 * reaches -60 dB.
644 inline ALfloat CalcDecayLength(const ALfloat coeff, const ALfloat decayTime)
645 { return std::log10(coeff) * decayTime / std::log10(REVERB_DECAY_GAIN); }
647 /* Calculate an attenuation to be applied to the input of any echo models to
648 * compensate for modal density and decay time.
650 inline ALfloat CalcDensityGain(const ALfloat a)
652 /* The energy of a signal can be obtained by finding the area under the
653 * squared signal. This takes the form of Sum(x_n^2), where x is the
654 * amplitude for the sample n.
656 * Decaying feedback matches exponential decay of the form Sum(a^n),
657 * where a is the attenuation coefficient, and n is the sample. The area
658 * under this decay curve can be calculated as: 1 / (1 - a).
660 * Modifying the above equation to find the area under the squared curve
661 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
662 * calculated by inverting the square root of this approximation,
663 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
665 return std::sqrt(1.0f - a*a);
668 /* Calculate the scattering matrix coefficients given a diffusion factor. */
669 inline ALvoid CalcMatrixCoeffs(const ALfloat diffusion, ALfloat *x, ALfloat *y)
671 /* The matrix is of order 4, so n is sqrt(4 - 1). */
672 ALfloat n{std::sqrt(3.0f)};
673 ALfloat t{diffusion * std::atan(n)};
675 /* Calculate the first mixing matrix coefficient. */
676 *x = std::cos(t);
677 /* Calculate the second mixing matrix coefficient. */
678 *y = std::sin(t) / n;
681 /* Calculate the limited HF ratio for use with the late reverb low-pass
682 * filters.
684 ALfloat CalcLimitedHfRatio(const ALfloat hfRatio, const ALfloat airAbsorptionGainHF,
685 const ALfloat decayTime)
687 /* Find the attenuation due to air absorption in dB (converting delay
688 * time to meters using the speed of sound). Then reversing the decay
689 * equation, solve for HF ratio. The delay length is cancelled out of
690 * the equation, so it can be calculated once for all lines.
692 ALfloat limitRatio{1.0f /
693 (CalcDecayLength(airAbsorptionGainHF, decayTime) * SPEEDOFSOUNDMETRESPERSEC)};
695 /* Using the limit calculated above, apply the upper bound to the HF ratio.
697 return minf(limitRatio, hfRatio);
701 /* Calculates the 3-band T60 damping coefficients for a particular delay line
702 * of specified length, using a combination of two shelf filter sections given
703 * decay times for each band split at two reference frequencies.
705 void T60Filter::calcCoeffs(const ALfloat length, const ALfloat lfDecayTime,
706 const ALfloat mfDecayTime, const ALfloat hfDecayTime, const ALfloat lf0norm,
707 const ALfloat hf0norm)
709 const float mfGain{CalcDecayCoeff(length, mfDecayTime)};
710 const float lfGain{CalcDecayCoeff(length, lfDecayTime) / mfGain};
711 const float hfGain{CalcDecayCoeff(length, hfDecayTime) / mfGain};
713 MidGain[1] = mfGain;
714 LFFilter.setParamsFromSlope(BiquadType::LowShelf, lf0norm, lfGain, 1.0f);
715 HFFilter.setParamsFromSlope(BiquadType::HighShelf, hf0norm, hfGain, 1.0f);
718 /* Update the early reflection line lengths and gain coefficients. */
719 void EarlyReflections::updateLines(const ALfloat density, const ALfloat diffusion,
720 const ALfloat decayTime, const ALfloat frequency)
722 const ALfloat multiplier{CalcDelayLengthMult(density)};
724 /* Calculate the all-pass feed-back/forward coefficient. */
725 VecAp.Coeff = std::sqrt(0.5f) * std::pow(diffusion, 2.0f);
727 for(size_t i{0u};i < NUM_LINES;i++)
729 /* Calculate the length (in seconds) of each all-pass line. */
730 ALfloat length{EARLY_ALLPASS_LENGTHS[i] * multiplier};
732 /* Calculate the delay offset for each all-pass line. */
733 VecAp.Offset[i][1] = float2uint(length * frequency);
735 /* Calculate the length (in seconds) of each delay line. */
736 length = EARLY_LINE_LENGTHS[i] * multiplier;
738 /* Calculate the delay offset for each delay line. */
739 Offset[i][1] = float2uint(length * frequency);
741 /* Calculate the gain (coefficient) for each line. */
742 Coeff[i][1] = CalcDecayCoeff(length, decayTime);
746 /* Update the late reverb line lengths and T60 coefficients. */
747 void LateReverb::updateLines(const ALfloat density, const ALfloat diffusion,
748 const ALfloat lfDecayTime, const ALfloat mfDecayTime, const ALfloat hfDecayTime,
749 const ALfloat lf0norm, const ALfloat hf0norm, const ALfloat frequency)
751 /* Scaling factor to convert the normalized reference frequencies from
752 * representing 0...freq to 0...max_reference.
754 const ALfloat norm_weight_factor{frequency / AL_EAXREVERB_MAX_HFREFERENCE};
756 const ALfloat late_allpass_avg{
757 std::accumulate(LATE_ALLPASS_LENGTHS.begin(), LATE_ALLPASS_LENGTHS.end(), 0.0f) /
758 float{NUM_LINES}};
760 /* To compensate for changes in modal density and decay time of the late
761 * reverb signal, the input is attenuated based on the maximal energy of
762 * the outgoing signal. This approximation is used to keep the apparent
763 * energy of the signal equal for all ranges of density and decay time.
765 * The average length of the delay lines is used to calculate the
766 * attenuation coefficient.
768 const ALfloat multiplier{CalcDelayLengthMult(density)};
769 ALfloat length{std::accumulate(LATE_LINE_LENGTHS.begin(), LATE_LINE_LENGTHS.end(), 0.0f) /
770 float{NUM_LINES} * multiplier};
771 length += late_allpass_avg * multiplier;
772 /* The density gain calculation uses an average decay time weighted by
773 * approximate bandwidth. This attempts to compensate for losses of energy
774 * that reduce decay time due to scattering into highly attenuated bands.
776 const ALfloat decayTimeWeighted{
777 (lf0norm*norm_weight_factor)*lfDecayTime +
778 (hf0norm*norm_weight_factor - lf0norm*norm_weight_factor)*mfDecayTime +
779 (1.0f - hf0norm*norm_weight_factor)*hfDecayTime};
780 DensityGain[1] = CalcDensityGain(CalcDecayCoeff(length, decayTimeWeighted));
782 /* Calculate the all-pass feed-back/forward coefficient. */
783 VecAp.Coeff = std::sqrt(0.5f) * std::pow(diffusion, 2.0f);
785 for(size_t i{0u};i < NUM_LINES;i++)
787 /* Calculate the length (in seconds) of each all-pass line. */
788 length = LATE_ALLPASS_LENGTHS[i] * multiplier;
790 /* Calculate the delay offset for each all-pass line. */
791 VecAp.Offset[i][1] = float2uint(length * frequency);
793 /* Calculate the length (in seconds) of each delay line. */
794 length = LATE_LINE_LENGTHS[i] * multiplier;
796 /* Calculate the delay offset for each delay line. */
797 Offset[i][1] = float2uint(length*frequency + 0.5f);
799 /* Approximate the absorption that the vector all-pass would exhibit
800 * given the current diffusion so we don't have to process a full T60
801 * filter for each of its four lines.
803 length += lerp(LATE_ALLPASS_LENGTHS[i], late_allpass_avg, diffusion) * multiplier;
805 /* Calculate the T60 damping coefficients for each line. */
806 T60[i].calcCoeffs(length, lfDecayTime, mfDecayTime, hfDecayTime, lf0norm, hf0norm);
811 /* Update the offsets for the main effect delay line. */
812 void ReverbState::updateDelayLine(const ALfloat earlyDelay, const ALfloat lateDelay,
813 const ALfloat density, const ALfloat decayTime, const ALfloat frequency)
815 const ALfloat multiplier{CalcDelayLengthMult(density)};
817 /* Early reflection taps are decorrelated by means of an average room
818 * reflection approximation described above the definition of the taps.
819 * This approximation is linear and so the above density multiplier can
820 * be applied to adjust the width of the taps. A single-band decay
821 * coefficient is applied to simulate initial attenuation and absorption.
823 * Late reverb taps are based on the late line lengths to allow a zero-
824 * delay path and offsets that would continue the propagation naturally
825 * into the late lines.
827 for(size_t i{0u};i < NUM_LINES;i++)
829 ALfloat length{earlyDelay + EARLY_TAP_LENGTHS[i]*multiplier};
830 mEarlyDelayTap[i][1] = float2uint(length * frequency);
832 length = EARLY_TAP_LENGTHS[i]*multiplier;
833 mEarlyDelayCoeff[i][1] = CalcDecayCoeff(length, decayTime);
835 length = (LATE_LINE_LENGTHS[i] - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*multiplier +
836 lateDelay;
837 mLateDelayTap[i][1] = mLateFeedTap + float2uint(length * frequency);
841 /* Creates a transform matrix given a reverb vector. The vector pans the reverb
842 * reflections toward the given direction, using its magnitude (up to 1) as a
843 * focal strength. This function results in a B-Format transformation matrix
844 * that spatially focuses the signal in the desired direction.
846 alu::Matrix GetTransformFromVector(const ALfloat *vec)
848 constexpr float sqrt_3{1.73205080756887719318f};
850 /* Normalize the panning vector according to the N3D scale, which has an
851 * extra sqrt(3) term on the directional components. Converting from OpenAL
852 * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however
853 * that the reverb panning vectors use left-handed coordinates, unlike the
854 * rest of OpenAL which use right-handed. This is fixed by negating Z,
855 * which cancels out with the B-Format Z negation.
857 ALfloat norm[3];
858 ALfloat mag{std::sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])};
859 if(mag > 1.0f)
861 norm[0] = vec[0] / mag * -sqrt_3;
862 norm[1] = vec[1] / mag * sqrt_3;
863 norm[2] = vec[2] / mag * sqrt_3;
864 mag = 1.0f;
866 else
868 /* If the magnitude is less than or equal to 1, just apply the sqrt(3)
869 * term. There's no need to renormalize the magnitude since it would
870 * just be reapplied in the matrix.
872 norm[0] = vec[0] * -sqrt_3;
873 norm[1] = vec[1] * sqrt_3;
874 norm[2] = vec[2] * sqrt_3;
877 return alu::Matrix{
878 1.0f, 0.0f, 0.0f, 0.0f,
879 norm[0], 1.0f-mag, 0.0f, 0.0f,
880 norm[1], 0.0f, 1.0f-mag, 0.0f,
881 norm[2], 0.0f, 0.0f, 1.0f-mag
885 /* Update the early and late 3D panning gains. */
886 void ReverbState::update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan,
887 const ALfloat earlyGain, const ALfloat lateGain, const EffectTarget &target)
889 /* Create matrices that transform a B-Format signal according to the
890 * panning vectors.
892 const alu::Matrix earlymat{GetTransformFromVector(ReflectionsPan)};
893 const alu::Matrix latemat{GetTransformFromVector(LateReverbPan)};
895 mOutTarget = target.Main->Buffer;
896 for(size_t i{0u};i < NUM_LINES;i++)
898 const ALfloat coeffs[MAX_AMBI_CHANNELS]{earlymat[0][i], earlymat[1][i], earlymat[2][i],
899 earlymat[3][i]};
900 ComputePanGains(target.Main, coeffs, earlyGain, mEarly.PanGain[i]);
902 for(size_t i{0u};i < NUM_LINES;i++)
904 const ALfloat coeffs[MAX_AMBI_CHANNELS]{latemat[0][i], latemat[1][i], latemat[2][i],
905 latemat[3][i]};
906 ComputePanGains(target.Main, coeffs, lateGain, mLate.PanGain[i]);
910 void ReverbState::update(const ALCcontext *Context, const ALeffectslot *Slot, const EffectProps *props, const EffectTarget target)
912 const ALCdevice *Device{Context->mDevice.get()};
913 const auto frequency = static_cast<ALfloat>(Device->Frequency);
915 /* Calculate the master filters */
916 float hf0norm{minf(props->Reverb.HFReference/frequency, 0.49f)};
917 mFilter[0].Lp.setParamsFromSlope(BiquadType::HighShelf, hf0norm, props->Reverb.GainHF, 1.0f);
918 float lf0norm{minf(props->Reverb.LFReference/frequency, 0.49f)};
919 mFilter[0].Hp.setParamsFromSlope(BiquadType::LowShelf, lf0norm, props->Reverb.GainLF, 1.0f);
920 for(size_t i{1u};i < NUM_LINES;i++)
922 mFilter[i].Lp.copyParamsFrom(mFilter[0].Lp);
923 mFilter[i].Hp.copyParamsFrom(mFilter[0].Hp);
926 /* Update the main effect delay and associated taps. */
927 updateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
928 props->Reverb.Density, props->Reverb.DecayTime, frequency);
930 /* Update the early lines. */
931 mEarly.updateLines(props->Reverb.Density, props->Reverb.Diffusion, props->Reverb.DecayTime,
932 frequency);
934 /* Get the mixing matrix coefficients. */
935 CalcMatrixCoeffs(props->Reverb.Diffusion, &mMixX, &mMixY);
937 /* If the HF limit parameter is flagged, calculate an appropriate limit
938 * based on the air absorption parameter.
940 ALfloat hfRatio{props->Reverb.DecayHFRatio};
941 if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
942 hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
943 props->Reverb.DecayTime);
945 /* Calculate the LF/HF decay times. */
946 const ALfloat lfDecayTime{clampf(props->Reverb.DecayTime * props->Reverb.DecayLFRatio,
947 AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME)};
948 const ALfloat hfDecayTime{clampf(props->Reverb.DecayTime * hfRatio,
949 AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME)};
951 /* Update the late lines. */
952 mLate.updateLines(props->Reverb.Density, props->Reverb.Diffusion, lfDecayTime,
953 props->Reverb.DecayTime, hfDecayTime, lf0norm, hf0norm, frequency);
955 /* Update early and late 3D panning. */
956 const ALfloat gain{props->Reverb.Gain * Slot->Params.Gain * ReverbBoost};
957 update3DPanning(props->Reverb.ReflectionsPan, props->Reverb.LateReverbPan,
958 props->Reverb.ReflectionsGain*gain, props->Reverb.LateReverbGain*gain, target);
960 /* Calculate the max update size from the smallest relevant delay. */
961 mMaxUpdate[1] = minz(MAX_UPDATE_SAMPLES, minz(mEarly.Offset[0][1], mLate.Offset[0][1]));
963 /* Determine if delay-line cross-fading is required. Density is essentially
964 * a master control for the feedback delays, so changes the offsets of many
965 * delay lines.
967 mDoFading |= (mParams.Density != props->Reverb.Density ||
968 /* Diffusion and decay times influences the decay rate (gain) of the
969 * late reverb T60 filter.
971 mParams.Diffusion != props->Reverb.Diffusion ||
972 mParams.DecayTime != props->Reverb.DecayTime ||
973 mParams.HFDecayTime != hfDecayTime ||
974 mParams.LFDecayTime != lfDecayTime ||
975 /* HF/LF References control the weighting used to calculate the density
976 * gain.
978 mParams.HFReference != props->Reverb.HFReference ||
979 mParams.LFReference != props->Reverb.LFReference);
980 if(mDoFading)
982 mParams.Density = props->Reverb.Density;
983 mParams.Diffusion = props->Reverb.Diffusion;
984 mParams.DecayTime = props->Reverb.DecayTime;
985 mParams.HFDecayTime = hfDecayTime;
986 mParams.LFDecayTime = lfDecayTime;
987 mParams.HFReference = props->Reverb.HFReference;
988 mParams.LFReference = props->Reverb.LFReference;
993 /**************************************
994 * Effect Processing *
995 **************************************/
997 /* Applies a scattering matrix to the 4-line (vector) input. This is used
998 * for both the below vector all-pass model and to perform modal feed-back
999 * delay network (FDN) mixing.
1001 * The matrix is derived from a skew-symmetric matrix to form a 4D rotation
1002 * matrix with a single unitary rotational parameter:
1004 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1005 * [ -a, d, c, -b ]
1006 * [ -b, -c, d, a ]
1007 * [ -c, b, -a, d ]
1009 * The rotation is constructed from the effect's diffusion parameter,
1010 * yielding:
1012 * 1 = x^2 + 3 y^2
1014 * Where a, b, and c are the coefficient y with differing signs, and d is the
1015 * coefficient x. The final matrix is thus:
1017 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1018 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1019 * [ y, -y, x, y ] x = cos(t)
1020 * [ -y, -y, -y, x ] y = sin(t) / n
1022 * Any square orthogonal matrix with an order that is a power of two will
1023 * work (where ^T is transpose, ^-1 is inverse):
1025 * M^T = M^-1
1027 * Using that knowledge, finding an appropriate matrix can be accomplished
1028 * naively by searching all combinations of:
1030 * M = D + S - S^T
1032 * Where D is a diagonal matrix (of x), and S is a triangular matrix (of y)
1033 * whose combination of signs are being iterated.
1035 inline auto VectorPartialScatter(const std::array<float,NUM_LINES> &RESTRICT in,
1036 const ALfloat xCoeff, const ALfloat yCoeff) -> std::array<float,NUM_LINES>
1038 std::array<float,NUM_LINES> out;
1039 out[0] = xCoeff*in[0] + yCoeff*( in[1] + -in[2] + in[3]);
1040 out[1] = xCoeff*in[1] + yCoeff*(-in[0] + in[2] + in[3]);
1041 out[2] = xCoeff*in[2] + yCoeff*( in[0] + -in[1] + in[3]);
1042 out[3] = xCoeff*in[3] + yCoeff*(-in[0] + -in[1] + -in[2] );
1043 return out;
1046 /* Utilizes the above, but reverses the input channels. */
1047 void VectorScatterRevDelayIn(const DelayLineI delay, size_t offset, const ALfloat xCoeff,
1048 const ALfloat yCoeff, const al::span<const ReverbUpdateLine,NUM_LINES> in, const size_t count)
1050 ASSUME(count > 0);
1052 for(size_t i{0u};i < count;)
1054 offset &= delay.Mask;
1055 size_t td{minz(delay.Mask+1 - offset, count-i)};
1056 do {
1057 std::array<float,NUM_LINES> f;
1058 for(size_t j{0u};j < NUM_LINES;j++)
1059 f[NUM_LINES-1-j] = in[j][i];
1060 ++i;
1062 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1063 } while(--td);
1067 /* This applies a Gerzon multiple-in/multiple-out (MIMO) vector all-pass
1068 * filter to the 4-line input.
1070 * It works by vectorizing a regular all-pass filter and replacing the delay
1071 * element with a scattering matrix (like the one above) and a diagonal
1072 * matrix of delay elements.
1074 * Two static specializations are used for transitional (cross-faded) delay
1075 * line processing and non-transitional processing.
1077 void VecAllpass::processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
1078 const ALfloat xCoeff, const ALfloat yCoeff, const size_t todo)
1080 const DelayLineI delay{Delay};
1081 const ALfloat feedCoeff{Coeff};
1083 ASSUME(todo > 0);
1085 size_t vap_offset[NUM_LINES];
1086 for(size_t j{0u};j < NUM_LINES;j++)
1087 vap_offset[j] = offset - Offset[j][0];
1088 for(size_t i{0u};i < todo;)
1090 for(size_t j{0u};j < NUM_LINES;j++)
1091 vap_offset[j] &= delay.Mask;
1092 offset &= delay.Mask;
1094 size_t maxoff{offset};
1095 for(size_t j{0u};j < NUM_LINES;j++)
1096 maxoff = maxz(maxoff, vap_offset[j]);
1097 size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
1099 do {
1100 std::array<float,NUM_LINES> f;
1101 for(size_t j{0u};j < NUM_LINES;j++)
1103 const ALfloat input{samples[j][i]};
1104 const ALfloat out{delay.Line[vap_offset[j]++][j] - feedCoeff*input};
1105 f[j] = input + feedCoeff*out;
1107 samples[j][i] = out;
1109 ++i;
1111 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1112 } while(--td);
1115 void VecAllpass::processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
1116 const ALfloat xCoeff, const ALfloat yCoeff, ALfloat fadeCount, const ALfloat fadeStep,
1117 const size_t todo)
1119 const DelayLineI delay{Delay};
1120 const ALfloat feedCoeff{Coeff};
1122 ASSUME(todo > 0);
1124 size_t vap_offset[NUM_LINES][2];
1125 for(size_t j{0u};j < NUM_LINES;j++)
1127 vap_offset[j][0] = offset - Offset[j][0];
1128 vap_offset[j][1] = offset - Offset[j][1];
1130 for(size_t i{0u};i < todo;)
1132 for(size_t j{0u};j < NUM_LINES;j++)
1134 vap_offset[j][0] &= delay.Mask;
1135 vap_offset[j][1] &= delay.Mask;
1137 offset &= delay.Mask;
1139 size_t maxoff{offset};
1140 for(size_t j{0u};j < NUM_LINES;j++)
1141 maxoff = maxz(maxoff, maxz(vap_offset[j][0], vap_offset[j][1]));
1142 size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
1144 do {
1145 fadeCount += 1.0f;
1146 const float fade{fadeCount * fadeStep};
1148 std::array<float,NUM_LINES> f;
1149 for(size_t j{0u};j < NUM_LINES;j++)
1150 f[j] = delay.Line[vap_offset[j][0]++][j]*(1.0f-fade) +
1151 delay.Line[vap_offset[j][1]++][j]*fade;
1153 for(size_t j{0u};j < NUM_LINES;j++)
1155 const ALfloat input{samples[j][i]};
1156 const ALfloat out{f[j] - feedCoeff*input};
1157 f[j] = input + feedCoeff*out;
1159 samples[j][i] = out;
1161 ++i;
1163 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1164 } while(--td);
1168 /* This generates early reflections.
1170 * This is done by obtaining the primary reflections (those arriving from the
1171 * same direction as the source) from the main delay line. These are
1172 * attenuated and all-pass filtered (based on the diffusion parameter).
1174 * The early lines are then fed in reverse (according to the approximately
1175 * opposite spatial location of the A-Format lines) to create the secondary
1176 * reflections (those arriving from the opposite direction as the source).
1178 * The early response is then completed by combining the primary reflections
1179 * with the delayed and attenuated output from the early lines.
1181 * Finally, the early response is reversed, scattered (based on diffusion),
1182 * and fed into the late reverb section of the main delay line.
1184 * Two static specializations are used for transitional (cross-faded) delay
1185 * line processing and non-transitional processing.
1187 void ReverbState::earlyUnfaded(const size_t offset, const size_t todo)
1189 const DelayLineI early_delay{mEarly.Delay};
1190 const DelayLineI main_delay{mDelay};
1191 const ALfloat mixX{mMixX};
1192 const ALfloat mixY{mMixY};
1194 ASSUME(todo > 0);
1196 /* First, load decorrelated samples from the main delay line as the primary
1197 * reflections.
1199 for(size_t j{0u};j < NUM_LINES;j++)
1201 size_t early_delay_tap{offset - mEarlyDelayTap[j][0]};
1202 const ALfloat coeff{mEarlyDelayCoeff[j][0]};
1203 for(size_t i{0u};i < todo;)
1205 early_delay_tap &= main_delay.Mask;
1206 size_t td{minz(main_delay.Mask+1 - early_delay_tap, todo - i)};
1207 do {
1208 mTempSamples[j][i++] = main_delay.Line[early_delay_tap++][j] * coeff;
1209 } while(--td);
1213 /* Apply a vector all-pass, to help color the initial reflections based on
1214 * the diffusion strength.
1216 mEarly.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
1218 /* Apply a delay and bounce to generate secondary reflections, combine with
1219 * the primary reflections and write out the result for mixing.
1221 for(size_t j{0u};j < NUM_LINES;j++)
1223 size_t feedb_tap{offset - mEarly.Offset[j][0]};
1224 const ALfloat feedb_coeff{mEarly.Coeff[j][0]};
1225 float *out = mEarlySamples[j].data();
1227 for(size_t i{0u};i < todo;)
1229 feedb_tap &= early_delay.Mask;
1230 size_t td{minz(early_delay.Mask+1 - feedb_tap, todo - i)};
1231 do {
1232 out[i] = mTempSamples[j][i] + early_delay.Line[feedb_tap++][j]*feedb_coeff;
1233 ++i;
1234 } while(--td);
1237 for(size_t j{0u};j < NUM_LINES;j++)
1238 early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
1240 /* Also write the result back to the main delay line for the late reverb
1241 * stage to pick up at the appropriate time, appplying a scatter and
1242 * bounce to improve the initial diffusion in the late reverb.
1244 const size_t late_feed_tap{offset - mLateFeedTap};
1245 VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
1247 void ReverbState::earlyFaded(const size_t offset, const size_t todo, const ALfloat fade,
1248 const ALfloat fadeStep)
1250 const DelayLineI early_delay{mEarly.Delay};
1251 const DelayLineI main_delay{mDelay};
1252 const ALfloat mixX{mMixX};
1253 const ALfloat mixY{mMixY};
1255 ASSUME(todo > 0);
1257 for(size_t j{0u};j < NUM_LINES;j++)
1259 size_t early_delay_tap0{offset - mEarlyDelayTap[j][0]};
1260 size_t early_delay_tap1{offset - mEarlyDelayTap[j][1]};
1261 const ALfloat oldCoeff{mEarlyDelayCoeff[j][0]};
1262 const ALfloat oldCoeffStep{-oldCoeff * fadeStep};
1263 const ALfloat newCoeffStep{mEarlyDelayCoeff[j][1] * fadeStep};
1264 ALfloat fadeCount{fade};
1266 for(size_t i{0u};i < todo;)
1268 early_delay_tap0 &= main_delay.Mask;
1269 early_delay_tap1 &= main_delay.Mask;
1270 size_t td{minz(main_delay.Mask+1 - maxz(early_delay_tap0, early_delay_tap1), todo-i)};
1271 do {
1272 fadeCount += 1.0f;
1273 const ALfloat fade0{oldCoeff + oldCoeffStep*fadeCount};
1274 const ALfloat fade1{newCoeffStep*fadeCount};
1275 mTempSamples[j][i++] =
1276 main_delay.Line[early_delay_tap0++][j]*fade0 +
1277 main_delay.Line[early_delay_tap1++][j]*fade1;
1278 } while(--td);
1282 mEarly.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
1284 for(size_t j{0u};j < NUM_LINES;j++)
1286 size_t feedb_tap0{offset - mEarly.Offset[j][0]};
1287 size_t feedb_tap1{offset - mEarly.Offset[j][1]};
1288 const ALfloat feedb_oldCoeff{mEarly.Coeff[j][0]};
1289 const ALfloat feedb_oldCoeffStep{-feedb_oldCoeff * fadeStep};
1290 const ALfloat feedb_newCoeffStep{mEarly.Coeff[j][1] * fadeStep};
1291 float *out = mEarlySamples[j].data();
1292 ALfloat fadeCount{fade};
1294 for(size_t i{0u};i < todo;)
1296 feedb_tap0 &= early_delay.Mask;
1297 feedb_tap1 &= early_delay.Mask;
1298 size_t td{minz(early_delay.Mask+1 - maxz(feedb_tap0, feedb_tap1), todo - i)};
1300 do {
1301 fadeCount += 1.0f;
1302 const ALfloat fade0{feedb_oldCoeff + feedb_oldCoeffStep*fadeCount};
1303 const ALfloat fade1{feedb_newCoeffStep*fadeCount};
1304 out[i] = mTempSamples[j][i] +
1305 early_delay.Line[feedb_tap0++][j]*fade0 +
1306 early_delay.Line[feedb_tap1++][j]*fade1;
1307 ++i;
1308 } while(--td);
1311 for(size_t j{0u};j < NUM_LINES;j++)
1312 early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
1314 const size_t late_feed_tap{offset - mLateFeedTap};
1315 VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
1318 /* This generates the reverb tail using a modified feed-back delay network
1319 * (FDN).
1321 * Results from the early reflections are mixed with the output from the late
1322 * delay lines.
1324 * The late response is then completed by T60 and all-pass filtering the mix.
1326 * Finally, the lines are reversed (so they feed their opposite directions)
1327 * and scattered with the FDN matrix before re-feeding the delay lines.
1329 * Two variations are made, one for for transitional (cross-faded) delay line
1330 * processing and one for non-transitional processing.
1332 void ReverbState::lateUnfaded(const size_t offset, const size_t todo)
1334 const DelayLineI late_delay{mLate.Delay};
1335 const DelayLineI main_delay{mDelay};
1336 const ALfloat mixX{mMixX};
1337 const ALfloat mixY{mMixY};
1339 ASSUME(todo > 0);
1341 /* First, load decorrelated samples from the main and feedback delay lines.
1342 * Filter the signal to apply its frequency-dependent decay.
1344 for(size_t j{0u};j < NUM_LINES;j++)
1346 size_t late_delay_tap{offset - mLateDelayTap[j][0]};
1347 size_t late_feedb_tap{offset - mLate.Offset[j][0]};
1348 const ALfloat midGain{mLate.T60[j].MidGain[0]};
1349 const ALfloat densityGain{mLate.DensityGain[0] * midGain};
1350 for(size_t i{0u};i < todo;)
1352 late_delay_tap &= main_delay.Mask;
1353 late_feedb_tap &= late_delay.Mask;
1354 size_t td{minz(todo - i,
1355 minz(main_delay.Mask+1 - late_delay_tap, late_delay.Mask+1 - late_feedb_tap))};
1356 do {
1357 mTempSamples[j][i++] =
1358 main_delay.Line[late_delay_tap++][j]*densityGain +
1359 late_delay.Line[late_feedb_tap++][j]*midGain;
1360 } while(--td);
1362 mLate.T60[j].process({mTempSamples[j].data(), todo});
1365 /* Apply a vector all-pass to improve micro-surface diffusion, and write
1366 * out the results for mixing.
1368 mLate.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
1369 for(size_t j{0u};j < NUM_LINES;j++)
1370 std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
1372 /* Finally, scatter and bounce the results to refeed the feedback buffer. */
1373 VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
1375 void ReverbState::lateFaded(const size_t offset, const size_t todo, const ALfloat fade,
1376 const ALfloat fadeStep)
1378 const DelayLineI late_delay{mLate.Delay};
1379 const DelayLineI main_delay{mDelay};
1380 const ALfloat mixX{mMixX};
1381 const ALfloat mixY{mMixY};
1383 ASSUME(todo > 0);
1385 for(size_t j{0u};j < NUM_LINES;j++)
1387 const ALfloat oldMidGain{mLate.T60[j].MidGain[0]};
1388 const ALfloat midGain{mLate.T60[j].MidGain[1]};
1389 const ALfloat oldMidStep{-oldMidGain * fadeStep};
1390 const ALfloat midStep{midGain * fadeStep};
1391 const ALfloat oldDensityGain{mLate.DensityGain[0] * oldMidGain};
1392 const ALfloat densityGain{mLate.DensityGain[1] * midGain};
1393 const ALfloat oldDensityStep{-oldDensityGain * fadeStep};
1394 const ALfloat densityStep{densityGain * fadeStep};
1395 size_t late_delay_tap0{offset - mLateDelayTap[j][0]};
1396 size_t late_delay_tap1{offset - mLateDelayTap[j][1]};
1397 size_t late_feedb_tap0{offset - mLate.Offset[j][0]};
1398 size_t late_feedb_tap1{offset - mLate.Offset[j][1]};
1399 ALfloat fadeCount{fade};
1401 for(size_t i{0u};i < todo;)
1403 late_delay_tap0 &= main_delay.Mask;
1404 late_delay_tap1 &= main_delay.Mask;
1405 late_feedb_tap0 &= late_delay.Mask;
1406 late_feedb_tap1 &= late_delay.Mask;
1407 size_t td{minz(todo - i,
1408 minz(main_delay.Mask+1 - maxz(late_delay_tap0, late_delay_tap1),
1409 late_delay.Mask+1 - maxz(late_feedb_tap0, late_feedb_tap1)))};
1410 do {
1411 fadeCount += 1.0f;
1412 const ALfloat fade0{oldDensityGain + oldDensityStep*fadeCount};
1413 const ALfloat fade1{densityStep*fadeCount};
1414 const ALfloat gfade0{oldMidGain + oldMidStep*fadeCount};
1415 const ALfloat gfade1{midStep*fadeCount};
1416 mTempSamples[j][i++] =
1417 main_delay.Line[late_delay_tap0++][j]*fade0 +
1418 main_delay.Line[late_delay_tap1++][j]*fade1 +
1419 late_delay.Line[late_feedb_tap0++][j]*gfade0 +
1420 late_delay.Line[late_feedb_tap1++][j]*gfade1;
1421 } while(--td);
1423 mLate.T60[j].process({mTempSamples[j].data(), todo});
1426 mLate.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
1427 for(size_t j{0u};j < NUM_LINES;j++)
1428 std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
1430 VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
1433 void ReverbState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
1435 size_t offset{mOffset};
1437 ASSUME(samplesToDo > 0);
1439 /* Convert B-Format to A-Format for processing. */
1440 const size_t numInput{samplesIn.size()};
1441 const al::span<float> tmpspan{mTempLine.data(), samplesToDo};
1442 for(size_t c{0u};c < NUM_LINES;c++)
1444 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
1445 MixRowSamples(tmpspan, {B2A[c], numInput}, samplesIn[0].data(), samplesIn[0].size());
1447 /* Band-pass the incoming samples and feed the initial delay line. */
1448 mFilter[c].Lp.process(tmpspan, tmpspan.begin());
1449 mFilter[c].Hp.process(tmpspan, tmpspan.begin());
1450 mDelay.write(offset, c, tmpspan.cbegin(), samplesToDo);
1453 /* Process reverb for these samples. */
1454 if LIKELY(!mDoFading)
1456 for(size_t base{0};base < samplesToDo;)
1458 /* Calculate the number of samples we can do this iteration. */
1459 size_t todo{minz(samplesToDo - base, mMaxUpdate[0])};
1460 /* Some mixers require maintaining a 4-sample alignment, so ensure
1461 * that if it's not the last iteration.
1463 if(base+todo < samplesToDo) todo &= ~size_t{3};
1464 ASSUME(todo > 0);
1466 /* Generate non-faded early reflections and late reverb. */
1467 earlyUnfaded(offset, todo);
1468 lateUnfaded(offset, todo);
1470 /* Finally, mix early reflections and late reverb. */
1471 (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
1473 offset += todo;
1474 base += todo;
1477 else
1479 const float fadeStep{1.0f / static_cast<float>(samplesToDo)};
1480 for(size_t base{0};base < samplesToDo;)
1482 size_t todo{minz(samplesToDo - base, minz(mMaxUpdate[0], mMaxUpdate[1]))};
1483 if(base+todo < samplesToDo) todo &= ~size_t{3};
1484 ASSUME(todo > 0);
1486 /* Generate cross-faded early reflections and late reverb. */
1487 auto fadeCount = static_cast<ALfloat>(base);
1488 earlyFaded(offset, todo, fadeCount, fadeStep);
1489 lateFaded(offset, todo, fadeCount, fadeStep);
1491 (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
1493 offset += todo;
1494 base += todo;
1497 /* Update the cross-fading delay line taps. */
1498 for(size_t c{0u};c < NUM_LINES;c++)
1500 mEarlyDelayTap[c][0] = mEarlyDelayTap[c][1];
1501 mEarlyDelayCoeff[c][0] = mEarlyDelayCoeff[c][1];
1502 mEarly.VecAp.Offset[c][0] = mEarly.VecAp.Offset[c][1];
1503 mEarly.Offset[c][0] = mEarly.Offset[c][1];
1504 mEarly.Coeff[c][0] = mEarly.Coeff[c][1];
1505 mLateDelayTap[c][0] = mLateDelayTap[c][1];
1506 mLate.VecAp.Offset[c][0] = mLate.VecAp.Offset[c][1];
1507 mLate.Offset[c][0] = mLate.Offset[c][1];
1508 mLate.T60[c].MidGain[0] = mLate.T60[c].MidGain[1];
1510 mLate.DensityGain[0] = mLate.DensityGain[1];
1511 mMaxUpdate[0] = mMaxUpdate[1];
1512 mDoFading = false;
1514 mOffset = offset;
1518 void EAXReverb_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
1520 switch(param)
1522 case AL_EAXREVERB_DECAY_HFLIMIT:
1523 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
1524 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hflimit out of range");
1525 props->Reverb.DecayHFLimit = val != AL_FALSE;
1526 break;
1528 default:
1529 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
1530 param);
1533 void EAXReverb_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
1534 { EAXReverb_setParami(props, context, param, vals[0]); }
1535 void EAXReverb_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
1537 switch(param)
1539 case AL_EAXREVERB_DENSITY:
1540 if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
1541 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb density out of range");
1542 props->Reverb.Density = val;
1543 break;
1545 case AL_EAXREVERB_DIFFUSION:
1546 if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
1547 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb diffusion out of range");
1548 props->Reverb.Diffusion = val;
1549 break;
1551 case AL_EAXREVERB_GAIN:
1552 if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
1553 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gain out of range");
1554 props->Reverb.Gain = val;
1555 break;
1557 case AL_EAXREVERB_GAINHF:
1558 if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
1559 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainhf out of range");
1560 props->Reverb.GainHF = val;
1561 break;
1563 case AL_EAXREVERB_GAINLF:
1564 if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
1565 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainlf out of range");
1566 props->Reverb.GainLF = val;
1567 break;
1569 case AL_EAXREVERB_DECAY_TIME:
1570 if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
1571 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay time out of range");
1572 props->Reverb.DecayTime = val;
1573 break;
1575 case AL_EAXREVERB_DECAY_HFRATIO:
1576 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
1577 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hfratio out of range");
1578 props->Reverb.DecayHFRatio = val;
1579 break;
1581 case AL_EAXREVERB_DECAY_LFRATIO:
1582 if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
1583 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay lfratio out of range");
1584 props->Reverb.DecayLFRatio = val;
1585 break;
1587 case AL_EAXREVERB_REFLECTIONS_GAIN:
1588 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
1589 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections gain out of range");
1590 props->Reverb.ReflectionsGain = val;
1591 break;
1593 case AL_EAXREVERB_REFLECTIONS_DELAY:
1594 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
1595 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections delay out of range");
1596 props->Reverb.ReflectionsDelay = val;
1597 break;
1599 case AL_EAXREVERB_LATE_REVERB_GAIN:
1600 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
1601 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb gain out of range");
1602 props->Reverb.LateReverbGain = val;
1603 break;
1605 case AL_EAXREVERB_LATE_REVERB_DELAY:
1606 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
1607 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb delay out of range");
1608 props->Reverb.LateReverbDelay = val;
1609 break;
1611 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1612 if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
1613 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb air absorption gainhf out of range");
1614 props->Reverb.AirAbsorptionGainHF = val;
1615 break;
1617 case AL_EAXREVERB_ECHO_TIME:
1618 if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
1619 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo time out of range");
1620 props->Reverb.EchoTime = val;
1621 break;
1623 case AL_EAXREVERB_ECHO_DEPTH:
1624 if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
1625 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo depth out of range");
1626 props->Reverb.EchoDepth = val;
1627 break;
1629 case AL_EAXREVERB_MODULATION_TIME:
1630 if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
1631 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation time out of range");
1632 props->Reverb.ModulationTime = val;
1633 break;
1635 case AL_EAXREVERB_MODULATION_DEPTH:
1636 if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
1637 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation depth out of range");
1638 props->Reverb.ModulationDepth = val;
1639 break;
1641 case AL_EAXREVERB_HFREFERENCE:
1642 if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
1643 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb hfreference out of range");
1644 props->Reverb.HFReference = val;
1645 break;
1647 case AL_EAXREVERB_LFREFERENCE:
1648 if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
1649 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb lfreference out of range");
1650 props->Reverb.LFReference = val;
1651 break;
1653 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1654 if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
1655 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb room rolloff factor out of range");
1656 props->Reverb.RoomRolloffFactor = val;
1657 break;
1659 default:
1660 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x", param);
1663 void EAXReverb_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
1665 switch(param)
1667 case AL_EAXREVERB_REFLECTIONS_PAN:
1668 if(!(std::isfinite(vals[0]) && std::isfinite(vals[1]) && std::isfinite(vals[2])))
1669 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections pan out of range");
1670 props->Reverb.ReflectionsPan[0] = vals[0];
1671 props->Reverb.ReflectionsPan[1] = vals[1];
1672 props->Reverb.ReflectionsPan[2] = vals[2];
1673 break;
1674 case AL_EAXREVERB_LATE_REVERB_PAN:
1675 if(!(std::isfinite(vals[0]) && std::isfinite(vals[1]) && std::isfinite(vals[2])))
1676 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb pan out of range");
1677 props->Reverb.LateReverbPan[0] = vals[0];
1678 props->Reverb.LateReverbPan[1] = vals[1];
1679 props->Reverb.LateReverbPan[2] = vals[2];
1680 break;
1682 default:
1683 EAXReverb_setParamf(props, context, param, vals[0]);
1684 break;
1688 void EAXReverb_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
1690 switch(param)
1692 case AL_EAXREVERB_DECAY_HFLIMIT:
1693 *val = props->Reverb.DecayHFLimit;
1694 break;
1696 default:
1697 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
1698 param);
1701 void EAXReverb_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
1702 { EAXReverb_getParami(props, context, param, vals); }
1703 void EAXReverb_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
1705 switch(param)
1707 case AL_EAXREVERB_DENSITY:
1708 *val = props->Reverb.Density;
1709 break;
1711 case AL_EAXREVERB_DIFFUSION:
1712 *val = props->Reverb.Diffusion;
1713 break;
1715 case AL_EAXREVERB_GAIN:
1716 *val = props->Reverb.Gain;
1717 break;
1719 case AL_EAXREVERB_GAINHF:
1720 *val = props->Reverb.GainHF;
1721 break;
1723 case AL_EAXREVERB_GAINLF:
1724 *val = props->Reverb.GainLF;
1725 break;
1727 case AL_EAXREVERB_DECAY_TIME:
1728 *val = props->Reverb.DecayTime;
1729 break;
1731 case AL_EAXREVERB_DECAY_HFRATIO:
1732 *val = props->Reverb.DecayHFRatio;
1733 break;
1735 case AL_EAXREVERB_DECAY_LFRATIO:
1736 *val = props->Reverb.DecayLFRatio;
1737 break;
1739 case AL_EAXREVERB_REFLECTIONS_GAIN:
1740 *val = props->Reverb.ReflectionsGain;
1741 break;
1743 case AL_EAXREVERB_REFLECTIONS_DELAY:
1744 *val = props->Reverb.ReflectionsDelay;
1745 break;
1747 case AL_EAXREVERB_LATE_REVERB_GAIN:
1748 *val = props->Reverb.LateReverbGain;
1749 break;
1751 case AL_EAXREVERB_LATE_REVERB_DELAY:
1752 *val = props->Reverb.LateReverbDelay;
1753 break;
1755 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1756 *val = props->Reverb.AirAbsorptionGainHF;
1757 break;
1759 case AL_EAXREVERB_ECHO_TIME:
1760 *val = props->Reverb.EchoTime;
1761 break;
1763 case AL_EAXREVERB_ECHO_DEPTH:
1764 *val = props->Reverb.EchoDepth;
1765 break;
1767 case AL_EAXREVERB_MODULATION_TIME:
1768 *val = props->Reverb.ModulationTime;
1769 break;
1771 case AL_EAXREVERB_MODULATION_DEPTH:
1772 *val = props->Reverb.ModulationDepth;
1773 break;
1775 case AL_EAXREVERB_HFREFERENCE:
1776 *val = props->Reverb.HFReference;
1777 break;
1779 case AL_EAXREVERB_LFREFERENCE:
1780 *val = props->Reverb.LFReference;
1781 break;
1783 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1784 *val = props->Reverb.RoomRolloffFactor;
1785 break;
1787 default:
1788 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x", param);
1791 void EAXReverb_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
1793 switch(param)
1795 case AL_EAXREVERB_REFLECTIONS_PAN:
1796 vals[0] = props->Reverb.ReflectionsPan[0];
1797 vals[1] = props->Reverb.ReflectionsPan[1];
1798 vals[2] = props->Reverb.ReflectionsPan[2];
1799 break;
1800 case AL_EAXREVERB_LATE_REVERB_PAN:
1801 vals[0] = props->Reverb.LateReverbPan[0];
1802 vals[1] = props->Reverb.LateReverbPan[1];
1803 vals[2] = props->Reverb.LateReverbPan[2];
1804 break;
1806 default:
1807 EAXReverb_getParamf(props, context, param, vals);
1808 break;
1812 DEFINE_ALEFFECT_VTABLE(EAXReverb);
1815 struct ReverbStateFactory final : public EffectStateFactory {
1816 EffectState *create() override { return new ReverbState{}; }
1817 EffectProps getDefaultProps() const noexcept override;
1818 const EffectVtable *getEffectVtable() const noexcept override { return &EAXReverb_vtable; }
1821 EffectProps ReverbStateFactory::getDefaultProps() const noexcept
1823 EffectProps props{};
1824 props.Reverb.Density = AL_EAXREVERB_DEFAULT_DENSITY;
1825 props.Reverb.Diffusion = AL_EAXREVERB_DEFAULT_DIFFUSION;
1826 props.Reverb.Gain = AL_EAXREVERB_DEFAULT_GAIN;
1827 props.Reverb.GainHF = AL_EAXREVERB_DEFAULT_GAINHF;
1828 props.Reverb.GainLF = AL_EAXREVERB_DEFAULT_GAINLF;
1829 props.Reverb.DecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME;
1830 props.Reverb.DecayHFRatio = AL_EAXREVERB_DEFAULT_DECAY_HFRATIO;
1831 props.Reverb.DecayLFRatio = AL_EAXREVERB_DEFAULT_DECAY_LFRATIO;
1832 props.Reverb.ReflectionsGain = AL_EAXREVERB_DEFAULT_REFLECTIONS_GAIN;
1833 props.Reverb.ReflectionsDelay = AL_EAXREVERB_DEFAULT_REFLECTIONS_DELAY;
1834 props.Reverb.ReflectionsPan[0] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
1835 props.Reverb.ReflectionsPan[1] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
1836 props.Reverb.ReflectionsPan[2] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
1837 props.Reverb.LateReverbGain = AL_EAXREVERB_DEFAULT_LATE_REVERB_GAIN;
1838 props.Reverb.LateReverbDelay = AL_EAXREVERB_DEFAULT_LATE_REVERB_DELAY;
1839 props.Reverb.LateReverbPan[0] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
1840 props.Reverb.LateReverbPan[1] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
1841 props.Reverb.LateReverbPan[2] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
1842 props.Reverb.EchoTime = AL_EAXREVERB_DEFAULT_ECHO_TIME;
1843 props.Reverb.EchoDepth = AL_EAXREVERB_DEFAULT_ECHO_DEPTH;
1844 props.Reverb.ModulationTime = AL_EAXREVERB_DEFAULT_MODULATION_TIME;
1845 props.Reverb.ModulationDepth = AL_EAXREVERB_DEFAULT_MODULATION_DEPTH;
1846 props.Reverb.AirAbsorptionGainHF = AL_EAXREVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
1847 props.Reverb.HFReference = AL_EAXREVERB_DEFAULT_HFREFERENCE;
1848 props.Reverb.LFReference = AL_EAXREVERB_DEFAULT_LFREFERENCE;
1849 props.Reverb.RoomRolloffFactor = AL_EAXREVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
1850 props.Reverb.DecayHFLimit = AL_EAXREVERB_DEFAULT_DECAY_HFLIMIT;
1851 return props;
1855 void StdReverb_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
1857 switch(param)
1859 case AL_REVERB_DECAY_HFLIMIT:
1860 if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
1861 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hflimit out of range");
1862 props->Reverb.DecayHFLimit = val != AL_FALSE;
1863 break;
1865 default:
1866 context->setError(AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
1869 void StdReverb_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
1870 { StdReverb_setParami(props, context, param, vals[0]); }
1871 void StdReverb_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
1873 switch(param)
1875 case AL_REVERB_DENSITY:
1876 if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
1877 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb density out of range");
1878 props->Reverb.Density = val;
1879 break;
1881 case AL_REVERB_DIFFUSION:
1882 if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
1883 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb diffusion out of range");
1884 props->Reverb.Diffusion = val;
1885 break;
1887 case AL_REVERB_GAIN:
1888 if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
1889 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gain out of range");
1890 props->Reverb.Gain = val;
1891 break;
1893 case AL_REVERB_GAINHF:
1894 if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
1895 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gainhf out of range");
1896 props->Reverb.GainHF = val;
1897 break;
1899 case AL_REVERB_DECAY_TIME:
1900 if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
1901 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay time out of range");
1902 props->Reverb.DecayTime = val;
1903 break;
1905 case AL_REVERB_DECAY_HFRATIO:
1906 if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
1907 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hfratio out of range");
1908 props->Reverb.DecayHFRatio = val;
1909 break;
1911 case AL_REVERB_REFLECTIONS_GAIN:
1912 if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
1913 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections gain out of range");
1914 props->Reverb.ReflectionsGain = val;
1915 break;
1917 case AL_REVERB_REFLECTIONS_DELAY:
1918 if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
1919 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections delay out of range");
1920 props->Reverb.ReflectionsDelay = val;
1921 break;
1923 case AL_REVERB_LATE_REVERB_GAIN:
1924 if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
1925 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb gain out of range");
1926 props->Reverb.LateReverbGain = val;
1927 break;
1929 case AL_REVERB_LATE_REVERB_DELAY:
1930 if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
1931 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb delay out of range");
1932 props->Reverb.LateReverbDelay = val;
1933 break;
1935 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1936 if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
1937 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb air absorption gainhf out of range");
1938 props->Reverb.AirAbsorptionGainHF = val;
1939 break;
1941 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1942 if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
1943 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb room rolloff factor out of range");
1944 props->Reverb.RoomRolloffFactor = val;
1945 break;
1947 default:
1948 context->setError(AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
1951 void StdReverb_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
1952 { StdReverb_setParamf(props, context, param, vals[0]); }
1954 void StdReverb_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
1956 switch(param)
1958 case AL_REVERB_DECAY_HFLIMIT:
1959 *val = props->Reverb.DecayHFLimit;
1960 break;
1962 default:
1963 context->setError(AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
1966 void StdReverb_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
1967 { StdReverb_getParami(props, context, param, vals); }
1968 void StdReverb_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
1970 switch(param)
1972 case AL_REVERB_DENSITY:
1973 *val = props->Reverb.Density;
1974 break;
1976 case AL_REVERB_DIFFUSION:
1977 *val = props->Reverb.Diffusion;
1978 break;
1980 case AL_REVERB_GAIN:
1981 *val = props->Reverb.Gain;
1982 break;
1984 case AL_REVERB_GAINHF:
1985 *val = props->Reverb.GainHF;
1986 break;
1988 case AL_REVERB_DECAY_TIME:
1989 *val = props->Reverb.DecayTime;
1990 break;
1992 case AL_REVERB_DECAY_HFRATIO:
1993 *val = props->Reverb.DecayHFRatio;
1994 break;
1996 case AL_REVERB_REFLECTIONS_GAIN:
1997 *val = props->Reverb.ReflectionsGain;
1998 break;
2000 case AL_REVERB_REFLECTIONS_DELAY:
2001 *val = props->Reverb.ReflectionsDelay;
2002 break;
2004 case AL_REVERB_LATE_REVERB_GAIN:
2005 *val = props->Reverb.LateReverbGain;
2006 break;
2008 case AL_REVERB_LATE_REVERB_DELAY:
2009 *val = props->Reverb.LateReverbDelay;
2010 break;
2012 case AL_REVERB_AIR_ABSORPTION_GAINHF:
2013 *val = props->Reverb.AirAbsorptionGainHF;
2014 break;
2016 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
2017 *val = props->Reverb.RoomRolloffFactor;
2018 break;
2020 default:
2021 context->setError(AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
2024 void StdReverb_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
2025 { StdReverb_getParamf(props, context, param, vals); }
2027 DEFINE_ALEFFECT_VTABLE(StdReverb);
2030 struct StdReverbStateFactory final : public EffectStateFactory {
2031 EffectState *create() override { return new ReverbState{}; }
2032 EffectProps getDefaultProps() const noexcept override;
2033 const EffectVtable *getEffectVtable() const noexcept override { return &StdReverb_vtable; }
2036 EffectProps StdReverbStateFactory::getDefaultProps() const noexcept
2038 EffectProps props{};
2039 props.Reverb.Density = AL_REVERB_DEFAULT_DENSITY;
2040 props.Reverb.Diffusion = AL_REVERB_DEFAULT_DIFFUSION;
2041 props.Reverb.Gain = AL_REVERB_DEFAULT_GAIN;
2042 props.Reverb.GainHF = AL_REVERB_DEFAULT_GAINHF;
2043 props.Reverb.GainLF = 1.0f;
2044 props.Reverb.DecayTime = AL_REVERB_DEFAULT_DECAY_TIME;
2045 props.Reverb.DecayHFRatio = AL_REVERB_DEFAULT_DECAY_HFRATIO;
2046 props.Reverb.DecayLFRatio = 1.0f;
2047 props.Reverb.ReflectionsGain = AL_REVERB_DEFAULT_REFLECTIONS_GAIN;
2048 props.Reverb.ReflectionsDelay = AL_REVERB_DEFAULT_REFLECTIONS_DELAY;
2049 props.Reverb.ReflectionsPan[0] = 0.0f;
2050 props.Reverb.ReflectionsPan[1] = 0.0f;
2051 props.Reverb.ReflectionsPan[2] = 0.0f;
2052 props.Reverb.LateReverbGain = AL_REVERB_DEFAULT_LATE_REVERB_GAIN;
2053 props.Reverb.LateReverbDelay = AL_REVERB_DEFAULT_LATE_REVERB_DELAY;
2054 props.Reverb.LateReverbPan[0] = 0.0f;
2055 props.Reverb.LateReverbPan[1] = 0.0f;
2056 props.Reverb.LateReverbPan[2] = 0.0f;
2057 props.Reverb.EchoTime = 0.25f;
2058 props.Reverb.EchoDepth = 0.0f;
2059 props.Reverb.ModulationTime = 0.25f;
2060 props.Reverb.ModulationDepth = 0.0f;
2061 props.Reverb.AirAbsorptionGainHF = AL_REVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
2062 props.Reverb.HFReference = 5000.0f;
2063 props.Reverb.LFReference = 250.0f;
2064 props.Reverb.RoomRolloffFactor = AL_REVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
2065 props.Reverb.DecayHFLimit = AL_REVERB_DEFAULT_DECAY_HFLIMIT;
2066 return props;
2069 } // namespace
2071 EffectStateFactory *ReverbStateFactory_getFactory()
2073 static ReverbStateFactory ReverbFactory{};
2074 return &ReverbFactory;
2077 EffectStateFactory *StdReverbStateFactory_getFactory()
2079 static StdReverbStateFactory ReverbFactory{};
2080 return &ReverbFactory;