equalizer.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 <functional>
  26. #include <variant>
  27. #include "alc/effects/base.h"
  28. #include "alspan.h"
  29. #include "core/ambidefs.h"
  30. #include "core/bufferline.h"
  31. #include "core/context.h"
  32. #include "core/device.h"
  33. #include "core/effects/base.h"
  34. #include "core/effectslot.h"
  35. #include "core/filters/biquad.h"
  36. #include "core/mixer.h"
  37. #include "intrusive_ptr.h"
  38. struct BufferStorage;
  39. namespace {
  40. /* The document "Effects Extension Guide.pdf" says that low and high *
  41. * frequencies are cutoff frequencies. This is not fully correct, they *
  42. * are corner frequencies for low and high shelf filters. If they were *
  43. * just cutoff frequencies, there would be no need in cutoff frequency *
  44. * gains, which are present. Documentation for "Creative Proteus X2" *
  45. * software describes 4-band equalizer functionality in a much better *
  46. * way. This equalizer seems to be a predecessor of OpenAL 4-band *
  47. * equalizer. With low and high shelf filters we are able to cutoff *
  48. * frequencies below and/or above corner frequencies using attenuation *
  49. * gains (below 1.0) and amplify all low and/or high frequencies using *
  50. * gains above 1.0. *
  51. * *
  52. * Low-shelf Low Mid Band High Mid Band High-shelf *
  53. * corner center center corner *
  54. * frequency frequency frequency frequency *
  55. * 50Hz..800Hz 200Hz..3000Hz 1000Hz..8000Hz 4000Hz..16000Hz *
  56. * *
  57. * | | | | *
  58. * | | | | *
  59. * B -----+ /--+--\ /--+--\ +----- *
  60. * O |\ | | | | | | /| *
  61. * O | \ - | - - | - / | *
  62. * S + | \ | | | | | | / | *
  63. * T | | | | | | | | | | *
  64. * ---------+---------------+------------------+---------------+-------- *
  65. * C | | | | | | | | | | *
  66. * U - | / | | | | | | \ | *
  67. * T | / - | - - | - \ | *
  68. * O |/ | | | | | | \| *
  69. * F -----+ \--+--/ \--+--/ +----- *
  70. * F | | | | *
  71. * | | | | *
  72. * *
  73. * Gains vary from 0.126 up to 7.943, which means from -18dB attenuation *
  74. * up to +18dB amplification. Band width varies from 0.01 up to 1.0 in *
  75. * octaves for two mid bands. *
  76. * *
  77. * Implementation is based on the "Cookbook formulae for audio EQ biquad *
  78. * filter coefficients" by Robert Bristow-Johnson *
  79. * http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt */
  80. struct EqualizerState final : public EffectState {
  81. struct OutParams {
  82. uint mTargetChannel{InvalidChannelIndex};
  83. /* Effect parameters */
  84. std::array<BiquadFilter,4> mFilter;
  85. /* Effect gains for each channel */
  86. float mCurrentGain{};
  87. float mTargetGain{};
  88. };
  89. std::array<OutParams,MaxAmbiChannels> mChans;
  90. alignas(16) FloatBufferLine mSampleBuffer{};
  91. void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
  92. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  93. const EffectTarget target) override;
  94. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  95. const al::span<FloatBufferLine> samplesOut) override;
  96. };
  97. void EqualizerState::deviceUpdate(const DeviceBase*, const BufferStorage*)
  98. {
  99. for(auto &e : mChans)
  100. {
  101. e.mTargetChannel = InvalidChannelIndex;
  102. std::for_each(e.mFilter.begin(), e.mFilter.end(), std::mem_fn(&BiquadFilter::clear));
  103. e.mCurrentGain = 0.0f;
  104. }
  105. }
  106. void EqualizerState::update(const ContextBase *context, const EffectSlot *slot,
  107. const EffectProps *props_, const EffectTarget target)
  108. {
  109. auto &props = std::get<EqualizerProps>(*props_);
  110. const DeviceBase *device{context->mDevice};
  111. auto frequency = static_cast<float>(device->Frequency);
  112. /* Calculate coefficients for the each type of filter. Note that the shelf
  113. * and peaking filters' gain is for the centerpoint of the transition band,
  114. * while the effect property gains are for the shelf/peak itself. So the
  115. * property gains need their dB halved (sqrt of linear gain) for the
  116. * shelf/peak to reach the provided gain.
  117. */
  118. float gain{std::sqrt(props.LowGain)};
  119. float f0norm{props.LowCutoff / frequency};
  120. mChans[0].mFilter[0].setParamsFromSlope(BiquadType::LowShelf, f0norm, gain, 0.75f);
  121. gain = std::sqrt(props.Mid1Gain);
  122. f0norm = props.Mid1Center / frequency;
  123. mChans[0].mFilter[1].setParamsFromBandwidth(BiquadType::Peaking, f0norm, gain,
  124. props.Mid1Width);
  125. gain = std::sqrt(props.Mid2Gain);
  126. f0norm = props.Mid2Center / frequency;
  127. mChans[0].mFilter[2].setParamsFromBandwidth(BiquadType::Peaking, f0norm, gain,
  128. props.Mid2Width);
  129. gain = std::sqrt(props.HighGain);
  130. f0norm = props.HighCutoff / frequency;
  131. mChans[0].mFilter[3].setParamsFromSlope(BiquadType::HighShelf, f0norm, gain, 0.75f);
  132. /* Copy the filter coefficients for the other input channels. */
  133. for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
  134. {
  135. mChans[i].mFilter[0].copyParamsFrom(mChans[0].mFilter[0]);
  136. mChans[i].mFilter[1].copyParamsFrom(mChans[0].mFilter[1]);
  137. mChans[i].mFilter[2].copyParamsFrom(mChans[0].mFilter[2]);
  138. mChans[i].mFilter[3].copyParamsFrom(mChans[0].mFilter[3]);
  139. }
  140. mOutTarget = target.Main->Buffer;
  141. auto set_channel = [this](size_t idx, uint outchan, float outgain)
  142. {
  143. mChans[idx].mTargetChannel = outchan;
  144. mChans[idx].mTargetGain = outgain;
  145. };
  146. target.Main->setAmbiMixParams(slot->Wet, slot->Gain, set_channel);
  147. }
  148. void EqualizerState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  149. {
  150. const auto buffer = al::span{mSampleBuffer}.first(samplesToDo);
  151. auto chan = mChans.begin();
  152. for(const auto &input : samplesIn)
  153. {
  154. if(const size_t outidx{chan->mTargetChannel}; outidx != InvalidChannelIndex)
  155. {
  156. const auto inbuf = al::span{input}.first(samplesToDo);
  157. DualBiquad{chan->mFilter[0], chan->mFilter[1]}.process(inbuf, buffer);
  158. DualBiquad{chan->mFilter[2], chan->mFilter[3]}.process(buffer, buffer);
  159. MixSamples(buffer, samplesOut[outidx], chan->mCurrentGain, chan->mTargetGain,
  160. samplesToDo);
  161. }
  162. ++chan;
  163. }
  164. }
  165. struct EqualizerStateFactory final : public EffectStateFactory {
  166. al::intrusive_ptr<EffectState> create() override
  167. { return al::intrusive_ptr<EffectState>{new EqualizerState{}}; }
  168. };
  169. } // namespace
  170. EffectStateFactory *EqualizerStateFactory_getFactory()
  171. {
  172. static EqualizerStateFactory EqualizerFactory{};
  173. return &EqualizerFactory;
  174. }