modulator.cpp 5.5 KB

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