alcReverb.c 51 KB

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