reverb.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <algorithm>
  22. #include <array>
  23. #include <cassert>
  24. #include <cmath>
  25. #include <cstdint>
  26. #include <cstdio>
  27. #include <functional>
  28. #include <numeric>
  29. #include <utility>
  30. #include <variant>
  31. #include "alc/effects/base.h"
  32. #include "alnumbers.h"
  33. #include "alnumeric.h"
  34. #include "alspan.h"
  35. #include "core/ambidefs.h"
  36. #include "core/bufferline.h"
  37. #include "core/context.h"
  38. #include "core/cubic_tables.h"
  39. #include "core/device.h"
  40. #include "core/effects/base.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 "vector.h"
  49. struct BufferStorage;
  50. namespace {
  51. using uint = unsigned int;
  52. constexpr float MaxModulationTime{4.0f};
  53. constexpr float DefaultModulationTime{0.25f};
  54. #define MOD_FRACBITS 24
  55. #define MOD_FRACONE (1<<MOD_FRACBITS)
  56. #define MOD_FRACMASK (MOD_FRACONE-1)
  57. /* Max samples per process iteration. Used to limit the size needed for
  58. * temporary buffers. Must be a multiple of 4 for SIMD alignment.
  59. */
  60. constexpr size_t MAX_UPDATE_SAMPLES{256};
  61. /* The number of spatialized lines or channels to process. Four channels allows
  62. * for a 3D A-Format response. NOTE: This can't be changed without taking care
  63. * of the conversion matrices, and a few places where the length arrays are
  64. * assumed to have 4 elements.
  65. */
  66. constexpr size_t NUM_LINES{4u};
  67. /* This coefficient is used to define the maximum frequency range controlled by
  68. * the modulation depth. The current value of 0.05 will allow it to swing from
  69. * 0.95x to 1.05x. This value must be below 1. At 1 it will cause the sampler
  70. * to stall on the downswing, and above 1 it will cause it to sample backwards.
  71. * The value 0.05 seems be nearest to Creative hardware behavior.
  72. */
  73. constexpr float MODULATION_DEPTH_COEFF{0.05f};
  74. /* The B-Format to (W-normalized) A-Format conversion matrix. This produces a
  75. * tetrahedral array of discrete signals (boosted by a factor of sqrt(3), to
  76. * reduce the error introduced in the conversion).
  77. */
  78. alignas(16) constexpr std::array<std::array<float,NUM_LINES>,NUM_LINES> B2A{{
  79. /* W Y Z X */
  80. {{ 0.5f, 0.5f, 0.5f, 0.5f }}, /* A0 */
  81. {{ 0.5f, -0.5f, -0.5f, 0.5f }}, /* A1 */
  82. {{ 0.5f, 0.5f, -0.5f, -0.5f }}, /* A2 */
  83. {{ 0.5f, -0.5f, 0.5f, -0.5f }} /* A3 */
  84. }};
  85. /* Converts (W-normalized) A-Format to B-Format for early reflections (scaled
  86. * by 1/sqrt(3) to compensate for the boost in the B2A matrix).
  87. */
  88. alignas(16) constexpr std::array<std::array<float,NUM_LINES>,NUM_LINES> EarlyA2B{{
  89. /* A0 A1 A2 A3 */
  90. {{ 0.5f, 0.5f, 0.5f, 0.5f }}, /* W */
  91. {{ 0.5f, -0.5f, 0.5f, -0.5f }}, /* Y */
  92. {{ 0.5f, -0.5f, -0.5f, 0.5f }}, /* Z */
  93. {{ 0.5f, 0.5f, -0.5f, -0.5f }} /* X */
  94. }};
  95. /* Converts (W-normalized) A-Format to B-Format for late reverb (scaled
  96. * by 1/sqrt(3) to compensate for the boost in the B2A matrix). The response
  97. * is rotated around Z (ambisonic X) so that the front lines are placed
  98. * horizontally in front, and the rear lines are placed vertically in back.
  99. */
  100. constexpr auto InvSqrt2 = static_cast<float>(1.0/al::numbers::sqrt2);
  101. alignas(16) constexpr std::array<std::array<float,NUM_LINES>,NUM_LINES> LateA2B{{
  102. /* A0 A1 A2 A3 */
  103. {{ 0.5f, 0.5f, 0.5f, 0.5f }}, /* W */
  104. {{ InvSqrt2, -InvSqrt2, 0.0f, 0.0f }}, /* Y */
  105. {{ 0.0f, 0.0f, -InvSqrt2, InvSqrt2 }}, /* Z */
  106. {{ 0.5f, 0.5f, -0.5f, -0.5f }} /* X */
  107. }};
  108. /* The all-pass and delay lines have a variable length dependent on the
  109. * effect's density parameter, which helps alter the perceived environment
  110. * size. The size-to-density conversion is a cubed scale:
  111. *
  112. * density = min(1.0, pow(size, 3.0) / DENSITY_SCALE);
  113. *
  114. * The line lengths scale linearly with room size, so the inverse density
  115. * conversion is needed, taking the cube root of the re-scaled density to
  116. * calculate the line length multiplier:
  117. *
  118. * length_mult = max(5.0, cbrt(density*DENSITY_SCALE));
  119. *
  120. * The density scale below will result in a max line multiplier of 50, for an
  121. * effective size range of 5m to 50m.
  122. */
  123. constexpr float DENSITY_SCALE{125000.0f};
  124. /* All delay line lengths are specified in seconds.
  125. *
  126. * To approximate early reflections, we break them up into primary (those
  127. * arriving from the same direction as the source) and secondary (those
  128. * arriving from the opposite direction).
  129. *
  130. * The early taps decorrelate the 4-channel signal to approximate an average
  131. * room response for the primary reflections after the initial early delay.
  132. *
  133. * Given an average room dimension (d_a) and the speed of sound (c) we can
  134. * calculate the average reflection delay (r_a) regardless of listener and
  135. * source positions as:
  136. *
  137. * r_a = d_a / c
  138. * c = 343.3
  139. *
  140. * This can extended to finding the average difference (r_d) between the
  141. * maximum (r_1) and minimum (r_0) reflection delays:
  142. *
  143. * r_0 = 2 / 3 r_a
  144. * = r_a - r_d / 2
  145. * = r_d
  146. * r_1 = 4 / 3 r_a
  147. * = r_a + r_d / 2
  148. * = 2 r_d
  149. * r_d = 2 / 3 r_a
  150. * = r_1 - r_0
  151. *
  152. * As can be determined by integrating the 1D model with a source (s) and
  153. * listener (l) positioned across the dimension of length (d_a):
  154. *
  155. * r_d = int_(l=0)^d_a (int_(s=0)^d_a |2 d_a - 2 (l + s)| ds) dl / c
  156. *
  157. * The initial taps (T_(i=0)^N) are then specified by taking a power series
  158. * that ranges between r_0 and half of r_1 less r_0:
  159. *
  160. * R_i = 2^(i / (2 N - 1)) r_d
  161. * = r_0 + (2^(i / (2 N - 1)) - 1) r_d
  162. * = r_0 + T_i
  163. * T_i = R_i - r_0
  164. * = (2^(i / (2 N - 1)) - 1) r_d
  165. *
  166. * Assuming an average of 1m, we get the following taps:
  167. */
  168. constexpr std::array<float,NUM_LINES> EARLY_TAP_LENGTHS{{
  169. 0.0000000e+0f, 2.0213520e-4f, 4.2531060e-4f, 6.7171600e-4f
  170. }};
  171. /* The early all-pass filter lengths are based on the early tap lengths:
  172. *
  173. * A_i = R_i / a
  174. *
  175. * Where a is the approximate maximum all-pass cycle limit (20).
  176. */
  177. constexpr std::array<float,NUM_LINES> EARLY_ALLPASS_LENGTHS{{
  178. 9.7096800e-5f, 1.0720356e-4f, 1.1836234e-4f, 1.3068260e-4f
  179. }};
  180. /* The early delay lines are used to transform the primary reflections into
  181. * the secondary reflections. The A-format is arranged in such a way that
  182. * the channels/lines are spatially opposite:
  183. *
  184. * C_i is opposite C_(N-i-1)
  185. *
  186. * The delays of the two opposing reflections (R_i and O_i) from a source
  187. * anywhere along a particular dimension always sum to twice its full delay:
  188. *
  189. * 2 r_a = R_i + O_i
  190. *
  191. * With that in mind we can determine the delay between the two reflections
  192. * and thus specify our early line lengths (L_(i=0)^N) using:
  193. *
  194. * O_i = 2 r_a - R_(N-i-1)
  195. * L_i = O_i - R_(N-i-1)
  196. * = 2 (r_a - R_(N-i-1))
  197. * = 2 (r_a - T_(N-i-1) - r_0)
  198. * = 2 r_a (1 - (2 / 3) 2^((N - i - 1) / (2 N - 1)))
  199. *
  200. * Using an average dimension of 1m, we get:
  201. */
  202. constexpr std::array<float,NUM_LINES> EARLY_LINE_LENGTHS{{
  203. 0.0000000e+0f, 4.9281100e-4f, 9.3916180e-4f, 1.3434322e-3f
  204. }};
  205. /* The late all-pass filter lengths are based on the late line lengths:
  206. *
  207. * A_i = (5 / 3) L_i / r_1
  208. */
  209. constexpr std::array<float,NUM_LINES> LATE_ALLPASS_LENGTHS{{
  210. 1.6182800e-4f, 2.0389060e-4f, 2.8159360e-4f, 3.2365600e-4f
  211. }};
  212. /* The late lines are used to approximate the decaying cycle of recursive
  213. * late reflections.
  214. *
  215. * Splitting the lines in half, we start with the shortest reflection paths
  216. * (L_(i=0)^(N/2)):
  217. *
  218. * L_i = 2^(i / (N - 1)) r_d
  219. *
  220. * Then for the opposite (longest) reflection paths (L_(i=N/2)^N):
  221. *
  222. * L_i = 2 r_a - L_(i-N/2)
  223. * = 2 r_a - 2^((i - N / 2) / (N - 1)) r_d
  224. *
  225. * For our 1m average room, we get:
  226. */
  227. constexpr std::array<float,NUM_LINES> LATE_LINE_LENGTHS{{
  228. 1.9419362e-3f, 2.4466860e-3f, 3.3791220e-3f, 3.8838720e-3f
  229. }};
  230. using ReverbUpdateLine = std::array<float,MAX_UPDATE_SAMPLES>;
  231. struct DelayLineI {
  232. /* The delay lines use interleaved samples, with the lengths being powers
  233. * of 2 to allow the use of bit-masking instead of a modulus for wrapping.
  234. */
  235. al::span<float> mLine;
  236. /* Given the allocated sample buffer, this function updates each delay line
  237. * offset.
  238. */
  239. void realizeLineOffset(al::span<float> sampleBuffer) noexcept
  240. { mLine = sampleBuffer; }
  241. /* Calculate the length of a delay line and store its mask and offset. */
  242. static
  243. auto calcLineLength(const float length, const float frequency, const uint extra) -> size_t
  244. {
  245. /* All line lengths are powers of 2, calculated from their lengths in
  246. * seconds, rounded up.
  247. */
  248. uint samples{float2uint(std::ceil(length*frequency))};
  249. samples = NextPowerOf2(samples + extra);
  250. /* Return the sample count for accumulation. */
  251. return samples*NUM_LINES;
  252. }
  253. };
  254. struct DelayLineU {
  255. al::span<float> mLine;
  256. void realizeLineOffset(al::span<float> sampleBuffer) noexcept
  257. {
  258. assert(sampleBuffer.size() > 4 && !(sampleBuffer.size() & (sampleBuffer.size()-1)));
  259. mLine = sampleBuffer;
  260. }
  261. static
  262. auto calcLineLength(const float length, const float frequency, const uint extra) -> size_t
  263. {
  264. uint samples{float2uint(std::ceil(length*frequency))};
  265. samples = NextPowerOf2(samples + extra);
  266. return samples*NUM_LINES;
  267. }
  268. [[nodiscard]]
  269. auto get(size_t chan) const noexcept
  270. {
  271. const size_t stride{mLine.size() / NUM_LINES};
  272. return mLine.subspan(chan*stride, stride);
  273. }
  274. void write(size_t offset, const size_t c, al::span<const float> in) const noexcept
  275. {
  276. const size_t stride{mLine.size() / NUM_LINES};
  277. const auto output = mLine.subspan(c*stride);
  278. while(!in.empty())
  279. {
  280. offset &= stride-1;
  281. const size_t td{std::min(stride - offset, in.size())};
  282. std::copy_n(in.begin(), td, output.begin() + ptrdiff_t(offset));
  283. offset += td;
  284. in = in.subspan(td);
  285. }
  286. }
  287. /* Writes the given input lines to the delay buffer, applying a geometric
  288. * reflection. This effectively applies the matrix
  289. *
  290. * [ +1/2 -1/2 -1/2 -1/2 ]
  291. * [ -1/2 +1/2 -1/2 -1/2 ]
  292. * [ -1/2 -1/2 +1/2 -1/2 ]
  293. * [ -1/2 -1/2 -1/2 +1/2 ]
  294. *
  295. * to the four input lines when writing to the delay buffer. The effect on
  296. * the B-Format signal is negating W, applying a 180-degree phase shift and
  297. * moving each response to its spatially opposite location.
  298. */
  299. void writeReflected(size_t offset, const al::span<const ReverbUpdateLine,NUM_LINES> in,
  300. const size_t count) const noexcept
  301. {
  302. const size_t stride{mLine.size() / NUM_LINES};
  303. for(size_t i{0u};i < count;)
  304. {
  305. offset &= stride-1;
  306. size_t td{std::min(stride - offset, count - i)};
  307. do {
  308. const std::array src{in[0][i], in[1][i], in[2][i], in[3][i]};
  309. ++i;
  310. const std::array f{
  311. (src[0] - src[1] - src[2] - src[3]) * 0.5f,
  312. (src[1] - src[0] - src[2] - src[3]) * 0.5f,
  313. (src[2] - src[0] - src[1] - src[3]) * 0.5f,
  314. (src[3] - src[0] - src[1] - src[2] ) * 0.5f
  315. };
  316. mLine[0*stride + offset] = f[0];
  317. mLine[1*stride + offset] = f[1];
  318. mLine[2*stride + offset] = f[2];
  319. mLine[3*stride + offset] = f[3];
  320. ++offset;
  321. } while(--td);
  322. }
  323. }
  324. };
  325. struct VecAllpass {
  326. DelayLineI Delay;
  327. float Coeff{0.0f};
  328. std::array<size_t,NUM_LINES> Offset{};
  329. void process(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
  330. const float xCoeff, const float yCoeff, const size_t todo) const noexcept;
  331. };
  332. struct Allpass4 {
  333. DelayLineU Delay;
  334. float Coeff{0.0f};
  335. std::array<size_t,NUM_LINES> Offset{};
  336. void process(const al::span<ReverbUpdateLine,NUM_LINES> samples, const size_t offset,
  337. const size_t todo) const noexcept;
  338. };
  339. struct T60Filter {
  340. /* Two filters are used to adjust the signal. One to control the low
  341. * frequencies, and one to control the high frequencies.
  342. */
  343. float MidGain{0.0f};
  344. BiquadFilter HFFilter, LFFilter;
  345. void calcCoeffs(const float length, const float lfDecayTime, const float mfDecayTime,
  346. const float hfDecayTime, const float lf0norm, const float hf0norm);
  347. /* Applies the two T60 damping filter sections. */
  348. void process(const al::span<float> samples)
  349. { DualBiquad{HFFilter, LFFilter}.process(samples, samples); }
  350. void clear() noexcept { HFFilter.clear(); LFFilter.clear(); }
  351. };
  352. struct EarlyReflections {
  353. Allpass4 VecAp;
  354. /* An echo line is used to complete the second half of the early
  355. * reflections.
  356. */
  357. DelayLineU Delay;
  358. std::array<size_t,NUM_LINES> Offset{};
  359. std::array<float,NUM_LINES> Coeff{};
  360. /* The gain for each output channel based on 3D panning. */
  361. struct OutGains {
  362. std::array<float,MaxAmbiChannels> Current{};
  363. std::array<float,MaxAmbiChannels> Target{};
  364. void clear() { Current.fill(0.0f); Target.fill(0.0); }
  365. };
  366. std::array<OutGains,NUM_LINES> Gains{};
  367. void updateLines(const float density_mult, const float diffusion, const float decayTime,
  368. const float frequency);
  369. void clear()
  370. {
  371. std::for_each(Gains.begin(), Gains.end(), std::mem_fn(&OutGains::clear));
  372. }
  373. };
  374. struct Modulation {
  375. /* The vibrato time is tracked with an index over a (MOD_FRACONE)
  376. * normalized range.
  377. */
  378. uint Index{0u}, Step{1u};
  379. /* The depth of frequency change, in samples. */
  380. float Depth{0.0f};
  381. std::array<uint,MAX_UPDATE_SAMPLES> ModDelays{};
  382. void updateModulator(float modTime, float modDepth, float frequency);
  383. auto calcDelays(size_t todo) -> al::span<const uint>;
  384. void clear() noexcept
  385. {
  386. Index = 0u;
  387. Step = 1u;
  388. Depth = 0.0f;
  389. }
  390. };
  391. struct LateReverb {
  392. /* A recursive delay line is used fill in the reverb tail. */
  393. DelayLineU Delay;
  394. std::array<size_t,NUM_LINES> Offset{};
  395. /* Attenuation to compensate for the modal density and decay rate of the
  396. * late lines.
  397. */
  398. float DensityGain{0.0f};
  399. /* T60 decay filters are used to simulate absorption. */
  400. std::array<T60Filter,NUM_LINES> T60;
  401. Modulation Mod;
  402. /* A Gerzon vector all-pass filter is used to simulate diffusion. */
  403. VecAllpass VecAp;
  404. /* The gain for each output channel based on 3D panning. */
  405. struct OutGains {
  406. std::array<float,MaxAmbiChannels> Current{};
  407. std::array<float,MaxAmbiChannels> Target{};
  408. void clear() { Current.fill(0.0f); Target.fill(0.0); }
  409. };
  410. std::array<OutGains,NUM_LINES> Gains{};
  411. void updateLines(const float density_mult, const float diffusion, const float lfDecayTime,
  412. const float mfDecayTime, const float hfDecayTime, const float lf0norm,
  413. const float hf0norm, const float frequency);
  414. void clear()
  415. {
  416. std::for_each(T60.begin(), T60.end(), std::mem_fn(&T60Filter::clear));
  417. Mod.clear();
  418. std::for_each(Gains.begin(), Gains.end(), std::mem_fn(&OutGains::clear));
  419. }
  420. };
  421. struct ReverbPipeline {
  422. /* Master effect filters */
  423. struct FilterPair {
  424. BiquadFilter Lp;
  425. BiquadFilter Hp;
  426. void clear() noexcept { Lp.clear(); Hp.clear(); }
  427. };
  428. std::array<FilterPair,NUM_LINES> mFilter;
  429. /* Late reverb input delay line (early reflections feed this, and late
  430. * reverb taps from it).
  431. */
  432. DelayLineU mLateDelayIn;
  433. /* Tap points for early reflection input delay. */
  434. std::array<std::array<size_t,2>,NUM_LINES> mEarlyDelayTap{};
  435. std::array<std::array<float,2>,NUM_LINES> mEarlyDelayCoeff{};
  436. /* Tap points for late reverb feed and delay. */
  437. std::array<std::array<size_t,2>,NUM_LINES> mLateDelayTap{};
  438. /* Coefficients for the all-pass and line scattering matrices. */
  439. float mMixX{1.0f};
  440. float mMixY{0.0f};
  441. EarlyReflections mEarly;
  442. LateReverb mLate;
  443. std::array<std::array<BandSplitter,NUM_LINES>,2> mAmbiSplitter;
  444. size_t mFadeSampleCount{1};
  445. void updateDelayLine(const float gain, const float earlyDelay, const float lateDelay,
  446. const float density_mult, const float decayTime, const float frequency);
  447. void update3DPanning(const al::span<const float,3> ReflectionsPan,
  448. const al::span<const float,3> LateReverbPan, const float earlyGain, const float lateGain,
  449. const bool doUpmix, const MixParams *mainMix);
  450. void processEarly(const DelayLineU &main_delay, size_t offset, const size_t samplesToDo,
  451. const al::span<ReverbUpdateLine,NUM_LINES> tempSamples,
  452. const al::span<FloatBufferLine,NUM_LINES> outSamples);
  453. void processLate(size_t offset, const size_t samplesToDo,
  454. const al::span<ReverbUpdateLine,NUM_LINES> tempSamples,
  455. const al::span<FloatBufferLine,NUM_LINES> outSamples);
  456. void clear() noexcept
  457. {
  458. std::for_each(mFilter.begin(), mFilter.end(), std::mem_fn(&FilterPair::clear));
  459. mEarlyDelayTap = {};
  460. mEarlyDelayCoeff = {};
  461. mLateDelayTap = {};
  462. mEarly.clear();
  463. mLate.clear();
  464. auto clear_filters = [](const al::span<BandSplitter,NUM_LINES> filters)
  465. { std::for_each(filters.begin(), filters.end(), std::mem_fn(&BandSplitter::clear)); };
  466. std::for_each(mAmbiSplitter.begin(), mAmbiSplitter.end(), clear_filters);
  467. }
  468. };
  469. struct ReverbState final : public EffectState {
  470. /* All delay lines are allocated as a single buffer to reduce memory
  471. * fragmentation and management code.
  472. */
  473. al::vector<float,16> mSampleBuffer;
  474. struct Params {
  475. /* Calculated parameters which indicate if cross-fading is needed after
  476. * an update.
  477. */
  478. float Density{1.0f};
  479. float Diffusion{1.0f};
  480. float DecayTime{1.49f};
  481. float HFDecayTime{0.83f * 1.49f};
  482. float LFDecayTime{1.0f * 1.49f};
  483. float ModulationTime{0.25f};
  484. float ModulationDepth{0.0f};
  485. float HFReference{5000.0f};
  486. float LFReference{250.0f};
  487. };
  488. Params mParams;
  489. enum PipelineState : uint8_t {
  490. DeviceClear,
  491. StartFade,
  492. Fading,
  493. Cleanup,
  494. Normal,
  495. };
  496. PipelineState mPipelineState{DeviceClear};
  497. bool mCurrentPipeline{false};
  498. /* Core delay line (early reflections tap from this). */
  499. DelayLineU mMainDelay;
  500. std::array<ReverbPipeline,2> mPipelines;
  501. /* The current write offset for all delay lines. */
  502. size_t mOffset{};
  503. /* Temporary storage used when processing. */
  504. alignas(16) FloatBufferLine mTempLine{};
  505. alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mTempSamples{};
  506. alignas(16) std::array<FloatBufferLine,NUM_LINES> mEarlySamples{};
  507. alignas(16) std::array<FloatBufferLine,NUM_LINES> mLateSamples{};
  508. std::array<float,MaxAmbiOrder+1> mOrderScales{};
  509. bool mUpmixOutput{false};
  510. void MixOutPlain(ReverbPipeline &pipeline, const al::span<FloatBufferLine> samplesOut,
  511. const size_t todo) const
  512. {
  513. /* When not upsampling, the panning gains convert to B-Format and pan
  514. * at the same time.
  515. */
  516. auto inBuffer = mEarlySamples.cbegin();
  517. for(auto &gains : pipeline.mEarly.Gains)
  518. {
  519. MixSamples(al::span{*inBuffer++}.first(todo), samplesOut, gains.Current, gains.Target,
  520. todo, 0);
  521. }
  522. inBuffer = mLateSamples.cbegin();
  523. for(auto &gains : pipeline.mLate.Gains)
  524. {
  525. MixSamples(al::span{*inBuffer++}.first(todo), samplesOut, gains.Current, gains.Target,
  526. todo, 0);
  527. }
  528. }
  529. void MixOutAmbiUp(ReverbPipeline &pipeline, const al::span<FloatBufferLine> samplesOut,
  530. const size_t todo)
  531. {
  532. auto DoMixRow = [](const al::span<float> OutBuffer, const al::span<const float,4> Gains,
  533. const al::span<const FloatBufferLine,4> InSamples)
  534. {
  535. auto inBuffer = InSamples.cbegin();
  536. std::fill(OutBuffer.begin(), OutBuffer.end(), 0.0f);
  537. for(const float gain : Gains)
  538. {
  539. if(std::fabs(gain) > GainSilenceThreshold)
  540. {
  541. auto mix_sample = [gain](const float sample, const float in) noexcept -> float
  542. { return sample + in*gain; };
  543. std::transform(OutBuffer.begin(), OutBuffer.end(), inBuffer->cbegin(),
  544. OutBuffer.begin(), mix_sample);
  545. }
  546. ++inBuffer;
  547. }
  548. };
  549. /* When upsampling, the B-Format conversion needs to be done separately
  550. * so the proper HF scaling can be applied to each B-Format channel.
  551. * The panning gains then pan and upsample the B-Format channels.
  552. */
  553. const auto tmpspan = al::span{mTempLine}.first(todo);
  554. auto hfscale = float{mOrderScales[0]};
  555. auto splitter = pipeline.mAmbiSplitter[0].begin();
  556. auto a2bcoeffs = EarlyA2B.cbegin();
  557. for(auto &gains : pipeline.mEarly.Gains)
  558. {
  559. DoMixRow(tmpspan, *(a2bcoeffs++), mEarlySamples);
  560. /* Apply scaling to the B-Format's HF response to "upsample" it to
  561. * higher-order output.
  562. */
  563. (splitter++)->processHfScale(tmpspan, hfscale);
  564. hfscale = mOrderScales[1];
  565. MixSamples(tmpspan, samplesOut, gains.Current, gains.Target, todo, 0);
  566. }
  567. hfscale = mOrderScales[0];
  568. splitter = pipeline.mAmbiSplitter[1].begin();
  569. a2bcoeffs = LateA2B.cbegin();
  570. for(auto &gains : pipeline.mLate.Gains)
  571. {
  572. DoMixRow(tmpspan, *(a2bcoeffs++), mLateSamples);
  573. (splitter++)->processHfScale(tmpspan, hfscale);
  574. hfscale = mOrderScales[1];
  575. MixSamples(tmpspan, samplesOut, gains.Current, gains.Target, todo, 0);
  576. }
  577. }
  578. void mixOut(ReverbPipeline &pipeline, const al::span<FloatBufferLine> samplesOut, const size_t todo)
  579. {
  580. if(mUpmixOutput)
  581. MixOutAmbiUp(pipeline, samplesOut, todo);
  582. else
  583. MixOutPlain(pipeline, samplesOut, todo);
  584. }
  585. void allocLines(const float frequency);
  586. void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
  587. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  588. const EffectTarget target) override;
  589. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  590. const al::span<FloatBufferLine> samplesOut) override;
  591. };
  592. /**************************************
  593. * Device Update *
  594. **************************************/
  595. inline float CalcDelayLengthMult(float density)
  596. { return std::max(5.0f, std::cbrt(density*DENSITY_SCALE)); }
  597. /* Calculates the delay line metrics and allocates the shared sample buffer
  598. * for all lines given the sample rate (frequency).
  599. */
  600. void ReverbState::allocLines(const float frequency)
  601. {
  602. /* Multiplier for the maximum density value, i.e. density=1, which is
  603. * actually the least density...
  604. */
  605. const float multiplier{CalcDelayLengthMult(1.0f)};
  606. /* The modulator's line length is calculated from the maximum modulation
  607. * time and depth coefficient, and halfed for the low-to-high frequency
  608. * swing.
  609. */
  610. static constexpr float max_mod_delay{MaxModulationTime*MODULATION_DEPTH_COEFF / 2.0f};
  611. std::array<size_t,11> linelengths{};
  612. size_t oidx{0};
  613. size_t totalSamples{0u};
  614. /* The main delay length includes the maximum early reflection delay and
  615. * the largest early tap width. It must also be extended by the update size
  616. * (BufferLineSize) for block processing.
  617. */
  618. float length{ReverbMaxReflectionsDelay + EARLY_TAP_LENGTHS.back()*multiplier};
  619. size_t count{mMainDelay.calcLineLength(length, frequency, BufferLineSize)};
  620. linelengths[oidx++] = count;
  621. totalSamples += count;
  622. for(auto &pipeline : mPipelines)
  623. {
  624. static constexpr float LateDiffAvg{(LATE_LINE_LENGTHS.back()-LATE_LINE_LENGTHS.front()) /
  625. float{NUM_LINES}};
  626. length = ReverbMaxLateReverbDelay + LateDiffAvg*multiplier;
  627. count = pipeline.mLateDelayIn.calcLineLength(length, frequency, BufferLineSize);
  628. linelengths[oidx++] = count;
  629. totalSamples += count;
  630. /* The early vector all-pass line. */
  631. length = EARLY_ALLPASS_LENGTHS.back() * multiplier;
  632. count = pipeline.mEarly.VecAp.Delay.calcLineLength(length, frequency, 0);
  633. linelengths[oidx++] = count;
  634. totalSamples += count;
  635. /* The early reflection line. */
  636. length = EARLY_LINE_LENGTHS.back() * multiplier;
  637. count = pipeline.mEarly.Delay.calcLineLength(length, frequency, MAX_UPDATE_SAMPLES);
  638. linelengths[oidx++] = count;
  639. totalSamples += count;
  640. /* The late vector all-pass line. */
  641. length = LATE_ALLPASS_LENGTHS.back() * multiplier;
  642. count = pipeline.mLate.VecAp.Delay.calcLineLength(length, frequency, 0);
  643. linelengths[oidx++] = count;
  644. totalSamples += count;
  645. /* The late delay lines are calculated from the largest maximum density
  646. * line length, and the maximum modulation delay. Four additional
  647. * samples are needed for resampling the modulator delay.
  648. */
  649. length = LATE_LINE_LENGTHS.back()*multiplier + max_mod_delay;
  650. count = pipeline.mLate.Delay.calcLineLength(length, frequency, 4);
  651. linelengths[oidx++] = count;
  652. totalSamples += count;
  653. }
  654. assert(oidx == linelengths.size());
  655. if(totalSamples != mSampleBuffer.size())
  656. decltype(mSampleBuffer)(totalSamples).swap(mSampleBuffer);
  657. /* Clear the sample buffer. */
  658. std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), 0.0f);
  659. /* Update all delays to reflect the new sample buffer. */
  660. auto bufferspan = al::span{mSampleBuffer};
  661. oidx = 0;
  662. mMainDelay.realizeLineOffset(bufferspan.first(linelengths[oidx]));
  663. bufferspan = bufferspan.subspan(linelengths[oidx++]);
  664. for(auto &pipeline : mPipelines)
  665. {
  666. pipeline.mLateDelayIn.realizeLineOffset(bufferspan.first(linelengths[oidx]));
  667. bufferspan = bufferspan.subspan(linelengths[oidx++]);
  668. pipeline.mEarly.VecAp.Delay.realizeLineOffset(bufferspan.first(linelengths[oidx]));
  669. bufferspan = bufferspan.subspan(linelengths[oidx++]);
  670. pipeline.mEarly.Delay.realizeLineOffset(bufferspan.first(linelengths[oidx]));
  671. bufferspan = bufferspan.subspan(linelengths[oidx++]);
  672. pipeline.mLate.VecAp.Delay.realizeLineOffset(bufferspan.first(linelengths[oidx]));
  673. bufferspan = bufferspan.subspan(linelengths[oidx++]);
  674. pipeline.mLate.Delay.realizeLineOffset(bufferspan.first(linelengths[oidx]));
  675. bufferspan = bufferspan.subspan(linelengths[oidx++]);
  676. }
  677. assert(oidx == linelengths.size());
  678. }
  679. void ReverbState::deviceUpdate(const DeviceBase *device, const BufferStorage*)
  680. {
  681. const auto frequency = static_cast<float>(device->Frequency);
  682. /* Allocate the delay lines. */
  683. allocLines(frequency);
  684. std::for_each(mPipelines.begin(), mPipelines.end(), std::mem_fn(&ReverbPipeline::clear));
  685. mPipelineState = DeviceClear;
  686. /* Reset offset base. */
  687. mOffset = 0;
  688. if(device->mAmbiOrder > 1)
  689. {
  690. mUpmixOutput = true;
  691. mOrderScales = AmbiScale::GetHFOrderScales(1, device->mAmbiOrder, device->m2DMixing);
  692. }
  693. else
  694. {
  695. mUpmixOutput = false;
  696. mOrderScales.fill(1.0f);
  697. }
  698. auto splitter = BandSplitter{device->mXOverFreq / frequency};
  699. auto set_splitters = [&splitter](ReverbPipeline &pipeline)
  700. {
  701. std::fill(pipeline.mAmbiSplitter[0].begin(), pipeline.mAmbiSplitter[0].end(), splitter);
  702. std::fill(pipeline.mAmbiSplitter[1].begin(), pipeline.mAmbiSplitter[1].end(), splitter);
  703. };
  704. std::for_each(mPipelines.begin(), mPipelines.end(), set_splitters);
  705. }
  706. /**************************************
  707. * Effect Update *
  708. **************************************/
  709. /* Calculate a decay coefficient given the length of each cycle and the time
  710. * until the decay reaches -60 dB.
  711. */
  712. inline float CalcDecayCoeff(const float length, const float decayTime)
  713. { return std::pow(ReverbDecayGain, length/decayTime); }
  714. /* Calculate a decay length from a coefficient and the time until the decay
  715. * reaches -60 dB.
  716. */
  717. inline float CalcDecayLength(const float coeff, const float decayTime)
  718. {
  719. constexpr float log10_decaygain{-3.0f/*std::log10(ReverbDecayGain)*/};
  720. return std::log10(coeff) * decayTime / log10_decaygain;
  721. }
  722. /* Calculate an attenuation to be applied to the input of any echo models to
  723. * compensate for modal density and decay time.
  724. */
  725. inline float CalcDensityGain(const float a)
  726. {
  727. /* The energy of a signal can be obtained by finding the area under the
  728. * squared signal. This takes the form of Sum(x_n^2), where x is the
  729. * amplitude for the sample n.
  730. *
  731. * Decaying feedback matches exponential decay of the form Sum(a^n),
  732. * where a is the attenuation coefficient, and n is the sample. The area
  733. * under this decay curve can be calculated as: 1 / (1 - a).
  734. *
  735. * Modifying the above equation to find the area under the squared curve
  736. * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
  737. * calculated by inverting the square root of this approximation,
  738. * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
  739. */
  740. return std::sqrt(1.0f - a*a);
  741. }
  742. /* Calculate the scattering matrix coefficients given a diffusion factor. */
  743. inline void CalcMatrixCoeffs(const float diffusion, float *x, float *y)
  744. {
  745. /* The matrix is of order 4, so n is sqrt(4 - 1). */
  746. constexpr float n{al::numbers::sqrt3_v<float>};
  747. const float t{diffusion * std::atan(n)};
  748. /* Calculate the first mixing matrix coefficient. */
  749. *x = std::cos(t);
  750. /* Calculate the second mixing matrix coefficient. */
  751. *y = std::sin(t) / n;
  752. }
  753. /* Calculate the limited HF ratio for use with the late reverb low-pass
  754. * filters.
  755. */
  756. float CalcLimitedHfRatio(const float hfRatio, const float airAbsorptionGainHF,
  757. const float decayTime)
  758. {
  759. /* Find the attenuation due to air absorption in dB (converting delay
  760. * time to meters using the speed of sound). Then reversing the decay
  761. * equation, solve for HF ratio. The delay length is cancelled out of
  762. * the equation, so it can be calculated once for all lines.
  763. */
  764. float limitRatio{1.0f / SpeedOfSoundMetersPerSec /
  765. CalcDecayLength(airAbsorptionGainHF, decayTime)};
  766. /* Using the limit calculated above, apply the upper bound to the HF ratio. */
  767. return std::min(limitRatio, hfRatio);
  768. }
  769. /* Calculates the 3-band T60 damping coefficients for a particular delay line
  770. * of specified length, using a combination of two shelf filter sections given
  771. * decay times for each band split at two reference frequencies.
  772. */
  773. void T60Filter::calcCoeffs(const float length, const float lfDecayTime,
  774. const float mfDecayTime, const float hfDecayTime, const float lf0norm,
  775. const float hf0norm)
  776. {
  777. const float mfGain{CalcDecayCoeff(length, mfDecayTime)};
  778. const float lfGain{CalcDecayCoeff(length, lfDecayTime) / mfGain};
  779. const float hfGain{CalcDecayCoeff(length, hfDecayTime) / mfGain};
  780. MidGain = mfGain;
  781. LFFilter.setParamsFromSlope(BiquadType::LowShelf, lf0norm, lfGain, 1.0f);
  782. HFFilter.setParamsFromSlope(BiquadType::HighShelf, hf0norm, hfGain, 1.0f);
  783. }
  784. /* Update the early reflection line lengths and gain coefficients. */
  785. void EarlyReflections::updateLines(const float density_mult, const float diffusion,
  786. const float decayTime, const float frequency)
  787. {
  788. /* Calculate the all-pass feed-back/forward coefficient. */
  789. VecAp.Coeff = diffusion*diffusion * InvSqrt2;
  790. for(size_t i{0u};i < NUM_LINES;i++)
  791. {
  792. /* Calculate the delay length of each all-pass line. */
  793. float length{EARLY_ALLPASS_LENGTHS[i] * density_mult};
  794. VecAp.Offset[i] = float2uint(length * frequency);
  795. /* Calculate the delay length of each delay line. */
  796. length = EARLY_LINE_LENGTHS[i] * density_mult;
  797. Offset[i] = float2uint(length * frequency);
  798. /* Calculate the gain (coefficient) for each line. */
  799. Coeff[i] = CalcDecayCoeff(length, decayTime);
  800. }
  801. }
  802. /* Update the EAX modulation step and depth. Keep in mind that this kind of
  803. * vibrato is additive and not multiplicative as one may expect. The downswing
  804. * will sound stronger than the upswing.
  805. */
  806. void Modulation::updateModulator(float modTime, float modDepth, float frequency)
  807. {
  808. /* Modulation is calculated in two parts.
  809. *
  810. * The modulation time effects the sinus rate, altering the speed of
  811. * frequency changes. An index is incremented for each sample with an
  812. * appropriate step size to generate an LFO, which will vary the feedback
  813. * delay over time.
  814. */
  815. Step = std::max(fastf2u(MOD_FRACONE / (frequency * modTime)), 1u);
  816. /* The modulation depth effects the amount of frequency change over the
  817. * range of the sinus. It needs to be scaled by the modulation time so that
  818. * a given depth produces a consistent change in frequency over all ranges
  819. * of time. Since the depth is applied to a sinus value, it needs to be
  820. * halved once for the sinus range and again for the sinus swing in time
  821. * (half of it is spent decreasing the frequency, half is spent increasing
  822. * it).
  823. */
  824. if(modTime >= DefaultModulationTime)
  825. {
  826. /* To cancel the effects of a long period modulation on the late
  827. * reverberation, the amount of pitch should be varied (decreased)
  828. * according to the modulation time. The natural form is varying
  829. * inversely, in fact resulting in an invariant.
  830. */
  831. Depth = MODULATION_DEPTH_COEFF / 4.0f * DefaultModulationTime * modDepth * frequency;
  832. }
  833. else
  834. Depth = MODULATION_DEPTH_COEFF / 4.0f * modTime * modDepth * frequency;
  835. }
  836. /* Update the late reverb line lengths and T60 coefficients. */
  837. void LateReverb::updateLines(const float density_mult, const float diffusion,
  838. const float lfDecayTime, const float mfDecayTime, const float hfDecayTime,
  839. const float lf0norm, const float hf0norm, const float frequency)
  840. {
  841. /* Scaling factor to convert the normalized reference frequencies from
  842. * representing 0...freq to 0...max_reference.
  843. */
  844. constexpr float MaxHFReference{20000.0f};
  845. const float norm_weight_factor{frequency / MaxHFReference};
  846. const float late_allpass_avg{
  847. std::accumulate(LATE_ALLPASS_LENGTHS.begin(), LATE_ALLPASS_LENGTHS.end(), 0.0f) /
  848. float{NUM_LINES}};
  849. /* To compensate for changes in modal density and decay time of the late
  850. * reverb signal, the input is attenuated based on the maximal energy of
  851. * the outgoing signal. This approximation is used to keep the apparent
  852. * energy of the signal equal for all ranges of density and decay time.
  853. *
  854. * The average length of the delay lines is used to calculate the
  855. * attenuation coefficient.
  856. */
  857. float length{std::accumulate(LATE_LINE_LENGTHS.begin(), LATE_LINE_LENGTHS.end(), 0.0f) /
  858. float{NUM_LINES} + late_allpass_avg};
  859. length *= density_mult;
  860. /* The density gain calculation uses an average decay time weighted by
  861. * approximate bandwidth. This attempts to compensate for losses of energy
  862. * that reduce decay time due to scattering into highly attenuated bands.
  863. */
  864. const float decayTimeWeighted{
  865. lf0norm*norm_weight_factor*lfDecayTime +
  866. (hf0norm - lf0norm)*norm_weight_factor*mfDecayTime +
  867. (1.0f - hf0norm*norm_weight_factor)*hfDecayTime};
  868. DensityGain = CalcDensityGain(CalcDecayCoeff(length, decayTimeWeighted));
  869. /* Calculate the all-pass feed-back/forward coefficient. */
  870. VecAp.Coeff = diffusion*diffusion * InvSqrt2;
  871. for(size_t i{0u};i < NUM_LINES;i++)
  872. {
  873. /* Calculate the delay length of each all-pass line. */
  874. length = LATE_ALLPASS_LENGTHS[i] * density_mult;
  875. VecAp.Offset[i] = float2uint(length * frequency);
  876. /* Calculate the delay length of each feedback delay line. A cubic
  877. * resampler is used for modulation on the feedback delay, which
  878. * includes one sample of delay. Reduce by one to compensate.
  879. */
  880. length = LATE_LINE_LENGTHS[i] * density_mult;
  881. Offset[i] = std::max(float2uint(length*frequency + 0.5f), 1u) - 1u;
  882. /* Approximate the absorption that the vector all-pass would exhibit
  883. * given the current diffusion so we don't have to process a full T60
  884. * filter for each of its four lines. Also include the average
  885. * modulation delay (depth is half the max delay in samples).
  886. */
  887. length += lerpf(LATE_ALLPASS_LENGTHS[i], late_allpass_avg, diffusion)*density_mult +
  888. Mod.Depth/frequency;
  889. /* Calculate the T60 damping coefficients for each line. */
  890. T60[i].calcCoeffs(length, lfDecayTime, mfDecayTime, hfDecayTime, lf0norm, hf0norm);
  891. }
  892. }
  893. /* Update the offsets for the main effect delay line. */
  894. void ReverbPipeline::updateDelayLine(const float gain, const float earlyDelay,
  895. const float lateDelay, const float density_mult, const float decayTime, const float frequency)
  896. {
  897. /* Early reflection taps are decorrelated by means of an average room
  898. * reflection approximation described above the definition of the taps.
  899. * This approximation is linear and so the above density multiplier can
  900. * be applied to adjust the width of the taps. A single-band decay
  901. * coefficient is applied to simulate initial attenuation and absorption.
  902. *
  903. * Late reverb taps are based on the late line lengths to allow a zero-
  904. * delay path and offsets that would continue the propagation naturally
  905. * into the late lines.
  906. */
  907. for(size_t i{0u};i < NUM_LINES;i++)
  908. {
  909. float length{EARLY_TAP_LENGTHS[i]*density_mult};
  910. mEarlyDelayTap[i][1] = float2uint((earlyDelay+length) * frequency);
  911. mEarlyDelayCoeff[i][1] = CalcDecayCoeff(length, decayTime) * gain;
  912. /* Reduce the late delay tap by the shortest early delay line length to
  913. * compensate for the late line input being fed by the delayed early
  914. * output.
  915. */
  916. length = (LATE_LINE_LENGTHS[i] - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*density_mult +
  917. lateDelay;
  918. mLateDelayTap[i][1] = float2uint(length * frequency);
  919. }
  920. }
  921. /* Creates a transform matrix given a reverb vector. The vector pans the reverb
  922. * reflections toward the given direction, using its magnitude (up to 1) as a
  923. * focal strength. This function results in a B-Format transformation matrix
  924. * that spatially focuses the signal in the desired direction.
  925. */
  926. std::array<std::array<float,4>,4> GetTransformFromVector(const al::span<const float,3> vec)
  927. {
  928. /* Normalize the panning vector according to the N3D scale, which has an
  929. * extra sqrt(3) term on the directional components. Converting from OpenAL
  930. * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however
  931. * that the reverb panning vectors use left-handed coordinates, unlike the
  932. * rest of OpenAL which use right-handed. This is fixed by negating Z,
  933. * which cancels out with the B-Format Z negation.
  934. */
  935. std::array<float,3> norm{{vec[0], vec[1], vec[2]}};
  936. float mag{std::sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])};
  937. if(mag > 1.0f)
  938. {
  939. const float scale{al::numbers::sqrt3_v<float> / mag};
  940. norm[0] *= -scale;
  941. norm[1] *= scale;
  942. norm[2] *= scale;
  943. mag = 1.0f;
  944. }
  945. else
  946. {
  947. /* If the magnitude is less than or equal to 1, just apply the sqrt(3)
  948. * term. There's no need to renormalize the magnitude since it would
  949. * just be reapplied in the matrix.
  950. */
  951. norm[0] *= -al::numbers::sqrt3_v<float>;
  952. norm[1] *= al::numbers::sqrt3_v<float>;
  953. norm[2] *= al::numbers::sqrt3_v<float>;
  954. }
  955. return std::array<std::array<float,4>,4>{{
  956. {{1.0f, 0.0f, 0.0f, 0.0f}},
  957. {{norm[0], 1.0f-mag, 0.0f, 0.0f}},
  958. {{norm[1], 0.0f, 1.0f-mag, 0.0f}},
  959. {{norm[2], 0.0f, 0.0f, 1.0f-mag}}
  960. }};
  961. }
  962. /* Update the early and late 3D panning gains. */
  963. void ReverbPipeline::update3DPanning(const al::span<const float,3> ReflectionsPan,
  964. const al::span<const float,3> LateReverbPan, const float earlyGain, const float lateGain,
  965. const bool doUpmix, const MixParams *mainMix)
  966. {
  967. /* Create matrices that transform a B-Format signal according to the
  968. * panning vectors.
  969. */
  970. const auto earlymat = GetTransformFromVector(ReflectionsPan);
  971. const auto latemat = GetTransformFromVector(LateReverbPan);
  972. const auto [earlycoeffs, latecoeffs] = [&]{
  973. if(doUpmix)
  974. {
  975. /* When upsampling, combine the early and late transforms with the
  976. * first-order upsample matrix. This results in panning gains that
  977. * apply the panning transform to first-order B-Format, which is
  978. * then upsampled.
  979. */
  980. auto mult_matrix = [](const al::span<const std::array<float,4>,4> mtx1)
  981. {
  982. std::array<std::array<float,MaxAmbiChannels>,NUM_LINES> res{};
  983. const auto mtx2 = al::span{AmbiScale::FirstOrderUp};
  984. for(size_t i{0};i < mtx1[0].size();++i)
  985. {
  986. const al::span dst{res[i]};
  987. static_assert(dst.size() >= std::tuple_size_v<decltype(mtx2)::element_type>);
  988. for(size_t k{0};k < mtx1.size();++k)
  989. {
  990. const float a{mtx1[k][i]};
  991. std::transform(mtx2[k].begin(), mtx2[k].end(), dst.begin(), dst.begin(),
  992. [a](const float in, const float out) noexcept -> float
  993. { return a*in + out; });
  994. }
  995. }
  996. return res;
  997. };
  998. return std::make_pair(mult_matrix(earlymat), mult_matrix(latemat));
  999. }
  1000. /* When not upsampling, combine the early and late A-to-B-Format
  1001. * conversions with their respective transform. This results panning
  1002. * gains that convert A-Format to B-Format, which is then panned.
  1003. */
  1004. auto mult_matrix = [](const al::span<const std::array<float,NUM_LINES>,4> mtx1,
  1005. const al::span<const std::array<float,4>,4> mtx2)
  1006. {
  1007. std::array<std::array<float,MaxAmbiChannels>,NUM_LINES> res{};
  1008. for(size_t i{0};i < mtx1[0].size();++i)
  1009. {
  1010. const al::span dst{res[i]};
  1011. static_assert(dst.size() >= std::tuple_size_v<decltype(mtx2)::element_type>);
  1012. for(size_t k{0};k < mtx1.size();++k)
  1013. {
  1014. const float a{mtx1[k][i]};
  1015. std::transform(mtx2[k].begin(), mtx2[k].end(), dst.begin(), dst.begin(),
  1016. [a](const float in, const float out) noexcept -> float
  1017. { return a*in + out; });
  1018. }
  1019. }
  1020. return res;
  1021. };
  1022. return std::make_pair(mult_matrix(EarlyA2B, earlymat), mult_matrix(LateA2B, latemat));
  1023. }();
  1024. auto earlygains = mEarly.Gains.begin();
  1025. for(auto &coeffs : earlycoeffs)
  1026. ComputePanGains(mainMix, coeffs, earlyGain, (earlygains++)->Target);
  1027. auto lategains = mLate.Gains.begin();
  1028. for(auto &coeffs : latecoeffs)
  1029. ComputePanGains(mainMix, coeffs, lateGain, (lategains++)->Target);
  1030. }
  1031. void ReverbState::update(const ContextBase *Context, const EffectSlot *Slot,
  1032. const EffectProps *props_, const EffectTarget target)
  1033. {
  1034. auto &props = std::get<ReverbProps>(*props_);
  1035. const DeviceBase *Device{Context->mDevice};
  1036. const auto frequency = static_cast<float>(Device->Frequency);
  1037. /* If the HF limit parameter is flagged, calculate an appropriate limit
  1038. * based on the air absorption parameter.
  1039. */
  1040. float hfRatio{props.DecayHFRatio};
  1041. if(props.DecayHFLimit && props.AirAbsorptionGainHF < 1.0f)
  1042. hfRatio = CalcLimitedHfRatio(hfRatio, props.AirAbsorptionGainHF, props.DecayTime);
  1043. /* Calculate the LF/HF decay times. */
  1044. constexpr float MinDecayTime{0.1f}, MaxDecayTime{20.0f};
  1045. const float lfDecayTime{std::clamp(props.DecayTime*props.DecayLFRatio, MinDecayTime,
  1046. MaxDecayTime)};
  1047. const float hfDecayTime{std::clamp(props.DecayTime*hfRatio, MinDecayTime, MaxDecayTime)};
  1048. /* Determine if a full update is required. */
  1049. const bool fullUpdate{mPipelineState == DeviceClear ||
  1050. /* Density is essentially a master control for the feedback delays, so
  1051. * changes the offsets of many delay lines.
  1052. */
  1053. mParams.Density != props.Density ||
  1054. /* Diffusion and decay times influences the decay rate (gain) of the
  1055. * late reverb T60 filter.
  1056. */
  1057. mParams.Diffusion != props.Diffusion ||
  1058. mParams.DecayTime != props.DecayTime ||
  1059. mParams.HFDecayTime != hfDecayTime ||
  1060. mParams.LFDecayTime != lfDecayTime ||
  1061. /* Modulation time and depth both require fading the modulation delay. */
  1062. mParams.ModulationTime != props.ModulationTime ||
  1063. mParams.ModulationDepth != props.ModulationDepth ||
  1064. /* HF/LF References control the weighting used to calculate the density
  1065. * gain.
  1066. */
  1067. mParams.HFReference != props.HFReference ||
  1068. mParams.LFReference != props.LFReference};
  1069. if(fullUpdate)
  1070. {
  1071. mParams.Density = props.Density;
  1072. mParams.Diffusion = props.Diffusion;
  1073. mParams.DecayTime = props.DecayTime;
  1074. mParams.HFDecayTime = hfDecayTime;
  1075. mParams.LFDecayTime = lfDecayTime;
  1076. mParams.ModulationTime = props.ModulationTime;
  1077. mParams.ModulationDepth = props.ModulationDepth;
  1078. mParams.HFReference = props.HFReference;
  1079. mParams.LFReference = props.LFReference;
  1080. mPipelineState = (mPipelineState != DeviceClear) ? StartFade : Normal;
  1081. mCurrentPipeline = !mCurrentPipeline;
  1082. auto &oldpipeline = mPipelines[!mCurrentPipeline];
  1083. for(size_t j{0};j < NUM_LINES;++j)
  1084. oldpipeline.mEarlyDelayCoeff[j][1] = 0.0f;
  1085. }
  1086. auto &pipeline = mPipelines[mCurrentPipeline];
  1087. /* The density-based room size (delay length) multiplier. */
  1088. const float density_mult{CalcDelayLengthMult(props.Density)};
  1089. /* Update the main effect delay and associated taps. */
  1090. pipeline.updateDelayLine(props.Gain, props.ReflectionsDelay, props.LateReverbDelay,
  1091. density_mult, props.DecayTime, frequency);
  1092. /* Update early and late 3D panning. */
  1093. mOutTarget = target.Main->Buffer;
  1094. const float gain{Slot->Gain * ReverbBoost};
  1095. pipeline.update3DPanning(props.ReflectionsPan, props.LateReverbPan, props.ReflectionsGain*gain,
  1096. props.LateReverbGain*gain, mUpmixOutput, target.Main);
  1097. /* Calculate the master filters */
  1098. float hf0norm{std::min(props.HFReference/frequency, 0.49f)};
  1099. pipeline.mFilter[0].Lp.setParamsFromSlope(BiquadType::HighShelf, hf0norm, props.GainHF, 1.0f);
  1100. float lf0norm{std::min(props.LFReference/frequency, 0.49f)};
  1101. pipeline.mFilter[0].Hp.setParamsFromSlope(BiquadType::LowShelf, lf0norm, props.GainLF, 1.0f);
  1102. for(size_t i{1u};i < NUM_LINES;i++)
  1103. {
  1104. pipeline.mFilter[i].Lp.copyParamsFrom(pipeline.mFilter[0].Lp);
  1105. pipeline.mFilter[i].Hp.copyParamsFrom(pipeline.mFilter[0].Hp);
  1106. }
  1107. if(fullUpdate)
  1108. {
  1109. /* Update the early lines. */
  1110. pipeline.mEarly.updateLines(density_mult, props.Diffusion, props.DecayTime, frequency);
  1111. /* Get the mixing matrix coefficients. */
  1112. CalcMatrixCoeffs(props.Diffusion, &pipeline.mMixX, &pipeline.mMixY);
  1113. /* Update the modulator rate and depth. */
  1114. pipeline.mLate.Mod.updateModulator(props.ModulationTime, props.ModulationDepth, frequency);
  1115. /* Update the late lines. */
  1116. pipeline.mLate.updateLines(density_mult, props.Diffusion, lfDecayTime, props.DecayTime,
  1117. hfDecayTime, lf0norm, hf0norm, frequency);
  1118. }
  1119. /* Calculate the gain at the start of the late reverb stage, and the gain
  1120. * difference from the decay target (0.001, or -60dB).
  1121. */
  1122. const float decayBase{props.ReflectionsGain * props.LateReverbGain};
  1123. const float decayDiff{ReverbDecayGain / decayBase};
  1124. /* Given the DecayTime (the amount of time for the late reverb to decay by
  1125. * -60dB), calculate the time to decay to -60dB from the start of the late
  1126. * reverb.
  1127. *
  1128. * Otherwise, if the late reverb already starts at -60dB or less, only
  1129. * include the time to get to the late reverb.
  1130. */
  1131. const float diffTime{!(decayDiff < 1.0f) ? 0.0f
  1132. : (std::log10(decayDiff)*(20.0f / -60.0f) * props.DecayTime)};
  1133. const float decaySamples{(props.ReflectionsDelay+props.LateReverbDelay+diffTime)
  1134. * frequency};
  1135. /* Limit to 100,000 samples (a touch over 2 seconds at 48khz) to avoid
  1136. * excessive double-processing.
  1137. */
  1138. pipeline.mFadeSampleCount = static_cast<size_t>(std::min(decaySamples, 100'000.0f));
  1139. }
  1140. /**************************************
  1141. * Effect Processing *
  1142. **************************************/
  1143. /* Applies a scattering matrix to the 4-line (vector) input. This is used
  1144. * for both the below vector all-pass model and to perform modal feed-back
  1145. * delay network (FDN) mixing.
  1146. *
  1147. * The matrix is derived from a skew-symmetric matrix to form a 4D rotation
  1148. * matrix with a single unitary rotational parameter:
  1149. *
  1150. * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
  1151. * [ -a, d, c, -b ]
  1152. * [ -b, -c, d, a ]
  1153. * [ -c, b, -a, d ]
  1154. *
  1155. * The rotation is constructed from the effect's diffusion parameter,
  1156. * yielding:
  1157. *
  1158. * 1 = x^2 + 3 y^2
  1159. *
  1160. * Where a, b, and c are the coefficient y with differing signs, and d is the
  1161. * coefficient x. The final matrix is thus:
  1162. *
  1163. * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
  1164. * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
  1165. * [ y, -y, x, y ] x = cos(t)
  1166. * [ -y, -y, -y, x ] y = sin(t) / n
  1167. *
  1168. * Any square orthogonal matrix with an order that is a power of two will
  1169. * work (where ^T is transpose, ^-1 is inverse):
  1170. *
  1171. * M^T = M^-1
  1172. *
  1173. * Using that knowledge, finding an appropriate matrix can be accomplished
  1174. * naively by searching all combinations of:
  1175. *
  1176. * M = D + S - S^T
  1177. *
  1178. * Where D is a diagonal matrix (of x), and S is a triangular matrix (of y)
  1179. * whose combination of signs are being iterated.
  1180. */
  1181. inline auto VectorPartialScatter(const std::array<float,NUM_LINES> &in, const float xCoeff,
  1182. const float yCoeff) noexcept -> std::array<float,NUM_LINES>
  1183. {
  1184. return std::array{
  1185. xCoeff*in[0] + yCoeff*( in[1] + -in[2] + in[3]),
  1186. xCoeff*in[1] + yCoeff*(-in[0] + in[2] + in[3]),
  1187. xCoeff*in[2] + yCoeff*( in[0] + -in[1] + in[3]),
  1188. xCoeff*in[3] + yCoeff*(-in[0] + -in[1] + -in[2] )
  1189. };
  1190. }
  1191. /* Utilizes the above, but also applies a line-based reflection on the input
  1192. * channels (swapping 0<->3 and 1<->2).
  1193. */
  1194. void VectorScatterRev(const float xCoeff, const float yCoeff,
  1195. const al::span<ReverbUpdateLine,NUM_LINES> samples, const size_t count) noexcept
  1196. {
  1197. ASSUME(count > 0);
  1198. for(size_t i{0u};i < count;++i)
  1199. {
  1200. std::array src{samples[0][i], samples[1][i], samples[2][i], samples[3][i]};
  1201. src = VectorPartialScatter(std::array{src[3], src[2], src[1], src[0]}, xCoeff, yCoeff);
  1202. samples[0][i] = src[0];
  1203. samples[1][i] = src[1];
  1204. samples[2][i] = src[2];
  1205. samples[3][i] = src[3];
  1206. }
  1207. }
  1208. /* This applies a Gerzon multiple-in/multiple-out (MIMO) vector all-pass
  1209. * filter to the 4-line input.
  1210. *
  1211. * It works by vectorizing a regular all-pass filter and replacing the delay
  1212. * element with a scattering matrix (like the one above) and a diagonal
  1213. * matrix of delay elements.
  1214. */
  1215. void VecAllpass::process(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t main_offset,
  1216. const float xCoeff, const float yCoeff, const size_t todo) const noexcept
  1217. {
  1218. const auto linelen = size_t{Delay.mLine.size()/NUM_LINES};
  1219. const float feedCoeff{Coeff};
  1220. ASSUME(todo > 0);
  1221. for(size_t i{0u};i < todo;)
  1222. {
  1223. std::array<size_t,NUM_LINES> vap_offset{};
  1224. std::transform(Offset.cbegin(), Offset.cend(), vap_offset.begin(),
  1225. [main_offset,mask=linelen-1](const size_t delay) noexcept -> size_t
  1226. { return (main_offset-delay) & mask; });
  1227. main_offset &= linelen-1;
  1228. const auto maxoff = std::accumulate(vap_offset.cbegin(), vap_offset.cend(), main_offset,
  1229. [](const size_t offset, const size_t apoffset) { return std::max(offset, apoffset); });
  1230. size_t td{std::min(linelen - maxoff, todo - i)};
  1231. auto delayIn = Delay.mLine.begin();
  1232. auto delayOut = Delay.mLine.begin() + ptrdiff_t(main_offset*NUM_LINES);
  1233. main_offset += td;
  1234. do {
  1235. std::array<float,NUM_LINES> f{};
  1236. for(size_t j{0u};j < NUM_LINES;j++)
  1237. {
  1238. const float input{samples[j][i]};
  1239. const float out{delayIn[vap_offset[j]*NUM_LINES + j] - feedCoeff*input};
  1240. f[j] = input + feedCoeff*out;
  1241. samples[j][i] = out;
  1242. }
  1243. delayIn += NUM_LINES;
  1244. ++i;
  1245. f = VectorPartialScatter(f, xCoeff, yCoeff);
  1246. delayOut = std::copy_n(f.cbegin(), f.size(), delayOut);
  1247. } while(--td);
  1248. }
  1249. }
  1250. /* This applies a more typical all-pass to each line, without the scattering
  1251. * matrix.
  1252. */
  1253. void Allpass4::process(const al::span<ReverbUpdateLine,NUM_LINES> samples, const size_t offset,
  1254. const size_t todo) const noexcept
  1255. {
  1256. const DelayLineU delay{Delay};
  1257. const float feedCoeff{Coeff};
  1258. ASSUME(todo > 0);
  1259. for(size_t j{0u};j < NUM_LINES;j++)
  1260. {
  1261. auto smpiter = samples[j].begin();
  1262. const auto buffer = delay.get(j);
  1263. size_t dstoffset{offset};
  1264. size_t vap_offset{offset - Offset[j]};
  1265. for(size_t i{0u};i < todo;)
  1266. {
  1267. vap_offset &= buffer.size()-1;
  1268. dstoffset &= buffer.size()-1;
  1269. const size_t maxoff{std::max(dstoffset, vap_offset)};
  1270. const size_t td{std::min(buffer.size() - maxoff, todo - i)};
  1271. auto proc_sample = [buffer,feedCoeff,&vap_offset,&dstoffset](const float x) -> float
  1272. {
  1273. const float y{buffer[vap_offset++] - feedCoeff*x};
  1274. buffer[dstoffset++] = x + feedCoeff*y;
  1275. return y;
  1276. };
  1277. smpiter = std::transform(smpiter, smpiter+td, smpiter, proc_sample);
  1278. i += td;
  1279. }
  1280. }
  1281. }
  1282. /* This generates early reflections.
  1283. *
  1284. * This is done by obtaining the primary reflections (those arriving from the
  1285. * same direction as the source) from the main delay line. These are
  1286. * attenuated and all-pass filtered (based on the diffusion parameter).
  1287. *
  1288. * The early lines are then reflected about the origin to create the secondary
  1289. * reflections (those arriving from the opposite direction as the source).
  1290. *
  1291. * The early response is then completed by combining the primary reflections
  1292. * with the delayed and attenuated output from the early lines.
  1293. *
  1294. * Finally, the early response is reflected, scattered (based on diffusion),
  1295. * and fed into the late reverb section of the main delay line.
  1296. */
  1297. void ReverbPipeline::processEarly(const DelayLineU &main_delay, size_t offset,
  1298. const size_t samplesToDo, const al::span<ReverbUpdateLine, NUM_LINES> tempSamples,
  1299. const al::span<FloatBufferLine, NUM_LINES> outSamples)
  1300. {
  1301. const DelayLineU early_delay{mEarly.Delay};
  1302. const DelayLineU in_delay{main_delay};
  1303. const float mixX{mMixX};
  1304. const float mixY{mMixY};
  1305. ASSUME(samplesToDo <= BufferLineSize);
  1306. for(size_t base{0};base < samplesToDo;)
  1307. {
  1308. const size_t todo{std::min(samplesToDo-base, MAX_UPDATE_SAMPLES)};
  1309. /* First, load decorrelated samples from the main delay line as the
  1310. * primary reflections.
  1311. */
  1312. const auto fadeStep = float{1.0f / static_cast<float>(todo)};
  1313. for(size_t j{0_uz};j < NUM_LINES;j++)
  1314. {
  1315. const auto input = in_delay.get(j);
  1316. auto early_delay_tap0 = size_t{offset - mEarlyDelayTap[j][0]};
  1317. auto early_delay_tap1 = size_t{offset - mEarlyDelayTap[j][1]};
  1318. mEarlyDelayTap[j][0] = mEarlyDelayTap[j][1];
  1319. const auto coeff0 = float{mEarlyDelayCoeff[j][0]};
  1320. const auto coeff1 = float{mEarlyDelayCoeff[j][1]};
  1321. mEarlyDelayCoeff[j][0] = mEarlyDelayCoeff[j][1];
  1322. auto fadeCount = float{0.0f};
  1323. auto tmp = tempSamples[j].begin();
  1324. for(size_t i{0_uz};i < todo;)
  1325. {
  1326. early_delay_tap0 &= input.size()-1;
  1327. early_delay_tap1 &= input.size()-1;
  1328. const auto max_tap = size_t{std::max(early_delay_tap0, early_delay_tap1)};
  1329. const auto td = size_t{std::min(input.size()-max_tap, todo-i)};
  1330. const auto intap0 = input.subspan(early_delay_tap0, td);
  1331. const auto intap1 = input.subspan(early_delay_tap1, td);
  1332. auto do_blend = [coeff0,coeff1,fadeStep,&fadeCount](const float in0,
  1333. const float in1) noexcept -> float
  1334. {
  1335. const auto ret = lerpf(in0*coeff0, in1*coeff1, fadeStep*fadeCount);
  1336. fadeCount += 1.0f;
  1337. return ret;
  1338. };
  1339. tmp = std::transform(intap0.begin(), intap0.end(), intap1.begin(), tmp, do_blend);
  1340. early_delay_tap0 += td;
  1341. early_delay_tap1 += td;
  1342. i += td;
  1343. }
  1344. /* Band-pass the incoming samples. */
  1345. auto&& filter = DualBiquad{mFilter[j].Lp, mFilter[j].Hp};
  1346. filter.process(al::span{tempSamples[j]}.first(todo), tempSamples[j]);
  1347. }
  1348. /* Apply an all-pass, to help color the initial reflections. */
  1349. mEarly.VecAp.process(tempSamples, offset, todo);
  1350. /* Apply a delay and bounce to generate secondary reflections. */
  1351. early_delay.writeReflected(offset, tempSamples, todo);
  1352. for(size_t j{0_uz};j < NUM_LINES;j++)
  1353. {
  1354. const auto input = early_delay.get(j);
  1355. auto feedb_tap = size_t{offset - mEarly.Offset[j]};
  1356. const auto feedb_coeff = float{mEarly.Coeff[j]};
  1357. auto out = outSamples[j].begin() + base;
  1358. auto tmp = tempSamples[j].begin();
  1359. for(size_t i{0_uz};i < todo;)
  1360. {
  1361. feedb_tap &= input.size()-1;
  1362. const auto td = size_t{std::min(input.size() - feedb_tap, todo - i)};
  1363. const auto delaySrc = input.subspan(feedb_tap, td);
  1364. /* Combine the main input with the attenuated delayed echo for
  1365. * the early output.
  1366. */
  1367. out = std::transform(delaySrc.begin(), delaySrc.end(), tmp, out,
  1368. [feedb_coeff](const float delayspl, const float mainspl) noexcept -> float
  1369. { return delayspl*feedb_coeff + mainspl; });
  1370. /* Move the (non-attenuated) delayed echo to the temp buffer
  1371. * for feeding the late reverb.
  1372. */
  1373. tmp = std::copy_n(delaySrc.begin(), delaySrc.size(), tmp);
  1374. feedb_tap += td;
  1375. i += td;
  1376. }
  1377. }
  1378. /* Finally, apply a scatter and bounce to improve the initial diffusion
  1379. * in the late reverb, writing the result to the late delay line input.
  1380. */
  1381. VectorScatterRev(mixX, mixY, tempSamples, todo);
  1382. for(size_t j{0_uz};j < NUM_LINES;j++)
  1383. mLateDelayIn.write(offset, j, al::span{tempSamples[j]}.first(todo));
  1384. base += todo;
  1385. offset += todo;
  1386. }
  1387. }
  1388. auto Modulation::calcDelays(size_t todo) -> al::span<const uint>
  1389. {
  1390. auto idx = uint{Index};
  1391. const auto step = uint{Step};
  1392. const auto depth = float{Depth * float{gCubicTable.sTableSteps}};
  1393. const auto delays = al::span{ModDelays}.first(todo);
  1394. std::generate(delays.begin(), delays.end(), [step,depth,&idx]
  1395. {
  1396. idx += step;
  1397. const auto x = float{static_cast<float>(idx&MOD_FRACMASK) * (1.0f/MOD_FRACONE)};
  1398. /* Approximate sin(x*2pi). As long as it roughly fits a sinusoid shape
  1399. * and stays within [-1...+1], it needn't be perfect.
  1400. */
  1401. const auto lfo = float{!(idx&(MOD_FRACONE>>1))
  1402. ? ((-16.0f * x * x) + (8.0f * x))
  1403. : ((16.0f * x * x) + (-8.0f * x) + (-16.0f * x) + 8.0f)};
  1404. return float2uint((lfo+1.0f) * depth);
  1405. });
  1406. Index = idx;
  1407. return delays;
  1408. }
  1409. /* This generates the reverb tail using a modified feed-back delay network
  1410. * (FDN).
  1411. *
  1412. * Results from the early reflections are mixed with the output from the
  1413. * modulated late delay lines.
  1414. *
  1415. * The late response is then completed by T60 and all-pass filtering the mix.
  1416. *
  1417. * Finally, the lines are reversed (so they feed their opposite directions)
  1418. * and scattered with the FDN matrix before re-feeding the delay lines.
  1419. */
  1420. void ReverbPipeline::processLate(size_t offset, const size_t samplesToDo,
  1421. const al::span<ReverbUpdateLine, NUM_LINES> tempSamples,
  1422. const al::span<FloatBufferLine, NUM_LINES> outSamples)
  1423. {
  1424. const DelayLineU late_delay{mLate.Delay};
  1425. const DelayLineU in_delay{mLateDelayIn};
  1426. const float mixX{mMixX};
  1427. const float mixY{mMixY};
  1428. ASSUME(samplesToDo <= BufferLineSize);
  1429. for(size_t base{0};base < samplesToDo;)
  1430. {
  1431. const size_t todo{std::min(std::min(mLate.Offset[0], MAX_UPDATE_SAMPLES),
  1432. samplesToDo-base)};
  1433. ASSUME(todo > 0);
  1434. /* First, calculate the modulated delays for the late feedback. */
  1435. const auto delays = mLate.Mod.calcDelays(todo);
  1436. /* Now load samples from the feedback delay lines. Filter the signal to
  1437. * apply its frequency-dependent decay.
  1438. */
  1439. for(size_t j{0_uz};j < NUM_LINES;++j)
  1440. {
  1441. const auto input = late_delay.get(j);
  1442. const auto midGain = float{mLate.T60[j].MidGain};
  1443. auto late_feedb_tap = size_t{offset - mLate.Offset[j]};
  1444. auto proc_sample = [input,midGain,&late_feedb_tap](const size_t idelay) -> float
  1445. {
  1446. /* Calculate the read sample offset and sub-sample offset
  1447. * between it and the next sample.
  1448. */
  1449. const auto delay = size_t{late_feedb_tap - (idelay>>gCubicTable.sTableBits)};
  1450. const auto delayoffset = size_t{idelay & gCubicTable.sTableMask};
  1451. ++late_feedb_tap;
  1452. /* Get the samples around the delayed offset, interpolated for
  1453. * output.
  1454. */
  1455. const auto out0 = float{input[(delay ) & (input.size()-1)]};
  1456. const auto out1 = float{input[(delay-1) & (input.size()-1)]};
  1457. const auto out2 = float{input[(delay-2) & (input.size()-1)]};
  1458. const auto out3 = float{input[(delay-3) & (input.size()-1)]};
  1459. const auto out = float{out0*gCubicTable.getCoeff0(delayoffset)
  1460. + out1*gCubicTable.getCoeff1(delayoffset)
  1461. + out2*gCubicTable.getCoeff2(delayoffset)
  1462. + out3*gCubicTable.getCoeff3(delayoffset)};
  1463. return out * midGain;
  1464. };
  1465. std::transform(delays.begin(), delays.end(), tempSamples[j].begin(), proc_sample);
  1466. mLate.T60[j].process(al::span{tempSamples[j]}.first(todo));
  1467. }
  1468. /* Next load decorrelated samples from the main delay lines. */
  1469. const float fadeStep{1.0f / static_cast<float>(todo)};
  1470. for(size_t j{0_uz};j < NUM_LINES;++j)
  1471. {
  1472. const auto input = in_delay.get(j);
  1473. auto late_delay_tap0 = size_t{offset - mLateDelayTap[j][0]};
  1474. auto late_delay_tap1 = size_t{offset - mLateDelayTap[j][1]};
  1475. mLateDelayTap[j][0] = mLateDelayTap[j][1];
  1476. const auto densityGain = float{mLate.DensityGain};
  1477. const auto densityStep = float{late_delay_tap0 != late_delay_tap1
  1478. ? densityGain*fadeStep : 0.0f};
  1479. auto fadeCount = float{0.0f};
  1480. auto samples = tempSamples[j].begin();
  1481. for(size_t i{0u};i < todo;)
  1482. {
  1483. late_delay_tap0 &= input.size()-1;
  1484. late_delay_tap1 &= input.size()-1;
  1485. const auto td = size_t{std::min(todo - i,
  1486. input.size() - std::max(late_delay_tap0, late_delay_tap1))};
  1487. auto proc_sample = [input,densityGain,densityStep,&late_delay_tap0,
  1488. &late_delay_tap1,&fadeCount](const float sample) noexcept -> float
  1489. {
  1490. const auto fade0 = float{densityGain - densityStep*fadeCount};
  1491. const auto fade1 = float{densityStep*fadeCount};
  1492. fadeCount += 1.0f;
  1493. return input[late_delay_tap0++]*fade0 + input[late_delay_tap1++]*fade1
  1494. + sample;
  1495. };
  1496. samples = std::transform(samples, samples+ptrdiff_t(td), samples, proc_sample);
  1497. i += td;
  1498. }
  1499. }
  1500. /* Apply a vector all-pass to improve micro-surface diffusion, and
  1501. * write out the results for mixing.
  1502. */
  1503. mLate.VecAp.process(tempSamples, offset, mixX, mixY, todo);
  1504. for(size_t j{0_uz};j < NUM_LINES;++j)
  1505. std::copy_n(tempSamples[j].begin(), todo, outSamples[j].begin()+base);
  1506. /* Finally, scatter and bounce the results to refeed the feedback buffer. */
  1507. VectorScatterRev(mixX, mixY, tempSamples, todo);
  1508. for(size_t j{0_uz};j < NUM_LINES;++j)
  1509. late_delay.write(offset, j, al::span{tempSamples[j]}.first(todo));
  1510. base += todo;
  1511. offset += todo;
  1512. }
  1513. }
  1514. void ReverbState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  1515. {
  1516. const size_t offset{mOffset};
  1517. ASSUME(samplesToDo <= BufferLineSize);
  1518. auto &oldpipeline = mPipelines[!mCurrentPipeline];
  1519. auto &pipeline = mPipelines[mCurrentPipeline];
  1520. /* Convert B-Format to A-Format for processing. */
  1521. const size_t numInput{std::min(samplesIn.size(), NUM_LINES)};
  1522. const al::span<float> tmpspan{al::assume_aligned<16>(mTempLine.data()), samplesToDo};
  1523. for(size_t c{0u};c < NUM_LINES;++c)
  1524. {
  1525. std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
  1526. for(size_t i{0};i < numInput;++i)
  1527. {
  1528. const float gain{B2A[c][i]};
  1529. auto mix_sample = [gain](const float sample, const float in) noexcept -> float
  1530. { return sample + in*gain; };
  1531. std::transform(tmpspan.begin(), tmpspan.end(), samplesIn[i].begin(), tmpspan.begin(),
  1532. mix_sample);
  1533. }
  1534. mMainDelay.write(offset, c, tmpspan);
  1535. }
  1536. if(mPipelineState < Fading)
  1537. mPipelineState = Fading;
  1538. /* Process reverb for these samples. and mix them to the output. */
  1539. pipeline.processEarly(mMainDelay, offset, samplesToDo, mTempSamples, mEarlySamples);
  1540. pipeline.processLate(offset, samplesToDo, mTempSamples, mLateSamples);
  1541. mixOut(pipeline, samplesOut, samplesToDo);
  1542. if(mPipelineState != Normal)
  1543. {
  1544. if(mPipelineState == Cleanup)
  1545. {
  1546. size_t numSamples{mSampleBuffer.size()/2};
  1547. const auto bufferspan = al::span{mSampleBuffer}.subspan(numSamples * !mCurrentPipeline,
  1548. numSamples);
  1549. std::fill_n(bufferspan.begin(), bufferspan.size(), 0.0f);
  1550. oldpipeline.clear();
  1551. mPipelineState = Normal;
  1552. }
  1553. else
  1554. {
  1555. /* If this is the final mix for this old pipeline, set the target
  1556. * gains to 0 to ensure a complete fade out, and set the state to
  1557. * Cleanup so the next invocation cleans up the delay buffers and
  1558. * filters.
  1559. */
  1560. if(samplesToDo >= oldpipeline.mFadeSampleCount)
  1561. {
  1562. for(auto &gains : oldpipeline.mEarly.Gains)
  1563. std::fill(gains.Target.begin(), gains.Target.end(), 0.0f);
  1564. for(auto &gains : oldpipeline.mLate.Gains)
  1565. std::fill(gains.Target.begin(), gains.Target.end(), 0.0f);
  1566. oldpipeline.mFadeSampleCount = 0;
  1567. mPipelineState = Cleanup;
  1568. }
  1569. else
  1570. oldpipeline.mFadeSampleCount -= samplesToDo;
  1571. /* Process the old reverb for these samples. */
  1572. oldpipeline.processEarly(mMainDelay, offset, samplesToDo, mTempSamples, mEarlySamples);
  1573. oldpipeline.processLate(offset, samplesToDo, mTempSamples, mLateSamples);
  1574. mixOut(oldpipeline, samplesOut, samplesToDo);
  1575. }
  1576. }
  1577. mOffset = offset + samplesToDo;
  1578. }
  1579. struct ReverbStateFactory final : public EffectStateFactory {
  1580. al::intrusive_ptr<EffectState> create() override
  1581. { return al::intrusive_ptr<EffectState>{new ReverbState{}}; }
  1582. };
  1583. } // namespace
  1584. EffectStateFactory *ReverbStateFactory_getFactory()
  1585. {
  1586. static ReverbStateFactory ReverbFactory{};
  1587. return &ReverbFactory;
  1588. }