aboutsummaryrefslogtreecommitdiffstats
path: root/Alc/mastering.c
blob: 1636c8d93fd03101651d4aa4a0843d635dd5415d (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "config.h"

#include <math.h>

#include "mastering.h"
#include "alu.h"
#include "almalloc.h"


extern inline ALuint GetCompressorSampleRate(const Compressor *Comp);

#define RMS_WINDOW_SIZE (1<<7)
#define RMS_WINDOW_MASK (RMS_WINDOW_SIZE-1)
#define RMS_VALUE_MAX  (1<<24)

static_assert(RMS_VALUE_MAX < (UINT_MAX / RMS_WINDOW_SIZE), "RMS_VALUE_MAX is too big");


/* Multichannel compression is linked via one of two modes:
 *
 *   Summed - Absolute sum of all channels.
 *   Maxed  - Absolute maximum of any channel.
 */
static void SumChannels(Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
                        ALfloat (*restrict OutBuffer)[BUFFERSIZE])
{
    ALsizei c, i;

    for(i = 0;i < SamplesToDo;i++)
        Comp->Envelope[i] = 0.0f;

    for(c = 0;c < NumChans;c++)
    {
        for(i = 0;i < SamplesToDo;i++)
            Comp->Envelope[i] += OutBuffer[c][i];
    }

    for(i = 0;i < SamplesToDo;i++)
        Comp->Envelope[i] = fabsf(Comp->Envelope[i]);
}

static void MaxChannels(Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
                        ALfloat (*restrict OutBuffer)[BUFFERSIZE])
{
    ALsizei c, i;

    for(i = 0;i < SamplesToDo;i++)
        Comp->Envelope[i] = 0.0f;

    for(c = 0;c < NumChans;c++)
    {
        for(i = 0;i < SamplesToDo;i++)
            Comp->Envelope[i] = maxf(Comp->Envelope[i], fabsf(OutBuffer[c][i]));
    }
}

/* Envelope detection/sensing can be done via:
 *
 *   RMS  - Rectangular windowed root mean square of linking stage.
 *   Peak - Implicit output from linking stage.
 */
static void RmsDetection(Compressor *Comp, const ALsizei SamplesToDo)
{
    ALuint sum = Comp->RmsSum;
    ALuint *window = Comp->RmsWindow;
    ALsizei index = Comp->RmsIndex;
    ALsizei i;

    for(i = 0;i < SamplesToDo;i++)
    {
        ALfloat sig = Comp->Envelope[i];

        sum -= window[index];
        window[index] = fastf2i(minf(sig * sig * 65536.0f, RMS_VALUE_MAX));
        sum += window[index];
        index = (index + 1) & RMS_WINDOW_MASK;

        Comp->Envelope[i] = sqrtf(sum / 65536.0f / RMS_WINDOW_SIZE);
    }

    Comp->RmsSum = sum;
    Comp->RmsIndex = index;
}

/* This isn't a very sophisticated envelope follower, but it gets the job
 * done.  First, it operates at logarithmic scales to keep transitions
 * appropriate for human hearing.  Second, it can apply adaptive (automated)
 * attack/release adjustments based on the signal.
 */
static void FollowEnvelope(Compressor *Comp, const ALsizei SamplesToDo)
{
    ALfloat attackMin = Comp->AttackMin;
    ALfloat attackMax = Comp->AttackMax;
    ALfloat releaseMin = Comp->ReleaseMin;
    ALfloat releaseMax = Comp->ReleaseMax;
    ALfloat last = Comp->EnvLast;
    ALsizei i;

    for(i = 0;i < SamplesToDo;i++)
    {
        ALfloat env = log10f(maxf(Comp->Envelope[i], 0.000001f));
        ALfloat slope = minf(1.0f, fabsf(env - last) / 4.5f);

        if(env > last)
            last = minf(env, last + lerp(attackMin, attackMax, 1.0f - (slope * slope)));
        else
            last = maxf(env, last + lerp(releaseMin, releaseMax, 1.0f - (slope * slope)));

        Comp->Envelope[i] = last;
    }

    Comp->EnvLast = last;
}

/* The envelope is converted to control gain with an optional soft knee. */
static void EnvelopeGain(Compressor *Comp, const ALsizei SamplesToDo, const ALfloat Slope)
{
    const ALfloat threshold = Comp->Threshold;
    const ALfloat knee = Comp->Knee;
    ALsizei i;

    if(!(knee > 0.0f))
    {
        for(i = 0;i < SamplesToDo;i++)
        {
            ALfloat gain = Slope * (threshold - Comp->Envelope[i]);
            Comp->Envelope[i] = powf(10.0f, minf(0.0f, gain));
        }
    }
    else
    {
        const ALfloat lower = threshold - (0.5f * knee);
        const ALfloat upper = threshold + (0.5f * knee);
        const ALfloat m = 0.5f * Slope / knee;

        for(i = 0;i < SamplesToDo;i++)
        {
            ALfloat env = Comp->Envelope[i];
            ALfloat gain;

            if(env > lower && env < upper)
                gain = m * (env - lower) * (lower - env);
            else
                gain = Slope * (threshold - env);

            Comp->Envelope[i] = powf(10.0f, minf(0.0f, gain));
        }
    }
}


Compressor *CompressorInit(const ALfloat PreGainDb, const ALfloat PostGainDb,
                           const ALboolean SummedLink, const ALboolean RmsSensing,
                           const ALfloat AttackTimeMin, const ALfloat AttackTimeMax,
                           const ALfloat ReleaseTimeMin, const ALfloat ReleaseTimeMax,
                           const ALfloat Ratio, const ALfloat ThresholdDb,
                           const ALfloat KneeDb, const ALuint SampleRate)
{
    Compressor *Comp;
    size_t size;
    ALsizei i;

    size = sizeof(*Comp);
    if(RmsSensing)
        size += sizeof(Comp->RmsWindow[0]) * RMS_WINDOW_SIZE;
    Comp = al_calloc(16, size);

    Comp->PreGain = powf(10.0f, PreGainDb / 20.0f);
    Comp->PostGain = powf(10.0f, PostGainDb / 20.0f);
    Comp->SummedLink = SummedLink;
    Comp->AttackMin = 1.0f / maxf(0.000001f, AttackTimeMin * SampleRate * logf(10.0f));
    Comp->AttackMax = 1.0f / maxf(0.000001f, AttackTimeMax * SampleRate * logf(10.0f));
    Comp->ReleaseMin = -1.0f / maxf(0.000001f, ReleaseTimeMin * SampleRate * logf(10.0f));
    Comp->ReleaseMax = -1.0f / maxf(0.000001f, ReleaseTimeMax * SampleRate * logf(10.0f));
    Comp->Ratio = Ratio;
    Comp->Threshold = ThresholdDb / 20.0f;
    Comp->Knee = maxf(0.0f, KneeDb / 20.0f);
    Comp->SampleRate = SampleRate;

    Comp->RmsSum = 0;
    if(RmsSensing)
        Comp->RmsWindow = (ALuint*)(Comp+1);
    else
        Comp->RmsWindow = NULL;
    Comp->RmsIndex = 0;

    for(i = 0;i < BUFFERSIZE;i++)
        Comp->Envelope[i] = 0.0f;
    Comp->EnvLast = -6.0f;

    return Comp;
}

void ApplyCompression(Compressor *Comp, const ALsizei NumChans, const ALsizei SamplesToDo,
                      ALfloat (*restrict OutBuffer)[BUFFERSIZE])
{
    ALsizei c, i;

    if(Comp->PreGain != 1.0f)
    {
        for(c = 0;c < NumChans;c++)
        {
            for(i = 0;i < SamplesToDo;i++)
                OutBuffer[c][i] *= Comp->PreGain;
        }
    }

    if(Comp->SummedLink)
        SumChannels(Comp, NumChans, SamplesToDo, OutBuffer);
    else
        MaxChannels(Comp, NumChans, SamplesToDo, OutBuffer);

    if(Comp->RmsWindow)
        RmsDetection(Comp, SamplesToDo);
    FollowEnvelope(Comp, SamplesToDo);

    if(Comp->Ratio > 0.0f)
        EnvelopeGain(Comp, SamplesToDo, 1.0f - (1.0f / Comp->Ratio));
    else
        EnvelopeGain(Comp, SamplesToDo, 1.0f);

    if(Comp->PostGain != 1.0f)
    {
        for(i = 0;i < SamplesToDo;i++)
            Comp->Envelope[i] *= Comp->PostGain;
    }
    for(c = 0;c < NumChans;c++)
    {
        for(i = 0;i < SamplesToDo;i++)
            OutBuffer[c][i] *= Comp->Envelope[i];
    }
}