🛠️🐜 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.

301 lines
10 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2018 by Raul Herraiz.
  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 <algorithm>
  24. #include "alMain.h"
  25. #include "alcontext.h"
  26. #include "alAuxEffectSlot.h"
  27. #include "alError.h"
  28. #include "alu.h"
  29. #include "filters/biquad.h"
  30. #include "vecmat.h"
  31. namespace {
  32. #define MIN_FREQ 20.0f
  33. #define MAX_FREQ 2500.0f
  34. #define Q_FACTOR 5.0f
  35. struct ALautowahState final : public EffectState {
  36. /* Effect parameters */
  37. ALfloat mAttackRate;
  38. ALfloat mReleaseRate;
  39. ALfloat mResonanceGain;
  40. ALfloat mPeakGain;
  41. ALfloat mFreqMinNorm;
  42. ALfloat mBandwidthNorm;
  43. ALfloat mEnvDelay;
  44. /* Filter components derived from the envelope. */
  45. struct {
  46. ALfloat cos_w0;
  47. ALfloat alpha;
  48. } mEnv[BUFFERSIZE];
  49. struct {
  50. /* Effect filters' history. */
  51. struct {
  52. ALfloat z1, z2;
  53. } Filter;
  54. /* Effect gains for each output channel */
  55. ALfloat CurrentGains[MAX_OUTPUT_CHANNELS];
  56. ALfloat TargetGains[MAX_OUTPUT_CHANNELS];
  57. } mChans[MAX_AMBI_CHANNELS];
  58. /* Effects buffers */
  59. alignas(16) ALfloat mBufferOut[BUFFERSIZE];
  60. ALboolean deviceUpdate(const ALCdevice *device) override;
  61. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  62. void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput) override;
  63. DEF_NEWDEL(ALautowahState)
  64. };
  65. ALboolean ALautowahState::deviceUpdate(const ALCdevice *UNUSED(device))
  66. {
  67. /* (Re-)initializing parameters and clear the buffers. */
  68. mAttackRate = 1.0f;
  69. mReleaseRate = 1.0f;
  70. mResonanceGain = 10.0f;
  71. mPeakGain = 4.5f;
  72. mFreqMinNorm = 4.5e-4f;
  73. mBandwidthNorm = 0.05f;
  74. mEnvDelay = 0.0f;
  75. for(auto &e : mEnv)
  76. {
  77. e.cos_w0 = 0.0f;
  78. e.alpha = 0.0f;
  79. }
  80. for(auto &chan : mChans)
  81. {
  82. std::fill(std::begin(chan.CurrentGains), std::end(chan.CurrentGains), 0.0f);
  83. chan.Filter.z1 = 0.0f;
  84. chan.Filter.z2 = 0.0f;
  85. }
  86. return AL_TRUE;
  87. }
  88. void ALautowahState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  89. {
  90. const ALCdevice *device{context->Device};
  91. const ALfloat ReleaseTime{clampf(props->Autowah.ReleaseTime, 0.001f, 1.0f)};
  92. mAttackRate = expf(-1.0f / (props->Autowah.AttackTime*device->Frequency));
  93. mReleaseRate = expf(-1.0f / (ReleaseTime*device->Frequency));
  94. /* 0-20dB Resonance Peak gain */
  95. mResonanceGain = std::sqrt(std::log10(props->Autowah.Resonance)*10.0f / 3.0f);
  96. mPeakGain = 1.0f - std::log10(props->Autowah.PeakGain/AL_AUTOWAH_MAX_PEAK_GAIN);
  97. mFreqMinNorm = MIN_FREQ / device->Frequency;
  98. mBandwidthNorm = (MAX_FREQ-MIN_FREQ) / device->Frequency;
  99. mOutBuffer = target.Main->Buffer;
  100. mOutChannels = target.Main->NumChannels;
  101. for(ALsizei i{0};i < slot->Wet.NumChannels;++i)
  102. {
  103. auto coeffs = GetAmbiIdentityRow(i);
  104. ComputePanGains(target.Main, coeffs.data(), slot->Params.Gain, mChans[i].TargetGains);
  105. }
  106. }
  107. void ALautowahState::process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput)
  108. {
  109. const ALfloat attack_rate = mAttackRate;
  110. const ALfloat release_rate = mReleaseRate;
  111. const ALfloat res_gain = mResonanceGain;
  112. const ALfloat peak_gain = mPeakGain;
  113. const ALfloat freq_min = mFreqMinNorm;
  114. const ALfloat bandwidth = mBandwidthNorm;
  115. ALfloat env_delay;
  116. ALsizei c, i;
  117. env_delay = mEnvDelay;
  118. for(i = 0;i < samplesToDo;i++)
  119. {
  120. ALfloat w0, sample, a;
  121. /* Envelope follower described on the book: Audio Effects, Theory,
  122. * Implementation and Application.
  123. */
  124. sample = peak_gain * std::fabs(samplesIn[0][i]);
  125. a = (sample > env_delay) ? attack_rate : release_rate;
  126. env_delay = lerp(sample, env_delay, a);
  127. /* Calculate the cos and alpha components for this sample's filter. */
  128. w0 = minf((bandwidth*env_delay + freq_min), 0.46f) * al::MathDefs<float>::Tau();
  129. mEnv[i].cos_w0 = cosf(w0);
  130. mEnv[i].alpha = sinf(w0)/(2.0f * Q_FACTOR);
  131. }
  132. mEnvDelay = env_delay;
  133. ASSUME(numInput > 0);
  134. for(c = 0;c < numInput;++c)
  135. {
  136. /* This effectively inlines BiquadFilter_setParams for a peaking
  137. * filter and BiquadFilter_processC. The alpha and cosine components
  138. * for the filter coefficients were previously calculated with the
  139. * envelope. Because the filter changes for each sample, the
  140. * coefficients are transient and don't need to be held.
  141. */
  142. ALfloat z1 = mChans[c].Filter.z1;
  143. ALfloat z2 = mChans[c].Filter.z2;
  144. for(i = 0;i < samplesToDo;i++)
  145. {
  146. const ALfloat alpha = mEnv[i].alpha;
  147. const ALfloat cos_w0 = mEnv[i].cos_w0;
  148. ALfloat input, output;
  149. ALfloat a[3], b[3];
  150. b[0] = 1.0f + alpha*res_gain;
  151. b[1] = -2.0f * cos_w0;
  152. b[2] = 1.0f - alpha*res_gain;
  153. a[0] = 1.0f + alpha/res_gain;
  154. a[1] = -2.0f * cos_w0;
  155. a[2] = 1.0f - alpha/res_gain;
  156. input = samplesIn[c][i];
  157. output = input*(b[0]/a[0]) + z1;
  158. z1 = input*(b[1]/a[0]) - output*(a[1]/a[0]) + z2;
  159. z2 = input*(b[2]/a[0]) - output*(a[2]/a[0]);
  160. mBufferOut[i] = output;
  161. }
  162. mChans[c].Filter.z1 = z1;
  163. mChans[c].Filter.z2 = z2;
  164. /* Now, mix the processed sound data to the output. */
  165. MixSamples(mBufferOut, numOutput, samplesOut, mChans[c].CurrentGains,
  166. mChans[c].TargetGains, samplesToDo, 0, samplesToDo);
  167. }
  168. }
  169. void ALautowah_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  170. {
  171. switch(param)
  172. {
  173. case AL_AUTOWAH_ATTACK_TIME:
  174. if(!(val >= AL_AUTOWAH_MIN_ATTACK_TIME && val <= AL_AUTOWAH_MAX_ATTACK_TIME))
  175. SETERR_RETURN(context, AL_INVALID_VALUE,,"Autowah attack time out of range");
  176. props->Autowah.AttackTime = val;
  177. break;
  178. case AL_AUTOWAH_RELEASE_TIME:
  179. if(!(val >= AL_AUTOWAH_MIN_RELEASE_TIME && val <= AL_AUTOWAH_MAX_RELEASE_TIME))
  180. SETERR_RETURN(context, AL_INVALID_VALUE,,"Autowah release time out of range");
  181. props->Autowah.ReleaseTime = val;
  182. break;
  183. case AL_AUTOWAH_RESONANCE:
  184. if(!(val >= AL_AUTOWAH_MIN_RESONANCE && val <= AL_AUTOWAH_MAX_RESONANCE))
  185. SETERR_RETURN(context, AL_INVALID_VALUE,,"Autowah resonance out of range");
  186. props->Autowah.Resonance = val;
  187. break;
  188. case AL_AUTOWAH_PEAK_GAIN:
  189. if(!(val >= AL_AUTOWAH_MIN_PEAK_GAIN && val <= AL_AUTOWAH_MAX_PEAK_GAIN))
  190. SETERR_RETURN(context, AL_INVALID_VALUE,,"Autowah peak gain out of range");
  191. props->Autowah.PeakGain = val;
  192. break;
  193. default:
  194. alSetError(context, AL_INVALID_ENUM, "Invalid autowah float property 0x%04x", param);
  195. }
  196. }
  197. void ALautowah_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  198. { ALautowah_setParamf(props, context, param, vals[0]); }
  199. void ALautowah_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
  200. { alSetError(context, AL_INVALID_ENUM, "Invalid autowah integer property 0x%04x", param); }
  201. void ALautowah_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
  202. { alSetError(context, AL_INVALID_ENUM, "Invalid autowah integer vector property 0x%04x", param); }
  203. void ALautowah_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  204. {
  205. switch(param)
  206. {
  207. case AL_AUTOWAH_ATTACK_TIME:
  208. *val = props->Autowah.AttackTime;
  209. break;
  210. case AL_AUTOWAH_RELEASE_TIME:
  211. *val = props->Autowah.ReleaseTime;
  212. break;
  213. case AL_AUTOWAH_RESONANCE:
  214. *val = props->Autowah.Resonance;
  215. break;
  216. case AL_AUTOWAH_PEAK_GAIN:
  217. *val = props->Autowah.PeakGain;
  218. break;
  219. default:
  220. alSetError(context, AL_INVALID_ENUM, "Invalid autowah float property 0x%04x", param);
  221. }
  222. }
  223. void ALautowah_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  224. { ALautowah_getParamf(props, context, param, vals); }
  225. void ALautowah_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  226. { alSetError(context, AL_INVALID_ENUM, "Invalid autowah integer property 0x%04x", param); }
  227. void ALautowah_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  228. { alSetError(context, AL_INVALID_ENUM, "Invalid autowah integer vector property 0x%04x", param); }
  229. DEFINE_ALEFFECT_VTABLE(ALautowah);
  230. struct AutowahStateFactory final : public EffectStateFactory {
  231. EffectState *create() override { return new ALautowahState{}; }
  232. EffectProps getDefaultProps() const noexcept override;
  233. const EffectVtable *getEffectVtable() const noexcept override { return &ALautowah_vtable; }
  234. };
  235. EffectProps AutowahStateFactory::getDefaultProps() const noexcept
  236. {
  237. EffectProps props{};
  238. props.Autowah.AttackTime = AL_AUTOWAH_DEFAULT_ATTACK_TIME;
  239. props.Autowah.ReleaseTime = AL_AUTOWAH_DEFAULT_RELEASE_TIME;
  240. props.Autowah.Resonance = AL_AUTOWAH_DEFAULT_RESONANCE;
  241. props.Autowah.PeakGain = AL_AUTOWAH_DEFAULT_PEAK_GAIN;
  242. return props;
  243. }
  244. } // namespace
  245. EffectStateFactory *AutowahStateFactory_getFactory()
  246. {
  247. static AutowahStateFactory AutowahFactory{};
  248. return &AutowahFactory;
  249. }