compressor.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
  39. static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei 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 ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
  64. {
  65. const ALCdevice *device = context->Device;
  66. ALuint i;
  67. state->Enabled = props->Compressor.OnOff;
  68. STATIC_CAST(ALeffectState,state)->OutBuffer = device->FOAOut.Buffer;
  69. STATIC_CAST(ALeffectState,state)->OutChannels = device->FOAOut.NumChannels;
  70. for(i = 0;i < 4;i++)
  71. ComputeFirstOrderGains(&device->FOAOut, IdentityMatrixf.m[i],
  72. slot->Params.Gain, state->Gain[i]);
  73. }
  74. static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
  75. {
  76. ALsizei i, j, k;
  77. ALsizei base;
  78. for(base = 0;base < SamplesToDo;)
  79. {
  80. ALfloat temps[64][4];
  81. ALsizei td = mini(64, SamplesToDo-base);
  82. /* Load samples into the temp buffer first. */
  83. for(j = 0;j < 4;j++)
  84. {
  85. for(i = 0;i < td;i++)
  86. temps[i][j] = SamplesIn[j][i+base];
  87. }
  88. if(state->Enabled)
  89. {
  90. ALfloat gain = state->GainCtrl;
  91. ALfloat output, amplitude;
  92. for(i = 0;i < td;i++)
  93. {
  94. /* Roughly calculate the maximum amplitude from the 4-channel
  95. * signal, and attack or release the gain control to reach it.
  96. */
  97. amplitude = fabsf(temps[i][0]);
  98. amplitude = maxf(amplitude + fabsf(temps[i][1]),
  99. maxf(amplitude + fabsf(temps[i][2]),
  100. amplitude + fabsf(temps[i][3])));
  101. if(amplitude > gain)
  102. gain = minf(gain+state->AttackRate, amplitude);
  103. else if(amplitude < gain)
  104. gain = maxf(gain-state->ReleaseRate, amplitude);
  105. /* Apply the inverse of the gain control to normalize/compress
  106. * the volume. */
  107. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  108. for(j = 0;j < 4;j++)
  109. temps[i][j] *= output;
  110. }
  111. state->GainCtrl = gain;
  112. }
  113. else
  114. {
  115. ALfloat gain = state->GainCtrl;
  116. ALfloat output, amplitude;
  117. for(i = 0;i < td;i++)
  118. {
  119. /* Same as above, except the amplitude is forced to 1. This
  120. * helps ensure smooth gain changes when the compressor is
  121. * turned on and off.
  122. */
  123. amplitude = 1.0f;
  124. if(amplitude > gain)
  125. gain = minf(gain+state->AttackRate, amplitude);
  126. else if(amplitude < gain)
  127. gain = maxf(gain-state->ReleaseRate, amplitude);
  128. output = 1.0f / clampf(gain, 0.5f, 2.0f);
  129. for(j = 0;j < 4;j++)
  130. temps[i][j] *= output;
  131. }
  132. state->GainCtrl = gain;
  133. }
  134. /* Now mix to the output. */
  135. for(j = 0;j < 4;j++)
  136. {
  137. for(k = 0;k < NumChannels;k++)
  138. {
  139. ALfloat gain = state->Gain[j][k];
  140. if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
  141. continue;
  142. for(i = 0;i < td;i++)
  143. SamplesOut[k][base+i] += gain * temps[i][j];
  144. }
  145. }
  146. base += td;
  147. }
  148. }
  149. typedef struct CompressorStateFactory {
  150. DERIVE_FROM_TYPE(EffectStateFactory);
  151. } CompressorStateFactory;
  152. static ALeffectState *CompressorStateFactory_create(CompressorStateFactory *UNUSED(factory))
  153. {
  154. ALcompressorState *state;
  155. NEW_OBJ0(state, ALcompressorState)();
  156. if(!state) return NULL;
  157. return STATIC_CAST(ALeffectState, state);
  158. }
  159. DEFINE_EFFECTSTATEFACTORY_VTABLE(CompressorStateFactory);
  160. EffectStateFactory *CompressorStateFactory_getFactory(void)
  161. {
  162. static CompressorStateFactory CompressorFactory = { { GET_VTABLE2(CompressorStateFactory, EffectStateFactory) } };
  163. return STATIC_CAST(EffectStateFactory, &CompressorFactory);
  164. }
  165. void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
  166. {
  167. ALeffectProps *props = &effect->Props;
  168. switch(param)
  169. {
  170. case AL_COMPRESSOR_ONOFF:
  171. if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
  172. SETERR_RETURN(context, AL_INVALID_VALUE,, "Compressor state out of range");
  173. props->Compressor.OnOff = val;
  174. break;
  175. default:
  176. alSetError(context, AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
  177. param);
  178. }
  179. }
  180. void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
  181. { ALcompressor_setParami(effect, context, param, vals[0]); }
  182. void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat UNUSED(val))
  183. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param); }
  184. void ALcompressor_setParamfv(ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, const ALfloat *UNUSED(vals))
  185. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x", param); }
  186. void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
  187. {
  188. const ALeffectProps *props = &effect->Props;
  189. switch(param)
  190. {
  191. case AL_COMPRESSOR_ONOFF:
  192. *val = props->Compressor.OnOff;
  193. break;
  194. default:
  195. alSetError(context, AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
  196. param);
  197. }
  198. }
  199. void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
  200. { ALcompressor_getParami(effect, context, param, vals); }
  201. void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat *UNUSED(val))
  202. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param); }
  203. void ALcompressor_getParamfv(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum param, ALfloat *UNUSED(vals))
  204. { alSetError(context, AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x", param); }
  205. DEFINE_ALEFFECT_VTABLE(ALcompressor);