distortion.cpp 6.7 KB

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