alcReverb.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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 "AL/al.h"
  25. #include "AL/alc.h"
  26. #include "alMain.h"
  27. #include "alAuxEffectSlot.h"
  28. #include "alEffect.h"
  29. #include "alError.h"
  30. #include "alu.h"
  31. typedef struct DelayLine
  32. {
  33. // The delay lines use sample lengths that are powers of 2 to allow the
  34. // use of bit-masking instead of a modulus for wrapping.
  35. ALuint Mask;
  36. ALfloat *Line;
  37. } DelayLine;
  38. typedef struct ALverbState {
  39. // Must be first in all effects!
  40. ALeffectState state;
  41. // All delay lines are allocated as a single buffer to reduce memory
  42. // fragmentation and management code.
  43. ALfloat *SampleBuffer;
  44. ALuint TotalSamples;
  45. // Master effect low-pass filter (2 chained 1-pole filters).
  46. FILTER LpFilter;
  47. ALfloat LpHistory[2];
  48. struct {
  49. // Modulator delay line.
  50. DelayLine Delay;
  51. // The vibrato time is tracked with an index over a modulus-wrapped
  52. // range (in samples).
  53. ALuint Index;
  54. ALuint Range;
  55. // The depth of frequency change (also in samples) and its filter.
  56. ALfloat Depth;
  57. ALfloat Coeff;
  58. ALfloat Filter;
  59. } Mod;
  60. // Initial effect delay.
  61. DelayLine Delay;
  62. // The tap points for the initial delay. First tap goes to early
  63. // reflections, the last to late reverb.
  64. ALuint DelayTap[2];
  65. struct {
  66. // Output gain for early reflections.
  67. ALfloat Gain;
  68. // Early reflections are done with 4 delay lines.
  69. ALfloat Coeff[4];
  70. DelayLine Delay[4];
  71. ALuint Offset[4];
  72. // The gain for each output channel based on 3D panning (only for the
  73. // EAX path).
  74. ALfloat PanGain[MAXCHANNELS];
  75. } Early;
  76. // Decorrelator delay line.
  77. DelayLine Decorrelator;
  78. // There are actually 4 decorrelator taps, but the first occurs at the
  79. // initial sample.
  80. ALuint DecoTap[3];
  81. struct {
  82. // Output gain for late reverb.
  83. ALfloat Gain;
  84. // Attenuation to compensate for the modal density and decay rate of
  85. // the late lines.
  86. ALfloat DensityGain;
  87. // The feed-back and feed-forward all-pass coefficient.
  88. ALfloat ApFeedCoeff;
  89. // Mixing matrix coefficient.
  90. ALfloat MixCoeff;
  91. // Late reverb has 4 parallel all-pass filters.
  92. ALfloat ApCoeff[4];
  93. DelayLine ApDelay[4];
  94. ALuint ApOffset[4];
  95. // In addition to 4 cyclical delay lines.
  96. ALfloat Coeff[4];
  97. DelayLine Delay[4];
  98. ALuint Offset[4];
  99. // The cyclical delay lines are 1-pole low-pass filtered.
  100. ALfloat LpCoeff[4];
  101. ALfloat LpSample[4];
  102. // The gain for each output channel based on 3D panning (only for the
  103. // EAX path).
  104. ALfloat PanGain[MAXCHANNELS];
  105. } Late;
  106. struct {
  107. // Attenuation to compensate for the modal density and decay rate of
  108. // the echo line.
  109. ALfloat DensityGain;
  110. // Echo delay and all-pass lines.
  111. DelayLine Delay;
  112. DelayLine ApDelay;
  113. ALfloat Coeff;
  114. ALfloat ApFeedCoeff;
  115. ALfloat ApCoeff;
  116. ALuint Offset;
  117. ALuint ApOffset;
  118. // The echo line is 1-pole low-pass filtered.
  119. ALfloat LpCoeff;
  120. ALfloat LpSample;
  121. // Echo mixing coefficients.
  122. ALfloat MixCoeff[2];
  123. } Echo;
  124. // The current read offset for all delay lines.
  125. ALuint Offset;
  126. // The gain for each output channel (non-EAX path only; aliased from
  127. // Late.PanGain)
  128. ALfloat *Gain;
  129. } ALverbState;
  130. /* This is a user config option for modifying the overall output of the reverb
  131. * effect.
  132. */
  133. ALfloat ReverbBoost = 1.0f;
  134. /* Specifies whether to use a standard reverb effect in place of EAX reverb */
  135. ALboolean EmulateEAXReverb = AL_FALSE;
  136. /* This coefficient is used to define the maximum frequency range controlled
  137. * by the modulation depth. The current value of 0.1 will allow it to swing
  138. * from 0.9x to 1.1x. This value must be below 1. At 1 it will cause the
  139. * sampler to stall on the downswing, and above 1 it will cause it to sample
  140. * backwards.
  141. */
  142. static const ALfloat MODULATION_DEPTH_COEFF = 0.1f;
  143. /* A filter is used to avoid the terrible distortion caused by changing
  144. * modulation time and/or depth. To be consistent across different sample
  145. * rates, the coefficient must be raised to a constant divided by the sample
  146. * rate: coeff^(constant / rate).
  147. */
  148. static const ALfloat MODULATION_FILTER_COEFF = 0.048f;
  149. static const ALfloat MODULATION_FILTER_CONST = 100000.0f;
  150. // When diffusion is above 0, an all-pass filter is used to take the edge off
  151. // the echo effect. It uses the following line length (in seconds).
  152. static const ALfloat ECHO_ALLPASS_LENGTH = 0.0133f;
  153. // Input into the late reverb is decorrelated between four channels. Their
  154. // timings are dependent on a fraction and multiplier. See the
  155. // UpdateDecorrelator() routine for the calculations involved.
  156. static const ALfloat DECO_FRACTION = 0.15f;
  157. static const ALfloat DECO_MULTIPLIER = 2.0f;
  158. // All delay line lengths are specified in seconds.
  159. // The lengths of the early delay lines.
  160. static const ALfloat EARLY_LINE_LENGTH[4] =
  161. {
  162. 0.0015f, 0.0045f, 0.0135f, 0.0405f
  163. };
  164. // The lengths of the late all-pass delay lines.
  165. static const ALfloat ALLPASS_LINE_LENGTH[4] =
  166. {
  167. 0.0151f, 0.0167f, 0.0183f, 0.0200f,
  168. };
  169. // The lengths of the late cyclical delay lines.
  170. static const ALfloat LATE_LINE_LENGTH[4] =
  171. {
  172. 0.0211f, 0.0311f, 0.0461f, 0.0680f
  173. };
  174. // The late cyclical delay lines have a variable length dependent on the
  175. // effect's density parameter (inverted for some reason) and this multiplier.
  176. static const ALfloat LATE_LINE_MULTIPLIER = 4.0f;
  177. // Basic delay line input/output routines.
  178. static __inline ALfloat DelayLineOut(DelayLine *Delay, ALuint offset)
  179. {
  180. return Delay->Line[offset&Delay->Mask];
  181. }
  182. static __inline ALvoid DelayLineIn(DelayLine *Delay, ALuint offset, ALfloat in)
  183. {
  184. Delay->Line[offset&Delay->Mask] = in;
  185. }
  186. // Attenuated delay line output routine.
  187. static __inline ALfloat AttenuatedDelayLineOut(DelayLine *Delay, ALuint offset, ALfloat coeff)
  188. {
  189. return coeff * Delay->Line[offset&Delay->Mask];
  190. }
  191. // Basic attenuated all-pass input/output routine.
  192. static __inline ALfloat AllpassInOut(DelayLine *Delay, ALuint outOffset, ALuint inOffset, ALfloat in, ALfloat feedCoeff, ALfloat coeff)
  193. {
  194. ALfloat out, feed;
  195. out = DelayLineOut(Delay, outOffset);
  196. feed = feedCoeff * in;
  197. DelayLineIn(Delay, inOffset, (feedCoeff * (out - feed)) + in);
  198. // The time-based attenuation is only applied to the delay output to
  199. // keep it from affecting the feed-back path (which is already controlled
  200. // by the all-pass feed coefficient).
  201. return (coeff * out) - feed;
  202. }
  203. // Given an input sample, this function produces modulation for the late
  204. // reverb.
  205. static __inline ALfloat EAXModulation(ALverbState *State, ALfloat in)
  206. {
  207. ALfloat sinus, frac;
  208. ALuint offset;
  209. ALfloat out0, out1;
  210. // Calculate the sinus rythm (dependent on modulation time and the
  211. // sampling rate). The center of the sinus is moved to reduce the delay
  212. // of the effect when the time or depth are low.
  213. sinus = 1.0f - aluCos(F_PI*2.0f * State->Mod.Index / State->Mod.Range);
  214. // The depth determines the range over which to read the input samples
  215. // from, so it must be filtered to reduce the distortion caused by even
  216. // small parameter changes.
  217. State->Mod.Filter = lerp(State->Mod.Filter, State->Mod.Depth,
  218. State->Mod.Coeff);
  219. // Calculate the read offset and fraction between it and the next sample.
  220. frac = (1.0f + (State->Mod.Filter * sinus));
  221. offset = fastf2u(frac);
  222. frac -= offset;
  223. // Get the two samples crossed by the offset, and feed the delay line
  224. // with the next input sample.
  225. out0 = DelayLineOut(&State->Mod.Delay, State->Offset - offset);
  226. out1 = DelayLineOut(&State->Mod.Delay, State->Offset - offset - 1);
  227. DelayLineIn(&State->Mod.Delay, State->Offset, in);
  228. // Step the modulation index forward, keeping it bound to its range.
  229. State->Mod.Index = (State->Mod.Index + 1) % State->Mod.Range;
  230. // The output is obtained by linearly interpolating the two samples that
  231. // were acquired above.
  232. return lerp(out0, out1, frac);
  233. }
  234. // Delay line output routine for early reflections.
  235. static __inline ALfloat EarlyDelayLineOut(ALverbState *State, ALuint index)
  236. {
  237. return AttenuatedDelayLineOut(&State->Early.Delay[index],
  238. State->Offset - State->Early.Offset[index],
  239. State->Early.Coeff[index]);
  240. }
  241. // Given an input sample, this function produces four-channel output for the
  242. // early reflections.
  243. static __inline ALvoid EarlyReflection(ALverbState *State, ALfloat in, ALfloat *out)
  244. {
  245. ALfloat d[4], v, f[4];
  246. // Obtain the decayed results of each early delay line.
  247. d[0] = EarlyDelayLineOut(State, 0);
  248. d[1] = EarlyDelayLineOut(State, 1);
  249. d[2] = EarlyDelayLineOut(State, 2);
  250. d[3] = EarlyDelayLineOut(State, 3);
  251. /* The following uses a lossless scattering junction from waveguide
  252. * theory. It actually amounts to a householder mixing matrix, which
  253. * will produce a maximally diffuse response, and means this can probably
  254. * be considered a simple feed-back delay network (FDN).
  255. * N
  256. * ---
  257. * \
  258. * v = 2/N / d_i
  259. * ---
  260. * i=1
  261. */
  262. v = (d[0] + d[1] + d[2] + d[3]) * 0.5f;
  263. // The junction is loaded with the input here.
  264. v += in;
  265. // Calculate the feed values for the delay lines.
  266. f[0] = v - d[0];
  267. f[1] = v - d[1];
  268. f[2] = v - d[2];
  269. f[3] = v - d[3];
  270. // Re-feed the delay lines.
  271. DelayLineIn(&State->Early.Delay[0], State->Offset, f[0]);
  272. DelayLineIn(&State->Early.Delay[1], State->Offset, f[1]);
  273. DelayLineIn(&State->Early.Delay[2], State->Offset, f[2]);
  274. DelayLineIn(&State->Early.Delay[3], State->Offset, f[3]);
  275. // Output the results of the junction for all four channels.
  276. out[0] = State->Early.Gain * f[0];
  277. out[1] = State->Early.Gain * f[1];
  278. out[2] = State->Early.Gain * f[2];
  279. out[3] = State->Early.Gain * f[3];
  280. }
  281. // All-pass input/output routine for late reverb.
  282. static __inline ALfloat LateAllPassInOut(ALverbState *State, ALuint index, ALfloat in)
  283. {
  284. return AllpassInOut(&State->Late.ApDelay[index],
  285. State->Offset - State->Late.ApOffset[index],
  286. State->Offset, in, State->Late.ApFeedCoeff,
  287. State->Late.ApCoeff[index]);
  288. }
  289. // Delay line output routine for late reverb.
  290. static __inline ALfloat LateDelayLineOut(ALverbState *State, ALuint index)
  291. {
  292. return AttenuatedDelayLineOut(&State->Late.Delay[index],
  293. State->Offset - State->Late.Offset[index],
  294. State->Late.Coeff[index]);
  295. }
  296. // Low-pass filter input/output routine for late reverb.
  297. static __inline ALfloat LateLowPassInOut(ALverbState *State, ALuint index, ALfloat in)
  298. {
  299. in = lerp(in, State->Late.LpSample[index], State->Late.LpCoeff[index]);
  300. State->Late.LpSample[index] = in;
  301. return in;
  302. }
  303. // Given four decorrelated input samples, this function produces four-channel
  304. // output for the late reverb.
  305. static __inline ALvoid LateReverb(ALverbState *State, ALfloat *in, ALfloat *out)
  306. {
  307. ALfloat d[4], f[4];
  308. // Obtain the decayed results of the cyclical delay lines, and add the
  309. // corresponding input channels. Then pass the results through the
  310. // low-pass filters.
  311. // This is where the feed-back cycles from line 0 to 1 to 3 to 2 and back
  312. // to 0.
  313. d[0] = LateLowPassInOut(State, 2, in[2] + LateDelayLineOut(State, 2));
  314. d[1] = LateLowPassInOut(State, 0, in[0] + LateDelayLineOut(State, 0));
  315. d[2] = LateLowPassInOut(State, 3, in[3] + LateDelayLineOut(State, 3));
  316. d[3] = LateLowPassInOut(State, 1, in[1] + LateDelayLineOut(State, 1));
  317. // To help increase diffusion, run each line through an all-pass filter.
  318. // When there is no diffusion, the shortest all-pass filter will feed the
  319. // shortest delay line.
  320. d[0] = LateAllPassInOut(State, 0, d[0]);
  321. d[1] = LateAllPassInOut(State, 1, d[1]);
  322. d[2] = LateAllPassInOut(State, 2, d[2]);
  323. d[3] = LateAllPassInOut(State, 3, d[3]);
  324. /* Late reverb is done with a modified feed-back delay network (FDN)
  325. * topology. Four input lines are each fed through their own all-pass
  326. * filter and then into the mixing matrix. The four outputs of the
  327. * mixing matrix are then cycled back to the inputs. Each output feeds
  328. * a different input to form a circlular feed cycle.
  329. *
  330. * The mixing matrix used is a 4D skew-symmetric rotation matrix derived
  331. * using a single unitary rotational parameter:
  332. *
  333. * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
  334. * [ -a, d, c, -b ]
  335. * [ -b, -c, d, a ]
  336. * [ -c, b, -a, d ]
  337. *
  338. * The rotation is constructed from the effect's diffusion parameter,
  339. * yielding: 1 = x^2 + 3 y^2; where a, b, and c are the coefficient y
  340. * with differing signs, and d is the coefficient x. The matrix is thus:
  341. *
  342. * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
  343. * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
  344. * [ y, -y, x, y ] x = cos(t)
  345. * [ -y, -y, -y, x ] y = sin(t) / n
  346. *
  347. * To reduce the number of multiplies, the x coefficient is applied with
  348. * the cyclical delay line coefficients. Thus only the y coefficient is
  349. * applied when mixing, and is modified to be: y / x.
  350. */
  351. f[0] = d[0] + (State->Late.MixCoeff * ( d[1] + -d[2] + d[3]));
  352. f[1] = d[1] + (State->Late.MixCoeff * (-d[0] + d[2] + d[3]));
  353. f[2] = d[2] + (State->Late.MixCoeff * ( d[0] + -d[1] + d[3]));
  354. f[3] = d[3] + (State->Late.MixCoeff * (-d[0] + -d[1] + -d[2] ));
  355. // Output the results of the matrix for all four channels, attenuated by
  356. // the late reverb gain (which is attenuated by the 'x' mix coefficient).
  357. out[0] = State->Late.Gain * f[0];
  358. out[1] = State->Late.Gain * f[1];
  359. out[2] = State->Late.Gain * f[2];
  360. out[3] = State->Late.Gain * f[3];
  361. // Re-feed the cyclical delay lines.
  362. DelayLineIn(&State->Late.Delay[0], State->Offset, f[0]);
  363. DelayLineIn(&State->Late.Delay[1], State->Offset, f[1]);
  364. DelayLineIn(&State->Late.Delay[2], State->Offset, f[2]);
  365. DelayLineIn(&State->Late.Delay[3], State->Offset, f[3]);
  366. }
  367. // Given an input sample, this function mixes echo into the four-channel late
  368. // reverb.
  369. static __inline ALvoid EAXEcho(ALverbState *State, ALfloat in, ALfloat *late)
  370. {
  371. ALfloat out, feed;
  372. // Get the latest attenuated echo sample for output.
  373. feed = AttenuatedDelayLineOut(&State->Echo.Delay,
  374. State->Offset - State->Echo.Offset,
  375. State->Echo.Coeff);
  376. // Mix the output into the late reverb channels.
  377. out = State->Echo.MixCoeff[0] * feed;
  378. late[0] = (State->Echo.MixCoeff[1] * late[0]) + out;
  379. late[1] = (State->Echo.MixCoeff[1] * late[1]) + out;
  380. late[2] = (State->Echo.MixCoeff[1] * late[2]) + out;
  381. late[3] = (State->Echo.MixCoeff[1] * late[3]) + out;
  382. // Mix the energy-attenuated input with the output and pass it through
  383. // the echo low-pass filter.
  384. feed += State->Echo.DensityGain * in;
  385. feed = lerp(feed, State->Echo.LpSample, State->Echo.LpCoeff);
  386. State->Echo.LpSample = feed;
  387. // Then the echo all-pass filter.
  388. feed = AllpassInOut(&State->Echo.ApDelay,
  389. State->Offset - State->Echo.ApOffset,
  390. State->Offset, feed, State->Echo.ApFeedCoeff,
  391. State->Echo.ApCoeff);
  392. // Feed the delay with the mixed and filtered sample.
  393. DelayLineIn(&State->Echo.Delay, State->Offset, feed);
  394. }
  395. // Perform the non-EAX reverb pass on a given input sample, resulting in
  396. // four-channel output.
  397. static __inline ALvoid VerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
  398. {
  399. ALfloat feed, taps[4];
  400. // Low-pass filter the incoming sample.
  401. in = lpFilter2P(&State->LpFilter, 0, in);
  402. // Feed the initial delay line.
  403. DelayLineIn(&State->Delay, State->Offset, in);
  404. // Calculate the early reflection from the first delay tap.
  405. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
  406. EarlyReflection(State, in, early);
  407. // Feed the decorrelator from the energy-attenuated output of the second
  408. // delay tap.
  409. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
  410. feed = in * State->Late.DensityGain;
  411. DelayLineIn(&State->Decorrelator, State->Offset, feed);
  412. // Calculate the late reverb from the decorrelator taps.
  413. taps[0] = feed;
  414. taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
  415. taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
  416. taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
  417. LateReverb(State, taps, late);
  418. // Step all delays forward one sample.
  419. State->Offset++;
  420. }
  421. // Perform the EAX reverb pass on a given input sample, resulting in four-
  422. // channel output.
  423. static __inline ALvoid EAXVerbPass(ALverbState *State, ALfloat in, ALfloat *early, ALfloat *late)
  424. {
  425. ALfloat feed, taps[4];
  426. // Low-pass filter the incoming sample.
  427. in = lpFilter2P(&State->LpFilter, 0, in);
  428. // Perform any modulation on the input.
  429. in = EAXModulation(State, in);
  430. // Feed the initial delay line.
  431. DelayLineIn(&State->Delay, State->Offset, in);
  432. // Calculate the early reflection from the first delay tap.
  433. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[0]);
  434. EarlyReflection(State, in, early);
  435. // Feed the decorrelator from the energy-attenuated output of the second
  436. // delay tap.
  437. in = DelayLineOut(&State->Delay, State->Offset - State->DelayTap[1]);
  438. feed = in * State->Late.DensityGain;
  439. DelayLineIn(&State->Decorrelator, State->Offset, feed);
  440. // Calculate the late reverb from the decorrelator taps.
  441. taps[0] = feed;
  442. taps[1] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[0]);
  443. taps[2] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[1]);
  444. taps[3] = DelayLineOut(&State->Decorrelator, State->Offset - State->DecoTap[2]);
  445. LateReverb(State, taps, late);
  446. // Calculate and mix in any echo.
  447. EAXEcho(State, in, late);
  448. // Step all delays forward one sample.
  449. State->Offset++;
  450. }
  451. // This processes the reverb state, given the input samples and an output
  452. // buffer.
  453. static ALvoid VerbProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
  454. {
  455. ALverbState *State = (ALverbState*)effect;
  456. ALuint index, c;
  457. ALfloat early[4], late[4], out[4];
  458. const ALfloat *panGain = State->Gain;
  459. for(index = 0;index < SamplesToDo;index++)
  460. {
  461. // Process reverb for this sample.
  462. VerbPass(State, SamplesIn[index], early, late);
  463. // Mix early reflections and late reverb.
  464. out[0] = (early[0] + late[0]);
  465. out[1] = (early[1] + late[1]);
  466. out[2] = (early[2] + late[2]);
  467. out[3] = (early[3] + late[3]);
  468. // Output the results.
  469. for(c = 0;c < MAXCHANNELS;c++)
  470. SamplesOut[index][c] += panGain[c] * out[c&3];
  471. }
  472. }
  473. // This processes the EAX reverb state, given the input samples and an output
  474. // buffer.
  475. static ALvoid EAXVerbProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
  476. {
  477. ALverbState *State = (ALverbState*)effect;
  478. ALuint index, c;
  479. ALfloat early[4], late[4];
  480. for(index = 0;index < SamplesToDo;index++)
  481. {
  482. // Process reverb for this sample.
  483. EAXVerbPass(State, SamplesIn[index], early, late);
  484. for(c = 0;c < MAXCHANNELS;c++)
  485. SamplesOut[index][c] += State->Early.PanGain[c]*early[c&3] +
  486. State->Late.PanGain[c]*late[c&3];
  487. }
  488. }
  489. // Given the allocated sample buffer, this function updates each delay line
  490. // offset.
  491. static __inline ALvoid RealizeLineOffset(ALfloat * sampleBuffer, DelayLine *Delay)
  492. {
  493. Delay->Line = &sampleBuffer[(ALintptrEXT)Delay->Line];
  494. }
  495. // Calculate the length of a delay line and store its mask and offset.
  496. static ALuint CalcLineLength(ALfloat length, ALintptrEXT offset, ALuint frequency, DelayLine *Delay)
  497. {
  498. ALuint samples;
  499. // All line lengths are powers of 2, calculated from their lengths, with
  500. // an additional sample in case of rounding errors.
  501. samples = NextPowerOf2(fastf2u(length * frequency) + 1);
  502. // All lines share a single sample buffer.
  503. Delay->Mask = samples - 1;
  504. Delay->Line = (ALfloat*)offset;
  505. // Return the sample count for accumulation.
  506. return samples;
  507. }
  508. /* Calculates the delay line metrics and allocates the shared sample buffer
  509. * for all lines given the sample rate (frequency). If an allocation failure
  510. * occurs, it returns AL_FALSE.
  511. */
  512. static ALboolean AllocLines(ALuint frequency, ALverbState *State)
  513. {
  514. ALuint totalSamples, index;
  515. ALfloat length;
  516. ALfloat *newBuffer = NULL;
  517. // All delay line lengths are calculated to accomodate the full range of
  518. // lengths given their respective paramters.
  519. totalSamples = 0;
  520. /* The modulator's line length is calculated from the maximum modulation
  521. * time and depth coefficient, and halfed for the low-to-high frequency
  522. * swing. An additional sample is added to keep it stable when there is no
  523. * modulation.
  524. */
  525. length = (AL_EAXREVERB_MAX_MODULATION_TIME*MODULATION_DEPTH_COEFF/2.0f) +
  526. (1.0f / frequency);
  527. totalSamples += CalcLineLength(length, totalSamples, frequency,
  528. &State->Mod.Delay);
  529. // The initial delay is the sum of the reflections and late reverb
  530. // delays.
  531. length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
  532. AL_EAXREVERB_MAX_LATE_REVERB_DELAY;
  533. totalSamples += CalcLineLength(length, totalSamples, frequency,
  534. &State->Delay);
  535. // The early reflection lines.
  536. for(index = 0;index < 4;index++)
  537. totalSamples += CalcLineLength(EARLY_LINE_LENGTH[index], totalSamples,
  538. frequency, &State->Early.Delay[index]);
  539. // The decorrelator line is calculated from the lowest reverb density (a
  540. // parameter value of 1).
  541. length = (DECO_FRACTION * DECO_MULTIPLIER * DECO_MULTIPLIER) *
  542. LATE_LINE_LENGTH[0] * (1.0f + LATE_LINE_MULTIPLIER);
  543. totalSamples += CalcLineLength(length, totalSamples, frequency,
  544. &State->Decorrelator);
  545. // The late all-pass lines.
  546. for(index = 0;index < 4;index++)
  547. totalSamples += CalcLineLength(ALLPASS_LINE_LENGTH[index], totalSamples,
  548. frequency, &State->Late.ApDelay[index]);
  549. // The late delay lines are calculated from the lowest reverb density.
  550. for(index = 0;index < 4;index++)
  551. {
  552. length = LATE_LINE_LENGTH[index] * (1.0f + LATE_LINE_MULTIPLIER);
  553. totalSamples += CalcLineLength(length, totalSamples, frequency,
  554. &State->Late.Delay[index]);
  555. }
  556. // The echo all-pass and delay lines.
  557. totalSamples += CalcLineLength(ECHO_ALLPASS_LENGTH, totalSamples,
  558. frequency, &State->Echo.ApDelay);
  559. totalSamples += CalcLineLength(AL_EAXREVERB_MAX_ECHO_TIME, totalSamples,
  560. frequency, &State->Echo.Delay);
  561. if(totalSamples != State->TotalSamples)
  562. {
  563. TRACE("New reverb buffer length: %u samples (%f sec)\n", totalSamples, totalSamples/(float)frequency);
  564. newBuffer = realloc(State->SampleBuffer, sizeof(ALfloat) * totalSamples);
  565. if(newBuffer == NULL)
  566. return AL_FALSE;
  567. State->SampleBuffer = newBuffer;
  568. State->TotalSamples = totalSamples;
  569. }
  570. // Update all delays to reflect the new sample buffer.
  571. RealizeLineOffset(State->SampleBuffer, &State->Delay);
  572. RealizeLineOffset(State->SampleBuffer, &State->Decorrelator);
  573. for(index = 0;index < 4;index++)
  574. {
  575. RealizeLineOffset(State->SampleBuffer, &State->Early.Delay[index]);
  576. RealizeLineOffset(State->SampleBuffer, &State->Late.ApDelay[index]);
  577. RealizeLineOffset(State->SampleBuffer, &State->Late.Delay[index]);
  578. }
  579. RealizeLineOffset(State->SampleBuffer, &State->Mod.Delay);
  580. RealizeLineOffset(State->SampleBuffer, &State->Echo.ApDelay);
  581. RealizeLineOffset(State->SampleBuffer, &State->Echo.Delay);
  582. // Clear the sample buffer.
  583. for(index = 0;index < State->TotalSamples;index++)
  584. State->SampleBuffer[index] = 0.0f;
  585. return AL_TRUE;
  586. }
  587. // This updates the device-dependant EAX reverb state. This is called on
  588. // initialization and any time the device parameters (eg. playback frequency,
  589. // format) have been changed.
  590. static ALboolean ReverbDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
  591. {
  592. ALverbState *State = (ALverbState*)effect;
  593. ALuint frequency = Device->Frequency, index;
  594. // Allocate the delay lines.
  595. if(!AllocLines(frequency, State))
  596. return AL_FALSE;
  597. // Calculate the modulation filter coefficient. Notice that the exponent
  598. // is calculated given the current sample rate. This ensures that the
  599. // resulting filter response over time is consistent across all sample
  600. // rates.
  601. State->Mod.Coeff = aluPow(MODULATION_FILTER_COEFF,
  602. MODULATION_FILTER_CONST / frequency);
  603. // The early reflection and late all-pass filter line lengths are static,
  604. // so their offsets only need to be calculated once.
  605. for(index = 0;index < 4;index++)
  606. {
  607. State->Early.Offset[index] = fastf2u(EARLY_LINE_LENGTH[index] *
  608. frequency);
  609. State->Late.ApOffset[index] = fastf2u(ALLPASS_LINE_LENGTH[index] *
  610. frequency);
  611. }
  612. // The echo all-pass filter line length is static, so its offset only
  613. // needs to be calculated once.
  614. State->Echo.ApOffset = fastf2u(ECHO_ALLPASS_LENGTH * frequency);
  615. return AL_TRUE;
  616. }
  617. // Calculate a decay coefficient given the length of each cycle and the time
  618. // until the decay reaches -60 dB.
  619. static __inline ALfloat CalcDecayCoeff(ALfloat length, ALfloat decayTime)
  620. {
  621. return aluPow(0.001f/*-60 dB*/, length/decayTime);
  622. }
  623. // Calculate a decay length from a coefficient and the time until the decay
  624. // reaches -60 dB.
  625. static __inline ALfloat CalcDecayLength(ALfloat coeff, ALfloat decayTime)
  626. {
  627. return aluLog10(coeff) * decayTime / aluLog10(0.001f)/*-60 dB*/;
  628. }
  629. // Calculate the high frequency parameter for the I3DL2 coefficient
  630. // calculation.
  631. static __inline ALfloat CalcI3DL2HFreq(ALfloat hfRef, ALuint frequency)
  632. {
  633. return aluCos(F_PI*2.0f * hfRef / frequency);
  634. }
  635. // Calculate an attenuation to be applied to the input of any echo models to
  636. // compensate for modal density and decay time.
  637. static __inline ALfloat CalcDensityGain(ALfloat a)
  638. {
  639. /* The energy of a signal can be obtained by finding the area under the
  640. * squared signal. This takes the form of Sum(x_n^2), where x is the
  641. * amplitude for the sample n.
  642. *
  643. * Decaying feedback matches exponential decay of the form Sum(a^n),
  644. * where a is the attenuation coefficient, and n is the sample. The area
  645. * under this decay curve can be calculated as: 1 / (1 - a).
  646. *
  647. * Modifying the above equation to find the squared area under the curve
  648. * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
  649. * calculated by inverting the square root of this approximation,
  650. * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
  651. */
  652. return aluSqrt(1.0f - (a * a));
  653. }
  654. // Calculate the mixing matrix coefficients given a diffusion factor.
  655. static __inline ALvoid CalcMatrixCoeffs(ALfloat diffusion, ALfloat *x, ALfloat *y)
  656. {
  657. ALfloat n, t;
  658. // The matrix is of order 4, so n is sqrt (4 - 1).
  659. n = aluSqrt(3.0f);
  660. t = diffusion * aluAtan(n);
  661. // Calculate the first mixing matrix coefficient.
  662. *x = aluCos(t);
  663. // Calculate the second mixing matrix coefficient.
  664. *y = aluSin(t) / n;
  665. }
  666. // Calculate the limited HF ratio for use with the late reverb low-pass
  667. // filters.
  668. static ALfloat CalcLimitedHfRatio(ALfloat hfRatio, ALfloat airAbsorptionGainHF, ALfloat decayTime)
  669. {
  670. ALfloat limitRatio;
  671. /* Find the attenuation due to air absorption in dB (converting delay
  672. * time to meters using the speed of sound). Then reversing the decay
  673. * equation, solve for HF ratio. The delay length is cancelled out of
  674. * the equation, so it can be calculated once for all lines.
  675. */
  676. limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) *
  677. SPEEDOFSOUNDMETRESPERSEC);
  678. /* Using the limit calculated above, apply the upper bound to the HF
  679. * ratio. Also need to limit the result to a minimum of 0.1, just like the
  680. * HF ratio parameter. */
  681. return clampf(limitRatio, 0.1f, hfRatio);
  682. }
  683. // Calculate the coefficient for a HF (and eventually LF) decay damping
  684. // filter.
  685. static __inline ALfloat CalcDampingCoeff(ALfloat hfRatio, ALfloat length, ALfloat decayTime, ALfloat decayCoeff, ALfloat cw)
  686. {
  687. ALfloat coeff, g;
  688. // Eventually this should boost the high frequencies when the ratio
  689. // exceeds 1.
  690. coeff = 0.0f;
  691. if (hfRatio < 1.0f)
  692. {
  693. // Calculate the low-pass coefficient by dividing the HF decay
  694. // coefficient by the full decay coefficient.
  695. g = CalcDecayCoeff(length, decayTime * hfRatio) / decayCoeff;
  696. // Damping is done with a 1-pole filter, so g needs to be squared.
  697. g *= g;
  698. coeff = lpCoeffCalc(g, cw);
  699. // Very low decay times will produce minimal output, so apply an
  700. // upper bound to the coefficient.
  701. coeff = minf(coeff, 0.98f);
  702. }
  703. return coeff;
  704. }
  705. // Update the EAX modulation index, range, and depth. Keep in mind that this
  706. // kind of vibrato is additive and not multiplicative as one may expect. The
  707. // downswing will sound stronger than the upswing.
  708. static ALvoid UpdateModulator(ALfloat modTime, ALfloat modDepth, ALuint frequency, ALverbState *State)
  709. {
  710. ALuint range;
  711. /* Modulation is calculated in two parts.
  712. *
  713. * The modulation time effects the sinus applied to the change in
  714. * frequency. An index out of the current time range (both in samples)
  715. * is incremented each sample. The range is bound to a reasonable
  716. * minimum (1 sample) and when the timing changes, the index is rescaled
  717. * to the new range (to keep the sinus consistent).
  718. */
  719. range = maxu(fastf2u(modTime*frequency), 1);
  720. State->Mod.Index = (ALuint)(State->Mod.Index * (ALuint64)range /
  721. State->Mod.Range);
  722. State->Mod.Range = range;
  723. /* The modulation depth effects the amount of frequency change over the
  724. * range of the sinus. It needs to be scaled by the modulation time so
  725. * that a given depth produces a consistent change in frequency over all
  726. * ranges of time. Since the depth is applied to a sinus value, it needs
  727. * to be halfed once for the sinus range and again for the sinus swing
  728. * in time (half of it is spent decreasing the frequency, half is spent
  729. * increasing it).
  730. */
  731. State->Mod.Depth = modDepth * MODULATION_DEPTH_COEFF * modTime / 2.0f /
  732. 2.0f * frequency;
  733. }
  734. // Update the offsets for the initial effect delay line.
  735. static ALvoid UpdateDelayLine(ALfloat earlyDelay, ALfloat lateDelay, ALuint frequency, ALverbState *State)
  736. {
  737. // Calculate the initial delay taps.
  738. State->DelayTap[0] = fastf2u(earlyDelay * frequency);
  739. State->DelayTap[1] = fastf2u((earlyDelay + lateDelay) * frequency);
  740. }
  741. // Update the early reflections gain and line coefficients.
  742. static ALvoid UpdateEarlyLines(ALfloat reverbGain, ALfloat earlyGain, ALfloat lateDelay, ALverbState *State)
  743. {
  744. ALuint index;
  745. // Calculate the early reflections gain (from the master effect gain, and
  746. // reflections gain parameters) with a constant attenuation of 0.5.
  747. State->Early.Gain = 0.5f * reverbGain * earlyGain;
  748. // Calculate the gain (coefficient) for each early delay line using the
  749. // late delay time. This expands the early reflections to the start of
  750. // the late reverb.
  751. for(index = 0;index < 4;index++)
  752. State->Early.Coeff[index] = CalcDecayCoeff(EARLY_LINE_LENGTH[index],
  753. lateDelay);
  754. }
  755. // Update the offsets for the decorrelator line.
  756. static ALvoid UpdateDecorrelator(ALfloat density, ALuint frequency, ALverbState *State)
  757. {
  758. ALuint index;
  759. ALfloat length;
  760. /* The late reverb inputs are decorrelated to smooth the reverb tail and
  761. * reduce harsh echos. The first tap occurs immediately, while the
  762. * remaining taps are delayed by multiples of a fraction of the smallest
  763. * cyclical delay time.
  764. *
  765. * offset[index] = (FRACTION (MULTIPLIER^index)) smallest_delay
  766. */
  767. for(index = 0;index < 3;index++)
  768. {
  769. length = (DECO_FRACTION * aluPow(DECO_MULTIPLIER, (ALfloat)index)) *
  770. LATE_LINE_LENGTH[0] * (1.0f + (density * LATE_LINE_MULTIPLIER));
  771. State->DecoTap[index] = fastf2u(length * frequency);
  772. }
  773. }
  774. // Update the late reverb gains, line lengths, and line coefficients.
  775. static ALvoid UpdateLateLines(ALfloat reverbGain, ALfloat lateGain, ALfloat xMix, ALfloat density, ALfloat decayTime, ALfloat diffusion, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
  776. {
  777. ALfloat length;
  778. ALuint index;
  779. /* Calculate the late reverb gain (from the master effect gain, and late
  780. * reverb gain parameters). Since the output is tapped prior to the
  781. * application of the next delay line coefficients, this gain needs to be
  782. * attenuated by the 'x' mixing matrix coefficient as well.
  783. */
  784. State->Late.Gain = reverbGain * lateGain * xMix;
  785. /* To compensate for changes in modal density and decay time of the late
  786. * reverb signal, the input is attenuated based on the maximal energy of
  787. * the outgoing signal. This approximation is used to keep the apparent
  788. * energy of the signal equal for all ranges of density and decay time.
  789. *
  790. * The average length of the cyclcical delay lines is used to calculate
  791. * the attenuation coefficient.
  792. */
  793. length = (LATE_LINE_LENGTH[0] + LATE_LINE_LENGTH[1] +
  794. LATE_LINE_LENGTH[2] + LATE_LINE_LENGTH[3]) / 4.0f;
  795. length *= 1.0f + (density * LATE_LINE_MULTIPLIER);
  796. State->Late.DensityGain = CalcDensityGain(CalcDecayCoeff(length,
  797. decayTime));
  798. // Calculate the all-pass feed-back and feed-forward coefficient.
  799. State->Late.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
  800. for(index = 0;index < 4;index++)
  801. {
  802. // Calculate the gain (coefficient) for each all-pass line.
  803. State->Late.ApCoeff[index] = CalcDecayCoeff(ALLPASS_LINE_LENGTH[index],
  804. decayTime);
  805. // Calculate the length (in seconds) of each cyclical delay line.
  806. length = LATE_LINE_LENGTH[index] * (1.0f + (density *
  807. LATE_LINE_MULTIPLIER));
  808. // Calculate the delay offset for each cyclical delay line.
  809. State->Late.Offset[index] = fastf2u(length * frequency);
  810. // Calculate the gain (coefficient) for each cyclical line.
  811. State->Late.Coeff[index] = CalcDecayCoeff(length, decayTime);
  812. // Calculate the damping coefficient for each low-pass filter.
  813. State->Late.LpCoeff[index] =
  814. CalcDampingCoeff(hfRatio, length, decayTime,
  815. State->Late.Coeff[index], cw);
  816. // Attenuate the cyclical line coefficients by the mixing coefficient
  817. // (x).
  818. State->Late.Coeff[index] *= xMix;
  819. }
  820. }
  821. // Update the echo gain, line offset, line coefficients, and mixing
  822. // coefficients.
  823. static ALvoid UpdateEchoLine(ALfloat reverbGain, ALfloat lateGain, ALfloat echoTime, ALfloat decayTime, ALfloat diffusion, ALfloat echoDepth, ALfloat hfRatio, ALfloat cw, ALuint frequency, ALverbState *State)
  824. {
  825. // Update the offset and coefficient for the echo delay line.
  826. State->Echo.Offset = fastf2u(echoTime * frequency);
  827. // Calculate the decay coefficient for the echo line.
  828. State->Echo.Coeff = CalcDecayCoeff(echoTime, decayTime);
  829. // Calculate the energy-based attenuation coefficient for the echo delay
  830. // line.
  831. State->Echo.DensityGain = CalcDensityGain(State->Echo.Coeff);
  832. // Calculate the echo all-pass feed coefficient.
  833. State->Echo.ApFeedCoeff = 0.5f * aluPow(diffusion, 2.0f);
  834. // Calculate the echo all-pass attenuation coefficient.
  835. State->Echo.ApCoeff = CalcDecayCoeff(ECHO_ALLPASS_LENGTH, decayTime);
  836. // Calculate the damping coefficient for each low-pass filter.
  837. State->Echo.LpCoeff = CalcDampingCoeff(hfRatio, echoTime, decayTime,
  838. State->Echo.Coeff, cw);
  839. /* Calculate the echo mixing coefficients. The first is applied to the
  840. * echo itself. The second is used to attenuate the late reverb when
  841. * echo depth is high and diffusion is low, so the echo is slightly
  842. * stronger than the decorrelated echos in the reverb tail.
  843. */
  844. State->Echo.MixCoeff[0] = reverbGain * lateGain * echoDepth;
  845. State->Echo.MixCoeff[1] = 1.0f - (echoDepth * 0.5f * (1.0f - diffusion));
  846. }
  847. // Update the early and late 3D panning gains.
  848. static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, ALfloat Gain, ALverbState *State)
  849. {
  850. ALfloat earlyPan[3] = { ReflectionsPan[0], ReflectionsPan[1],
  851. ReflectionsPan[2] };
  852. ALfloat latePan[3] = { LateReverbPan[0], LateReverbPan[1],
  853. LateReverbPan[2] };
  854. const ALfloat *ChannelGain;
  855. ALfloat ambientGain;
  856. ALfloat dirGain;
  857. ALfloat length;
  858. ALuint index;
  859. ALint pos;
  860. Gain *= ReverbBoost;
  861. // Attenuate non-directional reverb according to the number of channels
  862. ambientGain = aluSqrt(2.0f/Device->NumChan);
  863. // Calculate the 3D-panning gains for the early reflections and late
  864. // reverb.
  865. length = earlyPan[0]*earlyPan[0] + earlyPan[1]*earlyPan[1] + earlyPan[2]*earlyPan[2];
  866. if(length > 1.0f)
  867. {
  868. length = 1.0f / aluSqrt(length);
  869. earlyPan[0] *= length;
  870. earlyPan[1] *= length;
  871. earlyPan[2] *= length;
  872. }
  873. length = latePan[0]*latePan[0] + latePan[1]*latePan[1] + latePan[2]*latePan[2];
  874. if(length > 1.0f)
  875. {
  876. length = 1.0f / aluSqrt(length);
  877. latePan[0] *= length;
  878. latePan[1] *= length;
  879. latePan[2] *= length;
  880. }
  881. /* This code applies directional reverb just like the mixer applies
  882. * directional sources. It diffuses the sound toward all speakers as the
  883. * magnitude of the panning vector drops, which is only a rough
  884. * approximation of the expansion of sound across the speakers from the
  885. * panning direction.
  886. */
  887. pos = aluCart2LUTpos(earlyPan[2], earlyPan[0]);
  888. ChannelGain = Device->PanningLUT[pos];
  889. dirGain = aluSqrt((earlyPan[0] * earlyPan[0]) + (earlyPan[2] * earlyPan[2]));
  890. for(index = 0;index < MAXCHANNELS;index++)
  891. State->Early.PanGain[index] = 0.0f;
  892. for(index = 0;index < Device->NumChan;index++)
  893. {
  894. enum Channel chan = Device->Speaker2Chan[index];
  895. State->Early.PanGain[chan] = lerp(ambientGain, ChannelGain[chan], dirGain) * Gain;
  896. }
  897. pos = aluCart2LUTpos(latePan[2], latePan[0]);
  898. ChannelGain = Device->PanningLUT[pos];
  899. dirGain = aluSqrt((latePan[0] * latePan[0]) + (latePan[2] * latePan[2]));
  900. for(index = 0;index < MAXCHANNELS;index++)
  901. State->Late.PanGain[index] = 0.0f;
  902. for(index = 0;index < Device->NumChan;index++)
  903. {
  904. enum Channel chan = Device->Speaker2Chan[index];
  905. State->Late.PanGain[chan] = lerp(ambientGain, ChannelGain[chan], dirGain) * Gain;
  906. }
  907. }
  908. // This updates the EAX reverb state. This is called any time the EAX reverb
  909. // effect is loaded into a slot.
  910. static ALvoid ReverbUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
  911. {
  912. ALverbState *State = (ALverbState*)effect;
  913. ALuint frequency = Device->Frequency;
  914. ALboolean isEAX = AL_FALSE;
  915. ALfloat cw, x, y, hfRatio;
  916. if(Slot->effect.type == AL_EFFECT_EAXREVERB && !EmulateEAXReverb)
  917. {
  918. State->state.Process = EAXVerbProcess;
  919. isEAX = AL_TRUE;
  920. }
  921. else if(Slot->effect.type == AL_EFFECT_REVERB || EmulateEAXReverb)
  922. {
  923. State->state.Process = VerbProcess;
  924. isEAX = AL_FALSE;
  925. }
  926. // Calculate the master low-pass filter (from the master effect HF gain).
  927. if(isEAX) cw = CalcI3DL2HFreq(Slot->effect.Reverb.HFReference, frequency);
  928. else cw = CalcI3DL2HFreq(LOWPASSFREQREF, frequency);
  929. // This is done with 2 chained 1-pole filters, so no need to square g.
  930. State->LpFilter.coeff = lpCoeffCalc(Slot->effect.Reverb.GainHF, cw);
  931. if(isEAX)
  932. {
  933. // Update the modulator line.
  934. UpdateModulator(Slot->effect.Reverb.ModulationTime,
  935. Slot->effect.Reverb.ModulationDepth,
  936. frequency, State);
  937. }
  938. // Update the initial effect delay.
  939. UpdateDelayLine(Slot->effect.Reverb.ReflectionsDelay,
  940. Slot->effect.Reverb.LateReverbDelay,
  941. frequency, State);
  942. // Update the early lines.
  943. UpdateEarlyLines(Slot->effect.Reverb.Gain,
  944. Slot->effect.Reverb.ReflectionsGain,
  945. Slot->effect.Reverb.LateReverbDelay, State);
  946. // Update the decorrelator.
  947. UpdateDecorrelator(Slot->effect.Reverb.Density, frequency, State);
  948. // Get the mixing matrix coefficients (x and y).
  949. CalcMatrixCoeffs(Slot->effect.Reverb.Diffusion, &x, &y);
  950. // Then divide x into y to simplify the matrix calculation.
  951. State->Late.MixCoeff = y / x;
  952. // If the HF limit parameter is flagged, calculate an appropriate limit
  953. // based on the air absorption parameter.
  954. hfRatio = Slot->effect.Reverb.DecayHFRatio;
  955. if(Slot->effect.Reverb.DecayHFLimit &&
  956. Slot->effect.Reverb.AirAbsorptionGainHF < 1.0f)
  957. hfRatio = CalcLimitedHfRatio(hfRatio,
  958. Slot->effect.Reverb.AirAbsorptionGainHF,
  959. Slot->effect.Reverb.DecayTime);
  960. // Update the late lines.
  961. UpdateLateLines(Slot->effect.Reverb.Gain, Slot->effect.Reverb.LateReverbGain,
  962. x, Slot->effect.Reverb.Density, Slot->effect.Reverb.DecayTime,
  963. Slot->effect.Reverb.Diffusion, hfRatio, cw, frequency, State);
  964. if(isEAX)
  965. {
  966. // Update the echo line.
  967. UpdateEchoLine(Slot->effect.Reverb.Gain, Slot->effect.Reverb.LateReverbGain,
  968. Slot->effect.Reverb.EchoTime, Slot->effect.Reverb.DecayTime,
  969. Slot->effect.Reverb.Diffusion, Slot->effect.Reverb.EchoDepth,
  970. hfRatio, cw, frequency, State);
  971. // Update early and late 3D panning.
  972. Update3DPanning(Device, Slot->effect.Reverb.ReflectionsPan,
  973. Slot->effect.Reverb.LateReverbPan, Slot->Gain, State);
  974. }
  975. else
  976. {
  977. ALfloat gain = Slot->Gain;
  978. ALuint index;
  979. /* Update channel gains */
  980. gain *= aluSqrt(2.0f/Device->NumChan) * ReverbBoost;
  981. for(index = 0;index < MAXCHANNELS;index++)
  982. State->Gain[index] = 0.0f;
  983. for(index = 0;index < Device->NumChan;index++)
  984. {
  985. enum Channel chan = Device->Speaker2Chan[index];
  986. State->Gain[chan] = gain;
  987. }
  988. }
  989. }
  990. // This destroys the reverb state. It should be called only when the effect
  991. // slot has a different (or no) effect loaded over the reverb effect.
  992. static ALvoid ReverbDestroy(ALeffectState *effect)
  993. {
  994. ALverbState *State = (ALverbState*)effect;
  995. if(State)
  996. {
  997. free(State->SampleBuffer);
  998. State->SampleBuffer = NULL;
  999. free(State);
  1000. }
  1001. }
  1002. // This creates the reverb state. It should be called only when the reverb
  1003. // effect is loaded into a slot that doesn't already have a reverb effect.
  1004. ALeffectState *ReverbCreate(void)
  1005. {
  1006. ALverbState *State = NULL;
  1007. ALuint index;
  1008. State = malloc(sizeof(ALverbState));
  1009. if(!State)
  1010. return NULL;
  1011. State->state.Destroy = ReverbDestroy;
  1012. State->state.DeviceUpdate = ReverbDeviceUpdate;
  1013. State->state.Update = ReverbUpdate;
  1014. State->state.Process = VerbProcess;
  1015. State->TotalSamples = 0;
  1016. State->SampleBuffer = NULL;
  1017. State->LpFilter.coeff = 0.0f;
  1018. State->LpFilter.history[0] = 0.0f;
  1019. State->LpFilter.history[1] = 0.0f;
  1020. State->Mod.Delay.Mask = 0;
  1021. State->Mod.Delay.Line = NULL;
  1022. State->Mod.Index = 0;
  1023. State->Mod.Range = 1;
  1024. State->Mod.Depth = 0.0f;
  1025. State->Mod.Coeff = 0.0f;
  1026. State->Mod.Filter = 0.0f;
  1027. State->Delay.Mask = 0;
  1028. State->Delay.Line = NULL;
  1029. State->DelayTap[0] = 0;
  1030. State->DelayTap[1] = 0;
  1031. State->Early.Gain = 0.0f;
  1032. for(index = 0;index < 4;index++)
  1033. {
  1034. State->Early.Coeff[index] = 0.0f;
  1035. State->Early.Delay[index].Mask = 0;
  1036. State->Early.Delay[index].Line = NULL;
  1037. State->Early.Offset[index] = 0;
  1038. }
  1039. State->Decorrelator.Mask = 0;
  1040. State->Decorrelator.Line = NULL;
  1041. State->DecoTap[0] = 0;
  1042. State->DecoTap[1] = 0;
  1043. State->DecoTap[2] = 0;
  1044. State->Late.Gain = 0.0f;
  1045. State->Late.DensityGain = 0.0f;
  1046. State->Late.ApFeedCoeff = 0.0f;
  1047. State->Late.MixCoeff = 0.0f;
  1048. for(index = 0;index < 4;index++)
  1049. {
  1050. State->Late.ApCoeff[index] = 0.0f;
  1051. State->Late.ApDelay[index].Mask = 0;
  1052. State->Late.ApDelay[index].Line = NULL;
  1053. State->Late.ApOffset[index] = 0;
  1054. State->Late.Coeff[index] = 0.0f;
  1055. State->Late.Delay[index].Mask = 0;
  1056. State->Late.Delay[index].Line = NULL;
  1057. State->Late.Offset[index] = 0;
  1058. State->Late.LpCoeff[index] = 0.0f;
  1059. State->Late.LpSample[index] = 0.0f;
  1060. }
  1061. for(index = 0;index < MAXCHANNELS;index++)
  1062. {
  1063. State->Early.PanGain[index] = 0.0f;
  1064. State->Late.PanGain[index] = 0.0f;
  1065. }
  1066. State->Echo.DensityGain = 0.0f;
  1067. State->Echo.Delay.Mask = 0;
  1068. State->Echo.Delay.Line = NULL;
  1069. State->Echo.ApDelay.Mask = 0;
  1070. State->Echo.ApDelay.Line = NULL;
  1071. State->Echo.Coeff = 0.0f;
  1072. State->Echo.ApFeedCoeff = 0.0f;
  1073. State->Echo.ApCoeff = 0.0f;
  1074. State->Echo.Offset = 0;
  1075. State->Echo.ApOffset = 0;
  1076. State->Echo.LpCoeff = 0.0f;
  1077. State->Echo.LpSample = 0.0f;
  1078. State->Echo.MixCoeff[0] = 0.0f;
  1079. State->Echo.MixCoeff[1] = 0.0f;
  1080. State->Offset = 0;
  1081. State->Gain = State->Late.PanGain;
  1082. return &State->state;
  1083. }