Add 'restrict' to another parameter
[openal-soft.git] / Alc / effects / compressor.c
blob6329d3f1f9ef7a44d9009ba159686ab97c656c40
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)
45 ALeffectState_Destruct(STATIC_CAST(ALeffectState,state));
48 static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device)
50 const ALfloat attackTime = device->Frequency * 0.2f; /* 200ms Attack */
51 const ALfloat releaseTime = device->Frequency * 0.4f; /* 400ms Release */
53 state->AttackRate = 1.0f / attackTime;
54 state->ReleaseRate = 1.0f / releaseTime;
56 return AL_TRUE;
59 static ALvoid ALcompressorState_update(ALcompressorState *state, const ALCdevice *device, const ALeffectslot *slot, const ALeffectProps *props)
61 aluMatrixf matrix;
62 ALuint i;
64 state->Enabled = props->Compressor.OnOff;
66 aluMatrixfSet(&matrix,
67 1.0f, 0.0f, 0.0f, 0.0f,
68 0.0f, 1.0f, 0.0f, 0.0f,
69 0.0f, 0.0f, 1.0f, 0.0f,
70 0.0f, 0.0f, 0.0f, 1.0f
73 STATIC_CAST(ALeffectState,state)->OutBuffer = device->FOAOut.Buffer;
74 STATIC_CAST(ALeffectState,state)->OutChannels = device->FOAOut.NumChannels;
75 for(i = 0;i < 4;i++)
76 ComputeFirstOrderGains(device->FOAOut, matrix.m[i], slot->Params.Gain,
77 state->Gain[i]);
80 static ALvoid ALcompressorState_process(ALcompressorState *state, ALuint SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALuint NumChannels)
82 ALuint i, j, k;
83 ALuint base;
85 for(base = 0;base < SamplesToDo;)
87 ALfloat temps[64][4];
88 ALuint td = minu(64, SamplesToDo-base);
90 /* Load samples into the temp buffer first. */
91 for(j = 0;j < 4;j++)
93 for(i = 0;i < td;i++)
94 temps[i][j] = SamplesIn[j][i+base];
97 if(state->Enabled)
99 ALfloat gain = state->GainCtrl;
100 ALfloat output, amplitude;
102 for(i = 0;i < td;i++)
104 /* Roughly calculate the maximum amplitude from the 4-channel
105 * signal, and attack or release the gain control to reach it.
107 amplitude = fabsf(temps[i][0]);
108 amplitude = maxf(amplitude + fabsf(temps[i][1]),
109 maxf(amplitude + fabsf(temps[i][2]),
110 amplitude + fabsf(temps[i][3])));
111 if(amplitude > gain)
112 gain = minf(gain+state->AttackRate, amplitude);
113 else if(amplitude < gain)
114 gain = maxf(gain-state->ReleaseRate, amplitude);
116 /* Apply the inverse of the gain control to normalize/compress
117 * the volume. */
118 output = 1.0f / clampf(gain, 0.5f, 2.0f);
119 for(j = 0;j < 4;j++)
120 temps[i][j] *= output;
123 state->GainCtrl = gain;
125 else
127 ALfloat gain = state->GainCtrl;
128 ALfloat output, amplitude;
130 for(i = 0;i < td;i++)
132 /* Same as above, except the amplitude is forced to 1. This
133 * helps ensure smooth gain changes when the compressor is
134 * turned on and off.
136 amplitude = 1.0f;
137 if(amplitude > gain)
138 gain = minf(gain+state->AttackRate, amplitude);
139 else if(amplitude < gain)
140 gain = maxf(gain-state->ReleaseRate, amplitude);
142 output = 1.0f / clampf(gain, 0.5f, 2.0f);
143 for(j = 0;j < 4;j++)
144 temps[i][j] *= output;
147 state->GainCtrl = gain;
150 /* Now mix to the output. */
151 for(j = 0;j < 4;j++)
153 for(k = 0;k < NumChannels;k++)
155 ALfloat gain = state->Gain[j][k];
156 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
157 continue;
159 for(i = 0;i < td;i++)
160 SamplesOut[k][base+i] += gain * temps[i][j];
164 base += td;
168 DECLARE_DEFAULT_ALLOCATORS(ALcompressorState)
170 DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
173 typedef struct ALcompressorStateFactory {
174 DERIVE_FROM_TYPE(ALeffectStateFactory);
175 } ALcompressorStateFactory;
177 static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
179 ALcompressorState *state;
181 state = ALcompressorState_New(sizeof(*state));
182 if(!state) return NULL;
183 SET_VTABLE2(ALcompressorState, ALeffectState, state);
185 state->Enabled = AL_TRUE;
186 state->AttackRate = 0.0f;
187 state->ReleaseRate = 0.0f;
188 state->GainCtrl = 1.0f;
190 return STATIC_CAST(ALeffectState, state);
193 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
195 ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
197 static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
199 return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
203 void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
205 ALeffectProps *props = &effect->Props;
206 switch(param)
208 case AL_COMPRESSOR_ONOFF:
209 if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
210 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
211 props->Compressor.OnOff = val;
212 break;
214 default:
215 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
218 void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
220 ALcompressor_setParami(effect, context, param, vals[0]);
222 void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
223 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
224 void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
226 ALcompressor_setParamf(effect, context, param, vals[0]);
229 void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
231 const ALeffectProps *props = &effect->Props;
232 switch(param)
234 case AL_COMPRESSOR_ONOFF:
235 *val = props->Compressor.OnOff;
236 break;
237 default:
238 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
241 void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
243 ALcompressor_getParami(effect, context, param, vals);
245 void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
246 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
247 void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
249 ALcompressor_getParamf(effect, context, param, vals);
252 DEFINE_ALEFFECT_VTABLE(ALcompressor);