Use a weaker memory order for the current context iface
[openal-soft.git] / Alc / effects / compressor.c
blob25c7a3dc1ccc89e05cd442d53dc9fa144620d490
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 ALCdevice *device, 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 ALCdevice *device, const ALeffectslot *slot, const ALeffectProps *props)
81 ALuint i;
83 state->Enabled = props->Compressor.OnOff;
85 STATIC_CAST(ALeffectState,state)->OutBuffer = device->FOAOut.Buffer;
86 STATIC_CAST(ALeffectState,state)->OutChannels = device->FOAOut.NumChannels;
87 for(i = 0;i < 4;i++)
88 ComputeFirstOrderGains(device->FOAOut, IdentityMatrixf.m[i],
89 slot->Params.Gain, state->Gain[i]);
92 static ALvoid ALcompressorState_process(ALcompressorState *state, ALsizei SamplesToDo, const ALfloat (*restrict SamplesIn)[BUFFERSIZE], ALfloat (*restrict SamplesOut)[BUFFERSIZE], ALsizei NumChannels)
94 ALsizei i, j, k;
95 ALsizei base;
97 for(base = 0;base < SamplesToDo;)
99 ALfloat temps[64][4];
100 ALsizei td = mini(64, SamplesToDo-base);
102 /* Load samples into the temp buffer first. */
103 for(j = 0;j < 4;j++)
105 for(i = 0;i < td;i++)
106 temps[i][j] = SamplesIn[j][i+base];
109 if(state->Enabled)
111 ALfloat gain = state->GainCtrl;
112 ALfloat output, amplitude;
114 for(i = 0;i < td;i++)
116 /* Roughly calculate the maximum amplitude from the 4-channel
117 * signal, and attack or release the gain control to reach it.
119 amplitude = fabsf(temps[i][0]);
120 amplitude = maxf(amplitude + fabsf(temps[i][1]),
121 maxf(amplitude + fabsf(temps[i][2]),
122 amplitude + fabsf(temps[i][3])));
123 if(amplitude > gain)
124 gain = minf(gain+state->AttackRate, amplitude);
125 else if(amplitude < gain)
126 gain = maxf(gain-state->ReleaseRate, amplitude);
128 /* Apply the inverse of the gain control to normalize/compress
129 * the volume. */
130 output = 1.0f / clampf(gain, 0.5f, 2.0f);
131 for(j = 0;j < 4;j++)
132 temps[i][j] *= output;
135 state->GainCtrl = gain;
137 else
139 ALfloat gain = state->GainCtrl;
140 ALfloat output, amplitude;
142 for(i = 0;i < td;i++)
144 /* Same as above, except the amplitude is forced to 1. This
145 * helps ensure smooth gain changes when the compressor is
146 * turned on and off.
148 amplitude = 1.0f;
149 if(amplitude > gain)
150 gain = minf(gain+state->AttackRate, amplitude);
151 else if(amplitude < gain)
152 gain = maxf(gain-state->ReleaseRate, amplitude);
154 output = 1.0f / clampf(gain, 0.5f, 2.0f);
155 for(j = 0;j < 4;j++)
156 temps[i][j] *= output;
159 state->GainCtrl = gain;
162 /* Now mix to the output. */
163 for(j = 0;j < 4;j++)
165 for(k = 0;k < NumChannels;k++)
167 ALfloat gain = state->Gain[j][k];
168 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
169 continue;
171 for(i = 0;i < td;i++)
172 SamplesOut[k][base+i] += gain * temps[i][j];
176 base += td;
181 typedef struct ALcompressorStateFactory {
182 DERIVE_FROM_TYPE(ALeffectStateFactory);
183 } ALcompressorStateFactory;
185 static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
187 ALcompressorState *state;
189 NEW_OBJ0(state, ALcompressorState)();
190 if(!state) return NULL;
192 return STATIC_CAST(ALeffectState, state);
195 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
197 ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
199 static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
201 return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
205 void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
207 ALeffectProps *props = &effect->Props;
208 switch(param)
210 case AL_COMPRESSOR_ONOFF:
211 if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
212 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
213 props->Compressor.OnOff = val;
214 break;
216 default:
217 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
220 void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
222 ALcompressor_setParami(effect, context, param, vals[0]);
224 void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
225 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
226 void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
228 ALcompressor_setParamf(effect, context, param, vals[0]);
231 void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
233 const ALeffectProps *props = &effect->Props;
234 switch(param)
236 case AL_COMPRESSOR_ONOFF:
237 *val = props->Compressor.OnOff;
238 break;
239 default:
240 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
243 void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
245 ALcompressor_getParami(effect, context, param, vals);
247 void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
248 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
249 void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
251 ALcompressor_getParamf(effect, context, param, vals);
254 DEFINE_ALEFFECT_VTABLE(ALcompressor);