compressor.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_EFFECT_CHANNELS][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 *state);
  37. static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device);
  38. static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCdevice *device, const ALeffectslot *slot, const ALeffectProps *props);
  39. static ALvoid ALcompressorState_process(ALcompressorState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels);
  40. DECLARE_DEFAULT_ALLOCATORS(ALcompressorState)
  41. DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
  42. static void ALcompressorState_Construct(ALcompressorState *state)
  43. {
  44. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  45. SET_VTABLE2(ALcompressorState, ALeffectState, state);
  46. state->Enabled = AL_TRUE;
  47. state->AttackRate = 0.0f;
  48. state->ReleaseRate = 0.0f;
  49. state->GainCtrl = 1.0f;
  50. }
  51. static ALvoid ALcompressorState_Destruct(ALcompressorState *state)
  52. {
  53. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  54. }
  55. static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device)
  56. {
  57. const ALfloat attackTime = device->Frequency * 0.2f; /* 200ms Attack */
  58. const ALfloat releaseTime = device->Frequency * 0.4f; /* 400ms Release */
  59. state->AttackRate = 1.0f / attackTime;
  60. state->ReleaseRate = 1.0f / releaseTime;
  61. return AL_TRUE;
  62. }
  63. static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCdevice *device, const ALeffectslot *slot, const ALeffectProps *props)
  64. {
  65. ALuint i;
  66. state->Enabled = props->Compressor.OnOff;
  67. STATIC_CAST(ALeffectState,state)->OutBuffer = device->FOAOut.Buffer;
  68. STATIC_CAST(ALeffectState,state)->OutChannels = device->FOAOut.NumChannels;
  69. for(i = 0;i < 4;i++)
  70. ComputeFirstOrderGains(device->FOAOut, IdentityMatrixf.m[i],
  71. slot->Params.Gain, state->Gain[i]);
  72. }
  73. static ALvoid ALcompressorState_process(ALcompressorState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
  74. {
  75. ALuint i, j, k;
  76. ALuint base;
  77. for(base = 0;base < SamplesToDo;)
  78. {
  79. ALfloat temps[64][4];
  80. ALuint td = minu(64, SamplesToDo-base);
  81. /* Load samples into the temp buffer first. */
  82. for(j = 0;j < 4;j++)
  83. {
  84. for(i = 0;i < td;i++)
  85. temps[i][j] = SamplesIn[j][i+base];
  86. }
  87. if(state->Enabled)
  88. {
  89. ALfloat gain = state->GainCtrl;
  90. ALfloat output, amplitude;
  91. for(i = 0;i < td;i++)
  92. {
  93. /* Roughly calculate the maximum amplitude from the 4-channel
  94. * signal, and attack or release the gain control to reach it.
  95. */
  96. amplitude = fabsf(temps[i][0]);
  97. amplitude = maxf(amplitude + fabsf(temps[i][1]),
  98. maxf(amplitude + fabsf(temps[i][2]),
  99. amplitude + fabsf(temps[i][3])));
  100. if(amplitude > gain)
  101. gain = minf(gain+state->AttackRate, amplitude);
  102. else if(amplitude < gain)
  103. gain = maxf(gain-state->ReleaseRate, amplitude);
  104. /* Apply the inverse of the gain control to normalize/compress
  105. * the volume. */
  106. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  107. for(j = 0;j < 4;j++)
  108. temps[i][j] *= output;
  109. }
  110. state->GainCtrl = gain;
  111. }
  112. else
  113. {
  114. ALfloat gain = state->GainCtrl;
  115. ALfloat output, amplitude;
  116. for(i = 0;i < td;i++)
  117. {
  118. /* Same as above, except the amplitude is forced to 1. This
  119. * helps ensure smooth gain changes when the compressor is
  120. * turned on and off.
  121. */
  122. amplitude = 1.0f;
  123. if(amplitude > gain)
  124. gain = minf(gain+state->AttackRate, amplitude);
  125. else if(amplitude < gain)
  126. gain = maxf(gain-state->ReleaseRate, amplitude);
  127. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  128. for(j = 0;j < 4;j++)
  129. temps[i][j] *= output;
  130. }
  131. state->GainCtrl = gain;
  132. }
  133. /* Now mix to the output. */
  134. for(j = 0;j < 4;j++)
  135. {
  136. for(k = 0;k < NumChannels;k++)
  137. {
  138. ALfloat gain = state->Gain[j][k];
  139. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  140. continue;
  141. for(i = 0;i < td;i++)
  142. SamplesOut[k][base+i] += gain * temps[i][j];
  143. }
  144. }
  145. base += td;
  146. }
  147. }
  148. typedef struct ALcompressorStateFactory {
  149. DERIVE_FROM_TYPE(ALeffectStateFactory);
  150. } ALcompressorStateFactory;
  151. static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
  152. {
  153. ALcompressorState *state;
  154. NEW_OBJ0(state, ALcompressorState)();
  155. if(!state) return NULL;
  156. return STATIC_CAST(ALeffectState, state);
  157. }
  158. DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
  159. ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
  160. {
  161. static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
  162. return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
  163. }
  164. void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  165. {
  166. ALeffectProps *props = &effect->Props;
  167. switch(param)
  168. {
  169. case AL_COMPRESSOR_ONOFF:
  170. if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
  171. SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
  172. props->Compressor.OnOff = val;
  173. break;
  174. default:
  175. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  176. }
  177. }
  178. void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  179. {
  180. ALcompressor_setParami(effect, context, param, vals[0]);
  181. }
  182. void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
  183. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  184. void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
  185. {
  186. ALcompressor_setParamf(effect, context, param, vals[0]);
  187. }
  188. void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  189. {
  190. const ALeffectProps *props = &effect->Props;
  191. switch(param)
  192. {
  193. case AL_COMPRESSOR_ONOFF:
  194. *val = props->Compressor.OnOff;
  195. break;
  196. default:
  197. SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
  198. }
  199. }
  200. void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  201. {
  202. ALcompressor_getParami(effect, context, param, vals);
  203. }
  204. void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
  205. { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
  206. void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
  207. {
  208. ALcompressor_getParamf(effect, context, param, vals);
  209. }
  210. DEFINE_ALEFFECT_VTABLE(ALcompressor);