alcEcho.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. // Must be first in all effects!
  30. ALeffectState state;
  31. ALfloat *SampleBuffer;
  32. ALuint BufferLength;
  33. // The echo is two tap. The delay is the number of samples from before the
  34. // current offset
  35. struct {
  36. ALuint delay;
  37. } Tap[2];
  38. ALuint Offset;
  39. /* The panning gains for the two taps */
  40. ALfloat Gain[2][MAXCHANNELS];
  41. ALfloat FeedGain;
  42. FILTER iirFilter;
  43. ALfloat history[2];
  44. } ALechoState;
  45. static ALvoid EchoDestroy(ALeffectState *effect)
  46. {
  47. ALechoState *state = (ALechoState*)effect;
  48. if(state)
  49. {
  50. free(state->SampleBuffer);
  51. state->SampleBuffer = NULL;
  52. free(state);
  53. }
  54. }
  55. static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
  56. {
  57. ALechoState *state = (ALechoState*)effect;
  58. ALuint maxlen, i;
  59. // Use the next power of 2 for the buffer length, so the tap offsets can be
  60. // wrapped using a mask instead of a modulo
  61. maxlen = fastf2u(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
  62. maxlen += fastf2u(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
  63. maxlen = NextPowerOf2(maxlen);
  64. if(maxlen != state->BufferLength)
  65. {
  66. void *temp;
  67. temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
  68. if(!temp)
  69. return AL_FALSE;
  70. state->SampleBuffer = temp;
  71. state->BufferLength = maxlen;
  72. }
  73. for(i = 0;i < state->BufferLength;i++)
  74. state->SampleBuffer[i] = 0.0f;
  75. return AL_TRUE;
  76. }
  77. static ALvoid EchoUpdate(ALeffectState *effect, ALCdevice *Device, const ALeffectslot *Slot)
  78. {
  79. ALechoState *state = (ALechoState*)effect;
  80. ALuint frequency = Device->Frequency;
  81. ALfloat dirGain, ambientGain;
  82. const ALfloat *ChannelGain;
  83. ALfloat lrpan, cw, g, gain;
  84. ALuint i, pos;
  85. state->Tap[0].delay = fastf2u(Slot->effect.Echo.Delay * frequency) + 1;
  86. state->Tap[1].delay = fastf2u(Slot->effect.Echo.LRDelay * frequency);
  87. state->Tap[1].delay += state->Tap[0].delay;
  88. lrpan = Slot->effect.Echo.Spread;
  89. state->FeedGain = Slot->effect.Echo.Feedback;
  90. cw = aluCos(F_PI*2.0f * LOWPASSFREQREF / frequency);
  91. g = 1.0f - Slot->effect.Echo.Damping;
  92. state->iirFilter.coeff = lpCoeffCalc(g, cw);
  93. gain = Slot->Gain;
  94. for(i = 0;i < MAXCHANNELS;i++)
  95. {
  96. state->Gain[0][i] = 0.0f;
  97. state->Gain[1][i] = 0.0f;
  98. }
  99. ambientGain = aluSqrt(1.0f/Device->NumChan);
  100. dirGain = aluFabs(lrpan);
  101. /* First tap panning */
  102. pos = aluCart2LUTpos(0.0f, ((lrpan>0.0f)?-1.0f:1.0f));
  103. ChannelGain = Device->PanningLUT[pos];
  104. for(i = 0;i < Device->NumChan;i++)
  105. {
  106. enum Channel chan = Device->Speaker2Chan[i];
  107. state->Gain[0][chan] = lerp(ambientGain, ChannelGain[chan], dirGain) * gain;
  108. }
  109. /* Second tap panning */
  110. pos = aluCart2LUTpos(0.0f, ((lrpan>0.0f)?1.0f:-1.0f));
  111. ChannelGain = Device->PanningLUT[pos];
  112. for(i = 0;i < Device->NumChan;i++)
  113. {
  114. enum Channel chan = Device->Speaker2Chan[i];
  115. state->Gain[1][chan] = lerp(ambientGain, ChannelGain[chan], dirGain) * gain;
  116. }
  117. }
  118. static ALvoid EchoProcess(ALeffectState *effect, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[MAXCHANNELS])
  119. {
  120. ALechoState *state = (ALechoState*)effect;
  121. const ALuint mask = state->BufferLength-1;
  122. const ALuint tap1 = state->Tap[0].delay;
  123. const ALuint tap2 = state->Tap[1].delay;
  124. ALuint offset = state->Offset;
  125. ALfloat smp;
  126. ALuint i, k;
  127. for(i = 0;i < SamplesToDo;i++,offset++)
  128. {
  129. /* First tap */
  130. smp = state->SampleBuffer[(offset-tap1) & mask];
  131. for(k = 0;k < MAXCHANNELS;k++)
  132. SamplesOut[i][k] += smp * state->Gain[0][k];
  133. /* Second tap */
  134. smp = state->SampleBuffer[(offset-tap2) & mask];
  135. for(k = 0;k < MAXCHANNELS;k++)
  136. SamplesOut[i][k] += smp * state->Gain[1][k];
  137. // Apply damping and feedback gain to the second tap, and mix in the
  138. // new sample
  139. smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);
  140. state->SampleBuffer[offset&mask] = smp * state->FeedGain;
  141. }
  142. state->Offset = offset;
  143. }
  144. ALeffectState *EchoCreate(void)
  145. {
  146. ALechoState *state;
  147. state = malloc(sizeof(*state));
  148. if(!state)
  149. return NULL;
  150. state->state.Destroy = EchoDestroy;
  151. state->state.DeviceUpdate = EchoDeviceUpdate;
  152. state->state.Update = EchoUpdate;
  153. state->state.Process = EchoProcess;
  154. state->BufferLength = 0;
  155. state->SampleBuffer = NULL;
  156. state->Tap[0].delay = 0;
  157. state->Tap[1].delay = 0;
  158. state->Offset = 0;
  159. state->iirFilter.coeff = 0.0f;
  160. state->iirFilter.history[0] = 0.0f;
  161. state->iirFilter.history[1] = 0.0f;
  162. return &state->state;
  163. }