echo.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. typedef struct ALechoState {
  29. DERIVE_FROM_TYPE(ALeffectState);
  30. ALfloat *SampleBuffer;
  31. ALuint BufferLength;
  32. // The echo is two tap. The delay is the number of samples from before the
  33. // current offset
  34. struct {
  35. ALuint delay;
  36. } Tap[2];
  37. ALuint Offset;
  38. /* The panning gains for the two taps */
  39. ALfloat Gain[2][MAX_OUTPUT_CHANNELS];
  40. ALfloat FeedGain;
  41. ALfilterState Filter;
  42. } ALechoState;
  43. static ALvoid ALechoState_Destruct(ALechoState *state);
  44. static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device);
  45. static ALvoid ALechoState_update(ALechoState *state, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props);
  46. static ALvoid ALechoState_process(ALechoState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
  47. DECLARE_DEFAULT_ALLOCATORS(ALechoState)
  48. DEFINE_ALEFFECTSTATE_VTABLE(ALechoState);
  49. static void ALechoState_Construct(ALechoState *state)
  50. {
  51. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  52. SET_VTABLE2(ALechoState, ALeffectState, state);
  53. state->BufferLength = 0;
  54. state->SampleBuffer = NULL;
  55. state->Tap[0].delay = 0;
  56. state->Tap[1].delay = 0;
  57. state->Offset = 0;
  58. ALfilterState_clear(&state->Filter);
  59. }
  60. static ALvoid ALechoState_Destruct(ALechoState *state)
  61. {
  62. al_free(state->SampleBuffer);
  63. state->SampleBuffer = NULL;
  64. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  65. }
  66. static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device)
  67. {
  68. ALuint maxlen, i;
  69. // Use the next power of 2 for the buffer length, so the tap offsets can be
  70. // wrapped using a mask instead of a modulo
  71. maxlen = fastf2u(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
  72. maxlen += fastf2u(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
  73. maxlen = NextPowerOf2(maxlen);
  74. if(maxlen != state->BufferLength)
  75. {
  76. void *temp = al_calloc(16, maxlen * sizeof(ALfloat));
  77. if(!temp) return AL_FALSE;
  78. al_free(state->SampleBuffer);
  79. state->SampleBuffer = temp;
  80. state->BufferLength = maxlen;
  81. }
  82. for(i = 0;i < state->BufferLength;i++)
  83. state->SampleBuffer[i] = 0.0f;
  84. return AL_TRUE;
  85. }
  86. static ALvoid ALechoState_update(ALechoState *state, const ALCdevice *Device, const ALeffectslot *Slot, const ALeffectProps *props)
  87. {
  88. ALuint frequency = Device->Frequency;
  89. ALfloat coeffs[MAX_AMBI_COEFFS];
  90. ALfloat gain, lrpan, spread;
  91. state->Tap[0].delay = fastf2u(props->Echo.Delay * frequency) + 1;
  92. state->Tap[1].delay = fastf2u(props->Echo.LRDelay * frequency);
  93. state->Tap[1].delay += state->Tap[0].delay;
  94. spread = props->Echo.Spread;
  95. if(spread < 0.0f) lrpan = -1.0f;
  96. else lrpan = 1.0f;
  97. /* Convert echo spread (where 0 = omni, +/-1 = directional) to coverage
  98. * spread (where 0 = point, tau = omni).
  99. */
  100. spread = asinf(1.0f - fabsf(spread))*4.0f;
  101. state->FeedGain = props->Echo.Feedback;
  102. gain = minf(1.0f - props->Echo.Damping, 0.01f);
  103. ALfilterState_setParams(&state->Filter, ALfilterType_HighShelf,
  104. gain, LOWPASSFREQREF/frequency,
  105. calc_rcpQ_from_slope(gain, 0.75f));
  106. gain = Slot->Params.Gain;
  107. /* First tap panning */
  108. CalcXYZCoeffs(-lrpan, 0.0f, 0.0f, spread, coeffs);
  109. ComputePanningGains(Device->Dry, coeffs, gain, state->Gain[0]);
  110. /* Second tap panning */
  111. CalcXYZCoeffs( lrpan, 0.0f, 0.0f, spread, coeffs);
  112. ComputePanningGains(Device->Dry, coeffs, gain, state->Gain[1]);
  113. }
  114. static ALvoid ALechoState_process(ALechoState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  115. {
  116. const ALuint mask = state->BufferLength-1;
  117. const ALuint tap1 = state->Tap[0].delay;
  118. const ALuint tap2 = state->Tap[1].delay;
  119. ALuint offset = state->Offset;
  120. ALfloat x[2], y[2], in, out;
  121. ALuint base;
  122. ALuint i, k;
  123. x[0] = state->Filter.x[0];
  124. x[1] = state->Filter.x[1];
  125. y[0] = state->Filter.y[0];
  126. y[1] = state->Filter.y[1];
  127. for(base = 0;base < SamplesToDo;)
  128. {
  129. ALfloat temps[128][2];
  130. ALuint td = minu(128, SamplesToDo-base);
  131. for(i = 0;i < td;i++)
  132. {
  133. /* First tap */
  134. temps[i][0] = state->SampleBuffer[(offset-tap1) & mask];
  135. /* Second tap */
  136. temps[i][1] = state->SampleBuffer[(offset-tap2) & mask];
  137. // Apply damping and feedback gain to the second tap, and mix in the
  138. // new sample
  139. in = temps[i][1] + SamplesIn[0][i+base];
  140. out = in*state->Filter.b0 +
  141. x[0]*state->Filter.b1 + x[1]*state->Filter.b2 -
  142. y[0]*state->Filter.a1 - y[1]*state->Filter.a2;
  143. x[1] = x[0]; x[0] = in;
  144. y[1] = y[0]; y[0] = out;
  145. state->SampleBuffer[offset&mask] = out * state->FeedGain;
  146. offset++;
  147. }
  148. for(k = 0;k < NumChannels;k++)
  149. {
  150. ALfloat gain = state->Gain[0][k];
  151. if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
  152. {
  153. for(i = 0;i < td;i++)
  154. SamplesOut[k][i+base] += temps[i][0] * gain;
  155. }
  156. gain = state->Gain[1][k];
  157. if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
  158. {
  159. for(i = 0;i < td;i++)
  160. SamplesOut[k][i+base] += temps[i][1] * gain;
  161. }
  162. }
  163. base += td;
  164. }
  165. state->Filter.x[0] = x[0];
  166. state->Filter.x[1] = x[1];
  167. state->Filter.y[0] = y[0];
  168. state->Filter.y[1] = y[1];
  169. state->Offset = offset;
  170. }
  171. typedef struct ALechoStateFactory {
  172. DERIVE_FROM_TYPE(ALeffectStateFactory);
  173. } ALechoStateFactory;
  174. ALeffectState *ALechoStateFactory_create(ALechoStateFactory *UNUSED(factory))
  175. {
  176. ALechoState *state;
  177. NEW_OBJ0(state, ALechoState)();
  178. if(!state) return NULL;
  179. return STATIC_CAST(ALeffectState, state);
  180. }
  181. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALechoStateFactory);
  182. ALeffectStateFactory *ALechoStateFactory_getFactory(void)
  183. {
  184. static ALechoStateFactory EchoFactory = { { GET_VTABLE2(ALechoStateFactory, ALeffectStateFactory) } };
  185. return STATIC_CAST(ALeffectStateFactory, &EchoFactory);
  186. }
  187. void ALecho_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  188. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  189. void ALecho_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  190. {
  191. ALecho_setParami(effect, context, param, vals[0]);
  192. }
  193. void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  194. {
  195. ALeffectProps *props = &effect->Props;
  196. switch(param)
  197. {
  198. case AL_ECHO_DELAY:
  199. if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
  200. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  201. props->Echo.Delay = val;
  202. break;
  203. case AL_ECHO_LRDELAY:
  204. if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
  205. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  206. props->Echo.LRDelay = val;
  207. break;
  208. case AL_ECHO_DAMPING:
  209. if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
  210. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  211. props->Echo.Damping = val;
  212. break;
  213. case AL_ECHO_FEEDBACK:
  214. if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
  215. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  216. props->Echo.Feedback = val;
  217. break;
  218. case AL_ECHO_SPREAD:
  219. if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
  220. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  221. props->Echo.Spread = val;
  222. break;
  223. default:
  224. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  225. }
  226. }
  227. void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  228. {
  229. ALecho_setParamf(effect, context, param, vals[0]);
  230. }
  231. void ALecho_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  232. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  233. void ALecho_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  234. {
  235. ALecho_getParami(effect, context, param, vals);
  236. }
  237. void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  238. {
  239. const ALeffectProps *props = &effect->Props;
  240. switch(param)
  241. {
  242. case AL_ECHO_DELAY:
  243. *val = props->Echo.Delay;
  244. break;
  245. case AL_ECHO_LRDELAY:
  246. *val = props->Echo.LRDelay;
  247. break;
  248. case AL_ECHO_DAMPING:
  249. *val = props->Echo.Damping;
  250. break;
  251. case AL_ECHO_FEEDBACK:
  252. *val = props->Echo.Feedback;
  253. break;
  254. case AL_ECHO_SPREAD:
  255. *val = props->Echo.Spread;
  256. break;
  257. default:
  258. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  259. }
  260. }
  261. void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  262. {
  263. ALecho_getParamf(effect, context, param, vals);
  264. }
  265. DEFINE_ALEFFECT_VTABLE(ALecho);