File: GainReductionComputer.cpp

package info (click to toggle)
audacity 3.7.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 134,800 kB
  • sloc: cpp: 366,277; ansic: 198,323; lisp: 7,761; sh: 3,414; python: 1,501; xml: 1,385; perl: 854; makefile: 125
file content (171 lines) | stat: -rw-r--r-- 5,615 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 This file is part of the SimpleCompressor project.
 https://github.com/DanielRudrich/SimpleCompressor
 Copyright (c) 2019 Daniel Rudrich

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, version 3.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "GainReductionComputer.h"
#include "MathApprox.h"

namespace DanielRudrich {
namespace
{
float getSlope(float ratio) { return 1 / ratio - 1; }
float getRatio(float slope) { return 1 / (slope + 1); }
} // namespace

float GainReductionComputer::getCharacteristicSample(float inputLevelInDecibels,
                                                     float kneeInDecibels,
                                                     float thresholdInDecibels,
                                                     float ratio,
                                                     float makeUpGainInDecibels)
{
   const auto slope = getSlope(ratio);
   float overShoot = inputLevelInDecibels - thresholdInDecibels;
   overShoot = applyCharacteristicToOverShoot(overShoot, kneeInDecibels, slope);
   return overShoot + inputLevelInDecibels + makeUpGainInDecibels;
}

GainReductionComputer::GainReductionComputer()
{
    sampleRate = 0.0f;

    threshold = -10.0f;
    knee = 0.0f;
    kneeHalf = 0.0f;
    attackTime = 0.01f;
    releaseTime = 0.15f;
    setRatio (2); // 2 : 1
    makeUpGain = 0.0f;
    reset();
}

void GainReductionComputer::prepare (const double newSampleRate)
{
    sampleRate = newSampleRate;

    alphaAttack = 1.0f - timeToGain (attackTime);
    alphaRelease = 1.0f - timeToGain (releaseTime);
}

void GainReductionComputer::setAttackTime (const float attackTimeInSeconds)
{
    attackTime = attackTimeInSeconds;
    alphaAttack = 1.0f - timeToGain (attackTime);
}

void GainReductionComputer::setReleaseTime (const float releaseTimeInSeconds)
{
    releaseTime = releaseTimeInSeconds;
    alphaRelease = 1.0f - timeToGain (releaseTime);
}

const float GainReductionComputer::timeToGain (const float timeInSeconds)
{
    return std::exp (-1.0f / (static_cast<float> (sampleRate) * timeInSeconds));
}

void GainReductionComputer::setKnee (const float kneeInDecibels)
{
    knee = kneeInDecibels;
    kneeHalf = knee / 2.0f;
}

void GainReductionComputer::setThreshold (const float thresholdInDecibels)
{
    threshold = thresholdInDecibels;
}

void GainReductionComputer::setMakeUpGain (const float makeUpGainInDecibels)
{
    makeUpGain = makeUpGainInDecibels;
}

void GainReductionComputer::setRatio (const float ratio)
{
    slope = getSlope(ratio);
}


inline const float GainReductionComputer::applyCharacteristicToOverShoot (const float overShootInDecibels)
{
    return applyCharacteristicToOverShoot(overShootInDecibels, knee, slope);
}

float GainReductionComputer::applyCharacteristicToOverShoot(
    float overShootInDecibels, float knee, float slope)
{
    const auto kneeHalf = knee / 2;
    if (overShootInDecibels <= -kneeHalf)
        return 0.0f;
    else if (overShootInDecibels > -kneeHalf && overShootInDecibels <= kneeHalf)
        return 0.5f * slope * (overShootInDecibels + kneeHalf) * (overShootInDecibels + kneeHalf) / knee;
    else
        return slope * overShootInDecibels;
}

void GainReductionComputer::computeGainInDecibelsFromSidechainSignal (const float* sideChainSignal, float* destination, const int numSamples)
{
    maxInputLevel = -std::numeric_limits<float>::infinity();
    maxGainReduction = 0.0f;

    for (int i = 0; i < numSamples; ++i)
    {
        // convert sample to decibels
        const float levelInDecibels =
           log2ToDb * FastLog2(std::abs(sideChainSignal[i]));

        if (levelInDecibels > maxInputLevel)
            maxInputLevel = levelInDecibels;

        // calculate overshoot and apply knee and ratio
        const float overShoot = levelInDecibels - threshold;
        const float gainReduction = applyCharacteristicToOverShoot (overShoot);

        // apply ballistics
        const float diff = gainReduction - state;
        if (diff < 0.0f) // wanted gain reduction is below state -> attack phase
            state += alphaAttack * diff;
        else // release phase
            state += alphaRelease * diff;

        // write back gain reduction
        destination[i] = state;

        if (state < maxGainReduction)
            maxGainReduction = state;
    }
}

void GainReductionComputer::computeLinearGainFromSidechainSignal (const float* sideChainSignal, float* destination, const int numSamples)
{
    computeGainInDecibelsFromSidechainSignal (sideChainSignal, destination, numSamples);
    for (int i = 0; i < numSamples; ++i)
        destination[i] = std::pow (10.0f, 0.05f * (destination[i] + makeUpGain));
}


void GainReductionComputer::getCharacteristic (float* inputLevelsInDecibels, float* dest, const int numSamples)
{
    for (int i = 0; i < numSamples; ++i)
        dest[i] = getCharacteristicSample (inputLevelsInDecibels[i]);
}

float GainReductionComputer::getCharacteristicSample (const float inputLevelInDecibels)
{
    return getCharacteristicSample(inputLevelInDecibels, knee, threshold,
                                   getRatio(slope), makeUpGain);
}
} // namespace DanielRudrich