compressor.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #define AMP_ENVELOPE_MIN 0.5f
  27. #define AMP_ENVELOPE_MAX 2.0f
  28. #define ATTACK_TIME 0.1f /* 100ms to rise from min to max */
  29. #define RELEASE_TIME 0.2f /* 200ms to drop from max to min */
  30. typedef struct ALcompressorState {
  31. DERIVE_FROM_TYPE(ALeffectState);
  32. /* Effect gains for each channel */
  33. ALfloat Gain[MAX_EFFECT_CHANNELS][MAX_OUTPUT_CHANNELS];
  34. /* Effect parameters */
  35. ALboolean Enabled;
  36. ALfloat AttackMult;
  37. ALfloat ReleaseMult;
  38. ALfloat EnvFollower;
  39. } ALcompressorState;
  40. static ALvoid ALcompressorState_Destruct(ALcompressorState *state);
  41. static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device);
  42. static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
  43. static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
  44. DECLARE_DEFAULT_ALLOCATORS(ALcompressorState)
  45. DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
  46. static void ALcompressorState_Construct(ALcompressorState *state)
  47. {
  48. ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
  49. SET_VTABLE2(ALcompressorState, ALeffectState, state);
  50. state->Enabled = AL_TRUE;
  51. state->AttackMult = 1.0f;
  52. state->ReleaseMult = 1.0f;
  53. state->EnvFollower = 1.0f;
  54. }
  55. static ALvoid ALcompressorState_Destruct(ALcompressorState *state)
  56. {
  57. ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
  58. }
  59. static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device)
  60. {
  61. /* Number of samples to do a full attack and release (non-integer sample
  62. * counts are okay).
  63. */
  64. const ALfloat attackCount = (ALfloat)device->Frequency * ATTACK_TIME;
  65. const ALfloat releaseCount = (ALfloat)device->Frequency * RELEASE_TIME;
  66. /* Calculate per-sample multipliers to attack and release at the desired
  67. * rates.
  68. */
  69. state->AttackMult = powf(AMP_ENVELOPE_MAX/AMP_ENVELOPE_MIN, 1.0f/attackCount);
  70. state->ReleaseMult = powf(AMP_ENVELOPE_MIN/AMP_ENVELOPE_MAX, 1.0f/releaseCount);
  71. return AL_TRUE;
  72. }
  73. static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
  74. {
  75. const ALCdevice *device = context->Device;
  76. ALuint i;
  77. state->Enabled = props->Compressor.OnOff;
  78. STATIC_CAST(ALeffectState,state)->OutBuffer = device->FOAOut.Buffer;
  79. STATIC_CAST(ALeffectState,state)->OutChannels = device->FOAOut.NumChannels;
  80. for(i = 0;i < 4;i++)
  81. ComputePanGains(&device->FOAOut, IdentityMatrixf.m[i], slot->Params.Gain, state->Gain[i]);
  82. }
  83. static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  84. {
  85. ALsizei i, j, k;
  86. ALsizei base;
  87. for(base = 0;base < SamplesToDo;)
  88. {
  89. ALfloat gains[256];
  90. ALsizei td = mini(256, SamplesToDo-base);
  91. ALfloat env = state->EnvFollower;
  92. /* Generate the per-sample gains from the signal envelope. */
  93. if(state->Enabled)
  94. {
  95. for(i = 0;i < td;++i)
  96. {
  97. /* Clamp the absolute amplitude to the defined envelope limits,
  98. * then attack or release the envelope to reach it.
  99. */
  100. ALfloat amplitude = clampf(fabsf(SamplesIn[0][base+i]),
  101. AMP_ENVELOPE_MIN, AMP_ENVELOPE_MAX);
  102. if(amplitude > env)
  103. env = minf(env*state->AttackMult, amplitude);
  104. else if(amplitude < env)
  105. env = maxf(env*state->ReleaseMult, amplitude);
  106. /* Apply the reciprocal of the envelope to normalize the volume
  107. * (compress the dynamic range).
  108. */
  109. gains[i] = 1.0f / env;
  110. }
  111. }
  112. else
  113. {
  114. /* Same as above, except the amplitude is forced to 1. This helps
  115. * ensure smooth gain changes when the compressor is turned on and
  116. * off.
  117. */
  118. for(i = 0;i < td;++i)
  119. {
  120. ALfloat amplitude = 1.0f;
  121. if(amplitude > env)
  122. env = minf(env*state->AttackMult, amplitude);
  123. else if(amplitude < env)
  124. env = maxf(env*state->ReleaseMult, amplitude);
  125. gains[i] = 1.0f / env;
  126. }
  127. }
  128. state->EnvFollower = env;
  129. /* Now compress the signal amplitude to output. */
  130. for(j = 0;j < MAX_EFFECT_CHANNELS;j++)
  131. {
  132. for(k = 0;k < NumChannels;k++)
  133. {
  134. ALfloat gain = state->Gain[j][k];
  135. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  136. continue;
  137. for(i = 0;i < td;i++)
  138. SamplesOut[k][base+i] += SamplesIn[j][base+i] * gains[i] * gain;
  139. }
  140. }
  141. base += td;
  142. }
  143. }
  144. typedef struct CompressorStateFactory {
  145. DERIVE_FROM_TYPE(EffectStateFactory);
  146. } CompressorStateFactory;
  147. static ALeffectState *CompressorStateFactory_create(CompressorStateFactory *UNUSED(factory))
  148. {
  149. ALcompressorState *state;
  150. NEW_OBJ0(state, ALcompressorState)();
  151. if(!state) return NULL;
  152. return STATIC_CAST(ALeffectState, state);
  153. }
  154. DEFINE_EFFECTSTATEFACTORY_VTABLE(CompressorStateFactory);
  155. EffectStateFactory *CompressorStateFactory_getFactory(void)
  156. {
  157. static CompressorStateFactory CompressorFactory = { { GET_VTABLE2(CompressorStateFactory, EffectStateFactory) } };
  158. return STATIC_CAST(EffectStateFactory, &CompressorFactory);
  159. }
  160. void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  161. {
  162. ALeffectProps *props = &effect->Props;
  163. switch(param)
  164. {
  165. case AL_COMPRESSOR_ONOFF:
  166. if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
  167. SETERR_RETURN(context, AL_INVALID_VALUE,, "Compressor state out of range");
  168. props->Compressor.OnOff = val;
  169. break;
  170. default:
  171. alSetError(context, AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
  172. param);
  173. }
  174. }
  175. void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  176. { ALcompressor_setParami(effect, context, param, vals[0]); }
  177. void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat UNUSED(val))
  178. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param); }
  179. void ALcompressor_setParamfv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALfloat *UNUSED(vals))
  180. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x", param); }
  181. void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  182. {
  183. const ALeffectProps *props = &effect->Props;
  184. switch(param)
  185. {
  186. case AL_COMPRESSOR_ONOFF:
  187. *val = props->Compressor.OnOff;
  188. break;
  189. default:
  190. alSetError(context, AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
  191. param);
  192. }
  193. }
  194. void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  195. { ALcompressor_getParami(effect, context, param, vals); }
  196. void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat *UNUSED(val))
  197. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param); }
  198. void ALcompressor_getParamfv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat *UNUSED(vals))
  199. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x", param); }
  200. DEFINE_ALEFFECT_VTABLE(ALcompressor);