fshifter.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 "al/auxeffectslot.h"
  27. #include "alcmain.h"
  28. #include "alcontext.h"
  29. #include "alu.h"
  30. #include "alcomplex.h"
  31. namespace {
  32. using complex_d = std::complex<double>;
  33. #define HIL_SIZE 1024
  34. #define OVERSAMP (1<<2)
  35. #define HIL_STEP (HIL_SIZE / OVERSAMP)
  36. #define FIFO_LATENCY (HIL_STEP * (OVERSAMP-1))
  37. /* Define a Hann window, used to filter the HIL input and output. */
  38. /* Making this constexpr seems to require C++14. */
  39. std::array<ALdouble,HIL_SIZE> InitHannWindow()
  40. {
  41. std::array<ALdouble,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-1}};
  46. const double val{std::sin(static_cast<double>(i) * scale)};
  47. ret[i] = ret[HIL_SIZE-1-i] = val * val;
  48. }
  49. return ret;
  50. }
  51. alignas(16) const std::array<ALdouble,HIL_SIZE> HannWindow = InitHannWindow();
  52. struct FshifterState final : public EffectState {
  53. /* Effect parameters */
  54. size_t mCount{};
  55. ALsizei mPhaseStep[2]{};
  56. ALsizei mPhase[2]{};
  57. ALdouble mSign[2]{};
  58. /*Effects buffers*/
  59. ALfloat mInFIFO[HIL_SIZE]{};
  60. complex_d mOutFIFO[HIL_SIZE]{};
  61. complex_d mOutputAccum[HIL_SIZE]{};
  62. complex_d mAnalytic[HIL_SIZE]{};
  63. complex_d mOutdata[BUFFERSIZE]{};
  64. alignas(16) ALfloat mBufferOut[BUFFERSIZE]{};
  65. /* Effect gains for each output channel */
  66. struct {
  67. ALfloat Current[MAX_OUTPUT_CHANNELS]{};
  68. ALfloat Target[MAX_OUTPUT_CHANNELS]{};
  69. } mGains[2];
  70. ALboolean deviceUpdate(const ALCdevice *device) override;
  71. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  72. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
  73. DEF_NEWDEL(FshifterState)
  74. };
  75. ALboolean FshifterState::deviceUpdate(const ALCdevice*)
  76. {
  77. /* (Re-)initializing parameters and clear the buffers. */
  78. mCount = FIFO_LATENCY;
  79. std::fill(std::begin(mPhaseStep), std::end(mPhaseStep), 0);
  80. std::fill(std::begin(mPhase), std::end(mPhase), 0);
  81. std::fill(std::begin(mSign), std::end(mSign), 1.0);
  82. std::fill(std::begin(mInFIFO), std::end(mInFIFO), 0.0f);
  83. std::fill(std::begin(mOutFIFO), std::end(mOutFIFO), complex_d{});
  84. std::fill(std::begin(mOutputAccum), std::end(mOutputAccum), complex_d{});
  85. std::fill(std::begin(mAnalytic), std::end(mAnalytic), complex_d{});
  86. for(auto &gain : mGains)
  87. {
  88. std::fill(std::begin(gain.Current), std::end(gain.Current), 0.0f);
  89. std::fill(std::begin(gain.Target), std::end(gain.Target), 0.0f);
  90. }
  91. return AL_TRUE;
  92. }
  93. void FshifterState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  94. {
  95. const ALCdevice *device{context->mDevice.get()};
  96. ALfloat step{props->Fshifter.Frequency / static_cast<ALfloat>(device->Frequency)};
  97. mPhaseStep[0] = mPhaseStep[1] = fastf2i(minf(step, 0.5f) * FRACTIONONE);
  98. switch(props->Fshifter.LeftDirection)
  99. {
  100. case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN:
  101. mSign[0] = -1.0;
  102. break;
  103. case AL_FREQUENCY_SHIFTER_DIRECTION_UP:
  104. mSign[0] = 1.0;
  105. break;
  106. case AL_FREQUENCY_SHIFTER_DIRECTION_OFF:
  107. mPhase[0] = 0;
  108. mPhaseStep[0] = 0;
  109. break;
  110. }
  111. switch (props->Fshifter.RightDirection)
  112. {
  113. case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN:
  114. mSign[1] = -1.0;
  115. break;
  116. case AL_FREQUENCY_SHIFTER_DIRECTION_UP:
  117. mSign[1] = 1.0;
  118. break;
  119. case AL_FREQUENCY_SHIFTER_DIRECTION_OFF:
  120. mPhase[1] = 0;
  121. mPhaseStep[1] = 0;
  122. break;
  123. }
  124. ALfloat coeffs[2][MAX_AMBI_CHANNELS];
  125. CalcDirectionCoeffs({-1.0f, 0.0f, -1.0f}, 0.0f, coeffs[0]);
  126. CalcDirectionCoeffs({ 1.0f, 0.0f, -1.0f}, 0.0f, coeffs[1]);
  127. mOutTarget = target.Main->Buffer;
  128. ComputePanGains(target.Main, coeffs[0], slot->Params.Gain, mGains[0].Target);
  129. ComputePanGains(target.Main, coeffs[1], slot->Params.Gain, mGains[1].Target);
  130. }
  131. void FshifterState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  132. {
  133. static constexpr complex_d complex_zero{0.0, 0.0};
  134. ALfloat *RESTRICT BufferOut = mBufferOut;
  135. size_t j, k;
  136. for(size_t base{0u};base < samplesToDo;)
  137. {
  138. const size_t todo{minz(HIL_SIZE-mCount, samplesToDo-base)};
  139. ASSUME(todo > 0);
  140. /* Fill FIFO buffer with samples data */
  141. k = mCount;
  142. for(j = 0;j < todo;j++,k++)
  143. {
  144. mInFIFO[k] = samplesIn[0][base+j];
  145. mOutdata[base+j] = mOutFIFO[k-FIFO_LATENCY];
  146. }
  147. mCount += todo;
  148. base += todo;
  149. /* Check whether FIFO buffer is filled */
  150. if(mCount < HIL_SIZE) continue;
  151. mCount = FIFO_LATENCY;
  152. /* Real signal windowing and store in Analytic buffer */
  153. for(k = 0;k < HIL_SIZE;k++)
  154. {
  155. mAnalytic[k].real(mInFIFO[k] * HannWindow[k]);
  156. mAnalytic[k].imag(0.0);
  157. }
  158. /* Processing signal by Discrete Hilbert Transform (analytical signal). */
  159. complex_hilbert(mAnalytic);
  160. /* Windowing and add to output accumulator */
  161. for(k = 0;k < HIL_SIZE;k++)
  162. mOutputAccum[k] += 2.0/OVERSAMP*HannWindow[k]*mAnalytic[k];
  163. /* Shift accumulator, input & output FIFO */
  164. for(k = 0;k < HIL_STEP;k++) mOutFIFO[k] = mOutputAccum[k];
  165. for(j = 0;k < HIL_SIZE;k++,j++) mOutputAccum[j] = mOutputAccum[k];
  166. for(;j < HIL_SIZE;j++) mOutputAccum[j] = complex_zero;
  167. for(k = 0;k < FIFO_LATENCY;k++)
  168. mInFIFO[k] = mInFIFO[k+HIL_STEP];
  169. }
  170. /* Process frequency shifter using the analytic signal obtained. */
  171. for(ALsizei c{0};c < 2;++c)
  172. {
  173. for(k = 0;k < samplesToDo;++k)
  174. {
  175. double phase = mPhase[c] * ((1.0 / FRACTIONONE) * al::MathDefs<double>::Tau());
  176. BufferOut[k] = static_cast<float>(mOutdata[k].real()*std::cos(phase) +
  177. mOutdata[k].imag()*std::sin(phase)*mSign[c]);
  178. mPhase[c] += mPhaseStep[c];
  179. mPhase[c] &= FRACTIONMASK;
  180. }
  181. /* Now, mix the processed sound data to the output. */
  182. MixSamples({BufferOut, samplesToDo}, samplesOut, mGains[c].Current, mGains[c].Target,
  183. maxz(samplesToDo, 512), 0);
  184. }
  185. }
  186. void Fshifter_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  187. {
  188. switch(param)
  189. {
  190. case AL_FREQUENCY_SHIFTER_FREQUENCY:
  191. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_FREQUENCY && val <= AL_FREQUENCY_SHIFTER_MAX_FREQUENCY))
  192. SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter frequency out of range");
  193. props->Fshifter.Frequency = val;
  194. break;
  195. default:
  196. context->setError(AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x",
  197. param);
  198. }
  199. }
  200. void Fshifter_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  201. { Fshifter_setParamf(props, context, param, vals[0]); }
  202. void Fshifter_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
  203. {
  204. switch(param)
  205. {
  206. case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
  207. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_LEFT_DIRECTION && val <= AL_FREQUENCY_SHIFTER_MAX_LEFT_DIRECTION))
  208. SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter left direction out of range");
  209. props->Fshifter.LeftDirection = val;
  210. break;
  211. case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
  212. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_RIGHT_DIRECTION && val <= AL_FREQUENCY_SHIFTER_MAX_RIGHT_DIRECTION))
  213. SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter right direction out of range");
  214. props->Fshifter.RightDirection = val;
  215. break;
  216. default:
  217. context->setError(AL_INVALID_ENUM, "Invalid frequency shifter integer property 0x%04x",
  218. param);
  219. }
  220. }
  221. void Fshifter_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
  222. { Fshifter_setParami(props, context, param, vals[0]); }
  223. void Fshifter_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
  224. {
  225. switch(param)
  226. {
  227. case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
  228. *val = props->Fshifter.LeftDirection;
  229. break;
  230. case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
  231. *val = props->Fshifter.RightDirection;
  232. break;
  233. default:
  234. context->setError(AL_INVALID_ENUM, "Invalid frequency shifter integer property 0x%04x",
  235. param);
  236. }
  237. }
  238. void Fshifter_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
  239. { Fshifter_getParami(props, context, param, vals); }
  240. void Fshifter_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  241. {
  242. switch(param)
  243. {
  244. case AL_FREQUENCY_SHIFTER_FREQUENCY:
  245. *val = props->Fshifter.Frequency;
  246. break;
  247. default:
  248. context->setError(AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x",
  249. param);
  250. }
  251. }
  252. void Fshifter_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  253. { Fshifter_getParamf(props, context, param, vals); }
  254. DEFINE_ALEFFECT_VTABLE(Fshifter);
  255. struct FshifterStateFactory final : public EffectStateFactory {
  256. EffectState *create() override { return new FshifterState{}; }
  257. EffectProps getDefaultProps() const noexcept override;
  258. const EffectVtable *getEffectVtable() const noexcept override { return &Fshifter_vtable; }
  259. };
  260. EffectProps FshifterStateFactory::getDefaultProps() const noexcept
  261. {
  262. EffectProps props{};
  263. props.Fshifter.Frequency = AL_FREQUENCY_SHIFTER_DEFAULT_FREQUENCY;
  264. props.Fshifter.LeftDirection = AL_FREQUENCY_SHIFTER_DEFAULT_LEFT_DIRECTION;
  265. props.Fshifter.RightDirection = AL_FREQUENCY_SHIFTER_DEFAULT_RIGHT_DIRECTION;
  266. return props;
  267. }
  268. } // namespace
  269. EffectStateFactory *FshifterStateFactory_getFactory()
  270. {
  271. static FshifterStateFactory FshifterFactory{};
  272. return &FshifterFactory;
  273. }