🛠️🐜 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.

272 lines
9.5 KiB

  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 <algorithm>
  24. #include "alMain.h"
  25. #include "alcontext.h"
  26. #include "alFilter.h"
  27. #include "alAuxEffectSlot.h"
  28. #include "alError.h"
  29. #include "alu.h"
  30. #include "filters/biquad.h"
  31. #include "vector.h"
  32. namespace {
  33. struct EchoState final : public EffectState {
  34. al::vector<ALfloat,16> mSampleBuffer;
  35. // The echo is two tap. The delay is the number of samples from before the
  36. // current offset
  37. struct {
  38. ALsizei delay{0};
  39. } mTap[2];
  40. ALsizei mOffset{0};
  41. /* The panning gains for the two taps */
  42. struct {
  43. ALfloat Current[MAX_OUTPUT_CHANNELS]{};
  44. ALfloat Target[MAX_OUTPUT_CHANNELS]{};
  45. } mGains[2];
  46. BiquadFilter mFilter;
  47. ALfloat mFeedGain{0.0f};
  48. alignas(16) ALfloat mTempBuffer[2][BUFFERSIZE];
  49. ALboolean deviceUpdate(const ALCdevice *device) override;
  50. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  51. void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput) override;
  52. DEF_NEWDEL(EchoState)
  53. };
  54. ALboolean EchoState::deviceUpdate(const ALCdevice *Device)
  55. {
  56. ALuint maxlen;
  57. // Use the next power of 2 for the buffer length, so the tap offsets can be
  58. // wrapped using a mask instead of a modulo
  59. maxlen = float2int(AL_ECHO_MAX_DELAY*Device->Frequency + 0.5f) +
  60. float2int(AL_ECHO_MAX_LRDELAY*Device->Frequency + 0.5f);
  61. maxlen = NextPowerOf2(maxlen);
  62. if(maxlen <= 0) return AL_FALSE;
  63. if(maxlen != mSampleBuffer.size())
  64. {
  65. mSampleBuffer.resize(maxlen);
  66. mSampleBuffer.shrink_to_fit();
  67. }
  68. std::fill(mSampleBuffer.begin(), mSampleBuffer.end(), 0.0f);
  69. for(auto &e : mGains)
  70. {
  71. std::fill(std::begin(e.Current), std::end(e.Current), 0.0f);
  72. std::fill(std::begin(e.Target), std::end(e.Target), 0.0f);
  73. }
  74. return AL_TRUE;
  75. }
  76. void EchoState::update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  77. {
  78. const ALCdevice *device = context->Device;
  79. const auto frequency = static_cast<ALfloat>(device->Frequency);
  80. mTap[0].delay = maxi(float2int(props->Echo.Delay*frequency + 0.5f), 1);
  81. mTap[1].delay = float2int(props->Echo.LRDelay*frequency + 0.5f) + mTap[0].delay;
  82. const ALfloat gainhf{maxf(1.0f - props->Echo.Damping, 0.0625f)}; /* Limit -24dB */
  83. mFilter.setParams(BiquadType::HighShelf, gainhf, LOWPASSFREQREF/frequency,
  84. calc_rcpQ_from_slope(gainhf, 1.0f));
  85. mFeedGain = props->Echo.Feedback;
  86. /* Convert echo spread (where 0 = center, +/-1 = sides) to angle. */
  87. const ALfloat angle{std::asin(props->Echo.Spread)};
  88. ALfloat coeffs[2][MAX_AMBI_CHANNELS];
  89. CalcAngleCoeffs(-angle, 0.0f, 0.0f, coeffs[0]);
  90. CalcAngleCoeffs( angle, 0.0f, 0.0f, coeffs[1]);
  91. mOutBuffer = target.Main->Buffer;
  92. mOutChannels = target.Main->NumChannels;
  93. ComputePanGains(target.Main, coeffs[0], slot->Params.Gain, mGains[0].Target);
  94. ComputePanGains(target.Main, coeffs[1], slot->Params.Gain, mGains[1].Target);
  95. }
  96. void EchoState::process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei /*numInput*/, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput)
  97. {
  98. const auto mask = static_cast<ALsizei>(mSampleBuffer.size()-1);
  99. ALfloat *RESTRICT delaybuf{mSampleBuffer.data()};
  100. ALsizei offset{mOffset};
  101. ALsizei tap1{offset - mTap[0].delay};
  102. ALsizei tap2{offset - mTap[1].delay};
  103. ALfloat z1, z2;
  104. ASSUME(samplesToDo > 0);
  105. ASSUME(mask > 0);
  106. std::tie(z1, z2) = mFilter.getComponents();
  107. for(ALsizei i{0};i < samplesToDo;)
  108. {
  109. offset &= mask;
  110. tap1 &= mask;
  111. tap2 &= mask;
  112. ALsizei td{mini(mask+1 - maxi(offset, maxi(tap1, tap2)), samplesToDo-i)};
  113. do {
  114. /* Feed the delay buffer's input first. */
  115. delaybuf[offset] = samplesIn[0][i];
  116. /* Get delayed output from the first and second taps. Use the
  117. * second tap for feedback.
  118. */
  119. mTempBuffer[0][i] = delaybuf[tap1++];
  120. mTempBuffer[1][i] = delaybuf[tap2++];
  121. const float feedb{mTempBuffer[1][i++]};
  122. /* Add feedback to the delay buffer with damping and attenuation. */
  123. delaybuf[offset++] += mFilter.processOne(feedb, z1, z2) * mFeedGain;
  124. } while(--td);
  125. }
  126. mFilter.setComponents(z1, z2);
  127. mOffset = offset;
  128. for(ALsizei c{0};c < 2;c++)
  129. MixSamples(mTempBuffer[c], numOutput, samplesOut, mGains[c].Current, mGains[c].Target,
  130. samplesToDo, 0, samplesToDo);
  131. }
  132. void Echo_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
  133. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer property 0x%04x", param); }
  134. void Echo_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
  135. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer-vector property 0x%04x", param); }
  136. void Echo_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  137. {
  138. switch(param)
  139. {
  140. case AL_ECHO_DELAY:
  141. if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
  142. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo delay out of range");
  143. props->Echo.Delay = val;
  144. break;
  145. case AL_ECHO_LRDELAY:
  146. if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
  147. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo LR delay out of range");
  148. props->Echo.LRDelay = val;
  149. break;
  150. case AL_ECHO_DAMPING:
  151. if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
  152. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo damping out of range");
  153. props->Echo.Damping = val;
  154. break;
  155. case AL_ECHO_FEEDBACK:
  156. if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
  157. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo feedback out of range");
  158. props->Echo.Feedback = val;
  159. break;
  160. case AL_ECHO_SPREAD:
  161. if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
  162. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo spread out of range");
  163. props->Echo.Spread = val;
  164. break;
  165. default:
  166. alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
  167. }
  168. }
  169. void Echo_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  170. { Echo_setParamf(props, context, param, vals[0]); }
  171. void Echo_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  172. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer property 0x%04x", param); }
  173. void Echo_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  174. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer-vector property 0x%04x", param); }
  175. void Echo_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  176. {
  177. switch(param)
  178. {
  179. case AL_ECHO_DELAY:
  180. *val = props->Echo.Delay;
  181. break;
  182. case AL_ECHO_LRDELAY:
  183. *val = props->Echo.LRDelay;
  184. break;
  185. case AL_ECHO_DAMPING:
  186. *val = props->Echo.Damping;
  187. break;
  188. case AL_ECHO_FEEDBACK:
  189. *val = props->Echo.Feedback;
  190. break;
  191. case AL_ECHO_SPREAD:
  192. *val = props->Echo.Spread;
  193. break;
  194. default:
  195. alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
  196. }
  197. }
  198. void Echo_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  199. { Echo_getParamf(props, context, param, vals); }
  200. DEFINE_ALEFFECT_VTABLE(Echo);
  201. struct EchoStateFactory final : public EffectStateFactory {
  202. EffectState *create() override { return new EchoState{}; }
  203. EffectProps getDefaultProps() const noexcept override;
  204. const EffectVtable *getEffectVtable() const noexcept override { return &Echo_vtable; }
  205. };
  206. EffectProps EchoStateFactory::getDefaultProps() const noexcept
  207. {
  208. EffectProps props{};
  209. props.Echo.Delay = AL_ECHO_DEFAULT_DELAY;
  210. props.Echo.LRDelay = AL_ECHO_DEFAULT_LRDELAY;
  211. props.Echo.Damping = AL_ECHO_DEFAULT_DAMPING;
  212. props.Echo.Feedback = AL_ECHO_DEFAULT_FEEDBACK;
  213. props.Echo.Spread = AL_ECHO_DEFAULT_SPREAD;
  214. return props;
  215. }
  216. } // namespace
  217. EffectStateFactory *EchoStateFactory_getFactory()
  218. {
  219. static EchoStateFactory EchoFactory{};
  220. return &EchoFactory;
  221. }