alcEcho.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 LR gains for the first tap. The second tap uses the reverse
  40. ALfloat GainL;
  41. ALfloat GainR;
  42. ALfloat FeedGain;
  43. ALfloat Scale;
  44. FILTER iirFilter;
  45. ALfloat history[2];
  46. } ALechoState;
  47. static ALvoid EchoDestroy(ALeffectState *effect)
  48. {
  49. ALechoState *state = (ALechoState*)effect;
  50. if(state)
  51. {
  52. free(state->SampleBuffer);
  53. state->SampleBuffer = NULL;
  54. free(state);
  55. }
  56. }
  57. static ALboolean EchoDeviceUpdate(ALeffectState *effect, ALCdevice *Device)
  58. {
  59. ALechoState *state = (ALechoState*)effect;
  60. ALuint maxlen, i;
  61. // Use the next power of 2 for the buffer length, so the tap offsets can be
  62. // wrapped using a mask instead of a modulo
  63. maxlen = (ALuint)(AL_ECHO_MAX_DELAY * Device->Frequency) + 1;
  64. maxlen += (ALuint)(AL_ECHO_MAX_LRDELAY * Device->Frequency) + 1;
  65. maxlen = NextPowerOf2(maxlen);
  66. if(maxlen != state->BufferLength)
  67. {
  68. void *temp;
  69. temp = realloc(state->SampleBuffer, maxlen * sizeof(ALfloat));
  70. if(!temp)
  71. return AL_FALSE;
  72. state->SampleBuffer = temp;
  73. state->BufferLength = maxlen;
  74. }
  75. for(i = 0;i < state->BufferLength;i++)
  76. state->SampleBuffer[i] = 0.0f;
  77. state->Scale = aluSqrt(Device->NumChan / 6.0f);
  78. state->Scale = __min(state->Scale, 1.0f);
  79. return AL_TRUE;
  80. }
  81. static ALvoid EchoUpdate(ALeffectState *effect, ALCcontext *Context, const ALeffect *Effect)
  82. {
  83. ALechoState *state = (ALechoState*)effect;
  84. ALuint frequency = Context->Device->Frequency;
  85. ALfloat lrpan, cw, a, g;
  86. state->Tap[0].delay = (ALuint)(Effect->Echo.Delay * frequency) + 1;
  87. state->Tap[1].delay = (ALuint)(Effect->Echo.LRDelay * frequency);
  88. state->Tap[1].delay += state->Tap[0].delay;
  89. lrpan = Effect->Echo.Spread*0.5f + 0.5f;
  90. state->GainL = aluSqrt( lrpan);
  91. state->GainR = aluSqrt(1.0f-lrpan);
  92. state->FeedGain = Effect->Echo.Feedback;
  93. cw = cos(2.0*M_PI * LOWPASSFREQCUTOFF / frequency);
  94. g = 1.0f - Effect->Echo.Damping;
  95. a = 0.0f;
  96. if(g < 0.9999f) // 1-epsilon
  97. a = (1 - g*cw - aluSqrt(2*g*(1-cw) - g*g*(1 - cw*cw))) / (1 - g);
  98. state->iirFilter.coeff = a;
  99. }
  100. static ALvoid EchoProcess(ALeffectState *effect, const ALeffectslot *Slot, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[OUTPUTCHANNELS])
  101. {
  102. ALechoState *state = (ALechoState*)effect;
  103. const ALuint mask = state->BufferLength-1;
  104. const ALuint tap1 = state->Tap[0].delay;
  105. const ALuint tap2 = state->Tap[1].delay;
  106. ALuint offset = state->Offset;
  107. const ALfloat gain = Slot->Gain * state->Scale;
  108. ALfloat samp[2], smp;
  109. ALuint i;
  110. for(i = 0;i < SamplesToDo;i++,offset++)
  111. {
  112. // Sample first tap
  113. smp = state->SampleBuffer[(offset-tap1) & mask];
  114. samp[0] = smp * state->GainL;
  115. samp[1] = smp * state->GainR;
  116. // Sample second tap. Reverse LR panning
  117. smp = state->SampleBuffer[(offset-tap2) & mask];
  118. samp[0] += smp * state->GainR;
  119. samp[1] += smp * state->GainL;
  120. // Apply damping and feedback gain to the second tap, and mix in the
  121. // new sample
  122. smp = lpFilter2P(&state->iirFilter, 0, smp+SamplesIn[i]);
  123. state->SampleBuffer[offset&mask] = smp * state->FeedGain;
  124. // Apply slot gain
  125. samp[0] *= gain;
  126. samp[1] *= gain;
  127. SamplesOut[i][FRONT_LEFT] += samp[0];
  128. SamplesOut[i][FRONT_RIGHT] += samp[1];
  129. SamplesOut[i][SIDE_LEFT] += samp[0];
  130. SamplesOut[i][SIDE_RIGHT] += samp[1];
  131. SamplesOut[i][BACK_LEFT] += samp[0];
  132. SamplesOut[i][BACK_RIGHT] += samp[1];
  133. }
  134. state->Offset = offset;
  135. }
  136. ALeffectState *EchoCreate(void)
  137. {
  138. ALechoState *state;
  139. state = malloc(sizeof(*state));
  140. if(!state)
  141. return NULL;
  142. state->state.Destroy = EchoDestroy;
  143. state->state.DeviceUpdate = EchoDeviceUpdate;
  144. state->state.Update = EchoUpdate;
  145. state->state.Process = EchoProcess;
  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. state->GainL = 0.0f;
  152. state->GainR = 0.0f;
  153. state->Scale = 1.0f;
  154. state->iirFilter.coeff = 0.0f;
  155. state->iirFilter.history[0] = 0.0f;
  156. state->iirFilter.history[1] = 0.0f;
  157. return &state->state;
  158. }