reverb.c 66 KB

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