Cast to the pointer-to-type to increment the buffer
[openal-soft.git] / Alc / effects / compressor.c
blob9859a085044e057819e06055377adcbee4229daa
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_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 *UNUSED(state))
47 static ALboolean ALcompressorState_deviceUpdate(ALcompressorState *state, ALCdevice *device)
49 const ALfloat attackTime = device->Frequency * 0.2f; /* 200ms Attack */
50 const ALfloat releaseTime = device->Frequency * 0.4f; /* 400ms Release */
52 state->AttackRate = 1.0f / attackTime;
53 state->ReleaseRate = 1.0f / releaseTime;
55 return AL_TRUE;
58 static ALvoid ALcompressorState_update(ALcompressorState *state, ALCdevice *device, const ALeffectslot *slot)
60 state->Enabled = slot->EffectProps.Compressor.OnOff;
62 ComputeAmbientGains(device, slot->Gain, state->Gain);
65 static ALvoid ALcompressorState_process(ALcompressorState *state, ALuint SamplesToDo, const ALfloat *SamplesIn, ALfloat (*SamplesOut)[BUFFERSIZE], ALuint NumChannels)
67 ALuint it, kt;
68 ALuint base;
70 for(base = 0;base < SamplesToDo;)
72 ALfloat temps[256];
73 ALuint td = minu(256, SamplesToDo-base);
75 if(state->Enabled)
77 ALfloat output, smp, amplitude;
78 ALfloat gain = state->GainCtrl;
80 for(it = 0;it < td;it++)
82 smp = SamplesIn[it+base];
84 amplitude = fabsf(smp);
85 if(amplitude > gain)
86 gain = minf(gain+state->AttackRate, amplitude);
87 else if(amplitude < gain)
88 gain = maxf(gain-state->ReleaseRate, amplitude);
89 output = 1.0f / clampf(gain, 0.5f, 2.0f);
91 temps[it] = smp * output;
94 state->GainCtrl = gain;
96 else
98 ALfloat output, smp, amplitude;
99 ALfloat gain = state->GainCtrl;
101 for(it = 0;it < td;it++)
103 smp = SamplesIn[it+base];
105 amplitude = 1.0f;
106 if(amplitude > gain)
107 gain = minf(gain+state->AttackRate, amplitude);
108 else if(amplitude < gain)
109 gain = maxf(gain-state->ReleaseRate, amplitude);
110 output = 1.0f / clampf(gain, 0.5f, 2.0f);
112 temps[it] = smp * output;
115 state->GainCtrl = gain;
119 for(kt = 0;kt < NumChannels;kt++)
121 ALfloat gain = state->Gain[kt];
122 if(!(fabsf(gain) > GAIN_SILENCE_THRESHOLD))
123 continue;
125 for(it = 0;it < td;it++)
126 SamplesOut[kt][base+it] += gain * temps[it];
129 base += td;
133 DECLARE_DEFAULT_ALLOCATORS(ALcompressorState)
135 DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState);
138 typedef struct ALcompressorStateFactory {
139 DERIVE_FROM_TYPE(ALeffectStateFactory);
140 } ALcompressorStateFactory;
142 static ALeffectState *ALcompressorStateFactory_create(ALcompressorStateFactory *UNUSED(factory))
144 ALcompressorState *state;
146 state = ALcompressorState_New(sizeof(*state));
147 if(!state) return NULL;
148 SET_VTABLE2(ALcompressorState, ALeffectState, state);
150 state->Enabled = AL_TRUE;
151 state->AttackRate = 0.0f;
152 state->ReleaseRate = 0.0f;
153 state->GainCtrl = 1.0f;
155 return STATIC_CAST(ALeffectState, state);
158 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory);
160 ALeffectStateFactory *ALcompressorStateFactory_getFactory(void)
162 static ALcompressorStateFactory CompressorFactory = { { GET_VTABLE2(ALcompressorStateFactory, ALeffectStateFactory) } };
164 return STATIC_CAST(ALeffectStateFactory, &CompressorFactory);
168 void ALcompressor_setParami(ALeffect *effect, ALCcontext *context, ALenum param, ALint val)
170 ALeffectProps *props = &effect->Props;
171 switch(param)
173 case AL_COMPRESSOR_ONOFF:
174 if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
175 SET_ERROR_AND_RETURN(context, AL_INVALID_VALUE);
176 props->Compressor.OnOff = val;
177 break;
179 default:
180 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
183 void ALcompressor_setParamiv(ALeffect *effect, ALCcontext *context, ALenum param, const ALint *vals)
185 ALcompressor_setParami(effect, context, param, vals[0]);
187 void ALcompressor_setParamf(ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat UNUSED(val))
188 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
189 void ALcompressor_setParamfv(ALeffect *effect, ALCcontext *context, ALenum param, const ALfloat *vals)
191 ALcompressor_setParamf(effect, context, param, vals[0]);
194 void ALcompressor_getParami(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *val)
196 const ALeffectProps *props = &effect->Props;
197 switch(param)
199 case AL_COMPRESSOR_ONOFF:
200 *val = props->Compressor.OnOff;
201 break;
202 default:
203 SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM);
206 void ALcompressor_getParamiv(const ALeffect *effect, ALCcontext *context, ALenum param, ALint *vals)
208 ALcompressor_getParami(effect, context, param, vals);
210 void ALcompressor_getParamf(const ALeffect *UNUSED(effect), ALCcontext *context, ALenum UNUSED(param), ALfloat *UNUSED(val))
211 { SET_ERROR_AND_RETURN(context, AL_INVALID_ENUM); }
212 void ALcompressor_getParamfv(const ALeffect *effect, ALCcontext *context, ALenum param, ALfloat *vals)
214 ALcompressor_getParamf(effect, context, param, vals);
217 DEFINE_ALEFFECT_VTABLE(ALcompressor);