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

178 lines
6.2 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 <algorithm>
  22. #include <array>
  23. #include <cstdlib>
  24. #include <iterator>
  25. #include "alc/effects/base.h"
  26. #include "almalloc.h"
  27. #include "alnumbers.h"
  28. #include "alnumeric.h"
  29. #include "alspan.h"
  30. #include "core/bufferline.h"
  31. #include "core/context.h"
  32. #include "core/devformat.h"
  33. #include "core/device.h"
  34. #include "core/effectslot.h"
  35. #include "core/filters/biquad.h"
  36. #include "core/mixer.h"
  37. #include "core/mixer/defs.h"
  38. #include "intrusive_ptr.h"
  39. namespace {
  40. struct DistortionState final : public EffectState {
  41. /* Effect gains for each channel */
  42. float mGain[MAX_OUTPUT_CHANNELS]{};
  43. /* Effect parameters */
  44. BiquadFilter mLowpass;
  45. BiquadFilter mBandpass;
  46. float mAttenuation{};
  47. float mEdgeCoeff{};
  48. float mBuffer[2][BufferLineSize]{};
  49. void deviceUpdate(const DeviceBase *device, const Buffer &buffer) override;
  50. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  51. const EffectTarget target) override;
  52. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  53. const al::span<FloatBufferLine> samplesOut) override;
  54. DEF_NEWDEL(DistortionState)
  55. };
  56. void DistortionState::deviceUpdate(const DeviceBase*, const Buffer&)
  57. {
  58. mLowpass.clear();
  59. mBandpass.clear();
  60. }
  61. void DistortionState::update(const ContextBase *context, const EffectSlot *slot,
  62. const EffectProps *props, const EffectTarget target)
  63. {
  64. const DeviceBase *device{context->mDevice};
  65. /* Store waveshaper edge settings. */
  66. const float edge{minf(std::sin(al::numbers::pi_v<float>*0.5f * props->Distortion.Edge),
  67. 0.99f)};
  68. mEdgeCoeff = 2.0f * edge / (1.0f-edge);
  69. float cutoff{props->Distortion.LowpassCutoff};
  70. /* Bandwidth value is constant in octaves. */
  71. float bandwidth{(cutoff / 2.0f) / (cutoff * 0.67f)};
  72. /* Divide normalized frequency by the amount of oversampling done during
  73. * processing.
  74. */
  75. auto frequency = static_cast<float>(device->Frequency);
  76. mLowpass.setParamsFromBandwidth(BiquadType::LowPass, cutoff/frequency/4.0f, 1.0f, bandwidth);
  77. cutoff = props->Distortion.EQCenter;
  78. /* Convert bandwidth in Hz to octaves. */
  79. bandwidth = props->Distortion.EQBandwidth / (cutoff * 0.67f);
  80. mBandpass.setParamsFromBandwidth(BiquadType::BandPass, cutoff/frequency/4.0f, 1.0f, bandwidth);
  81. const auto coeffs = CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f);
  82. mOutTarget = target.Main->Buffer;
  83. ComputePanGains(target.Main, coeffs.data(), slot->Gain*props->Distortion.Gain, mGain);
  84. }
  85. void DistortionState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  86. {
  87. const float fc{mEdgeCoeff};
  88. for(size_t base{0u};base < samplesToDo;)
  89. {
  90. /* Perform 4x oversampling to avoid aliasing. Oversampling greatly
  91. * improves distortion quality and allows to implement lowpass and
  92. * bandpass filters using high frequencies, at which classic IIR
  93. * filters became unstable.
  94. */
  95. size_t todo{minz(BufferLineSize, (samplesToDo-base) * 4)};
  96. /* Fill oversample buffer using zero stuffing. Multiply the sample by
  97. * the amount of oversampling to maintain the signal's power.
  98. */
  99. for(size_t i{0u};i < todo;i++)
  100. mBuffer[0][i] = !(i&3) ? samplesIn[0][(i>>2)+base] * 4.0f : 0.0f;
  101. /* First step, do lowpass filtering of original signal. Additionally
  102. * perform buffer interpolation and lowpass cutoff for oversampling
  103. * (which is fortunately first step of distortion). So combine three
  104. * operations into the one.
  105. */
  106. mLowpass.process({mBuffer[0], todo}, mBuffer[1]);
  107. /* Second step, do distortion using waveshaper function to emulate
  108. * signal processing during tube overdriving. Three steps of
  109. * waveshaping are intended to modify waveform without boost/clipping/
  110. * attenuation process.
  111. */
  112. auto proc_sample = [fc](float smp) -> float
  113. {
  114. smp = (1.0f + fc) * smp/(1.0f + fc*std::abs(smp));
  115. smp = (1.0f + fc) * smp/(1.0f + fc*std::abs(smp)) * -1.0f;
  116. smp = (1.0f + fc) * smp/(1.0f + fc*std::abs(smp));
  117. return smp;
  118. };
  119. std::transform(std::begin(mBuffer[1]), std::begin(mBuffer[1])+todo, std::begin(mBuffer[0]),
  120. proc_sample);
  121. /* Third step, do bandpass filtering of distorted signal. */
  122. mBandpass.process({mBuffer[0], todo}, mBuffer[1]);
  123. todo >>= 2;
  124. const float *outgains{mGain};
  125. for(FloatBufferLine &output : samplesOut)
  126. {
  127. /* Fourth step, final, do attenuation and perform decimation,
  128. * storing only one sample out of four.
  129. */
  130. const float gain{*(outgains++)};
  131. if(!(std::fabs(gain) > GainSilenceThreshold))
  132. continue;
  133. for(size_t i{0u};i < todo;i++)
  134. output[base+i] += gain * mBuffer[1][i*4];
  135. }
  136. base += todo;
  137. }
  138. }
  139. struct DistortionStateFactory final : public EffectStateFactory {
  140. al::intrusive_ptr<EffectState> create() override
  141. { return al::intrusive_ptr<EffectState>{new DistortionState{}}; }
  142. };
  143. } // namespace
  144. EffectStateFactory *DistortionStateFactory_getFactory()
  145. {
  146. static DistortionStateFactory DistortionFactory{};
  147. return &DistortionFactory;
  148. }