🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

293 lines
9.8 KiB

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