echo.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. {
  45. free(state->SampleBuffer);
  46. state->SampleBuffer = NULL;
  47. }
  48. static ALboolean ALechoState_deviceUpdate(ALechoState *state, ALCdevice *Device)
  49. {
  50. ALuint maxlen, i;
  51. // Use the next power of 2 for the buffer length, so the tap offsets can be
  52. // wrapped using a mask instead of a modulo
  53. maxlen = fastf2u(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
  54. maxlen += fastf2u(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
  55. maxlen = NextPowerOf2(maxlen);
  56. if(maxlen != state->BufferLength)
  57. {
  58. void *temp;
  59. temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
  60. if(!temp) return AL_FALSE;
  61. state->SampleBuffer = temp;
  62. state->BufferLength = maxlen;
  63. }
  64. for(i = 0;i < state->BufferLength;i++)
  65. state->SampleBuffer[i] = 0.0f;
  66. return AL_TRUE;
  67. }
  68. static ALvoid ALechoState_update(ALechoState *state, ALCdevice *Device, const ALeffectslot *Slot)
  69. {
  70. ALfloat pandir[3] = { 0.0f, 0.0f, 0.0f };
  71. ALuint frequency = Device->Frequency;
  72. ALfloat gain, lrpan;
  73. state->Tap[0].delay = fastf2u(Slot->EffectProps.Echo.Delay * frequency) + 1;
  74. state->Tap[1].delay = fastf2u(Slot->EffectProps.Echo.LRDelay * frequency);
  75. state->Tap[1].delay += state->Tap[0].delay;
  76. lrpan = Slot->EffectProps.Echo.Spread;
  77. state->FeedGain = Slot->EffectProps.Echo.Feedback;
  78. gain = minf(1.0f - Slot->EffectProps.Echo.Damping, 0.01f);
  79. ALfilterState_setParams(&state->Filter, ALfilterType_HighShelf,
  80. gain, LOWPASSFREQREF/frequency,
  81. calc_rcpQ_from_slope(gain, 0.75f));
  82. gain = Slot->Gain;
  83. /* First tap panning */
  84. pandir[0] = -lrpan;
  85. ComputeDirectionalGains(Device, pandir, gain, state->Gain[0]);
  86. /* Second tap panning */
  87. pandir[0] = +lrpan;
  88. ComputeDirectionalGains(Device, pandir, gain, state->Gain[1]);
  89. }
  90. static ALvoid ALechoState_process(ALechoState *state, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  91. {
  92. const ALuint mask = state->BufferLength-1;
  93. const ALuint tap1 = state->Tap[0].delay;
  94. const ALuint tap2 = state->Tap[1].delay;
  95. ALuint offset = state->Offset;
  96. ALfloat smp;
  97. ALuint base;
  98. ALuint i, k;
  99. for(base = 0;base < SamplesToDo;)
  100. {
  101. ALfloat temps[128][2];
  102. ALuint td = minu(128, SamplesToDo-base);
  103. for(i = 0;i < td;i++)
  104. {
  105. /* First tap */
  106. temps[i][0] = state->SampleBuffer[(offset-tap1) & mask];
  107. /* Second tap */
  108. temps[i][1] = state->SampleBuffer[(offset-tap2) & mask];
  109. // Apply damping and feedback gain to the second tap, and mix in the
  110. // new sample
  111. smp = ALfilterState_processSingle(&state->Filter, temps[i][1]+SamplesIn[i+base]);
  112. state->SampleBuffer[offset&mask] = smp * state->FeedGain;
  113. offset++;
  114. }
  115. for(k = 0;k < NumChannels;k++)
  116. {
  117. ALfloat gain = state->Gain[0][k];
  118. if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
  119. {
  120. for(i = 0;i < td;i++)
  121. SamplesOut[k][i+base] += temps[i][0] * gain;
  122. }
  123. gain = state->Gain[1][k];
  124. if(fabsf(gain) > GAIN_SILENCE_THRESHOLD)
  125. {
  126. for(i = 0;i < td;i++)
  127. SamplesOut[k][i+base] += temps[i][1] * gain;
  128. }
  129. }
  130. base += td;
  131. }
  132. state->Offset = offset;
  133. }
  134. DECLARE_DEFAULT_ALLOCATORS(ALechoState)
  135. DEFINE_ALEFFECTSTATE_VTABLE(ALechoState);
  136. typedef struct ALechoStateFactory {
  137. DERIVE_FROM_TYPE(ALeffectStateFactory);
  138. } ALechoStateFactory;
  139. ALeffectState *ALechoStateFactory_create(ALechoStateFactory *UNUSED(factory))
  140. {
  141. ALechoState *state;
  142. state = ALechoState_New(sizeof(*state));
  143. if(!state) return NULL;
  144. SET_VTABLE2(ALechoState, ALeffectState, state);
  145. state->BufferLength = 0;
  146. state->SampleBuffer = NULL;
  147. state->Tap[0].delay = 0;
  148. state->Tap[1].delay = 0;
  149. state->Offset = 0;
  150. ALfilterState_clear(&state->Filter);
  151. return STATIC_CAST(ALeffectState, state);
  152. }
  153. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALechoStateFactory);
  154. ALeffectStateFactory *ALechoStateFactory_getFactory(void)
  155. {
  156. static ALechoStateFactory EchoFactory = { { GET_VTABLE2(ALechoStateFactory, ALeffectStateFactory) } };
  157. return STATIC_CAST(ALeffectStateFactory, &EchoFactory);
  158. }
  159. void ALecho_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  160. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  161. void ALecho_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  162. {
  163. ALecho_setParami(effect, context, param, vals[0]);
  164. }
  165. void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  166. {
  167. ALeffectProps *props = &effect->Props;
  168. switch(param)
  169. {
  170. case AL_ECHO_DELAY:
  171. if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
  172. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  173. props->Echo.Delay = val;
  174. break;
  175. case AL_ECHO_LRDELAY:
  176. if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
  177. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  178. props->Echo.LRDelay = val;
  179. break;
  180. case AL_ECHO_DAMPING:
  181. if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
  182. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  183. props->Echo.Damping = val;
  184. break;
  185. case AL_ECHO_FEEDBACK:
  186. if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
  187. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  188. props->Echo.Feedback = val;
  189. break;
  190. case AL_ECHO_SPREAD:
  191. if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
  192. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  193. props->Echo.Spread = val;
  194. break;
  195. default:
  196. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  197. }
  198. }
  199. void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  200. {
  201. ALecho_setParamf(effect, context, param, vals[0]);
  202. }
  203. void ALecho_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  204. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  205. void ALecho_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  206. {
  207. ALecho_getParami(effect, context, param, vals);
  208. }
  209. void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  210. {
  211. const ALeffectProps *props = &effect->Props;
  212. switch(param)
  213. {
  214. case AL_ECHO_DELAY:
  215. *val = props->Echo.Delay;
  216. break;
  217. case AL_ECHO_LRDELAY:
  218. *val = props->Echo.LRDelay;
  219. break;
  220. case AL_ECHO_DAMPING:
  221. *val = props->Echo.Damping;
  222. break;
  223. case AL_ECHO_FEEDBACK:
  224. *val = props->Echo.Feedback;
  225. break;
  226. case AL_ECHO_SPREAD:
  227. *val = props->Echo.Spread;
  228. break;
  229. default:
  230. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  231. }
  232. }
  233. void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  234. {
  235. ALecho_getParamf(effect, context, param, vals);
  236. }
  237. DEFINE_ALEFFECT_VTABLE(ALecho);