🛠️🐜 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.

183 lines
5.8 KiB

  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 <cstdlib>
  24. #include <iterator>
  25. #include "alc/effects/base.h"
  26. #include "almalloc.h"
  27. #include "alnumbers.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/devformat.h"
  34. #include "core/device.h"
  35. #include "core/effectslot.h"
  36. #include "core/filters/biquad.h"
  37. #include "core/mixer.h"
  38. #include "intrusive_ptr.h"
  39. namespace {
  40. using uint = unsigned int;
  41. #define MAX_UPDATE_SAMPLES 128
  42. #define WAVEFORM_FRACBITS 24
  43. #define WAVEFORM_FRACONE (1<<WAVEFORM_FRACBITS)
  44. #define WAVEFORM_FRACMASK (WAVEFORM_FRACONE-1)
  45. inline float Sin(uint index)
  46. {
  47. constexpr float scale{al::numbers::pi_v<float>*2.0f / WAVEFORM_FRACONE};
  48. return std::sin(static_cast<float>(index) * scale);
  49. }
  50. inline float Saw(uint index)
  51. { return static_cast<float>(index)*(2.0f/WAVEFORM_FRACONE) - 1.0f; }
  52. inline float Square(uint index)
  53. { return static_cast<float>(static_cast<int>((index>>(WAVEFORM_FRACBITS-2))&2) - 1); }
  54. inline float One(uint) { return 1.0f; }
  55. template<float (&func)(uint)>
  56. void Modulate(float *RESTRICT dst, uint index, const uint step, size_t todo)
  57. {
  58. for(size_t i{0u};i < todo;i++)
  59. {
  60. index += step;
  61. index &= WAVEFORM_FRACMASK;
  62. dst[i] = func(index);
  63. }
  64. }
  65. struct ModulatorState final : public EffectState {
  66. void (*mGetSamples)(float*RESTRICT, uint, const uint, size_t){};
  67. uint mIndex{0};
  68. uint mStep{1};
  69. struct {
  70. BiquadFilter Filter;
  71. float CurrentGains[MAX_OUTPUT_CHANNELS]{};
  72. float TargetGains[MAX_OUTPUT_CHANNELS]{};
  73. } mChans[MaxAmbiChannels];
  74. void deviceUpdate(const DeviceBase *device, const Buffer &buffer) override;
  75. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  76. const EffectTarget target) override;
  77. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  78. const al::span<FloatBufferLine> samplesOut) override;
  79. DEF_NEWDEL(ModulatorState)
  80. };
  81. void ModulatorState::deviceUpdate(const DeviceBase*, const Buffer&)
  82. {
  83. for(auto &e : mChans)
  84. {
  85. e.Filter.clear();
  86. std::fill(std::begin(e.CurrentGains), std::end(e.CurrentGains), 0.0f);
  87. }
  88. }
  89. void ModulatorState::update(const ContextBase *context, const EffectSlot *slot,
  90. const EffectProps *props, const EffectTarget target)
  91. {
  92. const DeviceBase *device{context->mDevice};
  93. const float step{props->Modulator.Frequency / static_cast<float>(device->Frequency)};
  94. mStep = fastf2u(clampf(step*WAVEFORM_FRACONE, 0.0f, float{WAVEFORM_FRACONE-1}));
  95. if(mStep == 0)
  96. mGetSamples = Modulate<One>;
  97. else if(props->Modulator.Waveform == ModulatorWaveform::Sinusoid)
  98. mGetSamples = Modulate<Sin>;
  99. else if(props->Modulator.Waveform == ModulatorWaveform::Sawtooth)
  100. mGetSamples = Modulate<Saw>;
  101. else /*if(props->Modulator.Waveform == ModulatorWaveform::Square)*/
  102. mGetSamples = Modulate<Square>;
  103. float f0norm{props->Modulator.HighPassCutoff / static_cast<float>(device->Frequency)};
  104. f0norm = clampf(f0norm, 1.0f/512.0f, 0.49f);
  105. /* Bandwidth value is constant in octaves. */
  106. mChans[0].Filter.setParamsFromBandwidth(BiquadType::HighPass, f0norm, 1.0f, 0.75f);
  107. for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
  108. mChans[i].Filter.copyParamsFrom(mChans[0].Filter);
  109. mOutTarget = target.Main->Buffer;
  110. auto set_gains = [slot,target](auto &chan, al::span<const float,MaxAmbiChannels> coeffs)
  111. { ComputePanGains(target.Main, coeffs.data(), slot->Gain, chan.TargetGains); };
  112. SetAmbiPanIdentity(std::begin(mChans), slot->Wet.Buffer.size(), set_gains);
  113. }
  114. void ModulatorState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  115. {
  116. for(size_t base{0u};base < samplesToDo;)
  117. {
  118. alignas(16) float modsamples[MAX_UPDATE_SAMPLES];
  119. const size_t td{minz(MAX_UPDATE_SAMPLES, samplesToDo-base)};
  120. mGetSamples(modsamples, mIndex, mStep, td);
  121. mIndex += static_cast<uint>(mStep * td);
  122. mIndex &= WAVEFORM_FRACMASK;
  123. auto chandata = std::begin(mChans);
  124. for(const auto &input : samplesIn)
  125. {
  126. alignas(16) float temps[MAX_UPDATE_SAMPLES];
  127. chandata->Filter.process({&input[base], td}, temps);
  128. for(size_t i{0u};i < td;i++)
  129. temps[i] *= modsamples[i];
  130. MixSamples({temps, td}, samplesOut, chandata->CurrentGains, chandata->TargetGains,
  131. samplesToDo-base, base);
  132. ++chandata;
  133. }
  134. base += td;
  135. }
  136. }
  137. struct ModulatorStateFactory final : public EffectStateFactory {
  138. al::intrusive_ptr<EffectState> create() override
  139. { return al::intrusive_ptr<EffectState>{new ModulatorState{}}; }
  140. };
  141. } // namespace
  142. EffectStateFactory *ModulatorStateFactory_getFactory()
  143. {
  144. static ModulatorStateFactory ModulatorFactory{};
  145. return &ModulatorFactory;
  146. }