reverb.cpp 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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 <cstdio>
  22. #include <cstdlib>
  23. #include <cmath>
  24. #include <array>
  25. #include <numeric>
  26. #include <algorithm>
  27. #include <functional>
  28. #include "al/auxeffectslot.h"
  29. #include "al/listener.h"
  30. #include "alcmain.h"
  31. #include "alcontext.h"
  32. #include "alu.h"
  33. #include "bformatdec.h"
  34. #include "filters/biquad.h"
  35. #include "vector.h"
  36. #include "vecmat.h"
  37. /* This is a user config option for modifying the overall output of the reverb
  38. * effect.
  39. */
  40. ALfloat ReverbBoost = 1.0f;
  41. namespace {
  42. using namespace std::placeholders;
  43. /* Max samples per process iteration. Used to limit the size needed for
  44. * temporary buffers. Must be a multiple of 4 for SIMD alignment.
  45. */
  46. constexpr size_t MAX_UPDATE_SAMPLES{256};
  47. /* The number of spatialized lines or channels to process. Four channels allows
  48. * for a 3D A-Format response. NOTE: This can't be changed without taking care
  49. * of the conversion matrices, and a few places where the length arrays are
  50. * assumed to have 4 elements.
  51. */
  52. constexpr size_t NUM_LINES{4u};
  53. /* The B-Format to A-Format conversion matrix. The arrangement of rows is
  54. * deliberately chosen to align the resulting lines to their spatial opposites
  55. * (0:above front left <-> 3:above back right, 1:below front right <-> 2:below
  56. * back left). It's not quite opposite, since the A-Format results in a
  57. * tetrahedron, but it's close enough. Should the model be extended to 8-lines
  58. * in the future, true opposites can be used.
  59. */
  60. alignas(16) constexpr ALfloat B2A[NUM_LINES][MAX_AMBI_CHANNELS]{
  61. { 0.288675134595f, 0.288675134595f, 0.288675134595f, 0.288675134595f },
  62. { 0.288675134595f, -0.288675134595f, -0.288675134595f, 0.288675134595f },
  63. { 0.288675134595f, 0.288675134595f, -0.288675134595f, -0.288675134595f },
  64. { 0.288675134595f, -0.288675134595f, 0.288675134595f, -0.288675134595f }
  65. };
  66. /* Converts A-Format to B-Format. */
  67. alignas(16) constexpr ALfloat A2B[NUM_LINES][NUM_LINES]{
  68. { 0.866025403785f, 0.866025403785f, 0.866025403785f, 0.866025403785f },
  69. { 0.866025403785f, -0.866025403785f, 0.866025403785f, -0.866025403785f },
  70. { 0.866025403785f, -0.866025403785f, -0.866025403785f, 0.866025403785f },
  71. { 0.866025403785f, 0.866025403785f, -0.866025403785f, -0.866025403785f }
  72. };
  73. /* The all-pass and delay lines have a variable length dependent on the
  74. * effect's density parameter, which helps alter the perceived environment
  75. * size. The size-to-density conversion is a cubed scale:
  76. *
  77. * density = min(1.0, pow(size, 3.0) / DENSITY_SCALE);
  78. *
  79. * The line lengths scale linearly with room size, so the inverse density
  80. * conversion is needed, taking the cube root of the re-scaled density to
  81. * calculate the line length multiplier:
  82. *
  83. * length_mult = max(5.0, cbrt(density*DENSITY_SCALE));
  84. *
  85. * The density scale below will result in a max line multiplier of 50, for an
  86. * effective size range of 5m to 50m.
  87. */
  88. constexpr ALfloat DENSITY_SCALE{125000.0f};
  89. /* All delay line lengths are specified in seconds.
  90. *
  91. * To approximate early reflections, we break them up into primary (those
  92. * arriving from the same direction as the source) and secondary (those
  93. * arriving from the opposite direction).
  94. *
  95. * The early taps decorrelate the 4-channel signal to approximate an average
  96. * room response for the primary reflections after the initial early delay.
  97. *
  98. * Given an average room dimension (d_a) and the speed of sound (c) we can
  99. * calculate the average reflection delay (r_a) regardless of listener and
  100. * source positions as:
  101. *
  102. * r_a = d_a / c
  103. * c = 343.3
  104. *
  105. * This can extended to finding the average difference (r_d) between the
  106. * maximum (r_1) and minimum (r_0) reflection delays:
  107. *
  108. * r_0 = 2 / 3 r_a
  109. * = r_a - r_d / 2
  110. * = r_d
  111. * r_1 = 4 / 3 r_a
  112. * = r_a + r_d / 2
  113. * = 2 r_d
  114. * r_d = 2 / 3 r_a
  115. * = r_1 - r_0
  116. *
  117. * As can be determined by integrating the 1D model with a source (s) and
  118. * listener (l) positioned across the dimension of length (d_a):
  119. *
  120. * r_d = int_(l=0)^d_a (int_(s=0)^d_a |2 d_a - 2 (l + s)| ds) dl / c
  121. *
  122. * The initial taps (T_(i=0)^N) are then specified by taking a power series
  123. * that ranges between r_0 and half of r_1 less r_0:
  124. *
  125. * R_i = 2^(i / (2 N - 1)) r_d
  126. * = r_0 + (2^(i / (2 N - 1)) - 1) r_d
  127. * = r_0 + T_i
  128. * T_i = R_i - r_0
  129. * = (2^(i / (2 N - 1)) - 1) r_d
  130. *
  131. * Assuming an average of 1m, we get the following taps:
  132. */
  133. constexpr std::array<ALfloat,NUM_LINES> EARLY_TAP_LENGTHS{{
  134. 0.0000000e+0f, 2.0213520e-4f, 4.2531060e-4f, 6.7171600e-4f
  135. }};
  136. /* The early all-pass filter lengths are based on the early tap lengths:
  137. *
  138. * A_i = R_i / a
  139. *
  140. * Where a is the approximate maximum all-pass cycle limit (20).
  141. */
  142. constexpr std::array<ALfloat,NUM_LINES> EARLY_ALLPASS_LENGTHS{{
  143. 9.7096800e-5f, 1.0720356e-4f, 1.1836234e-4f, 1.3068260e-4f
  144. }};
  145. /* The early delay lines are used to transform the primary reflections into
  146. * the secondary reflections. The A-format is arranged in such a way that
  147. * the channels/lines are spatially opposite:
  148. *
  149. * C_i is opposite C_(N-i-1)
  150. *
  151. * The delays of the two opposing reflections (R_i and O_i) from a source
  152. * anywhere along a particular dimension always sum to twice its full delay:
  153. *
  154. * 2 r_a = R_i + O_i
  155. *
  156. * With that in mind we can determine the delay between the two reflections
  157. * and thus specify our early line lengths (L_(i=0)^N) using:
  158. *
  159. * O_i = 2 r_a - R_(N-i-1)
  160. * L_i = O_i - R_(N-i-1)
  161. * = 2 (r_a - R_(N-i-1))
  162. * = 2 (r_a - T_(N-i-1) - r_0)
  163. * = 2 r_a (1 - (2 / 3) 2^((N - i - 1) / (2 N - 1)))
  164. *
  165. * Using an average dimension of 1m, we get:
  166. */
  167. constexpr std::array<ALfloat,NUM_LINES> EARLY_LINE_LENGTHS{{
  168. 5.9850400e-4f, 1.0913150e-3f, 1.5376658e-3f, 1.9419362e-3f
  169. }};
  170. /* The late all-pass filter lengths are based on the late line lengths:
  171. *
  172. * A_i = (5 / 3) L_i / r_1
  173. */
  174. constexpr std::array<ALfloat,NUM_LINES> LATE_ALLPASS_LENGTHS{{
  175. 1.6182800e-4f, 2.0389060e-4f, 2.8159360e-4f, 3.2365600e-4f
  176. }};
  177. /* The late lines are used to approximate the decaying cycle of recursive
  178. * late reflections.
  179. *
  180. * Splitting the lines in half, we start with the shortest reflection paths
  181. * (L_(i=0)^(N/2)):
  182. *
  183. * L_i = 2^(i / (N - 1)) r_d
  184. *
  185. * Then for the opposite (longest) reflection paths (L_(i=N/2)^N):
  186. *
  187. * L_i = 2 r_a - L_(i-N/2)
  188. * = 2 r_a - 2^((i - N / 2) / (N - 1)) r_d
  189. *
  190. * For our 1m average room, we get:
  191. */
  192. constexpr std::array<ALfloat,NUM_LINES> LATE_LINE_LENGTHS{{
  193. 1.9419362e-3f, 2.4466860e-3f, 3.3791220e-3f, 3.8838720e-3f
  194. }};
  195. using ReverbUpdateLine = std::array<float,MAX_UPDATE_SAMPLES>;
  196. struct DelayLineI {
  197. /* The delay lines use interleaved samples, with the lengths being powers
  198. * of 2 to allow the use of bit-masking instead of a modulus for wrapping.
  199. */
  200. size_t Mask{0u};
  201. union {
  202. uintptr_t LineOffset{0u};
  203. std::array<float,NUM_LINES> *Line;
  204. };
  205. /* Given the allocated sample buffer, this function updates each delay line
  206. * offset.
  207. */
  208. void realizeLineOffset(std::array<float,NUM_LINES> *sampleBuffer) noexcept
  209. { Line = sampleBuffer + LineOffset; }
  210. /* Calculate the length of a delay line and store its mask and offset. */
  211. ALuint calcLineLength(const ALfloat length, const uintptr_t offset, const ALfloat frequency,
  212. const ALuint extra)
  213. {
  214. /* All line lengths are powers of 2, calculated from their lengths in
  215. * seconds, rounded up.
  216. */
  217. ALuint samples{float2uint(std::ceil(length*frequency))};
  218. samples = NextPowerOf2(samples + extra);
  219. /* All lines share a single sample buffer. */
  220. Mask = samples - 1;
  221. LineOffset = offset;
  222. /* Return the sample count for accumulation. */
  223. return samples;
  224. }
  225. void write(size_t offset, const size_t c, const ALfloat *RESTRICT in, const size_t count) const noexcept
  226. {
  227. ASSUME(count > 0);
  228. for(size_t i{0u};i < count;)
  229. {
  230. offset &= Mask;
  231. size_t td{minz(Mask+1 - offset, count - i)};
  232. do {
  233. Line[offset++][c] = in[i++];
  234. } while(--td);
  235. }
  236. }
  237. };
  238. struct VecAllpass {
  239. DelayLineI Delay;
  240. ALfloat Coeff{0.0f};
  241. size_t Offset[NUM_LINES][2]{};
  242. void processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
  243. const ALfloat xCoeff, const ALfloat yCoeff, ALfloat fadeCount, const ALfloat fadeStep,
  244. const size_t todo);
  245. void processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
  246. const ALfloat xCoeff, const ALfloat yCoeff, const size_t todo);
  247. };
  248. struct T60Filter {
  249. /* Two filters are used to adjust the signal. One to control the low
  250. * frequencies, and one to control the high frequencies.
  251. */
  252. ALfloat MidGain[2]{0.0f, 0.0f};
  253. BiquadFilter HFFilter, LFFilter;
  254. void calcCoeffs(const ALfloat length, const ALfloat lfDecayTime, const ALfloat mfDecayTime,
  255. const ALfloat hfDecayTime, const ALfloat lf0norm, const ALfloat hf0norm);
  256. /* Applies the two T60 damping filter sections. */
  257. void process(ALfloat *samples, const size_t todo)
  258. {
  259. HFFilter.process(samples, samples, todo);
  260. LFFilter.process(samples, samples, todo);
  261. }
  262. };
  263. struct EarlyReflections {
  264. /* A Gerzon vector all-pass filter is used to simulate initial diffusion.
  265. * The spread from this filter also helps smooth out the reverb tail.
  266. */
  267. VecAllpass VecAp;
  268. /* An echo line is used to complete the second half of the early
  269. * reflections.
  270. */
  271. DelayLineI Delay;
  272. size_t Offset[NUM_LINES][2]{};
  273. ALfloat Coeff[NUM_LINES][2]{};
  274. /* The gain for each output channel based on 3D panning. */
  275. ALfloat CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
  276. ALfloat PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
  277. void updateLines(const ALfloat density, const ALfloat diffusion, const ALfloat decayTime,
  278. const ALfloat frequency);
  279. };
  280. struct LateReverb {
  281. /* A recursive delay line is used fill in the reverb tail. */
  282. DelayLineI Delay;
  283. size_t Offset[NUM_LINES][2]{};
  284. /* Attenuation to compensate for the modal density and decay rate of the
  285. * late lines.
  286. */
  287. ALfloat DensityGain[2]{0.0f, 0.0f};
  288. /* T60 decay filters are used to simulate absorption. */
  289. T60Filter T60[NUM_LINES];
  290. /* A Gerzon vector all-pass filter is used to simulate diffusion. */
  291. VecAllpass VecAp;
  292. /* The gain for each output channel based on 3D panning. */
  293. ALfloat CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
  294. ALfloat PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS]{};
  295. void updateLines(const ALfloat density, const ALfloat diffusion, const ALfloat lfDecayTime,
  296. const ALfloat mfDecayTime, const ALfloat hfDecayTime, const ALfloat lf0norm,
  297. const ALfloat hf0norm, const ALfloat frequency);
  298. };
  299. struct ReverbState final : public EffectState {
  300. /* All delay lines are allocated as a single buffer to reduce memory
  301. * fragmentation and management code.
  302. */
  303. al::vector<std::array<float,NUM_LINES>,16> mSampleBuffer;
  304. struct {
  305. /* Calculated parameters which indicate if cross-fading is needed after
  306. * an update.
  307. */
  308. ALfloat Density{AL_EAXREVERB_DEFAULT_DENSITY};
  309. ALfloat Diffusion{AL_EAXREVERB_DEFAULT_DIFFUSION};
  310. ALfloat DecayTime{AL_EAXREVERB_DEFAULT_DECAY_TIME};
  311. ALfloat HFDecayTime{AL_EAXREVERB_DEFAULT_DECAY_HFRATIO * AL_EAXREVERB_DEFAULT_DECAY_TIME};
  312. ALfloat LFDecayTime{AL_EAXREVERB_DEFAULT_DECAY_LFRATIO * AL_EAXREVERB_DEFAULT_DECAY_TIME};
  313. ALfloat HFReference{AL_EAXREVERB_DEFAULT_HFREFERENCE};
  314. ALfloat LFReference{AL_EAXREVERB_DEFAULT_LFREFERENCE};
  315. } mParams;
  316. /* Master effect filters */
  317. struct {
  318. BiquadFilter Lp;
  319. BiquadFilter Hp;
  320. } mFilter[NUM_LINES];
  321. /* Core delay line (early reflections and late reverb tap from this). */
  322. DelayLineI mDelay;
  323. /* Tap points for early reflection delay. */
  324. size_t mEarlyDelayTap[NUM_LINES][2]{};
  325. ALfloat mEarlyDelayCoeff[NUM_LINES][2]{};
  326. /* Tap points for late reverb feed and delay. */
  327. size_t mLateFeedTap{};
  328. size_t mLateDelayTap[NUM_LINES][2]{};
  329. /* Coefficients for the all-pass and line scattering matrices. */
  330. ALfloat mMixX{0.0f};
  331. ALfloat mMixY{0.0f};
  332. EarlyReflections mEarly;
  333. LateReverb mLate;
  334. bool mDoFading{};
  335. /* Maximum number of samples to process at once. */
  336. size_t mMaxUpdate[2]{MAX_UPDATE_SAMPLES, MAX_UPDATE_SAMPLES};
  337. /* The current write offset for all delay lines. */
  338. size_t mOffset{};
  339. /* Temporary storage used when processing. */
  340. union {
  341. alignas(16) FloatBufferLine mTempLine{};
  342. alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mTempSamples;
  343. };
  344. alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mEarlySamples{};
  345. alignas(16) std::array<ReverbUpdateLine,NUM_LINES> mLateSamples{};
  346. using MixOutT = void (ReverbState::*)(const al::span<FloatBufferLine> samplesOut,
  347. const size_t counter, const size_t offset, const size_t todo);
  348. MixOutT mMixOut{&ReverbState::MixOutPlain};
  349. std::array<ALfloat,MAX_AMBI_ORDER+1> mOrderScales{};
  350. std::array<std::array<BandSplitter,NUM_LINES>,2> mAmbiSplitter;
  351. void MixOutPlain(const al::span<FloatBufferLine> samplesOut, const size_t counter,
  352. const size_t offset, const size_t todo)
  353. {
  354. ASSUME(todo > 0);
  355. /* Convert back to B-Format, and mix the results to output. */
  356. const al::span<float> tmpspan{mTempLine.data(), todo};
  357. for(size_t c{0u};c < NUM_LINES;c++)
  358. {
  359. std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
  360. MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mEarlySamples[0].data(),
  361. mEarlySamples[0].size());
  362. MixSamples(tmpspan, samplesOut, mEarly.CurrentGain[c], mEarly.PanGain[c], counter,
  363. offset);
  364. }
  365. for(size_t c{0u};c < NUM_LINES;c++)
  366. {
  367. std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
  368. MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mLateSamples[0].data(),
  369. mLateSamples[0].size());
  370. MixSamples(tmpspan, samplesOut, mLate.CurrentGain[c], mLate.PanGain[c], counter,
  371. offset);
  372. }
  373. }
  374. void MixOutAmbiUp(const al::span<FloatBufferLine> samplesOut, const size_t counter,
  375. const size_t offset, const size_t todo)
  376. {
  377. ASSUME(todo > 0);
  378. const al::span<float> tmpspan{mTempLine.data(), todo};
  379. for(size_t c{0u};c < NUM_LINES;c++)
  380. {
  381. std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
  382. MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mEarlySamples[0].data(),
  383. mEarlySamples[0].size());
  384. /* Apply scaling to the B-Format's HF response to "upsample" it to
  385. * higher-order output.
  386. */
  387. const ALfloat hfscale{(c==0) ? mOrderScales[0] : mOrderScales[1]};
  388. mAmbiSplitter[0][c].applyHfScale(tmpspan.data(), hfscale, todo);
  389. MixSamples(tmpspan, samplesOut, mEarly.CurrentGain[c], mEarly.PanGain[c], counter,
  390. offset);
  391. }
  392. for(size_t c{0u};c < NUM_LINES;c++)
  393. {
  394. std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
  395. MixRowSamples(tmpspan, {A2B[c], NUM_LINES}, mLateSamples[0].data(),
  396. mLateSamples[0].size());
  397. const ALfloat hfscale{(c==0) ? mOrderScales[0] : mOrderScales[1]};
  398. mAmbiSplitter[1][c].applyHfScale(tmpspan.data(), hfscale, todo);
  399. MixSamples(tmpspan, samplesOut, mLate.CurrentGain[c], mLate.PanGain[c], counter,
  400. offset);
  401. }
  402. }
  403. bool allocLines(const ALfloat frequency);
  404. void updateDelayLine(const ALfloat earlyDelay, const ALfloat lateDelay, const ALfloat density,
  405. const ALfloat decayTime, const ALfloat frequency);
  406. void update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan,
  407. const ALfloat earlyGain, const ALfloat lateGain, const EffectTarget &target);
  408. void earlyUnfaded(const size_t offset, const size_t todo);
  409. void earlyFaded(const size_t offset, const size_t todo, const ALfloat fade,
  410. const ALfloat fadeStep);
  411. void lateUnfaded(const size_t offset, const size_t todo);
  412. void lateFaded(const size_t offset, const size_t todo, const ALfloat fade,
  413. const ALfloat fadeStep);
  414. ALboolean deviceUpdate(const ALCdevice *device) override;
  415. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  416. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
  417. DEF_NEWDEL(ReverbState)
  418. };
  419. /**************************************
  420. * Device Update *
  421. **************************************/
  422. inline ALfloat CalcDelayLengthMult(ALfloat density)
  423. { return maxf(5.0f, std::cbrt(density*DENSITY_SCALE)); }
  424. /* Calculates the delay line metrics and allocates the shared sample buffer
  425. * for all lines given the sample rate (frequency). If an allocation failure
  426. * occurs, it returns AL_FALSE.
  427. */
  428. bool ReverbState::allocLines(const ALfloat frequency)
  429. {
  430. /* All delay line lengths are calculated to accomodate the full range of
  431. * lengths given their respective paramters.
  432. */
  433. size_t totalSamples{0u};
  434. /* Multiplier for the maximum density value, i.e. density=1, which is
  435. * actually the least density...
  436. */
  437. ALfloat multiplier{CalcDelayLengthMult(AL_EAXREVERB_MAX_DENSITY)};
  438. /* The main delay length includes the maximum early reflection delay, the
  439. * largest early tap width, the maximum late reverb delay, and the
  440. * largest late tap width. Finally, it must also be extended by the
  441. * update size (BUFFERSIZE) for block processing.
  442. */
  443. ALfloat length{AL_EAXREVERB_MAX_REFLECTIONS_DELAY + EARLY_TAP_LENGTHS.back()*multiplier +
  444. AL_EAXREVERB_MAX_LATE_REVERB_DELAY +
  445. (LATE_LINE_LENGTHS.back() - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*multiplier};
  446. totalSamples += mDelay.calcLineLength(length, totalSamples, frequency, BUFFERSIZE);
  447. /* The early vector all-pass line. */
  448. length = EARLY_ALLPASS_LENGTHS.back() * multiplier;
  449. totalSamples += mEarly.VecAp.Delay.calcLineLength(length, totalSamples, frequency, 0);
  450. /* The early reflection line. */
  451. length = EARLY_LINE_LENGTHS.back() * multiplier;
  452. totalSamples += mEarly.Delay.calcLineLength(length, totalSamples, frequency, 0);
  453. /* The late vector all-pass line. */
  454. length = LATE_ALLPASS_LENGTHS.back() * multiplier;
  455. totalSamples += mLate.VecAp.Delay.calcLineLength(length, totalSamples, frequency, 0);
  456. /* The late delay lines are calculated from the largest maximum density
  457. * line length.
  458. */
  459. length = LATE_LINE_LENGTHS.back() * multiplier;
  460. totalSamples += mLate.Delay.calcLineLength(length, totalSamples, frequency, 0);
  461. if(totalSamples != mSampleBuffer.size())
  462. {
  463. mSampleBuffer.resize(totalSamples);
  464. mSampleBuffer.shrink_to_fit();
  465. }
  466. /* Clear the sample buffer. */
  467. std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), std::array<float,NUM_LINES>{});
  468. /* Update all delays to reflect the new sample buffer. */
  469. mDelay.realizeLineOffset(mSampleBuffer.data());
  470. mEarly.VecAp.Delay.realizeLineOffset(mSampleBuffer.data());
  471. mEarly.Delay.realizeLineOffset(mSampleBuffer.data());
  472. mLate.VecAp.Delay.realizeLineOffset(mSampleBuffer.data());
  473. mLate.Delay.realizeLineOffset(mSampleBuffer.data());
  474. return true;
  475. }
  476. ALboolean ReverbState::deviceUpdate(const ALCdevice *device)
  477. {
  478. const auto frequency = static_cast<ALfloat>(device->Frequency);
  479. /* Allocate the delay lines. */
  480. if(!allocLines(frequency))
  481. return AL_FALSE;
  482. const ALfloat multiplier{CalcDelayLengthMult(AL_EAXREVERB_MAX_DENSITY)};
  483. /* The late feed taps are set a fixed position past the latest delay tap. */
  484. mLateFeedTap = float2uint(
  485. (AL_EAXREVERB_MAX_REFLECTIONS_DELAY + EARLY_TAP_LENGTHS.back()*multiplier) * frequency);
  486. /* Clear filters and gain coefficients since the delay lines were all just
  487. * cleared (if not reallocated).
  488. */
  489. for(auto &filter : mFilter)
  490. {
  491. filter.Lp.clear();
  492. filter.Hp.clear();
  493. }
  494. for(auto &coeff : mEarlyDelayCoeff)
  495. std::fill(std::begin(coeff), std::end(coeff), 0.0f);
  496. for(auto &coeff : mEarly.Coeff)
  497. std::fill(std::begin(coeff), std::end(coeff), 0.0f);
  498. mLate.DensityGain[0] = 0.0f;
  499. mLate.DensityGain[1] = 0.0f;
  500. for(auto &t60 : mLate.T60)
  501. {
  502. t60.MidGain[0] = 0.0f;
  503. t60.MidGain[1] = 0.0f;
  504. t60.HFFilter.clear();
  505. t60.LFFilter.clear();
  506. }
  507. for(auto &gains : mEarly.CurrentGain)
  508. std::fill(std::begin(gains), std::end(gains), 0.0f);
  509. for(auto &gains : mEarly.PanGain)
  510. std::fill(std::begin(gains), std::end(gains), 0.0f);
  511. for(auto &gains : mLate.CurrentGain)
  512. std::fill(std::begin(gains), std::end(gains), 0.0f);
  513. for(auto &gains : mLate.PanGain)
  514. std::fill(std::begin(gains), std::end(gains), 0.0f);
  515. /* Reset fading and offset base. */
  516. mDoFading = true;
  517. std::fill(std::begin(mMaxUpdate), std::end(mMaxUpdate), MAX_UPDATE_SAMPLES);
  518. mOffset = 0;
  519. if(device->mAmbiOrder > 1)
  520. {
  521. mMixOut = &ReverbState::MixOutAmbiUp;
  522. mOrderScales = BFormatDec::GetHFOrderScales(1, device->mAmbiOrder);
  523. }
  524. else
  525. {
  526. mMixOut = &ReverbState::MixOutPlain;
  527. mOrderScales.fill(1.0f);
  528. }
  529. mAmbiSplitter[0][0].init(400.0f / frequency);
  530. std::fill(mAmbiSplitter[0].begin()+1, mAmbiSplitter[0].end(), mAmbiSplitter[0][0]);
  531. std::fill(mAmbiSplitter[1].begin(), mAmbiSplitter[1].end(), mAmbiSplitter[0][0]);
  532. return AL_TRUE;
  533. }
  534. /**************************************
  535. * Effect Update *
  536. **************************************/
  537. /* Calculate a decay coefficient given the length of each cycle and the time
  538. * until the decay reaches -60 dB.
  539. */
  540. inline ALfloat CalcDecayCoeff(const ALfloat length, const ALfloat decayTime)
  541. { return std::pow(REVERB_DECAY_GAIN, length/decayTime); }
  542. /* Calculate a decay length from a coefficient and the time until the decay
  543. * reaches -60 dB.
  544. */
  545. inline ALfloat CalcDecayLength(const ALfloat coeff, const ALfloat decayTime)
  546. { return std::log10(coeff) * decayTime / std::log10(REVERB_DECAY_GAIN); }
  547. /* Calculate an attenuation to be applied to the input of any echo models to
  548. * compensate for modal density and decay time.
  549. */
  550. inline ALfloat CalcDensityGain(const ALfloat a)
  551. {
  552. /* The energy of a signal can be obtained by finding the area under the
  553. * squared signal. This takes the form of Sum(x_n^2), where x is the
  554. * amplitude for the sample n.
  555. *
  556. * Decaying feedback matches exponential decay of the form Sum(a^n),
  557. * where a is the attenuation coefficient, and n is the sample. The area
  558. * under this decay curve can be calculated as: 1 / (1 - a).
  559. *
  560. * Modifying the above equation to find the area under the squared curve
  561. * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
  562. * calculated by inverting the square root of this approximation,
  563. * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
  564. */
  565. return std::sqrt(1.0f - a*a);
  566. }
  567. /* Calculate the scattering matrix coefficients given a diffusion factor. */
  568. inline ALvoid CalcMatrixCoeffs(const ALfloat diffusion, ALfloat *x, ALfloat *y)
  569. {
  570. /* The matrix is of order 4, so n is sqrt(4 - 1). */
  571. ALfloat n{std::sqrt(3.0f)};
  572. ALfloat t{diffusion * std::atan(n)};
  573. /* Calculate the first mixing matrix coefficient. */
  574. *x = std::cos(t);
  575. /* Calculate the second mixing matrix coefficient. */
  576. *y = std::sin(t) / n;
  577. }
  578. /* Calculate the limited HF ratio for use with the late reverb low-pass
  579. * filters.
  580. */
  581. ALfloat CalcLimitedHfRatio(const ALfloat hfRatio, const ALfloat airAbsorptionGainHF,
  582. const ALfloat decayTime)
  583. {
  584. /* Find the attenuation due to air absorption in dB (converting delay
  585. * time to meters using the speed of sound). Then reversing the decay
  586. * equation, solve for HF ratio. The delay length is cancelled out of
  587. * the equation, so it can be calculated once for all lines.
  588. */
  589. ALfloat limitRatio{1.0f /
  590. (CalcDecayLength(airAbsorptionGainHF, decayTime) * SPEEDOFSOUNDMETRESPERSEC)};
  591. /* Using the limit calculated above, apply the upper bound to the HF ratio.
  592. */
  593. return minf(limitRatio, hfRatio);
  594. }
  595. /* Calculates the 3-band T60 damping coefficients for a particular delay line
  596. * of specified length, using a combination of two shelf filter sections given
  597. * decay times for each band split at two reference frequencies.
  598. */
  599. void T60Filter::calcCoeffs(const ALfloat length, const ALfloat lfDecayTime,
  600. const ALfloat mfDecayTime, const ALfloat hfDecayTime, const ALfloat lf0norm,
  601. const ALfloat hf0norm)
  602. {
  603. const ALfloat mfGain{CalcDecayCoeff(length, mfDecayTime)};
  604. const ALfloat lfGain{maxf(CalcDecayCoeff(length, lfDecayTime)/mfGain, 0.001f)};
  605. const ALfloat hfGain{maxf(CalcDecayCoeff(length, hfDecayTime)/mfGain, 0.001f)};
  606. MidGain[1] = mfGain;
  607. LFFilter.setParams(BiquadType::LowShelf, lfGain, lf0norm,
  608. LFFilter.rcpQFromSlope(lfGain, 1.0f));
  609. HFFilter.setParams(BiquadType::HighShelf, hfGain, hf0norm,
  610. HFFilter.rcpQFromSlope(hfGain, 1.0f));
  611. }
  612. /* Update the early reflection line lengths and gain coefficients. */
  613. void EarlyReflections::updateLines(const ALfloat density, const ALfloat diffusion,
  614. const ALfloat decayTime, const ALfloat frequency)
  615. {
  616. const ALfloat multiplier{CalcDelayLengthMult(density)};
  617. /* Calculate the all-pass feed-back/forward coefficient. */
  618. VecAp.Coeff = std::sqrt(0.5f) * std::pow(diffusion, 2.0f);
  619. for(size_t i{0u};i < NUM_LINES;i++)
  620. {
  621. /* Calculate the length (in seconds) of each all-pass line. */
  622. ALfloat length{EARLY_ALLPASS_LENGTHS[i] * multiplier};
  623. /* Calculate the delay offset for each all-pass line. */
  624. VecAp.Offset[i][1] = float2uint(length * frequency);
  625. /* Calculate the length (in seconds) of each delay line. */
  626. length = EARLY_LINE_LENGTHS[i] * multiplier;
  627. /* Calculate the delay offset for each delay line. */
  628. Offset[i][1] = float2uint(length * frequency);
  629. /* Calculate the gain (coefficient) for each line. */
  630. Coeff[i][1] = CalcDecayCoeff(length, decayTime);
  631. }
  632. }
  633. /* Update the late reverb line lengths and T60 coefficients. */
  634. void LateReverb::updateLines(const ALfloat density, const ALfloat diffusion,
  635. const ALfloat lfDecayTime, const ALfloat mfDecayTime, const ALfloat hfDecayTime,
  636. const ALfloat lf0norm, const ALfloat hf0norm, const ALfloat frequency)
  637. {
  638. /* Scaling factor to convert the normalized reference frequencies from
  639. * representing 0...freq to 0...max_reference.
  640. */
  641. const ALfloat norm_weight_factor{frequency / AL_EAXREVERB_MAX_HFREFERENCE};
  642. const ALfloat late_allpass_avg{
  643. std::accumulate(LATE_ALLPASS_LENGTHS.begin(), LATE_ALLPASS_LENGTHS.end(), 0.0f) /
  644. float{NUM_LINES}};
  645. /* To compensate for changes in modal density and decay time of the late
  646. * reverb signal, the input is attenuated based on the maximal energy of
  647. * the outgoing signal. This approximation is used to keep the apparent
  648. * energy of the signal equal for all ranges of density and decay time.
  649. *
  650. * The average length of the delay lines is used to calculate the
  651. * attenuation coefficient.
  652. */
  653. const ALfloat multiplier{CalcDelayLengthMult(density)};
  654. ALfloat length{std::accumulate(LATE_LINE_LENGTHS.begin(), LATE_LINE_LENGTHS.end(), 0.0f) /
  655. float{NUM_LINES} * multiplier};
  656. length += late_allpass_avg * multiplier;
  657. /* The density gain calculation uses an average decay time weighted by
  658. * approximate bandwidth. This attempts to compensate for losses of energy
  659. * that reduce decay time due to scattering into highly attenuated bands.
  660. */
  661. const ALfloat decayTimeWeighted{
  662. (lf0norm*norm_weight_factor)*lfDecayTime +
  663. (hf0norm*norm_weight_factor - lf0norm*norm_weight_factor)*mfDecayTime +
  664. (1.0f - hf0norm*norm_weight_factor)*hfDecayTime};
  665. DensityGain[1] = CalcDensityGain(CalcDecayCoeff(length, decayTimeWeighted));
  666. /* Calculate the all-pass feed-back/forward coefficient. */
  667. VecAp.Coeff = std::sqrt(0.5f) * std::pow(diffusion, 2.0f);
  668. for(size_t i{0u};i < NUM_LINES;i++)
  669. {
  670. /* Calculate the length (in seconds) of each all-pass line. */
  671. length = LATE_ALLPASS_LENGTHS[i] * multiplier;
  672. /* Calculate the delay offset for each all-pass line. */
  673. VecAp.Offset[i][1] = float2uint(length * frequency);
  674. /* Calculate the length (in seconds) of each delay line. */
  675. length = LATE_LINE_LENGTHS[i] * multiplier;
  676. /* Calculate the delay offset for each delay line. */
  677. Offset[i][1] = float2uint(length*frequency + 0.5f);
  678. /* Approximate the absorption that the vector all-pass would exhibit
  679. * given the current diffusion so we don't have to process a full T60
  680. * filter for each of its four lines.
  681. */
  682. length += lerp(LATE_ALLPASS_LENGTHS[i], late_allpass_avg, diffusion) * multiplier;
  683. /* Calculate the T60 damping coefficients for each line. */
  684. T60[i].calcCoeffs(length, lfDecayTime, mfDecayTime, hfDecayTime, lf0norm, hf0norm);
  685. }
  686. }
  687. /* Update the offsets for the main effect delay line. */
  688. void ReverbState::updateDelayLine(const ALfloat earlyDelay, const ALfloat lateDelay,
  689. const ALfloat density, const ALfloat decayTime, const ALfloat frequency)
  690. {
  691. const ALfloat multiplier{CalcDelayLengthMult(density)};
  692. /* Early reflection taps are decorrelated by means of an average room
  693. * reflection approximation described above the definition of the taps.
  694. * This approximation is linear and so the above density multiplier can
  695. * be applied to adjust the width of the taps. A single-band decay
  696. * coefficient is applied to simulate initial attenuation and absorption.
  697. *
  698. * Late reverb taps are based on the late line lengths to allow a zero-
  699. * delay path and offsets that would continue the propagation naturally
  700. * into the late lines.
  701. */
  702. for(size_t i{0u};i < NUM_LINES;i++)
  703. {
  704. ALfloat length{earlyDelay + EARLY_TAP_LENGTHS[i]*multiplier};
  705. mEarlyDelayTap[i][1] = float2uint(length * frequency);
  706. length = EARLY_TAP_LENGTHS[i]*multiplier;
  707. mEarlyDelayCoeff[i][1] = CalcDecayCoeff(length, decayTime);
  708. length = (LATE_LINE_LENGTHS[i] - LATE_LINE_LENGTHS.front())/float{NUM_LINES}*multiplier +
  709. lateDelay;
  710. mLateDelayTap[i][1] = mLateFeedTap + float2uint(length * frequency);
  711. }
  712. }
  713. /* Creates a transform matrix given a reverb vector. The vector pans the reverb
  714. * reflections toward the given direction, using its magnitude (up to 1) as a
  715. * focal strength. This function results in a B-Format transformation matrix
  716. * that spatially focuses the signal in the desired direction.
  717. */
  718. alu::Matrix GetTransformFromVector(const ALfloat *vec)
  719. {
  720. constexpr float sqrt_3{1.73205080756887719318f};
  721. /* Normalize the panning vector according to the N3D scale, which has an
  722. * extra sqrt(3) term on the directional components. Converting from OpenAL
  723. * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however
  724. * that the reverb panning vectors use left-handed coordinates, unlike the
  725. * rest of OpenAL which use right-handed. This is fixed by negating Z,
  726. * which cancels out with the B-Format Z negation.
  727. */
  728. ALfloat norm[3];
  729. ALfloat mag{std::sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2])};
  730. if(mag > 1.0f)
  731. {
  732. norm[0] = vec[0] / mag * -sqrt_3;
  733. norm[1] = vec[1] / mag * sqrt_3;
  734. norm[2] = vec[2] / mag * sqrt_3;
  735. mag = 1.0f;
  736. }
  737. else
  738. {
  739. /* If the magnitude is less than or equal to 1, just apply the sqrt(3)
  740. * term. There's no need to renormalize the magnitude since it would
  741. * just be reapplied in the matrix.
  742. */
  743. norm[0] = vec[0] * -sqrt_3;
  744. norm[1] = vec[1] * sqrt_3;
  745. norm[2] = vec[2] * sqrt_3;
  746. }
  747. return alu::Matrix{
  748. 1.0f, 0.0f, 0.0f, 0.0f,
  749. norm[0], 1.0f-mag, 0.0f, 0.0f,
  750. norm[1], 0.0f, 1.0f-mag, 0.0f,
  751. norm[2], 0.0f, 0.0f, 1.0f-mag
  752. };
  753. }
  754. /* Update the early and late 3D panning gains. */
  755. void ReverbState::update3DPanning(const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan,
  756. const ALfloat earlyGain, const ALfloat lateGain, const EffectTarget &target)
  757. {
  758. /* Create matrices that transform a B-Format signal according to the
  759. * panning vectors.
  760. */
  761. const alu::Matrix earlymat{GetTransformFromVector(ReflectionsPan)};
  762. const alu::Matrix latemat{GetTransformFromVector(LateReverbPan)};
  763. mOutTarget = target.Main->Buffer;
  764. for(size_t i{0u};i < NUM_LINES;i++)
  765. {
  766. const ALfloat coeffs[MAX_AMBI_CHANNELS]{earlymat[0][i], earlymat[1][i], earlymat[2][i],
  767. earlymat[3][i]};
  768. ComputePanGains(target.Main, coeffs, earlyGain, mEarly.PanGain[i]);
  769. }
  770. for(size_t i{0u};i < NUM_LINES;i++)
  771. {
  772. const ALfloat coeffs[MAX_AMBI_CHANNELS]{latemat[0][i], latemat[1][i], latemat[2][i],
  773. latemat[3][i]};
  774. ComputePanGains(target.Main, coeffs, lateGain, mLate.PanGain[i]);
  775. }
  776. }
  777. void ReverbState::update(const ALCcontext *Context, const ALeffectslot *Slot, const EffectProps *props, const EffectTarget target)
  778. {
  779. const ALCdevice *Device{Context->mDevice.get()};
  780. const auto frequency = static_cast<ALfloat>(Device->Frequency);
  781. /* Calculate the master filters */
  782. ALfloat hf0norm{minf(props->Reverb.HFReference / frequency, 0.49f)};
  783. /* Restrict the filter gains from going below -60dB to keep the filter from
  784. * killing most of the signal.
  785. */
  786. ALfloat gainhf{maxf(props->Reverb.GainHF, 0.001f)};
  787. mFilter[0].Lp.setParams(BiquadType::HighShelf, gainhf, hf0norm,
  788. mFilter[0].Lp.rcpQFromSlope(gainhf, 1.0f));
  789. ALfloat lf0norm{minf(props->Reverb.LFReference / frequency, 0.49f)};
  790. ALfloat gainlf{maxf(props->Reverb.GainLF, 0.001f)};
  791. mFilter[0].Hp.setParams(BiquadType::LowShelf, gainlf, lf0norm,
  792. mFilter[0].Hp.rcpQFromSlope(gainlf, 1.0f));
  793. for(size_t i{1u};i < NUM_LINES;i++)
  794. {
  795. mFilter[i].Lp.copyParamsFrom(mFilter[0].Lp);
  796. mFilter[i].Hp.copyParamsFrom(mFilter[0].Hp);
  797. }
  798. /* Update the main effect delay and associated taps. */
  799. updateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
  800. props->Reverb.Density, props->Reverb.DecayTime, frequency);
  801. /* Update the early lines. */
  802. mEarly.updateLines(props->Reverb.Density, props->Reverb.Diffusion, props->Reverb.DecayTime,
  803. frequency);
  804. /* Get the mixing matrix coefficients. */
  805. CalcMatrixCoeffs(props->Reverb.Diffusion, &mMixX, &mMixY);
  806. /* If the HF limit parameter is flagged, calculate an appropriate limit
  807. * based on the air absorption parameter.
  808. */
  809. ALfloat hfRatio{props->Reverb.DecayHFRatio};
  810. if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
  811. hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
  812. props->Reverb.DecayTime);
  813. /* Calculate the LF/HF decay times. */
  814. const ALfloat lfDecayTime{clampf(props->Reverb.DecayTime * props->Reverb.DecayLFRatio,
  815. AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME)};
  816. const ALfloat hfDecayTime{clampf(props->Reverb.DecayTime * hfRatio,
  817. AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME)};
  818. /* Update the late lines. */
  819. mLate.updateLines(props->Reverb.Density, props->Reverb.Diffusion, lfDecayTime,
  820. props->Reverb.DecayTime, hfDecayTime, lf0norm, hf0norm, frequency);
  821. /* Update early and late 3D panning. */
  822. const ALfloat gain{props->Reverb.Gain * Slot->Params.Gain * ReverbBoost};
  823. update3DPanning(props->Reverb.ReflectionsPan, props->Reverb.LateReverbPan,
  824. props->Reverb.ReflectionsGain*gain, props->Reverb.LateReverbGain*gain, target);
  825. /* Calculate the max update size from the smallest relevant delay. */
  826. mMaxUpdate[1] = minz(MAX_UPDATE_SAMPLES, minz(mEarly.Offset[0][1], mLate.Offset[0][1]));
  827. /* Determine if delay-line cross-fading is required. Density is essentially
  828. * a master control for the feedback delays, so changes the offsets of many
  829. * delay lines.
  830. */
  831. mDoFading |= (mParams.Density != props->Reverb.Density ||
  832. /* Diffusion and decay times influences the decay rate (gain) of the
  833. * late reverb T60 filter.
  834. */
  835. mParams.Diffusion != props->Reverb.Diffusion ||
  836. mParams.DecayTime != props->Reverb.DecayTime ||
  837. mParams.HFDecayTime != hfDecayTime ||
  838. mParams.LFDecayTime != lfDecayTime ||
  839. /* HF/LF References control the weighting used to calculate the density
  840. * gain.
  841. */
  842. mParams.HFReference != props->Reverb.HFReference ||
  843. mParams.LFReference != props->Reverb.LFReference);
  844. if(mDoFading)
  845. {
  846. mParams.Density = props->Reverb.Density;
  847. mParams.Diffusion = props->Reverb.Diffusion;
  848. mParams.DecayTime = props->Reverb.DecayTime;
  849. mParams.HFDecayTime = hfDecayTime;
  850. mParams.LFDecayTime = lfDecayTime;
  851. mParams.HFReference = props->Reverb.HFReference;
  852. mParams.LFReference = props->Reverb.LFReference;
  853. }
  854. }
  855. /**************************************
  856. * Effect Processing *
  857. **************************************/
  858. /* Applies a scattering matrix to the 4-line (vector) input. This is used
  859. * for both the below vector all-pass model and to perform modal feed-back
  860. * delay network (FDN) mixing.
  861. *
  862. * The matrix is derived from a skew-symmetric matrix to form a 4D rotation
  863. * matrix with a single unitary rotational parameter:
  864. *
  865. * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
  866. * [ -a, d, c, -b ]
  867. * [ -b, -c, d, a ]
  868. * [ -c, b, -a, d ]
  869. *
  870. * The rotation is constructed from the effect's diffusion parameter,
  871. * yielding:
  872. *
  873. * 1 = x^2 + 3 y^2
  874. *
  875. * Where a, b, and c are the coefficient y with differing signs, and d is the
  876. * coefficient x. The final matrix is thus:
  877. *
  878. * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
  879. * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
  880. * [ y, -y, x, y ] x = cos(t)
  881. * [ -y, -y, -y, x ] y = sin(t) / n
  882. *
  883. * Any square orthogonal matrix with an order that is a power of two will
  884. * work (where ^T is transpose, ^-1 is inverse):
  885. *
  886. * M^T = M^-1
  887. *
  888. * Using that knowledge, finding an appropriate matrix can be accomplished
  889. * naively by searching all combinations of:
  890. *
  891. * M = D + S - S^T
  892. *
  893. * Where D is a diagonal matrix (of x), and S is a triangular matrix (of y)
  894. * whose combination of signs are being iterated.
  895. */
  896. inline auto VectorPartialScatter(const std::array<float,NUM_LINES> &RESTRICT in,
  897. const ALfloat xCoeff, const ALfloat yCoeff) -> std::array<float,NUM_LINES>
  898. {
  899. std::array<float,NUM_LINES> out;
  900. out[0] = xCoeff*in[0] + yCoeff*( in[1] + -in[2] + in[3]);
  901. out[1] = xCoeff*in[1] + yCoeff*(-in[0] + in[2] + in[3]);
  902. out[2] = xCoeff*in[2] + yCoeff*( in[0] + -in[1] + in[3]);
  903. out[3] = xCoeff*in[3] + yCoeff*(-in[0] + -in[1] + -in[2] );
  904. return out;
  905. }
  906. /* Utilizes the above, but reverses the input channels. */
  907. void VectorScatterRevDelayIn(const DelayLineI delay, size_t offset, const ALfloat xCoeff,
  908. const ALfloat yCoeff, const al::span<const ReverbUpdateLine,NUM_LINES> in, const size_t count)
  909. {
  910. ASSUME(count > 0);
  911. for(size_t i{0u};i < count;)
  912. {
  913. offset &= delay.Mask;
  914. size_t td{minz(delay.Mask+1 - offset, count-i)};
  915. do {
  916. std::array<float,NUM_LINES> f;
  917. for(size_t j{0u};j < NUM_LINES;j++)
  918. f[NUM_LINES-1-j] = in[j][i];
  919. ++i;
  920. delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
  921. } while(--td);
  922. }
  923. }
  924. /* This applies a Gerzon multiple-in/multiple-out (MIMO) vector all-pass
  925. * filter to the 4-line input.
  926. *
  927. * It works by vectorizing a regular all-pass filter and replacing the delay
  928. * element with a scattering matrix (like the one above) and a diagonal
  929. * matrix of delay elements.
  930. *
  931. * Two static specializations are used for transitional (cross-faded) delay
  932. * line processing and non-transitional processing.
  933. */
  934. void VecAllpass::processUnfaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
  935. const ALfloat xCoeff, const ALfloat yCoeff, const size_t todo)
  936. {
  937. const DelayLineI delay{Delay};
  938. const ALfloat feedCoeff{Coeff};
  939. ASSUME(todo > 0);
  940. size_t vap_offset[NUM_LINES];
  941. for(size_t j{0u};j < NUM_LINES;j++)
  942. vap_offset[j] = offset - Offset[j][0];
  943. for(size_t i{0u};i < todo;)
  944. {
  945. for(size_t j{0u};j < NUM_LINES;j++)
  946. vap_offset[j] &= delay.Mask;
  947. offset &= delay.Mask;
  948. size_t maxoff{offset};
  949. for(size_t j{0u};j < NUM_LINES;j++)
  950. maxoff = maxz(maxoff, vap_offset[j]);
  951. size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
  952. do {
  953. std::array<float,NUM_LINES> f;
  954. for(size_t j{0u};j < NUM_LINES;j++)
  955. {
  956. const ALfloat input{samples[j][i]};
  957. const ALfloat out{delay.Line[vap_offset[j]++][j] - feedCoeff*input};
  958. f[j] = input + feedCoeff*out;
  959. samples[j][i] = out;
  960. }
  961. ++i;
  962. delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
  963. } while(--td);
  964. }
  965. }
  966. void VecAllpass::processFaded(const al::span<ReverbUpdateLine,NUM_LINES> samples, size_t offset,
  967. const ALfloat xCoeff, const ALfloat yCoeff, ALfloat fadeCount, const ALfloat fadeStep,
  968. const size_t todo)
  969. {
  970. const DelayLineI delay{Delay};
  971. const ALfloat feedCoeff{Coeff};
  972. ASSUME(todo > 0);
  973. size_t vap_offset[NUM_LINES][2];
  974. for(size_t j{0u};j < NUM_LINES;j++)
  975. {
  976. vap_offset[j][0] = offset - Offset[j][0];
  977. vap_offset[j][1] = offset - Offset[j][1];
  978. }
  979. for(size_t i{0u};i < todo;)
  980. {
  981. for(size_t j{0u};j < NUM_LINES;j++)
  982. {
  983. vap_offset[j][0] &= delay.Mask;
  984. vap_offset[j][1] &= delay.Mask;
  985. }
  986. offset &= delay.Mask;
  987. size_t maxoff{offset};
  988. for(size_t j{0u};j < NUM_LINES;j++)
  989. maxoff = maxz(maxoff, maxz(vap_offset[j][0], vap_offset[j][1]));
  990. size_t td{minz(delay.Mask+1 - maxoff, todo - i)};
  991. do {
  992. fadeCount += 1.0f;
  993. const float fade{fadeCount * fadeStep};
  994. std::array<float,NUM_LINES> f;
  995. for(size_t j{0u};j < NUM_LINES;j++)
  996. f[j] = delay.Line[vap_offset[j][0]++][j]*(1.0f-fade) +
  997. delay.Line[vap_offset[j][1]++][j]*fade;
  998. for(size_t j{0u};j < NUM_LINES;j++)
  999. {
  1000. const ALfloat input{samples[j][i]};
  1001. const ALfloat out{f[j] - feedCoeff*input};
  1002. f[j] = input + feedCoeff*out;
  1003. samples[j][i] = out;
  1004. }
  1005. ++i;
  1006. delay.Line[offset++] = VectorPartialScatter(f, xCoeff, yCoeff);
  1007. } while(--td);
  1008. }
  1009. }
  1010. /* This generates early reflections.
  1011. *
  1012. * This is done by obtaining the primary reflections (those arriving from the
  1013. * same direction as the source) from the main delay line. These are
  1014. * attenuated and all-pass filtered (based on the diffusion parameter).
  1015. *
  1016. * The early lines are then fed in reverse (according to the approximately
  1017. * opposite spatial location of the A-Format lines) to create the secondary
  1018. * reflections (those arriving from the opposite direction as the source).
  1019. *
  1020. * The early response is then completed by combining the primary reflections
  1021. * with the delayed and attenuated output from the early lines.
  1022. *
  1023. * Finally, the early response is reversed, scattered (based on diffusion),
  1024. * and fed into the late reverb section of the main delay line.
  1025. *
  1026. * Two static specializations are used for transitional (cross-faded) delay
  1027. * line processing and non-transitional processing.
  1028. */
  1029. void ReverbState::earlyUnfaded(const size_t offset, const size_t todo)
  1030. {
  1031. const DelayLineI early_delay{mEarly.Delay};
  1032. const DelayLineI main_delay{mDelay};
  1033. const ALfloat mixX{mMixX};
  1034. const ALfloat mixY{mMixY};
  1035. ASSUME(todo > 0);
  1036. /* First, load decorrelated samples from the main delay line as the primary
  1037. * reflections.
  1038. */
  1039. for(size_t j{0u};j < NUM_LINES;j++)
  1040. {
  1041. size_t early_delay_tap{offset - mEarlyDelayTap[j][0]};
  1042. const ALfloat coeff{mEarlyDelayCoeff[j][0]};
  1043. for(size_t i{0u};i < todo;)
  1044. {
  1045. early_delay_tap &= main_delay.Mask;
  1046. size_t td{minz(main_delay.Mask+1 - early_delay_tap, todo - i)};
  1047. do {
  1048. mTempSamples[j][i++] = main_delay.Line[early_delay_tap++][j] * coeff;
  1049. } while(--td);
  1050. }
  1051. }
  1052. /* Apply a vector all-pass, to help color the initial reflections based on
  1053. * the diffusion strength.
  1054. */
  1055. mEarly.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
  1056. /* Apply a delay and bounce to generate secondary reflections, combine with
  1057. * the primary reflections and write out the result for mixing.
  1058. */
  1059. for(size_t j{0u};j < NUM_LINES;j++)
  1060. {
  1061. size_t feedb_tap{offset - mEarly.Offset[j][0]};
  1062. const ALfloat feedb_coeff{mEarly.Coeff[j][0]};
  1063. float *out = mEarlySamples[j].data();
  1064. for(size_t i{0u};i < todo;)
  1065. {
  1066. feedb_tap &= early_delay.Mask;
  1067. size_t td{minz(early_delay.Mask+1 - feedb_tap, todo - i)};
  1068. do {
  1069. out[i] = mTempSamples[j][i] + early_delay.Line[feedb_tap++][j]*feedb_coeff;
  1070. ++i;
  1071. } while(--td);
  1072. }
  1073. }
  1074. for(size_t j{0u};j < NUM_LINES;j++)
  1075. early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
  1076. /* Also write the result back to the main delay line for the late reverb
  1077. * stage to pick up at the appropriate time, appplying a scatter and
  1078. * bounce to improve the initial diffusion in the late reverb.
  1079. */
  1080. const size_t late_feed_tap{offset - mLateFeedTap};
  1081. VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
  1082. }
  1083. void ReverbState::earlyFaded(const size_t offset, const size_t todo, const ALfloat fade,
  1084. const ALfloat fadeStep)
  1085. {
  1086. const DelayLineI early_delay{mEarly.Delay};
  1087. const DelayLineI main_delay{mDelay};
  1088. const ALfloat mixX{mMixX};
  1089. const ALfloat mixY{mMixY};
  1090. ASSUME(todo > 0);
  1091. for(size_t j{0u};j < NUM_LINES;j++)
  1092. {
  1093. size_t early_delay_tap0{offset - mEarlyDelayTap[j][0]};
  1094. size_t early_delay_tap1{offset - mEarlyDelayTap[j][1]};
  1095. const ALfloat oldCoeff{mEarlyDelayCoeff[j][0]};
  1096. const ALfloat oldCoeffStep{-oldCoeff * fadeStep};
  1097. const ALfloat newCoeffStep{mEarlyDelayCoeff[j][1] * fadeStep};
  1098. ALfloat fadeCount{fade};
  1099. for(size_t i{0u};i < todo;)
  1100. {
  1101. early_delay_tap0 &= main_delay.Mask;
  1102. early_delay_tap1 &= main_delay.Mask;
  1103. size_t td{minz(main_delay.Mask+1 - maxz(early_delay_tap0, early_delay_tap1), todo-i)};
  1104. do {
  1105. fadeCount += 1.0f;
  1106. const ALfloat fade0{oldCoeff + oldCoeffStep*fadeCount};
  1107. const ALfloat fade1{newCoeffStep*fadeCount};
  1108. mTempSamples[j][i++] =
  1109. main_delay.Line[early_delay_tap0++][j]*fade0 +
  1110. main_delay.Line[early_delay_tap1++][j]*fade1;
  1111. } while(--td);
  1112. }
  1113. }
  1114. mEarly.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
  1115. for(size_t j{0u};j < NUM_LINES;j++)
  1116. {
  1117. size_t feedb_tap0{offset - mEarly.Offset[j][0]};
  1118. size_t feedb_tap1{offset - mEarly.Offset[j][1]};
  1119. const ALfloat feedb_oldCoeff{mEarly.Coeff[j][0]};
  1120. const ALfloat feedb_oldCoeffStep{-feedb_oldCoeff * fadeStep};
  1121. const ALfloat feedb_newCoeffStep{mEarly.Coeff[j][1] * fadeStep};
  1122. float *out = mEarlySamples[j].data();
  1123. ALfloat fadeCount{fade};
  1124. for(size_t i{0u};i < todo;)
  1125. {
  1126. feedb_tap0 &= early_delay.Mask;
  1127. feedb_tap1 &= early_delay.Mask;
  1128. size_t td{minz(early_delay.Mask+1 - maxz(feedb_tap0, feedb_tap1), todo - i)};
  1129. do {
  1130. fadeCount += 1.0f;
  1131. const ALfloat fade0{feedb_oldCoeff + feedb_oldCoeffStep*fadeCount};
  1132. const ALfloat fade1{feedb_newCoeffStep*fadeCount};
  1133. out[i] = mTempSamples[j][i] +
  1134. early_delay.Line[feedb_tap0++][j]*fade0 +
  1135. early_delay.Line[feedb_tap1++][j]*fade1;
  1136. ++i;
  1137. } while(--td);
  1138. }
  1139. }
  1140. for(size_t j{0u};j < NUM_LINES;j++)
  1141. early_delay.write(offset, NUM_LINES-1-j, mTempSamples[j].data(), todo);
  1142. const size_t late_feed_tap{offset - mLateFeedTap};
  1143. VectorScatterRevDelayIn(main_delay, late_feed_tap, mixX, mixY, mEarlySamples, todo);
  1144. }
  1145. /* This generates the reverb tail using a modified feed-back delay network
  1146. * (FDN).
  1147. *
  1148. * Results from the early reflections are mixed with the output from the late
  1149. * delay lines.
  1150. *
  1151. * The late response is then completed by T60 and all-pass filtering the mix.
  1152. *
  1153. * Finally, the lines are reversed (so they feed their opposite directions)
  1154. * and scattered with the FDN matrix before re-feeding the delay lines.
  1155. *
  1156. * Two variations are made, one for for transitional (cross-faded) delay line
  1157. * processing and one for non-transitional processing.
  1158. */
  1159. void ReverbState::lateUnfaded(const size_t offset, const size_t todo)
  1160. {
  1161. const DelayLineI late_delay{mLate.Delay};
  1162. const DelayLineI main_delay{mDelay};
  1163. const ALfloat mixX{mMixX};
  1164. const ALfloat mixY{mMixY};
  1165. ASSUME(todo > 0);
  1166. /* First, load decorrelated samples from the main and feedback delay lines.
  1167. * Filter the signal to apply its frequency-dependent decay.
  1168. */
  1169. for(size_t j{0u};j < NUM_LINES;j++)
  1170. {
  1171. size_t late_delay_tap{offset - mLateDelayTap[j][0]};
  1172. size_t late_feedb_tap{offset - mLate.Offset[j][0]};
  1173. const ALfloat midGain{mLate.T60[j].MidGain[0]};
  1174. const ALfloat densityGain{mLate.DensityGain[0] * midGain};
  1175. for(size_t i{0u};i < todo;)
  1176. {
  1177. late_delay_tap &= main_delay.Mask;
  1178. late_feedb_tap &= late_delay.Mask;
  1179. size_t td{minz(todo - i,
  1180. minz(main_delay.Mask+1 - late_delay_tap, late_delay.Mask+1 - late_feedb_tap))};
  1181. do {
  1182. mTempSamples[j][i++] =
  1183. main_delay.Line[late_delay_tap++][j]*densityGain +
  1184. late_delay.Line[late_feedb_tap++][j]*midGain;
  1185. } while(--td);
  1186. }
  1187. mLate.T60[j].process(mTempSamples[j].data(), todo);
  1188. }
  1189. /* Apply a vector all-pass to improve micro-surface diffusion, and write
  1190. * out the results for mixing.
  1191. */
  1192. mLate.VecAp.processUnfaded(mTempSamples, offset, mixX, mixY, todo);
  1193. for(size_t j{0u};j < NUM_LINES;j++)
  1194. std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
  1195. /* Finally, scatter and bounce the results to refeed the feedback buffer. */
  1196. VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
  1197. }
  1198. void ReverbState::lateFaded(const size_t offset, const size_t todo, const ALfloat fade,
  1199. const ALfloat fadeStep)
  1200. {
  1201. const DelayLineI late_delay{mLate.Delay};
  1202. const DelayLineI main_delay{mDelay};
  1203. const ALfloat mixX{mMixX};
  1204. const ALfloat mixY{mMixY};
  1205. ASSUME(todo > 0);
  1206. for(size_t j{0u};j < NUM_LINES;j++)
  1207. {
  1208. const ALfloat oldMidGain{mLate.T60[j].MidGain[0]};
  1209. const ALfloat midGain{mLate.T60[j].MidGain[1]};
  1210. const ALfloat oldMidStep{-oldMidGain * fadeStep};
  1211. const ALfloat midStep{midGain * fadeStep};
  1212. const ALfloat oldDensityGain{mLate.DensityGain[0] * oldMidGain};
  1213. const ALfloat densityGain{mLate.DensityGain[1] * midGain};
  1214. const ALfloat oldDensityStep{-oldDensityGain * fadeStep};
  1215. const ALfloat densityStep{densityGain * fadeStep};
  1216. size_t late_delay_tap0{offset - mLateDelayTap[j][0]};
  1217. size_t late_delay_tap1{offset - mLateDelayTap[j][1]};
  1218. size_t late_feedb_tap0{offset - mLate.Offset[j][0]};
  1219. size_t late_feedb_tap1{offset - mLate.Offset[j][1]};
  1220. ALfloat fadeCount{fade};
  1221. for(size_t i{0u};i < todo;)
  1222. {
  1223. late_delay_tap0 &= main_delay.Mask;
  1224. late_delay_tap1 &= main_delay.Mask;
  1225. late_feedb_tap0 &= late_delay.Mask;
  1226. late_feedb_tap1 &= late_delay.Mask;
  1227. size_t td{minz(todo - i,
  1228. minz(main_delay.Mask+1 - maxz(late_delay_tap0, late_delay_tap1),
  1229. late_delay.Mask+1 - maxz(late_feedb_tap0, late_feedb_tap1)))};
  1230. do {
  1231. fadeCount += 1.0f;
  1232. const ALfloat fade0{oldDensityGain + oldDensityStep*fadeCount};
  1233. const ALfloat fade1{densityStep*fadeCount};
  1234. const ALfloat gfade0{oldMidGain + oldMidStep*fadeCount};
  1235. const ALfloat gfade1{midStep*fadeCount};
  1236. mTempSamples[j][i++] =
  1237. main_delay.Line[late_delay_tap0++][j]*fade0 +
  1238. main_delay.Line[late_delay_tap1++][j]*fade1 +
  1239. late_delay.Line[late_feedb_tap0++][j]*gfade0 +
  1240. late_delay.Line[late_feedb_tap1++][j]*gfade1;
  1241. } while(--td);
  1242. }
  1243. mLate.T60[j].process(mTempSamples[j].data(), todo);
  1244. }
  1245. mLate.VecAp.processFaded(mTempSamples, offset, mixX, mixY, fade, fadeStep, todo);
  1246. for(size_t j{0u};j < NUM_LINES;j++)
  1247. std::copy_n(mTempSamples[j].begin(), todo, mLateSamples[j].begin());
  1248. VectorScatterRevDelayIn(late_delay, offset, mixX, mixY, mTempSamples, todo);
  1249. }
  1250. void ReverbState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  1251. {
  1252. size_t offset{mOffset};
  1253. ASSUME(samplesToDo > 0);
  1254. /* Convert B-Format to A-Format for processing. */
  1255. const size_t numInput{samplesIn.size()};
  1256. const al::span<float> tmpspan{mTempLine.data(), samplesToDo};
  1257. for(size_t c{0u};c < NUM_LINES;c++)
  1258. {
  1259. std::fill(tmpspan.begin(), tmpspan.end(), 0.0f);
  1260. MixRowSamples(tmpspan, {B2A[c], numInput}, samplesIn[0].data(), samplesIn[0].size());
  1261. /* Band-pass the incoming samples and feed the initial delay line. */
  1262. mFilter[c].Lp.process(mTempLine.data(), mTempLine.data(), samplesToDo);
  1263. mFilter[c].Hp.process(mTempLine.data(), mTempLine.data(), samplesToDo);
  1264. mDelay.write(offset, c, mTempLine.data(), samplesToDo);
  1265. }
  1266. /* Process reverb for these samples. */
  1267. if LIKELY(!mDoFading)
  1268. {
  1269. for(size_t base{0};base < samplesToDo;)
  1270. {
  1271. /* Calculate the number of samples we can do this iteration. */
  1272. size_t todo{minz(samplesToDo - base, mMaxUpdate[0])};
  1273. /* Some mixers require maintaining a 4-sample alignment, so ensure
  1274. * that if it's not the last iteration.
  1275. */
  1276. if(base+todo < samplesToDo) todo &= ~size_t{3};
  1277. ASSUME(todo > 0);
  1278. /* Generate non-faded early reflections and late reverb. */
  1279. earlyUnfaded(offset, todo);
  1280. lateUnfaded(offset, todo);
  1281. /* Finally, mix early reflections and late reverb. */
  1282. (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
  1283. offset += todo;
  1284. base += todo;
  1285. }
  1286. }
  1287. else
  1288. {
  1289. const float fadeStep{1.0f / static_cast<float>(samplesToDo)};
  1290. for(size_t base{0};base < samplesToDo;)
  1291. {
  1292. size_t todo{minz(samplesToDo - base, minz(mMaxUpdate[0], mMaxUpdate[1]))};
  1293. if(base+todo < samplesToDo) todo &= ~size_t{3};
  1294. ASSUME(todo > 0);
  1295. /* Generate cross-faded early reflections and late reverb. */
  1296. auto fadeCount = static_cast<ALfloat>(base);
  1297. earlyFaded(offset, todo, fadeCount, fadeStep);
  1298. lateFaded(offset, todo, fadeCount, fadeStep);
  1299. (this->*mMixOut)(samplesOut, samplesToDo-base, base, todo);
  1300. offset += todo;
  1301. base += todo;
  1302. }
  1303. /* Update the cross-fading delay line taps. */
  1304. for(size_t c{0u};c < NUM_LINES;c++)
  1305. {
  1306. mEarlyDelayTap[c][0] = mEarlyDelayTap[c][1];
  1307. mEarlyDelayCoeff[c][0] = mEarlyDelayCoeff[c][1];
  1308. mEarly.VecAp.Offset[c][0] = mEarly.VecAp.Offset[c][1];
  1309. mEarly.Offset[c][0] = mEarly.Offset[c][1];
  1310. mEarly.Coeff[c][0] = mEarly.Coeff[c][1];
  1311. mLateDelayTap[c][0] = mLateDelayTap[c][1];
  1312. mLate.VecAp.Offset[c][0] = mLate.VecAp.Offset[c][1];
  1313. mLate.Offset[c][0] = mLate.Offset[c][1];
  1314. mLate.T60[c].MidGain[0] = mLate.T60[c].MidGain[1];
  1315. }
  1316. mLate.DensityGain[0] = mLate.DensityGain[1];
  1317. mMaxUpdate[0] = mMaxUpdate[1];
  1318. mDoFading = false;
  1319. }
  1320. mOffset = offset;
  1321. }
  1322. void EAXReverb_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
  1323. {
  1324. switch(param)
  1325. {
  1326. case AL_EAXREVERB_DECAY_HFLIMIT:
  1327. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
  1328. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hflimit out of range");
  1329. props->Reverb.DecayHFLimit = val != AL_FALSE;
  1330. break;
  1331. default:
  1332. context->setError(AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
  1333. param);
  1334. }
  1335. }
  1336. void EAXReverb_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
  1337. { EAXReverb_setParami(props, context, param, vals[0]); }
  1338. void EAXReverb_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  1339. {
  1340. switch(param)
  1341. {
  1342. case AL_EAXREVERB_DENSITY:
  1343. if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
  1344. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb density out of range");
  1345. props->Reverb.Density = val;
  1346. break;
  1347. case AL_EAXREVERB_DIFFUSION:
  1348. if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
  1349. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb diffusion out of range");
  1350. props->Reverb.Diffusion = val;
  1351. break;
  1352. case AL_EAXREVERB_GAIN:
  1353. if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
  1354. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gain out of range");
  1355. props->Reverb.Gain = val;
  1356. break;
  1357. case AL_EAXREVERB_GAINHF:
  1358. if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
  1359. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainhf out of range");
  1360. props->Reverb.GainHF = val;
  1361. break;
  1362. case AL_EAXREVERB_GAINLF:
  1363. if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
  1364. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainlf out of range");
  1365. props->Reverb.GainLF = val;
  1366. break;
  1367. case AL_EAXREVERB_DECAY_TIME:
  1368. if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
  1369. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay time out of range");
  1370. props->Reverb.DecayTime = val;
  1371. break;
  1372. case AL_EAXREVERB_DECAY_HFRATIO:
  1373. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
  1374. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hfratio out of range");
  1375. props->Reverb.DecayHFRatio = val;
  1376. break;
  1377. case AL_EAXREVERB_DECAY_LFRATIO:
  1378. if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
  1379. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay lfratio out of range");
  1380. props->Reverb.DecayLFRatio = val;
  1381. break;
  1382. case AL_EAXREVERB_REFLECTIONS_GAIN:
  1383. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
  1384. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections gain out of range");
  1385. props->Reverb.ReflectionsGain = val;
  1386. break;
  1387. case AL_EAXREVERB_REFLECTIONS_DELAY:
  1388. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
  1389. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections delay out of range");
  1390. props->Reverb.ReflectionsDelay = val;
  1391. break;
  1392. case AL_EAXREVERB_LATE_REVERB_GAIN:
  1393. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
  1394. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb gain out of range");
  1395. props->Reverb.LateReverbGain = val;
  1396. break;
  1397. case AL_EAXREVERB_LATE_REVERB_DELAY:
  1398. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
  1399. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb delay out of range");
  1400. props->Reverb.LateReverbDelay = val;
  1401. break;
  1402. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  1403. if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
  1404. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb air absorption gainhf out of range");
  1405. props->Reverb.AirAbsorptionGainHF = val;
  1406. break;
  1407. case AL_EAXREVERB_ECHO_TIME:
  1408. if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
  1409. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo time out of range");
  1410. props->Reverb.EchoTime = val;
  1411. break;
  1412. case AL_EAXREVERB_ECHO_DEPTH:
  1413. if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
  1414. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo depth out of range");
  1415. props->Reverb.EchoDepth = val;
  1416. break;
  1417. case AL_EAXREVERB_MODULATION_TIME:
  1418. if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
  1419. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation time out of range");
  1420. props->Reverb.ModulationTime = val;
  1421. break;
  1422. case AL_EAXREVERB_MODULATION_DEPTH:
  1423. if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
  1424. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation depth out of range");
  1425. props->Reverb.ModulationDepth = val;
  1426. break;
  1427. case AL_EAXREVERB_HFREFERENCE:
  1428. if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
  1429. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb hfreference out of range");
  1430. props->Reverb.HFReference = val;
  1431. break;
  1432. case AL_EAXREVERB_LFREFERENCE:
  1433. if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
  1434. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb lfreference out of range");
  1435. props->Reverb.LFReference = val;
  1436. break;
  1437. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  1438. if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
  1439. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb room rolloff factor out of range");
  1440. props->Reverb.RoomRolloffFactor = val;
  1441. break;
  1442. default:
  1443. context->setError(AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x", param);
  1444. }
  1445. }
  1446. void EAXReverb_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  1447. {
  1448. switch(param)
  1449. {
  1450. case AL_EAXREVERB_REFLECTIONS_PAN:
  1451. if(!(std::isfinite(vals[0]) && std::isfinite(vals[1]) && std::isfinite(vals[2])))
  1452. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections pan out of range");
  1453. props->Reverb.ReflectionsPan[0] = vals[0];
  1454. props->Reverb.ReflectionsPan[1] = vals[1];
  1455. props->Reverb.ReflectionsPan[2] = vals[2];
  1456. break;
  1457. case AL_EAXREVERB_LATE_REVERB_PAN:
  1458. if(!(std::isfinite(vals[0]) && std::isfinite(vals[1]) && std::isfinite(vals[2])))
  1459. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb pan out of range");
  1460. props->Reverb.LateReverbPan[0] = vals[0];
  1461. props->Reverb.LateReverbPan[1] = vals[1];
  1462. props->Reverb.LateReverbPan[2] = vals[2];
  1463. break;
  1464. default:
  1465. EAXReverb_setParamf(props, context, param, vals[0]);
  1466. break;
  1467. }
  1468. }
  1469. void EAXReverb_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
  1470. {
  1471. switch(param)
  1472. {
  1473. case AL_EAXREVERB_DECAY_HFLIMIT:
  1474. *val = props->Reverb.DecayHFLimit;
  1475. break;
  1476. default:
  1477. context->setError(AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
  1478. param);
  1479. }
  1480. }
  1481. void EAXReverb_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
  1482. { EAXReverb_getParami(props, context, param, vals); }
  1483. void EAXReverb_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  1484. {
  1485. switch(param)
  1486. {
  1487. case AL_EAXREVERB_DENSITY:
  1488. *val = props->Reverb.Density;
  1489. break;
  1490. case AL_EAXREVERB_DIFFUSION:
  1491. *val = props->Reverb.Diffusion;
  1492. break;
  1493. case AL_EAXREVERB_GAIN:
  1494. *val = props->Reverb.Gain;
  1495. break;
  1496. case AL_EAXREVERB_GAINHF:
  1497. *val = props->Reverb.GainHF;
  1498. break;
  1499. case AL_EAXREVERB_GAINLF:
  1500. *val = props->Reverb.GainLF;
  1501. break;
  1502. case AL_EAXREVERB_DECAY_TIME:
  1503. *val = props->Reverb.DecayTime;
  1504. break;
  1505. case AL_EAXREVERB_DECAY_HFRATIO:
  1506. *val = props->Reverb.DecayHFRatio;
  1507. break;
  1508. case AL_EAXREVERB_DECAY_LFRATIO:
  1509. *val = props->Reverb.DecayLFRatio;
  1510. break;
  1511. case AL_EAXREVERB_REFLECTIONS_GAIN:
  1512. *val = props->Reverb.ReflectionsGain;
  1513. break;
  1514. case AL_EAXREVERB_REFLECTIONS_DELAY:
  1515. *val = props->Reverb.ReflectionsDelay;
  1516. break;
  1517. case AL_EAXREVERB_LATE_REVERB_GAIN:
  1518. *val = props->Reverb.LateReverbGain;
  1519. break;
  1520. case AL_EAXREVERB_LATE_REVERB_DELAY:
  1521. *val = props->Reverb.LateReverbDelay;
  1522. break;
  1523. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  1524. *val = props->Reverb.AirAbsorptionGainHF;
  1525. break;
  1526. case AL_EAXREVERB_ECHO_TIME:
  1527. *val = props->Reverb.EchoTime;
  1528. break;
  1529. case AL_EAXREVERB_ECHO_DEPTH:
  1530. *val = props->Reverb.EchoDepth;
  1531. break;
  1532. case AL_EAXREVERB_MODULATION_TIME:
  1533. *val = props->Reverb.ModulationTime;
  1534. break;
  1535. case AL_EAXREVERB_MODULATION_DEPTH:
  1536. *val = props->Reverb.ModulationDepth;
  1537. break;
  1538. case AL_EAXREVERB_HFREFERENCE:
  1539. *val = props->Reverb.HFReference;
  1540. break;
  1541. case AL_EAXREVERB_LFREFERENCE:
  1542. *val = props->Reverb.LFReference;
  1543. break;
  1544. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  1545. *val = props->Reverb.RoomRolloffFactor;
  1546. break;
  1547. default:
  1548. context->setError(AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x", param);
  1549. }
  1550. }
  1551. void EAXReverb_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  1552. {
  1553. switch(param)
  1554. {
  1555. case AL_EAXREVERB_REFLECTIONS_PAN:
  1556. vals[0] = props->Reverb.ReflectionsPan[0];
  1557. vals[1] = props->Reverb.ReflectionsPan[1];
  1558. vals[2] = props->Reverb.ReflectionsPan[2];
  1559. break;
  1560. case AL_EAXREVERB_LATE_REVERB_PAN:
  1561. vals[0] = props->Reverb.LateReverbPan[0];
  1562. vals[1] = props->Reverb.LateReverbPan[1];
  1563. vals[2] = props->Reverb.LateReverbPan[2];
  1564. break;
  1565. default:
  1566. EAXReverb_getParamf(props, context, param, vals);
  1567. break;
  1568. }
  1569. }
  1570. DEFINE_ALEFFECT_VTABLE(EAXReverb);
  1571. struct ReverbStateFactory final : public EffectStateFactory {
  1572. EffectState *create() override { return new ReverbState{}; }
  1573. EffectProps getDefaultProps() const noexcept override;
  1574. const EffectVtable *getEffectVtable() const noexcept override { return &EAXReverb_vtable; }
  1575. };
  1576. EffectProps ReverbStateFactory::getDefaultProps() const noexcept
  1577. {
  1578. EffectProps props{};
  1579. props.Reverb.Density = AL_EAXREVERB_DEFAULT_DENSITY;
  1580. props.Reverb.Diffusion = AL_EAXREVERB_DEFAULT_DIFFUSION;
  1581. props.Reverb.Gain = AL_EAXREVERB_DEFAULT_GAIN;
  1582. props.Reverb.GainHF = AL_EAXREVERB_DEFAULT_GAINHF;
  1583. props.Reverb.GainLF = AL_EAXREVERB_DEFAULT_GAINLF;
  1584. props.Reverb.DecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME;
  1585. props.Reverb.DecayHFRatio = AL_EAXREVERB_DEFAULT_DECAY_HFRATIO;
  1586. props.Reverb.DecayLFRatio = AL_EAXREVERB_DEFAULT_DECAY_LFRATIO;
  1587. props.Reverb.ReflectionsGain = AL_EAXREVERB_DEFAULT_REFLECTIONS_GAIN;
  1588. props.Reverb.ReflectionsDelay = AL_EAXREVERB_DEFAULT_REFLECTIONS_DELAY;
  1589. props.Reverb.ReflectionsPan[0] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
  1590. props.Reverb.ReflectionsPan[1] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
  1591. props.Reverb.ReflectionsPan[2] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
  1592. props.Reverb.LateReverbGain = AL_EAXREVERB_DEFAULT_LATE_REVERB_GAIN;
  1593. props.Reverb.LateReverbDelay = AL_EAXREVERB_DEFAULT_LATE_REVERB_DELAY;
  1594. props.Reverb.LateReverbPan[0] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
  1595. props.Reverb.LateReverbPan[1] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
  1596. props.Reverb.LateReverbPan[2] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
  1597. props.Reverb.EchoTime = AL_EAXREVERB_DEFAULT_ECHO_TIME;
  1598. props.Reverb.EchoDepth = AL_EAXREVERB_DEFAULT_ECHO_DEPTH;
  1599. props.Reverb.ModulationTime = AL_EAXREVERB_DEFAULT_MODULATION_TIME;
  1600. props.Reverb.ModulationDepth = AL_EAXREVERB_DEFAULT_MODULATION_DEPTH;
  1601. props.Reverb.AirAbsorptionGainHF = AL_EAXREVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
  1602. props.Reverb.HFReference = AL_EAXREVERB_DEFAULT_HFREFERENCE;
  1603. props.Reverb.LFReference = AL_EAXREVERB_DEFAULT_LFREFERENCE;
  1604. props.Reverb.RoomRolloffFactor = AL_EAXREVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
  1605. props.Reverb.DecayHFLimit = AL_EAXREVERB_DEFAULT_DECAY_HFLIMIT;
  1606. return props;
  1607. }
  1608. void StdReverb_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
  1609. {
  1610. switch(param)
  1611. {
  1612. case AL_REVERB_DECAY_HFLIMIT:
  1613. if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
  1614. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hflimit out of range");
  1615. props->Reverb.DecayHFLimit = val != AL_FALSE;
  1616. break;
  1617. default:
  1618. context->setError(AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
  1619. }
  1620. }
  1621. void StdReverb_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
  1622. { StdReverb_setParami(props, context, param, vals[0]); }
  1623. void StdReverb_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  1624. {
  1625. switch(param)
  1626. {
  1627. case AL_REVERB_DENSITY:
  1628. if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
  1629. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb density out of range");
  1630. props->Reverb.Density = val;
  1631. break;
  1632. case AL_REVERB_DIFFUSION:
  1633. if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
  1634. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb diffusion out of range");
  1635. props->Reverb.Diffusion = val;
  1636. break;
  1637. case AL_REVERB_GAIN:
  1638. if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
  1639. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gain out of range");
  1640. props->Reverb.Gain = val;
  1641. break;
  1642. case AL_REVERB_GAINHF:
  1643. if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
  1644. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gainhf out of range");
  1645. props->Reverb.GainHF = val;
  1646. break;
  1647. case AL_REVERB_DECAY_TIME:
  1648. if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
  1649. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay time out of range");
  1650. props->Reverb.DecayTime = val;
  1651. break;
  1652. case AL_REVERB_DECAY_HFRATIO:
  1653. if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
  1654. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hfratio out of range");
  1655. props->Reverb.DecayHFRatio = val;
  1656. break;
  1657. case AL_REVERB_REFLECTIONS_GAIN:
  1658. if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
  1659. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections gain out of range");
  1660. props->Reverb.ReflectionsGain = val;
  1661. break;
  1662. case AL_REVERB_REFLECTIONS_DELAY:
  1663. if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
  1664. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections delay out of range");
  1665. props->Reverb.ReflectionsDelay = val;
  1666. break;
  1667. case AL_REVERB_LATE_REVERB_GAIN:
  1668. if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
  1669. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb gain out of range");
  1670. props->Reverb.LateReverbGain = val;
  1671. break;
  1672. case AL_REVERB_LATE_REVERB_DELAY:
  1673. if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
  1674. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb delay out of range");
  1675. props->Reverb.LateReverbDelay = val;
  1676. break;
  1677. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  1678. if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
  1679. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb air absorption gainhf out of range");
  1680. props->Reverb.AirAbsorptionGainHF = val;
  1681. break;
  1682. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  1683. if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
  1684. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb room rolloff factor out of range");
  1685. props->Reverb.RoomRolloffFactor = val;
  1686. break;
  1687. default:
  1688. context->setError(AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
  1689. }
  1690. }
  1691. void StdReverb_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  1692. { StdReverb_setParamf(props, context, param, vals[0]); }
  1693. void StdReverb_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
  1694. {
  1695. switch(param)
  1696. {
  1697. case AL_REVERB_DECAY_HFLIMIT:
  1698. *val = props->Reverb.DecayHFLimit;
  1699. break;
  1700. default:
  1701. context->setError(AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
  1702. }
  1703. }
  1704. void StdReverb_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
  1705. { StdReverb_getParami(props, context, param, vals); }
  1706. void StdReverb_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  1707. {
  1708. switch(param)
  1709. {
  1710. case AL_REVERB_DENSITY:
  1711. *val = props->Reverb.Density;
  1712. break;
  1713. case AL_REVERB_DIFFUSION:
  1714. *val = props->Reverb.Diffusion;
  1715. break;
  1716. case AL_REVERB_GAIN:
  1717. *val = props->Reverb.Gain;
  1718. break;
  1719. case AL_REVERB_GAINHF:
  1720. *val = props->Reverb.GainHF;
  1721. break;
  1722. case AL_REVERB_DECAY_TIME:
  1723. *val = props->Reverb.DecayTime;
  1724. break;
  1725. case AL_REVERB_DECAY_HFRATIO:
  1726. *val = props->Reverb.DecayHFRatio;
  1727. break;
  1728. case AL_REVERB_REFLECTIONS_GAIN:
  1729. *val = props->Reverb.ReflectionsGain;
  1730. break;
  1731. case AL_REVERB_REFLECTIONS_DELAY:
  1732. *val = props->Reverb.ReflectionsDelay;
  1733. break;
  1734. case AL_REVERB_LATE_REVERB_GAIN:
  1735. *val = props->Reverb.LateReverbGain;
  1736. break;
  1737. case AL_REVERB_LATE_REVERB_DELAY:
  1738. *val = props->Reverb.LateReverbDelay;
  1739. break;
  1740. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  1741. *val = props->Reverb.AirAbsorptionGainHF;
  1742. break;
  1743. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  1744. *val = props->Reverb.RoomRolloffFactor;
  1745. break;
  1746. default:
  1747. context->setError(AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
  1748. }
  1749. }
  1750. void StdReverb_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  1751. { StdReverb_getParamf(props, context, param, vals); }
  1752. DEFINE_ALEFFECT_VTABLE(StdReverb);
  1753. struct StdReverbStateFactory final : public EffectStateFactory {
  1754. EffectState *create() override { return new ReverbState{}; }
  1755. EffectProps getDefaultProps() const noexcept override;
  1756. const EffectVtable *getEffectVtable() const noexcept override { return &StdReverb_vtable; }
  1757. };
  1758. EffectProps StdReverbStateFactory::getDefaultProps() const noexcept
  1759. {
  1760. EffectProps props{};
  1761. props.Reverb.Density = AL_REVERB_DEFAULT_DENSITY;
  1762. props.Reverb.Diffusion = AL_REVERB_DEFAULT_DIFFUSION;
  1763. props.Reverb.Gain = AL_REVERB_DEFAULT_GAIN;
  1764. props.Reverb.GainHF = AL_REVERB_DEFAULT_GAINHF;
  1765. props.Reverb.GainLF = 1.0f;
  1766. props.Reverb.DecayTime = AL_REVERB_DEFAULT_DECAY_TIME;
  1767. props.Reverb.DecayHFRatio = AL_REVERB_DEFAULT_DECAY_HFRATIO;
  1768. props.Reverb.DecayLFRatio = 1.0f;
  1769. props.Reverb.ReflectionsGain = AL_REVERB_DEFAULT_REFLECTIONS_GAIN;
  1770. props.Reverb.ReflectionsDelay = AL_REVERB_DEFAULT_REFLECTIONS_DELAY;
  1771. props.Reverb.ReflectionsPan[0] = 0.0f;
  1772. props.Reverb.ReflectionsPan[1] = 0.0f;
  1773. props.Reverb.ReflectionsPan[2] = 0.0f;
  1774. props.Reverb.LateReverbGain = AL_REVERB_DEFAULT_LATE_REVERB_GAIN;
  1775. props.Reverb.LateReverbDelay = AL_REVERB_DEFAULT_LATE_REVERB_DELAY;
  1776. props.Reverb.LateReverbPan[0] = 0.0f;
  1777. props.Reverb.LateReverbPan[1] = 0.0f;
  1778. props.Reverb.LateReverbPan[2] = 0.0f;
  1779. props.Reverb.EchoTime = 0.25f;
  1780. props.Reverb.EchoDepth = 0.0f;
  1781. props.Reverb.ModulationTime = 0.25f;
  1782. props.Reverb.ModulationDepth = 0.0f;
  1783. props.Reverb.AirAbsorptionGainHF = AL_REVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
  1784. props.Reverb.HFReference = 5000.0f;
  1785. props.Reverb.LFReference = 250.0f;
  1786. props.Reverb.RoomRolloffFactor = AL_REVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
  1787. props.Reverb.DecayHFLimit = AL_REVERB_DEFAULT_DECAY_HFLIMIT;
  1788. return props;
  1789. }
  1790. } // namespace
  1791. EffectStateFactory *ReverbStateFactory_getFactory()
  1792. {
  1793. static ReverbStateFactory ReverbFactory{};
  1794. return &ReverbFactory;
  1795. }
  1796. EffectStateFactory *StdReverbStateFactory_getFactory()
  1797. {
  1798. static StdReverbStateFactory ReverbFactory{};
  1799. return &ReverbFactory;
  1800. }