🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
9.5 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2009 by Chris Robinson.
  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 <cmath>
  22. #include <cstdlib>
  23. #include <cmath>
  24. #include <algorithm>
  25. #include "alMain.h"
  26. #include "alcontext.h"
  27. #include "alAuxEffectSlot.h"
  28. #include "alError.h"
  29. #include "alu.h"
  30. #include "filters/biquad.h"
  31. #include "vecmat.h"
  32. namespace {
  33. #define MAX_UPDATE_SAMPLES 128
  34. #define WAVEFORM_FRACBITS 24
  35. #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
  36. #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
  37. inline ALfloat Sin(ALsizei index)
  38. {
  39. return std::sin(static_cast<ALfloat>(index) *
  40. (al::MathDefs<float>::Tau() / ALfloat{WAVEFORM_FRACONE}));
  41. }
  42. inline ALfloat Saw(ALsizei index)
  43. {
  44. return static_cast<ALfloat>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f;
  45. }
  46. inline ALfloat Square(ALsizei index)
  47. {
  48. return static_cast<ALfloat>(((index>>(WAVEFORM_FRACBITS-2))&2) - 1);
  49. }
  50. inline ALfloat One(ALsizei UNUSED(index))
  51. {
  52. return 1.0f;
  53. }
  54. template<ALfloat func(ALsizei)>
  55. void Modulate(ALfloat *RESTRICT dst, ALsizei index, const ALsizei step, ALsizei todo)
  56. {
  57. ALsizei i;
  58. for(i = 0;i < todo;i++)
  59. {
  60. index += step;
  61. index &= WAVEFORM_FRACMASK;
  62. dst[i] = func(index);
  63. }
  64. }
  65. struct ModulatorState final : public EffectState {
  66. void (*mGetSamples)(ALfloat*RESTRICT, ALsizei, const ALsizei, ALsizei){};
  67. ALsizei mIndex{0};
  68. ALsizei mStep{1};
  69. struct {
  70. BiquadFilter Filter;
  71. ALfloat CurrentGains[MAX_OUTPUT_CHANNELS]{};
  72. ALfloat TargetGains[MAX_OUTPUT_CHANNELS]{};
  73. } mChans[MAX_AMBI_CHANNELS];
  74. ALboolean deviceUpdate(const ALCdevice *device) override;
  75. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  76. void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput) override;
  77. DEF_NEWDEL(ModulatorState)
  78. };
  79. ALboolean ModulatorState::deviceUpdate(const ALCdevice *UNUSED(device))
  80. {
  81. for(auto &e : mChans)
  82. {
  83. e.Filter.clear();
  84. std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
  85. }
  86. return AL_TRUE;
  87. }
  88. void ModulatorState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  89. {
  90. const ALCdevice *device{context->Device};
  91. const float step{props->Modulator.Frequency / static_cast<ALfloat>(device->Frequency)};
  92. mStep = fastf2i(clampf(step*WAVEFORM_FRACONE, 0.0f, ALfloat{WAVEFORM_FRACONE-1}));
  93. if(mStep == 0)
  94. mGetSamples = Modulate<One>;
  95. else if(props->Modulator.Waveform == AL_RING_MODULATOR_SINUSOID)
  96. mGetSamples = Modulate<Sin>;
  97. else if(props->Modulator.Waveform == AL_RING_MODULATOR_SAWTOOTH)
  98. mGetSamples = Modulate<Saw>;
  99. else /*if(Slot->Params.EffectProps.Modulator.Waveform == AL_RING_MODULATOR_SQUARE)*/
  100. mGetSamples = Modulate<Square>;
  101. ALfloat f0norm{props->Modulator.HighPassCutoff / static_cast<ALfloat>(device->Frequency)};
  102. f0norm = clampf(f0norm, 1.0f/512.0f, 0.49f);
  103. /* Bandwidth value is constant in octaves. */
  104. mChans[0].Filter.setParams(BiquadType::HighPass, 1.0f, f0norm,
  105. calc_rcpQ_from_bandwidth(f0norm, 0.75f));
  106. for(ALsizei i{1};i < slot->Wet.NumChannels;++i)
  107. mChans[i].Filter.copyParamsFrom(mChans[0].Filter);
  108. mOutBuffer = target.Main->Buffer;
  109. mOutChannels = target.Main->NumChannels;
  110. for(ALsizei i{0};i < slot->Wet.NumChannels;++i)
  111. {
  112. auto coeffs = GetAmbiIdentityRow(i);
  113. ComputePanGains(target.Main, coeffs.data(), slot->Params.Gain, mChans[i].TargetGains);
  114. }
  115. }
  116. void ModulatorState::process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput)
  117. {
  118. const ALsizei step = mStep;
  119. ALsizei base;
  120. for(base = 0;base < samplesToDo;)
  121. {
  122. alignas(16) ALfloat modsamples[MAX_UPDATE_SAMPLES];
  123. ALsizei td = mini(MAX_UPDATE_SAMPLES, samplesToDo-base);
  124. ALsizei c, i;
  125. mGetSamples(modsamples, mIndex, step, td);
  126. mIndex += (step*td) & WAVEFORM_FRACMASK;
  127. mIndex &= WAVEFORM_FRACMASK;
  128. ASSUME(numInput > 0);
  129. for(c = 0;c < numInput;c++)
  130. {
  131. alignas(16) ALfloat temps[MAX_UPDATE_SAMPLES];
  132. mChans[c].Filter.process(temps, &samplesIn[c][base], td);
  133. for(i = 0;i < td;i++)
  134. temps[i] *= modsamples[i];
  135. MixSamples(temps, numOutput, samplesOut, mChans[c].CurrentGains,
  136. mChans[c].TargetGains, samplesToDo-base, base, td);
  137. }
  138. base += td;
  139. }
  140. }
  141. void Modulator_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  142. {
  143. switch(param)
  144. {
  145. case AL_RING_MODULATOR_FREQUENCY:
  146. if(!(val >= AL_RING_MODULATOR_MIN_FREQUENCY && val <= AL_RING_MODULATOR_MAX_FREQUENCY))
  147. SETERR_RETURN(context, AL_INVALID_VALUE,, "Modulator frequency out of range");
  148. props->Modulator.Frequency = val;
  149. break;
  150. case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
  151. if(!(val >= AL_RING_MODULATOR_MIN_HIGHPASS_CUTOFF && val <= AL_RING_MODULATOR_MAX_HIGHPASS_CUTOFF))
  152. SETERR_RETURN(context, AL_INVALID_VALUE,, "Modulator high-pass cutoff out of range");
  153. props->Modulator.HighPassCutoff = val;
  154. break;
  155. default:
  156. alSetError(context, AL_INVALID_ENUM, "Invalid modulator float property 0x%04x", param);
  157. }
  158. }
  159. void Modulator_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  160. { Modulator_setParamf(props, context, param, vals[0]); }
  161. void Modulator_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
  162. {
  163. switch(param)
  164. {
  165. case AL_RING_MODULATOR_FREQUENCY:
  166. case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
  167. Modulator_setParamf(props, context, param, static_cast<ALfloat>(val));
  168. break;
  169. case AL_RING_MODULATOR_WAVEFORM:
  170. if(!(val >= AL_RING_MODULATOR_MIN_WAVEFORM && val <= AL_RING_MODULATOR_MAX_WAVEFORM))
  171. SETERR_RETURN(context, AL_INVALID_VALUE,, "Invalid modulator waveform");
  172. props->Modulator.Waveform = val;
  173. break;
  174. default:
  175. alSetError(context, AL_INVALID_ENUM, "Invalid modulator integer property 0x%04x", param);
  176. }
  177. }
  178. void Modulator_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
  179. { Modulator_setParami(props, context, param, vals[0]); }
  180. void Modulator_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
  181. {
  182. switch(param)
  183. {
  184. case AL_RING_MODULATOR_FREQUENCY:
  185. *val = static_cast<ALint>(props->Modulator.Frequency);
  186. break;
  187. case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
  188. *val = static_cast<ALint>(props->Modulator.HighPassCutoff);
  189. break;
  190. case AL_RING_MODULATOR_WAVEFORM:
  191. *val = props->Modulator.Waveform;
  192. break;
  193. default:
  194. alSetError(context, AL_INVALID_ENUM, "Invalid modulator integer property 0x%04x", param);
  195. }
  196. }
  197. void Modulator_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
  198. { Modulator_getParami(props, context, param, vals); }
  199. void Modulator_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  200. {
  201. switch(param)
  202. {
  203. case AL_RING_MODULATOR_FREQUENCY:
  204. *val = props->Modulator.Frequency;
  205. break;
  206. case AL_RING_MODULATOR_HIGHPASS_CUTOFF:
  207. *val = props->Modulator.HighPassCutoff;
  208. break;
  209. default:
  210. alSetError(context, AL_INVALID_ENUM, "Invalid modulator float property 0x%04x", param);
  211. }
  212. }
  213. void Modulator_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  214. { Modulator_getParamf(props, context, param, vals); }
  215. DEFINE_ALEFFECT_VTABLE(Modulator);
  216. struct ModulatorStateFactory final : public EffectStateFactory {
  217. EffectState *create() override { return new ModulatorState{}; }
  218. EffectProps getDefaultProps() const noexcept override;
  219. const EffectVtable *getEffectVtable() const noexcept override { return &Modulator_vtable; }
  220. };
  221. EffectProps ModulatorStateFactory::getDefaultProps() const noexcept
  222. {
  223. EffectProps props{};
  224. props.Modulator.Frequency = AL_RING_MODULATOR_DEFAULT_FREQUENCY;
  225. props.Modulator.HighPassCutoff = AL_RING_MODULATOR_DEFAULT_HIGHPASS_CUTOFF;
  226. props.Modulator.Waveform = AL_RING_MODULATOR_DEFAULT_WAVEFORM;
  227. return props;
  228. }
  229. } // namespace
  230. EffectStateFactory *ModulatorStateFactory_getFactory()
  231. {
  232. static ModulatorStateFactory ModulatorFactory{};
  233. return &ModulatorFactory;
  234. }