echo.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, 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][MaxChannels];
  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. ALuint frequency = Device->Frequency;
  71. ALfloat lrpan, gain;
  72. ALfloat dirGain;
  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. ALfilterState_setParams(&state->Filter, ALfilterType_HighShelf,
  79. 1.0f - Slot->EffectProps.Echo.Damping,
  80. (ALfloat)LOWPASSFREQREF/frequency, 0.0f);
  81. gain = Slot->Gain;
  82. dirGain = fabsf(lrpan);
  83. /* First tap panning */
  84. ComputeAngleGains(Device, atan2f(-lrpan, 0.0f), (1.0f-dirGain)*F_PI, gain, state->Gain[0]);
  85. /* Second tap panning */
  86. ComputeAngleGains(Device, atan2f(+lrpan, 0.0f), (1.0f-dirGain)*F_PI, gain, state->Gain[1]);
  87. }
  88. static ALvoid ALechoState_process(ALechoState *state, ALuint SamplesToDo, const ALfloat *restrict SamplesIn, ALfloat (*restrict SamplesOut)[BUFFERSIZE])
  89. {
  90. const ALuint mask = state->BufferLength-1;
  91. const ALuint tap1 = state->Tap[0].delay;
  92. const ALuint tap2 = state->Tap[1].delay;
  93. ALuint offset = state->Offset;
  94. ALfloat smp;
  95. ALuint base;
  96. ALuint i, k;
  97. for(base = 0;base < SamplesToDo;)
  98. {
  99. ALfloat temps[64][2];
  100. ALuint td = minu(SamplesToDo-base, 64);
  101. for(i = 0;i < td;i++)
  102. {
  103. /* First tap */
  104. temps[i][0] = state->SampleBuffer[(offset-tap1) & mask];
  105. /* Second tap */
  106. temps[i][1] = state->SampleBuffer[(offset-tap2) & mask];
  107. // Apply damping and feedback gain to the second tap, and mix in the
  108. // new sample
  109. smp = ALfilterState_processSingle(&state->Filter, temps[i][1]+SamplesIn[i+base]);
  110. state->SampleBuffer[offset&mask] = smp * state->FeedGain;
  111. offset++;
  112. }
  113. for(k = 0;k < MaxChannels;k++)
  114. {
  115. ALfloat gain = state->Gain[0][k];
  116. if(gain > GAIN_SILENCE_THRESHOLD)
  117. {
  118. for(i = 0;i < td;i++)
  119. SamplesOut[k][i+base] += temps[i][0] * gain;
  120. }
  121. gain = state->Gain[1][k];
  122. if(gain > GAIN_SILENCE_THRESHOLD)
  123. {
  124. for(i = 0;i < td;i++)
  125. SamplesOut[k][i+base] += temps[i][1] * gain;
  126. }
  127. }
  128. base += td;
  129. }
  130. state->Offset = offset;
  131. }
  132. static void ALechoState_Delete(ALechoState *state)
  133. {
  134. free(state);
  135. }
  136. DEFINE_ALEFFECTSTATE_VTABLE(ALechoState);
  137. typedef struct ALechoStateFactory {
  138. DERIVE_FROM_TYPE(ALeffectStateFactory);
  139. } ALechoStateFactory;
  140. ALeffectState *ALechoStateFactory_create(ALechoStateFactory *UNUSED(factory))
  141. {
  142. ALechoState *state;
  143. state = malloc(sizeof(*state));
  144. if(!state) return NULL;
  145. SET_VTABLE2(ALechoState, ALeffectState, state);
  146. state->BufferLength = 0;
  147. state->SampleBuffer = NULL;
  148. state->Tap[0].delay = 0;
  149. state->Tap[1].delay = 0;
  150. state->Offset = 0;
  151. ALfilterState_clear(&state->Filter);
  152. return STATIC_CAST(ALeffectState, state);
  153. }
  154. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALechoStateFactory);
  155. ALeffectStateFactory *ALechoStateFactory_getFactory(void)
  156. {
  157. static ALechoStateFactory EchoFactory = { { GET_VTABLE2(ALechoStateFactory, ALeffectStateFactory) } };
  158. return STATIC_CAST(ALeffectStateFactory, &EchoFactory);
  159. }
  160. void ALecho_setParami(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint UNUSED(val))
  161. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  162. void ALecho_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  163. {
  164. ALecho_setParami(effect, context, param, vals[0]);
  165. }
  166. void ALecho_setParamf(ALeffect *effect, ALCcontext *context, ALenum param, ALfloat val)
  167. {
  168. ALeffectProps *props = &effect->Props;
  169. switch(param)
  170. {
  171. case AL_ECHO_DELAY:
  172. if(!(val >= AL_ECHO_MIN_DELAY && val <= AL_ECHO_MAX_DELAY))
  173. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  174. props->Echo.Delay = val;
  175. break;
  176. case AL_ECHO_LRDELAY:
  177. if(!(val >= AL_ECHO_MIN_LRDELAY && val <= AL_ECHO_MAX_LRDELAY))
  178. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  179. props->Echo.LRDelay = val;
  180. break;
  181. case AL_ECHO_DAMPING:
  182. if(!(val >= AL_ECHO_MIN_DAMPING && val <= AL_ECHO_MAX_DAMPING))
  183. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  184. props->Echo.Damping = val;
  185. break;
  186. case AL_ECHO_FEEDBACK:
  187. if(!(val >= AL_ECHO_MIN_FEEDBACK && val <= AL_ECHO_MAX_FEEDBACK))
  188. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  189. props->Echo.Feedback = val;
  190. break;
  191. case AL_ECHO_SPREAD:
  192. if(!(val >= AL_ECHO_MIN_SPREAD && val <= AL_ECHO_MAX_SPREAD))
  193. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  194. props->Echo.Spread = val;
  195. break;
  196. default:
  197. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  198. }
  199. }
  200. void ALecho_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  201. {
  202. ALecho_setParamf(effect, context, param, vals[0]);
  203. }
  204. void ALecho_getParami(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALint *UNUSED(val))
  205. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  206. void ALecho_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  207. {
  208. ALecho_getParami(effect, context, param, vals);
  209. }
  210. void ALecho_getParamf(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *val)
  211. {
  212. const ALeffectProps *props = &effect->Props;
  213. switch(param)
  214. {
  215. case AL_ECHO_DELAY:
  216. *val = props->Echo.Delay;
  217. break;
  218. case AL_ECHO_LRDELAY:
  219. *val = props->Echo.LRDelay;
  220. break;
  221. case AL_ECHO_DAMPING:
  222. *val = props->Echo.Damping;
  223. break;
  224. case AL_ECHO_FEEDBACK:
  225. *val = props->Echo.Feedback;
  226. break;
  227. case AL_ECHO_SPREAD:
  228. *val = props->Echo.Spread;
  229. break;
  230. default:
  231. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  232. }
  233. }
  234. void ALecho_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  235. {
  236. ALecho_getParamf(effect, context, param, vals);
  237. }
  238. DEFINE_ALEFFECT_VTABLE(ALecho);