reverb.c 74 KB

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