Make a function static that's only used in one source file
[openal-soft.git] / Alc / effects / compressor.c
blob4ae2acdbee2220ab5843f8804801b9bb150e84f0
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.
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.
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
21 #include <stdlib.h>
23 #include "config.h"
24 #include "alError.h"
25 #include "alMain.h"
26 #include "alAuxEffectSlot.h"
27 #include "alu.h"
30 typedef struct ALcompressorState {
31 DERIVE_FROM_TYPE(ALeffectState);
33 /* Effect gains for each channel */
34 ALfloat Gain[MAX_EFFECT_CHANNELS][MAX_OUTPUT_CHANNELS];
36 /* Effect parameters */
37 ALboolean Enabled;
38 ALfloat AttackRate;
39 ALfloat ReleaseRate;
40 ALfloat GainCtrl;
41 } ALcompressorState;
43 static ALvoid ALcompressorState_Destruct(ALcompressorState *state);
44 static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device);
45 static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props);
46 static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels);
47 DECLARE_DEFAULT_ALLOCATORS(ALcompressorState)
49 DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
52 static void ALcompressorState_Construct(ALcompressorState *state)
54 ALeffectState_Construct(STATIC_CAST(ALeffectState, state));
55 SET_VTABLE2(ALcompressorState, ALeffectState, state);
57 state->Enabled = AL_TRUE;
58 state->AttackRate = 0.0f;
59 state->ReleaseRate = 0.0f;
60 state->GainCtrl = 1.0f;
63 static ALvoid ALcompressorState_Destruct(ALcompressorState *state)
65 ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
68 static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device)
70 const ALfloat attackTime = device->Frequency * 0.2f; /* 200ms Attack */
71 const ALfloat releaseTime = device->Frequency * 0.4f; /* 400ms Release */
73 state->AttackRate = 1.0f / attackTime;
74 state->ReleaseRate = 1.0f / releaseTime;
76 return AL_TRUE;
79 static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCcontext *context, const ALeffectslot *slot, const ALeffectProps *props)
81 const ALCdevice *device = context->Device;
82 ALuint i;
84 state->Enabled = props->Compressor.OnOff;
86 STATIC_CAST(ALeffectState,state)->OutBuffer = device->FOAOut.Buffer;
87 STATIC_CAST(ALeffectState,state)->OutChannels = device->FOAOut.NumChannels;
88 for(i = 0;i < 4;i++)
89 ComputeFirstOrderGains(&device->FOAOut, IdentityMatrixf.m[i],
90 slot->Params.Gain, state->Gain[i]);
93 static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
95 ALsizei i, j, k;
96 ALsizei base;
98 for(base = 0;base < SamplesToDo;)
100 ALfloat temps[64][4];
101 ALsizei td = mini(64, SamplesToDo-base);
103 /* Load samples into the temp buffer first. */
104 for(j = 0;j < 4;j++)
106 for(i = 0;i < td;i++)
107 temps[i][j] = SamplesIn[j][i+base];
110 if(state->Enabled)
112 ALfloat gain = state->GainCtrl;
113 ALfloat output, amplitude;
115 for(i = 0;i < td;i++)
117 /* Roughly calculate the maximum amplitude from the 4-channel
118 * signal, and attack or release the gain control to reach it.
120 amplitude = fabsf(temps[i][0]);
121 amplitude = maxf(amplitude + fabsf(temps[i][1]),
122 maxf(amplitude + fabsf(temps[i][2]),
123 amplitude + fabsf(temps[i][3])));
124 if(amplitude > gain)
125 gain = minf(gain+state->AttackRate, amplitude);
126 else if(amplitude < gain)
127 gain = maxf(gain-state->ReleaseRate, amplitude);
129 /* Apply the inverse of the gain control to normalize/compress
130 * the volume. */
131 output = 1.0f / clampf(gain, 0.5f, 2.0f);
132 for(j = 0;j < 4;j++)
133 temps[i][j] *= output;
136 state->GainCtrl = gain;
138 else
140 ALfloat gain = state->GainCtrl;
141 ALfloat output, amplitude;
143 for(i = 0;i < td;i++)
145 /* Same as above, except the amplitude is forced to 1. This
146 * helps ensure smooth gain changes when the compressor is
147 * turned on and off.
149 amplitude = 1.0f;
150 if(amplitude > gain)
151 gain = minf(gain+state->AttackRate, amplitude);
152 else if(amplitude < gain)
153 gain = maxf(gain-state->ReleaseRate, amplitude);
155 output = 1.0f / clampf(gain, 0.5f, 2.0f);
156 for(j = 0;j < 4;j++)
157 temps[i][j] *= output;
160 state->GainCtrl = gain;
163 /* Now mix to the output. */
164 for(j = 0;j < 4;j++)
166 for(k = 0;k < NumChannels;k++)
168 ALfloat gain = state->Gain[j][k];
169 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
170 continue;
172 for(i = 0;i < td;i++)
173 SamplesOut[k][base+i] += gain * temps[i][j];
177 base += td;
182 typedef struct ALcompressorStateFactory {
183 DERIVE_FROM_TYPE(ALeffectStateFactory);
184 } ALcompressorStateFactory;
186 static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
188 ALcompressorState *state;
190 NEW_OBJ0(state, ALcompressorState)();
191 if(!state) return NULL;
193 return STATIC_CAST(ALeffectState, state);
196 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
198 ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
200 static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
202 return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
206 void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
208 ALeffectProps *props = &effect->Props;
209 switch(param)
211 case AL_COMPRESSOR_ONOFF:
212 if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
213 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
214 props->Compressor.OnOff = val;
215 break;
217 default:
218 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
221 void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
223 ALcompressor_setParami(effect, context, param, vals[0]);
225 void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
226 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
227 void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
229 ALcompressor_setParamf(effect, context, param, vals[0]);
232 void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
234 const ALeffectProps *props = &effect->Props;
235 switch(param)
237 case AL_COMPRESSOR_ONOFF:
238 *val = props->Compressor.OnOff;
239 break;
240 default:
241 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
244 void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
246 ALcompressor_getParami(effect, context, param, vals);
248 void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
249 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
250 void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
252 ALcompressor_getParamf(effect, context, param, vals);
255 DEFINE_ALEFFECT_VTABLE(ALcompressor);