compressor.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2013 by Anis A. Hireche
  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 <stdlib.h>
  21. #include "config.h"
  22. #include "alError.h"
  23. #include "alMain.h"
  24. #include "alAuxEffectSlot.h"
  25. #include "alu.h"
  26. typedef struct ALcompressorState {
  27. DERIVE_FROM_TYPE(ALeffectState);
  28. /* Effect gains for each channel */
  29. ALfloat Gain[MaxChannels];
  30. /* Effect parameters */
  31. ALboolean Enabled;
  32. ALfloat AttackRate;
  33. ALfloat ReleaseRate;
  34. ALfloat GainCtrl;
  35. } ALcompressorState;
  36. static ALvoid ALcompressorState_Destruct(ALcompressorState *UNUSED(state))
  37. {
  38. }
  39. static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device)
  40. {
  41. const ALfloat attackTime = device->Frequency * 0.2f; /* 200ms Attack */
  42. const ALfloat releaseTime = device->Frequency * 0.4f; /* 400ms Release */
  43. state->AttackRate = 1.0f / attackTime;
  44. state->ReleaseRate = 1.0f / releaseTime;
  45. return AL_TRUE;
  46. }
  47. static ALvoid ALcompressorState_update(ALcompressorState *state, ALCdevice *Device, const ALeffectslot *Slot)
  48. {
  49. ALfloat gain;
  50. state->Enabled = Slot->EffectProps.Compressor.OnOff;
  51. gain = sqrtf(1.0f / Device->NumChan) * Slot->Gain;
  52. SetGains(Device, gain, state->Gain);
  53. }
  54. static ALvoid ALcompressorState_process(ALcompressorState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[BUFFERSIZE])
  55. {
  56. ALuint it, kt;
  57. ALuint base;
  58. for(base = 0;base < SamplesToDo;)
  59. {
  60. ALfloat temps[64];
  61. ALuint td = minu(SamplesToDo-base, 64);
  62. if(state->Enabled)
  63. {
  64. ALfloat output, smp, amplitude;
  65. ALfloat gain = state->GainCtrl;
  66. for(it = 0;it < td;it++)
  67. {
  68. smp = SamplesIn[it+base];
  69. amplitude = fabs(smp);
  70. if(amplitude > gain)
  71. gain = minf(gain+state->AttackRate, amplitude);
  72. else if(amplitude < gain)
  73. gain = maxf(gain-state->ReleaseRate, amplitude);
  74. output = 1.0 / clampf(gain, 0.5f, 2.0f);
  75. temps[it] = smp * output;
  76. }
  77. state->GainCtrl = gain;
  78. }
  79. else
  80. {
  81. ALfloat output, smp, amplitude;
  82. ALfloat gain = state->GainCtrl;
  83. for(it = 0;it < td;it++)
  84. {
  85. smp = SamplesIn[it+base];
  86. amplitude = 1.0f;
  87. if(amplitude > gain)
  88. gain = minf(gain+state->AttackRate, amplitude);
  89. else if(amplitude < gain)
  90. gain = maxf(gain-state->ReleaseRate, amplitude);
  91. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  92. temps[it] = smp * output;
  93. }
  94. state->GainCtrl = gain;
  95. }
  96. for(kt = 0;kt < MaxChannels;kt++)
  97. {
  98. ALfloat gain = state->Gain[kt];
  99. if(!(gain > GAIN_SILENCE_THRESHOLD))
  100. continue;
  101. for(it = 0;it < td;it++)
  102. SamplesOut[kt][base+it] += gain * temps[it];
  103. }
  104. base += td;
  105. }
  106. }
  107. static void ALcompressorState_Delete(ALcompressorState *state)
  108. {
  109. free(state);
  110. }
  111. DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
  112. typedef struct ALcompressorStateFactory {
  113. DERIVE_FROM_TYPE(ALeffectStateFactory);
  114. } ALcompressorStateFactory;
  115. static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
  116. {
  117. ALcompressorState *state;
  118. state = malloc(sizeof(*state));
  119. if(!state) return NULL;
  120. SET_VTABLE2(ALcompressorState, ALeffectState, state);
  121. state->Enabled = AL_TRUE;
  122. state->AttackRate = 0.0f;
  123. state->ReleaseRate = 0.0f;
  124. state->GainCtrl = 1.0f;
  125. return STATIC_CAST(ALeffectState, state);
  126. }
  127. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
  128. ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
  129. {
  130. static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
  131. return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
  132. }
  133. void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  134. {
  135. ALeffectProps *props = &effect->Props;
  136. switch(param)
  137. {
  138. case AL_COMPRESSOR_ONOFF:
  139. if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
  140. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  141. props->Compressor.OnOff = val;
  142. break;
  143. default:
  144. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  145. }
  146. }
  147. void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  148. {
  149. ALcompressor_setParami(effect, context, param, vals[0]);
  150. }
  151. void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
  152. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  153. void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  154. {
  155. ALcompressor_setParamf(effect, context, param, vals[0]);
  156. }
  157. void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  158. {
  159. const ALeffectProps *props = &effect->Props;
  160. switch(param)
  161. {
  162. case AL_COMPRESSOR_ONOFF:
  163. *val = props->Compressor.OnOff;
  164. break;
  165. default:
  166. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  167. }
  168. }
  169. void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  170. {
  171. ALcompressor_getParami(effect, context, param, vals);
  172. }
  173. void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
  174. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  175. void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  176. {
  177. ALcompressor_getParamf(effect, context, param, vals);
  178. }
  179. DEFINE_ALEFFECT_VTABLE(ALcompressor);