reverb.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  1. /**
  2. * Ambisonic reverb engine for the OpenAL cross platform audio library
  3. * Copyright (C) 2008-2017 by Chris Robinson and 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 "alListener.h"
  28. #include "alError.h"
  29. #include "filters/defs.h"
  30. /* This is a user config option for modifying the overall output of the reverb
  31. * effect.
  32. */
  33. ALfloat ReverbBoost = 1.0f;
  34. /* This is the maximum number of samples processed for each inner loop
  35. * iteration. */
  36. #define MAX_UPDATE_SAMPLES 256
  37. /* The number of samples used for cross-faded delay lines. This can be used
  38. * to balance the compensation for abrupt line changes and attenuation due to
  39. * minimally lengthed recursive lines. Try to keep this below the device
  40. * update size.
  41. */
  42. #define FADE_SAMPLES 128
  43. /* The number of spatialized lines or channels to process. Four channels allows
  44. * for a 3D A-Format response. NOTE: This can't be changed without taking care
  45. * of the conversion matrices, and a few places where the length arrays are
  46. * assumed to have 4 elements.
  47. */
  48. #define NUM_LINES 4
  49. /* The B-Format to A-Format conversion matrix. The arrangement of rows is
  50. * deliberately chosen to align the resulting lines to their spatial opposites
  51. * (0:above front left <-> 3:above back right, 1:below front right <-> 2:below
  52. * back left). It's not quite opposite, since the A-Format results in a
  53. * tetrahedron, but it's close enough. Should the model be extended to 8-lines
  54. * in the future, true opposites can be used.
  55. */
  56. static const aluMatrixf B2A = {{
  57. { 0.288675134595f, 0.288675134595f, 0.288675134595f, 0.288675134595f },
  58. { 0.288675134595f, -0.288675134595f, -0.288675134595f, 0.288675134595f },
  59. { 0.288675134595f, 0.288675134595f, -0.288675134595f, -0.288675134595f },
  60. { 0.288675134595f, -0.288675134595f, 0.288675134595f, -0.288675134595f }
  61. }};
  62. /* Converts A-Format to B-Format. */
  63. static const aluMatrixf A2B = {{
  64. { 0.866025403785f, 0.866025403785f, 0.866025403785f, 0.866025403785f },
  65. { 0.866025403785f, -0.866025403785f, 0.866025403785f, -0.866025403785f },
  66. { 0.866025403785f, -0.866025403785f, -0.866025403785f, 0.866025403785f },
  67. { 0.866025403785f, 0.866025403785f, -0.866025403785f, -0.866025403785f }
  68. }};
  69. static const ALfloat FadeStep = 1.0f / FADE_SAMPLES;
  70. /* The all-pass and delay lines have a variable length dependent on the
  71. * effect's density parameter, which helps alter the perceived environment
  72. * size. The size-to-density conversion is a cubed scale:
  73. *
  74. * density = min(1.0, pow(size, 3.0) / DENSITY_SCALE);
  75. *
  76. * The line lengths scale linearly with room size, so the inverse density
  77. * conversion is needed, taking the cube root of the re-scaled density to
  78. * calculate the line length multiplier:
  79. *
  80. * length_mult = max(5.0, cbrtf(density*DENSITY_SCALE));
  81. *
  82. * The density scale below will result in a max line multiplier of 50, for an
  83. * effective size range of 5m to 50m.
  84. */
  85. static const ALfloat DENSITY_SCALE = 125000.0f;
  86. /* All delay line lengths are specified in seconds.
  87. *
  88. * To approximate early reflections, we break them up into primary (those
  89. * arriving from the same direction as the source) and secondary (those
  90. * arriving from the opposite direction).
  91. *
  92. * The early taps decorrelate the 4-channel signal to approximate an average
  93. * room response for the primary reflections after the initial early delay.
  94. *
  95. * Given an average room dimension (d_a) and the speed of sound (c) we can
  96. * calculate the average reflection delay (r_a) regardless of listener and
  97. * source positions as:
  98. *
  99. * r_a = d_a / c
  100. * c = 343.3
  101. *
  102. * This can extended to finding the average difference (r_d) between the
  103. * maximum (r_1) and minimum (r_0) reflection delays:
  104. *
  105. * r_0 = 2 / 3 r_a
  106. * = r_a - r_d / 2
  107. * = r_d
  108. * r_1 = 4 / 3 r_a
  109. * = r_a + r_d / 2
  110. * = 2 r_d
  111. * r_d = 2 / 3 r_a
  112. * = r_1 - r_0
  113. *
  114. * As can be determined by integrating the 1D model with a source (s) and
  115. * listener (l) positioned across the dimension of length (d_a):
  116. *
  117. * r_d = int_(l=0)^d_a (int_(s=0)^d_a |2 d_a - 2 (l + s)| ds) dl / c
  118. *
  119. * The initial taps (T_(i=0)^N) are then specified by taking a power series
  120. * that ranges between r_0 and half of r_1 less r_0:
  121. *
  122. * R_i = 2^(i / (2 N - 1)) r_d
  123. * = r_0 + (2^(i / (2 N - 1)) - 1) r_d
  124. * = r_0 + T_i
  125. * T_i = R_i - r_0
  126. * = (2^(i / (2 N - 1)) - 1) r_d
  127. *
  128. * Assuming an average of 1m, we get the following taps:
  129. */
  130. static const ALfloat EARLY_TAP_LENGTHS[NUM_LINES] =
  131. {
  132. 0.0000000e+0f, 2.0213520e-4f, 4.2531060e-4f, 6.7171600e-4f
  133. };
  134. /* The early all-pass filter lengths are based on the early tap lengths:
  135. *
  136. * A_i = R_i / a
  137. *
  138. * Where a is the approximate maximum all-pass cycle limit (20).
  139. */
  140. static const ALfloat EARLY_ALLPASS_LENGTHS[NUM_LINES] =
  141. {
  142. 9.7096800e-5f, 1.0720356e-4f, 1.1836234e-4f, 1.3068260e-4f
  143. };
  144. /* The early delay lines are used to transform the primary reflections into
  145. * the secondary reflections. The A-format is arranged in such a way that
  146. * the channels/lines are spatially opposite:
  147. *
  148. * C_i is opposite C_(N-i-1)
  149. *
  150. * The delays of the two opposing reflections (R_i and O_i) from a source
  151. * anywhere along a particular dimension always sum to twice its full delay:
  152. *
  153. * 2 r_a = R_i + O_i
  154. *
  155. * With that in mind we can determine the delay between the two reflections
  156. * and thus specify our early line lengths (L_(i=0)^N) using:
  157. *
  158. * O_i = 2 r_a - R_(N-i-1)
  159. * L_i = O_i - R_(N-i-1)
  160. * = 2 (r_a - R_(N-i-1))
  161. * = 2 (r_a - T_(N-i-1) - r_0)
  162. * = 2 r_a (1 - (2 / 3) 2^((N - i - 1) / (2 N - 1)))
  163. *
  164. * Using an average dimension of 1m, we get:
  165. */
  166. static const ALfloat EARLY_LINE_LENGTHS[NUM_LINES] =
  167. {
  168. 5.9850400e-4f, 1.0913150e-3f, 1.5376658e-3f, 1.9419362e-3f
  169. };
  170. /* The late all-pass filter lengths are based on the late line lengths:
  171. *
  172. * A_i = (5 / 3) L_i / r_1
  173. */
  174. static const ALfloat LATE_ALLPASS_LENGTHS[NUM_LINES] =
  175. {
  176. 1.6182800e-4f, 2.0389060e-4f, 2.8159360e-4f, 3.2365600e-4f
  177. };
  178. /* The late lines are used to approximate the decaying cycle of recursive
  179. * late reflections.
  180. *
  181. * Splitting the lines in half, we start with the shortest reflection paths
  182. * (L_(i=0)^(N/2)):
  183. *
  184. * L_i = 2^(i / (N - 1)) r_d
  185. *
  186. * Then for the opposite (longest) reflection paths (L_(i=N/2)^N):
  187. *
  188. * L_i = 2 r_a - L_(i-N/2)
  189. * = 2 r_a - 2^((i - N / 2) / (N - 1)) r_d
  190. *
  191. * For our 1m average room, we get:
  192. */
  193. static const ALfloat LATE_LINE_LENGTHS[NUM_LINES] =
  194. {
  195. 1.9419362e-3f, 2.4466860e-3f, 3.3791220e-3f, 3.8838720e-3f
  196. };
  197. typedef struct DelayLineI {
  198. /* The delay lines use interleaved samples, with the lengths being powers
  199. * of 2 to allow the use of bit-masking instead of a modulus for wrapping.
  200. */
  201. ALsizei Mask;
  202. ALfloat (*Line)[NUM_LINES];
  203. } DelayLineI;
  204. typedef struct VecAllpass {
  205. DelayLineI Delay;
  206. ALfloat Coeff;
  207. ALsizei Offset[NUM_LINES][2];
  208. } VecAllpass;
  209. typedef struct T60Filter {
  210. /* Two filters are used to adjust the signal. One to control the low
  211. * frequencies, and one to control the high frequencies.
  212. */
  213. ALfloat MidGain[2];
  214. BiquadFilter HFFilter, LFFilter;
  215. } T60Filter;
  216. typedef struct EarlyReflections {
  217. /* A Gerzon vector all-pass filter is used to simulate initial diffusion.
  218. * The spread from this filter also helps smooth out the reverb tail.
  219. */
  220. VecAllpass VecAp;
  221. /* An echo line is used to complete the second half of the early
  222. * reflections.
  223. */
  224. DelayLineI Delay;
  225. ALsizei Offset[NUM_LINES][2];
  226. ALfloat Coeff[NUM_LINES][2];
  227. /* The gain for each output channel based on 3D panning. */
  228. ALfloat CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS];
  229. ALfloat PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS];
  230. } EarlyReflections;
  231. typedef struct LateReverb {
  232. /* A recursive delay line is used fill in the reverb tail. */
  233. DelayLineI Delay;
  234. ALsizei Offset[NUM_LINES][2];
  235. /* Attenuation to compensate for the modal density and decay rate of the
  236. * late lines.
  237. */
  238. ALfloat DensityGain[2];
  239. /* T60 decay filters are used to simulate absorption. */
  240. T60Filter T60[NUM_LINES];
  241. /* A Gerzon vector all-pass filter is used to simulate diffusion. */
  242. VecAllpass VecAp;
  243. /* The gain for each output channel based on 3D panning. */
  244. ALfloat CurrentGain[NUM_LINES][MAX_OUTPUT_CHANNELS];
  245. ALfloat PanGain[NUM_LINES][MAX_OUTPUT_CHANNELS];
  246. } LateReverb;
  247. typedef struct ReverbState {
  248. DERIVE_FROM_TYPE(ALeffectState);
  249. /* All delay lines are allocated as a single buffer to reduce memory
  250. * fragmentation and management code.
  251. */
  252. ALfloat *SampleBuffer;
  253. ALuint TotalSamples;
  254. struct {
  255. /* Calculated parameters which indicate if cross-fading is needed after
  256. * an update.
  257. */
  258. ALfloat Density, Diffusion;
  259. ALfloat DecayTime, HFDecayTime, LFDecayTime;
  260. ALfloat HFReference, LFReference;
  261. } Params;
  262. /* Master effect filters */
  263. struct {
  264. BiquadFilter Lp;
  265. BiquadFilter Hp;
  266. } Filter[NUM_LINES];
  267. /* Core delay line (early reflections and late reverb tap from this). */
  268. DelayLineI Delay;
  269. /* Tap points for early reflection delay. */
  270. ALsizei EarlyDelayTap[NUM_LINES][2];
  271. ALfloat EarlyDelayCoeff[NUM_LINES][2];
  272. /* Tap points for late reverb feed and delay. */
  273. ALsizei LateFeedTap;
  274. ALsizei LateDelayTap[NUM_LINES][2];
  275. /* Coefficients for the all-pass and line scattering matrices. */
  276. ALfloat MixX;
  277. ALfloat MixY;
  278. EarlyReflections Early;
  279. LateReverb Late;
  280. /* Indicates the cross-fade point for delay line reads [0,FADE_SAMPLES]. */
  281. ALsizei FadeCount;
  282. /* Maximum number of samples to process at once. */
  283. ALsizei MaxUpdate[2];
  284. /* The current write offset for all delay lines. */
  285. ALsizei Offset;
  286. /* Temporary storage used when processing. */
  287. alignas(16) ALfloat TempSamples[NUM_LINES][MAX_UPDATE_SAMPLES];
  288. alignas(16) ALfloat MixSamples[NUM_LINES][MAX_UPDATE_SAMPLES];
  289. } ReverbState;
  290. static ALvoid ReverbState_Destruct(ReverbState *State);
  291. static ALboolean ReverbState_deviceUpdate(ReverbState *State, ALCdevice *Device);
  292. static ALvoid ReverbState_update(ReverbState *State, const ALCcontext *Context, const ALeffectslot *Slot, const ALeffectProps *props);
  293. static ALvoid ReverbState_process(ReverbState *State, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
  294. DECLARE_DEFAULT_ALLOCATORS(ReverbState)
  295. DEFINE_ALEFFECTSTATE_VTABLE(ReverbState);
  296. static void ReverbState_Construct(ReverbState *state)
  297. {
  298. ALsizei i, j;
  299. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  300. SET_VTABLE2(ReverbState, ALeffectState, state);
  301. state->TotalSamples = 0;
  302. state->SampleBuffer = NULL;
  303. state->Params.Density = AL_EAXREVERB_DEFAULT_DENSITY;
  304. state->Params.Diffusion = AL_EAXREVERB_DEFAULT_DIFFUSION;
  305. state->Params.DecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME;
  306. state->Params.HFDecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME*AL_EAXREVERB_DEFAULT_DECAY_HFRATIO;
  307. state->Params.LFDecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME*AL_EAXREVERB_DEFAULT_DECAY_LFRATIO;
  308. state->Params.HFReference = AL_EAXREVERB_DEFAULT_HFREFERENCE;
  309. state->Params.LFReference = AL_EAXREVERB_DEFAULT_LFREFERENCE;
  310. for(i = 0;i < NUM_LINES;i++)
  311. {
  312. BiquadFilter_clear(&state->Filter[i].Lp);
  313. BiquadFilter_clear(&state->Filter[i].Hp);
  314. }
  315. state->Delay.Mask = 0;
  316. state->Delay.Line = NULL;
  317. for(i = 0;i < NUM_LINES;i++)
  318. {
  319. state->EarlyDelayTap[i][0] = 0;
  320. state->EarlyDelayTap[i][1] = 0;
  321. state->EarlyDelayCoeff[i][0] = 0.0f;
  322. state->EarlyDelayCoeff[i][1] = 0.0f;
  323. }
  324. state->LateFeedTap = 0;
  325. for(i = 0;i < NUM_LINES;i++)
  326. {
  327. state->LateDelayTap[i][0] = 0;
  328. state->LateDelayTap[i][1] = 0;
  329. }
  330. state->MixX = 0.0f;
  331. state->MixY = 0.0f;
  332. state->Early.VecAp.Delay.Mask = 0;
  333. state->Early.VecAp.Delay.Line = NULL;
  334. state->Early.VecAp.Coeff = 0.0f;
  335. state->Early.Delay.Mask = 0;
  336. state->Early.Delay.Line = NULL;
  337. for(i = 0;i < NUM_LINES;i++)
  338. {
  339. state->Early.VecAp.Offset[i][0] = 0;
  340. state->Early.VecAp.Offset[i][1] = 0;
  341. state->Early.Offset[i][0] = 0;
  342. state->Early.Offset[i][1] = 0;
  343. state->Early.Coeff[i][0] = 0.0f;
  344. state->Early.Coeff[i][1] = 0.0f;
  345. }
  346. state->Late.DensityGain[0] = 0.0f;
  347. state->Late.DensityGain[1] = 0.0f;
  348. state->Late.Delay.Mask = 0;
  349. state->Late.Delay.Line = NULL;
  350. state->Late.VecAp.Delay.Mask = 0;
  351. state->Late.VecAp.Delay.Line = NULL;
  352. state->Late.VecAp.Coeff = 0.0f;
  353. for(i = 0;i < NUM_LINES;i++)
  354. {
  355. state->Late.Offset[i][0] = 0;
  356. state->Late.Offset[i][1] = 0;
  357. state->Late.VecAp.Offset[i][0] = 0;
  358. state->Late.VecAp.Offset[i][1] = 0;
  359. state->Late.T60[i].MidGain[0] = 0.0f;
  360. state->Late.T60[i].MidGain[1] = 0.0f;
  361. BiquadFilter_clear(&state->Late.T60[i].HFFilter);
  362. BiquadFilter_clear(&state->Late.T60[i].LFFilter);
  363. }
  364. for(i = 0;i < NUM_LINES;i++)
  365. {
  366. for(j = 0;j < MAX_OUTPUT_CHANNELS;j++)
  367. {
  368. state->Early.CurrentGain[i][j] = 0.0f;
  369. state->Early.PanGain[i][j] = 0.0f;
  370. state->Late.CurrentGain[i][j] = 0.0f;
  371. state->Late.PanGain[i][j] = 0.0f;
  372. }
  373. }
  374. state->FadeCount = 0;
  375. state->MaxUpdate[0] = MAX_UPDATE_SAMPLES;
  376. state->MaxUpdate[1] = MAX_UPDATE_SAMPLES;
  377. state->Offset = 0;
  378. }
  379. static ALvoid ReverbState_Destruct(ReverbState *State)
  380. {
  381. al_free(State->SampleBuffer);
  382. State->SampleBuffer = NULL;
  383. ALeffectState_Destruct(STATIC_CAST(ALeffectState,State));
  384. }
  385. /**************************************
  386. * Device Update *
  387. **************************************/
  388. static inline ALfloat CalcDelayLengthMult(ALfloat density)
  389. {
  390. return maxf(5.0f, cbrtf(density*DENSITY_SCALE));
  391. }
  392. /* Given the allocated sample buffer, this function updates each delay line
  393. * offset.
  394. */
  395. static inline ALvoid RealizeLineOffset(ALfloat *sampleBuffer, DelayLineI *Delay)
  396. {
  397. union {
  398. ALfloat *f;
  399. ALfloat (*f4)[NUM_LINES];
  400. } u;
  401. u.f = &sampleBuffer[(ptrdiff_t)Delay->Line * NUM_LINES];
  402. Delay->Line = u.f4;
  403. }
  404. /* Calculate the length of a delay line and store its mask and offset. */
  405. static ALuint CalcLineLength(const ALfloat length, const ptrdiff_t offset, const ALuint frequency,
  406. const ALuint extra, DelayLineI *Delay)
  407. {
  408. ALuint samples;
  409. /* All line lengths are powers of 2, calculated from their lengths in
  410. * seconds, rounded up.
  411. */
  412. samples = float2int(ceilf(length*frequency));
  413. samples = NextPowerOf2(samples + extra);
  414. /* All lines share a single sample buffer. */
  415. Delay->Mask = samples - 1;
  416. Delay->Line = (ALfloat(*)[NUM_LINES])offset;
  417. /* Return the sample count for accumulation. */
  418. return samples;
  419. }
  420. /* Calculates the delay line metrics and allocates the shared sample buffer
  421. * for all lines given the sample rate (frequency). If an allocation failure
  422. * occurs, it returns AL_FALSE.
  423. */
  424. static ALboolean AllocLines(const ALuint frequency, ReverbState *State)
  425. {
  426. ALuint totalSamples, i;
  427. ALfloat multiplier, length;
  428. /* All delay line lengths are calculated to accomodate the full range of
  429. * lengths given their respective paramters.
  430. */
  431. totalSamples = 0;
  432. /* Multiplier for the maximum density value, i.e. density=1, which is
  433. * actually the least density...
  434. */
  435. multiplier = CalcDelayLengthMult(AL_EAXREVERB_MAX_DENSITY);
  436. /* The main delay length includes the maximum early reflection delay, the
  437. * largest early tap width, the maximum late reverb delay, and the
  438. * largest late tap width. Finally, it must also be extended by the
  439. * update size (MAX_UPDATE_SAMPLES) for block processing.
  440. */
  441. length = AL_EAXREVERB_MAX_REFLECTIONS_DELAY + EARLY_TAP_LENGTHS[NUM_LINES-1]*multiplier +
  442. AL_EAXREVERB_MAX_LATE_REVERB_DELAY +
  443. (LATE_LINE_LENGTHS[NUM_LINES-1] - LATE_LINE_LENGTHS[0])*0.25f*multiplier;
  444. totalSamples += CalcLineLength(length, totalSamples, frequency, MAX_UPDATE_SAMPLES,
  445. &State->Delay);
  446. /* The early vector all-pass line. */
  447. length = EARLY_ALLPASS_LENGTHS[NUM_LINES-1] * multiplier;
  448. totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
  449. &State->Early.VecAp.Delay);
  450. /* The early reflection line. */
  451. length = EARLY_LINE_LENGTHS[NUM_LINES-1] * multiplier;
  452. totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
  453. &State->Early.Delay);
  454. /* The late vector all-pass line. */
  455. length = LATE_ALLPASS_LENGTHS[NUM_LINES-1] * multiplier;
  456. totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
  457. &State->Late.VecAp.Delay);
  458. /* The late delay lines are calculated from the largest maximum density
  459. * line length.
  460. */
  461. length = LATE_LINE_LENGTHS[NUM_LINES-1] * multiplier;
  462. totalSamples += CalcLineLength(length, totalSamples, frequency, 0,
  463. &State->Late.Delay);
  464. if(totalSamples != State->TotalSamples)
  465. {
  466. ALfloat *newBuffer;
  467. TRACE("New reverb buffer length: %ux4 samples\n", totalSamples);
  468. newBuffer = al_calloc(16, sizeof(ALfloat[NUM_LINES]) * totalSamples);
  469. if(!newBuffer) return AL_FALSE;
  470. al_free(State->SampleBuffer);
  471. State->SampleBuffer = newBuffer;
  472. State->TotalSamples = totalSamples;
  473. }
  474. /* Update all delays to reflect the new sample buffer. */
  475. RealizeLineOffset(State->SampleBuffer, &State->Delay);
  476. RealizeLineOffset(State->SampleBuffer, &State->Early.VecAp.Delay);
  477. RealizeLineOffset(State->SampleBuffer, &State->Early.Delay);
  478. RealizeLineOffset(State->SampleBuffer, &State->Late.VecAp.Delay);
  479. RealizeLineOffset(State->SampleBuffer, &State->Late.Delay);
  480. /* Clear the sample buffer. */
  481. for(i = 0;i < State->TotalSamples;i++)
  482. State->SampleBuffer[i] = 0.0f;
  483. return AL_TRUE;
  484. }
  485. static ALboolean ReverbState_deviceUpdate(ReverbState *State, ALCdevice *Device)
  486. {
  487. ALuint frequency = Device->Frequency;
  488. ALfloat multiplier;
  489. ALsizei i, j;
  490. /* Allocate the delay lines. */
  491. if(!AllocLines(frequency, State))
  492. return AL_FALSE;
  493. multiplier = CalcDelayLengthMult(AL_EAXREVERB_MAX_DENSITY);
  494. /* The late feed taps are set a fixed position past the latest delay tap. */
  495. State->LateFeedTap = float2int((AL_EAXREVERB_MAX_REFLECTIONS_DELAY +
  496. EARLY_TAP_LENGTHS[NUM_LINES-1]*multiplier) *
  497. frequency);
  498. /* Clear filters and gain coefficients since the delay lines were all just
  499. * cleared (if not reallocated).
  500. */
  501. for(i = 0;i < NUM_LINES;i++)
  502. {
  503. BiquadFilter_clear(&State->Filter[i].Lp);
  504. BiquadFilter_clear(&State->Filter[i].Hp);
  505. }
  506. for(i = 0;i < NUM_LINES;i++)
  507. {
  508. State->EarlyDelayCoeff[i][0] = 0.0f;
  509. State->EarlyDelayCoeff[i][1] = 0.0f;
  510. }
  511. for(i = 0;i < NUM_LINES;i++)
  512. {
  513. State->Early.Coeff[i][0] = 0.0f;
  514. State->Early.Coeff[i][1] = 0.0f;
  515. }
  516. State->Late.DensityGain[0] = 0.0f;
  517. State->Late.DensityGain[1] = 0.0f;
  518. for(i = 0;i < NUM_LINES;i++)
  519. {
  520. State->Late.T60[i].MidGain[0] = 0.0f;
  521. State->Late.T60[i].MidGain[1] = 0.0f;
  522. BiquadFilter_clear(&State->Late.T60[i].HFFilter);
  523. BiquadFilter_clear(&State->Late.T60[i].LFFilter);
  524. }
  525. for(i = 0;i < NUM_LINES;i++)
  526. {
  527. for(j = 0;j < MAX_OUTPUT_CHANNELS;j++)
  528. {
  529. State->Early.CurrentGain[i][j] = 0.0f;
  530. State->Early.PanGain[i][j] = 0.0f;
  531. State->Late.CurrentGain[i][j] = 0.0f;
  532. State->Late.PanGain[i][j] = 0.0f;
  533. }
  534. }
  535. /* Reset counters and offset base. */
  536. State->FadeCount = 0;
  537. State->MaxUpdate[0] = MAX_UPDATE_SAMPLES;
  538. State->MaxUpdate[1] = MAX_UPDATE_SAMPLES;
  539. State->Offset = 0;
  540. return AL_TRUE;
  541. }
  542. /**************************************
  543. * Effect Update *
  544. **************************************/
  545. /* Calculate a decay coefficient given the length of each cycle and the time
  546. * until the decay reaches -60 dB.
  547. */
  548. static inline ALfloat CalcDecayCoeff(const ALfloat length, const ALfloat decayTime)
  549. {
  550. return powf(REVERB_DECAY_GAIN, length/decayTime);
  551. }
  552. /* Calculate a decay length from a coefficient and the time until the decay
  553. * reaches -60 dB.
  554. */
  555. static inline ALfloat CalcDecayLength(const ALfloat coeff, const ALfloat decayTime)
  556. {
  557. return log10f(coeff) * decayTime / log10f(REVERB_DECAY_GAIN);
  558. }
  559. /* Calculate an attenuation to be applied to the input of any echo models to
  560. * compensate for modal density and decay time.
  561. */
  562. static inline ALfloat CalcDensityGain(const ALfloat a)
  563. {
  564. /* The energy of a signal can be obtained by finding the area under the
  565. * squared signal. This takes the form of Sum(x_n^2), where x is the
  566. * amplitude for the sample n.
  567. *
  568. * Decaying feedback matches exponential decay of the form Sum(a^n),
  569. * where a is the attenuation coefficient, and n is the sample. The area
  570. * under this decay curve can be calculated as: 1 / (1 - a).
  571. *
  572. * Modifying the above equation to find the area under the squared curve
  573. * (for energy) yields: 1 / (1 - a^2). Input attenuation can then be
  574. * calculated by inverting the square root of this approximation,
  575. * yielding: 1 / sqrt(1 / (1 - a^2)), simplified to: sqrt(1 - a^2).
  576. */
  577. return sqrtf(1.0f - a*a);
  578. }
  579. /* Calculate the scattering matrix coefficients given a diffusion factor. */
  580. static inline ALvoid CalcMatrixCoeffs(const ALfloat diffusion, ALfloat *x, ALfloat *y)
  581. {
  582. ALfloat n, t;
  583. /* The matrix is of order 4, so n is sqrt(4 - 1). */
  584. n = sqrtf(3.0f);
  585. t = diffusion * atanf(n);
  586. /* Calculate the first mixing matrix coefficient. */
  587. *x = cosf(t);
  588. /* Calculate the second mixing matrix coefficient. */
  589. *y = sinf(t) / n;
  590. }
  591. /* Calculate the limited HF ratio for use with the late reverb low-pass
  592. * filters.
  593. */
  594. static ALfloat CalcLimitedHfRatio(const ALfloat hfRatio, const ALfloat airAbsorptionGainHF,
  595. const ALfloat decayTime, const ALfloat SpeedOfSound)
  596. {
  597. ALfloat limitRatio;
  598. /* Find the attenuation due to air absorption in dB (converting delay
  599. * time to meters using the speed of sound). Then reversing the decay
  600. * equation, solve for HF ratio. The delay length is cancelled out of
  601. * the equation, so it can be calculated once for all lines.
  602. */
  603. limitRatio = 1.0f / (CalcDecayLength(airAbsorptionGainHF, decayTime) * SpeedOfSound);
  604. /* Using the limit calculated above, apply the upper bound to the HF ratio.
  605. */
  606. return minf(limitRatio, hfRatio);
  607. }
  608. /* Calculates the 3-band T60 damping coefficients for a particular delay line
  609. * of specified length, using a combination of two shelf filter sections given
  610. * decay times for each band split at two reference frequencies.
  611. */
  612. static void CalcT60DampingCoeffs(const ALfloat length, const ALfloat lfDecayTime,
  613. const ALfloat mfDecayTime, const ALfloat hfDecayTime,
  614. const ALfloat lf0norm, const ALfloat hf0norm,
  615. T60Filter *filter)
  616. {
  617. ALfloat lfGain = CalcDecayCoeff(length, lfDecayTime);
  618. ALfloat mfGain = CalcDecayCoeff(length, mfDecayTime);
  619. ALfloat hfGain = CalcDecayCoeff(length, hfDecayTime);
  620. filter->MidGain[1] = mfGain;
  621. BiquadFilter_setParams(&filter->LFFilter, BiquadType_LowShelf, lfGain/mfGain, lf0norm,
  622. calc_rcpQ_from_slope(lfGain/mfGain, 1.0f));
  623. BiquadFilter_setParams(&filter->HFFilter, BiquadType_HighShelf, hfGain/mfGain, hf0norm,
  624. calc_rcpQ_from_slope(hfGain/mfGain, 1.0f));
  625. }
  626. /* Update the offsets for the main effect delay line. */
  627. static ALvoid UpdateDelayLine(const ALfloat earlyDelay, const ALfloat lateDelay, const ALfloat density, const ALfloat decayTime, const ALuint frequency, ReverbState *State)
  628. {
  629. ALfloat multiplier, length;
  630. ALuint i;
  631. multiplier = CalcDelayLengthMult(density);
  632. /* Early reflection taps are decorrelated by means of an average room
  633. * reflection approximation described above the definition of the taps.
  634. * This approximation is linear and so the above density multiplier can
  635. * be applied to adjust the width of the taps. A single-band decay
  636. * coefficient is applied to simulate initial attenuation and absorption.
  637. *
  638. * Late reverb taps are based on the late line lengths to allow a zero-
  639. * delay path and offsets that would continue the propagation naturally
  640. * into the late lines.
  641. */
  642. for(i = 0;i < NUM_LINES;i++)
  643. {
  644. length = earlyDelay + EARLY_TAP_LENGTHS[i]*multiplier;
  645. State->EarlyDelayTap[i][1] = float2int(length * frequency);
  646. length = EARLY_TAP_LENGTHS[i]*multiplier;
  647. State->EarlyDelayCoeff[i][1] = CalcDecayCoeff(length, decayTime);
  648. length = lateDelay + (LATE_LINE_LENGTHS[i] - LATE_LINE_LENGTHS[0])*0.25f*multiplier;
  649. State->LateDelayTap[i][1] = State->LateFeedTap + float2int(length * frequency);
  650. }
  651. }
  652. /* Update the early reflection line lengths and gain coefficients. */
  653. static ALvoid UpdateEarlyLines(const ALfloat density, const ALfloat diffusion, const ALfloat decayTime, const ALuint frequency, EarlyReflections *Early)
  654. {
  655. ALfloat multiplier, length;
  656. ALsizei i;
  657. multiplier = CalcDelayLengthMult(density);
  658. /* Calculate the all-pass feed-back/forward coefficient. */
  659. Early->VecAp.Coeff = sqrtf(0.5f) * powf(diffusion, 2.0f);
  660. for(i = 0;i < NUM_LINES;i++)
  661. {
  662. /* Calculate the length (in seconds) of each all-pass line. */
  663. length = EARLY_ALLPASS_LENGTHS[i] * multiplier;
  664. /* Calculate the delay offset for each all-pass line. */
  665. Early->VecAp.Offset[i][1] = float2int(length * frequency);
  666. /* Calculate the length (in seconds) of each delay line. */
  667. length = EARLY_LINE_LENGTHS[i] * multiplier;
  668. /* Calculate the delay offset for each delay line. */
  669. Early->Offset[i][1] = float2int(length * frequency);
  670. /* Calculate the gain (coefficient) for each line. */
  671. Early->Coeff[i][1] = CalcDecayCoeff(length, decayTime);
  672. }
  673. }
  674. /* Update the late reverb line lengths and T60 coefficients. */
  675. static ALvoid UpdateLateLines(const ALfloat density, const ALfloat diffusion, const ALfloat lfDecayTime, const ALfloat mfDecayTime, const ALfloat hfDecayTime, const ALfloat lf0norm, const ALfloat hf0norm, const ALuint frequency, LateReverb *Late)
  676. {
  677. /* Scaling factor to convert the normalized reference frequencies from
  678. * representing 0...freq to 0...max_reference.
  679. */
  680. const ALfloat norm_weight_factor = (ALfloat)frequency / AL_EAXREVERB_MAX_HFREFERENCE;
  681. ALfloat multiplier, length, bandWeights[3];
  682. ALsizei i;
  683. /* To compensate for changes in modal density and decay time of the late
  684. * reverb signal, the input is attenuated based on the maximal energy of
  685. * the outgoing signal. This approximation is used to keep the apparent
  686. * energy of the signal equal for all ranges of density and decay time.
  687. *
  688. * The average length of the delay lines is used to calculate the
  689. * attenuation coefficient.
  690. */
  691. multiplier = CalcDelayLengthMult(density);
  692. length = (LATE_LINE_LENGTHS[0] + LATE_LINE_LENGTHS[1] +
  693. LATE_LINE_LENGTHS[2] + LATE_LINE_LENGTHS[3]) / 4.0f * multiplier;
  694. length += (LATE_ALLPASS_LENGTHS[0] + LATE_ALLPASS_LENGTHS[1] +
  695. LATE_ALLPASS_LENGTHS[2] + LATE_ALLPASS_LENGTHS[3]) / 4.0f * multiplier;
  696. /* The density gain calculation uses an average decay time weighted by
  697. * approximate bandwidth. This attempts to compensate for losses of energy
  698. * that reduce decay time due to scattering into highly attenuated bands.
  699. */
  700. bandWeights[0] = lf0norm*norm_weight_factor;
  701. bandWeights[1] = hf0norm*norm_weight_factor - lf0norm*norm_weight_factor;
  702. bandWeights[2] = 1.0f - hf0norm*norm_weight_factor;
  703. Late->DensityGain[1] = CalcDensityGain(
  704. CalcDecayCoeff(length,
  705. bandWeights[0]*lfDecayTime + bandWeights[1]*mfDecayTime + bandWeights[2]*hfDecayTime
  706. )
  707. );
  708. /* Calculate the all-pass feed-back/forward coefficient. */
  709. Late->VecAp.Coeff = sqrtf(0.5f) * powf(diffusion, 2.0f);
  710. for(i = 0;i < NUM_LINES;i++)
  711. {
  712. /* Calculate the length (in seconds) of each all-pass line. */
  713. length = LATE_ALLPASS_LENGTHS[i] * multiplier;
  714. /* Calculate the delay offset for each all-pass line. */
  715. Late->VecAp.Offset[i][1] = float2int(length * frequency);
  716. /* Calculate the length (in seconds) of each delay line. */
  717. length = LATE_LINE_LENGTHS[i] * multiplier;
  718. /* Calculate the delay offset for each delay line. */
  719. Late->Offset[i][1] = float2int(length*frequency + 0.5f);
  720. /* Approximate the absorption that the vector all-pass would exhibit
  721. * given the current diffusion so we don't have to process a full T60
  722. * filter for each of its four lines.
  723. */
  724. length += lerp(LATE_ALLPASS_LENGTHS[i],
  725. (LATE_ALLPASS_LENGTHS[0] + LATE_ALLPASS_LENGTHS[1] +
  726. LATE_ALLPASS_LENGTHS[2] + LATE_ALLPASS_LENGTHS[3]) / 4.0f,
  727. diffusion) * multiplier;
  728. /* Calculate the T60 damping coefficients for each line. */
  729. CalcT60DampingCoeffs(length, lfDecayTime, mfDecayTime, hfDecayTime,
  730. lf0norm, hf0norm, &Late->T60[i]);
  731. }
  732. }
  733. /* Creates a transform matrix given a reverb vector. The vector pans the reverb
  734. * reflections toward the given direction, using its magnitude (up to 1) as a
  735. * focal strength. This function results in a B-Format transformation matrix
  736. * that spatially focuses the signal in the desired direction.
  737. */
  738. static aluMatrixf GetTransformFromVector(const ALfloat *vec)
  739. {
  740. aluMatrixf focus;
  741. ALfloat norm[3];
  742. ALfloat mag;
  743. /* Normalize the panning vector according to the N3D scale, which has an
  744. * extra sqrt(3) term on the directional components. Converting from OpenAL
  745. * to B-Format also requires negating X (ACN 1) and Z (ACN 3). Note however
  746. * that the reverb panning vectors use left-handed coordinates, unlike the
  747. * rest of OpenAL which use right-handed. This is fixed by negating Z,
  748. * which cancels out with the B-Format Z negation.
  749. */
  750. mag = sqrtf(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
  751. if(mag > 1.0f)
  752. {
  753. norm[0] = vec[0] / mag * -SQRTF_3;
  754. norm[1] = vec[1] / mag * SQRTF_3;
  755. norm[2] = vec[2] / mag * SQRTF_3;
  756. mag = 1.0f;
  757. }
  758. else
  759. {
  760. /* If the magnitude is less than or equal to 1, just apply the sqrt(3)
  761. * term. There's no need to renormalize the magnitude since it would
  762. * just be reapplied in the matrix.
  763. */
  764. norm[0] = vec[0] * -SQRTF_3;
  765. norm[1] = vec[1] * SQRTF_3;
  766. norm[2] = vec[2] * SQRTF_3;
  767. }
  768. aluMatrixfSet(&focus,
  769. 1.0f, 0.0f, 0.0f, 0.0f,
  770. norm[0], 1.0f-mag, 0.0f, 0.0f,
  771. norm[1], 0.0f, 1.0f-mag, 0.0f,
  772. norm[2], 0.0f, 0.0f, 1.0f-mag
  773. );
  774. return focus;
  775. }
  776. /* Update the early and late 3D panning gains. */
  777. static ALvoid Update3DPanning(const ALCdevice *Device, const ALfloat *ReflectionsPan, const ALfloat *LateReverbPan, const ALfloat earlyGain, const ALfloat lateGain, ReverbState *State)
  778. {
  779. aluMatrixf transform, rot;
  780. ALsizei i;
  781. STATIC_CAST(ALeffectState,State)->OutBuffer = Device->FOAOut.Buffer;
  782. STATIC_CAST(ALeffectState,State)->OutChannels = Device->FOAOut.NumChannels;
  783. /* Note: _res is transposed. */
  784. #define MATRIX_MULT(_res, _m1, _m2) do { \
  785. int row, col; \
  786. for(col = 0;col < 4;col++) \
  787. { \
  788. for(row = 0;row < 4;row++) \
  789. _res.m[col][row] = _m1.m[row][0]*_m2.m[0][col] + _m1.m[row][1]*_m2.m[1][col] + \
  790. _m1.m[row][2]*_m2.m[2][col] + _m1.m[row][3]*_m2.m[3][col]; \
  791. } \
  792. } while(0)
  793. /* Create a matrix that first converts A-Format to B-Format, then
  794. * transforms the B-Format signal according to the panning vector.
  795. */
  796. rot = GetTransformFromVector(ReflectionsPan);
  797. MATRIX_MULT(transform, rot, A2B);
  798. memset(&State->Early.PanGain, 0, sizeof(State->Early.PanGain));
  799. for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
  800. ComputePanGains(&Device->FOAOut, transform.m[i], earlyGain,
  801. State->Early.PanGain[i]);
  802. rot = GetTransformFromVector(LateReverbPan);
  803. MATRIX_MULT(transform, rot, A2B);
  804. memset(&State->Late.PanGain, 0, sizeof(State->Late.PanGain));
  805. for(i = 0;i < MAX_EFFECT_CHANNELS;i++)
  806. ComputePanGains(&Device->FOAOut, transform.m[i], lateGain,
  807. State->Late.PanGain[i]);
  808. #undef MATRIX_MULT
  809. }
  810. static void ReverbState_update(ReverbState *State, const ALCcontext *Context, const ALeffectslot *Slot, const ALeffectProps *props)
  811. {
  812. const ALCdevice *Device = Context->Device;
  813. const ALlistener *Listener = Context->Listener;
  814. ALuint frequency = Device->Frequency;
  815. ALfloat lf0norm, hf0norm, hfRatio;
  816. ALfloat lfDecayTime, hfDecayTime;
  817. ALfloat gain, gainlf, gainhf;
  818. ALsizei i;
  819. /* Calculate the master filters */
  820. hf0norm = minf(props->Reverb.HFReference / frequency, 0.49f);
  821. /* Restrict the filter gains from going below -60dB to keep the filter from
  822. * killing most of the signal.
  823. */
  824. gainhf = maxf(props->Reverb.GainHF, 0.001f);
  825. BiquadFilter_setParams(&State->Filter[0].Lp, BiquadType_HighShelf, gainhf, hf0norm,
  826. calc_rcpQ_from_slope(gainhf, 1.0f));
  827. lf0norm = minf(props->Reverb.LFReference / frequency, 0.49f);
  828. gainlf = maxf(props->Reverb.GainLF, 0.001f);
  829. BiquadFilter_setParams(&State->Filter[0].Hp, BiquadType_LowShelf, gainlf, lf0norm,
  830. calc_rcpQ_from_slope(gainlf, 1.0f));
  831. for(i = 1;i < NUM_LINES;i++)
  832. {
  833. BiquadFilter_copyParams(&State->Filter[i].Lp, &State->Filter[0].Lp);
  834. BiquadFilter_copyParams(&State->Filter[i].Hp, &State->Filter[0].Hp);
  835. }
  836. /* Update the main effect delay and associated taps. */
  837. UpdateDelayLine(props->Reverb.ReflectionsDelay, props->Reverb.LateReverbDelay,
  838. props->Reverb.Density, props->Reverb.DecayTime, frequency,
  839. State);
  840. /* Update the early lines. */
  841. UpdateEarlyLines(props->Reverb.Density, props->Reverb.Diffusion,
  842. props->Reverb.DecayTime, frequency, &State->Early);
  843. /* Get the mixing matrix coefficients. */
  844. CalcMatrixCoeffs(props->Reverb.Diffusion, &State->MixX, &State->MixY);
  845. /* If the HF limit parameter is flagged, calculate an appropriate limit
  846. * based on the air absorption parameter.
  847. */
  848. hfRatio = props->Reverb.DecayHFRatio;
  849. if(props->Reverb.DecayHFLimit && props->Reverb.AirAbsorptionGainHF < 1.0f)
  850. hfRatio = CalcLimitedHfRatio(hfRatio, props->Reverb.AirAbsorptionGainHF,
  851. props->Reverb.DecayTime, Listener->Params.ReverbSpeedOfSound
  852. );
  853. /* Calculate the LF/HF decay times. */
  854. lfDecayTime = clampf(props->Reverb.DecayTime * props->Reverb.DecayLFRatio,
  855. AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME);
  856. hfDecayTime = clampf(props->Reverb.DecayTime * hfRatio,
  857. AL_EAXREVERB_MIN_DECAY_TIME, AL_EAXREVERB_MAX_DECAY_TIME);
  858. /* Update the late lines. */
  859. UpdateLateLines(props->Reverb.Density, props->Reverb.Diffusion,
  860. lfDecayTime, props->Reverb.DecayTime, hfDecayTime, lf0norm, hf0norm,
  861. frequency, &State->Late
  862. );
  863. /* Update early and late 3D panning. */
  864. gain = props->Reverb.Gain * Slot->Params.Gain * ReverbBoost;
  865. Update3DPanning(Device, props->Reverb.ReflectionsPan, props->Reverb.LateReverbPan,
  866. props->Reverb.ReflectionsGain*gain, props->Reverb.LateReverbGain*gain,
  867. State);
  868. /* Calculate the max update size from the smallest relevant delay. */
  869. State->MaxUpdate[1] = mini(MAX_UPDATE_SAMPLES,
  870. mini(State->Early.Offset[0][1], State->Late.Offset[0][1])
  871. );
  872. /* Determine if delay-line cross-fading is required. Density is essentially
  873. * a master control for the feedback delays, so changes the offsets of many
  874. * delay lines.
  875. */
  876. if(State->Params.Density != props->Reverb.Density ||
  877. /* Diffusion and decay times influences the decay rate (gain) of the
  878. * late reverb T60 filter.
  879. */
  880. State->Params.Diffusion != props->Reverb.Diffusion ||
  881. State->Params.DecayTime != props->Reverb.DecayTime ||
  882. State->Params.HFDecayTime != hfDecayTime ||
  883. State->Params.LFDecayTime != lfDecayTime ||
  884. /* HF/LF References control the weighting used to calculate the density
  885. * gain.
  886. */
  887. State->Params.HFReference != props->Reverb.HFReference ||
  888. State->Params.LFReference != props->Reverb.LFReference)
  889. State->FadeCount = 0;
  890. State->Params.Density = props->Reverb.Density;
  891. State->Params.Diffusion = props->Reverb.Diffusion;
  892. State->Params.DecayTime = props->Reverb.DecayTime;
  893. State->Params.HFDecayTime = hfDecayTime;
  894. State->Params.LFDecayTime = lfDecayTime;
  895. State->Params.HFReference = props->Reverb.HFReference;
  896. State->Params.LFReference = props->Reverb.LFReference;
  897. }
  898. /**************************************
  899. * Effect Processing *
  900. **************************************/
  901. /* Basic delay line input/output routines. */
  902. static inline ALfloat DelayLineOut(const DelayLineI *Delay, const ALsizei offset, const ALsizei c)
  903. {
  904. return Delay->Line[offset&Delay->Mask][c];
  905. }
  906. /* Cross-faded delay line output routine. Instead of interpolating the
  907. * offsets, this interpolates (cross-fades) the outputs at each offset.
  908. */
  909. static inline ALfloat FadedDelayLineOut(const DelayLineI *Delay, const ALsizei off0,
  910. const ALsizei off1, const ALsizei c,
  911. const ALfloat sc0, const ALfloat sc1)
  912. {
  913. return Delay->Line[off0&Delay->Mask][c]*sc0 +
  914. Delay->Line[off1&Delay->Mask][c]*sc1;
  915. }
  916. static inline void DelayLineIn(const DelayLineI *Delay, ALsizei offset, const ALsizei c,
  917. const ALfloat *restrict in, ALsizei count)
  918. {
  919. ALsizei i;
  920. for(i = 0;i < count;i++)
  921. Delay->Line[(offset++)&Delay->Mask][c] = *(in++);
  922. }
  923. /* Applies a scattering matrix to the 4-line (vector) input. This is used
  924. * for both the below vector all-pass model and to perform modal feed-back
  925. * delay network (FDN) mixing.
  926. *
  927. * The matrix is derived from a skew-symmetric matrix to form a 4D rotation
  928. * matrix with a single unitary rotational parameter:
  929. *
  930. * [ d, a, b, c ] 1 = a^2 + b^2 + c^2 + d^2
  931. * [ -a, d, c, -b ]
  932. * [ -b, -c, d, a ]
  933. * [ -c, b, -a, d ]
  934. *
  935. * The rotation is constructed from the effect's diffusion parameter,
  936. * yielding:
  937. *
  938. * 1 = x^2 + 3 y^2
  939. *
  940. * Where a, b, and c are the coefficient y with differing signs, and d is the
  941. * coefficient x. The final matrix is thus:
  942. *
  943. * [ x, y, -y, y ] n = sqrt(matrix_order - 1)
  944. * [ -y, x, y, y ] t = diffusion_parameter * atan(n)
  945. * [ y, -y, x, y ] x = cos(t)
  946. * [ -y, -y, -y, x ] y = sin(t) / n
  947. *
  948. * Any square orthogonal matrix with an order that is a power of two will
  949. * work (where ^T is transpose, ^-1 is inverse):
  950. *
  951. * M^T = M^-1
  952. *
  953. * Using that knowledge, finding an appropriate matrix can be accomplished
  954. * naively by searching all combinations of:
  955. *
  956. * M = D + S - S^T
  957. *
  958. * Where D is a diagonal matrix (of x), and S is a triangular matrix (of y)
  959. * whose combination of signs are being iterated.
  960. */
  961. static inline void VectorPartialScatter(ALfloat *restrict out, const ALfloat *restrict in,
  962. const ALfloat xCoeff, const ALfloat yCoeff)
  963. {
  964. out[0] = xCoeff*in[0] + yCoeff*( in[1] + -in[2] + in[3]);
  965. out[1] = xCoeff*in[1] + yCoeff*(-in[0] + in[2] + in[3]);
  966. out[2] = xCoeff*in[2] + yCoeff*( in[0] + -in[1] + in[3]);
  967. out[3] = xCoeff*in[3] + yCoeff*(-in[0] + -in[1] + -in[2] );
  968. }
  969. #define VectorScatterDelayIn(delay, o, in, xcoeff, ycoeff) \
  970. VectorPartialScatter((delay)->Line[(o)&(delay)->Mask], in, xcoeff, ycoeff)
  971. /* Utilizes the above, but reverses the input channels. */
  972. static inline void VectorScatterRevDelayIn(const DelayLineI *Delay, ALint offset,
  973. const ALfloat xCoeff, const ALfloat yCoeff,
  974. const ALfloat (*restrict in)[MAX_UPDATE_SAMPLES],
  975. const ALsizei count)
  976. {
  977. const DelayLineI delay = *Delay;
  978. ALsizei i, j;
  979. for(i = 0;i < count;++i)
  980. {
  981. ALfloat f[NUM_LINES];
  982. for(j = 0;j < NUM_LINES;j++)
  983. f[NUM_LINES-1-j] = in[j][i];
  984. VectorScatterDelayIn(&delay, offset++, f, xCoeff, yCoeff);
  985. }
  986. }
  987. /* This applies a Gerzon multiple-in/multiple-out (MIMO) vector all-pass
  988. * filter to the 4-line input.
  989. *
  990. * It works by vectorizing a regular all-pass filter and replacing the delay
  991. * element with a scattering matrix (like the one above) and a diagonal
  992. * matrix of delay elements.
  993. *
  994. * Two static specializations are used for transitional (cross-faded) delay
  995. * line processing and non-transitional processing.
  996. */
  997. static void VectorAllpass_Unfaded(ALfloat (*restrict samples)[MAX_UPDATE_SAMPLES], ALsizei offset,
  998. const ALfloat xCoeff, const ALfloat yCoeff, ALsizei todo,
  999. VecAllpass *Vap)
  1000. {
  1001. const DelayLineI delay = Vap->Delay;
  1002. const ALfloat feedCoeff = Vap->Coeff;
  1003. ALsizei vap_offset[NUM_LINES];
  1004. ALsizei i, j;
  1005. ASSUME(todo > 0);
  1006. for(j = 0;j < NUM_LINES;j++)
  1007. vap_offset[j] = offset-Vap->Offset[j][0];
  1008. for(i = 0;i < todo;i++)
  1009. {
  1010. ALfloat f[NUM_LINES];
  1011. for(j = 0;j < NUM_LINES;j++)
  1012. {
  1013. ALfloat input = samples[j][i];
  1014. ALfloat out = DelayLineOut(&delay, vap_offset[j]++, j) - feedCoeff*input;
  1015. f[j] = input + feedCoeff*out;
  1016. samples[j][i] = out;
  1017. }
  1018. VectorScatterDelayIn(&delay, offset, f, xCoeff, yCoeff);
  1019. ++offset;
  1020. }
  1021. }
  1022. static void VectorAllpass_Faded(ALfloat (*restrict samples)[MAX_UPDATE_SAMPLES], ALsizei offset,
  1023. const ALfloat xCoeff, const ALfloat yCoeff, ALfloat fade,
  1024. ALsizei todo, VecAllpass *Vap)
  1025. {
  1026. const DelayLineI delay = Vap->Delay;
  1027. const ALfloat feedCoeff = Vap->Coeff;
  1028. ALsizei vap_offset[NUM_LINES][2];
  1029. ALsizei i, j;
  1030. ASSUME(todo > 0);
  1031. fade *= 1.0f/FADE_SAMPLES;
  1032. for(j = 0;j < NUM_LINES;j++)
  1033. {
  1034. vap_offset[j][0] = offset-Vap->Offset[j][0];
  1035. vap_offset[j][1] = offset-Vap->Offset[j][1];
  1036. }
  1037. for(i = 0;i < todo;i++)
  1038. {
  1039. ALfloat f[NUM_LINES];
  1040. for(j = 0;j < NUM_LINES;j++)
  1041. {
  1042. ALfloat input = samples[j][i];
  1043. ALfloat out =
  1044. FadedDelayLineOut(&delay, vap_offset[j][0]++, vap_offset[j][1]++, j,
  1045. 1.0f-fade, fade
  1046. ) - feedCoeff*input;
  1047. f[j] = input + feedCoeff*out;
  1048. samples[j][i] = out;
  1049. }
  1050. fade += FadeStep;
  1051. VectorScatterDelayIn(&delay, offset, f, xCoeff, yCoeff);
  1052. ++offset;
  1053. }
  1054. }
  1055. /* This generates early reflections.
  1056. *
  1057. * This is done by obtaining the primary reflections (those arriving from the
  1058. * same direction as the source) from the main delay line. These are
  1059. * attenuated and all-pass filtered (based on the diffusion parameter).
  1060. *
  1061. * The early lines are then fed in reverse (according to the approximately
  1062. * opposite spatial location of the A-Format lines) to create the secondary
  1063. * reflections (those arriving from the opposite direction as the source).
  1064. *
  1065. * The early response is then completed by combining the primary reflections
  1066. * with the delayed and attenuated output from the early lines.
  1067. *
  1068. * Finally, the early response is reversed, scattered (based on diffusion),
  1069. * and fed into the late reverb section of the main delay line.
  1070. *
  1071. * Two static specializations are used for transitional (cross-faded) delay
  1072. * line processing and non-transitional processing.
  1073. */
  1074. static void EarlyReflection_Unfaded(ReverbState *State, ALsizei offset, const ALsizei todo,
  1075. ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
  1076. {
  1077. ALfloat (*restrict temps)[MAX_UPDATE_SAMPLES] = State->TempSamples;
  1078. const DelayLineI early_delay = State->Early.Delay;
  1079. const DelayLineI main_delay = State->Delay;
  1080. const ALfloat mixX = State->MixX;
  1081. const ALfloat mixY = State->MixY;
  1082. ALsizei late_feed_tap;
  1083. ALsizei i, j;
  1084. ASSUME(todo > 0);
  1085. /* First, load decorrelated samples from the main delay line as the primary
  1086. * reflections.
  1087. */
  1088. for(j = 0;j < NUM_LINES;j++)
  1089. {
  1090. ALsizei early_delay_tap = offset - State->EarlyDelayTap[j][0];
  1091. ALfloat coeff = State->EarlyDelayCoeff[j][0];
  1092. for(i = 0;i < todo;i++)
  1093. temps[j][i] = DelayLineOut(&main_delay, early_delay_tap++, j) * coeff;
  1094. }
  1095. /* Apply a vector all-pass, to help color the initial reflections based on
  1096. * the diffusion strength.
  1097. */
  1098. VectorAllpass_Unfaded(temps, offset, mixX, mixY, todo, &State->Early.VecAp);
  1099. /* Apply a delay and bounce to generate secondary reflections, combine with
  1100. * the primary reflections and write out the result for mixing.
  1101. */
  1102. for(j = 0;j < NUM_LINES;j++)
  1103. {
  1104. ALint early_feedb_tap = offset - State->Early.Offset[j][0];
  1105. ALfloat early_feedb_coeff = State->Early.Coeff[j][0];
  1106. for(i = 0;i < todo;i++)
  1107. out[j][i] = DelayLineOut(&early_delay, early_feedb_tap++, j)*early_feedb_coeff +
  1108. temps[j][i];
  1109. }
  1110. for(j = 0;j < NUM_LINES;j++)
  1111. DelayLineIn(&early_delay, offset, NUM_LINES-1-j, temps[j], todo);
  1112. /* Also write the result back to the main delay line for the late reverb
  1113. * stage to pick up at the appropriate time, appplying a scatter and
  1114. * bounce to improve the initial diffusion in the late reverb.
  1115. */
  1116. late_feed_tap = offset - State->LateFeedTap;
  1117. VectorScatterRevDelayIn(&main_delay, late_feed_tap, mixX, mixY, out, todo);
  1118. }
  1119. static void EarlyReflection_Faded(ReverbState *State, ALsizei offset, const ALsizei todo,
  1120. const ALfloat fade, ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
  1121. {
  1122. ALfloat (*restrict temps)[MAX_UPDATE_SAMPLES] = State->TempSamples;
  1123. const DelayLineI early_delay = State->Early.Delay;
  1124. const DelayLineI main_delay = State->Delay;
  1125. const ALfloat mixX = State->MixX;
  1126. const ALfloat mixY = State->MixY;
  1127. ALsizei late_feed_tap;
  1128. ALsizei i, j;
  1129. ASSUME(todo > 0);
  1130. for(j = 0;j < NUM_LINES;j++)
  1131. {
  1132. ALsizei early_delay_tap0 = offset - State->EarlyDelayTap[j][0];
  1133. ALsizei early_delay_tap1 = offset - State->EarlyDelayTap[j][1];
  1134. ALfloat oldCoeff = State->EarlyDelayCoeff[j][0];
  1135. ALfloat oldCoeffStep = -oldCoeff / FADE_SAMPLES;
  1136. ALfloat newCoeffStep = State->EarlyDelayCoeff[j][1] / FADE_SAMPLES;
  1137. ALfloat fadeCount = fade;
  1138. for(i = 0;i < todo;i++)
  1139. {
  1140. const ALfloat fade0 = oldCoeff + oldCoeffStep*fadeCount;
  1141. const ALfloat fade1 = newCoeffStep*fadeCount;
  1142. temps[j][i] = FadedDelayLineOut(&main_delay,
  1143. early_delay_tap0++, early_delay_tap1++, j, fade0, fade1
  1144. );
  1145. fadeCount += 1.0f;
  1146. }
  1147. }
  1148. VectorAllpass_Faded(temps, offset, mixX, mixY, fade, todo, &State->Early.VecAp);
  1149. for(j = 0;j < NUM_LINES;j++)
  1150. {
  1151. ALint feedb_tap0 = offset - State->Early.Offset[j][0];
  1152. ALint feedb_tap1 = offset - State->Early.Offset[j][1];
  1153. ALfloat feedb_oldCoeff = State->Early.Coeff[j][0];
  1154. ALfloat feedb_oldCoeffStep = -feedb_oldCoeff / FADE_SAMPLES;
  1155. ALfloat feedb_newCoeffStep = State->Early.Coeff[j][1] / FADE_SAMPLES;
  1156. ALfloat fadeCount = fade;
  1157. for(i = 0;i < todo;i++)
  1158. {
  1159. const ALfloat fade0 = feedb_oldCoeff + feedb_oldCoeffStep*fadeCount;
  1160. const ALfloat fade1 = feedb_newCoeffStep*fadeCount;
  1161. out[j][i] = FadedDelayLineOut(&early_delay,
  1162. feedb_tap0++, feedb_tap1++, j, fade0, fade1
  1163. ) + temps[j][i];
  1164. fadeCount += 1.0f;
  1165. }
  1166. }
  1167. for(j = 0;j < NUM_LINES;j++)
  1168. DelayLineIn(&early_delay, offset, NUM_LINES-1-j, temps[j], todo);
  1169. late_feed_tap = offset - State->LateFeedTap;
  1170. VectorScatterRevDelayIn(&main_delay, late_feed_tap, mixX, mixY, out, todo);
  1171. }
  1172. /* Applies the two T60 damping filter sections. */
  1173. static inline void LateT60Filter(ALfloat *restrict samples, const ALsizei todo, T60Filter *filter)
  1174. {
  1175. ALfloat temp[MAX_UPDATE_SAMPLES];
  1176. BiquadFilter_process(&filter->HFFilter, temp, samples, todo);
  1177. BiquadFilter_process(&filter->LFFilter, samples, temp, todo);
  1178. }
  1179. /* This generates the reverb tail using a modified feed-back delay network
  1180. * (FDN).
  1181. *
  1182. * Results from the early reflections are mixed with the output from the late
  1183. * delay lines.
  1184. *
  1185. * The late response is then completed by T60 and all-pass filtering the mix.
  1186. *
  1187. * Finally, the lines are reversed (so they feed their opposite directions)
  1188. * and scattered with the FDN matrix before re-feeding the delay lines.
  1189. *
  1190. * Two variations are made, one for for transitional (cross-faded) delay line
  1191. * processing and one for non-transitional processing.
  1192. */
  1193. static void LateReverb_Unfaded(ReverbState *State, ALsizei offset, const ALsizei todo,
  1194. ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
  1195. {
  1196. ALfloat (*restrict temps)[MAX_UPDATE_SAMPLES] = State->TempSamples;
  1197. const DelayLineI late_delay = State->Late.Delay;
  1198. const DelayLineI main_delay = State->Delay;
  1199. const ALfloat mixX = State->MixX;
  1200. const ALfloat mixY = State->MixY;
  1201. ALsizei i, j;
  1202. ASSUME(todo > 0);
  1203. /* First, load decorrelated samples from the main and feedback delay lines.
  1204. * Filter the signal to apply its frequency-dependent decay.
  1205. */
  1206. for(j = 0;j < NUM_LINES;j++)
  1207. {
  1208. ALsizei late_delay_tap = offset - State->LateDelayTap[j][0];
  1209. ALsizei late_feedb_tap = offset - State->Late.Offset[j][0];
  1210. ALfloat midGain = State->Late.T60[j].MidGain[0];
  1211. const ALfloat densityGain = State->Late.DensityGain[0] * midGain;
  1212. for(i = 0;i < todo;i++)
  1213. temps[j][i] = DelayLineOut(&main_delay, late_delay_tap++, j)*densityGain +
  1214. DelayLineOut(&late_delay, late_feedb_tap++, j)*midGain;
  1215. LateT60Filter(temps[j], todo, &State->Late.T60[j]);
  1216. }
  1217. /* Apply a vector all-pass to improve micro-surface diffusion, and write
  1218. * out the results for mixing.
  1219. */
  1220. VectorAllpass_Unfaded(temps, offset, mixX, mixY, todo, &State->Late.VecAp);
  1221. for(j = 0;j < NUM_LINES;j++)
  1222. memcpy(out[j], temps[j], todo*sizeof(ALfloat));
  1223. /* Finally, scatter and bounce the results to refeed the feedback buffer. */
  1224. VectorScatterRevDelayIn(&late_delay, offset, mixX, mixY, out, todo);
  1225. }
  1226. static void LateReverb_Faded(ReverbState *State, ALsizei offset, const ALsizei todo,
  1227. const ALfloat fade, ALfloat (*restrict out)[MAX_UPDATE_SAMPLES])
  1228. {
  1229. ALfloat (*restrict temps)[MAX_UPDATE_SAMPLES] = State->TempSamples;
  1230. const DelayLineI late_delay = State->Late.Delay;
  1231. const DelayLineI main_delay = State->Delay;
  1232. const ALfloat mixX = State->MixX;
  1233. const ALfloat mixY = State->MixY;
  1234. ALsizei i, j;
  1235. ASSUME(todo > 0);
  1236. for(j = 0;j < NUM_LINES;j++)
  1237. {
  1238. const ALfloat oldMidGain = State->Late.T60[j].MidGain[0];
  1239. const ALfloat midGain = State->Late.T60[j].MidGain[1];
  1240. const ALfloat oldMidStep = -oldMidGain / FADE_SAMPLES;
  1241. const ALfloat midStep = midGain / FADE_SAMPLES;
  1242. const ALfloat oldDensityGain = State->Late.DensityGain[0] * oldMidGain;
  1243. const ALfloat densityGain = State->Late.DensityGain[1] * midGain;
  1244. const ALfloat oldDensityStep = -oldDensityGain / FADE_SAMPLES;
  1245. const ALfloat densityStep = densityGain / FADE_SAMPLES;
  1246. ALsizei late_delay_tap0 = offset - State->LateDelayTap[j][0];
  1247. ALsizei late_delay_tap1 = offset - State->LateDelayTap[j][1];
  1248. ALsizei late_feedb_tap0 = offset - State->Late.Offset[j][0];
  1249. ALsizei late_feedb_tap1 = offset - State->Late.Offset[j][1];
  1250. ALfloat fadeCount = fade;
  1251. for(i = 0;i < todo;i++)
  1252. {
  1253. const ALfloat fade0 = oldDensityGain + oldDensityStep*fadeCount;
  1254. const ALfloat fade1 = densityStep*fadeCount;
  1255. const ALfloat gfade0 = oldMidGain + oldMidStep*fadeCount;
  1256. const ALfloat gfade1 = midStep*fadeCount;
  1257. temps[j][i] =
  1258. FadedDelayLineOut(&main_delay, late_delay_tap0++, late_delay_tap1++, j,
  1259. fade0, fade1) +
  1260. FadedDelayLineOut(&late_delay, late_feedb_tap0++, late_feedb_tap1++, j,
  1261. gfade0, gfade1);
  1262. fadeCount += 1.0f;
  1263. }
  1264. LateT60Filter(temps[j], todo, &State->Late.T60[j]);
  1265. }
  1266. VectorAllpass_Faded(temps, offset, mixX, mixY, fade, todo, &State->Late.VecAp);
  1267. for(j = 0;j < NUM_LINES;j++)
  1268. memcpy(out[j], temps[j], todo*sizeof(ALfloat));
  1269. VectorScatterRevDelayIn(&late_delay, offset, mixX, mixY, temps, todo);
  1270. }
  1271. static ALvoid ReverbState_process(ReverbState *State, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  1272. {
  1273. ALfloat (*restrict afmt)[MAX_UPDATE_SAMPLES] = State->TempSamples;
  1274. ALfloat (*restrict samples)[MAX_UPDATE_SAMPLES] = State->MixSamples;
  1275. ALsizei fadeCount = State->FadeCount;
  1276. ALsizei offset = State->Offset;
  1277. ALsizei base, c;
  1278. /* Process reverb for these samples. */
  1279. for(base = 0;base < SamplesToDo;)
  1280. {
  1281. ALsizei todo = SamplesToDo - base;
  1282. /* If cross-fading, don't do more samples than there are to fade. */
  1283. if(FADE_SAMPLES-fadeCount > 0)
  1284. {
  1285. todo = mini(todo, FADE_SAMPLES-fadeCount);
  1286. todo = mini(todo, State->MaxUpdate[0]);
  1287. }
  1288. todo = mini(todo, State->MaxUpdate[1]);
  1289. /* If this is not the final update, ensure the update size is a
  1290. * multiple of 4 for the SIMD mixers.
  1291. */
  1292. if(todo < SamplesToDo-base)
  1293. todo &= ~3;
  1294. /* Convert B-Format to A-Format for processing. */
  1295. memset(afmt, 0, sizeof(*afmt)*NUM_LINES);
  1296. for(c = 0;c < NUM_LINES;c++)
  1297. MixRowSamples(afmt[c], B2A.m[c],
  1298. SamplesIn, MAX_EFFECT_CHANNELS, base, todo
  1299. );
  1300. /* Process the samples for reverb. */
  1301. for(c = 0;c < NUM_LINES;c++)
  1302. {
  1303. /* Band-pass the incoming samples. */
  1304. BiquadFilter_process(&State->Filter[c].Lp, samples[0], afmt[c], todo);
  1305. BiquadFilter_process(&State->Filter[c].Hp, samples[1], samples[0], todo);
  1306. /* Feed the initial delay line. */
  1307. DelayLineIn(&State->Delay, offset, c, samples[1], todo);
  1308. }
  1309. if(UNLIKELY(fadeCount < FADE_SAMPLES))
  1310. {
  1311. ALfloat fade = (ALfloat)fadeCount;
  1312. /* Generate early reflections. */
  1313. EarlyReflection_Faded(State, offset, todo, fade, samples);
  1314. /* Mix the A-Format results to output, implicitly converting back
  1315. * to B-Format.
  1316. */
  1317. for(c = 0;c < NUM_LINES;c++)
  1318. MixSamples(samples[c], NumChannels, SamplesOut,
  1319. State->Early.CurrentGain[c], State->Early.PanGain[c],
  1320. SamplesToDo-base, base, todo
  1321. );
  1322. /* Generate and mix late reverb. */
  1323. LateReverb_Faded(State, offset, todo, fade, samples);
  1324. for(c = 0;c < NUM_LINES;c++)
  1325. MixSamples(samples[c], NumChannels, SamplesOut,
  1326. State->Late.CurrentGain[c], State->Late.PanGain[c],
  1327. SamplesToDo-base, base, todo
  1328. );
  1329. /* Step fading forward. */
  1330. fadeCount += todo;
  1331. if(LIKELY(fadeCount >= FADE_SAMPLES))
  1332. {
  1333. /* Update the cross-fading delay line taps. */
  1334. fadeCount = FADE_SAMPLES;
  1335. for(c = 0;c < NUM_LINES;c++)
  1336. {
  1337. State->EarlyDelayTap[c][0] = State->EarlyDelayTap[c][1];
  1338. State->EarlyDelayCoeff[c][0] = State->EarlyDelayCoeff[c][1];
  1339. State->Early.VecAp.Offset[c][0] = State->Early.VecAp.Offset[c][1];
  1340. State->Early.Offset[c][0] = State->Early.Offset[c][1];
  1341. State->Early.Coeff[c][0] = State->Early.Coeff[c][1];
  1342. State->LateDelayTap[c][0] = State->LateDelayTap[c][1];
  1343. State->Late.VecAp.Offset[c][0] = State->Late.VecAp.Offset[c][1];
  1344. State->Late.Offset[c][0] = State->Late.Offset[c][1];
  1345. State->Late.T60[c].MidGain[0] = State->Late.T60[c].MidGain[1];
  1346. }
  1347. State->Late.DensityGain[0] = State->Late.DensityGain[1];
  1348. State->MaxUpdate[0] = State->MaxUpdate[1];
  1349. }
  1350. }
  1351. else
  1352. {
  1353. /* Generate and mix early reflections. */
  1354. EarlyReflection_Unfaded(State, offset, todo, samples);
  1355. for(c = 0;c < NUM_LINES;c++)
  1356. MixSamples(samples[c], NumChannels, SamplesOut,
  1357. State->Early.CurrentGain[c], State->Early.PanGain[c],
  1358. SamplesToDo-base, base, todo
  1359. );
  1360. /* Generate and mix late reverb. */
  1361. LateReverb_Unfaded(State, offset, todo, samples);
  1362. for(c = 0;c < NUM_LINES;c++)
  1363. MixSamples(samples[c], NumChannels, SamplesOut,
  1364. State->Late.CurrentGain[c], State->Late.PanGain[c],
  1365. SamplesToDo-base, base, todo
  1366. );
  1367. }
  1368. /* Step all delays forward. */
  1369. offset += todo;
  1370. base += todo;
  1371. }
  1372. State->Offset = offset;
  1373. State->FadeCount = fadeCount;
  1374. }
  1375. typedef struct ReverbStateFactory {
  1376. DERIVE_FROM_TYPE(EffectStateFactory);
  1377. } ReverbStateFactory;
  1378. static ALeffectState *ReverbStateFactory_create(ReverbStateFactory* UNUSED(factory))
  1379. {
  1380. ReverbState *state;
  1381. NEW_OBJ0(state, ReverbState)();
  1382. if(!state) return NULL;
  1383. return STATIC_CAST(ALeffectState, state);
  1384. }
  1385. DEFINE_EFFECTSTATEFACTORY_VTABLE(ReverbStateFactory);
  1386. EffectStateFactory *ReverbStateFactory_getFactory(void)
  1387. {
  1388. static ReverbStateFactory ReverbFactory = { { GET_VTABLE2(ReverbStateFactory, EffectStateFactory) } };
  1389. return STATIC_CAST(EffectStateFactory, &ReverbFactory);
  1390. }
  1391. void ALeaxreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  1392. {
  1393. ALeffectProps *props = &effect->Props;
  1394. switch(param)
  1395. {
  1396. case AL_EAXREVERB_DECAY_HFLIMIT:
  1397. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
  1398. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hflimit out of range");
  1399. props->Reverb.DecayHFLimit = val;
  1400. break;
  1401. default:
  1402. alSetError(context, AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
  1403. param);
  1404. }
  1405. }
  1406. void ALeaxreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  1407. { ALeaxreverb_setParami(effect, context, param, vals[0]); }
  1408. void ALeaxreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  1409. {
  1410. ALeffectProps *props = &effect->Props;
  1411. switch(param)
  1412. {
  1413. case AL_EAXREVERB_DENSITY:
  1414. if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
  1415. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb density out of range");
  1416. props->Reverb.Density = val;
  1417. break;
  1418. case AL_EAXREVERB_DIFFUSION:
  1419. if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
  1420. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb diffusion out of range");
  1421. props->Reverb.Diffusion = val;
  1422. break;
  1423. case AL_EAXREVERB_GAIN:
  1424. if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
  1425. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gain out of range");
  1426. props->Reverb.Gain = val;
  1427. break;
  1428. case AL_EAXREVERB_GAINHF:
  1429. if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
  1430. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainhf out of range");
  1431. props->Reverb.GainHF = val;
  1432. break;
  1433. case AL_EAXREVERB_GAINLF:
  1434. if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
  1435. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb gainlf out of range");
  1436. props->Reverb.GainLF = val;
  1437. break;
  1438. case AL_EAXREVERB_DECAY_TIME:
  1439. if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
  1440. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay time out of range");
  1441. props->Reverb.DecayTime = val;
  1442. break;
  1443. case AL_EAXREVERB_DECAY_HFRATIO:
  1444. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
  1445. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay hfratio out of range");
  1446. props->Reverb.DecayHFRatio = val;
  1447. break;
  1448. case AL_EAXREVERB_DECAY_LFRATIO:
  1449. if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
  1450. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb decay lfratio out of range");
  1451. props->Reverb.DecayLFRatio = val;
  1452. break;
  1453. case AL_EAXREVERB_REFLECTIONS_GAIN:
  1454. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
  1455. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections gain out of range");
  1456. props->Reverb.ReflectionsGain = val;
  1457. break;
  1458. case AL_EAXREVERB_REFLECTIONS_DELAY:
  1459. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
  1460. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections delay out of range");
  1461. props->Reverb.ReflectionsDelay = val;
  1462. break;
  1463. case AL_EAXREVERB_LATE_REVERB_GAIN:
  1464. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
  1465. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb gain out of range");
  1466. props->Reverb.LateReverbGain = val;
  1467. break;
  1468. case AL_EAXREVERB_LATE_REVERB_DELAY:
  1469. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
  1470. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb delay out of range");
  1471. props->Reverb.LateReverbDelay = val;
  1472. break;
  1473. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  1474. if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
  1475. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb air absorption gainhf out of range");
  1476. props->Reverb.AirAbsorptionGainHF = val;
  1477. break;
  1478. case AL_EAXREVERB_ECHO_TIME:
  1479. if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
  1480. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo time out of range");
  1481. props->Reverb.EchoTime = val;
  1482. break;
  1483. case AL_EAXREVERB_ECHO_DEPTH:
  1484. if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
  1485. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb echo depth out of range");
  1486. props->Reverb.EchoDepth = val;
  1487. break;
  1488. case AL_EAXREVERB_MODULATION_TIME:
  1489. if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
  1490. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation time out of range");
  1491. props->Reverb.ModulationTime = val;
  1492. break;
  1493. case AL_EAXREVERB_MODULATION_DEPTH:
  1494. if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
  1495. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb modulation depth out of range");
  1496. props->Reverb.ModulationDepth = val;
  1497. break;
  1498. case AL_EAXREVERB_HFREFERENCE:
  1499. if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
  1500. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb hfreference out of range");
  1501. props->Reverb.HFReference = val;
  1502. break;
  1503. case AL_EAXREVERB_LFREFERENCE:
  1504. if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
  1505. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb lfreference out of range");
  1506. props->Reverb.LFReference = val;
  1507. break;
  1508. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  1509. if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
  1510. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb room rolloff factor out of range");
  1511. props->Reverb.RoomRolloffFactor = val;
  1512. break;
  1513. default:
  1514. alSetError(context, AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x",
  1515. param);
  1516. }
  1517. }
  1518. void ALeaxreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  1519. {
  1520. ALeffectProps *props = &effect->Props;
  1521. switch(param)
  1522. {
  1523. case AL_EAXREVERB_REFLECTIONS_PAN:
  1524. if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
  1525. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb reflections pan out of range");
  1526. props->Reverb.ReflectionsPan[0] = vals[0];
  1527. props->Reverb.ReflectionsPan[1] = vals[1];
  1528. props->Reverb.ReflectionsPan[2] = vals[2];
  1529. break;
  1530. case AL_EAXREVERB_LATE_REVERB_PAN:
  1531. if(!(isfinite(vals[0]) && isfinite(vals[1]) && isfinite(vals[2])))
  1532. SETERR_RETURN(context, AL_INVALID_VALUE,, "EAX Reverb late reverb pan out of range");
  1533. props->Reverb.LateReverbPan[0] = vals[0];
  1534. props->Reverb.LateReverbPan[1] = vals[1];
  1535. props->Reverb.LateReverbPan[2] = vals[2];
  1536. break;
  1537. default:
  1538. ALeaxreverb_setParamf(effect, context, param, vals[0]);
  1539. break;
  1540. }
  1541. }
  1542. void ALeaxreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  1543. {
  1544. const ALeffectProps *props = &effect->Props;
  1545. switch(param)
  1546. {
  1547. case AL_EAXREVERB_DECAY_HFLIMIT:
  1548. *val = props->Reverb.DecayHFLimit;
  1549. break;
  1550. default:
  1551. alSetError(context, AL_INVALID_ENUM, "Invalid EAX reverb integer property 0x%04x",
  1552. param);
  1553. }
  1554. }
  1555. void ALeaxreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  1556. { ALeaxreverb_getParami(effect, context, param, vals); }
  1557. void ALeaxreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  1558. {
  1559. const ALeffectProps *props = &effect->Props;
  1560. switch(param)
  1561. {
  1562. case AL_EAXREVERB_DENSITY:
  1563. *val = props->Reverb.Density;
  1564. break;
  1565. case AL_EAXREVERB_DIFFUSION:
  1566. *val = props->Reverb.Diffusion;
  1567. break;
  1568. case AL_EAXREVERB_GAIN:
  1569. *val = props->Reverb.Gain;
  1570. break;
  1571. case AL_EAXREVERB_GAINHF:
  1572. *val = props->Reverb.GainHF;
  1573. break;
  1574. case AL_EAXREVERB_GAINLF:
  1575. *val = props->Reverb.GainLF;
  1576. break;
  1577. case AL_EAXREVERB_DECAY_TIME:
  1578. *val = props->Reverb.DecayTime;
  1579. break;
  1580. case AL_EAXREVERB_DECAY_HFRATIO:
  1581. *val = props->Reverb.DecayHFRatio;
  1582. break;
  1583. case AL_EAXREVERB_DECAY_LFRATIO:
  1584. *val = props->Reverb.DecayLFRatio;
  1585. break;
  1586. case AL_EAXREVERB_REFLECTIONS_GAIN:
  1587. *val = props->Reverb.ReflectionsGain;
  1588. break;
  1589. case AL_EAXREVERB_REFLECTIONS_DELAY:
  1590. *val = props->Reverb.ReflectionsDelay;
  1591. break;
  1592. case AL_EAXREVERB_LATE_REVERB_GAIN:
  1593. *val = props->Reverb.LateReverbGain;
  1594. break;
  1595. case AL_EAXREVERB_LATE_REVERB_DELAY:
  1596. *val = props->Reverb.LateReverbDelay;
  1597. break;
  1598. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  1599. *val = props->Reverb.AirAbsorptionGainHF;
  1600. break;
  1601. case AL_EAXREVERB_ECHO_TIME:
  1602. *val = props->Reverb.EchoTime;
  1603. break;
  1604. case AL_EAXREVERB_ECHO_DEPTH:
  1605. *val = props->Reverb.EchoDepth;
  1606. break;
  1607. case AL_EAXREVERB_MODULATION_TIME:
  1608. *val = props->Reverb.ModulationTime;
  1609. break;
  1610. case AL_EAXREVERB_MODULATION_DEPTH:
  1611. *val = props->Reverb.ModulationDepth;
  1612. break;
  1613. case AL_EAXREVERB_HFREFERENCE:
  1614. *val = props->Reverb.HFReference;
  1615. break;
  1616. case AL_EAXREVERB_LFREFERENCE:
  1617. *val = props->Reverb.LFReference;
  1618. break;
  1619. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  1620. *val = props->Reverb.RoomRolloffFactor;
  1621. break;
  1622. default:
  1623. alSetError(context, AL_INVALID_ENUM, "Invalid EAX reverb float property 0x%04x",
  1624. param);
  1625. }
  1626. }
  1627. void ALeaxreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  1628. {
  1629. const ALeffectProps *props = &effect->Props;
  1630. switch(param)
  1631. {
  1632. case AL_EAXREVERB_REFLECTIONS_PAN:
  1633. vals[0] = props->Reverb.ReflectionsPan[0];
  1634. vals[1] = props->Reverb.ReflectionsPan[1];
  1635. vals[2] = props->Reverb.ReflectionsPan[2];
  1636. break;
  1637. case AL_EAXREVERB_LATE_REVERB_PAN:
  1638. vals[0] = props->Reverb.LateReverbPan[0];
  1639. vals[1] = props->Reverb.LateReverbPan[1];
  1640. vals[2] = props->Reverb.LateReverbPan[2];
  1641. break;
  1642. default:
  1643. ALeaxreverb_getParamf(effect, context, param, vals);
  1644. break;
  1645. }
  1646. }
  1647. DEFINE_ALEFFECT_VTABLE(ALeaxreverb);
  1648. void ALreverb_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  1649. {
  1650. ALeffectProps *props = &effect->Props;
  1651. switch(param)
  1652. {
  1653. case AL_REVERB_DECAY_HFLIMIT:
  1654. if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
  1655. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hflimit out of range");
  1656. props->Reverb.DecayHFLimit = val;
  1657. break;
  1658. default:
  1659. alSetError(context, AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
  1660. }
  1661. }
  1662. void ALreverb_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  1663. { ALreverb_setParami(effect, context, param, vals[0]); }
  1664. void ALreverb_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  1665. {
  1666. ALeffectProps *props = &effect->Props;
  1667. switch(param)
  1668. {
  1669. case AL_REVERB_DENSITY:
  1670. if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
  1671. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb density out of range");
  1672. props->Reverb.Density = val;
  1673. break;
  1674. case AL_REVERB_DIFFUSION:
  1675. if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
  1676. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb diffusion out of range");
  1677. props->Reverb.Diffusion = val;
  1678. break;
  1679. case AL_REVERB_GAIN:
  1680. if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
  1681. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gain out of range");
  1682. props->Reverb.Gain = val;
  1683. break;
  1684. case AL_REVERB_GAINHF:
  1685. if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
  1686. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb gainhf out of range");
  1687. props->Reverb.GainHF = val;
  1688. break;
  1689. case AL_REVERB_DECAY_TIME:
  1690. if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
  1691. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay time out of range");
  1692. props->Reverb.DecayTime = val;
  1693. break;
  1694. case AL_REVERB_DECAY_HFRATIO:
  1695. if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
  1696. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb decay hfratio out of range");
  1697. props->Reverb.DecayHFRatio = val;
  1698. break;
  1699. case AL_REVERB_REFLECTIONS_GAIN:
  1700. if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
  1701. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections gain out of range");
  1702. props->Reverb.ReflectionsGain = val;
  1703. break;
  1704. case AL_REVERB_REFLECTIONS_DELAY:
  1705. if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
  1706. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb reflections delay out of range");
  1707. props->Reverb.ReflectionsDelay = val;
  1708. break;
  1709. case AL_REVERB_LATE_REVERB_GAIN:
  1710. if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
  1711. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb gain out of range");
  1712. props->Reverb.LateReverbGain = val;
  1713. break;
  1714. case AL_REVERB_LATE_REVERB_DELAY:
  1715. if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
  1716. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb late reverb delay out of range");
  1717. props->Reverb.LateReverbDelay = val;
  1718. break;
  1719. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  1720. if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
  1721. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb air absorption gainhf out of range");
  1722. props->Reverb.AirAbsorptionGainHF = val;
  1723. break;
  1724. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  1725. if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
  1726. SETERR_RETURN(context, AL_INVALID_VALUE,, "Reverb room rolloff factor out of range");
  1727. props->Reverb.RoomRolloffFactor = val;
  1728. break;
  1729. default:
  1730. alSetError(context, AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
  1731. }
  1732. }
  1733. void ALreverb_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  1734. { ALreverb_setParamf(effect, context, param, vals[0]); }
  1735. void ALreverb_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  1736. {
  1737. const ALeffectProps *props = &effect->Props;
  1738. switch(param)
  1739. {
  1740. case AL_REVERB_DECAY_HFLIMIT:
  1741. *val = props->Reverb.DecayHFLimit;
  1742. break;
  1743. default:
  1744. alSetError(context, AL_INVALID_ENUM, "Invalid reverb integer property 0x%04x", param);
  1745. }
  1746. }
  1747. void ALreverb_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  1748. { ALreverb_getParami(effect, context, param, vals); }
  1749. void ALreverb_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  1750. {
  1751. const ALeffectProps *props = &effect->Props;
  1752. switch(param)
  1753. {
  1754. case AL_REVERB_DENSITY:
  1755. *val = props->Reverb.Density;
  1756. break;
  1757. case AL_REVERB_DIFFUSION:
  1758. *val = props->Reverb.Diffusion;
  1759. break;
  1760. case AL_REVERB_GAIN:
  1761. *val = props->Reverb.Gain;
  1762. break;
  1763. case AL_REVERB_GAINHF:
  1764. *val = props->Reverb.GainHF;
  1765. break;
  1766. case AL_REVERB_DECAY_TIME:
  1767. *val = props->Reverb.DecayTime;
  1768. break;
  1769. case AL_REVERB_DECAY_HFRATIO:
  1770. *val = props->Reverb.DecayHFRatio;
  1771. break;
  1772. case AL_REVERB_REFLECTIONS_GAIN:
  1773. *val = props->Reverb.ReflectionsGain;
  1774. break;
  1775. case AL_REVERB_REFLECTIONS_DELAY:
  1776. *val = props->Reverb.ReflectionsDelay;
  1777. break;
  1778. case AL_REVERB_LATE_REVERB_GAIN:
  1779. *val = props->Reverb.LateReverbGain;
  1780. break;
  1781. case AL_REVERB_LATE_REVERB_DELAY:
  1782. *val = props->Reverb.LateReverbDelay;
  1783. break;
  1784. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  1785. *val = props->Reverb.AirAbsorptionGainHF;
  1786. break;
  1787. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  1788. *val = props->Reverb.RoomRolloffFactor;
  1789. break;
  1790. default:
  1791. alSetError(context, AL_INVALID_ENUM, "Invalid reverb float property 0x%04x", param);
  1792. }
  1793. }
  1794. void ALreverb_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  1795. { ALreverb_getParamf(effect, context, param, vals); }
  1796. DEFINE_ALEFFECT_VTABLE(ALreverb);