Remove extraneous parenthesis
[openal-soft.git] / alc / effects / compressor.cpp
blob6724482e2f2419c0d6d364af86024043857ff51f
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 "config.h"
23 #include <cstdlib>
25 #include "al/auxeffectslot.h"
26 #include "alcmain.h"
27 #include "alcontext.h"
28 #include "alu.h"
29 #include "vecmat.h"
32 namespace {
34 #define AMP_ENVELOPE_MIN 0.5f
35 #define AMP_ENVELOPE_MAX 2.0f
37 #define ATTACK_TIME 0.1f /* 100ms to rise from min to max */
38 #define RELEASE_TIME 0.2f /* 200ms to drop from max to min */
41 struct CompressorState final : public EffectState {
42 /* Effect gains for each channel */
43 float mGain[MAX_AMBI_CHANNELS][MAX_OUTPUT_CHANNELS]{};
45 /* Effect parameters */
46 bool mEnabled{true};
47 float mAttackMult{1.0f};
48 float mReleaseMult{1.0f};
49 float mEnvFollower{1.0f};
52 void deviceUpdate(const ALCdevice *device) override;
53 void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
54 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut) override;
56 DEF_NEWDEL(CompressorState)
59 void CompressorState::deviceUpdate(const ALCdevice *device)
61 /* Number of samples to do a full attack and release (non-integer sample
62 * counts are okay).
64 const float attackCount{static_cast<float>(device->Frequency) * ATTACK_TIME};
65 const float releaseCount{static_cast<float>(device->Frequency) * RELEASE_TIME};
67 /* Calculate per-sample multipliers to attack and release at the desired
68 * rates.
70 mAttackMult = std::pow(AMP_ENVELOPE_MAX/AMP_ENVELOPE_MIN, 1.0f/attackCount);
71 mReleaseMult = std::pow(AMP_ENVELOPE_MIN/AMP_ENVELOPE_MAX, 1.0f/releaseCount);
74 void CompressorState::update(const ALCcontext*, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
76 mEnabled = props->Compressor.OnOff;
78 mOutTarget = target.Main->Buffer;
79 auto set_gains = [slot,target](auto &gains, al::span<const float,MAX_AMBI_CHANNELS> coeffs)
80 { ComputePanGains(target.Main, coeffs.data(), slot->Params.Gain, gains); };
81 SetAmbiPanIdentity(std::begin(mGain), slot->Wet.Buffer.size(), set_gains);
84 void CompressorState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
86 for(size_t base{0u};base < samplesToDo;)
88 float gains[256];
89 const size_t td{minz(256, samplesToDo-base)};
91 /* Generate the per-sample gains from the signal envelope. */
92 float env{mEnvFollower};
93 if(mEnabled)
95 for(size_t i{0u};i < td;++i)
97 /* Clamp the absolute amplitude to the defined envelope limits,
98 * then attack or release the envelope to reach it.
100 const float amplitude{clampf(std::fabs(samplesIn[0][base+i]), AMP_ENVELOPE_MIN,
101 AMP_ENVELOPE_MAX)};
102 if(amplitude > env)
103 env = minf(env*mAttackMult, amplitude);
104 else if(amplitude < env)
105 env = maxf(env*mReleaseMult, amplitude);
107 /* Apply the reciprocal of the envelope to normalize the volume
108 * (compress the dynamic range).
110 gains[i] = 1.0f / env;
113 else
115 /* Same as above, except the amplitude is forced to 1. This helps
116 * ensure smooth gain changes when the compressor is turned on and
117 * off.
119 for(size_t i{0u};i < td;++i)
121 const float amplitude{1.0f};
122 if(amplitude > env)
123 env = minf(env*mAttackMult, amplitude);
124 else if(amplitude < env)
125 env = maxf(env*mReleaseMult, amplitude);
127 gains[i] = 1.0f / env;
130 mEnvFollower = env;
132 /* Now compress the signal amplitude to output. */
133 auto changains = std::addressof(mGain[0]);
134 for(const auto &input : samplesIn)
136 const float *outgains{*(changains++)};
137 for(FloatBufferLine &output : samplesOut)
139 const float gain{*(outgains++)};
140 if(!(std::fabs(gain) > GAIN_SILENCE_THRESHOLD))
141 continue;
143 for(size_t i{0u};i < td;i++)
144 output[base+i] += input[base+i] * gains[i] * gain;
148 base += td;
153 void Compressor_setParami(EffectProps *props, ALenum param, int val)
155 switch(param)
157 case AL_COMPRESSOR_ONOFF:
158 if(!(val >= AL_COMPRESSOR_MIN_ONOFF && val <= AL_COMPRESSOR_MAX_ONOFF))
159 throw effect_exception{AL_INVALID_VALUE, "Compressor state out of range"};
160 props->Compressor.OnOff = (val != AL_FALSE);
161 break;
163 default:
164 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
165 param};
168 void Compressor_setParamiv(EffectProps *props, ALenum param, const int *vals)
169 { Compressor_setParami(props, param, vals[0]); }
170 void Compressor_setParamf(EffectProps*, ALenum param, float)
171 { throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param}; }
172 void Compressor_setParamfv(EffectProps*, ALenum param, const float*)
174 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x",
175 param};
178 void Compressor_getParami(const EffectProps *props, ALenum param, int *val)
180 switch(param)
182 case AL_COMPRESSOR_ONOFF:
183 *val = props->Compressor.OnOff;
184 break;
186 default:
187 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor integer property 0x%04x",
188 param};
191 void Compressor_getParamiv(const EffectProps *props, ALenum param, int *vals)
192 { Compressor_getParami(props, param, vals); }
193 void Compressor_getParamf(const EffectProps*, ALenum param, float*)
194 { throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float property 0x%04x", param}; }
195 void Compressor_getParamfv(const EffectProps*, ALenum param, float*)
197 throw effect_exception{AL_INVALID_ENUM, "Invalid compressor float-vector property 0x%04x",
198 param};
201 DEFINE_ALEFFECT_VTABLE(Compressor);
204 struct CompressorStateFactory final : public EffectStateFactory {
205 EffectState *create() override { return new CompressorState{}; }
206 EffectProps getDefaultProps() const noexcept override;
207 const EffectVtable *getEffectVtable() const noexcept override { return &Compressor_vtable; }
210 EffectProps CompressorStateFactory::getDefaultProps() const noexcept
212 EffectProps props{};
213 props.Compressor.OnOff = AL_COMPRESSOR_DEFAULT_ONOFF;
214 return props;
217 } // namespace
219 EffectStateFactory *CompressorStateFactory_getFactory()
221 static CompressorStateFactory CompressorFactory{};
222 return &CompressorFactory;