modulator.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 <cstdint>
  25. #include <cstdlib>
  26. #include <functional>
  27. #include <variant>
  28. #include "alc/effects/base.h"
  29. #include "alnumbers.h"
  30. #include "alnumeric.h"
  31. #include "alspan.h"
  32. #include "core/ambidefs.h"
  33. #include "core/bufferline.h"
  34. #include "core/context.h"
  35. #include "core/device.h"
  36. #include "core/effects/base.h"
  37. #include "core/effectslot.h"
  38. #include "core/filters/biquad.h"
  39. #include "core/mixer.h"
  40. #include "intrusive_ptr.h"
  41. #include "opthelpers.h"
  42. struct BufferStorage;
  43. namespace {
  44. using uint = unsigned int;
  45. struct SinFunc {
  46. static auto Get(uint index, float scale) noexcept(noexcept(std::sin(0.0f))) -> float
  47. { return std::sin(static_cast<float>(index) * scale); }
  48. };
  49. struct SawFunc {
  50. static constexpr auto Get(uint index, float scale) noexcept -> float
  51. { return static_cast<float>(index)*scale - 1.0f; }
  52. };
  53. struct SquareFunc {
  54. static constexpr auto Get(uint index, float scale) noexcept -> float
  55. { return float(static_cast<float>(index)*scale < 0.5f)*2.0f - 1.0f; }
  56. };
  57. struct OneFunc {
  58. static constexpr auto Get(uint, float) noexcept -> float
  59. { return 1.0f; }
  60. };
  61. struct ModulatorState final : public EffectState {
  62. std::variant<OneFunc,SinFunc,SawFunc,SquareFunc> mSampleGen;
  63. uint mIndex{0};
  64. uint mRange{1};
  65. float mIndexScale{0.0f};
  66. alignas(16) FloatBufferLine mModSamples{};
  67. alignas(16) FloatBufferLine mBuffer{};
  68. struct OutParams {
  69. uint mTargetChannel{InvalidChannelIndex};
  70. BiquadFilter mFilter;
  71. float mCurrentGain{};
  72. float mTargetGain{};
  73. };
  74. std::array<OutParams,MaxAmbiChannels> mChans;
  75. void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
  76. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  77. const EffectTarget target) override;
  78. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  79. const al::span<FloatBufferLine> samplesOut) override;
  80. };
  81. void ModulatorState::deviceUpdate(const DeviceBase*, const BufferStorage*)
  82. {
  83. for(auto &e : mChans)
  84. {
  85. e.mTargetChannel = InvalidChannelIndex;
  86. e.mFilter.clear();
  87. e.mCurrentGain = 0.0f;
  88. }
  89. }
  90. void ModulatorState::update(const ContextBase *context, const EffectSlot *slot,
  91. const EffectProps *props_, const EffectTarget target)
  92. {
  93. auto &props = std::get<ModulatorProps>(*props_);
  94. const DeviceBase *device{context->mDevice};
  95. /* The effective frequency will be adjusted to have a whole number of
  96. * samples per cycle (at 48khz, that allows 8000, 6857.14, 6000, 5333.33,
  97. * 4800, etc). We could do better by using fixed-point stepping over a sin
  98. * function, with additive synthesis for the square and sawtooth waveforms,
  99. * but that may need a more efficient sin function since it needs to do
  100. * many iterations per sample.
  101. */
  102. const float samplesPerCycle{props.Frequency > 0.0f
  103. ? static_cast<float>(device->Frequency)/props.Frequency + 0.5f
  104. : 1.0f};
  105. const uint range{static_cast<uint>(std::clamp(samplesPerCycle, 1.0f,
  106. static_cast<float>(device->Frequency)))};
  107. mIndex = static_cast<uint>(uint64_t{mIndex} * range / mRange);
  108. mRange = range;
  109. if(mRange == 1)
  110. {
  111. mIndexScale = 0.0f;
  112. mSampleGen.emplace<OneFunc>();
  113. }
  114. else if(props.Waveform == ModulatorWaveform::Sinusoid)
  115. {
  116. mIndexScale = al::numbers::pi_v<float>*2.0f / static_cast<float>(mRange);
  117. mSampleGen.emplace<SinFunc>();
  118. }
  119. else if(props.Waveform == ModulatorWaveform::Sawtooth)
  120. {
  121. mIndexScale = 2.0f / static_cast<float>(mRange-1);
  122. mSampleGen.emplace<SawFunc>();
  123. }
  124. else if(props.Waveform == ModulatorWaveform::Square)
  125. {
  126. /* For square wave, the range should be even (there should be an equal
  127. * number of high and low samples). An odd number of samples per cycle
  128. * would need a more complex value generator.
  129. */
  130. mRange = (mRange+1) & ~1u;
  131. mIndexScale = 1.0f / static_cast<float>(mRange-1);
  132. mSampleGen.emplace<SquareFunc>();
  133. }
  134. float f0norm{props.HighPassCutoff / static_cast<float>(device->Frequency)};
  135. f0norm = std::clamp(f0norm, 1.0f/512.0f, 0.49f);
  136. /* Bandwidth value is constant in octaves. */
  137. mChans[0].mFilter.setParamsFromBandwidth(BiquadType::HighPass, f0norm, 1.0f, 0.75f);
  138. for(size_t i{1u};i < slot->Wet.Buffer.size();++i)
  139. mChans[i].mFilter.copyParamsFrom(mChans[0].mFilter);
  140. mOutTarget = target.Main->Buffer;
  141. auto set_channel = [this](size_t idx, uint outchan, float outgain)
  142. {
  143. mChans[idx].mTargetChannel = outchan;
  144. mChans[idx].mTargetGain = outgain;
  145. };
  146. target.Main->setAmbiMixParams(slot->Wet, slot->Gain, set_channel);
  147. }
  148. void ModulatorState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  149. {
  150. ASSUME(samplesToDo > 0);
  151. std::visit([this,samplesToDo](auto&& type)
  152. {
  153. const uint range{mRange};
  154. const float scale{mIndexScale};
  155. uint index{mIndex};
  156. ASSUME(range > 1);
  157. for(size_t i{0};i < samplesToDo;)
  158. {
  159. size_t rem{std::min(samplesToDo-i, size_t{range-index})};
  160. do {
  161. mModSamples[i++] = type.Get(index++, scale);
  162. } while(--rem);
  163. if(index == range)
  164. index = 0;
  165. }
  166. mIndex = index;
  167. }, mSampleGen);
  168. auto chandata = mChans.begin();
  169. for(const auto &input : samplesIn)
  170. {
  171. if(const size_t outidx{chandata->mTargetChannel}; outidx != InvalidChannelIndex)
  172. {
  173. chandata->mFilter.process(al::span{input}.first(samplesToDo), mBuffer);
  174. std::transform(mBuffer.cbegin(), mBuffer.cbegin()+samplesToDo, mModSamples.cbegin(),
  175. mBuffer.begin(), std::multiplies<>{});
  176. MixSamples(al::span{mBuffer}.first(samplesToDo), samplesOut[outidx],
  177. chandata->mCurrentGain, chandata->mTargetGain, std::min(samplesToDo, 64_uz));
  178. }
  179. ++chandata;
  180. }
  181. }
  182. struct ModulatorStateFactory final : public EffectStateFactory {
  183. al::intrusive_ptr<EffectState> create() override
  184. { return al::intrusive_ptr<EffectState>{new ModulatorState{}}; }
  185. };
  186. } // namespace
  187. EffectStateFactory *ModulatorStateFactory_getFactory()
  188. {
  189. static ModulatorStateFactory ModulatorFactory{};
  190. return &ModulatorFactory;
  191. }