reverb.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /**
  2. * Reverb for the OpenAL cross platform audio library
  3. * Copyright (C) 2008-2009 by 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., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, 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 "alEffect.h"
  28. #include "alFilter.h"
  29. #include "alError.h"
  30. typedef struct DelayLine
  31. {
  32. // The delay lines use sample lengths that are powers of 2 to allow the
  33. // use of bit-masking instead of a modulus for wrapping.
  34. ALuint Mask;
  35. ALfloat *Line;
  36. } DelayLine;
  37. typedef struct ALreverbState {
  38. DERIVE_FROM_TYPE(ALeffectState);
  39. ALboolean IsEax;
  40. // All delay lines are allocated as a single buffer to reduce memory
  41. // fragmentation and management code.
  42. ALfloat *SampleBuffer;
  43. ALuint TotalSamples;
  44. // Master effect filters
  45. ALfilterState LpFilter;
  46. ALfilterState HpFilter; // EAX only
  47. struct {
  48. // Modulator delay line.
  49. DelayLine Delay;
  50. // The vibrato time is tracked with an index over a modulus-wrapped
  51. // range (in samples).
  52. ALuint Index;
  53. ALuint Range;
  54. // The depth of frequency change (also in samples) and its filter.
  55. ALfloat Depth;
  56. ALfloat Coeff;
  57. ALfloat Filter;
  58. } Mod;
  59. // Initial effect delay.
  60. DelayLine Delay;
  61. // The tap points for the initial delay. First tap goes to early
  62. // reflections, the last to late reverb.
  63. ALuint DelayTap[2];
  64. struct {
  65. // Output gain for early reflections.
  66. ALfloat Gain;
  67. // Early reflections are done with 4 delay lines.
  68. ALfloat Coeff[4];
  69. DelayLine Delay[4];
  70. ALuint Offset[4];
  71. // The gain for each output channel based on 3D panning (only for the
  72. // EAX path).
  73. ALfloat PanGain[MaxChannels];
  74. } Early;
  75. // Decorrelator delay line.
  76. DelayLine Decorrelator;
  77. // There are actually 4 decorrelator taps, but the first occurs at the
  78. // initial sample.
  79. ALuint DecoTap[3];
  80. struct {
  81. // Output gain for late reverb.
  82. ALfloat Gain;
  83. // Attenuation to compensate for the modal density and decay rate of
  84. // the late lines.
  85. ALfloat DensityGain;
  86. // The feed-back and feed-forward all-pass coefficient.
  87. ALfloat ApFeedCoeff;
  88. // Mixing matrix coefficient.
  89. ALfloat MixCoeff;
  90. // Late reverb has 4 parallel all-pass filters.
  91. ALfloat ApCoeff[4];
  92. DelayLine ApDelay[4];
  93. ALuint ApOffset[4];
  94. // In addition to 4 cyclical delay lines.
  95. ALfloat Coeff[4];
  96. DelayLine Delay[4];
  97. ALuint Offset[4];
  98. // The cyclical delay lines are 1-pole low-pass filtered.
  99. ALfloat LpCoeff[4];
  100. ALfloat LpSample[4];
  101. // The gain for each output channel based on 3D panning (only for the
  102. // EAX path).
  103. ALfloat PanGain[MaxChannels];
  104. } Late;
  105. struct {
  106. // Attenuation to compensate for the modal density and decay rate of
  107. // the echo line.
  108. ALfloat DensityGain;
  109. // Echo delay and all-pass lines.
  110. DelayLine Delay;
  111. DelayLine ApDelay;
  112. ALfloat Coeff;
  113. ALfloat ApFeedCoeff;
  114. ALfloat ApCoeff;
  115. ALuint Offset;
  116. ALuint ApOffset;
  117. // The echo line is 1-pole low-pass filtered.
  118. ALfloat LpCoeff;
  119. ALfloat LpSample;
  120. // Echo mixing coefficients.
  121. ALfloat MixCoeff[2];
  122. } Echo;
  123. // The current read offset for all delay lines.
  124. ALuint Offset;
  125. // The gain for each output channel (non-EAX path only; aliased from
  126. // Late.PanGain)
  127. ALfloat *Gain;
  128. /* Temporary storage used when processing, before deinterlacing. */
  129. ALfloat ReverbSamples[BUFFERSIZE][4];
  130. ALfloat EarlySamples[BUFFERSIZE][4];
  131. } ALreverbState;
  132. /* This is a user config option for modifying the overall output of the reverb
  133. * effect.
  134. */
  135. ALfloat ReverbBoost = 1.0f;
  136. /* Specifies whether to use a standard reverb effect in place of EAX reverb */
  137. ALboolean EmulateEAXReverb = AL_FALSE;
  138. /* This coefficient is used to define the maximum frequency range controlled
  139. * by the modulation depth. The current value of 0.1 will allow it to swing
  140. * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
  141. * sampler to stall on the downswing, and above 1 it will cause it to sample
  142. * backwards.
  143. */
  144. static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
  145. /* A filter is used to avoid the terrible distortion caused by changing
  146. * modulation time and/or depth. To be consistent across different sample
  147. * rates, the coefficient must be raised to a constant divided by the sample
  148. * rate: coeff^(constant / rate).
  149. */
  150. static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
  151. static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
  152. // When diffusion is above 0, an all-pass filter is used to take the edge off
  153. // the echo effect. It uses the following line length (in seconds).
  154. static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
  155. // Input into the late reverb is decorrelated between four channels. Their
  156. // timings are dependent on a fraction and multiplier. See the
  157. // UpdateDecorrelator() routine for the calculations involved.
  158. static const ALfloat DECO_FRACTION = 0.15f;
  159. static const ALfloat DECO_MULTIPLIER = 2.0f;
  160. // All delay line lengths are specified in seconds.
  161. // The lengths of the early delay lines.
  162. static const ALfloat EARLY_LINE_LENGTH[4] =
  163. {
  164. 0.0015f, 0.0045f, 0.0135f, 0.0405f
  165. };
  166. // The lengths of the late all-pass delay lines.
  167. static const ALfloat ALLPASS_LINE_LENGTH[4] =
  168. {
  169. 0.0151f, 0.0167f, 0.0183f, 0.0200f,
  170. };
  171. // The lengths of the late cyclical delay lines.
  172. static const ALfloat LATE_LINE_LENGTH[4] =
  173. {
  174. 0.0211f, 0.0311f, 0.0461f, 0.0680f
  175. };
  176. // The late cyclical delay lines have a variable length dependent on the
  177. // effect's density parameter (inverted for some reason) and this multiplier.
  178. static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
  179. // Basic delay line input/output routines.
  180. static inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
  181. {
  182. return Delay->Line[offset&Delay->Mask];
  183. }
  184. static inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
  185. {
  186. Delay->Line[offset&Delay->Mask] = in;
  187. }
  188. // Attenuated delay line output routine.
  189. static inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
  190. {
  191. return coeff * Delay->Line[offset&Delay->Mask];
  192. }
  193. // Basic attenuated all-pass input/output routine.
  194. static inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
  195. {
  196. ALfloat out, feed;
  197. out = DelayLineOut(Delay, outOffset);
  198. feed = feedCoeff * in;
  199. DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
  200. // The time-based attenuation is only applied to the delay output to
  201. // keep it from affecting the feed-back path (which is already controlled
  202. // by the all-pass feed coefficient).
  203. return (coeff * out) - feed;
  204. }
  205. // Given an input sample, this function produces modulation for the late
  206. // reverb.
  207. static inline ALfloat EAXModulation(ALreverbState *State, ALfloat in)
  208. {
  209. ALfloat sinus, frac;
  210. ALuint offset;
  211. ALfloat out0, out1;
  212. // Calculate the sinus rythm (dependent on modulation time and the
  213. // sampling rate). The center of the sinus is moved to reduce the delay
  214. // of the effect when the time or depth are low.
  215. sinus = 1.0f - cosf(F_2PI * State->Mod.Index / State->Mod.Range);
  216. // The depth determines the range over which to read the input samples
  217. // from, so it must be filtered to reduce the distortion caused by even
  218. // small parameter changes.
  219. State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
  220. State->Mod.Coeff);
  221. // Calculate the read offset and fraction between it and the next sample.
  222. frac = (1.0f + (State->Mod.Filter * sinus));
  223. offset = fastf2u(frac);
  224. frac -= offset;
  225. // Get the two samples crossed by the offset, and feed the delay line
  226. // with the next input sample.
  227. out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
  228. out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
  229. DelayLineIn(&State->Mod.Delay, State->Offset, in);
  230. // Step the modulation index forward, keeping it bound to its range.
  231. State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
  232. // The output is obtained by linearly interpolating the two samples that
  233. // were acquired above.
  234. return lerp(out0, out1, frac);
  235. }
  236. // Delay line output routine for early reflections.
  237. static inline ALfloat EarlyDelayLineOut(ALreverbState *State, ALuint index)
  238. {
  239. return AttenuatedDelayLineOut(&State->Early.Delay[index],
  240. State->Offset - State->Early.Offset[index],
  241. State->Early.Coeff[index]);
  242. }
  243. // Given an input sample, this function produces four-channel output for the
  244. // early reflections.
  245. static inline ALvoid EarlyReflection(ALreverbState *State, ALfloat in, ALfloat *restrict out)
  246. {
  247. ALfloat d[4], v, f[4];
  248. // Obtain the decayed results of each early delay line.
  249. d[0] = EarlyDelayLineOut(State, 0);
  250. d[1] = EarlyDelayLineOut(State, 1);
  251. d[2] = EarlyDelayLineOut(State, 2);
  252. d[3] = EarlyDelayLineOut(State, 3);
  253. /* The following uses a lossless scattering junction from waveguide
  254. * theory. It actually amounts to a householder mixing matrix, which
  255. * will produce a maximally diffuse response, and means this can probably
  256. * be considered a simple feed-back delay network (FDN).
  257. * N
  258. * ---
  259. * \
  260. * v = 2/N / d_i
  261. * ---
  262. * i=1
  263. */
  264. v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
  265. // The junction is loaded with the input here.
  266. v += in;
  267. // Calculate the feed values for the delay lines.
  268. f[0] = v - d[0];
  269. f[1] = v - d[1];
  270. f[2] = v - d[2];
  271. f[3] = v - d[3];
  272. // Re-feed the delay lines.
  273. DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
  274. DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
  275. DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
  276. DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
  277. // Output the results of the junction for all four channels.
  278. out[0] = State->Early.Gain * f[0];
  279. out[1] = State->Early.Gain * f[1];
  280. out[2] = State->Early.Gain * f[2];
  281. out[3] = State->Early.Gain * f[3];
  282. }
  283. // All-pass input/output routine for late reverb.
  284. static inline ALfloat LateAllPassInOut(ALreverbState *State, ALuint index, ALfloat in)
  285. {
  286. return AllpassInOut(&State->Late.ApDelay[index],
  287. State->Offset - State->Late.ApOffset[index],
  288. State->Offset, in, State->Late.ApFeedCoeff,
  289. State->Late.ApCoeff[index]);
  290. }
  291. // Delay line output routine for late reverb.
  292. static inline ALfloat LateDelayLineOut(ALreverbState *State, ALuint index)
  293. {
  294. return AttenuatedDelayLineOut(&State->Late.Delay[index],
  295. State->Offset - State->Late.Offset[index],
  296. State->Late.Coeff[index]);
  297. }
  298. // Low-pass filter input/output routine for late reverb.
  299. static inline ALfloat LateLowPassInOut(ALreverbState *State, ALuint index, ALfloat in)
  300. {
  301. in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
  302. State->Late.LpSample[index] = in;
  303. return in;
  304. }
  305. // Given four decorrelated input samples, this function produces four-channel
  306. // output for the late reverb.
  307. static inline ALvoid LateReverb(ALreverbState *State, const ALfloat *restrict in, ALfloat *restrict out)
  308. {
  309. ALfloat d[4], f[4];
  310. // Obtain the decayed results of the cyclical delay lines, and add the
  311. // corresponding input channels. Then pass the results through the
  312. // low-pass filters.
  313. // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
  314. // to 0.
  315. d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
  316. d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
  317. d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
  318. d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
  319. // To help increase diffusion, run each line through an all-pass filter.
  320. // When there is no diffusion, the shortest all-pass filter will feed the
  321. // shortest delay line.
  322. d[0] = LateAllPassInOut(State, 0, d[0]);
  323. d[1] = LateAllPassInOut(State, 1, d[1]);
  324. d[2] = LateAllPassInOut(State, 2, d[2]);
  325. d[3] = LateAllPassInOut(State, 3, d[3]);
  326. /* Late reverb is done with a modified feed-back delay network (FDN)
  327. * topology. Four input lines are each fed through their own all-pass
  328. * filter and then into the mixing matrix. The four outputs of the
  329. * mixing matrix are then cycled back to the inputs. Each output feeds
  330. * a different input to form a circlular feed cycle.
  331. *
  332. * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
  333. * using a single unitary rotational parameter:
  334. *
  335. * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
  336. * [ -a, d, c, -b ]
  337. * [ -b, -c, d, a ]
  338. * [ -c, b, -a, d ]
  339. *
  340. * The rotation is constructed from the effect's diffusion parameter,
  341. * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
  342. * with differing signs, and d is the coefficient x. The matrix is thus:
  343. *
  344. * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
  345. * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
  346. * [ y, -y, x, y ] x = cos(t)
  347. * [ -y, -y, -y, x ] y = sin(t) / n
  348. *
  349. * To reduce the number of multiplies, the x coefficient is applied with
  350. * the cyclical delay line coefficients. Thus only the y coefficient is
  351. * applied when mixing, and is modified to be: y / x.
  352. */
  353. f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
  354. f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
  355. f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
  356. f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
  357. // Output the results of the matrix for all four channels, attenuated by
  358. // the late reverb gain (which is attenuated by the 'x' mix coefficient).
  359. out[0] = State->Late.Gain * f[0];
  360. out[1] = State->Late.Gain * f[1];
  361. out[2] = State->Late.Gain * f[2];
  362. out[3] = State->Late.Gain * f[3];
  363. // Re-feed the cyclical delay lines.
  364. DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
  365. DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
  366. DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
  367. DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
  368. }
  369. // Given an input sample, this function mixes echo into the four-channel late
  370. // reverb.
  371. static inline ALvoid EAXEcho(ALreverbState *State, ALfloat in, ALfloat *restrict late)
  372. {
  373. ALfloat out, feed;
  374. // Get the latest attenuated echo sample for output.
  375. feed = AttenuatedDelayLineOut(&State->Echo.Delay,
  376. State->Offset - State->Echo.Offset,
  377. State->Echo.Coeff);
  378. // Mix the output into the late reverb channels.
  379. out = State->Echo.MixCoeff[0] * feed;
  380. late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
  381. late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
  382. late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
  383. late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
  384. // Mix the energy-attenuated input with the output and pass it through
  385. // the echo low-pass filter.
  386. feed += State->Echo.DensityGain * in;
  387. feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
  388. State->Echo.LpSample = feed;
  389. // Then the echo all-pass filter.
  390. feed = AllpassInOut(&State->Echo.ApDelay,
  391. State->Offset - State->Echo.ApOffset,
  392. State->Offset, feed, State->Echo.ApFeedCoeff,
  393. State->Echo.ApCoeff);
  394. // Feed the delay with the mixed and filtered sample.
  395. DelayLineIn(&State->Echo.Delay, State->Offset, feed);
  396. }
  397. // Perform the non-EAX reverb pass on a given input sample, resulting in
  398. // four-channel output.
  399. static inline ALvoid VerbPass(ALreverbState *State, ALfloat in, ALfloat *restrict out)
  400. {
  401. ALfloat feed, late[4], taps[4];
  402. // Filter the incoming sample.
  403. in = ALfilterState_processSingle(&State->LpFilter, in);
  404. // Feed the initial delay line.
  405. DelayLineIn(&State->Delay, State->Offset, in);
  406. // Calculate the early reflection from the first delay tap.
  407. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
  408. EarlyReflection(State, in, out);
  409. // Feed the decorrelator from the energy-attenuated output of the second
  410. // delay tap.
  411. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
  412. feed = in * State->Late.DensityGain;
  413. DelayLineIn(&State->Decorrelator, State->Offset, feed);
  414. // Calculate the late reverb from the decorrelator taps.
  415. taps[0] = feed;
  416. taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
  417. taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
  418. taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
  419. LateReverb(State, taps, late);
  420. // Mix early reflections and late reverb.
  421. out[0] += late[0];
  422. out[1] += late[1];
  423. out[2] += late[2];
  424. out[3] += late[3];
  425. // Step all delays forward one sample.
  426. State->Offset++;
  427. }
  428. // Perform the EAX reverb pass on a given input sample, resulting in four-
  429. // channel output.
  430. static inline ALvoid EAXVerbPass(ALreverbState *State, ALfloat in, ALfloat *restrict early, ALfloat *restrict late)
  431. {
  432. ALfloat feed, taps[4];
  433. // Low-pass filter the incoming sample.
  434. in = ALfilterState_processSingle(&State->LpFilter, in);
  435. in = ALfilterState_processSingle(&State->HpFilter, in);
  436. // Perform any modulation on the input.
  437. in = EAXModulation(State, in);
  438. // Feed the initial delay line.
  439. DelayLineIn(&State->Delay, State->Offset, in);
  440. // Calculate the early reflection from the first delay tap.
  441. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
  442. EarlyReflection(State, in, early);
  443. // Feed the decorrelator from the energy-attenuated output of the second
  444. // delay tap.
  445. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
  446. feed = in * State->Late.DensityGain;
  447. DelayLineIn(&State->Decorrelator, State->Offset, feed);
  448. // Calculate the late reverb from the decorrelator taps.
  449. taps[0] = feed;
  450. taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
  451. taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
  452. taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
  453. LateReverb(State, taps, late);
  454. // Calculate and mix in any echo.
  455. EAXEcho(State, in, late);
  456. // Step all delays forward one sample.
  457. State->Offset++;
  458. }
  459. static ALvoid ALreverbState_processStandard(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE])
  460. {
  461. ALfloat (*restrict out)[4] = State->ReverbSamples;
  462. ALuint index, c;
  463. /* Process reverb for these samples. */
  464. for(index = 0;index < SamplesToDo;index++)
  465. VerbPass(State, SamplesIn[index], out[index]);
  466. for(c = 0;c < MaxChannels;c++)
  467. {
  468. ALfloat gain = State->Gain[c];
  469. if(!(gain > GAIN_SILENCE_THRESHOLD))
  470. continue;
  471. for(index = 0;index < SamplesToDo;index++)
  472. SamplesOut[c][index] += gain * out[index][c&3];
  473. }
  474. }
  475. static ALvoid ALreverbState_processEax(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE])
  476. {
  477. ALfloat (*restrict early)[4] = State->EarlySamples;
  478. ALfloat (*restrict late)[4] = State->ReverbSamples;
  479. ALuint index, c;
  480. /* Process reverb for these samples. */
  481. for(index = 0;index < SamplesToDo;index++)
  482. EAXVerbPass(State, SamplesIn[index], early[index], late[index]);
  483. for(c = 0;c < MaxChannels;c++)
  484. {
  485. ALfloat earlyGain, lateGain;
  486. earlyGain = State->Early.PanGain[c];
  487. if(earlyGain > GAIN_SILENCE_THRESHOLD)
  488. {
  489. for(index = 0;index < SamplesToDo;index++)
  490. SamplesOut[c][index] += earlyGain*early[index][c&3];
  491. }
  492. lateGain = State->Late.PanGain[c];
  493. if(lateGain > GAIN_SILENCE_THRESHOLD)
  494. {
  495. for(index = 0;index < SamplesToDo;index++)
  496. SamplesOut[c][index] += lateGain*late[index][c&3];
  497. }
  498. }
  499. }
  500. static ALvoid ALreverbState_process(ALreverbState *State, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE])
  501. {
  502. if(State->IsEax)
  503. ALreverbState_processEax(State, SamplesToDo, SamplesIn, SamplesOut);
  504. else
  505. ALreverbState_processStandard(State, SamplesToDo, SamplesIn, SamplesOut);
  506. }
  507. // Given the allocated sample buffer, this function updates each delay line
  508. // offset.
  509. static inline ALvoid RealizeLineOffset(ALfloat *sampleBuffer, DelayLine *Delay)
  510. {
  511. Delay->Line = &sampleBuffer[(ALintptrEXT)Delay->Line];
  512. }
  513. // Calculate the length of a delay line and store its mask and offset.
  514. static ALuint CalcLineLength(ALfloat length, ALintptrEXT offset, ALuint frequency, DelayLine *Delay)
  515. {
  516. ALuint samples;
  517. // All line lengths are powers of 2, calculated from their lengths, with
  518. // an additional sample in case of rounding errors.
  519. samples = NextPowerOf2(fastf2u(length * frequency) + 1);
  520. // All lines share a single sample buffer.
  521. Delay->Mask = samples - 1;
  522. Delay->Line = (ALfloat*)offset;
  523. // Return the sample count for accumulation.
  524. return samples;
  525. }
  526. /* Calculates the delay line metrics and allocates the shared sample buffer
  527. * for all lines given the sample rate (frequency). If an allocation failure
  528. * occurs, it returns AL_FALSE.
  529. */
  530. static ALboolean AllocLines(ALuint frequency, ALreverbState *State)
  531. {
  532. ALuint totalSamples, index;
  533. ALfloat length;
  534. ALfloat *newBuffer = NULL;
  535. // All delay line lengths are calculated to accomodate the full range of
  536. // lengths given their respective paramters.
  537. totalSamples = 0;
  538. /* The modulator's line length is calculated from the maximum modulation
  539. * time and depth coefficient, and halfed for the low-to-high frequency
  540. * swing. An additional sample is added to keep it stable when there is no
  541. * modulation.
  542. */
  543. length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f) +
  544. (1.0f / frequency);
  545. totalSamples += CalcLineLength(length, totalSamples, frequency,
  546. &State->Mod.Delay);
  547. // The initial delay is the sum of the reflections and late reverb
  548. // delays.
  549. length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
  550. AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
  551. totalSamples += CalcLineLength(length, totalSamples, frequency,
  552. &State->Delay);
  553. // The early reflection lines.
  554. for(index = 0;index < 4;index++)
  555. totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
  556. frequency, &State->Early.Delay[index]);
  557. // The decorrelator line is calculated from the lowest reverb density (a
  558. // parameter value of 1).
  559. length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
  560. LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
  561. totalSamples += CalcLineLength(length, totalSamples, frequency,
  562. &State->Decorrelator);
  563. // The late all-pass lines.
  564. for(index = 0;index < 4;index++)
  565. totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
  566. frequency, &State->Late.ApDelay[index]);
  567. // The late delay lines are calculated from the lowest reverb density.
  568. for(index = 0;index < 4;index++)
  569. {
  570. length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
  571. totalSamples += CalcLineLength(length, totalSamples, frequency,
  572. &State->Late.Delay[index]);
  573. }
  574. // The echo all-pass and delay lines.
  575. totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
  576. frequency, &State->Echo.ApDelay);
  577. totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
  578. frequency, &State->Echo.Delay);
  579. if(totalSamples != State->TotalSamples)
  580. {
  581. TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
  582. newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
  583. if(newBuffer == NULL)
  584. return AL_FALSE;
  585. State->SampleBuffer = newBuffer;
  586. State->TotalSamples = totalSamples;
  587. }
  588. // Update all delays to reflect the new sample buffer.
  589. RealizeLineOffset(State->SampleBuffer, &State->Delay);
  590. RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
  591. for(index = 0;index < 4;index++)
  592. {
  593. RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
  594. RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
  595. RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
  596. }
  597. RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
  598. RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
  599. RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
  600. // Clear the sample buffer.
  601. for(index = 0;index < State->TotalSamples;index++)
  602. State->SampleBuffer[index] = 0.0f;
  603. return AL_TRUE;
  604. }
  605. static ALboolean ALreverbState_deviceUpdate(ALreverbState *State, ALCdevice *Device)
  606. {
  607. ALuint frequency = Device->Frequency, index;
  608. // Allocate the delay lines.
  609. if(!AllocLines(frequency, State))
  610. return AL_FALSE;
  611. // Calculate the modulation filter coefficient. Notice that the exponent
  612. // is calculated given the current sample rate. This ensures that the
  613. // resulting filter response over time is consistent across all sample
  614. // rates.
  615. State->Mod.Coeff = powf(MODULATION_FILTER_COEFF,
  616. MODULATION_FILTER_CONST / frequency);
  617. // The early reflection and late all-pass filter line lengths are static,
  618. // so their offsets only need to be calculated once.
  619. for(index = 0;index < 4;index++)
  620. {
  621. State->Early.Offset[index] = fastf2u(EARLY_LINE_LENGTH[index] *
  622. frequency);
  623. State->Late.ApOffset[index] = fastf2u(ALLPASS_LINE_LENGTH[index] *
  624. frequency);
  625. }
  626. // The echo all-pass filter line length is static, so its offset only
  627. // needs to be calculated once.
  628. State->Echo.ApOffset = fastf2u(ECHO_ALLPASS_LENGTH * frequency);
  629. return AL_TRUE;
  630. }
  631. // Calculate a decay coefficient given the length of each cycle and the time
  632. // until the decay reaches -60 dB.
  633. static inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
  634. {
  635. return powf(0.001f/*-60 dB*/, length/decayTime);
  636. }
  637. // Calculate a decay length from a coefficient and the time until the decay
  638. // reaches -60 dB.
  639. static inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
  640. {
  641. return log10f(coeff) * decayTime / log10f(0.001f)/*-60 dB*/;
  642. }
  643. // Calculate an attenuation to be applied to the input of any echo models to
  644. // compensate for modal density and decay time.
  645. static inline ALfloat CalcDensityGain(ALfloat a)
  646. {
  647. /* The energy of a signal can be obtained by finding the area under the
  648. * squared signal. This takes the form of Sum(x_n^2), where x is the
  649. * amplitude for the sample n.
  650. *
  651. * Decaying feedback matches exponential decay of the form Sum(a^n),
  652. * where a is the attenuation coefficient, and n is the sample. The area
  653. * under this decay curve can be calculated as: 1 / (1 - a).
  654. *
  655. * Modifying the above equation to find the squared area under the curve
  656. * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
  657. * calculated by inverting the square root of this approximation,
  658. * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
  659. */
  660. return sqrtf(1.0f - (a * a));
  661. }
  662. // Calculate the mixing matrix coefficients given a diffusion factor.
  663. static inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
  664. {
  665. ALfloat n, t;
  666. // The matrix is of order 4, so n is sqrt (4 - 1).
  667. n = sqrtf(3.0f);
  668. t = diffusion * atanf(n);
  669. // Calculate the first mixing matrix coefficient.
  670. *x = cosf(t);
  671. // Calculate the second mixing matrix coefficient.
  672. *y = sinf(t) / n;
  673. }
  674. // Calculate the limited HF ratio for use with the late reverb low-pass
  675. // filters.
  676. static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
  677. {
  678. ALfloat limitRatio;
  679. /* Find the attenuation due to air absorption in dB (converting delay
  680. * time to meters using the speed of sound). Then reversing the decay
  681. * equation, solve for HF ratio. The delay length is cancelled out of
  682. * the equation, so it can be calculated once for all lines.
  683. */
  684. limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
  685. SPEEDOFSOUNDMETRESPERSEC);
  686. /* Using the limit calculated above, apply the upper bound to the HF
  687. * ratio. Also need to limit the result to a minimum of 0.1, just like the
  688. * HF ratio parameter. */
  689. return clampf(limitRatio, 0.1f, hfRatio);
  690. }
  691. // Calculate the coefficient for a HF (and eventually LF) decay damping
  692. // filter.
  693. static inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
  694. {
  695. ALfloat coeff, g;
  696. // Eventually this should boost the high frequencies when the ratio
  697. // exceeds 1.
  698. coeff = 0.0f;
  699. if (hfRatio < 1.0f)
  700. {
  701. // Calculate the low-pass coefficient by dividing the HF decay
  702. // coefficient by the full decay coefficient.
  703. g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
  704. // Damping is done with a 1-pole filter, so g needs to be squared.
  705. g *= g;
  706. if(g < 0.9999f) /* 1-epsilon */
  707. {
  708. /* Be careful with gains < 0.001, as that causes the coefficient
  709. * head towards 1, which will flatten the signal. */
  710. g = maxf(g, 0.001f);
  711. coeff = (1 - g*cw - sqrtf(2*g*(1-cw) - g*g*(1 - cw*cw))) /
  712. (1 - g);
  713. }
  714. // Very low decay times will produce minimal output, so apply an
  715. // upper bound to the coefficient.
  716. coeff = minf(coeff, 0.98f);
  717. }
  718. return coeff;
  719. }
  720. // Update the EAX modulation index, range, and depth. Keep in mind that this
  721. // kind of vibrato is additive and not multiplicative as one may expect. The
  722. // downswing will sound stronger than the upswing.
  723. static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALreverbState *State)
  724. {
  725. ALuint range;
  726. /* Modulation is calculated in two parts.
  727. *
  728. * The modulation time effects the sinus applied to the change in
  729. * frequency. An index out of the current time range (both in samples)
  730. * is incremented each sample. The range is bound to a reasonable
  731. * minimum (1 sample) and when the timing changes, the index is rescaled
  732. * to the new range (to keep the sinus consistent).
  733. */
  734. range = maxu(fastf2u(modTime*frequency), 1);
  735. State->Mod.Index = (ALuint)(State->Mod.Index * (ALuint64)range /
  736. State->Mod.Range);
  737. State->Mod.Range = range;
  738. /* The modulation depth effects the amount of frequency change over the
  739. * range of the sinus. It needs to be scaled by the modulation time so
  740. * that a given depth produces a consistent change in frequency over all
  741. * ranges of time. Since the depth is applied to a sinus value, it needs
  742. * to be halfed once for the sinus range and again for the sinus swing
  743. * in time (half of it is spent decreasing the frequency, half is spent
  744. * increasing it).
  745. */
  746. State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
  747. 2.0f * frequency;
  748. }
  749. // Update the offsets for the initial effect delay line.
  750. static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALreverbState *State)
  751. {
  752. // Calculate the initial delay taps.
  753. State->DelayTap[0] = fastf2u(earlyDelay * frequency);
  754. State->DelayTap[1] = fastf2u((earlyDelay + lateDelay) * frequency);
  755. }
  756. // Update the early reflections gain and line coefficients.
  757. static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALreverbState *State)
  758. {
  759. ALuint index;
  760. // Calculate the early reflections gain (from the master effect gain, and
  761. // reflections gain parameters) with a constant attenuation of 0.5.
  762. State->Early.Gain = 0.5f * reverbGain * earlyGain;
  763. // Calculate the gain (coefficient) for each early delay line using the
  764. // late delay time. This expands the early reflections to the start of
  765. // the late reverb.
  766. for(index = 0;index < 4;index++)
  767. State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
  768. lateDelay);
  769. }
  770. // Update the offsets for the decorrelator line.
  771. static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALreverbState *State)
  772. {
  773. ALuint index;
  774. ALfloat length;
  775. /* The late reverb inputs are decorrelated to smooth the reverb tail and
  776. * reduce harsh echos. The first tap occurs immediately, while the
  777. * remaining taps are delayed by multiples of a fraction of the smallest
  778. * cyclical delay time.
  779. *
  780. * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
  781. */
  782. for(index = 0;index < 3;index++)
  783. {
  784. length = (DECO_FRACTION * powf(DECO_MULTIPLIER, (ALfloat)index)) *
  785. LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
  786. State->DecoTap[index] = fastf2u(length * frequency);
  787. }
  788. }
  789. // Update the late reverb gains, line lengths, and line coefficients.
  790. static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
  791. {
  792. ALfloat length;
  793. ALuint index;
  794. /* Calculate the late reverb gain (from the master effect gain, and late
  795. * reverb gain parameters). Since the output is tapped prior to the
  796. * application of the next delay line coefficients, this gain needs to be
  797. * attenuated by the 'x' mixing matrix coefficient as well.
  798. */
  799. State->Late.Gain = reverbGain * lateGain * xMix;
  800. /* To compensate for changes in modal density and decay time of the late
  801. * reverb signal, the input is attenuated based on the maximal energy of
  802. * the outgoing signal. This approximation is used to keep the apparent
  803. * energy of the signal equal for all ranges of density and decay time.
  804. *
  805. * The average length of the cyclcical delay lines is used to calculate
  806. * the attenuation coefficient.
  807. */
  808. length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
  809. LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
  810. length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
  811. State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,
  812. decayTime));
  813. // Calculate the all-pass feed-back and feed-forward coefficient.
  814. State->Late.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
  815. for(index = 0;index < 4;index++)
  816. {
  817. // Calculate the gain (coefficient) for each all-pass line.
  818. State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],
  819. decayTime);
  820. // Calculate the length (in seconds) of each cyclical delay line.
  821. length = LATE_LINE_LENGTH[index] * (1.0f + (density *
  822. LATE_LINE_MULTIPLIER));
  823. // Calculate the delay offset for each cyclical delay line.
  824. State->Late.Offset[index] = fastf2u(length * frequency);
  825. // Calculate the gain (coefficient) for each cyclical line.
  826. State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
  827. // Calculate the damping coefficient for each low-pass filter.
  828. State->Late.LpCoeff[index] =
  829. CalcDampingCoeff(hfRatio, length, decayTime,
  830. State->Late.Coeff[index], cw);
  831. // Attenuate the cyclical line coefficients by the mixing coefficient
  832. // (x).
  833. State->Late.Coeff[index] *= xMix;
  834. }
  835. }
  836. // Update the echo gain, line offset, line coefficients, and mixing
  837. // coefficients.
  838. static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALreverbState *State)
  839. {
  840. // Update the offset and coefficient for the echo delay line.
  841. State->Echo.Offset = fastf2u(echoTime * frequency);
  842. // Calculate the decay coefficient for the echo line.
  843. State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
  844. // Calculate the energy-based attenuation coefficient for the echo delay
  845. // line.
  846. State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
  847. // Calculate the echo all-pass feed coefficient.
  848. State->Echo.ApFeedCoeff = 0.5f * powf(diffusion, 2.0f);
  849. // Calculate the echo all-pass attenuation coefficient.
  850. State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
  851. // Calculate the damping coefficient for each low-pass filter.
  852. State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
  853. State->Echo.Coeff, cw);
  854. /* Calculate the echo mixing coefficients. The first is applied to the
  855. * echo itself. The second is used to attenuate the late reverb when
  856. * echo depth is high and diffusion is low, so the echo is slightly
  857. * stronger than the decorrelated echos in the reverb tail.
  858. */
  859. State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;
  860. State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));
  861. }
  862. // Update the early and late 3D panning gains.
  863. static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALreverbState *State)
  864. {
  865. ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],
  866. ReflectionsPan[2] };
  867. ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],
  868. LateReverbPan[2] };
  869. ALfloat ambientGain;
  870. ALfloat dirGain;
  871. ALfloat length;
  872. Gain *= ReverbBoost;
  873. /* Attenuate reverb according to its coverage (dirGain=0 will give
  874. * Gain*ambientGain, and dirGain=1 will give Gain). */
  875. ambientGain = minf(sqrtf(2.0f/Device->NumChan), 1.0f);
  876. length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];
  877. if(length > 1.0f)
  878. {
  879. length = 1.0f / sqrtf(length);
  880. earlyPan[0] *= length;
  881. earlyPan[1] *= length;
  882. earlyPan[2] *= length;
  883. }
  884. length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];
  885. if(length > 1.0f)
  886. {
  887. length = 1.0f / sqrtf(length);
  888. latePan[0] *= length;
  889. latePan[1] *= length;
  890. latePan[2] *= length;
  891. }
  892. dirGain = sqrtf(earlyPan[0]*earlyPan[0] + earlyPan[2]*earlyPan[2]);
  893. ComputeAngleGains(Device, atan2f(earlyPan[0], earlyPan[2]), (1.0f-dirGain)*F_PI,
  894. lerp(ambientGain, 1.0f, dirGain) * Gain, State->Early.PanGain);
  895. dirGain = sqrtf(latePan[0]*latePan[0] + latePan[2]*latePan[2]);
  896. ComputeAngleGains(Device, atan2f(latePan[0], latePan[2]), (1.0f-dirGain)*F_PI,
  897. lerp(ambientGain, 1.0f, dirGain) * Gain, State->Late.PanGain);
  898. }
  899. static ALvoid ALreverbState_update(ALreverbState *State, ALCdevice *Device, const ALeffectslot *Slot)
  900. {
  901. ALuint frequency = Device->Frequency;
  902. ALfloat lfscale, hfscale, hfRatio;
  903. ALfloat cw, x, y;
  904. if(Slot->EffectType == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
  905. State->IsEax = AL_TRUE;
  906. else if(Slot->EffectType == AL_EFFECT_REVERB || EmulateEAXReverb)
  907. State->IsEax = AL_FALSE;
  908. // Calculate the master low-pass filter (from the master effect HF gain).
  909. if(State->IsEax)
  910. {
  911. hfscale = Slot->EffectProps.Reverb.HFReference / frequency;
  912. ALfilterState_setParams(&State->LpFilter, ALfilterType_HighShelf,
  913. Slot->EffectProps.Reverb.GainHF,
  914. hfscale, 0.0f);
  915. lfscale = Slot->EffectProps.Reverb.LFReference / frequency;
  916. ALfilterState_setParams(&State->HpFilter, ALfilterType_LowShelf,
  917. Slot->EffectProps.Reverb.GainLF,
  918. lfscale, 0.0f);
  919. }
  920. else
  921. {
  922. hfscale = (ALfloat)LOWPASSFREQREF / frequency;
  923. ALfilterState_setParams(&State->LpFilter, ALfilterType_HighShelf,
  924. Slot->EffectProps.Reverb.GainHF,
  925. hfscale, 0.0f);
  926. }
  927. if(State->IsEax)
  928. {
  929. // Update the modulator line.
  930. UpdateModulator(Slot->EffectProps.Reverb.ModulationTime,
  931. Slot->EffectProps.Reverb.ModulationDepth,
  932. frequency, State);
  933. }
  934. // Update the initial effect delay.
  935. UpdateDelayLine(Slot->EffectProps.Reverb.ReflectionsDelay,
  936. Slot->EffectProps.Reverb.LateReverbDelay,
  937. frequency, State);
  938. // Update the early lines.
  939. UpdateEarlyLines(Slot->EffectProps.Reverb.Gain,
  940. Slot->EffectProps.Reverb.ReflectionsGain,
  941. Slot->EffectProps.Reverb.LateReverbDelay, State);
  942. // Update the decorrelator.
  943. UpdateDecorrelator(Slot->EffectProps.Reverb.Density, frequency, State);
  944. // Get the mixing matrix coefficients (x and y).
  945. CalcMatrixCoeffs(Slot->EffectProps.Reverb.Diffusion, &x, &y);
  946. // Then divide x into y to simplify the matrix calculation.
  947. State->Late.MixCoeff = y / x;
  948. // If the HF limit parameter is flagged, calculate an appropriate limit
  949. // based on the air absorption parameter.
  950. hfRatio = Slot->EffectProps.Reverb.DecayHFRatio;
  951. if(Slot->EffectProps.Reverb.DecayHFLimit &&
  952. Slot->EffectProps.Reverb.AirAbsorptionGainHF < 1.0f)
  953. hfRatio = CalcLimitedHfRatio(hfRatio,
  954. Slot->EffectProps.Reverb.AirAbsorptionGainHF,
  955. Slot->EffectProps.Reverb.DecayTime);
  956. cw = cosf(F_2PI * hfscale);
  957. // Update the late lines.
  958. UpdateLateLines(Slot->EffectProps.Reverb.Gain, Slot->EffectProps.Reverb.LateReverbGain,
  959. x, Slot->EffectProps.Reverb.Density, Slot->EffectProps.Reverb.DecayTime,
  960. Slot->EffectProps.Reverb.Diffusion, hfRatio, cw, frequency, State);
  961. if(State->IsEax)
  962. {
  963. // Update the echo line.
  964. UpdateEchoLine(Slot->EffectProps.Reverb.Gain, Slot->EffectProps.Reverb.LateReverbGain,
  965. Slot->EffectProps.Reverb.EchoTime, Slot->EffectProps.Reverb.DecayTime,
  966. Slot->EffectProps.Reverb.Diffusion, Slot->EffectProps.Reverb.EchoDepth,
  967. hfRatio, cw, frequency, State);
  968. // Update early and late 3D panning.
  969. Update3DPanning(Device, Slot->EffectProps.Reverb.ReflectionsPan,
  970. Slot->EffectProps.Reverb.LateReverbPan, Slot->Gain, State);
  971. }
  972. else
  973. {
  974. /* Update channel gains */
  975. ALfloat gain = sqrtf(2.0f/Device->NumChan) * ReverbBoost * Slot->Gain;
  976. SetGains(Device, gain, State->Gain);
  977. }
  978. }
  979. static ALvoid ALreverbState_Destruct(ALreverbState *State)
  980. {
  981. free(State->SampleBuffer);
  982. State->SampleBuffer = NULL;
  983. }
  984. static void ALreverbState_Delete(ALreverbState *state)
  985. {
  986. free(state);
  987. }
  988. DEFINE_ALEFFECTSTATE_VTABLE(ALreverbState);
  989. typedef struct ALreverbStateFactory {
  990. DERIVE_FROM_TYPE(ALeffectStateFactory);
  991. } ALreverbStateFactory;
  992. static ALeffectState *ALreverbStateFactory_create(ALreverbStateFactory* UNUSED(factory))
  993. {
  994. ALreverbState *state;
  995. ALuint index;
  996. state = malloc(sizeof(ALreverbState));
  997. if(!state) return NULL;
  998. SET_VTABLE2(ALreverbState, ALeffectState, state);
  999. state->TotalSamples = 0;
  1000. state->SampleBuffer = NULL;
  1001. ALfilterState_clear(&state->LpFilter);
  1002. ALfilterState_clear(&state->HpFilter);
  1003. state->Mod.Delay.Mask = 0;
  1004. state->Mod.Delay.Line = NULL;
  1005. state->Mod.Index = 0;
  1006. state->Mod.Range = 1;
  1007. state->Mod.Depth = 0.0f;
  1008. state->Mod.Coeff = 0.0f;
  1009. state->Mod.Filter = 0.0f;
  1010. state->Delay.Mask = 0;
  1011. state->Delay.Line = NULL;
  1012. state->DelayTap[0] = 0;
  1013. state->DelayTap[1] = 0;
  1014. state->Early.Gain = 0.0f;
  1015. for(index = 0;index < 4;index++)
  1016. {
  1017. state->Early.Coeff[index] = 0.0f;
  1018. state->Early.Delay[index].Mask = 0;
  1019. state->Early.Delay[index].Line = NULL;
  1020. state->Early.Offset[index] = 0;
  1021. }
  1022. state->Decorrelator.Mask = 0;
  1023. state->Decorrelator.Line = NULL;
  1024. state->DecoTap[0] = 0;
  1025. state->DecoTap[1] = 0;
  1026. state->DecoTap[2] = 0;
  1027. state->Late.Gain = 0.0f;
  1028. state->Late.DensityGain = 0.0f;
  1029. state->Late.ApFeedCoeff = 0.0f;
  1030. state->Late.MixCoeff = 0.0f;
  1031. for(index = 0;index < 4;index++)
  1032. {
  1033. state->Late.ApCoeff[index] = 0.0f;
  1034. state->Late.ApDelay[index].Mask = 0;
  1035. state->Late.ApDelay[index].Line = NULL;
  1036. state->Late.ApOffset[index] = 0;
  1037. state->Late.Coeff[index] = 0.0f;
  1038. state->Late.Delay[index].Mask = 0;
  1039. state->Late.Delay[index].Line = NULL;
  1040. state->Late.Offset[index] = 0;
  1041. state->Late.LpCoeff[index] = 0.0f;
  1042. state->Late.LpSample[index] = 0.0f;
  1043. }
  1044. for(index = 0;index < MaxChannels;index++)
  1045. {
  1046. state->Early.PanGain[index] = 0.0f;
  1047. state->Late.PanGain[index] = 0.0f;
  1048. }
  1049. state->Echo.DensityGain = 0.0f;
  1050. state->Echo.Delay.Mask = 0;
  1051. state->Echo.Delay.Line = NULL;
  1052. state->Echo.ApDelay.Mask = 0;
  1053. state->Echo.ApDelay.Line = NULL;
  1054. state->Echo.Coeff = 0.0f;
  1055. state->Echo.ApFeedCoeff = 0.0f;
  1056. state->Echo.ApCoeff = 0.0f;
  1057. state->Echo.Offset = 0;
  1058. state->Echo.ApOffset = 0;
  1059. state->Echo.LpCoeff = 0.0f;
  1060. state->Echo.LpSample = 0.0f;
  1061. state->Echo.MixCoeff[0] = 0.0f;
  1062. state->Echo.MixCoeff[1] = 0.0f;
  1063. state->Offset = 0;
  1064. state->Gain = state->Late.PanGain;
  1065. return STATIC_CAST(ALeffectState, state);
  1066. }
  1067. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALreverbStateFactory);
  1068. ALeffectStateFactory *ALreverbStateFactory_getFactory(void)
  1069. {
  1070. static ALreverbStateFactory ReverbFactory = { { GET_VTABLE2(ALreverbStateFactory, ALeffectStateFactory) } };
  1071. return STATIC_CAST(ALeffectStateFactory, &ReverbFactory);
  1072. }
  1073. void ALeaxreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  1074. {
  1075. ALeffectProps *props = &effect->Props;
  1076. switch(param)
  1077. {
  1078. case AL_EAXREVERB_DECAY_HFLIMIT:
  1079. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
  1080. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1081. props->Reverb.DecayHFLimit = val;
  1082. break;
  1083. default:
  1084. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1085. }
  1086. }
  1087. void ALeaxreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  1088. {
  1089. ALeaxreverb_setParami(effect, context, param, vals[0]);
  1090. }
  1091. void ALeaxreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  1092. {
  1093. ALeffectProps *props = &effect->Props;
  1094. switch(param)
  1095. {
  1096. case AL_EAXREVERB_DENSITY:
  1097. if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
  1098. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1099. props->Reverb.Density = val;
  1100. break;
  1101. case AL_EAXREVERB_DIFFUSION:
  1102. if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
  1103. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1104. props->Reverb.Diffusion = val;
  1105. break;
  1106. case AL_EAXREVERB_GAIN:
  1107. if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
  1108. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1109. props->Reverb.Gain = val;
  1110. break;
  1111. case AL_EAXREVERB_GAINHF:
  1112. if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
  1113. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1114. props->Reverb.GainHF = val;
  1115. break;
  1116. case AL_EAXREVERB_GAINLF:
  1117. if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
  1118. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1119. props->Reverb.GainLF = val;
  1120. break;
  1121. case AL_EAXREVERB_DECAY_TIME:
  1122. if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
  1123. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1124. props->Reverb.DecayTime = val;
  1125. break;
  1126. case AL_EAXREVERB_DECAY_HFRATIO:
  1127. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
  1128. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1129. props->Reverb.DecayHFRatio = val;
  1130. break;
  1131. case AL_EAXREVERB_DECAY_LFRATIO:
  1132. if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
  1133. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1134. props->Reverb.DecayLFRatio = val;
  1135. break;
  1136. case AL_EAXREVERB_REFLECTIONS_GAIN:
  1137. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
  1138. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1139. props->Reverb.ReflectionsGain = val;
  1140. break;
  1141. case AL_EAXREVERB_REFLECTIONS_DELAY:
  1142. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
  1143. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1144. props->Reverb.ReflectionsDelay = val;
  1145. break;
  1146. case AL_EAXREVERB_LATE_REVERB_GAIN:
  1147. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
  1148. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1149. props->Reverb.LateReverbGain = val;
  1150. break;
  1151. case AL_EAXREVERB_LATE_REVERB_DELAY:
  1152. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
  1153. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1154. props->Reverb.LateReverbDelay = val;
  1155. break;
  1156. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  1157. if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
  1158. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1159. props->Reverb.AirAbsorptionGainHF = val;
  1160. break;
  1161. case AL_EAXREVERB_ECHO_TIME:
  1162. if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
  1163. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1164. props->Reverb.EchoTime = val;
  1165. break;
  1166. case AL_EAXREVERB_ECHO_DEPTH:
  1167. if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
  1168. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1169. props->Reverb.EchoDepth = val;
  1170. break;
  1171. case AL_EAXREVERB_MODULATION_TIME:
  1172. if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
  1173. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1174. props->Reverb.ModulationTime = val;
  1175. break;
  1176. case AL_EAXREVERB_MODULATION_DEPTH:
  1177. if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
  1178. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1179. props->Reverb.ModulationDepth = val;
  1180. break;
  1181. case AL_EAXREVERB_HFREFERENCE:
  1182. if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
  1183. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1184. props->Reverb.HFReference = val;
  1185. break;
  1186. case AL_EAXREVERB_LFREFERENCE:
  1187. if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
  1188. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1189. props->Reverb.LFReference = val;
  1190. break;
  1191. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  1192. if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
  1193. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1194. props->Reverb.RoomRolloffFactor = val;
  1195. break;
  1196. default:
  1197. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1198. }
  1199. }
  1200. void ALeaxreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  1201. {
  1202. ALeffectProps *props = &effect->Props;
  1203. switch(param)
  1204. {
  1205. case AL_EAXREVERB_REFLECTIONS_PAN:
  1206. if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
  1207. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1208. LockContext(context);
  1209. props->Reverb.ReflectionsPan[0] = vals[0];
  1210. props->Reverb.ReflectionsPan[1] = vals[1];
  1211. props->Reverb.ReflectionsPan[2] = vals[2];
  1212. UnlockContext(context);
  1213. break;
  1214. case AL_EAXREVERB_LATE_REVERB_PAN:
  1215. if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
  1216. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1217. LockContext(context);
  1218. props->Reverb.LateReverbPan[0] = vals[0];
  1219. props->Reverb.LateReverbPan[1] = vals[1];
  1220. props->Reverb.LateReverbPan[2] = vals[2];
  1221. UnlockContext(context);
  1222. break;
  1223. default:
  1224. ALeaxreverb_setParamf(effect, context, param, vals[0]);
  1225. break;
  1226. }
  1227. }
  1228. void ALeaxreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  1229. {
  1230. const ALeffectProps *props = &effect->Props;
  1231. switch(param)
  1232. {
  1233. case AL_EAXREVERB_DECAY_HFLIMIT:
  1234. *val = props->Reverb.DecayHFLimit;
  1235. break;
  1236. default:
  1237. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1238. }
  1239. }
  1240. void ALeaxreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  1241. {
  1242. ALeaxreverb_getParami(effect, context, param, vals);
  1243. }
  1244. void ALeaxreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  1245. {
  1246. const ALeffectProps *props = &effect->Props;
  1247. switch(param)
  1248. {
  1249. case AL_EAXREVERB_DENSITY:
  1250. *val = props->Reverb.Density;
  1251. break;
  1252. case AL_EAXREVERB_DIFFUSION:
  1253. *val = props->Reverb.Diffusion;
  1254. break;
  1255. case AL_EAXREVERB_GAIN:
  1256. *val = props->Reverb.Gain;
  1257. break;
  1258. case AL_EAXREVERB_GAINHF:
  1259. *val = props->Reverb.GainHF;
  1260. break;
  1261. case AL_EAXREVERB_GAINLF:
  1262. *val = props->Reverb.GainLF;
  1263. break;
  1264. case AL_EAXREVERB_DECAY_TIME:
  1265. *val = props->Reverb.DecayTime;
  1266. break;
  1267. case AL_EAXREVERB_DECAY_HFRATIO:
  1268. *val = props->Reverb.DecayHFRatio;
  1269. break;
  1270. case AL_EAXREVERB_DECAY_LFRATIO:
  1271. *val = props->Reverb.DecayLFRatio;
  1272. break;
  1273. case AL_EAXREVERB_REFLECTIONS_GAIN:
  1274. *val = props->Reverb.ReflectionsGain;
  1275. break;
  1276. case AL_EAXREVERB_REFLECTIONS_DELAY:
  1277. *val = props->Reverb.ReflectionsDelay;
  1278. break;
  1279. case AL_EAXREVERB_LATE_REVERB_GAIN:
  1280. *val = props->Reverb.LateReverbGain;
  1281. break;
  1282. case AL_EAXREVERB_LATE_REVERB_DELAY:
  1283. *val = props->Reverb.LateReverbDelay;
  1284. break;
  1285. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  1286. *val = props->Reverb.AirAbsorptionGainHF;
  1287. break;
  1288. case AL_EAXREVERB_ECHO_TIME:
  1289. *val = props->Reverb.EchoTime;
  1290. break;
  1291. case AL_EAXREVERB_ECHO_DEPTH:
  1292. *val = props->Reverb.EchoDepth;
  1293. break;
  1294. case AL_EAXREVERB_MODULATION_TIME:
  1295. *val = props->Reverb.ModulationTime;
  1296. break;
  1297. case AL_EAXREVERB_MODULATION_DEPTH:
  1298. *val = props->Reverb.ModulationDepth;
  1299. break;
  1300. case AL_EAXREVERB_HFREFERENCE:
  1301. *val = props->Reverb.HFReference;
  1302. break;
  1303. case AL_EAXREVERB_LFREFERENCE:
  1304. *val = props->Reverb.LFReference;
  1305. break;
  1306. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  1307. *val = props->Reverb.RoomRolloffFactor;
  1308. break;
  1309. default:
  1310. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1311. }
  1312. }
  1313. void ALeaxreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  1314. {
  1315. const ALeffectProps *props = &effect->Props;
  1316. switch(param)
  1317. {
  1318. case AL_EAXREVERB_REFLECTIONS_PAN:
  1319. LockContext(context);
  1320. vals[0] = props->Reverb.ReflectionsPan[0];
  1321. vals[1] = props->Reverb.ReflectionsPan[1];
  1322. vals[2] = props->Reverb.ReflectionsPan[2];
  1323. UnlockContext(context);
  1324. break;
  1325. case AL_EAXREVERB_LATE_REVERB_PAN:
  1326. LockContext(context);
  1327. vals[0] = props->Reverb.LateReverbPan[0];
  1328. vals[1] = props->Reverb.LateReverbPan[1];
  1329. vals[2] = props->Reverb.LateReverbPan[2];
  1330. UnlockContext(context);
  1331. break;
  1332. default:
  1333. ALeaxreverb_getParamf(effect, context, param, vals);
  1334. break;
  1335. }
  1336. }
  1337. DEFINE_ALEFFECT_VTABLE(ALeaxreverb);
  1338. void ALreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  1339. {
  1340. ALeffectProps *props = &effect->Props;
  1341. switch(param)
  1342. {
  1343. case AL_REVERB_DECAY_HFLIMIT:
  1344. if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
  1345. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1346. props->Reverb.DecayHFLimit = val;
  1347. break;
  1348. default:
  1349. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1350. }
  1351. }
  1352. void ALreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  1353. {
  1354. ALreverb_setParami(effect, context, param, vals[0]);
  1355. }
  1356. void ALreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  1357. {
  1358. ALeffectProps *props = &effect->Props;
  1359. switch(param)
  1360. {
  1361. case AL_REVERB_DENSITY:
  1362. if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
  1363. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1364. props->Reverb.Density = val;
  1365. break;
  1366. case AL_REVERB_DIFFUSION:
  1367. if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
  1368. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1369. props->Reverb.Diffusion = val;
  1370. break;
  1371. case AL_REVERB_GAIN:
  1372. if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
  1373. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1374. props->Reverb.Gain = val;
  1375. break;
  1376. case AL_REVERB_GAINHF:
  1377. if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
  1378. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1379. props->Reverb.GainHF = val;
  1380. break;
  1381. case AL_REVERB_DECAY_TIME:
  1382. if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
  1383. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1384. props->Reverb.DecayTime = val;
  1385. break;
  1386. case AL_REVERB_DECAY_HFRATIO:
  1387. if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
  1388. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1389. props->Reverb.DecayHFRatio = val;
  1390. break;
  1391. case AL_REVERB_REFLECTIONS_GAIN:
  1392. if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
  1393. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1394. props->Reverb.ReflectionsGain = val;
  1395. break;
  1396. case AL_REVERB_REFLECTIONS_DELAY:
  1397. if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
  1398. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1399. props->Reverb.ReflectionsDelay = val;
  1400. break;
  1401. case AL_REVERB_LATE_REVERB_GAIN:
  1402. if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
  1403. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1404. props->Reverb.LateReverbGain = val;
  1405. break;
  1406. case AL_REVERB_LATE_REVERB_DELAY:
  1407. if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
  1408. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1409. props->Reverb.LateReverbDelay = val;
  1410. break;
  1411. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  1412. if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
  1413. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1414. props->Reverb.AirAbsorptionGainHF = val;
  1415. break;
  1416. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  1417. if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
  1418. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  1419. props->Reverb.RoomRolloffFactor = val;
  1420. break;
  1421. default:
  1422. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1423. }
  1424. }
  1425. void ALreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  1426. {
  1427. ALreverb_setParamf(effect, context, param, vals[0]);
  1428. }
  1429. void ALreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  1430. {
  1431. const ALeffectProps *props = &effect->Props;
  1432. switch(param)
  1433. {
  1434. case AL_REVERB_DECAY_HFLIMIT:
  1435. *val = props->Reverb.DecayHFLimit;
  1436. break;
  1437. default:
  1438. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1439. }
  1440. }
  1441. void ALreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  1442. {
  1443. ALreverb_getParami(effect, context, param, vals);
  1444. }
  1445. void ALreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  1446. {
  1447. const ALeffectProps *props = &effect->Props;
  1448. switch(param)
  1449. {
  1450. case AL_REVERB_DENSITY:
  1451. *val = props->Reverb.Density;
  1452. break;
  1453. case AL_REVERB_DIFFUSION:
  1454. *val = props->Reverb.Diffusion;
  1455. break;
  1456. case AL_REVERB_GAIN:
  1457. *val = props->Reverb.Gain;
  1458. break;
  1459. case AL_REVERB_GAINHF:
  1460. *val = props->Reverb.GainHF;
  1461. break;
  1462. case AL_REVERB_DECAY_TIME:
  1463. *val = props->Reverb.DecayTime;
  1464. break;
  1465. case AL_REVERB_DECAY_HFRATIO:
  1466. *val = props->Reverb.DecayHFRatio;
  1467. break;
  1468. case AL_REVERB_REFLECTIONS_GAIN:
  1469. *val = props->Reverb.ReflectionsGain;
  1470. break;
  1471. case AL_REVERB_REFLECTIONS_DELAY:
  1472. *val = props->Reverb.ReflectionsDelay;
  1473. break;
  1474. case AL_REVERB_LATE_REVERB_GAIN:
  1475. *val = props->Reverb.LateReverbGain;
  1476. break;
  1477. case AL_REVERB_LATE_REVERB_DELAY:
  1478. *val = props->Reverb.LateReverbDelay;
  1479. break;
  1480. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  1481. *val = props->Reverb.AirAbsorptionGainHF;
  1482. break;
  1483. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  1484. *val = props->Reverb.RoomRolloffFactor;
  1485. break;
  1486. default:
  1487. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  1488. }
  1489. }
  1490. void ALreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  1491. {
  1492. ALreverb_getParamf(effect, context, param, vals);
  1493. }
  1494. DEFINE_ALEFFECT_VTABLE(ALreverb);