equalizer.cpp 8.2 KB

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