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_EFFECT_CHANNELS
][MAX_OUTPUT_CHANNELS
];
36 /* Effect parameters */
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
;
59 static ALvoid
ALcompressorState_update(ALcompressorState
*state
, const ALCdevice
*device
, const ALeffectslot
*slot
, const ALeffectProps
*props
)
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
;
76 ComputeFirstOrderGains(device
->FOAOut
, matrix
.m
[i
], slot
->Params
.Gain
,
80 static ALvoid
ALcompressorState_process(ALcompressorState
*state
, ALuint SamplesToDo
, const ALfloat (*restrict SamplesIn
)[BUFFERSIZE
], ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
85 for(base
= 0;base
< SamplesToDo
;)
88 ALuint td
= minu(64, SamplesToDo
-base
);
90 /* Load samples into the temp buffer first. */
94 temps
[i
][j
] = SamplesIn
[j
][i
+base
];
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])));
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
118 output
= 1.0f
/ clampf(gain
, 0.5f
, 2.0f
);
120 temps
[i
][j
] *= output
;
123 state
->GainCtrl
= gain
;
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
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
);
144 temps
[i
][j
] *= output
;
147 state
->GainCtrl
= gain
;
150 /* Now mix to the output. */
153 for(k
= 0;k
< NumChannels
;k
++)
155 ALfloat gain
= state
->Gain
[j
][k
];
156 if(!(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
))
159 for(i
= 0;i
< td
;i
++)
160 SamplesOut
[k
][base
+i
] += gain
* temps
[i
][j
];
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
;
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
;
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
;
234 case AL_COMPRESSOR_ONOFF
:
235 *val
= props
->Compressor
.OnOff
;
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
);