Move some temp variables closer to where they're used
[openal-soft.git] / alc / effects / compressor.cpp
blob366f22753a9e79f760583a9503adcf9b3e6f389a
1 /**
2 * This file is part of the OpenAL Soft cross platform audio library
4 * Copyright (C) 2013 by Anis A. Hireche
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
16 * * Neither the name of Spherical-Harmonic-Transform nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include "config.h"
35 #include <array>
36 #include <cstdlib>
37 #include <iterator>
38 #include <utility>
40 #include "alc/effects/base.h"
41 #include "almalloc.h"
42 #include "alnumeric.h"
43 #include "alspan.h"
44 #include "core/ambidefs.h"
45 #include "core/bufferline.h"
46 #include "core/devformat.h"
47 #include "core/device.h"
48 #include "core/effectslot.h"
49 #include "core/mixer.h"
50 #include "core/mixer/defs.h"
51 #include "intrusive_ptr.h"
53 struct ContextBase;
56 namespace {
58 #define AMP_ENVELOPE_MIN 0.5f
59 #define AMP_ENVELOPE_MAX 2.0f
61 #define ATTACK_TIME 0.1f /* 100ms to rise from min to max */
62 #define RELEASE_TIME 0.2f /* 200ms to drop from max to min */
65 struct CompressorState final : public EffectState {
66 /* Effect gains for each channel */
67 float mGain[MaxAmbiChannels][MAX_OUTPUT_CHANNELS]{};
69 /* Effect parameters */
70 bool mEnabled{true};
71 float mAttackMult{1.0f};
72 float mReleaseMult{1.0f};
73 float mEnvFollower{1.0f};
76 void deviceUpdate(const DeviceBase *device, const Buffer &buffer) override;
77 void update(const ContextBase *context, const EffectSlot *slot, const EffectProps *props,
78 const EffectTarget target) override;
79 void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
80 const al::span<FloatBufferLine> samplesOut) override;
82 DEF_NEWDEL(CompressorState)
85 void CompressorState::deviceUpdate(const DeviceBase *device, const Buffer&)
87 /* Number of samples to do a full attack and release (non-integer sample
88 * counts are okay).
90 const float attackCount{static_cast<float>(device->Frequency) * ATTACK_TIME};
91 const float releaseCount{static_cast<float>(device->Frequency) * RELEASE_TIME};
93 /* Calculate per-sample multipliers to attack and release at the desired
94 * rates.
96 mAttackMult = std::pow(AMP_ENVELOPE_MAX/AMP_ENVELOPE_MIN, 1.0f/attackCount);
97 mReleaseMult = std::pow(AMP_ENVELOPE_MIN/AMP_ENVELOPE_MAX, 1.0f/releaseCount);
100 void CompressorState::update(const ContextBase*, const EffectSlot *slot,
101 const EffectProps *props, const EffectTarget target)
103 mEnabled = props->Compressor.OnOff;
105 mOutTarget = target.Main->Buffer;
106 auto set_gains = [slot,target](auto &gains, al::span<const float,MaxAmbiChannels> coeffs)
107 { ComputePanGains(target.Main, coeffs.data(), slot->Gain, gains); };
108 SetAmbiPanIdentity(std::begin(mGain), slot->Wet.Buffer.size(), set_gains);
111 void CompressorState::process(const size_t samplesToDo,
112 const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
114 for(size_t base{0u};base < samplesToDo;)
116 float gains[256];
117 const size_t td{minz(256, samplesToDo-base)};
119 /* Generate the per-sample gains from the signal envelope. */
120 float env{mEnvFollower};
121 if(mEnabled)
123 for(size_t i{0u};i < td;++i)
125 /* Clamp the absolute amplitude to the defined envelope limits,
126 * then attack or release the envelope to reach it.
128 const float amplitude{clampf(std::fabs(samplesIn[0][base+i]), AMP_ENVELOPE_MIN,
129 AMP_ENVELOPE_MAX)};
130 if(amplitude > env)
131 env = minf(env*mAttackMult, amplitude);
132 else if(amplitude < env)
133 env = maxf(env*mReleaseMult, amplitude);
135 /* Apply the reciprocal of the envelope to normalize the volume
136 * (compress the dynamic range).
138 gains[i] = 1.0f / env;
141 else
143 /* Same as above, except the amplitude is forced to 1. This helps
144 * ensure smooth gain changes when the compressor is turned on and
145 * off.
147 for(size_t i{0u};i < td;++i)
149 const float amplitude{1.0f};
150 if(amplitude > env)
151 env = minf(env*mAttackMult, amplitude);
152 else if(amplitude < env)
153 env = maxf(env*mReleaseMult, amplitude);
155 gains[i] = 1.0f / env;
158 mEnvFollower = env;
160 /* Now compress the signal amplitude to output. */
161 auto changains = std::addressof(mGain[0]);
162 for(const auto &input : samplesIn)
164 const float *outgains{*(changains++)};
165 for(FloatBufferLine &output : samplesOut)
167 const float gain{*(outgains++)};
168 if(!(std::fabs(gain) > GainSilenceThreshold))
169 continue;
171 for(size_t i{0u};i < td;i++)
172 output[base+i] += input[base+i] * gains[i] * gain;
176 base += td;
181 struct CompressorStateFactory final : public EffectStateFactory {
182 al::intrusive_ptr<EffectState> create() override
183 { return al::intrusive_ptr<EffectState>{new CompressorState{}}; }
186 } // namespace
188 EffectStateFactory *CompressorStateFactory_getFactory()
190 static CompressorStateFactory CompressorFactory{};
191 return &CompressorFactory;