Avoid static constexpr for arrays iterated over at run-time
[openal-soft.git] / alc / effects / reverb.cpp
blob6e56adf2882f28fc3f4b074d2ade7f731462d39d
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(ALfloat *samples, const size_t todo)
291 HFFilter.process(samples, samples, todo);
292 LFFilter.process(samples, samples, todo);
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.data(), hfscale, todo);
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.data(), hfscale, todo);
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 ALfloat mfGain{CalcDecayCoeff(length, mfDecayTime)};
710 const ALfloat lfGain{maxf(CalcDecayCoeff(length, lfDecayTime)/mfGain, 0.001f)};
711 const ALfloat hfGain{maxf(CalcDecayCoeff(length, hfDecayTime)/mfGain, 0.001f)};
713 MidGain[1] = mfGain;
714 LFFilter.setParams(BiquadType::LowShelf, lfGain, lf0norm,
715 LFFilter.rcpQFromSlope(lfGain, 1.0f));
716 HFFilter.setParams(BiquadType::HighShelf, hfGain, hf0norm,
717 HFFilter.rcpQFromSlope(hfGain, 1.0f));
720 /* Update the early reflection line lengths and gain coefficients. */
721 void EarlyReflections::updateLines(const ALfloat density, const ALfloat diffusion,
722 const ALfloat decayTime, const ALfloat frequency)
724 const ALfloat multiplier{CalcDelayLengthMult(density)};
726 /* Calculate the all-pass feed-back/forward coefficient. */
727 VecAp.Coeff = std::sqrt(0.5f) * std::pow(diffusion, 2.0f);
729 for(size_t i{0u};i < NUM_LINES;i++)
731 /* Calculate the length (in seconds) of each all-pass line. */
732 ALfloat length{EARLY_ALLPASS_LENGTHS[i] * multiplier};
734 /* Calculate the delay offset for each all-pass line. */
735 VecAp.Offset[i][1] = float2uint(length * frequency);
737 /* Calculate the length (in seconds) of each delay line. */
738 length = EARLY_LINE_LENGTHS[i] * multiplier;
740 /* Calculate the delay offset for each delay line. */
741 Offset[i][1] = float2uint(length * frequency);
743 /* Calculate the gain (coefficient) for each line. */
744 Coeff[i][1] = CalcDecayCoeff(length, decayTime);
748 /* Update the late reverb line lengths and T60 coefficients. */
749 void LateReverb::updateLines(const ALfloat density, const ALfloat diffusion,
750 const ALfloat lfDecayTime, const ALfloat mfDecayTime, const ALfloat hfDecayTime,
751 const ALfloat lf0norm, const ALfloat hf0norm, const ALfloat frequency)
753 /* Scaling factor to convert the normalized reference frequencies from
754 * representing 0...freq to 0...max_reference.
756 const ALfloat norm_weight_factor{frequency / AL_EAXREVERB_MAX_HFREFERENCE};
758 const ALfloat late_allpass_avg{
759 std::accumulate(LATE_ALLPASS_LENGTHS.begin(), LATE_ALLPASS_LENGTHS.end(), 0.0f) /
760 float{NUM_LINES}};
762 /* To compensate for changes in modal density and decay time of the late
763 * reverb signal, the input is attenuated based on the maximal energy of
764 * the outgoing signal. This approximation is used to keep the apparent
765 * energy of the signal equal for all ranges of density and decay time.
767 * The average length of the delay lines is used to calculate the
768 * attenuation coefficient.
770 const ALfloat multiplier{CalcDelayLengthMult(density)};
771 ALfloat length{std::accumulate(LATE_LINE_LENGTHS.begin(), LATE_LINE_LENGTHS.end(), 0.0f) /
772 float{NUM_LINES} * multiplier};
773 length += late_allpass_avg * multiplier;
774 /* The density gain calculation uses an average decay time weighted by
775 * approximate bandwidth. This attempts to compensate for losses of energy
776 * that reduce decay time due to scattering into highly attenuated bands.
778 const ALfloat decayTimeWeighted{
779 (lf0norm*norm_weight_factor)*lfDecayTime +
780 (hf0norm*norm_weight_factor - lf0norm*norm_weight_factor)*mfDecayTime +
781 (1.0f - hf0norm*norm_weight_factor)*hfDecayTime};
782 DensityGain[1] = CalcDensityGain(CalcDecayCoeff(length, decayTimeWeighted));
784 /* Calculate the all-pass feed-back/forward coefficient. */
785 VecAp.Coeff = std::sqrt(0.5f) * std::pow(diffusion, 2.0f);
787 for(size_t i{0u};i < NUM_LINES;i++)
789 /* Calculate the length (in seconds) of each all-pass line. */
790 length = LATE_ALLPASS_LENGTHS[i] * multiplier;
792 /* Calculate the delay offset for each all-pass line. */
793 VecAp.Offset[i][1] = float2uint(length * frequency);
795 /* Calculate the length (in seconds) of each delay line. */
796 length = LATE_LINE_LENGTHS[i] * multiplier;
798 /* Calculate the delay offset for each delay line. */
799 Offset[i][1] = float2uint(length*frequency + 0.5f);
801 /* Approximate the absorption that the vector all-pass would exhibit
802 * given the current diffusion so we don't have to process a full T60
803 * filter for each of its four lines.
805 length += lerp(LATE_ALLPASS_LENGTHS[i], late_allpass_avg, diffusion) * multiplier;
807 /* Calculate the T60 damping coefficients for each line. */
808 T60[i].calcCoeffs(length, lfDecayTime, mfDecayTime, hfDecayTime, lf0norm, hf0norm);
813 /* Update the offsets for the main effect delay line. */
814 void ReverbState::updateDelayLine(const ALfloat earlyDelay, const ALfloat lateDelay,
815 const ALfloat density, const ALfloat decayTime, const ALfloat frequency)
817 const ALfloat multiplier{CalcDelayLengthMult(density)};
819 /* Early reflection taps are decorrelated by means of an average room
820 * reflection approximation described above the definition of the taps.
821 * This approximation is linear and so the above density multiplier can
822 * be applied to adjust the width of the taps. A single-band decay
823 * coefficient is applied to simulate initial attenuation and absorption.
825 * Late reverb taps are based on the late line lengths to allow a zero-
826 * delay path and offsets that would continue the propagation naturally
827 * into the late lines.
829 for(size_t i{0u};i < NUM_LINES;i++)
831 ALfloat length{earlyDelay + EARLY_TAP_LENGTHS[i]*multiplier};
832 mEarlyDelayTap[i][1] = float2uint(length * frequency);
834 length = EARLY_TAP_LENGTHS[i]*multiplier;
835 mEarlyDelayCoeff[i][1] = CalcDecayCoeff(length, decayTime);
837 length = (LATE_LINE_LENGTHS[i] - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*multiplier +
838 lateDelay;
839 mLateDelayTap[i][1] = mLateFeedTap + float2uint(length * frequency);
843 /* Creates a transform matrix given a reverb vector. The vector pans the reverb
844 * reflections toward the given direction, using its magnitude (up to 1) as a
845 * focal strength. This function results in a B-Format transformation matrix
846 * that spatially focuses the signal in the desired direction.
848 alu::Matrix GetTransformFromVector(const ALfloat *vec)
850 constexpr float sqrt_3{1.73205080756887719318f};
852 /* Normalize the panning vector according to the N3D scale, which has an
853 * extra sqrt(3) term on the directional components. Converting from OpenAL
854 * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however
855 * that the reverb panning vectors use left-handed coordinates, unlike the
856 * rest of OpenAL which use right-handed. This is fixed by negating Z,
857 * which cancels out with the B-Format Z negation.
859 ALfloat norm[3];
860 ALfloat mag{std::sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])};
861 if(mag > 1.0f)
863 norm[0] = vec[0] / mag * -sqrt_3;
864 norm[1] = vec[1] / mag * sqrt_3;
865 norm[2] = vec[2] / mag * sqrt_3;
866 mag = 1.0f;
868 else
870 /* If the magnitude is less than or equal to 1, just apply the sqrt(3)
871 * term. There's no need to renormalize the magnitude since it would
872 * just be reapplied in the matrix.
874 norm[0] = vec[0] * -sqrt_3;
875 norm[1] = vec[1] * sqrt_3;
876 norm[2] = vec[2] * sqrt_3;
879 return alu::Matrix{
880 1.0f, 0.0f, 0.0f, 0.0f,
881 norm[0], 1.0f-mag, 0.0f, 0.0f,
882 norm[1], 0.0f, 1.0f-mag, 0.0f,
883 norm[2], 0.0f, 0.0f, 1.0f-mag
887 /* Update the early and late 3D panning gains. */
888 void ReverbState::update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan,
889 const ALfloat earlyGain, const ALfloat lateGain, const EffectTarget &target)
891 /* Create matrices that transform a B-Format signal according to the
892 * panning vectors.
894 const alu::Matrix earlymat{GetTransformFromVector(ReflectionsPan)};
895 const alu::Matrix latemat{GetTransformFromVector(LateReverbPan)};
897 mOutTarget = target.Main->Buffer;
898 for(size_t i{0u};i < NUM_LINES;i++)
900 const ALfloat coeffs[MAX_AMBI_CHANNELS]{earlymat[0][i], earlymat[1][i], earlymat[2][i],
901 earlymat[3][i]};
902 ComputePanGains(target.Main, coeffs, earlyGain, mEarly.PanGain[i]);
904 for(size_t i{0u};i < NUM_LINES;i++)
906 const ALfloat coeffs[MAX_AMBI_CHANNELS]{latemat[0][i], latemat[1][i], latemat[2][i],
907 latemat[3][i]};
908 ComputePanGains(target.Main, coeffs, lateGain, mLate.PanGain[i]);
912 void ReverbState::update(const ALCcontext *Context, const ALeffectslot *Slot, const EffectProps *props, const EffectTarget target)
914 const ALCdevice *Device{Context->mDevice.get()};
915 const auto frequency = static_cast<ALfloat>(Device->Frequency);
917 /* Calculate the master filters */
918 ALfloat hf0norm{minf(props->Reverb.HFReference / frequency, 0.49f)};
919 /* Restrict the filter gains from going below -60dB to keep the filter from
920 * killing most of the signal.
922 ALfloat gainhf{maxf(props->Reverb.GainHF, 0.001f)};
923 mFilter[0].Lp.setParams(BiquadType::HighShelf, gainhf, hf0norm,
924 mFilter[0].Lp.rcpQFromSlope(gainhf, 1.0f));
925 ALfloat lf0norm{minf(props->Reverb.LFReference / frequency, 0.49f)};
926 ALfloat gainlf{maxf(props->Reverb.GainLF, 0.001f)};
927 mFilter[0].Hp.setParams(BiquadType::LowShelf, gainlf, lf0norm,
928 mFilter[0].Hp.rcpQFromSlope(gainlf, 1.0f));
929 for(size_t i{1u};i < NUM_LINES;i++)
931 mFilter[i].Lp.copyParamsFrom(mFilter[0].Lp);
932 mFilter[i].Hp.copyParamsFrom(mFilter[0].Hp);
935 /* Update the main effect delay and associated taps. */
936 updateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
937 props->Reverb.Density, props->Reverb.DecayTime, frequency);
939 /* Update the early lines. */
940 mEarly.updateLines(props->Reverb.Density, props->Reverb.Diffusion, props->Reverb.DecayTime,
941 frequency);
943 /* Get the mixing matrix coefficients. */
944 CalcMatrixCoeffs(props->Reverb.Diffusion, &mMixX, &mMixY);
946 /* If the HF limit parameter is flagged, calculate an appropriate limit
947 * based on the air absorption parameter.
949 ALfloat hfRatio{props->Reverb.DecayHFRatio};
950 if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
951 hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
952 props->Reverb.DecayTime);
954 /* Calculate the LF/HF decay times. */
955 const ALfloat lfDecayTime{clampf(props->Reverb.DecayTime * props->Reverb.DecayLFRatio,
956 AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME)};
957 const ALfloat hfDecayTime{clampf(props->Reverb.DecayTime * hfRatio,
958 AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME)};
960 /* Update the late lines. */
961 mLate.updateLines(props->Reverb.Density, props->Reverb.Diffusion, lfDecayTime,
962 props->Reverb.DecayTime, hfDecayTime, lf0norm, hf0norm, frequency);
964 /* Update early and late 3D panning. */
965 const ALfloat gain{props->Reverb.Gain * Slot->Params.Gain * ReverbBoost};
966 update3DPanning(props->Reverb.ReflectionsPan, props->Reverb.LateReverbPan,
967 props->Reverb.ReflectionsGain*gain, props->Reverb.LateReverbGain*gain, target);
969 /* Calculate the max update size from the smallest relevant delay. */
970 mMaxUpdate[1] = minz(MAX_UPDATE_SAMPLES, minz(mEarly.Offset[0][1], mLate.Offset[0][1]));
972 /* Determine if delay-line cross-fading is required. Density is essentially
973 * a master control for the feedback delays, so changes the offsets of many
974 * delay lines.
976 mDoFading |= (mParams.Density != props->Reverb.Density ||
977 /* Diffusion and decay times influences the decay rate (gain) of the
978 * late reverb T60 filter.
980 mParams.Diffusion != props->Reverb.Diffusion ||
981 mParams.DecayTime != props->Reverb.DecayTime ||
982 mParams.HFDecayTime != hfDecayTime ||
983 mParams.LFDecayTime != lfDecayTime ||
984 /* HF/LF References control the weighting used to calculate the density
985 * gain.
987 mParams.HFReference != props->Reverb.HFReference ||
988 mParams.LFReference != props->Reverb.LFReference);
989 if(mDoFading)
991 mParams.Density = props->Reverb.Density;
992 mParams.Diffusion = props->Reverb.Diffusion;
993 mParams.DecayTime = props->Reverb.DecayTime;
994 mParams.HFDecayTime = hfDecayTime;
995 mParams.LFDecayTime = lfDecayTime;
996 mParams.HFReference = props->Reverb.HFReference;
997 mParams.LFReference = props->Reverb.LFReference;
1002 /**************************************
1003 * Effect Processing *
1004 **************************************/
1006 /* Applies a scattering matrix to the 4-line (vector) input. This is used
1007 * for both the below vector all-pass model and to perform modal feed-back
1008 * delay network (FDN) mixing.
1010 * The matrix is derived from a skew-symmetric matrix to form a 4D rotation
1011 * matrix with a single unitary rotational parameter:
1013 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1014 * [ -a, d, c, -b ]
1015 * [ -b, -c, d, a ]
1016 * [ -c, b, -a, d ]
1018 * The rotation is constructed from the effect's diffusion parameter,
1019 * yielding:
1021 * 1 = x^2 + 3 y^2
1023 * Where a, b, and c are the coefficient y with differing signs, and d is the
1024 * coefficient x. The final matrix is thus:
1026 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1027 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1028 * [ y, -y, x, y ] x = cos(t)
1029 * [ -y, -y, -y, x ] y = sin(t) / n
1031 * Any square orthogonal matrix with an order that is a power of two will
1032 * work (where ^T is transpose, ^-1 is inverse):
1034 * M^T = M^-1
1036 * Using that knowledge, finding an appropriate matrix can be accomplished
1037 * naively by searching all combinations of:
1039 * M = D + S - S^T
1041 * Where D is a diagonal matrix (of x), and S is a triangular matrix (of y)
1042 * whose combination of signs are being iterated.
1044 inline auto VectorPartialScatter(const std::array<float,NUM_LINES> &RESTRICT in,
1045 const ALfloat xCoeff, const ALfloat yCoeff) -> std::array<float,NUM_LINES>
1047 std::array<float,NUM_LINES> out;
1048 out[0] = xCoeff*in[0] + yCoeff*( in[1] + -in[2] + in[3]);
1049 out[1] = xCoeff*in[1] + yCoeff*(-in[0] + in[2] + in[3]);
1050 out[2] = xCoeff*in[2] + yCoeff*( in[0] + -in[1] + in[3]);
1051 out[3] = xCoeff*in[3] + yCoeff*(-in[0] + -in[1] + -in[2] );
1052 return out;
1055 /* Utilizes the above, but reverses the input channels. */
1056 void VectorScatterRevDelayIn(const DelayLineI delay, size_t offset, const ALfloat xCoeff,
1057 const ALfloat yCoeff, const al::span<const ReverbUpdateLine,NUM_LINES> in, const size_t count)
1059 ASSUME(count > 0);
1061 for(size_t i{0u};i < count;)
1063 offset &= delay.Mask;
1064 size_t td{minz(delay.Mask+1 - offset, count-i)};
1065 do {
1066 std::array<float,NUM_LINES> f;
1067 for(size_t j{0u};j < NUM_LINES;j++)
1068 f[NUM_LINES-1-j] = in[j][i];
1069 ++i;
1071 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1072 } while(--td);
1076 /* This applies a Gerzon multiple-in/multiple-out (MIMO) vector all-pass
1077 * filter to the 4-line input.
1079 * It works by vectorizing a regular all-pass filter and replacing the delay
1080 * element with a scattering matrix (like the one above) and a diagonal
1081 * matrix of delay elements.
1083 * Two static specializations are used for transitional (cross-faded) delay
1084 * line processing and non-transitional processing.
1086 void VecAllpass::processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
1087 const ALfloat xCoeff, const ALfloat yCoeff, const size_t todo)
1089 const DelayLineI delay{Delay};
1090 const ALfloat feedCoeff{Coeff};
1092 ASSUME(todo > 0);
1094 size_t vap_offset[NUM_LINES];
1095 for(size_t j{0u};j < NUM_LINES;j++)
1096 vap_offset[j] = offset - Offset[j][0];
1097 for(size_t i{0u};i < todo;)
1099 for(size_t j{0u};j < NUM_LINES;j++)
1100 vap_offset[j] &= delay.Mask;
1101 offset &= delay.Mask;
1103 size_t maxoff{offset};
1104 for(size_t j{0u};j < NUM_LINES;j++)
1105 maxoff = maxz(maxoff, vap_offset[j]);
1106 size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
1108 do {
1109 std::array<float,NUM_LINES> f;
1110 for(size_t j{0u};j < NUM_LINES;j++)
1112 const ALfloat input{samples[j][i]};
1113 const ALfloat out{delay.Line[vap_offset[j]++][j] - feedCoeff*input};
1114 f[j] = input + feedCoeff*out;
1116 samples[j][i] = out;
1118 ++i;
1120 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1121 } while(--td);
1124 void VecAllpass::processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
1125 const ALfloat xCoeff, const ALfloat yCoeff, ALfloat fadeCount, const ALfloat fadeStep,
1126 const size_t todo)
1128 const DelayLineI delay{Delay};
1129 const ALfloat feedCoeff{Coeff};
1131 ASSUME(todo > 0);
1133 size_t vap_offset[NUM_LINES][2];
1134 for(size_t j{0u};j < NUM_LINES;j++)
1136 vap_offset[j][0] = offset - Offset[j][0];
1137 vap_offset[j][1] = offset - Offset[j][1];
1139 for(size_t i{0u};i < todo;)
1141 for(size_t j{0u};j < NUM_LINES;j++)
1143 vap_offset[j][0] &= delay.Mask;
1144 vap_offset[j][1] &= delay.Mask;
1146 offset &= delay.Mask;
1148 size_t maxoff{offset};
1149 for(size_t j{0u};j < NUM_LINES;j++)
1150 maxoff = maxz(maxoff, maxz(vap_offset[j][0], vap_offset[j][1]));
1151 size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
1153 do {
1154 fadeCount += 1.0f;
1155 const float fade{fadeCount * fadeStep};
1157 std::array<float,NUM_LINES> f;
1158 for(size_t j{0u};j < NUM_LINES;j++)
1159 f[j] = delay.Line[vap_offset[j][0]++][j]*(1.0f-fade) +
1160 delay.Line[vap_offset[j][1]++][j]*fade;
1162 for(size_t j{0u};j < NUM_LINES;j++)
1164 const ALfloat input{samples[j][i]};
1165 const ALfloat out{f[j] - feedCoeff*input};
1166 f[j] = input + feedCoeff*out;
1168 samples[j][i] = out;
1170 ++i;
1172 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1173 } while(--td);
1177 /* This generates early reflections.
1179 * This is done by obtaining the primary reflections (those arriving from the
1180 * same direction as the source) from the main delay line. These are
1181 * attenuated and all-pass filtered (based on the diffusion parameter).
1183 * The early lines are then fed in reverse (according to the approximately
1184 * opposite spatial location of the A-Format lines) to create the secondary
1185 * reflections (those arriving from the opposite direction as the source).
1187 * The early response is then completed by combining the primary reflections
1188 * with the delayed and attenuated output from the early lines.
1190 * Finally, the early response is reversed, scattered (based on diffusion),
1191 * and fed into the late reverb section of the main delay line.
1193 * Two static specializations are used for transitional (cross-faded) delay
1194 * line processing and non-transitional processing.
1196 void ReverbState::earlyUnfaded(const size_t offset, const size_t todo)
1198 const DelayLineI early_delay{mEarly.Delay};
1199 const DelayLineI main_delay{mDelay};
1200 const ALfloat mixX{mMixX};
1201 const ALfloat mixY{mMixY};
1203 ASSUME(todo > 0);
1205 /* First, load decorrelated samples from the main delay line as the primary
1206 * reflections.
1208 for(size_t j{0u};j < NUM_LINES;j++)
1210 size_t early_delay_tap{offset - mEarlyDelayTap[j][0]};
1211 const ALfloat coeff{mEarlyDelayCoeff[j][0]};
1212 for(size_t i{0u};i < todo;)
1214 early_delay_tap &= main_delay.Mask;
1215 size_t td{minz(main_delay.Mask+1 - early_delay_tap, todo - i)};
1216 do {
1217 mTempSamples[j][i++] = main_delay.Line[early_delay_tap++][j] * coeff;
1218 } while(--td);
1222 /* Apply a vector all-pass, to help color the initial reflections based on
1223 * the diffusion strength.
1225 mEarly.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
1227 /* Apply a delay and bounce to generate secondary reflections, combine with
1228 * the primary reflections and write out the result for mixing.
1230 for(size_t j{0u};j < NUM_LINES;j++)
1232 size_t feedb_tap{offset - mEarly.Offset[j][0]};
1233 const ALfloat feedb_coeff{mEarly.Coeff[j][0]};
1234 float *out = mEarlySamples[j].data();
1236 for(size_t i{0u};i < todo;)
1238 feedb_tap &= early_delay.Mask;
1239 size_t td{minz(early_delay.Mask+1 - feedb_tap, todo - i)};
1240 do {
1241 out[i] = mTempSamples[j][i] + early_delay.Line[feedb_tap++][j]*feedb_coeff;
1242 ++i;
1243 } while(--td);
1246 for(size_t j{0u};j < NUM_LINES;j++)
1247 early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
1249 /* Also write the result back to the main delay line for the late reverb
1250 * stage to pick up at the appropriate time, appplying a scatter and
1251 * bounce to improve the initial diffusion in the late reverb.
1253 const size_t late_feed_tap{offset - mLateFeedTap};
1254 VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
1256 void ReverbState::earlyFaded(const size_t offset, const size_t todo, const ALfloat fade,
1257 const ALfloat fadeStep)
1259 const DelayLineI early_delay{mEarly.Delay};
1260 const DelayLineI main_delay{mDelay};
1261 const ALfloat mixX{mMixX};
1262 const ALfloat mixY{mMixY};
1264 ASSUME(todo > 0);
1266 for(size_t j{0u};j < NUM_LINES;j++)
1268 size_t early_delay_tap0{offset - mEarlyDelayTap[j][0]};
1269 size_t early_delay_tap1{offset - mEarlyDelayTap[j][1]};
1270 const ALfloat oldCoeff{mEarlyDelayCoeff[j][0]};
1271 const ALfloat oldCoeffStep{-oldCoeff * fadeStep};
1272 const ALfloat newCoeffStep{mEarlyDelayCoeff[j][1] * fadeStep};
1273 ALfloat fadeCount{fade};
1275 for(size_t i{0u};i < todo;)
1277 early_delay_tap0 &= main_delay.Mask;
1278 early_delay_tap1 &= main_delay.Mask;
1279 size_t td{minz(main_delay.Mask+1 - maxz(early_delay_tap0, early_delay_tap1), todo-i)};
1280 do {
1281 fadeCount += 1.0f;
1282 const ALfloat fade0{oldCoeff + oldCoeffStep*fadeCount};
1283 const ALfloat fade1{newCoeffStep*fadeCount};
1284 mTempSamples[j][i++] =
1285 main_delay.Line[early_delay_tap0++][j]*fade0 +
1286 main_delay.Line[early_delay_tap1++][j]*fade1;
1287 } while(--td);
1291 mEarly.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
1293 for(size_t j{0u};j < NUM_LINES;j++)
1295 size_t feedb_tap0{offset - mEarly.Offset[j][0]};
1296 size_t feedb_tap1{offset - mEarly.Offset[j][1]};
1297 const ALfloat feedb_oldCoeff{mEarly.Coeff[j][0]};
1298 const ALfloat feedb_oldCoeffStep{-feedb_oldCoeff * fadeStep};
1299 const ALfloat feedb_newCoeffStep{mEarly.Coeff[j][1] * fadeStep};
1300 float *out = mEarlySamples[j].data();
1301 ALfloat fadeCount{fade};
1303 for(size_t i{0u};i < todo;)
1305 feedb_tap0 &= early_delay.Mask;
1306 feedb_tap1 &= early_delay.Mask;
1307 size_t td{minz(early_delay.Mask+1 - maxz(feedb_tap0, feedb_tap1), todo - i)};
1309 do {
1310 fadeCount += 1.0f;
1311 const ALfloat fade0{feedb_oldCoeff + feedb_oldCoeffStep*fadeCount};
1312 const ALfloat fade1{feedb_newCoeffStep*fadeCount};
1313 out[i] = mTempSamples[j][i] +
1314 early_delay.Line[feedb_tap0++][j]*fade0 +
1315 early_delay.Line[feedb_tap1++][j]*fade1;
1316 ++i;
1317 } while(--td);
1320 for(size_t j{0u};j < NUM_LINES;j++)
1321 early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
1323 const size_t late_feed_tap{offset - mLateFeedTap};
1324 VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
1327 /* This generates the reverb tail using a modified feed-back delay network
1328 * (FDN).
1330 * Results from the early reflections are mixed with the output from the late
1331 * delay lines.
1333 * The late response is then completed by T60 and all-pass filtering the mix.
1335 * Finally, the lines are reversed (so they feed their opposite directions)
1336 * and scattered with the FDN matrix before re-feeding the delay lines.
1338 * Two variations are made, one for for transitional (cross-faded) delay line
1339 * processing and one for non-transitional processing.
1341 void ReverbState::lateUnfaded(const size_t offset, const size_t todo)
1343 const DelayLineI late_delay{mLate.Delay};
1344 const DelayLineI main_delay{mDelay};
1345 const ALfloat mixX{mMixX};
1346 const ALfloat mixY{mMixY};
1348 ASSUME(todo > 0);
1350 /* First, load decorrelated samples from the main and feedback delay lines.
1351 * Filter the signal to apply its frequency-dependent decay.
1353 for(size_t j{0u};j < NUM_LINES;j++)
1355 size_t late_delay_tap{offset - mLateDelayTap[j][0]};
1356 size_t late_feedb_tap{offset - mLate.Offset[j][0]};
1357 const ALfloat midGain{mLate.T60[j].MidGain[0]};
1358 const ALfloat densityGain{mLate.DensityGain[0] * midGain};
1359 for(size_t i{0u};i < todo;)
1361 late_delay_tap &= main_delay.Mask;
1362 late_feedb_tap &= late_delay.Mask;
1363 size_t td{minz(todo - i,
1364 minz(main_delay.Mask+1 - late_delay_tap, late_delay.Mask+1 - late_feedb_tap))};
1365 do {
1366 mTempSamples[j][i++] =
1367 main_delay.Line[late_delay_tap++][j]*densityGain +
1368 late_delay.Line[late_feedb_tap++][j]*midGain;
1369 } while(--td);
1371 mLate.T60[j].process(mTempSamples[j].data(), todo);
1374 /* Apply a vector all-pass to improve micro-surface diffusion, and write
1375 * out the results for mixing.
1377 mLate.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
1378 for(size_t j{0u};j < NUM_LINES;j++)
1379 std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
1381 /* Finally, scatter and bounce the results to refeed the feedback buffer. */
1382 VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
1384 void ReverbState::lateFaded(const size_t offset, const size_t todo, const ALfloat fade,
1385 const ALfloat fadeStep)
1387 const DelayLineI late_delay{mLate.Delay};
1388 const DelayLineI main_delay{mDelay};
1389 const ALfloat mixX{mMixX};
1390 const ALfloat mixY{mMixY};
1392 ASSUME(todo > 0);
1394 for(size_t j{0u};j < NUM_LINES;j++)
1396 const ALfloat oldMidGain{mLate.T60[j].MidGain[0]};
1397 const ALfloat midGain{mLate.T60[j].MidGain[1]};
1398 const ALfloat oldMidStep{-oldMidGain * fadeStep};
1399 const ALfloat midStep{midGain * fadeStep};
1400 const ALfloat oldDensityGain{mLate.DensityGain[0] * oldMidGain};
1401 const ALfloat densityGain{mLate.DensityGain[1] * midGain};
1402 const ALfloat oldDensityStep{-oldDensityGain * fadeStep};
1403 const ALfloat densityStep{densityGain * fadeStep};
1404 size_t late_delay_tap0{offset - mLateDelayTap[j][0]};
1405 size_t late_delay_tap1{offset - mLateDelayTap[j][1]};
1406 size_t late_feedb_tap0{offset - mLate.Offset[j][0]};
1407 size_t late_feedb_tap1{offset - mLate.Offset[j][1]};
1408 ALfloat fadeCount{fade};
1410 for(size_t i{0u};i < todo;)
1412 late_delay_tap0 &= main_delay.Mask;
1413 late_delay_tap1 &= main_delay.Mask;
1414 late_feedb_tap0 &= late_delay.Mask;
1415 late_feedb_tap1 &= late_delay.Mask;
1416 size_t td{minz(todo - i,
1417 minz(main_delay.Mask+1 - maxz(late_delay_tap0, late_delay_tap1),
1418 late_delay.Mask+1 - maxz(late_feedb_tap0, late_feedb_tap1)))};
1419 do {
1420 fadeCount += 1.0f;
1421 const ALfloat fade0{oldDensityGain + oldDensityStep*fadeCount};
1422 const ALfloat fade1{densityStep*fadeCount};
1423 const ALfloat gfade0{oldMidGain + oldMidStep*fadeCount};
1424 const ALfloat gfade1{midStep*fadeCount};
1425 mTempSamples[j][i++] =
1426 main_delay.Line[late_delay_tap0++][j]*fade0 +
1427 main_delay.Line[late_delay_tap1++][j]*fade1 +
1428 late_delay.Line[late_feedb_tap0++][j]*gfade0 +
1429 late_delay.Line[late_feedb_tap1++][j]*gfade1;
1430 } while(--td);
1432 mLate.T60[j].process(mTempSamples[j].data(), todo);
1435 mLate.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
1436 for(size_t j{0u};j < NUM_LINES;j++)
1437 std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
1439 VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
1442 void ReverbState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
1444 size_t offset{mOffset};
1446 ASSUME(samplesToDo > 0);
1448 /* Convert B-Format to A-Format for processing. */
1449 const size_t numInput{samplesIn.size()};
1450 const al::span<float> tmpspan{mTempLine.data(), samplesToDo};
1451 for(size_t c{0u};c < NUM_LINES;c++)
1453 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
1454 MixRowSamples(tmpspan, {B2A[c], numInput}, samplesIn[0].data(), samplesIn[0].size());
1456 /* Band-pass the incoming samples and feed the initial delay line. */
1457 mFilter[c].Lp.process(mTempLine.data(), mTempLine.data(), samplesToDo);
1458 mFilter[c].Hp.process(mTempLine.data(), mTempLine.data(), samplesToDo);
1459 mDelay.write(offset, c, mTempLine.data(), samplesToDo);
1462 /* Process reverb for these samples. */
1463 if LIKELY(!mDoFading)
1465 for(size_t base{0};base < samplesToDo;)
1467 /* Calculate the number of samples we can do this iteration. */
1468 size_t todo{minz(samplesToDo - base, mMaxUpdate[0])};
1469 /* Some mixers require maintaining a 4-sample alignment, so ensure
1470 * that if it's not the last iteration.
1472 if(base+todo < samplesToDo) todo &= ~size_t{3};
1473 ASSUME(todo > 0);
1475 /* Generate non-faded early reflections and late reverb. */
1476 earlyUnfaded(offset, todo);
1477 lateUnfaded(offset, todo);
1479 /* Finally, mix early reflections and late reverb. */
1480 (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
1482 offset += todo;
1483 base += todo;
1486 else
1488 const float fadeStep{1.0f / static_cast<float>(samplesToDo)};
1489 for(size_t base{0};base < samplesToDo;)
1491 size_t todo{minz(samplesToDo - base, minz(mMaxUpdate[0], mMaxUpdate[1]))};
1492 if(base+todo < samplesToDo) todo &= ~size_t{3};
1493 ASSUME(todo > 0);
1495 /* Generate cross-faded early reflections and late reverb. */
1496 auto fadeCount = static_cast<ALfloat>(base);
1497 earlyFaded(offset, todo, fadeCount, fadeStep);
1498 lateFaded(offset, todo, fadeCount, fadeStep);
1500 (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
1502 offset += todo;
1503 base += todo;
1506 /* Update the cross-fading delay line taps. */
1507 for(size_t c{0u};c < NUM_LINES;c++)
1509 mEarlyDelayTap[c][0] = mEarlyDelayTap[c][1];
1510 mEarlyDelayCoeff[c][0] = mEarlyDelayCoeff[c][1];
1511 mEarly.VecAp.Offset[c][0] = mEarly.VecAp.Offset[c][1];
1512 mEarly.Offset[c][0] = mEarly.Offset[c][1];
1513 mEarly.Coeff[c][0] = mEarly.Coeff[c][1];
1514 mLateDelayTap[c][0] = mLateDelayTap[c][1];
1515 mLate.VecAp.Offset[c][0] = mLate.VecAp.Offset[c][1];
1516 mLate.Offset[c][0] = mLate.Offset[c][1];
1517 mLate.T60[c].MidGain[0] = mLate.T60[c].MidGain[1];
1519 mLate.DensityGain[0] = mLate.DensityGain[1];
1520 mMaxUpdate[0] = mMaxUpdate[1];
1521 mDoFading = false;
1523 mOffset = offset;
1527 void EAXReverb_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
1529 switch(param)
1531 case AL_EAXREVERB_DECAY_HFLIMIT:
1532 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
1533 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hflimit out of range");
1534 props->Reverb.DecayHFLimit = val != AL_FALSE;
1535 break;
1537 default:
1538 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
1539 param);
1542 void EAXReverb_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
1543 { EAXReverb_setParami(props, context, param, vals[0]); }
1544 void EAXReverb_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
1546 switch(param)
1548 case AL_EAXREVERB_DENSITY:
1549 if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
1550 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb density out of range");
1551 props->Reverb.Density = val;
1552 break;
1554 case AL_EAXREVERB_DIFFUSION:
1555 if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
1556 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb diffusion out of range");
1557 props->Reverb.Diffusion = val;
1558 break;
1560 case AL_EAXREVERB_GAIN:
1561 if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
1562 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gain out of range");
1563 props->Reverb.Gain = val;
1564 break;
1566 case AL_EAXREVERB_GAINHF:
1567 if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
1568 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainhf out of range");
1569 props->Reverb.GainHF = val;
1570 break;
1572 case AL_EAXREVERB_GAINLF:
1573 if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
1574 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainlf out of range");
1575 props->Reverb.GainLF = val;
1576 break;
1578 case AL_EAXREVERB_DECAY_TIME:
1579 if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
1580 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay time out of range");
1581 props->Reverb.DecayTime = val;
1582 break;
1584 case AL_EAXREVERB_DECAY_HFRATIO:
1585 if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
1586 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hfratio out of range");
1587 props->Reverb.DecayHFRatio = val;
1588 break;
1590 case AL_EAXREVERB_DECAY_LFRATIO:
1591 if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
1592 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay lfratio out of range");
1593 props->Reverb.DecayLFRatio = val;
1594 break;
1596 case AL_EAXREVERB_REFLECTIONS_GAIN:
1597 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
1598 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections gain out of range");
1599 props->Reverb.ReflectionsGain = val;
1600 break;
1602 case AL_EAXREVERB_REFLECTIONS_DELAY:
1603 if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
1604 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections delay out of range");
1605 props->Reverb.ReflectionsDelay = val;
1606 break;
1608 case AL_EAXREVERB_LATE_REVERB_GAIN:
1609 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
1610 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb gain out of range");
1611 props->Reverb.LateReverbGain = val;
1612 break;
1614 case AL_EAXREVERB_LATE_REVERB_DELAY:
1615 if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
1616 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb delay out of range");
1617 props->Reverb.LateReverbDelay = val;
1618 break;
1620 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1621 if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
1622 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb air absorption gainhf out of range");
1623 props->Reverb.AirAbsorptionGainHF = val;
1624 break;
1626 case AL_EAXREVERB_ECHO_TIME:
1627 if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
1628 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo time out of range");
1629 props->Reverb.EchoTime = val;
1630 break;
1632 case AL_EAXREVERB_ECHO_DEPTH:
1633 if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
1634 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo depth out of range");
1635 props->Reverb.EchoDepth = val;
1636 break;
1638 case AL_EAXREVERB_MODULATION_TIME:
1639 if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
1640 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation time out of range");
1641 props->Reverb.ModulationTime = val;
1642 break;
1644 case AL_EAXREVERB_MODULATION_DEPTH:
1645 if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
1646 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation depth out of range");
1647 props->Reverb.ModulationDepth = val;
1648 break;
1650 case AL_EAXREVERB_HFREFERENCE:
1651 if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
1652 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb hfreference out of range");
1653 props->Reverb.HFReference = val;
1654 break;
1656 case AL_EAXREVERB_LFREFERENCE:
1657 if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
1658 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb lfreference out of range");
1659 props->Reverb.LFReference = val;
1660 break;
1662 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1663 if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
1664 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb room rolloff factor out of range");
1665 props->Reverb.RoomRolloffFactor = val;
1666 break;
1668 default:
1669 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x", param);
1672 void EAXReverb_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
1674 switch(param)
1676 case AL_EAXREVERB_REFLECTIONS_PAN:
1677 if(!(std::isfinite(vals[0]) && std::isfinite(vals[1]) && std::isfinite(vals[2])))
1678 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections pan out of range");
1679 props->Reverb.ReflectionsPan[0] = vals[0];
1680 props->Reverb.ReflectionsPan[1] = vals[1];
1681 props->Reverb.ReflectionsPan[2] = vals[2];
1682 break;
1683 case AL_EAXREVERB_LATE_REVERB_PAN:
1684 if(!(std::isfinite(vals[0]) && std::isfinite(vals[1]) && std::isfinite(vals[2])))
1685 SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb pan out of range");
1686 props->Reverb.LateReverbPan[0] = vals[0];
1687 props->Reverb.LateReverbPan[1] = vals[1];
1688 props->Reverb.LateReverbPan[2] = vals[2];
1689 break;
1691 default:
1692 EAXReverb_setParamf(props, context, param, vals[0]);
1693 break;
1697 void EAXReverb_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
1699 switch(param)
1701 case AL_EAXREVERB_DECAY_HFLIMIT:
1702 *val = props->Reverb.DecayHFLimit;
1703 break;
1705 default:
1706 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
1707 param);
1710 void EAXReverb_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
1711 { EAXReverb_getParami(props, context, param, vals); }
1712 void EAXReverb_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
1714 switch(param)
1716 case AL_EAXREVERB_DENSITY:
1717 *val = props->Reverb.Density;
1718 break;
1720 case AL_EAXREVERB_DIFFUSION:
1721 *val = props->Reverb.Diffusion;
1722 break;
1724 case AL_EAXREVERB_GAIN:
1725 *val = props->Reverb.Gain;
1726 break;
1728 case AL_EAXREVERB_GAINHF:
1729 *val = props->Reverb.GainHF;
1730 break;
1732 case AL_EAXREVERB_GAINLF:
1733 *val = props->Reverb.GainLF;
1734 break;
1736 case AL_EAXREVERB_DECAY_TIME:
1737 *val = props->Reverb.DecayTime;
1738 break;
1740 case AL_EAXREVERB_DECAY_HFRATIO:
1741 *val = props->Reverb.DecayHFRatio;
1742 break;
1744 case AL_EAXREVERB_DECAY_LFRATIO:
1745 *val = props->Reverb.DecayLFRatio;
1746 break;
1748 case AL_EAXREVERB_REFLECTIONS_GAIN:
1749 *val = props->Reverb.ReflectionsGain;
1750 break;
1752 case AL_EAXREVERB_REFLECTIONS_DELAY:
1753 *val = props->Reverb.ReflectionsDelay;
1754 break;
1756 case AL_EAXREVERB_LATE_REVERB_GAIN:
1757 *val = props->Reverb.LateReverbGain;
1758 break;
1760 case AL_EAXREVERB_LATE_REVERB_DELAY:
1761 *val = props->Reverb.LateReverbDelay;
1762 break;
1764 case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
1765 *val = props->Reverb.AirAbsorptionGainHF;
1766 break;
1768 case AL_EAXREVERB_ECHO_TIME:
1769 *val = props->Reverb.EchoTime;
1770 break;
1772 case AL_EAXREVERB_ECHO_DEPTH:
1773 *val = props->Reverb.EchoDepth;
1774 break;
1776 case AL_EAXREVERB_MODULATION_TIME:
1777 *val = props->Reverb.ModulationTime;
1778 break;
1780 case AL_EAXREVERB_MODULATION_DEPTH:
1781 *val = props->Reverb.ModulationDepth;
1782 break;
1784 case AL_EAXREVERB_HFREFERENCE:
1785 *val = props->Reverb.HFReference;
1786 break;
1788 case AL_EAXREVERB_LFREFERENCE:
1789 *val = props->Reverb.LFReference;
1790 break;
1792 case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
1793 *val = props->Reverb.RoomRolloffFactor;
1794 break;
1796 default:
1797 context->setError(AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x", param);
1800 void EAXReverb_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
1802 switch(param)
1804 case AL_EAXREVERB_REFLECTIONS_PAN:
1805 vals[0] = props->Reverb.ReflectionsPan[0];
1806 vals[1] = props->Reverb.ReflectionsPan[1];
1807 vals[2] = props->Reverb.ReflectionsPan[2];
1808 break;
1809 case AL_EAXREVERB_LATE_REVERB_PAN:
1810 vals[0] = props->Reverb.LateReverbPan[0];
1811 vals[1] = props->Reverb.LateReverbPan[1];
1812 vals[2] = props->Reverb.LateReverbPan[2];
1813 break;
1815 default:
1816 EAXReverb_getParamf(props, context, param, vals);
1817 break;
1821 DEFINE_ALEFFECT_VTABLE(EAXReverb);
1824 struct ReverbStateFactory final : public EffectStateFactory {
1825 EffectState *create() override { return new ReverbState{}; }
1826 EffectProps getDefaultProps() const noexcept override;
1827 const EffectVtable *getEffectVtable() const noexcept override { return &EAXReverb_vtable; }
1830 EffectProps ReverbStateFactory::getDefaultProps() const noexcept
1832 EffectProps props{};
1833 props.Reverb.Density = AL_EAXREVERB_DEFAULT_DENSITY;
1834 props.Reverb.Diffusion = AL_EAXREVERB_DEFAULT_DIFFUSION;
1835 props.Reverb.Gain = AL_EAXREVERB_DEFAULT_GAIN;
1836 props.Reverb.GainHF = AL_EAXREVERB_DEFAULT_GAINHF;
1837 props.Reverb.GainLF = AL_EAXREVERB_DEFAULT_GAINLF;
1838 props.Reverb.DecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME;
1839 props.Reverb.DecayHFRatio = AL_EAXREVERB_DEFAULT_DECAY_HFRATIO;
1840 props.Reverb.DecayLFRatio = AL_EAXREVERB_DEFAULT_DECAY_LFRATIO;
1841 props.Reverb.ReflectionsGain = AL_EAXREVERB_DEFAULT_REFLECTIONS_GAIN;
1842 props.Reverb.ReflectionsDelay = AL_EAXREVERB_DEFAULT_REFLECTIONS_DELAY;
1843 props.Reverb.ReflectionsPan[0] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
1844 props.Reverb.ReflectionsPan[1] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
1845 props.Reverb.ReflectionsPan[2] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
1846 props.Reverb.LateReverbGain = AL_EAXREVERB_DEFAULT_LATE_REVERB_GAIN;
1847 props.Reverb.LateReverbDelay = AL_EAXREVERB_DEFAULT_LATE_REVERB_DELAY;
1848 props.Reverb.LateReverbPan[0] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
1849 props.Reverb.LateReverbPan[1] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
1850 props.Reverb.LateReverbPan[2] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
1851 props.Reverb.EchoTime = AL_EAXREVERB_DEFAULT_ECHO_TIME;
1852 props.Reverb.EchoDepth = AL_EAXREVERB_DEFAULT_ECHO_DEPTH;
1853 props.Reverb.ModulationTime = AL_EAXREVERB_DEFAULT_MODULATION_TIME;
1854 props.Reverb.ModulationDepth = AL_EAXREVERB_DEFAULT_MODULATION_DEPTH;
1855 props.Reverb.AirAbsorptionGainHF = AL_EAXREVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
1856 props.Reverb.HFReference = AL_EAXREVERB_DEFAULT_HFREFERENCE;
1857 props.Reverb.LFReference = AL_EAXREVERB_DEFAULT_LFREFERENCE;
1858 props.Reverb.RoomRolloffFactor = AL_EAXREVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
1859 props.Reverb.DecayHFLimit = AL_EAXREVERB_DEFAULT_DECAY_HFLIMIT;
1860 return props;
1864 void StdReverb_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
1866 switch(param)
1868 case AL_REVERB_DECAY_HFLIMIT:
1869 if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
1870 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hflimit out of range");
1871 props->Reverb.DecayHFLimit = val != AL_FALSE;
1872 break;
1874 default:
1875 context->setError(AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
1878 void StdReverb_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
1879 { StdReverb_setParami(props, context, param, vals[0]); }
1880 void StdReverb_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
1882 switch(param)
1884 case AL_REVERB_DENSITY:
1885 if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
1886 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb density out of range");
1887 props->Reverb.Density = val;
1888 break;
1890 case AL_REVERB_DIFFUSION:
1891 if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
1892 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb diffusion out of range");
1893 props->Reverb.Diffusion = val;
1894 break;
1896 case AL_REVERB_GAIN:
1897 if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
1898 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gain out of range");
1899 props->Reverb.Gain = val;
1900 break;
1902 case AL_REVERB_GAINHF:
1903 if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
1904 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gainhf out of range");
1905 props->Reverb.GainHF = val;
1906 break;
1908 case AL_REVERB_DECAY_TIME:
1909 if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
1910 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay time out of range");
1911 props->Reverb.DecayTime = val;
1912 break;
1914 case AL_REVERB_DECAY_HFRATIO:
1915 if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
1916 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hfratio out of range");
1917 props->Reverb.DecayHFRatio = val;
1918 break;
1920 case AL_REVERB_REFLECTIONS_GAIN:
1921 if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
1922 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections gain out of range");
1923 props->Reverb.ReflectionsGain = val;
1924 break;
1926 case AL_REVERB_REFLECTIONS_DELAY:
1927 if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
1928 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections delay out of range");
1929 props->Reverb.ReflectionsDelay = val;
1930 break;
1932 case AL_REVERB_LATE_REVERB_GAIN:
1933 if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
1934 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb gain out of range");
1935 props->Reverb.LateReverbGain = val;
1936 break;
1938 case AL_REVERB_LATE_REVERB_DELAY:
1939 if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
1940 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb delay out of range");
1941 props->Reverb.LateReverbDelay = val;
1942 break;
1944 case AL_REVERB_AIR_ABSORPTION_GAINHF:
1945 if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
1946 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb air absorption gainhf out of range");
1947 props->Reverb.AirAbsorptionGainHF = val;
1948 break;
1950 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
1951 if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
1952 SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb room rolloff factor out of range");
1953 props->Reverb.RoomRolloffFactor = val;
1954 break;
1956 default:
1957 context->setError(AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
1960 void StdReverb_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
1961 { StdReverb_setParamf(props, context, param, vals[0]); }
1963 void StdReverb_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
1965 switch(param)
1967 case AL_REVERB_DECAY_HFLIMIT:
1968 *val = props->Reverb.DecayHFLimit;
1969 break;
1971 default:
1972 context->setError(AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
1975 void StdReverb_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
1976 { StdReverb_getParami(props, context, param, vals); }
1977 void StdReverb_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
1979 switch(param)
1981 case AL_REVERB_DENSITY:
1982 *val = props->Reverb.Density;
1983 break;
1985 case AL_REVERB_DIFFUSION:
1986 *val = props->Reverb.Diffusion;
1987 break;
1989 case AL_REVERB_GAIN:
1990 *val = props->Reverb.Gain;
1991 break;
1993 case AL_REVERB_GAINHF:
1994 *val = props->Reverb.GainHF;
1995 break;
1997 case AL_REVERB_DECAY_TIME:
1998 *val = props->Reverb.DecayTime;
1999 break;
2001 case AL_REVERB_DECAY_HFRATIO:
2002 *val = props->Reverb.DecayHFRatio;
2003 break;
2005 case AL_REVERB_REFLECTIONS_GAIN:
2006 *val = props->Reverb.ReflectionsGain;
2007 break;
2009 case AL_REVERB_REFLECTIONS_DELAY:
2010 *val = props->Reverb.ReflectionsDelay;
2011 break;
2013 case AL_REVERB_LATE_REVERB_GAIN:
2014 *val = props->Reverb.LateReverbGain;
2015 break;
2017 case AL_REVERB_LATE_REVERB_DELAY:
2018 *val = props->Reverb.LateReverbDelay;
2019 break;
2021 case AL_REVERB_AIR_ABSORPTION_GAINHF:
2022 *val = props->Reverb.AirAbsorptionGainHF;
2023 break;
2025 case AL_REVERB_ROOM_ROLLOFF_FACTOR:
2026 *val = props->Reverb.RoomRolloffFactor;
2027 break;
2029 default:
2030 context->setError(AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
2033 void StdReverb_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
2034 { StdReverb_getParamf(props, context, param, vals); }
2036 DEFINE_ALEFFECT_VTABLE(StdReverb);
2039 struct StdReverbStateFactory final : public EffectStateFactory {
2040 EffectState *create() override { return new ReverbState{}; }
2041 EffectProps getDefaultProps() const noexcept override;
2042 const EffectVtable *getEffectVtable() const noexcept override { return &StdReverb_vtable; }
2045 EffectProps StdReverbStateFactory::getDefaultProps() const noexcept
2047 EffectProps props{};
2048 props.Reverb.Density = AL_REVERB_DEFAULT_DENSITY;
2049 props.Reverb.Diffusion = AL_REVERB_DEFAULT_DIFFUSION;
2050 props.Reverb.Gain = AL_REVERB_DEFAULT_GAIN;
2051 props.Reverb.GainHF = AL_REVERB_DEFAULT_GAINHF;
2052 props.Reverb.GainLF = 1.0f;
2053 props.Reverb.DecayTime = AL_REVERB_DEFAULT_DECAY_TIME;
2054 props.Reverb.DecayHFRatio = AL_REVERB_DEFAULT_DECAY_HFRATIO;
2055 props.Reverb.DecayLFRatio = 1.0f;
2056 props.Reverb.ReflectionsGain = AL_REVERB_DEFAULT_REFLECTIONS_GAIN;
2057 props.Reverb.ReflectionsDelay = AL_REVERB_DEFAULT_REFLECTIONS_DELAY;
2058 props.Reverb.ReflectionsPan[0] = 0.0f;
2059 props.Reverb.ReflectionsPan[1] = 0.0f;
2060 props.Reverb.ReflectionsPan[2] = 0.0f;
2061 props.Reverb.LateReverbGain = AL_REVERB_DEFAULT_LATE_REVERB_GAIN;
2062 props.Reverb.LateReverbDelay = AL_REVERB_DEFAULT_LATE_REVERB_DELAY;
2063 props.Reverb.LateReverbPan[0] = 0.0f;
2064 props.Reverb.LateReverbPan[1] = 0.0f;
2065 props.Reverb.LateReverbPan[2] = 0.0f;
2066 props.Reverb.EchoTime = 0.25f;
2067 props.Reverb.EchoDepth = 0.0f;
2068 props.Reverb.ModulationTime = 0.25f;
2069 props.Reverb.ModulationDepth = 0.0f;
2070 props.Reverb.AirAbsorptionGainHF = AL_REVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
2071 props.Reverb.HFReference = 5000.0f;
2072 props.Reverb.LFReference = 250.0f;
2073 props.Reverb.RoomRolloffFactor = AL_REVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
2074 props.Reverb.DecayHFLimit = AL_REVERB_DEFAULT_DECAY_HFLIMIT;
2075 return props;
2078 } // namespace
2080 EffectStateFactory *ReverbStateFactory_getFactory()
2082 static ReverbStateFactory ReverbFactory{};
2083 return &ReverbFactory;
2086 EffectStateFactory *StdReverbStateFactory_getFactory()
2088 static StdReverbStateFactory ReverbFactory{};
2089 return &ReverbFactory;