🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

302 lines
10 KiB

  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 "alMain.h"
  27. #include "alcontext.h"
  28. #include "alAuxEffectSlot.h"
  29. #include "alError.h"
  30. #include "alu.h"
  31. #include "alcomplex.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. /* Making this constexpr seems to require C++14. */
  40. std::array<ALdouble,HIL_SIZE> InitHannWindow()
  41. {
  42. std::array<ALdouble,HIL_SIZE> ret;
  43. /* Create lookup table of the Hann window for the desired size, i.e. HIL_SIZE */
  44. for(ALsizei i{0};i < HIL_SIZE>>1;i++)
  45. {
  46. ALdouble val = std::sin(al::MathDefs<double>::Pi() * i / ALdouble{HIL_SIZE-1});
  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. ALsizei mCount{};
  55. ALsizei mPhaseStep{};
  56. ALsizei mPhase{};
  57. ALdouble mLdSign{};
  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. ALfloat mCurrentGains[MAX_OUTPUT_CHANNELS]{};
  67. ALfloat mTargetGains[MAX_OUTPUT_CHANNELS]{};
  68. ALboolean deviceUpdate(const ALCdevice *device) override;
  69. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  70. void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput) override;
  71. DEF_NEWDEL(FshifterState)
  72. };
  73. ALboolean FshifterState::deviceUpdate(const ALCdevice *UNUSED(device))
  74. {
  75. /* (Re-)initializing parameters and clear the buffers. */
  76. mCount = FIFO_LATENCY;
  77. mPhaseStep = 0;
  78. mPhase = 0;
  79. mLdSign = 1.0;
  80. std::fill(std::begin(mInFIFO), std::end(mInFIFO), 0.0f);
  81. std::fill(std::begin(mOutFIFO), std::end(mOutFIFO), complex_d{});
  82. std::fill(std::begin(mOutputAccum), std::end(mOutputAccum), complex_d{});
  83. std::fill(std::begin(mAnalytic), std::end(mAnalytic), complex_d{});
  84. std::fill(std::begin(mCurrentGains), std::end(mCurrentGains), 0.0f);
  85. std::fill(std::begin(mTargetGains), std::end(mTargetGains), 0.0f);
  86. return AL_TRUE;
  87. }
  88. void FshifterState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  89. {
  90. const ALCdevice *device{context->Device};
  91. ALfloat step{props->Fshifter.Frequency / static_cast<ALfloat>(device->Frequency)};
  92. mPhaseStep = fastf2i(minf(step, 0.5f) * FRACTIONONE);
  93. switch(props->Fshifter.LeftDirection)
  94. {
  95. case AL_FREQUENCY_SHIFTER_DIRECTION_DOWN:
  96. mLdSign = -1.0;
  97. break;
  98. case AL_FREQUENCY_SHIFTER_DIRECTION_UP:
  99. mLdSign = 1.0;
  100. break;
  101. case AL_FREQUENCY_SHIFTER_DIRECTION_OFF:
  102. mPhase = 0;
  103. mPhaseStep = 0;
  104. break;
  105. }
  106. ALfloat coeffs[MAX_AMBI_CHANNELS];
  107. CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f, coeffs);
  108. mOutBuffer = target.Main->Buffer;
  109. mOutChannels = target.Main->NumChannels;
  110. ComputePanGains(target.Main, coeffs, slot->Params.Gain, mTargetGains);
  111. }
  112. void FshifterState::process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei /*numInput*/, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput)
  113. {
  114. static constexpr complex_d complex_zero{0.0, 0.0};
  115. ALfloat *RESTRICT BufferOut = mBufferOut;
  116. ALsizei j, k, base;
  117. for(base = 0;base < samplesToDo;)
  118. {
  119. const ALsizei todo{mini(HIL_SIZE-mCount, samplesToDo-base)};
  120. ASSUME(todo > 0);
  121. /* Fill FIFO buffer with samples data */
  122. k = mCount;
  123. for(j = 0;j < todo;j++,k++)
  124. {
  125. mInFIFO[k] = samplesIn[0][base+j];
  126. mOutdata[base+j] = mOutFIFO[k-FIFO_LATENCY];
  127. }
  128. mCount += todo;
  129. base += todo;
  130. /* Check whether FIFO buffer is filled */
  131. if(mCount < HIL_SIZE) continue;
  132. mCount = FIFO_LATENCY;
  133. /* Real signal windowing and store in Analytic buffer */
  134. for(k = 0;k < HIL_SIZE;k++)
  135. {
  136. mAnalytic[k].real(mInFIFO[k] * HannWindow[k]);
  137. mAnalytic[k].imag(0.0);
  138. }
  139. /* Processing signal by Discrete Hilbert Transform (analytical signal). */
  140. complex_hilbert(mAnalytic, HIL_SIZE);
  141. /* Windowing and add to output accumulator */
  142. for(k = 0;k < HIL_SIZE;k++)
  143. mOutputAccum[k] += 2.0/OVERSAMP*HannWindow[k]*mAnalytic[k];
  144. /* Shift accumulator, input & output FIFO */
  145. for(k = 0;k < HIL_STEP;k++) mOutFIFO[k] = mOutputAccum[k];
  146. for(j = 0;k < HIL_SIZE;k++,j++) mOutputAccum[j] = mOutputAccum[k];
  147. for(;j < HIL_SIZE;j++) mOutputAccum[j] = complex_zero;
  148. for(k = 0;k < FIFO_LATENCY;k++)
  149. mInFIFO[k] = mInFIFO[k+HIL_STEP];
  150. }
  151. /* Process frequency shifter using the analytic signal obtained. */
  152. for(k = 0;k < samplesToDo;k++)
  153. {
  154. double phase = mPhase * ((1.0/FRACTIONONE) * al::MathDefs<double>::Tau());
  155. BufferOut[k] = static_cast<float>(mOutdata[k].real()*std::cos(phase) +
  156. mOutdata[k].imag()*std::sin(phase)*mLdSign);
  157. mPhase += mPhaseStep;
  158. mPhase &= FRACTIONMASK;
  159. }
  160. /* Now, mix the processed sound data to the output. */
  161. MixSamples(BufferOut, numOutput, samplesOut, mCurrentGains, mTargetGains,
  162. maxi(samplesToDo, 512), 0, samplesToDo);
  163. }
  164. void Fshifter_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  165. {
  166. switch(param)
  167. {
  168. case AL_FREQUENCY_SHIFTER_FREQUENCY:
  169. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_FREQUENCY && val <= AL_FREQUENCY_SHIFTER_MAX_FREQUENCY))
  170. SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter frequency out of range");
  171. props->Fshifter.Frequency = val;
  172. break;
  173. default:
  174. alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x", param);
  175. }
  176. }
  177. void Fshifter_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  178. { Fshifter_setParamf(props, context, param, vals[0]); }
  179. void Fshifter_setParami(EffectProps *props, ALCcontext *context, ALenum param, ALint val)
  180. {
  181. switch(param)
  182. {
  183. case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
  184. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_LEFT_DIRECTION && val <= AL_FREQUENCY_SHIFTER_MAX_LEFT_DIRECTION))
  185. SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter left direction out of range");
  186. props->Fshifter.LeftDirection = val;
  187. break;
  188. case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
  189. if(!(val >= AL_FREQUENCY_SHIFTER_MIN_RIGHT_DIRECTION && val <= AL_FREQUENCY_SHIFTER_MAX_RIGHT_DIRECTION))
  190. SETERR_RETURN(context, AL_INVALID_VALUE,,"Frequency shifter right direction out of range");
  191. props->Fshifter.RightDirection = val;
  192. break;
  193. default:
  194. alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter integer property 0x%04x", param);
  195. }
  196. }
  197. void Fshifter_setParamiv(EffectProps *props, ALCcontext *context, ALenum param, const ALint *vals)
  198. { Fshifter_setParami(props, context, param, vals[0]); }
  199. void Fshifter_getParami(const EffectProps *props, ALCcontext *context, ALenum param, ALint *val)
  200. {
  201. switch(param)
  202. {
  203. case AL_FREQUENCY_SHIFTER_LEFT_DIRECTION:
  204. *val = props->Fshifter.LeftDirection;
  205. break;
  206. case AL_FREQUENCY_SHIFTER_RIGHT_DIRECTION:
  207. *val = props->Fshifter.RightDirection;
  208. break;
  209. default:
  210. alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter integer property 0x%04x", param);
  211. }
  212. }
  213. void Fshifter_getParamiv(const EffectProps *props, ALCcontext *context, ALenum param, ALint *vals)
  214. { Fshifter_getParami(props, context, param, vals); }
  215. void Fshifter_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  216. {
  217. switch(param)
  218. {
  219. case AL_FREQUENCY_SHIFTER_FREQUENCY:
  220. *val = props->Fshifter.Frequency;
  221. break;
  222. default:
  223. alSetError(context, AL_INVALID_ENUM, "Invalid frequency shifter float property 0x%04x", param);
  224. }
  225. }
  226. void Fshifter_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  227. { Fshifter_getParamf(props, context, param, vals); }
  228. DEFINE_ALEFFECT_VTABLE(Fshifter);
  229. struct FshifterStateFactory final : public EffectStateFactory {
  230. EffectState *create() override { return new FshifterState{}; }
  231. EffectProps getDefaultProps() const noexcept override;
  232. const EffectVtable *getEffectVtable() const noexcept override { return &Fshifter_vtable; }
  233. };
  234. EffectProps FshifterStateFactory::getDefaultProps() const noexcept
  235. {
  236. EffectProps props{};
  237. props.Fshifter.Frequency = AL_FREQUENCY_SHIFTER_DEFAULT_FREQUENCY;
  238. props.Fshifter.LeftDirection = AL_FREQUENCY_SHIFTER_DEFAULT_LEFT_DIRECTION;
  239. props.Fshifter.RightDirection = AL_FREQUENCY_SHIFTER_DEFAULT_RIGHT_DIRECTION;
  240. return props;
  241. }
  242. } // namespace
  243. EffectStateFactory *FshifterStateFactory_getFactory()
  244. {
  245. static FshifterStateFactory FshifterFactory{};
  246. return &FshifterFactory;
  247. }