fshifter.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 <algorithm>
  22. #include <array>
  23. #include <cmath>
  24. #include <complex>
  25. #include <cstdlib>
  26. #include <variant>
  27. #include "alc/effects/base.h"
  28. #include "alcomplex.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/mixer.h"
  39. #include "core/mixer/defs.h"
  40. #include "intrusive_ptr.h"
  41. #include "opthelpers.h"
  42. struct BufferStorage;
  43. namespace {
  44. using uint = unsigned int;
  45. using complex_d = std::complex<double>;
  46. constexpr size_t HilSize{1024};
  47. constexpr size_t HilHalfSize{HilSize >> 1};
  48. constexpr size_t OversampleFactor{4};
  49. static_assert(HilSize%OversampleFactor == 0, "Factor must be a clean divisor of the size");
  50. constexpr size_t HilStep{HilSize / OversampleFactor};
  51. /* Define a Hann window, used to filter the HIL input and output. */
  52. struct Windower {
  53. alignas(16) std::array<double,HilSize> mData{};
  54. Windower()
  55. {
  56. /* Create lookup table of the Hann window for the desired size. */
  57. for(size_t i{0};i < HilHalfSize;i++)
  58. {
  59. constexpr double scale{al::numbers::pi / double{HilSize}};
  60. const double val{std::sin((static_cast<double>(i)+0.5) * scale)};
  61. mData[i] = mData[HilSize-1-i] = val * val;
  62. }
  63. }
  64. };
  65. const Windower gWindow{};
  66. struct FshifterState final : public EffectState {
  67. /* Effect parameters */
  68. size_t mCount{};
  69. size_t mPos{};
  70. std::array<uint,2> mPhaseStep{};
  71. std::array<uint,2> mPhase{};
  72. std::array<double,2> mSign{};
  73. /* Effects buffers */
  74. std::array<double,HilSize> mInFIFO{};
  75. std::array<complex_d,HilStep> mOutFIFO{};
  76. std::array<complex_d,HilSize> mOutputAccum{};
  77. std::array<complex_d,HilSize> mAnalytic{};
  78. std::array<complex_d,BufferLineSize> mOutdata{};
  79. alignas(16) FloatBufferLine mBufferOut{};
  80. /* Effect gains for each output channel */
  81. struct OutGains {
  82. std::array<float,MaxAmbiChannels> Current{};
  83. std::array<float,MaxAmbiChannels> Target{};
  84. };
  85. std::array<OutGains,2> mGains;
  86. void deviceUpdate(const DeviceBase *device, const BufferStorage *buffer) override;
  87. void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
  88. const EffectTarget target) override;
  89. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  90. const al::span<FloatBufferLine> samplesOut) override;
  91. };
  92. void FshifterState::deviceUpdate(const DeviceBase*, const BufferStorage*)
  93. {
  94. /* (Re-)initializing parameters and clear the buffers. */
  95. mCount = 0;
  96. mPos = HilSize - HilStep;
  97. mPhaseStep.fill(0u);
  98. mPhase.fill(0u);
  99. mSign.fill(1.0);
  100. mInFIFO.fill(0.0);
  101. mOutFIFO.fill(complex_d{});
  102. mOutputAccum.fill(complex_d{});
  103. mAnalytic.fill(complex_d{});
  104. for(auto &gain : mGains)
  105. {
  106. gain.Current.fill(0.0f);
  107. gain.Target.fill(0.0f);
  108. }
  109. }
  110. void FshifterState::update(const ContextBase *context, const EffectSlot *slot,
  111. const EffectProps *props_, const EffectTarget target)
  112. {
  113. auto &props = std::get<FshifterProps>(*props_);
  114. const DeviceBase *device{context->mDevice};
  115. const float step{props.Frequency / static_cast<float>(device->Frequency)};
  116. mPhaseStep[0] = mPhaseStep[1] = fastf2u(std::min(step, 1.0f) * MixerFracOne);
  117. switch(props.LeftDirection)
  118. {
  119. case FShifterDirection::Down:
  120. mSign[0] = -1.0;
  121. break;
  122. case FShifterDirection::Up:
  123. mSign[0] = 1.0;
  124. break;
  125. case FShifterDirection::Off:
  126. mPhase[0] = 0;
  127. mPhaseStep[0] = 0;
  128. break;
  129. }
  130. switch(props.RightDirection)
  131. {
  132. case FShifterDirection::Down:
  133. mSign[1] = -1.0;
  134. break;
  135. case FShifterDirection::Up:
  136. mSign[1] = 1.0;
  137. break;
  138. case FShifterDirection::Off:
  139. mPhase[1] = 0;
  140. mPhaseStep[1] = 0;
  141. break;
  142. }
  143. static constexpr auto inv_sqrt2 = static_cast<float>(1.0 / al::numbers::sqrt2);
  144. static constexpr auto lcoeffs_pw = CalcDirectionCoeffs(std::array{-1.0f, 0.0f, 0.0f});
  145. static constexpr auto rcoeffs_pw = CalcDirectionCoeffs(std::array{ 1.0f, 0.0f, 0.0f});
  146. static constexpr auto lcoeffs_nrml = CalcDirectionCoeffs(std::array{-inv_sqrt2, 0.0f, inv_sqrt2});
  147. static constexpr auto rcoeffs_nrml = CalcDirectionCoeffs(std::array{ inv_sqrt2, 0.0f, inv_sqrt2});
  148. auto &lcoeffs = (device->mRenderMode != RenderMode::Pairwise) ? lcoeffs_nrml : lcoeffs_pw;
  149. auto &rcoeffs = (device->mRenderMode != RenderMode::Pairwise) ? rcoeffs_nrml : rcoeffs_pw;
  150. mOutTarget = target.Main->Buffer;
  151. ComputePanGains(target.Main, lcoeffs, slot->Gain, mGains[0].Target);
  152. ComputePanGains(target.Main, rcoeffs, slot->Gain, mGains[1].Target);
  153. }
  154. void FshifterState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  155. {
  156. for(size_t base{0u};base < samplesToDo;)
  157. {
  158. size_t todo{std::min(HilStep-mCount, samplesToDo-base)};
  159. /* Fill FIFO buffer with samples data */
  160. const size_t pos{mPos};
  161. size_t count{mCount};
  162. do {
  163. mInFIFO[pos+count] = samplesIn[0][base];
  164. mOutdata[base] = mOutFIFO[count];
  165. ++base; ++count;
  166. } while(--todo);
  167. mCount = count;
  168. /* Check whether FIFO buffer is filled */
  169. if(mCount < HilStep) break;
  170. mCount = 0;
  171. mPos = (mPos+HilStep) & (HilSize-1);
  172. /* Real signal windowing and store in Analytic buffer */
  173. for(size_t src{mPos}, k{0u};src < HilSize;++src,++k)
  174. mAnalytic[k] = mInFIFO[src]*gWindow.mData[k];
  175. for(size_t src{0u}, k{HilSize-mPos};src < mPos;++src,++k)
  176. mAnalytic[k] = mInFIFO[src]*gWindow.mData[k];
  177. /* Processing signal by Discrete Hilbert Transform (analytical signal). */
  178. complex_hilbert(mAnalytic);
  179. /* Windowing and add to output accumulator */
  180. for(size_t dst{mPos}, k{0u};dst < HilSize;++dst,++k)
  181. mOutputAccum[dst] += 2.0/OversampleFactor*gWindow.mData[k]*mAnalytic[k];
  182. for(size_t dst{0u}, k{HilSize-mPos};dst < mPos;++dst,++k)
  183. mOutputAccum[dst] += 2.0/OversampleFactor*gWindow.mData[k]*mAnalytic[k];
  184. /* Copy out the accumulated result, then clear for the next iteration. */
  185. std::copy_n(mOutputAccum.cbegin() + mPos, HilStep, mOutFIFO.begin());
  186. std::fill_n(mOutputAccum.begin() + mPos, HilStep, complex_d{});
  187. }
  188. /* Process frequency shifter using the analytic signal obtained. */
  189. for(size_t c{0};c < 2;++c)
  190. {
  191. const double sign{mSign[c]};
  192. const uint phase_step{mPhaseStep[c]};
  193. uint phase_idx{mPhase[c]};
  194. std::transform(mOutdata.cbegin(), mOutdata.cbegin()+samplesToDo, mBufferOut.begin(),
  195. [&phase_idx,phase_step,sign](const complex_d &in) -> float
  196. {
  197. const double phase{phase_idx * (al::numbers::pi*2.0 / MixerFracOne)};
  198. const auto out = static_cast<float>(in.real()*std::cos(phase) +
  199. in.imag()*std::sin(phase)*sign);
  200. phase_idx += phase_step;
  201. phase_idx &= MixerFracMask;
  202. return out;
  203. });
  204. mPhase[c] = phase_idx;
  205. /* Now, mix the processed sound data to the output. */
  206. MixSamples(al::span{mBufferOut}.first(samplesToDo), samplesOut, mGains[c].Current,
  207. mGains[c].Target, std::max(samplesToDo, 512_uz), 0);
  208. }
  209. }
  210. struct FshifterStateFactory final : public EffectStateFactory {
  211. al::intrusive_ptr<EffectState> create() override
  212. { return al::intrusive_ptr<EffectState>{new FshifterState{}}; }
  213. };
  214. } // namespace
  215. EffectStateFactory *FshifterStateFactory_getFactory()
  216. {
  217. static FshifterStateFactory FshifterFactory{};
  218. return &FshifterFactory;
  219. }