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
*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
, const ALCdevice
*device
, const ALeffectslot
*slot
)
64 state
->Enabled
= slot
->EffectProps
.Compressor
.OnOff
;
66 scale
= device
->Dry
.AmbiScale
;
67 aluMatrixfSet(&matrix
,
68 1.0f
, 0.0f
, 0.0f
, 0.0f
,
69 0.0f
, scale
, 0.0f
, 0.0f
,
70 0.0f
, 0.0f
, scale
, 0.0f
,
71 0.0f
, 0.0f
, 0.0f
, scale
74 ComputeFirstOrderGains(device
->Dry
.AmbiCoeffs
, device
->Dry
.NumChannels
,
75 matrix
.m
[i
], slot
->Gain
, state
->Gain
[i
]);
78 static ALvoid
ALcompressorState_process(ALcompressorState
*state
, ALuint SamplesToDo
, const ALfloat (*restrict SamplesIn
)[BUFFERSIZE
], ALfloat (*restrict SamplesOut
)[BUFFERSIZE
], ALuint NumChannels
)
83 for(base
= 0;base
< SamplesToDo
;)
86 ALuint td
= minu(64, SamplesToDo
-base
);
88 /* Load samples into the temp buffer first. */
92 temps
[i
][j
] = SamplesIn
[j
][i
+base
];
97 ALfloat gain
= state
->GainCtrl
;
98 ALfloat output
, amplitude
;
100 for(i
= 0;i
< td
;i
++)
102 /* Roughly calculate the maximum amplitude from the 4-channel
103 * signal, and attack or release the gain control to reach it.
105 amplitude
= fabsf(temps
[i
][0]);
106 amplitude
= maxf(amplitude
+ fabsf(temps
[i
][1]),
107 maxf(amplitude
+ fabsf(temps
[i
][2]),
108 amplitude
+ fabsf(temps
[i
][3])));
110 gain
= minf(gain
+state
->AttackRate
, amplitude
);
111 else if(amplitude
< gain
)
112 gain
= maxf(gain
-state
->ReleaseRate
, amplitude
);
114 /* Apply the inverse of the gain control to normalize/compress
116 output
= 1.0f
/ clampf(gain
, 0.5f
, 2.0f
);
118 temps
[i
][j
] *= output
;
121 state
->GainCtrl
= gain
;
125 ALfloat gain
= state
->GainCtrl
;
126 ALfloat output
, amplitude
;
128 for(i
= 0;i
< td
;i
++)
130 /* Same as above, except the amplitude is forced to 1. This
131 * helps ensure smooth gain changes when the compressor is
136 gain
= minf(gain
+state
->AttackRate
, amplitude
);
137 else if(amplitude
< gain
)
138 gain
= maxf(gain
-state
->ReleaseRate
, amplitude
);
140 output
= 1.0f
/ clampf(gain
, 0.5f
, 2.0f
);
142 temps
[i
][j
] *= output
;
145 state
->GainCtrl
= gain
;
148 /* Now mix to the output. */
151 for(k
= 0;k
< NumChannels
;k
++)
153 ALfloat gain
= state
->Gain
[j
][k
];
154 if(!(fabsf(gain
) > GAIN_SILENCE_THRESHOLD
))
157 for(i
= 0;i
< td
;i
++)
158 SamplesOut
[k
][base
+i
] += gain
* temps
[i
][j
];
166 DECLARE_DEFAULT_ALLOCATORS(ALcompressorState
)
168 DEFINE_ALEFFECTSTATE_VTABLE(ALcompressorState
);
171 typedef struct ALcompressorStateFactory
{
172 DERIVE_FROM_TYPE(ALeffectStateFactory
);
173 } ALcompressorStateFactory
;
175 static ALeffectState
*ALcompressorStateFactory_create(ALcompressorStateFactory
*UNUSED(factory
))
177 ALcompressorState
*state
;
179 state
= ALcompressorState_New(sizeof(*state
));
180 if(!state
) return NULL
;
181 SET_VTABLE2(ALcompressorState
, ALeffectState
, state
);
183 state
->Enabled
= AL_TRUE
;
184 state
->AttackRate
= 0.0f
;
185 state
->ReleaseRate
= 0.0f
;
186 state
->GainCtrl
= 1.0f
;
188 return STATIC_CAST(ALeffectState
, state
);
191 DEFINE_ALEFFECTSTATEFACTORY_VTABLE(ALcompressorStateFactory
);
193 ALeffectStateFactory
*ALcompressorStateFactory_getFactory(void)
195 static ALcompressorStateFactory CompressorFactory
= { { GET_VTABLE2(ALcompressorStateFactory
, ALeffectStateFactory
) } };
197 return STATIC_CAST(ALeffectStateFactory
, &CompressorFactory
);
201 void ALcompressor_setParami(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint val
)
203 ALeffectProps
*props
= &effect
->Props
;
206 case AL_COMPRESSOR_ONOFF
:
207 if(!(val
>= AL_COMPRESSOR_MIN_ONOFF
&& val
<= AL_COMPRESSOR_MAX_ONOFF
))
208 SET_ERROR_AND_RETURN(context
, AL_INVALID_VALUE
);
209 props
->Compressor
.OnOff
= val
;
213 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
216 void ALcompressor_setParamiv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALint
*vals
)
218 ALcompressor_setParami(effect
, context
, param
, vals
[0]);
220 void ALcompressor_setParamf(ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum
UNUSED(param
), ALfloat
UNUSED(val
))
221 { SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
); }
222 void ALcompressor_setParamfv(ALeffect
*effect
, ALCcontext
*context
, ALenum param
, const ALfloat
*vals
)
224 ALcompressor_setParamf(effect
, context
, param
, vals
[0]);
227 void ALcompressor_getParami(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*val
)
229 const ALeffectProps
*props
= &effect
->Props
;
232 case AL_COMPRESSOR_ONOFF
:
233 *val
= props
->Compressor
.OnOff
;
236 SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
);
239 void ALcompressor_getParamiv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALint
*vals
)
241 ALcompressor_getParami(effect
, context
, param
, vals
);
243 void ALcompressor_getParamf(const ALeffect
*UNUSED(effect
), ALCcontext
*context
, ALenum
UNUSED(param
), ALfloat
*UNUSED(val
))
244 { SET_ERROR_AND_RETURN(context
, AL_INVALID_ENUM
); }
245 void ALcompressor_getParamfv(const ALeffect
*effect
, ALCcontext
*context
, ALenum param
, ALfloat
*vals
)
247 ALcompressor_getParamf(effect
, context
, param
, vals
);
250 DEFINE_ALEFFECT_VTABLE(ALcompressor
);