chorus.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 <climits>
  23. #include <cmath>
  24. #include <cstdlib>
  25. #include <iterator>
  26. #include "alcmain.h"
  27. #include "alcontext.h"
  28. #include "almalloc.h"
  29. #include "alnumeric.h"
  30. #include "alspan.h"
  31. #include "alu.h"
  32. #include "core/ambidefs.h"
  33. #include "effects/base.h"
  34. #include "effectslot.h"
  35. #include "math_defs.h"
  36. #include "opthelpers.h"
  37. #include "vector.h"
  38. namespace {
  39. #define MAX_UPDATE_SAMPLES 256
  40. struct ChorusState final : public EffectState {
  41. al::vector<float,16> mSampleBuffer;
  42. uint mOffset{0};
  43. uint mLfoOffset{0};
  44. uint mLfoRange{1};
  45. float mLfoScale{0.0f};
  46. uint mLfoDisp{0};
  47. /* Gains for left and right sides */
  48. struct {
  49. float Current[MAX_OUTPUT_CHANNELS]{};
  50. float Target[MAX_OUTPUT_CHANNELS]{};
  51. } mGains[2];
  52. /* effect parameters */
  53. ChorusWaveform mWaveform{};
  54. int mDelay{0};
  55. float mDepth{0.0f};
  56. float mFeedback{0.0f};
  57. void getTriangleDelays(uint (*delays)[MAX_UPDATE_SAMPLES], const size_t todo);
  58. void getSinusoidDelays(uint (*delays)[MAX_UPDATE_SAMPLES], const size_t todo);
  59. void deviceUpdate(const ALCdevice *device, const Buffer &buffer) override;
  60. void update(const ALCcontext *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. DEF_NEWDEL(ChorusState)
  65. };
  66. void ChorusState::deviceUpdate(const ALCdevice *Device, const Buffer&)
  67. {
  68. constexpr float max_delay{maxf(AL_CHORUS_MAX_DELAY, AL_FLANGER_MAX_DELAY)};
  69. const auto frequency = static_cast<float>(Device->Frequency);
  70. const size_t maxlen{NextPowerOf2(float2uint(max_delay*2.0f*frequency) + 1u)};
  71. if(maxlen != mSampleBuffer.size())
  72. al::vector<float,16>(maxlen).swap(mSampleBuffer);
  73. std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), 0.0f);
  74. for(auto &e : mGains)
  75. {
  76. std::fill(std::begin(e.Current), std::end(e.Current), 0.0f);
  77. std::fill(std::begin(e.Target), std::end(e.Target), 0.0f);
  78. }
  79. }
  80. void ChorusState::update(const ALCcontext *Context, const EffectSlot *Slot,
  81. const EffectProps *props, const EffectTarget target)
  82. {
  83. constexpr int mindelay{(MaxResamplerPadding>>1) << MixerFracBits};
  84. /* The LFO depth is scaled to be relative to the sample delay. Clamp the
  85. * delay and depth to allow enough padding for resampling.
  86. */
  87. const ALCdevice *device{Context->mDevice.get()};
  88. const auto frequency = static_cast<float>(device->Frequency);
  89. mWaveform = props->Chorus.Waveform;
  90. mDelay = maxi(float2int(props->Chorus.Delay*frequency*MixerFracOne + 0.5f), mindelay);
  91. mDepth = minf(props->Chorus.Depth * static_cast<float>(mDelay),
  92. static_cast<float>(mDelay - mindelay));
  93. mFeedback = props->Chorus.Feedback;
  94. /* Gains for left and right sides */
  95. const auto lcoeffs = CalcDirectionCoeffs({-1.0f, 0.0f, 0.0f}, 0.0f);
  96. const auto rcoeffs = CalcDirectionCoeffs({ 1.0f, 0.0f, 0.0f}, 0.0f);
  97. mOutTarget = target.Main->Buffer;
  98. ComputePanGains(target.Main, lcoeffs.data(), Slot->Gain, mGains[0].Target);
  99. ComputePanGains(target.Main, rcoeffs.data(), Slot->Gain, mGains[1].Target);
  100. float rate{props->Chorus.Rate};
  101. if(!(rate > 0.0f))
  102. {
  103. mLfoOffset = 0;
  104. mLfoRange = 1;
  105. mLfoScale = 0.0f;
  106. mLfoDisp = 0;
  107. }
  108. else
  109. {
  110. /* Calculate LFO coefficient (number of samples per cycle). Limit the
  111. * max range to avoid overflow when calculating the displacement.
  112. */
  113. uint lfo_range{float2uint(minf(frequency/rate + 0.5f, float{INT_MAX/360 - 180}))};
  114. mLfoOffset = mLfoOffset * lfo_range / mLfoRange;
  115. mLfoRange = lfo_range;
  116. switch(mWaveform)
  117. {
  118. case ChorusWaveform::Triangle:
  119. mLfoScale = 4.0f / static_cast<float>(mLfoRange);
  120. break;
  121. case ChorusWaveform::Sinusoid:
  122. mLfoScale = al::MathDefs<float>::Tau() / static_cast<float>(mLfoRange);
  123. break;
  124. }
  125. /* Calculate lfo phase displacement */
  126. int phase{props->Chorus.Phase};
  127. if(phase < 0) phase = 360 + phase;
  128. mLfoDisp = (mLfoRange*static_cast<uint>(phase) + 180) / 360;
  129. }
  130. }
  131. void ChorusState::getTriangleDelays(uint (*delays)[MAX_UPDATE_SAMPLES], const size_t todo)
  132. {
  133. const uint lfo_range{mLfoRange};
  134. const float lfo_scale{mLfoScale};
  135. const float depth{mDepth};
  136. const int delay{mDelay};
  137. ASSUME(lfo_range > 0);
  138. ASSUME(todo > 0);
  139. uint offset{mLfoOffset};
  140. auto gen_lfo = [&offset,lfo_range,lfo_scale,depth,delay]() -> uint
  141. {
  142. offset = (offset+1)%lfo_range;
  143. const float offset_norm{static_cast<float>(offset) * lfo_scale};
  144. return static_cast<uint>(fastf2i((1.0f-std::abs(2.0f-offset_norm)) * depth) + delay);
  145. };
  146. std::generate_n(delays[0], todo, gen_lfo);
  147. offset = (mLfoOffset+mLfoDisp) % lfo_range;
  148. std::generate_n(delays[1], todo, gen_lfo);
  149. mLfoOffset = static_cast<uint>(mLfoOffset+todo) % lfo_range;
  150. }
  151. void ChorusState::getSinusoidDelays(uint (*delays)[MAX_UPDATE_SAMPLES], const size_t todo)
  152. {
  153. const uint lfo_range{mLfoRange};
  154. const float lfo_scale{mLfoScale};
  155. const float depth{mDepth};
  156. const int delay{mDelay};
  157. ASSUME(lfo_range > 0);
  158. ASSUME(todo > 0);
  159. uint offset{mLfoOffset};
  160. auto gen_lfo = [&offset,lfo_range,lfo_scale,depth,delay]() -> uint
  161. {
  162. offset = (offset+1)%lfo_range;
  163. const float offset_norm{static_cast<float>(offset) * lfo_scale};
  164. return static_cast<uint>(fastf2i(std::sin(offset_norm)*depth) + delay);
  165. };
  166. std::generate_n(delays[0], todo, gen_lfo);
  167. offset = (mLfoOffset+mLfoDisp) % lfo_range;
  168. std::generate_n(delays[1], todo, gen_lfo);
  169. mLfoOffset = static_cast<uint>(mLfoOffset+todo) % lfo_range;
  170. }
  171. void ChorusState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  172. {
  173. const size_t bufmask{mSampleBuffer.size()-1};
  174. const float feedback{mFeedback};
  175. const uint avgdelay{(static_cast<uint>(mDelay) + (MixerFracOne>>1)) >> MixerFracBits};
  176. float *RESTRICT delaybuf{mSampleBuffer.data()};
  177. uint offset{mOffset};
  178. for(size_t base{0u};base < samplesToDo;)
  179. {
  180. const size_t todo{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
  181. uint moddelays[2][MAX_UPDATE_SAMPLES];
  182. if(mWaveform == ChorusWaveform::Sinusoid)
  183. getSinusoidDelays(moddelays, todo);
  184. else /*if(mWaveform == ChorusWaveform::Triangle)*/
  185. getTriangleDelays(moddelays, todo);
  186. alignas(16) float temps[2][MAX_UPDATE_SAMPLES];
  187. for(size_t i{0u};i < todo;++i)
  188. {
  189. // Feed the buffer's input first (necessary for delays < 1).
  190. delaybuf[offset&bufmask] = samplesIn[0][base+i];
  191. // Tap for the left output.
  192. uint delay{offset - (moddelays[0][i]>>MixerFracBits)};
  193. float mu{static_cast<float>(moddelays[0][i]&MixerFracMask) * (1.0f/MixerFracOne)};
  194. temps[0][i] = cubic(delaybuf[(delay+1) & bufmask], delaybuf[(delay ) & bufmask],
  195. delaybuf[(delay-1) & bufmask], delaybuf[(delay-2) & bufmask], mu);
  196. // Tap for the right output.
  197. delay = offset - (moddelays[1][i]>>MixerFracBits);
  198. mu = static_cast<float>(moddelays[1][i]&MixerFracMask) * (1.0f/MixerFracOne);
  199. temps[1][i] = cubic(delaybuf[(delay+1) & bufmask], delaybuf[(delay ) & bufmask],
  200. delaybuf[(delay-1) & bufmask], delaybuf[(delay-2) & bufmask], mu);
  201. // Accumulate feedback from the average delay of the taps.
  202. delaybuf[offset&bufmask] += delaybuf[(offset-avgdelay) & bufmask] * feedback;
  203. ++offset;
  204. }
  205. for(ALsizei c{0};c < 2;++c)
  206. MixSamples({temps[c], todo}, samplesOut, mGains[c].Current, mGains[c].Target,
  207. samplesToDo-base, base);
  208. base += todo;
  209. }
  210. mOffset = offset;
  211. }
  212. struct ChorusStateFactory final : public EffectStateFactory {
  213. al::intrusive_ptr<EffectState> create() override
  214. { return al::intrusive_ptr<EffectState>{new ChorusState{}}; }
  215. };
  216. /* Flanger is basically a chorus with a really short delay. They can both use
  217. * the same processing functions, so piggyback flanger on the chorus functions.
  218. */
  219. struct FlangerStateFactory final : public EffectStateFactory {
  220. al::intrusive_ptr<EffectState> create() override
  221. { return al::intrusive_ptr<EffectState>{new ChorusState{}}; }
  222. };
  223. } // namespace
  224. EffectStateFactory *ChorusStateFactory_getFactory()
  225. {
  226. static ChorusStateFactory ChorusFactory{};
  227. return &ChorusFactory;
  228. }
  229. EffectStateFactory *FlangerStateFactory_getFactory()
  230. {
  231. static FlangerStateFactory FlangerFactory{};
  232. return &FlangerFactory;
  233. }