compressor.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.,
  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 <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[MAX_OUTPUT_CHANNELS];
  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. state->Enabled = slot->EffectProps.Compressor.OnOff;
  50. ComputeAmbientGains(device, slot->Gain, state->Gain);
  51. }
  52. static ALvoid ALcompressorState_process(ALcompressorState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  53. {
  54. ALuint it, kt;
  55. ALuint base;
  56. for(base = 0;base < SamplesToDo;)
  57. {
  58. ALfloat temps[256];
  59. ALuint td = minu(256, SamplesToDo-base);
  60. if(state->Enabled)
  61. {
  62. ALfloat output, smp, amplitude;
  63. ALfloat gain = state->GainCtrl;
  64. for(it = 0;it < td;it++)
  65. {
  66. smp = SamplesIn[it+base];
  67. amplitude = fabsf(smp);
  68. if(amplitude > gain)
  69. gain = minf(gain+state->AttackRate, amplitude);
  70. else if(amplitude < gain)
  71. gain = maxf(gain-state->ReleaseRate, amplitude);
  72. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  73. temps[it] = smp * output;
  74. }
  75. state->GainCtrl = gain;
  76. }
  77. else
  78. {
  79. ALfloat output, smp, amplitude;
  80. ALfloat gain = state->GainCtrl;
  81. for(it = 0;it < td;it++)
  82. {
  83. smp = SamplesIn[it+base];
  84. amplitude = 1.0f;
  85. if(amplitude > gain)
  86. gain = minf(gain+state->AttackRate, amplitude);
  87. else if(amplitude < gain)
  88. gain = maxf(gain-state->ReleaseRate, amplitude);
  89. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  90. temps[it] = smp * output;
  91. }
  92. state->GainCtrl = gain;
  93. }
  94. for(kt = 0;kt < NumChannels;kt++)
  95. {
  96. ALfloat gain = state->Gain[kt];
  97. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  98. continue;
  99. for(it = 0;it < td;it++)
  100. SamplesOut[kt][base+it] += gain * temps[it];
  101. }
  102. base += td;
  103. }
  104. }
  105. DECLARE_DEFAULT_ALLOCATORS(ALcompressorState)
  106. DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
  107. typedef struct ALcompressorStateFactory {
  108. DERIVE_FROM_TYPE(ALeffectStateFactory);
  109. } ALcompressorStateFactory;
  110. static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
  111. {
  112. ALcompressorState *state;
  113. state = ALcompressorState_New(sizeof(*state));
  114. if(!state) return NULL;
  115. SET_VTABLE2(ALcompressorState, ALeffectState, state);
  116. state->Enabled = AL_TRUE;
  117. state->AttackRate = 0.0f;
  118. state->ReleaseRate = 0.0f;
  119. state->GainCtrl = 1.0f;
  120. return STATIC_CAST(ALeffectState, state);
  121. }
  122. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
  123. ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
  124. {
  125. static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
  126. return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
  127. }
  128. void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  129. {
  130. ALeffectProps *props = &effect->Props;
  131. switch(param)
  132. {
  133. case AL_COMPRESSOR_ONOFF:
  134. if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
  135. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  136. props->Compressor.OnOff = val;
  137. break;
  138. default:
  139. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  140. }
  141. }
  142. void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  143. {
  144. ALcompressor_setParami(effect, context, param, vals[0]);
  145. }
  146. void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
  147. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  148. void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  149. {
  150. ALcompressor_setParamf(effect, context, param, vals[0]);
  151. }
  152. void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  153. {
  154. const ALeffectProps *props = &effect->Props;
  155. switch(param)
  156. {
  157. case AL_COMPRESSOR_ONOFF:
  158. *val = props->Compressor.OnOff;
  159. break;
  160. default:
  161. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  162. }
  163. }
  164. void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  165. {
  166. ALcompressor_getParami(effect, context, param, vals);
  167. }
  168. void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
  169. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  170. void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  171. {
  172. ALcompressor_getParamf(effect, context, param, vals);
  173. }
  174. DEFINE_ALEFFECT_VTABLE(ALcompressor);