fshifter.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2018 by Raul Herraiz.
  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 <array>
  24. #include <complex>
  25. #include <algorithm>
  26. #include "alcmain.h"
  27. #include "alcomplex.h"
  28. #include "alcontext.h"
  29. #include "alu.h"
  30. #include "effectslot.h"
  31. #include "math_defs.h"
  32. namespace {
  33. using complex_d = std::complex<double>;
  34. #define HIL_SIZE 1024
  35. #define OVERSAMP (1<<2)
  36. #define HIL_STEP (HIL_SIZE / OVERSAMP)
  37. #define FIFO_LATENCY (HIL_STEP * (OVERSAMP-1))
  38. /* Define a Hann window, used to filter the HIL input and output. */
  39. std::array<double,HIL_SIZE> InitHannWindow()
  40. {
  41. std::array<double,HIL_SIZE> ret;
  42. /* Create lookup table of the Hann window for the desired size, i.e. HIL_SIZE */
  43. for(size_t i{0};i < HIL_SIZE>>1;i++)
  44. {
  45. constexpr double scale{al::MathDefs<double>::Pi() / double{HIL_SIZE}};
  46. const double val{std::sin(static_cast<double>(i+1) * scale)};
  47. ret[i] = ret[HIL_SIZE-1-i] = val * val;
  48. }
  49. return ret;
  50. }
  51. alignas(16) const std::array<double,HIL_SIZE> HannWindow = InitHannWindow();
  52. struct FshifterState final : public EffectState {
  53. /* Effect parameters */
  54. size_t mCount{};
  55. uint mPhaseStep[2]{};
  56. uint mPhase[2]{};
  57. double mSign[2]{};
  58. /* Effects buffers */
  59. double mInFIFO[HIL_SIZE]{};
  60. complex_d mOutFIFO[HIL_STEP]{};
  61. complex_d mOutputAccum[HIL_SIZE]{};
  62. complex_d mAnalytic[HIL_SIZE]{};
  63. complex_d mOutdata[BufferLineSize]{};
  64. alignas(16) float mBufferOut[BufferLineSize]{};
  65. /* Effect gains for each output channel */
  66. struct {
  67. float Current[MAX_OUTPUT_CHANNELS]{};
  68. float Target[MAX_OUTPUT_CHANNELS]{};
  69. } mGains[2];
  70. void deviceUpdate(const ALCdevice *device, const Buffer &buffer) override;
  71. void update(const ALCcontext *context, const EffectSlot *slot, const EffectProps *props,
  72. const EffectTarget target) override;
  73. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  74. const al::span<FloatBufferLine> samplesOut) override;
  75. DEF_NEWDEL(FshifterState)
  76. };
  77. void FshifterState::deviceUpdate(const ALCdevice*, const Buffer&)
  78. {
  79. /* (Re-)initializing parameters and clear the buffers. */
  80. mCount = FIFO_LATENCY;
  81. std::fill(std::begin(mPhaseStep), std::end(mPhaseStep), 0u);
  82. std::fill(std::begin(mPhase), std::end(mPhase), 0u);
  83. std::fill(std::begin(mSign), std::end(mSign), 1.0);
  84. std::fill(std::begin(mInFIFO), std::end(mInFIFO), 0.0);
  85. std::fill(std::begin(mOutFIFO), std::end(mOutFIFO), complex_d{});
  86. std::fill(std::begin(mOutputAccum), std::end(mOutputAccum), complex_d{});
  87. std::fill(std::begin(mAnalytic), std::end(mAnalytic), complex_d{});
  88. for(auto &gain : mGains)
  89. {
  90. std::fill(std::begin(gain.Current), std::end(gain.Current), 0.0f);
  91. std::fill(std::begin(gain.Target), std::end(gain.Target), 0.0f);
  92. }
  93. }
  94. void FshifterState::update(const ALCcontext *context, const EffectSlot *slot,
  95. const EffectProps *props, const EffectTarget target)
  96. {
  97. const ALCdevice *device{context->mDevice.get()};
  98. const float step{props->Fshifter.Frequency / static_cast<float>(device->Frequency)};
  99. mPhaseStep[0] = mPhaseStep[1] = fastf2u(minf(step, 1.0f) * MixerFracOne);
  100. switch(props->Fshifter.LeftDirection)
  101. {
  102. case FShifterDirection::Down:
  103. mSign[0] = -1.0;
  104. break;
  105. case FShifterDirection::Up:
  106. mSign[0] = 1.0;
  107. break;
  108. case FShifterDirection::Off:
  109. mPhase[0] = 0;
  110. mPhaseStep[0] = 0;
  111. break;
  112. }
  113. switch(props->Fshifter.RightDirection)
  114. {
  115. case FShifterDirection::Down:
  116. mSign[1] = -1.0;
  117. break;
  118. case FShifterDirection::Up:
  119. mSign[1] = 1.0;
  120. break;
  121. case FShifterDirection::Off:
  122. mPhase[1] = 0;
  123. mPhaseStep[1] = 0;
  124. break;
  125. }
  126. const auto lcoeffs = CalcDirectionCoeffs({-1.0f, 0.0f, 0.0f}, 0.0f);
  127. const auto rcoeffs = CalcDirectionCoeffs({ 1.0f, 0.0f, 0.0f}, 0.0f);
  128. mOutTarget = target.Main->Buffer;
  129. ComputePanGains(target.Main, lcoeffs.data(), slot->Gain, mGains[0].Target);
  130. ComputePanGains(target.Main, rcoeffs.data(), slot->Gain, mGains[1].Target);
  131. }
  132. void FshifterState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  133. {
  134. for(size_t base{0u};base < samplesToDo;)
  135. {
  136. size_t todo{minz(HIL_SIZE-mCount, samplesToDo-base)};
  137. /* Fill FIFO buffer with samples data */
  138. size_t count{mCount};
  139. do {
  140. mInFIFO[count] = samplesIn[0][base];
  141. mOutdata[base] = mOutFIFO[count-FIFO_LATENCY];
  142. ++base; ++count;
  143. } while(--todo);
  144. mCount = count;
  145. /* Check whether FIFO buffer is filled */
  146. if(mCount < HIL_SIZE) break;
  147. mCount = FIFO_LATENCY;
  148. /* Real signal windowing and store in Analytic buffer */
  149. for(size_t k{0};k < HIL_SIZE;k++)
  150. mAnalytic[k] = mInFIFO[k]*HannWindow[k];
  151. /* Processing signal by Discrete Hilbert Transform (analytical signal). */
  152. complex_hilbert(mAnalytic);
  153. /* Windowing and add to output accumulator */
  154. for(size_t k{0};k < HIL_SIZE;k++)
  155. mOutputAccum[k] += 2.0/OVERSAMP*HannWindow[k]*mAnalytic[k];
  156. /* Shift accumulator, input & output FIFO */
  157. std::copy_n(mOutputAccum, HIL_STEP, mOutFIFO);
  158. auto accum_iter = std::copy(std::begin(mOutputAccum)+HIL_STEP, std::end(mOutputAccum),
  159. std::begin(mOutputAccum));
  160. std::fill(accum_iter, std::end(mOutputAccum), complex_d{});
  161. std::copy(std::begin(mInFIFO)+HIL_STEP, std::end(mInFIFO), std::begin(mInFIFO));
  162. }
  163. /* Process frequency shifter using the analytic signal obtained. */
  164. float *RESTRICT BufferOut{mBufferOut};
  165. for(int c{0};c < 2;++c)
  166. {
  167. const uint phase_step{mPhaseStep[c]};
  168. uint phase_idx{mPhase[c]};
  169. for(size_t k{0};k < samplesToDo;++k)
  170. {
  171. const double phase{phase_idx * ((1.0/MixerFracOne) * al::MathDefs<double>::Tau())};
  172. BufferOut[k] = static_cast<float>(mOutdata[k].real()*std::cos(phase) +
  173. mOutdata[k].imag()*std::sin(phase)*mSign[c]);
  174. phase_idx += phase_step;
  175. phase_idx &= MixerFracMask;
  176. }
  177. mPhase[c] = phase_idx;
  178. /* Now, mix the processed sound data to the output. */
  179. MixSamples({BufferOut, samplesToDo}, samplesOut, mGains[c].Current, mGains[c].Target,
  180. maxz(samplesToDo, 512), 0);
  181. }
  182. }
  183. struct FshifterStateFactory final : public EffectStateFactory {
  184. al::intrusive_ptr<EffectState> create() override
  185. { return al::intrusive_ptr<EffectState>{new FshifterState{}}; }
  186. };
  187. } // namespace
  188. EffectStateFactory *FshifterStateFactory_getFactory()
  189. {
  190. static FshifterStateFactory FshifterFactory{};
  191. return &FshifterFactory;
  192. }