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

275 lines
10 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2013 by Mike Gorchak
  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 "alMain.h"
  25. #include "alcontext.h"
  26. #include "alAuxEffectSlot.h"
  27. #include "alError.h"
  28. #include "alu.h"
  29. #include "filters/biquad.h"
  30. namespace {
  31. struct DistortionState final : public EffectState {
  32. /* Effect gains for each channel */
  33. ALfloat mGain[MAX_OUTPUT_CHANNELS]{};
  34. /* Effect parameters */
  35. BiquadFilter mLowpass;
  36. BiquadFilter mBandpass;
  37. ALfloat mAttenuation{};
  38. ALfloat mEdgeCoeff{};
  39. ALfloat mBuffer[2][BUFFERSIZE]{};
  40. ALboolean deviceUpdate(const ALCdevice *device) override;
  41. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  42. void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput) override;
  43. DEF_NEWDEL(DistortionState)
  44. };
  45. ALboolean DistortionState::deviceUpdate(const ALCdevice *UNUSED(device))
  46. {
  47. mLowpass.clear();
  48. mBandpass.clear();
  49. return AL_TRUE;
  50. }
  51. void DistortionState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  52. {
  53. const ALCdevice *device{context->Device};
  54. /* Store waveshaper edge settings. */
  55. const ALfloat edge{
  56. minf(std::sin(al::MathDefs<float>::Pi()*0.5f * props->Distortion.Edge), 0.99f)};
  57. mEdgeCoeff = 2.0f * edge / (1.0f-edge);
  58. ALfloat cutoff{props->Distortion.LowpassCutoff};
  59. /* Bandwidth value is constant in octaves. */
  60. ALfloat bandwidth{(cutoff / 2.0f) / (cutoff * 0.67f)};
  61. /* Multiply sampling frequency by the amount of oversampling done during
  62. * processing.
  63. */
  64. auto frequency = static_cast<ALfloat>(device->Frequency);
  65. mLowpass.setParams(BiquadType::LowPass, 1.0f, cutoff / (frequency*4.0f),
  66. calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  67. );
  68. cutoff = props->Distortion.EQCenter;
  69. /* Convert bandwidth in Hz to octaves. */
  70. bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
  71. mBandpass.setParams(BiquadType::BandPass, 1.0f, cutoff / (frequency*4.0f),
  72. calc_rcpQ_from_bandwidth(cutoff / (frequency*4.0f), bandwidth)
  73. );
  74. ALfloat coeffs[MAX_AMBI_CHANNELS];
  75. CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f, coeffs);
  76. mOutBuffer = target.Main->Buffer;
  77. mOutChannels = target.Main->NumChannels;
  78. ComputePanGains(target.Main, coeffs, slot->Params.Gain*props->Distortion.Gain, mGain);
  79. }
  80. void DistortionState::process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei /*numInput*/, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput)
  81. {
  82. ALfloat (*RESTRICT buffer)[BUFFERSIZE] = mBuffer;
  83. const ALfloat fc = mEdgeCoeff;
  84. ALsizei base;
  85. ALsizei i, k;
  86. for(base = 0;base < samplesToDo;)
  87. {
  88. /* Perform 4x oversampling to avoid aliasing. Oversampling greatly
  89. * improves distortion quality and allows to implement lowpass and
  90. * bandpass filters using high frequencies, at which classic IIR
  91. * filters became unstable.
  92. */
  93. ALsizei todo{mini(BUFFERSIZE, (samplesToDo-base) * 4)};
  94. /* Fill oversample buffer using zero stuffing. Multiply the sample by
  95. * the amount of oversampling to maintain the signal's power.
  96. */
  97. for(i = 0;i < todo;i++)
  98. buffer[0][i] = !(i&3) ? samplesIn[0][(i>>2)+base] * 4.0f : 0.0f;
  99. /* First step, do lowpass filtering of original signal. Additionally
  100. * perform buffer interpolation and lowpass cutoff for oversampling
  101. * (which is fortunately first step of distortion). So combine three
  102. * operations into the one.
  103. */
  104. mLowpass.process(buffer[1], buffer[0], todo);
  105. /* Second step, do distortion using waveshaper function to emulate
  106. * signal processing during tube overdriving. Three steps of
  107. * waveshaping are intended to modify waveform without boost/clipping/
  108. * attenuation process.
  109. */
  110. for(i = 0;i < todo;i++)
  111. {
  112. ALfloat smp = buffer[1][i];
  113. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  114. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp)) * -1.0f;
  115. smp = (1.0f + fc) * smp/(1.0f + fc*fabsf(smp));
  116. buffer[0][i] = smp;
  117. }
  118. /* Third step, do bandpass filtering of distorted signal. */
  119. mBandpass.process(buffer[1], buffer[0], todo);
  120. todo >>= 2;
  121. for(k = 0;k < numOutput;k++)
  122. {
  123. /* Fourth step, final, do attenuation and perform decimation,
  124. * storing only one sample out of four.
  125. */
  126. const ALfloat gain{mGain[k]};
  127. if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
  128. continue;
  129. for(i = 0;i < todo;i++)
  130. samplesOut[k][base+i] += gain * buffer[1][i*4];
  131. }
  132. base += todo;
  133. }
  134. }
  135. void Distortion_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
  136. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
  137. void Distortion_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
  138. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
  139. void Distortion_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  140. {
  141. switch(param)
  142. {
  143. case AL_DISTORTION_EDGE:
  144. if(!(val >= AL_DISTORTION_MIN_EDGE && val <= AL_DISTORTION_MAX_EDGE))
  145. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion edge out of range");
  146. props->Distortion.Edge = val;
  147. break;
  148. case AL_DISTORTION_GAIN:
  149. if(!(val >= AL_DISTORTION_MIN_GAIN && val <= AL_DISTORTION_MAX_GAIN))
  150. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion gain out of range");
  151. props->Distortion.Gain = val;
  152. break;
  153. case AL_DISTORTION_LOWPASS_CUTOFF:
  154. if(!(val >= AL_DISTORTION_MIN_LOWPASS_CUTOFF && val <= AL_DISTORTION_MAX_LOWPASS_CUTOFF))
  155. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion low-pass cutoff out of range");
  156. props->Distortion.LowpassCutoff = val;
  157. break;
  158. case AL_DISTORTION_EQCENTER:
  159. if(!(val >= AL_DISTORTION_MIN_EQCENTER && val <= AL_DISTORTION_MAX_EQCENTER))
  160. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ center out of range");
  161. props->Distortion.EQCenter = val;
  162. break;
  163. case AL_DISTORTION_EQBANDWIDTH:
  164. if(!(val >= AL_DISTORTION_MIN_EQBANDWIDTH && val <= AL_DISTORTION_MAX_EQBANDWIDTH))
  165. SETERR_RETURN(context, AL_INVALID_VALUE,, "Distortion EQ bandwidth out of range");
  166. props->Distortion.EQBandwidth = val;
  167. break;
  168. default:
  169. alSetError(context, AL_INVALID_ENUM, "Invalid distortion float property 0x%04x",
  170. param);
  171. }
  172. }
  173. void Distortion_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  174. { Distortion_setParamf(props, context, param, vals[0]); }
  175. void Distortion_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  176. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer property 0x%04x", param); }
  177. void Distortion_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  178. { alSetError(context, AL_INVALID_ENUM, "Invalid distortion integer-vector property 0x%04x", param); }
  179. void Distortion_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  180. {
  181. switch(param)
  182. {
  183. case AL_DISTORTION_EDGE:
  184. *val = props->Distortion.Edge;
  185. break;
  186. case AL_DISTORTION_GAIN:
  187. *val = props->Distortion.Gain;
  188. break;
  189. case AL_DISTORTION_LOWPASS_CUTOFF:
  190. *val = props->Distortion.LowpassCutoff;
  191. break;
  192. case AL_DISTORTION_EQCENTER:
  193. *val = props->Distortion.EQCenter;
  194. break;
  195. case AL_DISTORTION_EQBANDWIDTH:
  196. *val = props->Distortion.EQBandwidth;
  197. break;
  198. default:
  199. alSetError(context, AL_INVALID_ENUM, "Invalid distortion float property 0x%04x",
  200. param);
  201. }
  202. }
  203. void Distortion_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  204. { Distortion_getParamf(props, context, param, vals); }
  205. DEFINE_ALEFFECT_VTABLE(Distortion);
  206. struct DistortionStateFactory final : public EffectStateFactory {
  207. EffectState *create() override { return new DistortionState{}; }
  208. EffectProps getDefaultProps() const noexcept override;
  209. const EffectVtable *getEffectVtable() const noexcept override { return &Distortion_vtable; }
  210. };
  211. EffectProps DistortionStateFactory::getDefaultProps() const noexcept
  212. {
  213. EffectProps props{};
  214. props.Distortion.Edge = AL_DISTORTION_DEFAULT_EDGE;
  215. props.Distortion.Gain = AL_DISTORTION_DEFAULT_GAIN;
  216. props.Distortion.LowpassCutoff = AL_DISTORTION_DEFAULT_LOWPASS_CUTOFF;
  217. props.Distortion.EQCenter = AL_DISTORTION_DEFAULT_EQCENTER;
  218. props.Distortion.EQBandwidth = AL_DISTORTION_DEFAULT_EQBANDWIDTH;
  219. return props;
  220. }
  221. } // namespace
  222. EffectStateFactory *DistortionStateFactory_getFactory()
  223. {
  224. static DistortionStateFactory DistortionFactory{};
  225. return &DistortionFactory;
  226. }