Move some temp variables closer to where they're used
[openal-soft.git] / alc / effects / reverb.cpp
blobf5818c27cdec27e10d4cc8fa8f62ae852952de69
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 <algorithm>
24 #include <array>
25 #include <cstdio>
26 #include <functional>
27 #include <iterator>
28 #include <numeric>
29 #include <stdint.h>
31 #include "alc/effects/base.h"
32 #include "almalloc.h"
33 #include "alnumbers.h"
34 #include "alnumeric.h"
35 #include "alspan.h"
36 #include "core/ambidefs.h"
37 #include "core/bufferline.h"
38 #include "core/context.h"
39 #include "core/devformat.h"
40 #include "core/device.h"
41 #include "core/effectslot.h"
42 #include "core/filters/biquad.h"
43 #include "core/filters/splitter.h"
44 #include "core/mixer.h"
45 #include "core/mixer/defs.h"
46 #include "intrusive_ptr.h"
47 #include "opthelpers.h"
48 #include "vecmat.h"
49 #include "vector.h"
51 /* This is a user config option for modifying the overall output of the reverb
52 * effect.
54 float ReverbBoost = 1.0f;
56 namespace {
58 using uint = unsigned int;
60 constexpr float MaxModulationTime{4.0f};
61 constexpr float DefaultModulationTime{0.25f};
63 #define MOD_FRACBITS 24
64 #define MOD_FRACONE (1<<MOD_FRACBITS)
65 #define MOD_FRACMASK (MOD_FRACONE-1)
68 using namespace std::placeholders;
70 /* Max samples per process iteration. Used to limit the size needed for
71 * temporary buffers. Must be a multiple of 4 for SIMD alignment.
73 constexpr size_t MAX_UPDATE_SAMPLES{256};
75 /* The number of spatialized lines or channels to process. Four channels allows
76 * for a 3D A-Format response. NOTE: This can't be changed without taking care
77 * of the conversion matrices, and a few places where the length arrays are
78 * assumed to have 4 elements.
80 constexpr size_t NUM_LINES{4u};
83 /* This coefficient is used to define the maximum frequency range controlled by
84 * the modulation depth. The current value of 0.05 will allow it to swing from
85 * 0.95x to 1.05x. This value must be below 1. At 1 it will cause the sampler
86 * to stall on the downswing, and above 1 it will cause it to sample backwards.
87 * The value 0.05 seems be nearest to Creative hardware behavior.
89 constexpr float MODULATION_DEPTH_COEFF{0.05f};
92 /* The B-Format to A-Format conversion matrix. The arrangement of rows is
93 * deliberately chosen to align the resulting lines to their spatial opposites
94 * (0:above front left <-> 3:above back right, 1:below front right <-> 2:below
95 * back left). It's not quite opposite, since the A-Format results in a
96 * tetrahedron, but it's close enough. Should the model be extended to 8-lines
97 * in the future, true opposites can be used.
99 alignas(16) constexpr float B2A[NUM_LINES][NUM_LINES]{
100 { 0.5f, 0.5f, 0.5f, 0.5f },
101 { 0.5f, -0.5f, -0.5f, 0.5f },
102 { 0.5f, 0.5f, -0.5f, -0.5f },
103 { 0.5f, -0.5f, 0.5f, -0.5f }
106 /* Converts A-Format to B-Format for early reflections. */
107 alignas(16) constexpr float EarlyA2B[NUM_LINES][NUM_LINES]{
108 { 0.5f, 0.5f, 0.5f, 0.5f },
109 { 0.5f, -0.5f, 0.5f, -0.5f },
110 { 0.5f, -0.5f, -0.5f, 0.5f },
111 { 0.5f, 0.5f, -0.5f, -0.5f }
114 /* Converts A-Format to B-Format for late reverb. */
115 constexpr auto InvSqrt2 = static_cast<float>(1.0/al::numbers::sqrt2);
116 alignas(16) constexpr float LateA2B[NUM_LINES][NUM_LINES]{
117 { 0.5f, 0.5f, 0.5f, 0.5f },
118 { InvSqrt2, -InvSqrt2, 0.0f, 0.0f },
119 { 0.0f, 0.0f, InvSqrt2, -InvSqrt2 },
120 { 0.5f, 0.5f, -0.5f, -0.5f }
123 /* The all-pass and delay lines have a variable length dependent on the
124 * effect's density parameter, which helps alter the perceived environment
125 * size. The size-to-density conversion is a cubed scale:
127 * density = min(1.0, pow(size, 3.0) / DENSITY_SCALE);
129 * The line lengths scale linearly with room size, so the inverse density
130 * conversion is needed, taking the cube root of the re-scaled density to
131 * calculate the line length multiplier:
133 * length_mult = max(5.0, cbrt(density*DENSITY_SCALE));
135 * The density scale below will result in a max line multiplier of 50, for an
136 * effective size range of 5m to 50m.
138 constexpr float DENSITY_SCALE{125000.0f};
140 /* All delay line lengths are specified in seconds.
142 * To approximate early reflections, we break them up into primary (those
143 * arriving from the same direction as the source) and secondary (those
144 * arriving from the opposite direction).
146 * The early taps decorrelate the 4-channel signal to approximate an average
147 * room response for the primary reflections after the initial early delay.
149 * Given an average room dimension (d_a) and the speed of sound (c) we can
150 * calculate the average reflection delay (r_a) regardless of listener and
151 * source positions as:
153 * r_a = d_a / c
154 * c = 343.3
156 * This can extended to finding the average difference (r_d) between the
157 * maximum (r_1) and minimum (r_0) reflection delays:
159 * r_0 = 2 / 3 r_a
160 * = r_a - r_d / 2
161 * = r_d
162 * r_1 = 4 / 3 r_a
163 * = r_a + r_d / 2
164 * = 2 r_d
165 * r_d = 2 / 3 r_a
166 * = r_1 - r_0
168 * As can be determined by integrating the 1D model with a source (s) and
169 * listener (l) positioned across the dimension of length (d_a):
171 * r_d = int_(l=0)^d_a (int_(s=0)^d_a |2 d_a - 2 (l + s)| ds) dl / c
173 * The initial taps (T_(i=0)^N) are then specified by taking a power series
174 * that ranges between r_0 and half of r_1 less r_0:
176 * R_i = 2^(i / (2 N - 1)) r_d
177 * = r_0 + (2^(i / (2 N - 1)) - 1) r_d
178 * = r_0 + T_i
179 * T_i = R_i - r_0
180 * = (2^(i / (2 N - 1)) - 1) r_d
182 * Assuming an average of 1m, we get the following taps:
184 constexpr std::array<float,NUM_LINES> EARLY_TAP_LENGTHS{{
185 0.0000000e+0f, 2.0213520e-4f, 4.2531060e-4f, 6.7171600e-4f
188 /* The early all-pass filter lengths are based on the early tap lengths:
190 * A_i = R_i / a
192 * Where a is the approximate maximum all-pass cycle limit (20).
194 constexpr std::array<float,NUM_LINES> EARLY_ALLPASS_LENGTHS{{
195 9.7096800e-5f, 1.0720356e-4f, 1.1836234e-4f, 1.3068260e-4f
198 /* The early delay lines are used to transform the primary reflections into
199 * the secondary reflections. The A-format is arranged in such a way that
200 * the channels/lines are spatially opposite:
202 * C_i is opposite C_(N-i-1)
204 * The delays of the two opposing reflections (R_i and O_i) from a source
205 * anywhere along a particular dimension always sum to twice its full delay:
207 * 2 r_a = R_i + O_i
209 * With that in mind we can determine the delay between the two reflections
210 * and thus specify our early line lengths (L_(i=0)^N) using:
212 * O_i = 2 r_a - R_(N-i-1)
213 * L_i = O_i - R_(N-i-1)
214 * = 2 (r_a - R_(N-i-1))
215 * = 2 (r_a - T_(N-i-1) - r_0)
216 * = 2 r_a (1 - (2 / 3) 2^((N - i - 1) / (2 N - 1)))
218 * Using an average dimension of 1m, we get:
220 constexpr std::array<float,NUM_LINES> EARLY_LINE_LENGTHS{{
221 5.9850400e-4f, 1.0913150e-3f, 1.5376658e-3f, 1.9419362e-3f
224 /* The late all-pass filter lengths are based on the late line lengths:
226 * A_i = (5 / 3) L_i / r_1
228 constexpr std::array<float,NUM_LINES> LATE_ALLPASS_LENGTHS{{
229 1.6182800e-4f, 2.0389060e-4f, 2.8159360e-4f, 3.2365600e-4f
232 /* The late lines are used to approximate the decaying cycle of recursive
233 * late reflections.
235 * Splitting the lines in half, we start with the shortest reflection paths
236 * (L_(i=0)^(N/2)):
238 * L_i = 2^(i / (N - 1)) r_d
240 * Then for the opposite (longest) reflection paths (L_(i=N/2)^N):
242 * L_i = 2 r_a - L_(i-N/2)
243 * = 2 r_a - 2^((i - N / 2) / (N - 1)) r_d
245 * For our 1m average room, we get:
247 constexpr std::array<float,NUM_LINES> LATE_LINE_LENGTHS{{
248 1.9419362e-3f, 2.4466860e-3f, 3.3791220e-3f, 3.8838720e-3f
252 using ReverbUpdateLine = std::array<float,MAX_UPDATE_SAMPLES>;
254 struct DelayLineI {
255 /* The delay lines use interleaved samples, with the lengths being powers
256 * of 2 to allow the use of bit-masking instead of a modulus for wrapping.
258 size_t Mask{0u};
259 union {
260 uintptr_t LineOffset{0u};
261 std::array<float,NUM_LINES> *Line;
264 /* Given the allocated sample buffer, this function updates each delay line
265 * offset.
267 void realizeLineOffset(std::array<float,NUM_LINES> *sampleBuffer) noexcept
268 { Line = sampleBuffer + LineOffset; }
270 /* Calculate the length of a delay line and store its mask and offset. */
271 uint calcLineLength(const float length, const uintptr_t offset, const float frequency,
272 const uint extra)
274 /* All line lengths are powers of 2, calculated from their lengths in
275 * seconds, rounded up.
277 uint samples{float2uint(std::ceil(length*frequency))};
278 samples = NextPowerOf2(samples + extra);
280 /* All lines share a single sample buffer. */
281 Mask = samples - 1;
282 LineOffset = offset;
284 /* Return the sample count for accumulation. */
285 return samples;
288 void write(size_t offset, const size_t c, const float *RESTRICT in, const size_t count) const noexcept
290 ASSUME(count > 0);
291 for(size_t i{0u};i < count;)
293 offset &= Mask;
294 size_t td{minz(Mask+1 - offset, count - i)};
295 do {
296 Line[offset++][c] = in[i++];
297 } while(--td);
302 struct VecAllpass {
303 DelayLineI Delay;
304 float Coeff{0.0f};
305 size_t Offset[NUM_LINES][2]{};
307 void processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
308 const float xCoeff, const float yCoeff, float fadeCount, const float fadeStep,
309 const size_t todo);
310 void processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
311 const float xCoeff, const float yCoeff, const size_t todo);
314 struct T60Filter {
315 /* Two filters are used to adjust the signal. One to control the low
316 * frequencies, and one to control the high frequencies.
318 float MidGain[2]{0.0f, 0.0f};
319 BiquadFilter HFFilter, LFFilter;
321 void calcCoeffs(const float length, const float lfDecayTime, const float mfDecayTime,
322 const float hfDecayTime, const float lf0norm, const float hf0norm);
324 /* Applies the two T60 damping filter sections. */
325 void process(const al::span<float> samples)
326 { DualBiquad{HFFilter, LFFilter}.process(samples, samples.data()); }
329 struct EarlyReflections {
330 /* A Gerzon vector all-pass filter is used to simulate initial diffusion.
331 * The spread from this filter also helps smooth out the reverb tail.
333 VecAllpass VecAp;
335 /* An echo line is used to complete the second half of the early
336 * reflections.
338 DelayLineI Delay;
339 size_t Offset[NUM_LINES][2]{};
340 float Coeff[NUM_LINES][2]{};
342 /* The gain for each output channel based on 3D panning. */
343 float CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
344 float PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
346 void updateLines(const float density_mult, const float diffusion, const float decayTime,
347 const float frequency);
351 struct Modulation {
352 /* The vibrato time is tracked with an index over a (MOD_FRACONE)
353 * normalized range.
355 uint Index, Step;
357 /* The depth of frequency change, in samples. */
358 float Depth[2];
360 float ModDelays[MAX_UPDATE_SAMPLES];
362 void updateModulator(float modTime, float modDepth, float frequency);
364 void calcDelays(size_t todo);
365 void calcFadedDelays(size_t todo, float fadeCount, float fadeStep);
368 struct LateReverb {
369 /* A recursive delay line is used fill in the reverb tail. */
370 DelayLineI Delay;
371 size_t Offset[NUM_LINES][2]{};
373 /* Attenuation to compensate for the modal density and decay rate of the
374 * late lines.
376 float DensityGain[2]{0.0f, 0.0f};
378 /* T60 decay filters are used to simulate absorption. */
379 T60Filter T60[NUM_LINES];
381 Modulation Mod;
383 /* A Gerzon vector all-pass filter is used to simulate diffusion. */
384 VecAllpass VecAp;
386 /* The gain for each output channel based on 3D panning. */
387 float CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
388 float PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
390 void updateLines(const float density_mult, const float diffusion, const float lfDecayTime,
391 const float mfDecayTime, const float hfDecayTime, const float lf0norm,
392 const float hf0norm, const float frequency);
395 struct ReverbState final : public EffectState {
396 /* All delay lines are allocated as a single buffer to reduce memory
397 * fragmentation and management code.
399 al::vector<std::array<float,NUM_LINES>,16> mSampleBuffer;
401 struct {
402 /* Calculated parameters which indicate if cross-fading is needed after
403 * an update.
405 float Density{1.0f};
406 float Diffusion{1.0f};
407 float DecayTime{1.49f};
408 float HFDecayTime{0.83f * 1.49f};
409 float LFDecayTime{1.0f * 1.49f};
410 float ModulationTime{0.25f};
411 float ModulationDepth{0.0f};
412 float HFReference{5000.0f};
413 float LFReference{250.0f};
414 } mParams;
416 /* Master effect filters */
417 struct {
418 BiquadFilter Lp;
419 BiquadFilter Hp;
420 } mFilter[NUM_LINES];
422 /* Core delay line (early reflections and late reverb tap from this). */
423 DelayLineI mDelay;
425 /* Tap points for early reflection delay. */
426 size_t mEarlyDelayTap[NUM_LINES][2]{};
427 float mEarlyDelayCoeff[NUM_LINES][2]{};
429 /* Tap points for late reverb feed and delay. */
430 size_t mLateFeedTap{};
431 size_t mLateDelayTap[NUM_LINES][2]{};
433 /* Coefficients for the all-pass and line scattering matrices. */
434 float mMixX{0.0f};
435 float mMixY{0.0f};
437 EarlyReflections mEarly;
439 LateReverb mLate;
441 bool mDoFading{};
443 /* Maximum number of samples to process at once. */
444 size_t mMaxUpdate[2]{MAX_UPDATE_SAMPLES, MAX_UPDATE_SAMPLES};
446 /* The current write offset for all delay lines. */
447 size_t mOffset{};
449 /* Temporary storage used when processing. */
450 union {
451 alignas(16) FloatBufferLine mTempLine{};
452 alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mTempSamples;
454 alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mEarlySamples{};
455 alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mLateSamples{};
457 using MixOutT = void (ReverbState::*)(const al::span<FloatBufferLine> samplesOut,
458 const size_t counter, const size_t offset, const size_t todo);
460 MixOutT mMixOut{&ReverbState::MixOutPlain};
461 std::array<float,MaxAmbiOrder+1> mOrderScales{};
462 std::array<std::array<BandSplitter,NUM_LINES>,2> mAmbiSplitter;
465 static void DoMixRow(const al::span<float> OutBuffer, const al::span<const float> Gains,
466 const float *InSamples, const size_t InStride)
468 std::fill(OutBuffer.begin(), OutBuffer.end(), 0.0f);
469 for(const float gain : Gains)
471 const float *RESTRICT input{al::assume_aligned<16>(InSamples)};
472 InSamples += InStride;
474 if(!(std::fabs(gain) > GainSilenceThreshold))
475 continue;
477 for(float &sample : OutBuffer)
479 sample += *input * gain;
480 ++input;
486 void MixOutPlain(const al::span<FloatBufferLine> samplesOut, const size_t counter,
487 const size_t offset, const size_t todo)
489 ASSUME(todo > 0);
491 /* Convert back to B-Format, and mix the results to output. */
492 const al::span<float> tmpspan{al::assume_aligned<16>(mTempLine.data()), todo};
493 for(size_t c{0u};c < NUM_LINES;c++)
495 DoMixRow(tmpspan, EarlyA2B[c], mEarlySamples[0].data(), mEarlySamples[0].size());
496 MixSamples(tmpspan, samplesOut, mEarly.CurrentGain[c], mEarly.PanGain[c], counter,
497 offset);
499 for(size_t c{0u};c < NUM_LINES;c++)
501 DoMixRow(tmpspan, LateA2B[c], mLateSamples[0].data(), mLateSamples[0].size());
502 MixSamples(tmpspan, samplesOut, mLate.CurrentGain[c], mLate.PanGain[c], counter,
503 offset);
507 void MixOutAmbiUp(const al::span<FloatBufferLine> samplesOut, const size_t counter,
508 const size_t offset, const size_t todo)
510 ASSUME(todo > 0);
512 const al::span<float> tmpspan{al::assume_aligned<16>(mTempLine.data()), todo};
513 for(size_t c{0u};c < NUM_LINES;c++)
515 DoMixRow(tmpspan, EarlyA2B[c], mEarlySamples[0].data(), mEarlySamples[0].size());
517 /* Apply scaling to the B-Format's HF response to "upsample" it to
518 * higher-order output.
520 const float hfscale{(c==0) ? mOrderScales[0] : mOrderScales[1]};
521 mAmbiSplitter[0][c].processHfScale(tmpspan, hfscale);
523 MixSamples(tmpspan, samplesOut, mEarly.CurrentGain[c], mEarly.PanGain[c], counter,
524 offset);
526 for(size_t c{0u};c < NUM_LINES;c++)
528 DoMixRow(tmpspan, LateA2B[c], mLateSamples[0].data(), mLateSamples[0].size());
530 const float hfscale{(c==0) ? mOrderScales[0] : mOrderScales[1]};
531 mAmbiSplitter[1][c].processHfScale(tmpspan, hfscale);
533 MixSamples(tmpspan, samplesOut, mLate.CurrentGain[c], mLate.PanGain[c], counter,
534 offset);
538 void allocLines(const float frequency);
540 void updateDelayLine(const float earlyDelay, const float lateDelay, const float density_mult,
541 const float decayTime, const float frequency);
542 void update3DPanning(const float *ReflectionsPan, const float *LateReverbPan,
543 const float earlyGain, const float lateGain, const EffectTarget &target);
545 void earlyUnfaded(const size_t offset, const size_t todo);
546 void earlyFaded(const size_t offset, const size_t todo, const float fade,
547 const float fadeStep);
549 void lateUnfaded(const size_t offset, const size_t todo);
550 void lateFaded(const size_t offset, const size_t todo, const float fade,
551 const float fadeStep);
553 void deviceUpdate(const DeviceBase *device, const Buffer &buffer) override;
554 void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
555 const EffectTarget target) override;
556 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
557 const al::span<FloatBufferLine> samplesOut) override;
559 DEF_NEWDEL(ReverbState)
562 /**************************************
563 * Device Update *
564 **************************************/
566 inline float CalcDelayLengthMult(float density)
567 { return maxf(5.0f, std::cbrt(density*DENSITY_SCALE)); }
569 /* Calculates the delay line metrics and allocates the shared sample buffer
570 * for all lines given the sample rate (frequency).
572 void ReverbState::allocLines(const float frequency)
574 /* All delay line lengths are calculated to accomodate the full range of
575 * lengths given their respective paramters.
577 size_t totalSamples{0u};
579 /* Multiplier for the maximum density value, i.e. density=1, which is
580 * actually the least density...
582 const float multiplier{CalcDelayLengthMult(1.0f)};
584 /* The main delay length includes the maximum early reflection delay, the
585 * largest early tap width, the maximum late reverb delay, and the
586 * largest late tap width. Finally, it must also be extended by the
587 * update size (BufferLineSize) for block processing.
589 constexpr float LateLineDiffAvg{(LATE_LINE_LENGTHS.back()-LATE_LINE_LENGTHS.front()) /
590 float{NUM_LINES}};
591 float length{ReverbMaxReflectionsDelay + EARLY_TAP_LENGTHS.back()*multiplier +
592 ReverbMaxLateReverbDelay + LateLineDiffAvg*multiplier};
593 totalSamples += mDelay.calcLineLength(length, totalSamples, frequency, BufferLineSize);
595 /* The early vector all-pass line. */
596 length = EARLY_ALLPASS_LENGTHS.back() * multiplier;
597 totalSamples += mEarly.VecAp.Delay.calcLineLength(length, totalSamples, frequency, 0);
599 /* The early reflection line. */
600 length = EARLY_LINE_LENGTHS.back() * multiplier;
601 totalSamples += mEarly.Delay.calcLineLength(length, totalSamples, frequency, 0);
603 /* The late vector all-pass line. */
604 length = LATE_ALLPASS_LENGTHS.back() * multiplier;
605 totalSamples += mLate.VecAp.Delay.calcLineLength(length, totalSamples, frequency, 0);
607 /* The modulator's line length is calculated from the maximum modulation
608 * time and depth coefficient, and halfed for the low-to-high frequency
609 * swing.
611 constexpr float max_mod_delay{MaxModulationTime*MODULATION_DEPTH_COEFF / 2.0f};
613 /* The late delay lines are calculated from the largest maximum density
614 * line length, and the maximum modulation delay. An additional sample is
615 * added to keep it stable when there is no modulation.
617 length = LATE_LINE_LENGTHS.back()*multiplier + max_mod_delay;
618 totalSamples += mLate.Delay.calcLineLength(length, totalSamples, frequency, 1);
620 if(totalSamples != mSampleBuffer.size())
621 decltype(mSampleBuffer)(totalSamples).swap(mSampleBuffer);
623 /* Clear the sample buffer. */
624 std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), decltype(mSampleBuffer)::value_type{});
626 /* Update all delays to reflect the new sample buffer. */
627 mDelay.realizeLineOffset(mSampleBuffer.data());
628 mEarly.VecAp.Delay.realizeLineOffset(mSampleBuffer.data());
629 mEarly.Delay.realizeLineOffset(mSampleBuffer.data());
630 mLate.VecAp.Delay.realizeLineOffset(mSampleBuffer.data());
631 mLate.Delay.realizeLineOffset(mSampleBuffer.data());
634 void ReverbState::deviceUpdate(const DeviceBase *device, const Buffer&)
636 const auto frequency = static_cast<float>(device->Frequency);
638 /* Allocate the delay lines. */
639 allocLines(frequency);
641 const float multiplier{CalcDelayLengthMult(1.0f)};
643 /* The late feed taps are set a fixed position past the latest delay tap. */
644 mLateFeedTap = float2uint((ReverbMaxReflectionsDelay + EARLY_TAP_LENGTHS.back()*multiplier) *
645 frequency);
647 /* Clear filters and gain coefficients since the delay lines were all just
648 * cleared (if not reallocated).
650 for(auto &filter : mFilter)
652 filter.Lp.clear();
653 filter.Hp.clear();
656 for(auto &coeff : mEarlyDelayCoeff)
657 std::fill(std::begin(coeff), std::end(coeff), 0.0f);
658 for(auto &coeff : mEarly.Coeff)
659 std::fill(std::begin(coeff), std::end(coeff), 0.0f);
661 mLate.DensityGain[0] = 0.0f;
662 mLate.DensityGain[1] = 0.0f;
663 for(auto &t60 : mLate.T60)
665 t60.MidGain[0] = 0.0f;
666 t60.MidGain[1] = 0.0f;
667 t60.HFFilter.clear();
668 t60.LFFilter.clear();
671 mLate.Mod.Index = 0;
672 mLate.Mod.Step = 1;
673 std::fill(std::begin(mLate.Mod.Depth), std::end(mLate.Mod.Depth), 0.0f);
675 for(auto &gains : mEarly.CurrentGain)
676 std::fill(std::begin(gains), std::end(gains), 0.0f);
677 for(auto &gains : mEarly.PanGain)
678 std::fill(std::begin(gains), std::end(gains), 0.0f);
679 for(auto &gains : mLate.CurrentGain)
680 std::fill(std::begin(gains), std::end(gains), 0.0f);
681 for(auto &gains : mLate.PanGain)
682 std::fill(std::begin(gains), std::end(gains), 0.0f);
684 /* Reset fading and offset base. */
685 mDoFading = true;
686 std::fill(std::begin(mMaxUpdate), std::end(mMaxUpdate), MAX_UPDATE_SAMPLES);
687 mOffset = 0;
689 if(device->mAmbiOrder > 1)
691 mMixOut = &ReverbState::MixOutAmbiUp;
692 mOrderScales = AmbiScale::GetHFOrderScales(1, device->mAmbiOrder);
694 else
696 mMixOut = &ReverbState::MixOutPlain;
697 mOrderScales.fill(1.0f);
699 mAmbiSplitter[0][0].init(device->mXOverFreq / frequency);
700 std::fill(mAmbiSplitter[0].begin()+1, mAmbiSplitter[0].end(), mAmbiSplitter[0][0]);
701 std::fill(mAmbiSplitter[1].begin(), mAmbiSplitter[1].end(), mAmbiSplitter[0][0]);
704 /**************************************
705 * Effect Update *
706 **************************************/
708 /* Calculate a decay coefficient given the length of each cycle and the time
709 * until the decay reaches -60 dB.
711 inline float CalcDecayCoeff(const float length, const float decayTime)
712 { return std::pow(ReverbDecayGain, length/decayTime); }
714 /* Calculate a decay length from a coefficient and the time until the decay
715 * reaches -60 dB.
717 inline float CalcDecayLength(const float coeff, const float decayTime)
719 constexpr float log10_decaygain{-3.0f/*std::log10(ReverbDecayGain)*/};
720 return std::log10(coeff) * decayTime / log10_decaygain;
723 /* Calculate an attenuation to be applied to the input of any echo models to
724 * compensate for modal density and decay time.
726 inline float CalcDensityGain(const float a)
728 /* The energy of a signal can be obtained by finding the area under the
729 * squared signal. This takes the form of Sum(x_n^2), where x is the
730 * amplitude for the sample n.
732 * Decaying feedback matches exponential decay of the form Sum(a^n),
733 * where a is the attenuation coefficient, and n is the sample. The area
734 * under this decay curve can be calculated as: 1 / (1 - a).
736 * Modifying the above equation to find the area under the squared curve
737 * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
738 * calculated by inverting the square root of this approximation,
739 * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
741 return std::sqrt(1.0f - a*a);
744 /* Calculate the scattering matrix coefficients given a diffusion factor. */
745 inline void CalcMatrixCoeffs(const float diffusion, float *x, float *y)
747 /* The matrix is of order 4, so n is sqrt(4 - 1). */
748 constexpr float n{al::numbers::sqrt3_v<float>};
749 const float t{diffusion * std::atan(n)};
751 /* Calculate the first mixing matrix coefficient. */
752 *x = std::cos(t);
753 /* Calculate the second mixing matrix coefficient. */
754 *y = std::sin(t) / n;
757 /* Calculate the limited HF ratio for use with the late reverb low-pass
758 * filters.
760 float CalcLimitedHfRatio(const float hfRatio, const float airAbsorptionGainHF,
761 const float decayTime)
763 /* Find the attenuation due to air absorption in dB (converting delay
764 * time to meters using the speed of sound). Then reversing the decay
765 * equation, solve for HF ratio. The delay length is cancelled out of
766 * the equation, so it can be calculated once for all lines.
768 float limitRatio{1.0f / SpeedOfSoundMetersPerSec /
769 CalcDecayLength(airAbsorptionGainHF, decayTime)};
771 /* Using the limit calculated above, apply the upper bound to the HF ratio. */
772 return minf(limitRatio, hfRatio);
776 /* Calculates the 3-band T60 damping coefficients for a particular delay line
777 * of specified length, using a combination of two shelf filter sections given
778 * decay times for each band split at two reference frequencies.
780 void T60Filter::calcCoeffs(const float length, const float lfDecayTime,
781 const float mfDecayTime, const float hfDecayTime, const float lf0norm,
782 const float hf0norm)
784 const float mfGain{CalcDecayCoeff(length, mfDecayTime)};
785 const float lfGain{CalcDecayCoeff(length, lfDecayTime) / mfGain};
786 const float hfGain{CalcDecayCoeff(length, hfDecayTime) / mfGain};
788 MidGain[1] = mfGain;
789 LFFilter.setParamsFromSlope(BiquadType::LowShelf, lf0norm, lfGain, 1.0f);
790 HFFilter.setParamsFromSlope(BiquadType::HighShelf, hf0norm, hfGain, 1.0f);
793 /* Update the early reflection line lengths and gain coefficients. */
794 void EarlyReflections::updateLines(const float density_mult, const float diffusion,
795 const float decayTime, const float frequency)
797 /* Calculate the all-pass feed-back/forward coefficient. */
798 VecAp.Coeff = diffusion*diffusion * InvSqrt2;
800 for(size_t i{0u};i < NUM_LINES;i++)
802 /* Calculate the delay length of each all-pass line. */
803 float length{EARLY_ALLPASS_LENGTHS[i] * density_mult};
804 VecAp.Offset[i][1] = float2uint(length * frequency);
806 /* Calculate the delay length of each delay line. */
807 length = EARLY_LINE_LENGTHS[i] * density_mult;
808 Offset[i][1] = float2uint(length * frequency);
810 /* Calculate the gain (coefficient) for each line. */
811 Coeff[i][1] = CalcDecayCoeff(length, decayTime);
815 /* Update the EAX modulation step and depth. Keep in mind that this kind of
816 * vibrato is additive and not multiplicative as one may expect. The downswing
817 * will sound stronger than the upswing.
819 void Modulation::updateModulator(float modTime, float modDepth, float frequency)
821 /* Modulation is calculated in two parts.
823 * The modulation time effects the sinus rate, altering the speed of
824 * frequency changes. An index is incremented for each sample with an
825 * appropriate step size to generate an LFO, which will vary the feedback
826 * delay over time.
828 Step = maxu(fastf2u(MOD_FRACONE / (frequency * modTime)), 1);
830 /* The modulation depth effects the amount of frequency change over the
831 * range of the sinus. It needs to be scaled by the modulation time so that
832 * a given depth produces a consistent change in frequency over all ranges
833 * of time. Since the depth is applied to a sinus value, it needs to be
834 * halved once for the sinus range and again for the sinus swing in time
835 * (half of it is spent decreasing the frequency, half is spent increasing
836 * it).
838 if(modTime >= DefaultModulationTime)
840 /* To cancel the effects of a long period modulation on the late
841 * reverberation, the amount of pitch should be varied (decreased)
842 * according to the modulation time. The natural form is varying
843 * inversely, in fact resulting in an invariant.
845 Depth[1] = MODULATION_DEPTH_COEFF / 4.0f * DefaultModulationTime * modDepth * frequency;
847 else
848 Depth[1] = MODULATION_DEPTH_COEFF / 4.0f * modTime * modDepth * frequency;
851 /* Update the late reverb line lengths and T60 coefficients. */
852 void LateReverb::updateLines(const float density_mult, const float diffusion,
853 const float lfDecayTime, const float mfDecayTime, const float hfDecayTime,
854 const float lf0norm, const float hf0norm, const float frequency)
856 /* Scaling factor to convert the normalized reference frequencies from
857 * representing 0...freq to 0...max_reference.
859 constexpr float MaxHFReference{20000.0f};
860 const float norm_weight_factor{frequency / MaxHFReference};
862 const float late_allpass_avg{
863 std::accumulate(LATE_ALLPASS_LENGTHS.begin(), LATE_ALLPASS_LENGTHS.end(), 0.0f) /
864 float{NUM_LINES}};
866 /* To compensate for changes in modal density and decay time of the late
867 * reverb signal, the input is attenuated based on the maximal energy of
868 * the outgoing signal. This approximation is used to keep the apparent
869 * energy of the signal equal for all ranges of density and decay time.
871 * The average length of the delay lines is used to calculate the
872 * attenuation coefficient.
874 float length{std::accumulate(LATE_LINE_LENGTHS.begin(), LATE_LINE_LENGTHS.end(), 0.0f) /
875 float{NUM_LINES} + late_allpass_avg};
876 length *= density_mult;
877 /* The density gain calculation uses an average decay time weighted by
878 * approximate bandwidth. This attempts to compensate for losses of energy
879 * that reduce decay time due to scattering into highly attenuated bands.
881 const float decayTimeWeighted{
882 lf0norm*norm_weight_factor*lfDecayTime +
883 (hf0norm - lf0norm)*norm_weight_factor*mfDecayTime +
884 (1.0f - hf0norm*norm_weight_factor)*hfDecayTime};
885 DensityGain[1] = CalcDensityGain(CalcDecayCoeff(length, decayTimeWeighted));
887 /* Calculate the all-pass feed-back/forward coefficient. */
888 VecAp.Coeff = diffusion*diffusion * InvSqrt2;
890 for(size_t i{0u};i < NUM_LINES;i++)
892 /* Calculate the delay length of each all-pass line. */
893 length = LATE_ALLPASS_LENGTHS[i] * density_mult;
894 VecAp.Offset[i][1] = float2uint(length * frequency);
896 /* Calculate the delay length of each feedback delay line. */
897 length = LATE_LINE_LENGTHS[i] * density_mult;
898 Offset[i][1] = float2uint(length*frequency + 0.5f);
900 /* Approximate the absorption that the vector all-pass would exhibit
901 * given the current diffusion so we don't have to process a full T60
902 * filter for each of its four lines. Also include the average
903 * modulation delay (depth is half the max delay in samples).
905 length += lerp(LATE_ALLPASS_LENGTHS[i], late_allpass_avg, diffusion)*density_mult +
906 Mod.Depth[1]/frequency;
908 /* Calculate the T60 damping coefficients for each line. */
909 T60[i].calcCoeffs(length, lfDecayTime, mfDecayTime, hfDecayTime, lf0norm, hf0norm);
914 /* Update the offsets for the main effect delay line. */
915 void ReverbState::updateDelayLine(const float earlyDelay, const float lateDelay,
916 const float density_mult, const float decayTime, const float frequency)
918 /* Early reflection taps are decorrelated by means of an average room
919 * reflection approximation described above the definition of the taps.
920 * This approximation is linear and so the above density multiplier can
921 * be applied to adjust the width of the taps. A single-band decay
922 * coefficient is applied to simulate initial attenuation and absorption.
924 * Late reverb taps are based on the late line lengths to allow a zero-
925 * delay path and offsets that would continue the propagation naturally
926 * into the late lines.
928 for(size_t i{0u};i < NUM_LINES;i++)
930 float length{EARLY_TAP_LENGTHS[i]*density_mult};
931 mEarlyDelayTap[i][1] = float2uint((earlyDelay+length) * frequency);
932 mEarlyDelayCoeff[i][1] = CalcDecayCoeff(length, decayTime);
934 length = (LATE_LINE_LENGTHS[i] - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*density_mult +
935 lateDelay;
936 mLateDelayTap[i][1] = mLateFeedTap + float2uint(length * frequency);
940 /* Creates a transform matrix given a reverb vector. The vector pans the reverb
941 * reflections toward the given direction, using its magnitude (up to 1) as a
942 * focal strength. This function results in a B-Format transformation matrix
943 * that spatially focuses the signal in the desired direction.
945 alu::Matrix GetTransformFromVector(const float *vec)
947 /* Normalize the panning vector according to the N3D scale, which has an
948 * extra sqrt(3) term on the directional components. Converting from OpenAL
949 * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however
950 * that the reverb panning vectors use left-handed coordinates, unlike the
951 * rest of OpenAL which use right-handed. This is fixed by negating Z,
952 * which cancels out with the B-Format Z negation.
954 float norm[3];
955 float mag{std::sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])};
956 if(mag > 1.0f)
958 norm[0] = vec[0] / mag * -al::numbers::sqrt3_v<float>;
959 norm[1] = vec[1] / mag * al::numbers::sqrt3_v<float>;
960 norm[2] = vec[2] / mag * al::numbers::sqrt3_v<float>;
961 mag = 1.0f;
963 else
965 /* If the magnitude is less than or equal to 1, just apply the sqrt(3)
966 * term. There's no need to renormalize the magnitude since it would
967 * just be reapplied in the matrix.
969 norm[0] = vec[0] * -al::numbers::sqrt3_v<float>;
970 norm[1] = vec[1] * al::numbers::sqrt3_v<float>;
971 norm[2] = vec[2] * al::numbers::sqrt3_v<float>;
974 return alu::Matrix{
975 1.0f, 0.0f, 0.0f, 0.0f,
976 norm[0], 1.0f-mag, 0.0f, 0.0f,
977 norm[1], 0.0f, 1.0f-mag, 0.0f,
978 norm[2], 0.0f, 0.0f, 1.0f-mag
982 /* Update the early and late 3D panning gains. */
983 void ReverbState::update3DPanning(const float *ReflectionsPan, const float *LateReverbPan,
984 const float earlyGain, const float lateGain, const EffectTarget &target)
986 /* Create matrices that transform a B-Format signal according to the
987 * panning vectors.
989 const alu::Matrix earlymat{GetTransformFromVector(ReflectionsPan)};
990 const alu::Matrix latemat{GetTransformFromVector(LateReverbPan)};
992 mOutTarget = target.Main->Buffer;
993 for(size_t i{0u};i < NUM_LINES;i++)
995 const float coeffs[MaxAmbiChannels]{earlymat[0][i], earlymat[1][i], earlymat[2][i],
996 earlymat[3][i]};
997 ComputePanGains(target.Main, coeffs, earlyGain, mEarly.PanGain[i]);
999 for(size_t i{0u};i < NUM_LINES;i++)
1001 const float coeffs[MaxAmbiChannels]{latemat[0][i], latemat[1][i], latemat[2][i],
1002 latemat[3][i]};
1003 ComputePanGains(target.Main, coeffs, lateGain, mLate.PanGain[i]);
1007 void ReverbState::update(const ContextBase *Context, const EffectSlot *Slot,
1008 const EffectProps *props, const EffectTarget target)
1010 const DeviceBase *Device{Context->mDevice};
1011 const auto frequency = static_cast<float>(Device->Frequency);
1013 /* Calculate the master filters */
1014 float hf0norm{minf(props->Reverb.HFReference/frequency, 0.49f)};
1015 mFilter[0].Lp.setParamsFromSlope(BiquadType::HighShelf, hf0norm, props->Reverb.GainHF, 1.0f);
1016 float lf0norm{minf(props->Reverb.LFReference/frequency, 0.49f)};
1017 mFilter[0].Hp.setParamsFromSlope(BiquadType::LowShelf, lf0norm, props->Reverb.GainLF, 1.0f);
1018 for(size_t i{1u};i < NUM_LINES;i++)
1020 mFilter[i].Lp.copyParamsFrom(mFilter[0].Lp);
1021 mFilter[i].Hp.copyParamsFrom(mFilter[0].Hp);
1024 /* The density-based room size (delay length) multiplier. */
1025 const float density_mult{CalcDelayLengthMult(props->Reverb.Density)};
1027 /* Update the main effect delay and associated taps. */
1028 updateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
1029 density_mult, props->Reverb.DecayTime, frequency);
1031 /* Update the early lines. */
1032 mEarly.updateLines(density_mult, props->Reverb.Diffusion, props->Reverb.DecayTime, frequency);
1034 /* Get the mixing matrix coefficients. */
1035 CalcMatrixCoeffs(props->Reverb.Diffusion, &mMixX, &mMixY);
1037 /* If the HF limit parameter is flagged, calculate an appropriate limit
1038 * based on the air absorption parameter.
1040 float hfRatio{props->Reverb.DecayHFRatio};
1041 if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
1042 hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
1043 props->Reverb.DecayTime);
1045 /* Calculate the LF/HF decay times. */
1046 constexpr float MinDecayTime{0.1f}, MaxDecayTime{20.0f};
1047 const float lfDecayTime{clampf(props->Reverb.DecayTime*props->Reverb.DecayLFRatio,
1048 MinDecayTime, MaxDecayTime)};
1049 const float hfDecayTime{clampf(props->Reverb.DecayTime*hfRatio, MinDecayTime, MaxDecayTime)};
1051 /* Update the modulator rate and depth. */
1052 mLate.Mod.updateModulator(props->Reverb.ModulationTime, props->Reverb.ModulationDepth,
1053 frequency);
1055 /* Update the late lines. */
1056 mLate.updateLines(density_mult, props->Reverb.Diffusion, lfDecayTime,
1057 props->Reverb.DecayTime, hfDecayTime, lf0norm, hf0norm, frequency);
1059 /* Update early and late 3D panning. */
1060 const float gain{props->Reverb.Gain * Slot->Gain * ReverbBoost};
1061 update3DPanning(props->Reverb.ReflectionsPan, props->Reverb.LateReverbPan,
1062 props->Reverb.ReflectionsGain*gain, props->Reverb.LateReverbGain*gain, target);
1064 /* Calculate the max update size from the smallest relevant delay. */
1065 mMaxUpdate[1] = minz(MAX_UPDATE_SAMPLES, minz(mEarly.Offset[0][1], mLate.Offset[0][1]));
1067 /* Determine if delay-line cross-fading is required. Density is essentially
1068 * a master control for the feedback delays, so changes the offsets of many
1069 * delay lines.
1071 mDoFading |= (mParams.Density != props->Reverb.Density ||
1072 /* Diffusion and decay times influences the decay rate (gain) of the
1073 * late reverb T60 filter.
1075 mParams.Diffusion != props->Reverb.Diffusion ||
1076 mParams.DecayTime != props->Reverb.DecayTime ||
1077 mParams.HFDecayTime != hfDecayTime ||
1078 mParams.LFDecayTime != lfDecayTime ||
1079 /* Modulation time and depth both require fading the modulation delay. */
1080 mParams.ModulationTime != props->Reverb.ModulationTime ||
1081 mParams.ModulationDepth != props->Reverb.ModulationDepth ||
1082 /* HF/LF References control the weighting used to calculate the density
1083 * gain.
1085 mParams.HFReference != props->Reverb.HFReference ||
1086 mParams.LFReference != props->Reverb.LFReference);
1087 if(mDoFading)
1089 mParams.Density = props->Reverb.Density;
1090 mParams.Diffusion = props->Reverb.Diffusion;
1091 mParams.DecayTime = props->Reverb.DecayTime;
1092 mParams.HFDecayTime = hfDecayTime;
1093 mParams.LFDecayTime = lfDecayTime;
1094 mParams.ModulationTime = props->Reverb.ModulationTime;
1095 mParams.ModulationDepth = props->Reverb.ModulationDepth;
1096 mParams.HFReference = props->Reverb.HFReference;
1097 mParams.LFReference = props->Reverb.LFReference;
1102 /**************************************
1103 * Effect Processing *
1104 **************************************/
1106 /* Applies a scattering matrix to the 4-line (vector) input. This is used
1107 * for both the below vector all-pass model and to perform modal feed-back
1108 * delay network (FDN) mixing.
1110 * The matrix is derived from a skew-symmetric matrix to form a 4D rotation
1111 * matrix with a single unitary rotational parameter:
1113 * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
1114 * [ -a, d, c, -b ]
1115 * [ -b, -c, d, a ]
1116 * [ -c, b, -a, d ]
1118 * The rotation is constructed from the effect's diffusion parameter,
1119 * yielding:
1121 * 1 = x^2 + 3 y^2
1123 * Where a, b, and c are the coefficient y with differing signs, and d is the
1124 * coefficient x. The final matrix is thus:
1126 * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
1127 * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
1128 * [ y, -y, x, y ] x = cos(t)
1129 * [ -y, -y, -y, x ] y = sin(t) / n
1131 * Any square orthogonal matrix with an order that is a power of two will
1132 * work (where ^T is transpose, ^-1 is inverse):
1134 * M^T = M^-1
1136 * Using that knowledge, finding an appropriate matrix can be accomplished
1137 * naively by searching all combinations of:
1139 * M = D + S - S^T
1141 * Where D is a diagonal matrix (of x), and S is a triangular matrix (of y)
1142 * whose combination of signs are being iterated.
1144 inline auto VectorPartialScatter(const std::array<float,NUM_LINES> &RESTRICT in,
1145 const float xCoeff, const float yCoeff) -> std::array<float,NUM_LINES>
1147 return std::array<float,NUM_LINES>{{
1148 xCoeff*in[0] + yCoeff*( in[1] + -in[2] + in[3]),
1149 xCoeff*in[1] + yCoeff*(-in[0] + in[2] + in[3]),
1150 xCoeff*in[2] + yCoeff*( in[0] + -in[1] + in[3]),
1151 xCoeff*in[3] + yCoeff*(-in[0] + -in[1] + -in[2] )
1155 /* Utilizes the above, but reverses the input channels. */
1156 void VectorScatterRevDelayIn(const DelayLineI delay, size_t offset, const float xCoeff,
1157 const float yCoeff, const al::span<const ReverbUpdateLine,NUM_LINES> in, const size_t count)
1159 ASSUME(count > 0);
1161 for(size_t i{0u};i < count;)
1163 offset &= delay.Mask;
1164 size_t td{minz(delay.Mask+1 - offset, count-i)};
1165 do {
1166 std::array<float,NUM_LINES> f;
1167 for(size_t j{0u};j < NUM_LINES;j++)
1168 f[NUM_LINES-1-j] = in[j][i];
1169 ++i;
1171 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1172 } while(--td);
1176 /* This applies a Gerzon multiple-in/multiple-out (MIMO) vector all-pass
1177 * filter to the 4-line input.
1179 * It works by vectorizing a regular all-pass filter and replacing the delay
1180 * element with a scattering matrix (like the one above) and a diagonal
1181 * matrix of delay elements.
1183 * Two static specializations are used for transitional (cross-faded) delay
1184 * line processing and non-transitional processing.
1186 void VecAllpass::processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
1187 const float xCoeff, const float yCoeff, const size_t todo)
1189 const DelayLineI delay{Delay};
1190 const float feedCoeff{Coeff};
1192 ASSUME(todo > 0);
1194 size_t vap_offset[NUM_LINES];
1195 for(size_t j{0u};j < NUM_LINES;j++)
1196 vap_offset[j] = offset - Offset[j][0];
1197 for(size_t i{0u};i < todo;)
1199 for(size_t j{0u};j < NUM_LINES;j++)
1200 vap_offset[j] &= delay.Mask;
1201 offset &= delay.Mask;
1203 size_t maxoff{offset};
1204 for(size_t j{0u};j < NUM_LINES;j++)
1205 maxoff = maxz(maxoff, vap_offset[j]);
1206 size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
1208 do {
1209 std::array<float,NUM_LINES> f;
1210 for(size_t j{0u};j < NUM_LINES;j++)
1212 const float input{samples[j][i]};
1213 const float out{delay.Line[vap_offset[j]++][j] - feedCoeff*input};
1214 f[j] = input + feedCoeff*out;
1216 samples[j][i] = out;
1218 ++i;
1220 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1221 } while(--td);
1224 void VecAllpass::processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
1225 const float xCoeff, const float yCoeff, float fadeCount, const float fadeStep,
1226 const size_t todo)
1228 const DelayLineI delay{Delay};
1229 const float feedCoeff{Coeff};
1231 ASSUME(todo > 0);
1233 size_t vap_offset[NUM_LINES][2];
1234 for(size_t j{0u};j < NUM_LINES;j++)
1236 vap_offset[j][0] = offset - Offset[j][0];
1237 vap_offset[j][1] = offset - Offset[j][1];
1239 for(size_t i{0u};i < todo;)
1241 for(size_t j{0u};j < NUM_LINES;j++)
1243 vap_offset[j][0] &= delay.Mask;
1244 vap_offset[j][1] &= delay.Mask;
1246 offset &= delay.Mask;
1248 size_t maxoff{offset};
1249 for(size_t j{0u};j < NUM_LINES;j++)
1250 maxoff = maxz(maxoff, maxz(vap_offset[j][0], vap_offset[j][1]));
1251 size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
1253 do {
1254 fadeCount += 1.0f;
1255 const float fade{fadeCount * fadeStep};
1257 std::array<float,NUM_LINES> f;
1258 for(size_t j{0u};j < NUM_LINES;j++)
1259 f[j] = delay.Line[vap_offset[j][0]++][j]*(1.0f-fade) +
1260 delay.Line[vap_offset[j][1]++][j]*fade;
1262 for(size_t j{0u};j < NUM_LINES;j++)
1264 const float input{samples[j][i]};
1265 const float out{f[j] - feedCoeff*input};
1266 f[j] = input + feedCoeff*out;
1268 samples[j][i] = out;
1270 ++i;
1272 delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
1273 } while(--td);
1277 /* This generates early reflections.
1279 * This is done by obtaining the primary reflections (those arriving from the
1280 * same direction as the source) from the main delay line. These are
1281 * attenuated and all-pass filtered (based on the diffusion parameter).
1283 * The early lines are then fed in reverse (according to the approximately
1284 * opposite spatial location of the A-Format lines) to create the secondary
1285 * reflections (those arriving from the opposite direction as the source).
1287 * The early response is then completed by combining the primary reflections
1288 * with the delayed and attenuated output from the early lines.
1290 * Finally, the early response is reversed, scattered (based on diffusion),
1291 * and fed into the late reverb section of the main delay line.
1293 * Two static specializations are used for transitional (cross-faded) delay
1294 * line processing and non-transitional processing.
1296 void ReverbState::earlyUnfaded(const size_t offset, const size_t todo)
1298 const DelayLineI early_delay{mEarly.Delay};
1299 const DelayLineI main_delay{mDelay};
1300 const float mixX{mMixX};
1301 const float mixY{mMixY};
1303 ASSUME(todo > 0);
1305 /* First, load decorrelated samples from the main delay line as the primary
1306 * reflections.
1308 for(size_t j{0u};j < NUM_LINES;j++)
1310 size_t early_delay_tap{offset - mEarlyDelayTap[j][0]};
1311 const float coeff{mEarlyDelayCoeff[j][0]};
1312 for(size_t i{0u};i < todo;)
1314 early_delay_tap &= main_delay.Mask;
1315 size_t td{minz(main_delay.Mask+1 - early_delay_tap, todo - i)};
1316 do {
1317 mTempSamples[j][i++] = main_delay.Line[early_delay_tap++][j] * coeff;
1318 } while(--td);
1322 /* Apply a vector all-pass, to help color the initial reflections based on
1323 * the diffusion strength.
1325 mEarly.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
1327 /* Apply a delay and bounce to generate secondary reflections, combine with
1328 * the primary reflections and write out the result for mixing.
1330 for(size_t j{0u};j < NUM_LINES;j++)
1332 size_t feedb_tap{offset - mEarly.Offset[j][0]};
1333 const float feedb_coeff{mEarly.Coeff[j][0]};
1334 float *out{mEarlySamples[j].data()};
1336 for(size_t i{0u};i < todo;)
1338 feedb_tap &= early_delay.Mask;
1339 size_t td{minz(early_delay.Mask+1 - feedb_tap, todo - i)};
1340 do {
1341 out[i] = mTempSamples[j][i] + early_delay.Line[feedb_tap++][j]*feedb_coeff;
1342 ++i;
1343 } while(--td);
1346 for(size_t j{0u};j < NUM_LINES;j++)
1347 early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
1349 /* Also write the result back to the main delay line for the late reverb
1350 * stage to pick up at the appropriate time, appplying a scatter and
1351 * bounce to improve the initial diffusion in the late reverb.
1353 const size_t late_feed_tap{offset - mLateFeedTap};
1354 VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
1356 void ReverbState::earlyFaded(const size_t offset, const size_t todo, const float fade,
1357 const float fadeStep)
1359 const DelayLineI early_delay{mEarly.Delay};
1360 const DelayLineI main_delay{mDelay};
1361 const float mixX{mMixX};
1362 const float mixY{mMixY};
1364 ASSUME(todo > 0);
1366 for(size_t j{0u};j < NUM_LINES;j++)
1368 size_t early_delay_tap0{offset - mEarlyDelayTap[j][0]};
1369 size_t early_delay_tap1{offset - mEarlyDelayTap[j][1]};
1370 const float oldCoeff{mEarlyDelayCoeff[j][0]};
1371 const float oldCoeffStep{-oldCoeff * fadeStep};
1372 const float newCoeffStep{mEarlyDelayCoeff[j][1] * fadeStep};
1373 float fadeCount{fade};
1375 for(size_t i{0u};i < todo;)
1377 early_delay_tap0 &= main_delay.Mask;
1378 early_delay_tap1 &= main_delay.Mask;
1379 size_t td{minz(main_delay.Mask+1 - maxz(early_delay_tap0, early_delay_tap1), todo-i)};
1380 do {
1381 fadeCount += 1.0f;
1382 const float fade0{oldCoeff + oldCoeffStep*fadeCount};
1383 const float fade1{newCoeffStep*fadeCount};
1384 mTempSamples[j][i++] =
1385 main_delay.Line[early_delay_tap0++][j]*fade0 +
1386 main_delay.Line[early_delay_tap1++][j]*fade1;
1387 } while(--td);
1391 mEarly.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
1393 for(size_t j{0u};j < NUM_LINES;j++)
1395 size_t feedb_tap0{offset - mEarly.Offset[j][0]};
1396 size_t feedb_tap1{offset - mEarly.Offset[j][1]};
1397 const float feedb_oldCoeff{mEarly.Coeff[j][0]};
1398 const float feedb_oldCoeffStep{-feedb_oldCoeff * fadeStep};
1399 const float feedb_newCoeffStep{mEarly.Coeff[j][1] * fadeStep};
1400 float *out{mEarlySamples[j].data()};
1401 float fadeCount{fade};
1403 for(size_t i{0u};i < todo;)
1405 feedb_tap0 &= early_delay.Mask;
1406 feedb_tap1 &= early_delay.Mask;
1407 size_t td{minz(early_delay.Mask+1 - maxz(feedb_tap0, feedb_tap1), todo - i)};
1409 do {
1410 fadeCount += 1.0f;
1411 const float fade0{feedb_oldCoeff + feedb_oldCoeffStep*fadeCount};
1412 const float fade1{feedb_newCoeffStep*fadeCount};
1413 out[i] = mTempSamples[j][i] +
1414 early_delay.Line[feedb_tap0++][j]*fade0 +
1415 early_delay.Line[feedb_tap1++][j]*fade1;
1416 ++i;
1417 } while(--td);
1420 for(size_t j{0u};j < NUM_LINES;j++)
1421 early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
1423 const size_t late_feed_tap{offset - mLateFeedTap};
1424 VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
1428 void Modulation::calcDelays(size_t todo)
1430 constexpr float inv_scale{MOD_FRACONE / al::numbers::pi_v<float> / 2.0f};
1431 uint idx{Index};
1432 const uint step{Step};
1433 const float depth{Depth[0]};
1434 for(size_t i{0};i < todo;++i)
1436 idx += step;
1437 const float lfo{std::sin(static_cast<float>(idx&MOD_FRACMASK) / inv_scale)};
1438 ModDelays[i] = (lfo+1.0f) * depth;
1440 Index = idx;
1443 void Modulation::calcFadedDelays(size_t todo, float fadeCount, float fadeStep)
1445 constexpr float inv_scale{MOD_FRACONE / al::numbers::pi_v<float> / 2.0f};
1446 uint idx{Index};
1447 const uint step{Step};
1448 const float depth{Depth[0]};
1449 const float depthStep{(Depth[1]-depth) * fadeStep};
1450 for(size_t i{0};i < todo;++i)
1452 fadeCount += 1.0f;
1453 idx += step;
1454 const float lfo{std::sin(static_cast<float>(idx&MOD_FRACMASK) / inv_scale)};
1455 ModDelays[i] = (lfo+1.0f) * (depth + depthStep*fadeCount);
1457 Index = idx;
1461 /* This generates the reverb tail using a modified feed-back delay network
1462 * (FDN).
1464 * Results from the early reflections are mixed with the output from the
1465 * modulated late delay lines.
1467 * The late response is then completed by T60 and all-pass filtering the mix.
1469 * Finally, the lines are reversed (so they feed their opposite directions)
1470 * and scattered with the FDN matrix before re-feeding the delay lines.
1472 * Two variations are made, one for for transitional (cross-faded) delay line
1473 * processing and one for non-transitional processing.
1475 void ReverbState::lateUnfaded(const size_t offset, const size_t todo)
1477 const DelayLineI late_delay{mLate.Delay};
1478 const DelayLineI main_delay{mDelay};
1479 const float mixX{mMixX};
1480 const float mixY{mMixY};
1482 ASSUME(todo > 0);
1484 /* First, calculate the modulated delays for the late feedback. */
1485 mLate.Mod.calcDelays(todo);
1487 /* Next, load decorrelated samples from the main and feedback delay lines.
1488 * Filter the signal to apply its frequency-dependent decay.
1490 for(size_t j{0u};j < NUM_LINES;j++)
1492 size_t late_delay_tap{offset - mLateDelayTap[j][0]};
1493 size_t late_feedb_tap{offset - mLate.Offset[j][0]};
1494 const float midGain{mLate.T60[j].MidGain[0]};
1495 const float densityGain{mLate.DensityGain[0] * midGain};
1497 for(size_t i{0u};i < todo;)
1499 late_delay_tap &= main_delay.Mask;
1500 size_t td{minz(todo - i, main_delay.Mask+1 - late_delay_tap)};
1501 do {
1502 /* Calculate the read offset and fraction between it and the
1503 * next sample.
1505 const float fdelay{mLate.Mod.ModDelays[i]};
1506 const size_t delay{float2uint(fdelay)};
1507 const float frac{fdelay - static_cast<float>(delay)};
1509 /* Feed the delay line with the late feedback sample, and get
1510 * the two samples crossed by the delayed offset.
1512 const float out0{late_delay.Line[(late_feedb_tap-delay) & late_delay.Mask][j]};
1513 const float out1{late_delay.Line[(late_feedb_tap-delay-1) & late_delay.Mask][j]};
1514 ++late_feedb_tap;
1516 /* The output is obtained by linearly interpolating the two
1517 * samples that were acquired above, and combined with the main
1518 * delay tap.
1520 mTempSamples[j][i] = lerp(out0, out1, frac)*midGain +
1521 main_delay.Line[late_delay_tap++][j]*densityGain;
1522 ++i;
1523 } while(--td);
1525 mLate.T60[j].process({mTempSamples[j].data(), todo});
1528 /* Apply a vector all-pass to improve micro-surface diffusion, and write
1529 * out the results for mixing.
1531 mLate.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
1532 for(size_t j{0u};j < NUM_LINES;j++)
1533 std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
1535 /* Finally, scatter and bounce the results to refeed the feedback buffer. */
1536 VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
1538 void ReverbState::lateFaded(const size_t offset, const size_t todo, const float fade,
1539 const float fadeStep)
1541 const DelayLineI late_delay{mLate.Delay};
1542 const DelayLineI main_delay{mDelay};
1543 const float mixX{mMixX};
1544 const float mixY{mMixY};
1546 ASSUME(todo > 0);
1548 mLate.Mod.calcFadedDelays(todo, fade, fadeStep);
1550 for(size_t j{0u};j < NUM_LINES;j++)
1552 const float oldMidGain{mLate.T60[j].MidGain[0]};
1553 const float midGain{mLate.T60[j].MidGain[1]};
1554 const float oldMidStep{-oldMidGain * fadeStep};
1555 const float midStep{midGain * fadeStep};
1556 const float oldDensityGain{mLate.DensityGain[0] * oldMidGain};
1557 const float densityGain{mLate.DensityGain[1] * midGain};
1558 const float oldDensityStep{-oldDensityGain * fadeStep};
1559 const float densityStep{densityGain * fadeStep};
1560 size_t late_delay_tap0{offset - mLateDelayTap[j][0]};
1561 size_t late_delay_tap1{offset - mLateDelayTap[j][1]};
1562 size_t late_feedb_tap0{offset - mLate.Offset[j][0]};
1563 size_t late_feedb_tap1{offset - mLate.Offset[j][1]};
1564 float fadeCount{fade};
1566 for(size_t i{0u};i < todo;)
1568 late_delay_tap0 &= main_delay.Mask;
1569 late_delay_tap1 &= main_delay.Mask;
1570 size_t td{minz(todo - i, main_delay.Mask+1 - maxz(late_delay_tap0, late_delay_tap1))};
1571 do {
1572 fadeCount += 1.0f;
1574 const float fdelay{mLate.Mod.ModDelays[i]};
1575 const size_t delay{float2uint(fdelay)};
1576 const float frac{fdelay - static_cast<float>(delay)};
1578 const float out00{late_delay.Line[(late_feedb_tap0-delay) & late_delay.Mask][j]};
1579 const float out01{late_delay.Line[(late_feedb_tap0-delay-1) & late_delay.Mask][j]};
1580 ++late_feedb_tap0;
1581 const float out10{late_delay.Line[(late_feedb_tap1-delay) & late_delay.Mask][j]};
1582 const float out11{late_delay.Line[(late_feedb_tap1-delay-1) & late_delay.Mask][j]};
1583 ++late_feedb_tap1;
1585 const float fade0{oldDensityGain + oldDensityStep*fadeCount};
1586 const float fade1{densityStep*fadeCount};
1587 const float gfade0{oldMidGain + oldMidStep*fadeCount};
1588 const float gfade1{midStep*fadeCount};
1589 mTempSamples[j][i] = lerp(out00, out01, frac)*gfade0 +
1590 lerp(out10, out11, frac)*gfade1 +
1591 main_delay.Line[late_delay_tap0++][j]*fade0 +
1592 main_delay.Line[late_delay_tap1++][j]*fade1;
1593 ++i;
1594 } while(--td);
1596 mLate.T60[j].process({mTempSamples[j].data(), todo});
1599 mLate.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
1600 for(size_t j{0u};j < NUM_LINES;j++)
1601 std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
1603 VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
1606 void ReverbState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
1608 size_t offset{mOffset};
1610 ASSUME(samplesToDo > 0);
1612 /* Convert B-Format to A-Format for processing. */
1613 const size_t numInput{minz(samplesIn.size(), NUM_LINES)};
1614 const al::span<float> tmpspan{al::assume_aligned<16>(mTempLine.data()), samplesToDo};
1615 for(size_t c{0u};c < NUM_LINES;c++)
1617 std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
1618 for(size_t i{0};i < numInput;++i)
1620 const float gain{B2A[c][i]};
1621 const float *RESTRICT input{al::assume_aligned<16>(samplesIn[i].data())};
1623 for(float &sample : tmpspan)
1625 sample += *input * gain;
1626 ++input;
1630 /* Band-pass the incoming samples and feed the initial delay line. */
1631 DualBiquad{mFilter[c].Lp, mFilter[c].Hp}.process(tmpspan, tmpspan.data());
1632 mDelay.write(offset, c, tmpspan.cbegin(), samplesToDo);
1635 /* Process reverb for these samples. */
1636 if LIKELY(!mDoFading)
1638 for(size_t base{0};base < samplesToDo;)
1640 /* Calculate the number of samples we can do this iteration. */
1641 size_t todo{minz(samplesToDo - base, mMaxUpdate[0])};
1642 /* Some mixers require maintaining a 4-sample alignment, so ensure
1643 * that if it's not the last iteration.
1645 if(base+todo < samplesToDo) todo &= ~size_t{3};
1646 ASSUME(todo > 0);
1648 /* Generate non-faded early reflections and late reverb. */
1649 earlyUnfaded(offset, todo);
1650 lateUnfaded(offset, todo);
1652 /* Finally, mix early reflections and late reverb. */
1653 (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
1655 offset += todo;
1656 base += todo;
1659 else
1661 const float fadeStep{1.0f / static_cast<float>(samplesToDo)};
1662 for(size_t base{0};base < samplesToDo;)
1664 size_t todo{minz(samplesToDo - base, minz(mMaxUpdate[0], mMaxUpdate[1]))};
1665 if(base+todo < samplesToDo) todo &= ~size_t{3};
1666 ASSUME(todo > 0);
1668 /* Generate cross-faded early reflections and late reverb. */
1669 auto fadeCount = static_cast<float>(base);
1670 earlyFaded(offset, todo, fadeCount, fadeStep);
1671 lateFaded(offset, todo, fadeCount, fadeStep);
1673 (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
1675 offset += todo;
1676 base += todo;
1679 /* Update the cross-fading delay line taps. */
1680 for(size_t c{0u};c < NUM_LINES;c++)
1682 mEarlyDelayTap[c][0] = mEarlyDelayTap[c][1];
1683 mEarlyDelayCoeff[c][0] = mEarlyDelayCoeff[c][1];
1684 mLateDelayTap[c][0] = mLateDelayTap[c][1];
1685 mEarly.VecAp.Offset[c][0] = mEarly.VecAp.Offset[c][1];
1686 mEarly.Offset[c][0] = mEarly.Offset[c][1];
1687 mEarly.Coeff[c][0] = mEarly.Coeff[c][1];
1688 mLate.Offset[c][0] = mLate.Offset[c][1];
1689 mLate.T60[c].MidGain[0] = mLate.T60[c].MidGain[1];
1690 mLate.VecAp.Offset[c][0] = mLate.VecAp.Offset[c][1];
1692 mLate.DensityGain[0] = mLate.DensityGain[1];
1693 mLate.Mod.Depth[0] = mLate.Mod.Depth[1];
1694 mMaxUpdate[0] = mMaxUpdate[1];
1695 mDoFading = false;
1697 mOffset = offset;
1701 struct ReverbStateFactory final : public EffectStateFactory {
1702 al::intrusive_ptr<EffectState> create() override
1703 { return al::intrusive_ptr<EffectState>{new ReverbState{}}; }
1706 struct StdReverbStateFactory final : public EffectStateFactory {
1707 al::intrusive_ptr<EffectState> create() override
1708 { return al::intrusive_ptr<EffectState>{new ReverbState{}}; }
1711 } // namespace
1713 EffectStateFactory *ReverbStateFactory_getFactory()
1715 static ReverbStateFactory ReverbFactory{};
1716 return &ReverbFactory;
1719 EffectStateFactory *StdReverbStateFactory_getFactory()
1721 static StdReverbStateFactory ReverbFactory{};
1722 return &ReverbFactory;