reverb.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  1. #include "config.h"
  2. #include <algorithm>
  3. #include <array>
  4. #include <cmath>
  5. #include <variant>
  6. #include "AL/al.h"
  7. #include "AL/efx.h"
  8. #include "alc/context.h"
  9. #include "alnumeric.h"
  10. #include "alspan.h"
  11. #include "core/logging.h"
  12. #include "effects.h"
  13. #include "fmt/ranges.h"
  14. #include "opthelpers.h"
  15. #if ALSOFT_EAX
  16. #include <cassert>
  17. #include "al/eax/api.h"
  18. #include "al/eax/call.h"
  19. #include "al/eax/effect.h"
  20. #include "al/eax/exception.h"
  21. #include "al/eax/utils.h"
  22. #endif // ALSOFT_EAX
  23. namespace {
  24. constexpr EffectProps genDefaultProps() noexcept
  25. {
  26. ReverbProps props{};
  27. props.Density = AL_EAXREVERB_DEFAULT_DENSITY;
  28. props.Diffusion = AL_EAXREVERB_DEFAULT_DIFFUSION;
  29. props.Gain = AL_EAXREVERB_DEFAULT_GAIN;
  30. props.GainHF = AL_EAXREVERB_DEFAULT_GAINHF;
  31. props.GainLF = AL_EAXREVERB_DEFAULT_GAINLF;
  32. props.DecayTime = AL_EAXREVERB_DEFAULT_DECAY_TIME;
  33. props.DecayHFRatio = AL_EAXREVERB_DEFAULT_DECAY_HFRATIO;
  34. props.DecayLFRatio = AL_EAXREVERB_DEFAULT_DECAY_LFRATIO;
  35. props.ReflectionsGain = AL_EAXREVERB_DEFAULT_REFLECTIONS_GAIN;
  36. props.ReflectionsDelay = AL_EAXREVERB_DEFAULT_REFLECTIONS_DELAY;
  37. props.ReflectionsPan[0] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
  38. props.ReflectionsPan[1] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
  39. props.ReflectionsPan[2] = AL_EAXREVERB_DEFAULT_REFLECTIONS_PAN_XYZ;
  40. props.LateReverbGain = AL_EAXREVERB_DEFAULT_LATE_REVERB_GAIN;
  41. props.LateReverbDelay = AL_EAXREVERB_DEFAULT_LATE_REVERB_DELAY;
  42. props.LateReverbPan[0] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
  43. props.LateReverbPan[1] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
  44. props.LateReverbPan[2] = AL_EAXREVERB_DEFAULT_LATE_REVERB_PAN_XYZ;
  45. props.EchoTime = AL_EAXREVERB_DEFAULT_ECHO_TIME;
  46. props.EchoDepth = AL_EAXREVERB_DEFAULT_ECHO_DEPTH;
  47. props.ModulationTime = AL_EAXREVERB_DEFAULT_MODULATION_TIME;
  48. props.ModulationDepth = AL_EAXREVERB_DEFAULT_MODULATION_DEPTH;
  49. props.AirAbsorptionGainHF = AL_EAXREVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
  50. props.HFReference = AL_EAXREVERB_DEFAULT_HFREFERENCE;
  51. props.LFReference = AL_EAXREVERB_DEFAULT_LFREFERENCE;
  52. props.RoomRolloffFactor = AL_EAXREVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
  53. props.DecayHFLimit = AL_EAXREVERB_DEFAULT_DECAY_HFLIMIT;
  54. return props;
  55. }
  56. constexpr EffectProps genDefaultStdProps() noexcept
  57. {
  58. ReverbProps props{};
  59. props.Density = AL_REVERB_DEFAULT_DENSITY;
  60. props.Diffusion = AL_REVERB_DEFAULT_DIFFUSION;
  61. props.Gain = AL_REVERB_DEFAULT_GAIN;
  62. props.GainHF = AL_REVERB_DEFAULT_GAINHF;
  63. props.GainLF = 1.0f;
  64. props.DecayTime = AL_REVERB_DEFAULT_DECAY_TIME;
  65. props.DecayHFRatio = AL_REVERB_DEFAULT_DECAY_HFRATIO;
  66. props.DecayLFRatio = 1.0f;
  67. props.ReflectionsGain = AL_REVERB_DEFAULT_REFLECTIONS_GAIN;
  68. props.ReflectionsDelay = AL_REVERB_DEFAULT_REFLECTIONS_DELAY;
  69. props.ReflectionsPan = {0.0f, 0.0f, 0.0f};
  70. props.LateReverbGain = AL_REVERB_DEFAULT_LATE_REVERB_GAIN;
  71. props.LateReverbDelay = AL_REVERB_DEFAULT_LATE_REVERB_DELAY;
  72. props.LateReverbPan = {0.0f, 0.0f, 0.0f};
  73. props.EchoTime = 0.25f;
  74. props.EchoDepth = 0.0f;
  75. props.ModulationTime = 0.25f;
  76. props.ModulationDepth = 0.0f;
  77. props.AirAbsorptionGainHF = AL_REVERB_DEFAULT_AIR_ABSORPTION_GAINHF;
  78. props.HFReference = 5000.0f;
  79. props.LFReference = 250.0f;
  80. props.RoomRolloffFactor = AL_REVERB_DEFAULT_ROOM_ROLLOFF_FACTOR;
  81. props.DecayHFLimit = AL_REVERB_DEFAULT_DECAY_HFLIMIT;
  82. return props;
  83. }
  84. } // namespace
  85. const EffectProps ReverbEffectProps{genDefaultProps()};
  86. void ReverbEffectHandler::SetParami(ALCcontext *context, ReverbProps &props, ALenum param, int val)
  87. {
  88. switch(param)
  89. {
  90. case AL_EAXREVERB_DECAY_HFLIMIT:
  91. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFLIMIT && val <= AL_EAXREVERB_MAX_DECAY_HFLIMIT))
  92. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay hflimit out of range");
  93. props.DecayHFLimit = val != AL_FALSE;
  94. return;
  95. }
  96. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb integer property {:#04x}",
  97. as_unsigned(param));
  98. }
  99. void ReverbEffectHandler::SetParamiv(ALCcontext *context, ReverbProps &props, ALenum param, const int *vals)
  100. { SetParami(context, props, param, *vals); }
  101. void ReverbEffectHandler::SetParamf(ALCcontext *context, ReverbProps &props, ALenum param, float val)
  102. {
  103. switch(param)
  104. {
  105. case AL_EAXREVERB_DENSITY:
  106. if(!(val >= AL_EAXREVERB_MIN_DENSITY && val <= AL_EAXREVERB_MAX_DENSITY))
  107. context->throw_error(AL_INVALID_VALUE, "EAX Reverb density out of range");
  108. props.Density = val;
  109. return;
  110. case AL_EAXREVERB_DIFFUSION:
  111. if(!(val >= AL_EAXREVERB_MIN_DIFFUSION && val <= AL_EAXREVERB_MAX_DIFFUSION))
  112. context->throw_error(AL_INVALID_VALUE, "EAX Reverb diffusion out of range");
  113. props.Diffusion = val;
  114. return;
  115. case AL_EAXREVERB_GAIN:
  116. if(!(val >= AL_EAXREVERB_MIN_GAIN && val <= AL_EAXREVERB_MAX_GAIN))
  117. context->throw_error(AL_INVALID_VALUE, "EAX Reverb gain out of range");
  118. props.Gain = val;
  119. return;
  120. case AL_EAXREVERB_GAINHF:
  121. if(!(val >= AL_EAXREVERB_MIN_GAINHF && val <= AL_EAXREVERB_MAX_GAINHF))
  122. context->throw_error(AL_INVALID_VALUE, "EAX Reverb gainhf out of range");
  123. props.GainHF = val;
  124. return;
  125. case AL_EAXREVERB_GAINLF:
  126. if(!(val >= AL_EAXREVERB_MIN_GAINLF && val <= AL_EAXREVERB_MAX_GAINLF))
  127. context->throw_error(AL_INVALID_VALUE, "EAX Reverb gainlf out of range");
  128. props.GainLF = val;
  129. return;
  130. case AL_EAXREVERB_DECAY_TIME:
  131. if(!(val >= AL_EAXREVERB_MIN_DECAY_TIME && val <= AL_EAXREVERB_MAX_DECAY_TIME))
  132. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay time out of range");
  133. props.DecayTime = val;
  134. return;
  135. case AL_EAXREVERB_DECAY_HFRATIO:
  136. if(!(val >= AL_EAXREVERB_MIN_DECAY_HFRATIO && val <= AL_EAXREVERB_MAX_DECAY_HFRATIO))
  137. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay hfratio out of range");
  138. props.DecayHFRatio = val;
  139. return;
  140. case AL_EAXREVERB_DECAY_LFRATIO:
  141. if(!(val >= AL_EAXREVERB_MIN_DECAY_LFRATIO && val <= AL_EAXREVERB_MAX_DECAY_LFRATIO))
  142. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay lfratio out of range");
  143. props.DecayLFRatio = val;
  144. return;
  145. case AL_EAXREVERB_REFLECTIONS_GAIN:
  146. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_GAIN && val <= AL_EAXREVERB_MAX_REFLECTIONS_GAIN))
  147. context->throw_error(AL_INVALID_VALUE, "EAX Reverb reflections gain out of range");
  148. props.ReflectionsGain = val;
  149. return;
  150. case AL_EAXREVERB_REFLECTIONS_DELAY:
  151. if(!(val >= AL_EAXREVERB_MIN_REFLECTIONS_DELAY && val <= AL_EAXREVERB_MAX_REFLECTIONS_DELAY))
  152. context->throw_error(AL_INVALID_VALUE, "EAX Reverb reflections delay out of range");
  153. props.ReflectionsDelay = val;
  154. return;
  155. case AL_EAXREVERB_LATE_REVERB_GAIN:
  156. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_GAIN && val <= AL_EAXREVERB_MAX_LATE_REVERB_GAIN))
  157. context->throw_error(AL_INVALID_VALUE, "EAX Reverb late reverb gain out of range");
  158. props.LateReverbGain = val;
  159. return;
  160. case AL_EAXREVERB_LATE_REVERB_DELAY:
  161. if(!(val >= AL_EAXREVERB_MIN_LATE_REVERB_DELAY && val <= AL_EAXREVERB_MAX_LATE_REVERB_DELAY))
  162. context->throw_error(AL_INVALID_VALUE, "EAX Reverb late reverb delay out of range");
  163. props.LateReverbDelay = val;
  164. return;
  165. case AL_EAXREVERB_ECHO_TIME:
  166. if(!(val >= AL_EAXREVERB_MIN_ECHO_TIME && val <= AL_EAXREVERB_MAX_ECHO_TIME))
  167. context->throw_error(AL_INVALID_VALUE, "EAX Reverb echo time out of range");
  168. props.EchoTime = val;
  169. return;
  170. case AL_EAXREVERB_ECHO_DEPTH:
  171. if(!(val >= AL_EAXREVERB_MIN_ECHO_DEPTH && val <= AL_EAXREVERB_MAX_ECHO_DEPTH))
  172. context->throw_error(AL_INVALID_VALUE, "EAX Reverb echo depth out of range");
  173. props.EchoDepth = val;
  174. return;
  175. case AL_EAXREVERB_MODULATION_TIME:
  176. if(!(val >= AL_EAXREVERB_MIN_MODULATION_TIME && val <= AL_EAXREVERB_MAX_MODULATION_TIME))
  177. context->throw_error(AL_INVALID_VALUE, "EAX Reverb modulation time out of range");
  178. props.ModulationTime = val;
  179. return;
  180. case AL_EAXREVERB_MODULATION_DEPTH:
  181. if(!(val >= AL_EAXREVERB_MIN_MODULATION_DEPTH && val <= AL_EAXREVERB_MAX_MODULATION_DEPTH))
  182. context->throw_error(AL_INVALID_VALUE, "EAX Reverb modulation depth out of range");
  183. props.ModulationDepth = val;
  184. return;
  185. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF:
  186. if(!(val >= AL_EAXREVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_EAXREVERB_MAX_AIR_ABSORPTION_GAINHF))
  187. context->throw_error(AL_INVALID_VALUE, "EAX Reverb air absorption gainhf out of range");
  188. props.AirAbsorptionGainHF = val;
  189. return;
  190. case AL_EAXREVERB_HFREFERENCE:
  191. if(!(val >= AL_EAXREVERB_MIN_HFREFERENCE && val <= AL_EAXREVERB_MAX_HFREFERENCE))
  192. context->throw_error(AL_INVALID_VALUE, "EAX Reverb hfreference out of range");
  193. props.HFReference = val;
  194. return;
  195. case AL_EAXREVERB_LFREFERENCE:
  196. if(!(val >= AL_EAXREVERB_MIN_LFREFERENCE && val <= AL_EAXREVERB_MAX_LFREFERENCE))
  197. context->throw_error(AL_INVALID_VALUE, "EAX Reverb lfreference out of range");
  198. props.LFReference = val;
  199. return;
  200. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR:
  201. if(!(val >= AL_EAXREVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_EAXREVERB_MAX_ROOM_ROLLOFF_FACTOR))
  202. context->throw_error(AL_INVALID_VALUE, "EAX Reverb room rolloff factor out of range");
  203. props.RoomRolloffFactor = val;
  204. return;
  205. }
  206. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb float property {:#04x}",
  207. as_unsigned(param));
  208. }
  209. void ReverbEffectHandler::SetParamfv(ALCcontext *context, ReverbProps &props, ALenum param, const float *vals)
  210. {
  211. static constexpr auto finite_checker = [](float f) -> bool { return std::isfinite(f); };
  212. al::span<const float> values;
  213. switch(param)
  214. {
  215. case AL_EAXREVERB_REFLECTIONS_PAN:
  216. values = {vals, 3_uz};
  217. if(!std::all_of(values.cbegin(), values.cend(), finite_checker))
  218. context->throw_error(AL_INVALID_VALUE, "EAX Reverb reflections pan out of range");
  219. std::copy(values.cbegin(), values.cend(), props.ReflectionsPan.begin());
  220. return;
  221. case AL_EAXREVERB_LATE_REVERB_PAN:
  222. values = {vals, 3_uz};
  223. if(!std::all_of(values.cbegin(), values.cend(), finite_checker))
  224. context->throw_error(AL_INVALID_VALUE, "EAX Reverb late reverb pan out of range");
  225. std::copy(values.cbegin(), values.cend(), props.LateReverbPan.begin());
  226. return;
  227. }
  228. SetParamf(context, props, param, *vals);
  229. }
  230. void ReverbEffectHandler::GetParami(ALCcontext *context, const ReverbProps &props, ALenum param, int *val)
  231. {
  232. switch(param)
  233. {
  234. case AL_EAXREVERB_DECAY_HFLIMIT: *val = props.DecayHFLimit; return;
  235. }
  236. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb integer property {:#04x}",
  237. as_unsigned(param));
  238. }
  239. void ReverbEffectHandler::GetParamiv(ALCcontext *context, const ReverbProps &props, ALenum param, int *vals)
  240. { GetParami(context, props, param, vals); }
  241. void ReverbEffectHandler::GetParamf(ALCcontext *context, const ReverbProps &props, ALenum param, float *val)
  242. {
  243. switch(param)
  244. {
  245. case AL_EAXREVERB_DENSITY: *val = props.Density; return;
  246. case AL_EAXREVERB_DIFFUSION: *val = props.Diffusion; return;
  247. case AL_EAXREVERB_GAIN: *val = props.Gain; return;
  248. case AL_EAXREVERB_GAINHF: *val = props.GainHF; return;
  249. case AL_EAXREVERB_GAINLF: *val = props.GainLF; return;
  250. case AL_EAXREVERB_DECAY_TIME: *val = props.DecayTime; return;
  251. case AL_EAXREVERB_DECAY_HFRATIO: *val = props.DecayHFRatio; return;
  252. case AL_EAXREVERB_DECAY_LFRATIO: *val = props.DecayLFRatio; return;
  253. case AL_EAXREVERB_REFLECTIONS_GAIN: *val = props.ReflectionsGain; return;
  254. case AL_EAXREVERB_REFLECTIONS_DELAY: *val = props.ReflectionsDelay; return;
  255. case AL_EAXREVERB_LATE_REVERB_GAIN: *val = props.LateReverbGain; return;
  256. case AL_EAXREVERB_LATE_REVERB_DELAY: *val = props.LateReverbDelay; return;
  257. case AL_EAXREVERB_ECHO_TIME: *val = props.EchoTime; return;
  258. case AL_EAXREVERB_ECHO_DEPTH: *val = props.EchoDepth; return;
  259. case AL_EAXREVERB_MODULATION_TIME: *val = props.ModulationTime; return;
  260. case AL_EAXREVERB_MODULATION_DEPTH: *val = props.ModulationDepth; return;
  261. case AL_EAXREVERB_AIR_ABSORPTION_GAINHF: *val = props.AirAbsorptionGainHF; return;
  262. case AL_EAXREVERB_HFREFERENCE: *val = props.HFReference; return;
  263. case AL_EAXREVERB_LFREFERENCE: *val = props.LFReference; return;
  264. case AL_EAXREVERB_ROOM_ROLLOFF_FACTOR: *val = props.RoomRolloffFactor; return;
  265. }
  266. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb float property {:#04x}",
  267. as_unsigned(param));
  268. }
  269. void ReverbEffectHandler::GetParamfv(ALCcontext *context, const ReverbProps &props, ALenum param, float *vals)
  270. {
  271. al::span<float> values;
  272. switch(param)
  273. {
  274. case AL_EAXREVERB_REFLECTIONS_PAN:
  275. values = {vals, 3_uz};
  276. std::copy(props.ReflectionsPan.cbegin(), props.ReflectionsPan.cend(), values.begin());
  277. return;
  278. case AL_EAXREVERB_LATE_REVERB_PAN:
  279. values = {vals, 3_uz};
  280. std::copy(props.LateReverbPan.cbegin(), props.LateReverbPan.cend(), values.begin());
  281. return;
  282. }
  283. GetParamf(context, props, param, vals);
  284. }
  285. const EffectProps StdReverbEffectProps{genDefaultStdProps()};
  286. void StdReverbEffectHandler::SetParami(ALCcontext *context, ReverbProps &props, ALenum param, int val)
  287. {
  288. switch(param)
  289. {
  290. case AL_REVERB_DECAY_HFLIMIT:
  291. if(!(val >= AL_REVERB_MIN_DECAY_HFLIMIT && val <= AL_REVERB_MAX_DECAY_HFLIMIT))
  292. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay hflimit out of range");
  293. props.DecayHFLimit = val != AL_FALSE;
  294. return;
  295. }
  296. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb integer property {:#04x}",
  297. as_unsigned(param));
  298. }
  299. void StdReverbEffectHandler::SetParamiv(ALCcontext *context, ReverbProps &props, ALenum param, const int *vals)
  300. { SetParami(context, props, param, *vals); }
  301. void StdReverbEffectHandler::SetParamf(ALCcontext *context, ReverbProps &props, ALenum param, float val)
  302. {
  303. switch(param)
  304. {
  305. case AL_REVERB_DENSITY:
  306. if(!(val >= AL_REVERB_MIN_DENSITY && val <= AL_REVERB_MAX_DENSITY))
  307. context->throw_error(AL_INVALID_VALUE, "EAX Reverb density out of range");
  308. props.Density = val;
  309. return;
  310. case AL_REVERB_DIFFUSION:
  311. if(!(val >= AL_REVERB_MIN_DIFFUSION && val <= AL_REVERB_MAX_DIFFUSION))
  312. context->throw_error(AL_INVALID_VALUE, "EAX Reverb diffusion out of range");
  313. props.Diffusion = val;
  314. return;
  315. case AL_REVERB_GAIN:
  316. if(!(val >= AL_REVERB_MIN_GAIN && val <= AL_REVERB_MAX_GAIN))
  317. context->throw_error(AL_INVALID_VALUE, "EAX Reverb gain out of range");
  318. props.Gain = val;
  319. return;
  320. case AL_REVERB_GAINHF:
  321. if(!(val >= AL_REVERB_MIN_GAINHF && val <= AL_REVERB_MAX_GAINHF))
  322. context->throw_error(AL_INVALID_VALUE, "EAX Reverb gainhf out of range");
  323. props.GainHF = val;
  324. return;
  325. case AL_REVERB_DECAY_TIME:
  326. if(!(val >= AL_REVERB_MIN_DECAY_TIME && val <= AL_REVERB_MAX_DECAY_TIME))
  327. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay time out of range");
  328. props.DecayTime = val;
  329. return;
  330. case AL_REVERB_DECAY_HFRATIO:
  331. if(!(val >= AL_REVERB_MIN_DECAY_HFRATIO && val <= AL_REVERB_MAX_DECAY_HFRATIO))
  332. context->throw_error(AL_INVALID_VALUE, "EAX Reverb decay hfratio out of range");
  333. props.DecayHFRatio = val;
  334. return;
  335. case AL_REVERB_REFLECTIONS_GAIN:
  336. if(!(val >= AL_REVERB_MIN_REFLECTIONS_GAIN && val <= AL_REVERB_MAX_REFLECTIONS_GAIN))
  337. context->throw_error(AL_INVALID_VALUE, "EAX Reverb reflections gain out of range");
  338. props.ReflectionsGain = val;
  339. return;
  340. case AL_REVERB_REFLECTIONS_DELAY:
  341. if(!(val >= AL_REVERB_MIN_REFLECTIONS_DELAY && val <= AL_REVERB_MAX_REFLECTIONS_DELAY))
  342. context->throw_error(AL_INVALID_VALUE, "EAX Reverb reflections delay out of range");
  343. props.ReflectionsDelay = val;
  344. return;
  345. case AL_REVERB_LATE_REVERB_GAIN:
  346. if(!(val >= AL_REVERB_MIN_LATE_REVERB_GAIN && val <= AL_REVERB_MAX_LATE_REVERB_GAIN))
  347. context->throw_error(AL_INVALID_VALUE, "EAX Reverb late reverb gain out of range");
  348. props.LateReverbGain = val;
  349. return;
  350. case AL_REVERB_LATE_REVERB_DELAY:
  351. if(!(val >= AL_REVERB_MIN_LATE_REVERB_DELAY && val <= AL_REVERB_MAX_LATE_REVERB_DELAY))
  352. context->throw_error(AL_INVALID_VALUE, "EAX Reverb late reverb delay out of range");
  353. props.LateReverbDelay = val;
  354. return;
  355. case AL_REVERB_AIR_ABSORPTION_GAINHF:
  356. if(!(val >= AL_REVERB_MIN_AIR_ABSORPTION_GAINHF && val <= AL_REVERB_MAX_AIR_ABSORPTION_GAINHF))
  357. context->throw_error(AL_INVALID_VALUE, "EAX Reverb air absorption gainhf out of range");
  358. props.AirAbsorptionGainHF = val;
  359. return;
  360. case AL_REVERB_ROOM_ROLLOFF_FACTOR:
  361. if(!(val >= AL_REVERB_MIN_ROOM_ROLLOFF_FACTOR && val <= AL_REVERB_MAX_ROOM_ROLLOFF_FACTOR))
  362. context->throw_error(AL_INVALID_VALUE, "EAX Reverb room rolloff factor out of range");
  363. props.RoomRolloffFactor = val;
  364. return;
  365. }
  366. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb float property {:#04x}",
  367. as_unsigned(param));
  368. }
  369. void StdReverbEffectHandler::SetParamfv(ALCcontext *context, ReverbProps &props, ALenum param, const float *vals)
  370. { SetParamf(context, props, param, *vals); }
  371. void StdReverbEffectHandler::GetParami(ALCcontext *context, const ReverbProps &props, ALenum param, int *val)
  372. {
  373. switch(param)
  374. {
  375. case AL_REVERB_DECAY_HFLIMIT: *val = props.DecayHFLimit; return;
  376. }
  377. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb integer property {:#04x}",
  378. as_unsigned(param));
  379. }
  380. void StdReverbEffectHandler::GetParamiv(ALCcontext *context, const ReverbProps &props, ALenum param, int *vals)
  381. { GetParami(context, props, param, vals); }
  382. void StdReverbEffectHandler::GetParamf(ALCcontext *context, const ReverbProps &props, ALenum param, float *val)
  383. {
  384. switch(param)
  385. {
  386. case AL_REVERB_DENSITY: *val = props.Density; return;
  387. case AL_REVERB_DIFFUSION: *val = props.Diffusion; return;
  388. case AL_REVERB_GAIN: *val = props.Gain; return;
  389. case AL_REVERB_GAINHF: *val = props.GainHF; return;
  390. case AL_REVERB_DECAY_TIME: *val = props.DecayTime; return;
  391. case AL_REVERB_DECAY_HFRATIO: *val = props.DecayHFRatio; return;
  392. case AL_REVERB_REFLECTIONS_GAIN: *val = props.ReflectionsGain; return;
  393. case AL_REVERB_REFLECTIONS_DELAY: *val = props.ReflectionsDelay; return;
  394. case AL_REVERB_LATE_REVERB_GAIN: *val = props.LateReverbGain; return;
  395. case AL_REVERB_LATE_REVERB_DELAY: *val = props.LateReverbDelay; return;
  396. case AL_REVERB_AIR_ABSORPTION_GAINHF: *val = props.AirAbsorptionGainHF; return;
  397. case AL_REVERB_ROOM_ROLLOFF_FACTOR: *val = props.RoomRolloffFactor; return;
  398. }
  399. context->throw_error(AL_INVALID_ENUM, "Invalid EAX reverb float property {:#04x}",
  400. as_unsigned(param));
  401. }
  402. void StdReverbEffectHandler::GetParamfv(ALCcontext *context, const ReverbProps &props, ALenum param, float *vals)
  403. { GetParamf(context, props, param, vals); }
  404. #if ALSOFT_EAX
  405. namespace {
  406. class EaxReverbEffectException : public EaxException
  407. {
  408. public:
  409. explicit EaxReverbEffectException(const char* message)
  410. : EaxException{"EAX_REVERB_EFFECT", message}
  411. {}
  412. }; // EaxReverbEffectException
  413. struct EnvironmentValidator1 {
  414. void operator()(unsigned long ulEnvironment) const
  415. {
  416. eax_validate_range<EaxReverbEffectException>(
  417. "Environment",
  418. ulEnvironment,
  419. EAXREVERB_MINENVIRONMENT,
  420. EAX1REVERB_MAXENVIRONMENT);
  421. }
  422. }; // EnvironmentValidator1
  423. struct VolumeValidator {
  424. void operator()(float volume) const
  425. {
  426. eax_validate_range<EaxReverbEffectException>(
  427. "Volume",
  428. volume,
  429. EAX1REVERB_MINVOLUME,
  430. EAX1REVERB_MAXVOLUME);
  431. }
  432. }; // VolumeValidator
  433. struct DecayTimeValidator {
  434. void operator()(float flDecayTime) const
  435. {
  436. eax_validate_range<EaxReverbEffectException>(
  437. "Decay Time",
  438. flDecayTime,
  439. EAXREVERB_MINDECAYTIME,
  440. EAXREVERB_MAXDECAYTIME);
  441. }
  442. }; // DecayTimeValidator
  443. struct DampingValidator {
  444. void operator()(float damping) const
  445. {
  446. eax_validate_range<EaxReverbEffectException>(
  447. "Damping",
  448. damping,
  449. EAX1REVERB_MINDAMPING,
  450. EAX1REVERB_MAXDAMPING);
  451. }
  452. }; // DampingValidator
  453. struct AllValidator1 {
  454. void operator()(const EAX_REVERBPROPERTIES& all) const
  455. {
  456. EnvironmentValidator1{}(all.environment);
  457. VolumeValidator{}(all.fVolume);
  458. DecayTimeValidator{}(all.fDecayTime_sec);
  459. DampingValidator{}(all.fDamping);
  460. }
  461. }; // AllValidator1
  462. struct RoomValidator {
  463. void operator()(long lRoom) const
  464. {
  465. eax_validate_range<EaxReverbEffectException>(
  466. "Room",
  467. lRoom,
  468. EAXREVERB_MINROOM,
  469. EAXREVERB_MAXROOM);
  470. }
  471. }; // RoomValidator
  472. struct RoomHFValidator {
  473. void operator()(long lRoomHF) const
  474. {
  475. eax_validate_range<EaxReverbEffectException>(
  476. "Room HF",
  477. lRoomHF,
  478. EAXREVERB_MINROOMHF,
  479. EAXREVERB_MAXROOMHF);
  480. }
  481. }; // RoomHFValidator
  482. struct RoomRolloffFactorValidator {
  483. void operator()(float flRoomRolloffFactor) const
  484. {
  485. eax_validate_range<EaxReverbEffectException>(
  486. "Room Rolloff Factor",
  487. flRoomRolloffFactor,
  488. EAXREVERB_MINROOMROLLOFFFACTOR,
  489. EAXREVERB_MAXROOMROLLOFFFACTOR);
  490. }
  491. }; // RoomRolloffFactorValidator
  492. struct DecayHFRatioValidator {
  493. void operator()(float flDecayHFRatio) const
  494. {
  495. eax_validate_range<EaxReverbEffectException>(
  496. "Decay HF Ratio",
  497. flDecayHFRatio,
  498. EAXREVERB_MINDECAYHFRATIO,
  499. EAXREVERB_MAXDECAYHFRATIO);
  500. }
  501. }; // DecayHFRatioValidator
  502. struct ReflectionsValidator {
  503. void operator()(long lReflections) const
  504. {
  505. eax_validate_range<EaxReverbEffectException>(
  506. "Reflections",
  507. lReflections,
  508. EAXREVERB_MINREFLECTIONS,
  509. EAXREVERB_MAXREFLECTIONS);
  510. }
  511. }; // ReflectionsValidator
  512. struct ReflectionsDelayValidator {
  513. void operator()(float flReflectionsDelay) const
  514. {
  515. eax_validate_range<EaxReverbEffectException>(
  516. "Reflections Delay",
  517. flReflectionsDelay,
  518. EAXREVERB_MINREFLECTIONSDELAY,
  519. EAXREVERB_MAXREFLECTIONSDELAY);
  520. }
  521. }; // ReflectionsDelayValidator
  522. struct ReverbValidator {
  523. void operator()(long lReverb) const
  524. {
  525. eax_validate_range<EaxReverbEffectException>(
  526. "Reverb",
  527. lReverb,
  528. EAXREVERB_MINREVERB,
  529. EAXREVERB_MAXREVERB);
  530. }
  531. }; // ReverbValidator
  532. struct ReverbDelayValidator {
  533. void operator()(float flReverbDelay) const
  534. {
  535. eax_validate_range<EaxReverbEffectException>(
  536. "Reverb Delay",
  537. flReverbDelay,
  538. EAXREVERB_MINREVERBDELAY,
  539. EAXREVERB_MAXREVERBDELAY);
  540. }
  541. }; // ReverbDelayValidator
  542. struct EnvironmentSizeValidator {
  543. void operator()(float flEnvironmentSize) const
  544. {
  545. eax_validate_range<EaxReverbEffectException>(
  546. "Environment Size",
  547. flEnvironmentSize,
  548. EAXREVERB_MINENVIRONMENTSIZE,
  549. EAXREVERB_MAXENVIRONMENTSIZE);
  550. }
  551. }; // EnvironmentSizeValidator
  552. struct EnvironmentDiffusionValidator {
  553. void operator()(float flEnvironmentDiffusion) const
  554. {
  555. eax_validate_range<EaxReverbEffectException>(
  556. "Environment Diffusion",
  557. flEnvironmentDiffusion,
  558. EAXREVERB_MINENVIRONMENTDIFFUSION,
  559. EAXREVERB_MAXENVIRONMENTDIFFUSION);
  560. }
  561. }; // EnvironmentDiffusionValidator
  562. struct AirAbsorptionHFValidator {
  563. void operator()(float flAirAbsorptionHF) const
  564. {
  565. eax_validate_range<EaxReverbEffectException>(
  566. "Air Absorbtion HF",
  567. flAirAbsorptionHF,
  568. EAXREVERB_MINAIRABSORPTIONHF,
  569. EAXREVERB_MAXAIRABSORPTIONHF);
  570. }
  571. }; // AirAbsorptionHFValidator
  572. struct FlagsValidator2 {
  573. void operator()(unsigned long ulFlags) const
  574. {
  575. eax_validate_range<EaxReverbEffectException>(
  576. "Flags",
  577. ulFlags,
  578. 0UL,
  579. ~EAX2LISTENERFLAGS_RESERVED);
  580. }
  581. }; // FlagsValidator2
  582. struct AllValidator2 {
  583. void operator()(const EAX20LISTENERPROPERTIES& all) const
  584. {
  585. RoomValidator{}(all.lRoom);
  586. RoomHFValidator{}(all.lRoomHF);
  587. RoomRolloffFactorValidator{}(all.flRoomRolloffFactor);
  588. DecayTimeValidator{}(all.flDecayTime);
  589. DecayHFRatioValidator{}(all.flDecayHFRatio);
  590. ReflectionsValidator{}(all.lReflections);
  591. ReflectionsDelayValidator{}(all.flReflectionsDelay);
  592. ReverbValidator{}(all.lReverb);
  593. ReverbDelayValidator{}(all.flReverbDelay);
  594. EnvironmentValidator1{}(all.dwEnvironment);
  595. EnvironmentSizeValidator{}(all.flEnvironmentSize);
  596. EnvironmentDiffusionValidator{}(all.flEnvironmentDiffusion);
  597. AirAbsorptionHFValidator{}(all.flAirAbsorptionHF);
  598. FlagsValidator2{}(all.dwFlags);
  599. }
  600. }; // AllValidator2
  601. struct EnvironmentValidator3 {
  602. void operator()(unsigned long ulEnvironment) const
  603. {
  604. eax_validate_range<EaxReverbEffectException>(
  605. "Environment",
  606. ulEnvironment,
  607. EAXREVERB_MINENVIRONMENT,
  608. EAX30REVERB_MAXENVIRONMENT);
  609. }
  610. }; // EnvironmentValidator1
  611. struct RoomLFValidator {
  612. void operator()(long lRoomLF) const
  613. {
  614. eax_validate_range<EaxReverbEffectException>(
  615. "Room LF",
  616. lRoomLF,
  617. EAXREVERB_MINROOMLF,
  618. EAXREVERB_MAXROOMLF);
  619. }
  620. }; // RoomLFValidator
  621. struct DecayLFRatioValidator {
  622. void operator()(float flDecayLFRatio) const
  623. {
  624. eax_validate_range<EaxReverbEffectException>(
  625. "Decay LF Ratio",
  626. flDecayLFRatio,
  627. EAXREVERB_MINDECAYLFRATIO,
  628. EAXREVERB_MAXDECAYLFRATIO);
  629. }
  630. }; // DecayLFRatioValidator
  631. struct VectorValidator {
  632. void operator()(const EAXVECTOR&) const
  633. {}
  634. }; // VectorValidator
  635. struct EchoTimeValidator {
  636. void operator()(float flEchoTime) const
  637. {
  638. eax_validate_range<EaxReverbEffectException>(
  639. "Echo Time",
  640. flEchoTime,
  641. EAXREVERB_MINECHOTIME,
  642. EAXREVERB_MAXECHOTIME);
  643. }
  644. }; // EchoTimeValidator
  645. struct EchoDepthValidator {
  646. void operator()(float flEchoDepth) const
  647. {
  648. eax_validate_range<EaxReverbEffectException>(
  649. "Echo Depth",
  650. flEchoDepth,
  651. EAXREVERB_MINECHODEPTH,
  652. EAXREVERB_MAXECHODEPTH);
  653. }
  654. }; // EchoDepthValidator
  655. struct ModulationTimeValidator {
  656. void operator()(float flModulationTime) const
  657. {
  658. eax_validate_range<EaxReverbEffectException>(
  659. "Modulation Time",
  660. flModulationTime,
  661. EAXREVERB_MINMODULATIONTIME,
  662. EAXREVERB_MAXMODULATIONTIME);
  663. }
  664. }; // ModulationTimeValidator
  665. struct ModulationDepthValidator {
  666. void operator()(float flModulationDepth) const
  667. {
  668. eax_validate_range<EaxReverbEffectException>(
  669. "Modulation Depth",
  670. flModulationDepth,
  671. EAXREVERB_MINMODULATIONDEPTH,
  672. EAXREVERB_MAXMODULATIONDEPTH);
  673. }
  674. }; // ModulationDepthValidator
  675. struct HFReferenceValidator {
  676. void operator()(float flHFReference) const
  677. {
  678. eax_validate_range<EaxReverbEffectException>(
  679. "HF Reference",
  680. flHFReference,
  681. EAXREVERB_MINHFREFERENCE,
  682. EAXREVERB_MAXHFREFERENCE);
  683. }
  684. }; // HFReferenceValidator
  685. struct LFReferenceValidator {
  686. void operator()(float flLFReference) const
  687. {
  688. eax_validate_range<EaxReverbEffectException>(
  689. "LF Reference",
  690. flLFReference,
  691. EAXREVERB_MINLFREFERENCE,
  692. EAXREVERB_MAXLFREFERENCE);
  693. }
  694. }; // LFReferenceValidator
  695. struct FlagsValidator3 {
  696. void operator()(unsigned long ulFlags) const
  697. {
  698. eax_validate_range<EaxReverbEffectException>(
  699. "Flags",
  700. ulFlags,
  701. 0UL,
  702. ~EAXREVERBFLAGS_RESERVED);
  703. }
  704. }; // FlagsValidator3
  705. struct AllValidator3 {
  706. void operator()(const EAXREVERBPROPERTIES& all) const
  707. {
  708. EnvironmentValidator3{}(all.ulEnvironment);
  709. EnvironmentSizeValidator{}(all.flEnvironmentSize);
  710. EnvironmentDiffusionValidator{}(all.flEnvironmentDiffusion);
  711. RoomValidator{}(all.lRoom);
  712. RoomHFValidator{}(all.lRoomHF);
  713. RoomLFValidator{}(all.lRoomLF);
  714. DecayTimeValidator{}(all.flDecayTime);
  715. DecayHFRatioValidator{}(all.flDecayHFRatio);
  716. DecayLFRatioValidator{}(all.flDecayLFRatio);
  717. ReflectionsValidator{}(all.lReflections);
  718. ReflectionsDelayValidator{}(all.flReflectionsDelay);
  719. VectorValidator{}(all.vReflectionsPan);
  720. ReverbValidator{}(all.lReverb);
  721. ReverbDelayValidator{}(all.flReverbDelay);
  722. VectorValidator{}(all.vReverbPan);
  723. EchoTimeValidator{}(all.flEchoTime);
  724. EchoDepthValidator{}(all.flEchoDepth);
  725. ModulationTimeValidator{}(all.flModulationTime);
  726. ModulationDepthValidator{}(all.flModulationDepth);
  727. AirAbsorptionHFValidator{}(all.flAirAbsorptionHF);
  728. HFReferenceValidator{}(all.flHFReference);
  729. LFReferenceValidator{}(all.flLFReference);
  730. RoomRolloffFactorValidator{}(all.flRoomRolloffFactor);
  731. FlagsValidator3{}(all.ulFlags);
  732. }
  733. }; // AllValidator3
  734. struct EnvironmentDeferrer2 {
  735. void operator()(EAX20LISTENERPROPERTIES& props, unsigned long dwEnvironment) const
  736. {
  737. props = EAX2REVERB_PRESETS[dwEnvironment];
  738. }
  739. }; // EnvironmentDeferrer2
  740. struct EnvironmentSizeDeferrer2 {
  741. void operator()(EAX20LISTENERPROPERTIES& props, float flEnvironmentSize) const
  742. {
  743. if (props.flEnvironmentSize == flEnvironmentSize)
  744. {
  745. return;
  746. }
  747. const auto scale = flEnvironmentSize / props.flEnvironmentSize;
  748. props.flEnvironmentSize = flEnvironmentSize;
  749. if ((props.dwFlags & EAX2LISTENERFLAGS_DECAYTIMESCALE) != 0)
  750. {
  751. props.flDecayTime = std::clamp(
  752. props.flDecayTime * scale,
  753. EAXREVERB_MINDECAYTIME,
  754. EAXREVERB_MAXDECAYTIME);
  755. }
  756. if ((props.dwFlags & EAX2LISTENERFLAGS_REFLECTIONSSCALE) != 0 &&
  757. (props.dwFlags & EAX2LISTENERFLAGS_REFLECTIONSDELAYSCALE) != 0)
  758. {
  759. props.lReflections = std::clamp(
  760. props.lReflections - static_cast<long>(gain_to_level_mb(scale)),
  761. EAXREVERB_MINREFLECTIONS,
  762. EAXREVERB_MAXREFLECTIONS);
  763. }
  764. if ((props.dwFlags & EAX2LISTENERFLAGS_REFLECTIONSDELAYSCALE) != 0)
  765. {
  766. props.flReflectionsDelay = std::clamp(
  767. props.flReflectionsDelay * scale,
  768. EAXREVERB_MINREFLECTIONSDELAY,
  769. EAXREVERB_MAXREFLECTIONSDELAY);
  770. }
  771. if ((props.dwFlags & EAX2LISTENERFLAGS_REVERBSCALE) != 0)
  772. {
  773. const auto log_scalar = ((props.dwFlags & EAXREVERBFLAGS_DECAYTIMESCALE) != 0) ? 2'000.0F : 3'000.0F;
  774. props.lReverb = std::clamp(
  775. props.lReverb - static_cast<long>(std::log10(scale) * log_scalar),
  776. EAXREVERB_MINREVERB,
  777. EAXREVERB_MAXREVERB);
  778. }
  779. if ((props.dwFlags & EAX2LISTENERFLAGS_REVERBDELAYSCALE) != 0)
  780. {
  781. props.flReverbDelay = std::clamp(
  782. props.flReverbDelay * scale,
  783. EAXREVERB_MINREVERBDELAY,
  784. EAXREVERB_MAXREVERBDELAY);
  785. }
  786. }
  787. }; // EnvironmentSizeDeferrer2
  788. struct EnvironmentDeferrer3 {
  789. void operator()(EAXREVERBPROPERTIES& props, unsigned long ulEnvironment) const
  790. {
  791. if (ulEnvironment == EAX_ENVIRONMENT_UNDEFINED)
  792. {
  793. props.ulEnvironment = EAX_ENVIRONMENT_UNDEFINED;
  794. return;
  795. }
  796. props = EAXREVERB_PRESETS[ulEnvironment];
  797. }
  798. }; // EnvironmentDeferrer3
  799. struct EnvironmentSizeDeferrer3 {
  800. void operator()(EAXREVERBPROPERTIES& props, float flEnvironmentSize) const
  801. {
  802. if (props.flEnvironmentSize == flEnvironmentSize)
  803. {
  804. return;
  805. }
  806. const auto scale = flEnvironmentSize / props.flEnvironmentSize;
  807. props.ulEnvironment = EAX_ENVIRONMENT_UNDEFINED;
  808. props.flEnvironmentSize = flEnvironmentSize;
  809. if ((props.ulFlags & EAXREVERBFLAGS_DECAYTIMESCALE) != 0)
  810. {
  811. props.flDecayTime = std::clamp(
  812. props.flDecayTime * scale,
  813. EAXREVERB_MINDECAYTIME,
  814. EAXREVERB_MAXDECAYTIME);
  815. }
  816. if ((props.ulFlags & EAXREVERBFLAGS_REFLECTIONSSCALE) != 0 &&
  817. (props.ulFlags & EAXREVERBFLAGS_REFLECTIONSDELAYSCALE) != 0)
  818. {
  819. props.lReflections = std::clamp(
  820. props.lReflections - static_cast<long>(gain_to_level_mb(scale)),
  821. EAXREVERB_MINREFLECTIONS,
  822. EAXREVERB_MAXREFLECTIONS);
  823. }
  824. if ((props.ulFlags & EAXREVERBFLAGS_REFLECTIONSDELAYSCALE) != 0)
  825. {
  826. props.flReflectionsDelay = std::clamp(
  827. props.flReflectionsDelay * scale,
  828. EAXREVERB_MINREFLECTIONSDELAY,
  829. EAXREVERB_MAXREFLECTIONSDELAY);
  830. }
  831. if ((props.ulFlags & EAXREVERBFLAGS_REVERBSCALE) != 0)
  832. {
  833. const auto log_scalar = ((props.ulFlags & EAXREVERBFLAGS_DECAYTIMESCALE) != 0) ? 2'000.0F : 3'000.0F;
  834. props.lReverb = std::clamp(
  835. props.lReverb - static_cast<long>(std::log10(scale) * log_scalar),
  836. EAXREVERB_MINREVERB,
  837. EAXREVERB_MAXREVERB);
  838. }
  839. if ((props.ulFlags & EAXREVERBFLAGS_REVERBDELAYSCALE) != 0)
  840. {
  841. props.flReverbDelay = std::clamp(
  842. props.flReverbDelay * scale,
  843. EAXREVERB_MINREVERBDELAY,
  844. EAXREVERB_MAXREVERBDELAY);
  845. }
  846. if ((props.ulFlags & EAXREVERBFLAGS_ECHOTIMESCALE) != 0)
  847. {
  848. props.flEchoTime = std::clamp(
  849. props.flEchoTime * scale,
  850. EAXREVERB_MINECHOTIME,
  851. EAXREVERB_MAXECHOTIME);
  852. }
  853. if ((props.ulFlags & EAXREVERBFLAGS_MODULATIONTIMESCALE) != 0)
  854. {
  855. props.flModulationTime = std::clamp(
  856. props.flModulationTime * scale,
  857. EAXREVERB_MINMODULATIONTIME,
  858. EAXREVERB_MAXMODULATIONTIME);
  859. }
  860. }
  861. }; // EnvironmentSizeDeferrer3
  862. } // namespace
  863. struct EaxReverbCommitter::Exception : public EaxReverbEffectException
  864. {
  865. using EaxReverbEffectException::EaxReverbEffectException;
  866. };
  867. [[noreturn]] void EaxReverbCommitter::fail(const char* message)
  868. {
  869. throw Exception{message};
  870. }
  871. void EaxReverbCommitter::translate(const EAX_REVERBPROPERTIES& src, EAXREVERBPROPERTIES& dst) noexcept
  872. {
  873. assert(src.environment <= EAX1REVERB_MAXENVIRONMENT);
  874. dst = EAXREVERB_PRESETS[src.environment];
  875. dst.flDecayTime = src.fDecayTime_sec;
  876. dst.flDecayHFRatio = src.fDamping;
  877. dst.lReverb = static_cast<int>(std::min(gain_to_level_mb(src.fVolume), 0.0f));
  878. }
  879. void EaxReverbCommitter::translate(const EAX20LISTENERPROPERTIES& src, EAXREVERBPROPERTIES& dst) noexcept
  880. {
  881. assert(src.dwEnvironment <= EAX1REVERB_MAXENVIRONMENT);
  882. dst = EAXREVERB_PRESETS[src.dwEnvironment];
  883. dst.ulEnvironment = src.dwEnvironment;
  884. dst.flEnvironmentSize = src.flEnvironmentSize;
  885. dst.flEnvironmentDiffusion = src.flEnvironmentDiffusion;
  886. dst.lRoom = src.lRoom;
  887. dst.lRoomHF = src.lRoomHF;
  888. dst.flDecayTime = src.flDecayTime;
  889. dst.flDecayHFRatio = src.flDecayHFRatio;
  890. dst.lReflections = src.lReflections;
  891. dst.flReflectionsDelay = src.flReflectionsDelay;
  892. dst.lReverb = src.lReverb;
  893. dst.flReverbDelay = src.flReverbDelay;
  894. dst.flAirAbsorptionHF = src.flAirAbsorptionHF;
  895. dst.flRoomRolloffFactor = src.flRoomRolloffFactor;
  896. dst.ulFlags = src.dwFlags;
  897. }
  898. bool EaxReverbCommitter::commit(const EAX_REVERBPROPERTIES &props)
  899. {
  900. EAXREVERBPROPERTIES dst{};
  901. translate(props, dst);
  902. return commit(dst);
  903. }
  904. bool EaxReverbCommitter::commit(const EAX20LISTENERPROPERTIES &props)
  905. {
  906. EAXREVERBPROPERTIES dst{};
  907. translate(props, dst);
  908. return commit(dst);
  909. }
  910. bool EaxReverbCommitter::commit(const EAXREVERBPROPERTIES &props)
  911. {
  912. if(auto *cur = std::get_if<EAXREVERBPROPERTIES>(&mEaxProps); cur && *cur == props)
  913. return false;
  914. mEaxProps = props;
  915. const auto size = props.flEnvironmentSize;
  916. const auto density = (size * size * size) / 16.0f;
  917. mAlProps = [&]{
  918. ReverbProps ret{};
  919. ret.Density = std::min(density, AL_EAXREVERB_MAX_DENSITY);
  920. ret.Diffusion = props.flEnvironmentDiffusion;
  921. ret.Gain = level_mb_to_gain(static_cast<float>(props.lRoom));
  922. ret.GainHF = level_mb_to_gain(static_cast<float>(props.lRoomHF));
  923. ret.GainLF = level_mb_to_gain(static_cast<float>(props.lRoomLF));
  924. ret.DecayTime = props.flDecayTime;
  925. ret.DecayHFRatio = props.flDecayHFRatio;
  926. ret.DecayLFRatio = props.flDecayLFRatio;
  927. ret.ReflectionsGain = level_mb_to_gain(static_cast<float>(props.lReflections));
  928. ret.ReflectionsDelay = props.flReflectionsDelay;
  929. ret.ReflectionsPan = {props.vReflectionsPan.x, props.vReflectionsPan.y,
  930. props.vReflectionsPan.z};
  931. ret.LateReverbGain = level_mb_to_gain(static_cast<float>(props.lReverb));
  932. ret.LateReverbDelay = props.flReverbDelay;
  933. ret.LateReverbPan = {props.vReverbPan.x, props.vReverbPan.y, props.vReverbPan.z};
  934. ret.EchoTime = props.flEchoTime;
  935. ret.EchoDepth = props.flEchoDepth;
  936. ret.ModulationTime = props.flModulationTime;
  937. ret.ModulationDepth = props.flModulationDepth;
  938. ret.AirAbsorptionGainHF = level_mb_to_gain(props.flAirAbsorptionHF);
  939. ret.HFReference = props.flHFReference;
  940. ret.LFReference = props.flLFReference;
  941. ret.RoomRolloffFactor = props.flRoomRolloffFactor;
  942. ret.DecayHFLimit = ((props.ulFlags & EAXREVERBFLAGS_DECAYHFLIMIT) != 0);
  943. if(EaxTraceCommits) UNLIKELY
  944. {
  945. TRACE("Reverb commit:\n"
  946. " Density: {:f}\n"
  947. " Diffusion: {:f}\n"
  948. " Gain: {:f}\n"
  949. " GainHF: {:f}\n"
  950. " GainLF: {:f}\n"
  951. " DecayTime: {:f}\n"
  952. " DecayHFRatio: {:f}\n"
  953. " DecayLFRatio: {:f}\n"
  954. " ReflectionsGain: {:f}\n"
  955. " ReflectionsDelay: {:f}\n"
  956. " ReflectionsPan: {}\n"
  957. " LateReverbGain: {:f}\n"
  958. " LateReverbDelay: {:f}\n"
  959. " LateRevernPan: {}\n"
  960. " EchoTime: {:f}\n"
  961. " EchoDepth: {:f}\n"
  962. " ModulationTime: {:f}\n"
  963. " ModulationDepth: {:f}\n"
  964. " AirAbsorptionGainHF: {:f}\n"
  965. " HFReference: {:f}\n"
  966. " LFReference: {:f}\n"
  967. " RoomRolloffFactor: {:f}\n"
  968. " DecayHFLimit: {}", ret.Density, ret.Diffusion, ret.Gain, ret.GainHF, ret.GainLF,
  969. ret.DecayTime, ret.DecayHFRatio, ret.DecayLFRatio, ret.ReflectionsGain,
  970. ret.ReflectionsDelay, ret.ReflectionsPan, ret.LateReverbGain, ret.LateReverbDelay,
  971. ret.LateReverbPan, ret.EchoTime, ret.EchoDepth, ret.ModulationTime,
  972. ret.ModulationDepth, ret.AirAbsorptionGainHF, ret.HFReference, ret.LFReference,
  973. ret.RoomRolloffFactor, ret.DecayHFLimit ? "true" : "false");
  974. }
  975. return ret;
  976. }();
  977. return true;
  978. }
  979. void EaxReverbCommitter::SetDefaults(EAX_REVERBPROPERTIES &props)
  980. {
  981. props = EAX1REVERB_PRESETS[EAX_ENVIRONMENT_GENERIC];
  982. }
  983. void EaxReverbCommitter::SetDefaults(EAX20LISTENERPROPERTIES &props)
  984. {
  985. props = EAX2REVERB_PRESETS[EAX2_ENVIRONMENT_GENERIC];
  986. props.lRoom = -10'000L;
  987. }
  988. void EaxReverbCommitter::SetDefaults(EAXREVERBPROPERTIES &props)
  989. {
  990. props = EAXREVERB_PRESETS[EAX_ENVIRONMENT_GENERIC];
  991. }
  992. void EaxReverbCommitter::SetDefaults(EaxEffectProps &props)
  993. {
  994. SetDefaults(props.emplace<EAXREVERBPROPERTIES>());
  995. }
  996. void EaxReverbCommitter::Get(const EaxCall &call, const EAX_REVERBPROPERTIES &props)
  997. {
  998. switch(call.get_property_id())
  999. {
  1000. case DSPROPERTY_EAX_ALL: call.set_value<Exception>(props); break;
  1001. case DSPROPERTY_EAX_ENVIRONMENT: call.set_value<Exception>(props.environment); break;
  1002. case DSPROPERTY_EAX_VOLUME: call.set_value<Exception>(props.fVolume); break;
  1003. case DSPROPERTY_EAX_DECAYTIME: call.set_value<Exception>(props.fDecayTime_sec); break;
  1004. case DSPROPERTY_EAX_DAMPING: call.set_value<Exception>(props.fDamping); break;
  1005. default: fail_unknown_property_id();
  1006. }
  1007. }
  1008. void EaxReverbCommitter::Get(const EaxCall &call, const EAX20LISTENERPROPERTIES &props)
  1009. {
  1010. switch(call.get_property_id())
  1011. {
  1012. case DSPROPERTY_EAX20LISTENER_NONE: break;
  1013. case DSPROPERTY_EAX20LISTENER_ALLPARAMETERS: call.set_value<Exception>(props); break;
  1014. case DSPROPERTY_EAX20LISTENER_ROOM: call.set_value<Exception>(props.lRoom); break;
  1015. case DSPROPERTY_EAX20LISTENER_ROOMHF: call.set_value<Exception>(props.lRoomHF); break;
  1016. case DSPROPERTY_EAX20LISTENER_ROOMROLLOFFFACTOR: call.set_value<Exception>(props.flRoomRolloffFactor); break;
  1017. case DSPROPERTY_EAX20LISTENER_DECAYTIME: call.set_value<Exception>(props.flDecayTime); break;
  1018. case DSPROPERTY_EAX20LISTENER_DECAYHFRATIO: call.set_value<Exception>(props.flDecayHFRatio); break;
  1019. case DSPROPERTY_EAX20LISTENER_REFLECTIONS: call.set_value<Exception>(props.lReflections); break;
  1020. case DSPROPERTY_EAX20LISTENER_REFLECTIONSDELAY: call.set_value<Exception>(props.flReflectionsDelay); break;
  1021. case DSPROPERTY_EAX20LISTENER_REVERB: call.set_value<Exception>(props.lReverb); break;
  1022. case DSPROPERTY_EAX20LISTENER_REVERBDELAY: call.set_value<Exception>(props.flReverbDelay); break;
  1023. case DSPROPERTY_EAX20LISTENER_ENVIRONMENT: call.set_value<Exception>(props.dwEnvironment); break;
  1024. case DSPROPERTY_EAX20LISTENER_ENVIRONMENTSIZE: call.set_value<Exception>(props.flEnvironmentSize); break;
  1025. case DSPROPERTY_EAX20LISTENER_ENVIRONMENTDIFFUSION: call.set_value<Exception>(props.flEnvironmentDiffusion); break;
  1026. case DSPROPERTY_EAX20LISTENER_AIRABSORPTIONHF: call.set_value<Exception>(props.flAirAbsorptionHF); break;
  1027. case DSPROPERTY_EAX20LISTENER_FLAGS: call.set_value<Exception>(props.dwFlags); break;
  1028. default: fail_unknown_property_id();
  1029. }
  1030. }
  1031. void EaxReverbCommitter::Get(const EaxCall &call, const EAXREVERBPROPERTIES &props)
  1032. {
  1033. switch(call.get_property_id())
  1034. {
  1035. case EAXREVERB_NONE: break;
  1036. case EAXREVERB_ALLPARAMETERS: call.set_value<Exception>(props); break;
  1037. case EAXREVERB_ENVIRONMENT: call.set_value<Exception>(props.ulEnvironment); break;
  1038. case EAXREVERB_ENVIRONMENTSIZE: call.set_value<Exception>(props.flEnvironmentSize); break;
  1039. case EAXREVERB_ENVIRONMENTDIFFUSION: call.set_value<Exception>(props.flEnvironmentDiffusion); break;
  1040. case EAXREVERB_ROOM: call.set_value<Exception>(props.lRoom); break;
  1041. case EAXREVERB_ROOMHF: call.set_value<Exception>(props.lRoomHF); break;
  1042. case EAXREVERB_ROOMLF: call.set_value<Exception>(props.lRoomLF); break;
  1043. case EAXREVERB_DECAYTIME: call.set_value<Exception>(props.flDecayTime); break;
  1044. case EAXREVERB_DECAYHFRATIO: call.set_value<Exception>(props.flDecayHFRatio); break;
  1045. case EAXREVERB_DECAYLFRATIO: call.set_value<Exception>(props.flDecayLFRatio); break;
  1046. case EAXREVERB_REFLECTIONS: call.set_value<Exception>(props.lReflections); break;
  1047. case EAXREVERB_REFLECTIONSDELAY: call.set_value<Exception>(props.flReflectionsDelay); break;
  1048. case EAXREVERB_REFLECTIONSPAN: call.set_value<Exception>(props.vReflectionsPan); break;
  1049. case EAXREVERB_REVERB: call.set_value<Exception>(props.lReverb); break;
  1050. case EAXREVERB_REVERBDELAY: call.set_value<Exception>(props.flReverbDelay); break;
  1051. case EAXREVERB_REVERBPAN: call.set_value<Exception>(props.vReverbPan); break;
  1052. case EAXREVERB_ECHOTIME: call.set_value<Exception>(props.flEchoTime); break;
  1053. case EAXREVERB_ECHODEPTH: call.set_value<Exception>(props.flEchoDepth); break;
  1054. case EAXREVERB_MODULATIONTIME: call.set_value<Exception>(props.flModulationTime); break;
  1055. case EAXREVERB_MODULATIONDEPTH: call.set_value<Exception>(props.flModulationDepth); break;
  1056. case EAXREVERB_AIRABSORPTIONHF: call.set_value<Exception>(props.flAirAbsorptionHF); break;
  1057. case EAXREVERB_HFREFERENCE: call.set_value<Exception>(props.flHFReference); break;
  1058. case EAXREVERB_LFREFERENCE: call.set_value<Exception>(props.flLFReference); break;
  1059. case EAXREVERB_ROOMROLLOFFFACTOR: call.set_value<Exception>(props.flRoomRolloffFactor); break;
  1060. case EAXREVERB_FLAGS: call.set_value<Exception>(props.ulFlags); break;
  1061. default: fail_unknown_property_id();
  1062. }
  1063. }
  1064. void EaxReverbCommitter::Set(const EaxCall &call, EAX_REVERBPROPERTIES &props)
  1065. {
  1066. switch(call.get_property_id())
  1067. {
  1068. case DSPROPERTY_EAX_ALL: defer<AllValidator1>(call, props); break;
  1069. case DSPROPERTY_EAX_ENVIRONMENT: defer<EnvironmentValidator1>(call, props.environment); break;
  1070. case DSPROPERTY_EAX_VOLUME: defer<VolumeValidator>(call, props.fVolume); break;
  1071. case DSPROPERTY_EAX_DECAYTIME: defer<DecayTimeValidator>(call, props.fDecayTime_sec); break;
  1072. case DSPROPERTY_EAX_DAMPING: defer<DampingValidator>(call, props.fDamping); break;
  1073. default: fail_unknown_property_id();
  1074. }
  1075. }
  1076. void EaxReverbCommitter::Set(const EaxCall &call, EAX20LISTENERPROPERTIES &props)
  1077. {
  1078. switch(call.get_property_id())
  1079. {
  1080. case DSPROPERTY_EAX20LISTENER_NONE: break;
  1081. case DSPROPERTY_EAX20LISTENER_ALLPARAMETERS: defer<AllValidator2>(call, props); break;
  1082. case DSPROPERTY_EAX20LISTENER_ROOM: defer<RoomValidator>(call, props.lRoom); break;
  1083. case DSPROPERTY_EAX20LISTENER_ROOMHF: defer<RoomHFValidator>(call, props.lRoomHF); break;
  1084. case DSPROPERTY_EAX20LISTENER_ROOMROLLOFFFACTOR: defer<RoomRolloffFactorValidator>(call, props.flRoomRolloffFactor); break;
  1085. case DSPROPERTY_EAX20LISTENER_DECAYTIME: defer<DecayTimeValidator>(call, props.flDecayTime); break;
  1086. case DSPROPERTY_EAX20LISTENER_DECAYHFRATIO: defer<DecayHFRatioValidator>(call, props.flDecayHFRatio); break;
  1087. case DSPROPERTY_EAX20LISTENER_REFLECTIONS: defer<ReflectionsValidator>(call, props.lReflections); break;
  1088. case DSPROPERTY_EAX20LISTENER_REFLECTIONSDELAY: defer<ReflectionsDelayValidator>(call, props.flReverbDelay); break;
  1089. case DSPROPERTY_EAX20LISTENER_REVERB: defer<ReverbValidator>(call, props.lReverb); break;
  1090. case DSPROPERTY_EAX20LISTENER_REVERBDELAY: defer<ReverbDelayValidator>(call, props.flReverbDelay); break;
  1091. case DSPROPERTY_EAX20LISTENER_ENVIRONMENT: defer<EnvironmentValidator1, EnvironmentDeferrer2>(call, props, props.dwEnvironment); break;
  1092. case DSPROPERTY_EAX20LISTENER_ENVIRONMENTSIZE: defer<EnvironmentSizeValidator, EnvironmentSizeDeferrer2>(call, props, props.flEnvironmentSize); break;
  1093. case DSPROPERTY_EAX20LISTENER_ENVIRONMENTDIFFUSION: defer<EnvironmentDiffusionValidator>(call, props.flEnvironmentDiffusion); break;
  1094. case DSPROPERTY_EAX20LISTENER_AIRABSORPTIONHF: defer<AirAbsorptionHFValidator>(call, props.flAirAbsorptionHF); break;
  1095. case DSPROPERTY_EAX20LISTENER_FLAGS: defer<FlagsValidator2>(call, props.dwFlags); break;
  1096. default: fail_unknown_property_id();
  1097. }
  1098. }
  1099. void EaxReverbCommitter::Set(const EaxCall &call, EAXREVERBPROPERTIES &props)
  1100. {
  1101. switch(call.get_property_id())
  1102. {
  1103. case EAXREVERB_NONE: break;
  1104. case EAXREVERB_ALLPARAMETERS: defer<AllValidator3>(call, props); break;
  1105. case EAXREVERB_ENVIRONMENT: defer<EnvironmentValidator3, EnvironmentDeferrer3>(call, props, props.ulEnvironment); break;
  1106. case EAXREVERB_ENVIRONMENTSIZE: defer<EnvironmentSizeValidator, EnvironmentSizeDeferrer3>(call, props, props.flEnvironmentSize); break;
  1107. case EAXREVERB_ENVIRONMENTDIFFUSION: defer3<EnvironmentDiffusionValidator>(call, props, props.flEnvironmentDiffusion); break;
  1108. case EAXREVERB_ROOM: defer3<RoomValidator>(call, props, props.lRoom); break;
  1109. case EAXREVERB_ROOMHF: defer3<RoomHFValidator>(call, props, props.lRoomHF); break;
  1110. case EAXREVERB_ROOMLF: defer3<RoomLFValidator>(call, props, props.lRoomLF); break;
  1111. case EAXREVERB_DECAYTIME: defer3<DecayTimeValidator>(call, props, props.flDecayTime); break;
  1112. case EAXREVERB_DECAYHFRATIO: defer3<DecayHFRatioValidator>(call, props, props.flDecayHFRatio); break;
  1113. case EAXREVERB_DECAYLFRATIO: defer3<DecayLFRatioValidator>(call, props, props.flDecayLFRatio); break;
  1114. case EAXREVERB_REFLECTIONS: defer3<ReflectionsValidator>(call, props, props.lReflections); break;
  1115. case EAXREVERB_REFLECTIONSDELAY: defer3<ReflectionsDelayValidator>(call, props, props.flReflectionsDelay); break;
  1116. case EAXREVERB_REFLECTIONSPAN: defer3<VectorValidator>(call, props, props.vReflectionsPan); break;
  1117. case EAXREVERB_REVERB: defer3<ReverbValidator>(call, props, props.lReverb); break;
  1118. case EAXREVERB_REVERBDELAY: defer3<ReverbDelayValidator>(call, props, props.flReverbDelay); break;
  1119. case EAXREVERB_REVERBPAN: defer3<VectorValidator>(call, props, props.vReverbPan); break;
  1120. case EAXREVERB_ECHOTIME: defer3<EchoTimeValidator>(call, props, props.flEchoTime); break;
  1121. case EAXREVERB_ECHODEPTH: defer3<EchoDepthValidator>(call, props, props.flEchoDepth); break;
  1122. case EAXREVERB_MODULATIONTIME: defer3<ModulationTimeValidator>(call, props, props.flModulationTime); break;
  1123. case EAXREVERB_MODULATIONDEPTH: defer3<ModulationDepthValidator>(call, props, props.flModulationDepth); break;
  1124. case EAXREVERB_AIRABSORPTIONHF: defer3<AirAbsorptionHFValidator>(call, props, props.flAirAbsorptionHF); break;
  1125. case EAXREVERB_HFREFERENCE: defer3<HFReferenceValidator>(call, props, props.flHFReference); break;
  1126. case EAXREVERB_LFREFERENCE: defer3<LFReferenceValidator>(call, props, props.flLFReference); break;
  1127. case EAXREVERB_ROOMROLLOFFFACTOR: defer3<RoomRolloffFactorValidator>(call, props, props.flRoomRolloffFactor); break;
  1128. case EAXREVERB_FLAGS: defer3<FlagsValidator3>(call, props, props.ulFlags); break;
  1129. default: fail_unknown_property_id();
  1130. }
  1131. }
  1132. #endif // ALSOFT_EAX