reverb.c 80 KB

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