echo.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2009 by Chris Robinson.
  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 <vector>
  27. #include "alc/effects/base.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 "intrusive_ptr.h"
  39. #include "opthelpers.h"
  40. struct BufferStorage;
  41. namespace {
  42. using uint = unsigned int;
  43. constexpr float LowpassFreqRef{5000.0f};
  44. struct EchoState final : public EffectState {
  45. std::vector<float> mSampleBuffer;
  46. // The echo is two tap. The delay is the number of samples from before the
  47. // current offset
  48. std::array<size_t,2> mDelayTap{};
  49. size_t mOffset{0u};
  50. /* The panning gains for the two taps */
  51. struct OutGains {
  52. std::array<float,MaxAmbiChannels> Current{};
  53. std::array<float,MaxAmbiChannels> Target{};
  54. };
  55. std::array<OutGains,2> mGains;
  56. BiquadFilter mFilter;
  57. float mFeedGain{0.0f};
  58. alignas(16) std::array<FloatBufferLine,2> mTempBuffer{};
  59. void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
  60. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  61. const EffectTarget target) override;
  62. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  63. const al::span<FloatBufferLine> samplesOut) override;
  64. };
  65. void EchoState::deviceUpdate(const DeviceBase *Device, const BufferStorage*)
  66. {
  67. const auto frequency = static_cast<float>(Device->Frequency);
  68. // Use the next power of 2 for the buffer length, so the tap offsets can be
  69. // wrapped using a mask instead of a modulo
  70. const uint maxlen{NextPowerOf2(float2uint(EchoMaxDelay*frequency + 0.5f) +
  71. float2uint(EchoMaxLRDelay*frequency + 0.5f))};
  72. if(maxlen != mSampleBuffer.size())
  73. decltype(mSampleBuffer)(maxlen).swap(mSampleBuffer);
  74. std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), 0.0f);
  75. for(auto &e : mGains)
  76. {
  77. std::fill(e.Current.begin(), e.Current.end(), 0.0f);
  78. std::fill(e.Target.begin(), e.Target.end(), 0.0f);
  79. }
  80. }
  81. void EchoState::update(const ContextBase *context, const EffectSlot *slot,
  82. const EffectProps *props_, const EffectTarget target)
  83. {
  84. auto &props = std::get<EchoProps>(*props_);
  85. const DeviceBase *device{context->mDevice};
  86. const auto frequency = static_cast<float>(device->Frequency);
  87. mDelayTap[0] = std::max(float2uint(std::round(props.Delay*frequency)), 1u);
  88. mDelayTap[1] = float2uint(std::round(props.LRDelay*frequency)) + mDelayTap[0];
  89. const float gainhf{std::max(1.0f - props.Damping, 0.0625f)}; /* Limit -24dB */
  90. mFilter.setParamsFromSlope(BiquadType::HighShelf, LowpassFreqRef/frequency, gainhf, 1.0f);
  91. mFeedGain = props.Feedback;
  92. /* Convert echo spread (where 0 = center, +/-1 = sides) to a 2D vector. */
  93. const float x{props.Spread}; /* +x = left */
  94. const float z{std::sqrt(1.0f - x*x)};
  95. const auto coeffs0 = CalcAmbiCoeffs( x, 0.0f, z, 0.0f);
  96. const auto coeffs1 = CalcAmbiCoeffs(-x, 0.0f, z, 0.0f);
  97. mOutTarget = target.Main->Buffer;
  98. ComputePanGains(target.Main, coeffs0, slot->Gain, mGains[0].Target);
  99. ComputePanGains(target.Main, coeffs1, slot->Gain, mGains[1].Target);
  100. }
  101. void EchoState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  102. {
  103. const auto delaybuf = al::span{mSampleBuffer};
  104. const size_t mask{delaybuf.size()-1};
  105. size_t offset{mOffset};
  106. size_t tap1{offset - mDelayTap[0]};
  107. size_t tap2{offset - mDelayTap[1]};
  108. ASSUME(samplesToDo > 0);
  109. const BiquadFilter filter{mFilter};
  110. auto [z1, z2] = mFilter.getComponents();
  111. for(size_t i{0u};i < samplesToDo;)
  112. {
  113. offset &= mask;
  114. tap1 &= mask;
  115. tap2 &= mask;
  116. size_t td{std::min(mask+1 - std::max(offset, std::max(tap1, tap2)), samplesToDo-i)};
  117. do {
  118. /* Feed the delay buffer's input first. */
  119. delaybuf[offset] = samplesIn[0][i];
  120. /* Get delayed output from the first and second taps. Use the
  121. * second tap for feedback.
  122. */
  123. mTempBuffer[0][i] = delaybuf[tap1++];
  124. mTempBuffer[1][i] = delaybuf[tap2++];
  125. const float feedb{mTempBuffer[1][i++]};
  126. /* Add feedback to the delay buffer with damping and attenuation. */
  127. delaybuf[offset++] += filter.processOne(feedb, z1, z2) * mFeedGain;
  128. } while(--td);
  129. }
  130. mFilter.setComponents(z1, z2);
  131. mOffset = offset;
  132. for(size_t c{0};c < 2;c++)
  133. MixSamples(al::span{mTempBuffer[c]}.first(samplesToDo), samplesOut, mGains[c].Current,
  134. mGains[c].Target, samplesToDo, 0);
  135. }
  136. struct EchoStateFactory final : public EffectStateFactory {
  137. al::intrusive_ptr<EffectState> create() override
  138. { return al::intrusive_ptr<EffectState>{new EchoState{}}; }
  139. };
  140. } // namespace
  141. EffectStateFactory *EchoStateFactory_getFactory()
  142. {
  143. static EchoStateFactory EchoFactory{};
  144. return &EchoFactory;
  145. }