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
26 #include "alAuxEffectSlot.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 */
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
;
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
)
70 for(base
= 0;base
< SamplesToDo
;)
73 ALuint td
= minu(256, SamplesToDo
-base
);
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
);
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
;
98 ALfloat output
, smp
, amplitude
;
99 ALfloat gain
= state
->GainCtrl
;
101 for(it
= 0;it
< td
;it
++)
103 smp
= SamplesIn
[it
+base
];
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
))
125 for(it
= 0;it
< td
;it
++)
126 SamplesOut
[kt
][base
+it
] += gain
* temps
[it
];
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
;
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
;
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
;
199 case AL_COMPRESSOR_ONOFF
:
200 *val
= props
->Compressor
.OnOff
;
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
);