echo.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <math.h>
  22. #include <stdlib.h>
  23. #include "alMain.h"
  24. #include "alFilter.h"
  25. #include "alAuxEffectSlot.h"
  26. #include "alError.h"
  27. #include "alu.h"
  28. #include "filters/defs.h"
  29. typedef struct ALechoState {
  30. DERIVE_FROM_TYPE(ALeffectState);
  31. ALfloat *SampleBuffer;
  32. ALsizei BufferLength;
  33. // The echo is two tap. The delay is the number of samples from before the
  34. // current offset
  35. struct {
  36. ALsizei delay;
  37. } Tap[2];
  38. ALsizei Offset;
  39. /* The panning gains for the two taps */
  40. struct {
  41. ALfloat Current[MAX_OUTPUT_CHANNELS];
  42. ALfloat Target[MAX_OUTPUT_CHANNELS];
  43. } Gains[2];
  44. ALfloat FeedGain;
  45. BiquadState Filter;
  46. } ALechoState;
  47. static ALvoid ALechoState_Destruct(ALechoState *state);
  48. static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device);
  49. static ALvoid ALechoState_update(ALechoState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
  50. static ALvoid ALechoState_process(ALechoState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
  51. DECLARE_DEFAULT_ALLOCATORS(ALechoState)
  52. DEFINE_ALEFFECTSTATE_VTABLE(ALechoState);
  53. static void ALechoState_Construct(ALechoState *state)
  54. {
  55. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  56. SET_VTABLE2(ALechoState, ALeffectState, state);
  57. state->BufferLength = 0;
  58. state->SampleBuffer = NULL;
  59. state->Tap[0].delay = 0;
  60. state->Tap[1].delay = 0;
  61. state->Offset = 0;
  62. BiquadState_clear(&state->Filter);
  63. }
  64. static ALvoid ALechoState_Destruct(ALechoState *state)
  65. {
  66. al_free(state->SampleBuffer);
  67. state->SampleBuffer = NULL;
  68. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  69. }
  70. static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device)
  71. {
  72. ALsizei maxlen;
  73. // Use the next power of 2 for the buffer length, so the tap offsets can be
  74. // wrapped using a mask instead of a modulo
  75. maxlen = fastf2i(AL_ECHO_MAX_DELAY*Device->Frequency + 0.5f) +
  76. fastf2i(AL_ECHO_MAX_LRDELAY*Device->Frequency + 0.5f);
  77. maxlen = NextPowerOf2(maxlen);
  78. if(maxlen <= 0) return AL_FALSE;
  79. if(maxlen != state->BufferLength)
  80. {
  81. void *temp = al_calloc(16, maxlen * sizeof(ALfloat));
  82. if(!temp) return AL_FALSE;
  83. al_free(state->SampleBuffer);
  84. state->SampleBuffer = temp;
  85. state->BufferLength = maxlen;
  86. }
  87. memset(state->SampleBuffer, 0, state->BufferLength*sizeof(ALfloat));
  88. memset(state->Gains, 0, sizeof(state->Gains));
  89. return AL_TRUE;
  90. }
  91. static ALvoid ALechoState_update(ALechoState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
  92. {
  93. const ALCdevice *device = context->Device;
  94. ALuint frequency = device->Frequency;
  95. ALfloat coeffs[MAX_AMBI_COEFFS];
  96. ALfloat gainhf, lrpan, spread;
  97. state->Tap[0].delay = maxi(fastf2i(props->Echo.Delay*frequency + 0.5f), 1);
  98. state->Tap[1].delay = fastf2i(props->Echo.LRDelay*frequency + 0.5f);
  99. state->Tap[1].delay += state->Tap[0].delay;
  100. spread = props->Echo.Spread;
  101. if(spread < 0.0f) lrpan = -1.0f;
  102. else lrpan = 1.0f;
  103. /* Convert echo spread (where 0 = omni, +/-1 = directional) to coverage
  104. * spread (where 0 = point, tau = omni).
  105. */
  106. spread = asinf(1.0f - fabsf(spread))*4.0f;
  107. state->FeedGain = props->Echo.Feedback;
  108. gainhf = maxf(1.0f - props->Echo.Damping, 0.0625f); /* Limit -24dB */
  109. BiquadState_setParams(&state->Filter, BiquadType_HighShelf,
  110. gainhf, LOWPASSFREQREF/frequency,
  111. calc_rcpQ_from_slope(gainhf, 1.0f));
  112. /* First tap panning */
  113. CalcAngleCoeffs(-F_PI_2*lrpan, 0.0f, spread, coeffs);
  114. ComputeDryPanGains(&device->Dry, coeffs, slot->Params.Gain, state->Gains[0].Target);
  115. /* Second tap panning */
  116. CalcAngleCoeffs( F_PI_2*lrpan, 0.0f, spread, coeffs);
  117. ComputeDryPanGains(&device->Dry, coeffs, slot->Params.Gain, state->Gains[1].Target);
  118. }
  119. static ALvoid ALechoState_process(ALechoState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  120. {
  121. const ALsizei mask = state->BufferLength-1;
  122. const ALsizei tap1 = state->Tap[0].delay;
  123. const ALsizei tap2 = state->Tap[1].delay;
  124. ALfloat *restrict delaybuf = state->SampleBuffer;
  125. ALsizei offset = state->Offset;
  126. ALfloat x[2], y[2], in, out;
  127. ALsizei base;
  128. ALsizei c, i;
  129. x[0] = state->Filter.x[0];
  130. x[1] = state->Filter.x[1];
  131. y[0] = state->Filter.y[0];
  132. y[1] = state->Filter.y[1];
  133. for(base = 0;base < SamplesToDo;)
  134. {
  135. alignas(16) ALfloat temps[2][128];
  136. ALsizei td = mini(128, SamplesToDo-base);
  137. for(i = 0;i < td;i++)
  138. {
  139. /* Feed the delay buffer's input first. */
  140. delaybuf[offset&mask] = SamplesIn[0][i+base];
  141. /* First tap */
  142. temps[0][i] = delaybuf[(offset-tap1) & mask];
  143. /* Second tap */
  144. temps[1][i] = delaybuf[(offset-tap2) & mask];
  145. /* Apply damping to the second tap, then add it to the buffer with
  146. * feedback attenuation.
  147. */
  148. in = temps[1][i];
  149. out = in*state->Filter.b0 +
  150. x[0]*state->Filter.b1 + x[1]*state->Filter.b2 -
  151. y[0]*state->Filter.a1 - y[1]*state->Filter.a2;
  152. x[1] = x[0]; x[0] = in;
  153. y[1] = y[0]; y[0] = out;
  154. delaybuf[offset&mask] += out * state->FeedGain;
  155. offset++;
  156. }
  157. for(c = 0;c < 2;c++)
  158. MixSamples(temps[c], NumChannels, SamplesOut, state->Gains[c].Current,
  159. state->Gains[c].Target, SamplesToDo-base, base, td);
  160. base += td;
  161. }
  162. state->Filter.x[0] = x[0];
  163. state->Filter.x[1] = x[1];
  164. state->Filter.y[0] = y[0];
  165. state->Filter.y[1] = y[1];
  166. state->Offset = offset;
  167. }
  168. typedef struct EchoStateFactory {
  169. DERIVE_FROM_TYPE(EffectStateFactory);
  170. } EchoStateFactory;
  171. ALeffectState *EchoStateFactory_create(EchoStateFactory *UNUSED(factory))
  172. {
  173. ALechoState *state;
  174. NEW_OBJ0(state, ALechoState)();
  175. if(!state) return NULL;
  176. return STATIC_CAST(ALeffectState, state);
  177. }
  178. DEFINE_EFFECTSTATEFACTORY_VTABLE(EchoStateFactory);
  179. EffectStateFactory *EchoStateFactory_getFactory(void)
  180. {
  181. static EchoStateFactory EchoFactory = { { GET_VTABLE2(EchoStateFactory, EffectStateFactory) } };
  182. return STATIC_CAST(EffectStateFactory, &EchoFactory);
  183. }
  184. void ALecho_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint UNUSED(val))
  185. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer property 0x%04x", param); }
  186. void ALecho_setParamiv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALint *UNUSED(vals))
  187. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer-vector property 0x%04x", param); }
  188. void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  189. {
  190. ALeffectProps *props = &effect->Props;
  191. switch(param)
  192. {
  193. case AL_ECHO_DELAY:
  194. if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
  195. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo delay out of range");
  196. props->Echo.Delay = val;
  197. break;
  198. case AL_ECHO_LRDELAY:
  199. if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
  200. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo LR delay out of range");
  201. props->Echo.LRDelay = val;
  202. break;
  203. case AL_ECHO_DAMPING:
  204. if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
  205. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo damping out of range");
  206. props->Echo.Damping = val;
  207. break;
  208. case AL_ECHO_FEEDBACK:
  209. if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
  210. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo feedback out of range");
  211. props->Echo.Feedback = val;
  212. break;
  213. case AL_ECHO_SPREAD:
  214. if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
  215. SETERR_RETURN(context, AL_INVALID_VALUE,, "Echo spread out of range");
  216. props->Echo.Spread = val;
  217. break;
  218. default:
  219. alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
  220. }
  221. }
  222. void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  223. { ALecho_setParamf(effect, context, param, vals[0]); }
  224. void ALecho_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(val))
  225. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer property 0x%04x", param); }
  226. void ALecho_getParamiv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALint *UNUSED(vals))
  227. { alSetError(context, AL_INVALID_ENUM, "Invalid echo integer-vector property 0x%04x", param); }
  228. void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  229. {
  230. const ALeffectProps *props = &effect->Props;
  231. switch(param)
  232. {
  233. case AL_ECHO_DELAY:
  234. *val = props->Echo.Delay;
  235. break;
  236. case AL_ECHO_LRDELAY:
  237. *val = props->Echo.LRDelay;
  238. break;
  239. case AL_ECHO_DAMPING:
  240. *val = props->Echo.Damping;
  241. break;
  242. case AL_ECHO_FEEDBACK:
  243. *val = props->Echo.Feedback;
  244. break;
  245. case AL_ECHO_SPREAD:
  246. *val = props->Echo.Spread;
  247. break;
  248. default:
  249. alSetError(context, AL_INVALID_ENUM, "Invalid echo float property 0x%04x", param);
  250. }
  251. }
  252. void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  253. { ALecho_getParamf(effect, context, param, vals); }
  254. DEFINE_ALEFFECT_VTABLE(ALecho);