-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathEbu128LoudnessMeter.h
266 lines (201 loc) · 8.88 KB
/
Ebu128LoudnessMeter.h
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
===============================================================================
Ebu128LoudnessMeter.h
By Samuel Gaehwiler from Klangfreund.
Used in the klangfreund.com/lufsmeter/
License: MIT
I'd be happy to hear about your usage of this code!
-> klangfreund.com/contact/
-------------------------------------------------------------------------------
The MIT License (MIT)
Copyright (c) 2018 Klangfreund, Samuel Gaehwiler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================================================
*/
#ifndef __EBU128_LOUDNESS_METER__
#define __EBU128_LOUDNESS_METER__
#include "MacrosAndJuceHeaders.h"
#include "filters/SecondOrderIIRFilter.h"
#include <map>
#include <vector>
using std::map;
using std::vector;
/**
Measures the loudness of an audio stream.
The loudness is measured according to the documents
(List)
- EBU - R 128
- ITU 1770 Rev 2,3 and 4
- EBU - Tech 3341 (EBU mode metering)
- EBU - Tech 3342 (LRA, loudness range)
- EBU - Tech 3343
*/
class Ebu128LoudnessMeter //: public AudioProcessor
{
public:
Ebu128LoudnessMeter();
~Ebu128LoudnessMeter();
// --------- AudioProcessor methods ---------
// const String getName ();
/**
@param sampleRate
@param numberOfChannels
@param estimatedSamplesPerBlock
@param expectedRequestRate Assumption about how many times
a second the measurement values will be requested. Internally,
this will be changed to a multiple of 10 because exactly every
0.1 second a gating block needs to be measured (for the
integrated loudness measurement).
*/
void prepareToPlay (double sampleRate,
int numberOfInputChannels,
int estimatedSamplesPerBlock,
int expectedRequestRate);
void processBlock (const AudioSampleBuffer& buffer);
float getShortTermLoudness() const;
float getMaximumShortTermLoudness() const;
vector<float>& getMomentaryLoudnessForIndividualChannels();
float getMomentaryLoudness() const;
float getMaximumMomentaryLoudness() const;
float getIntegratedLoudness() const;
float getLoudnessRangeStart() const;
float getLoudnessRangeEnd() const;
float getLoudnessRange() const;
/** Returns the time passed since the last reset.
In seconds.
*/
float getMeasurementDuration() const;
void setFreezeLoudnessRangeOnSilence (bool freeze);
void reset();
private:
static int round (double d);
/** The buffer given to processBlock() will be copied to this buffer, such
that the filtering and squaring won't affect the audio output. I.e. thanks
to this, the audio will pass through this without getting changed.
It also stores the number of input channels implicitely, set in prepareToPlay.
*/
AudioSampleBuffer bufferForMeasurement;
SecondOrderIIRFilter preFilter;
SecondOrderIIRFilter revisedLowFrequencyBCurveFilter;
int numberOfBins;
int numberOfSamplesPerBin;
int numberOfSamplesInAllBins;
int numberOfBinsToCover400ms;
int numberOfSamplesIn400ms;
int numberOfBinsToCover100ms;
int numberOfBinsSinceLastGateMeasurementForI;
// int millisecondsSinceLastGateMeasurementForLRA;
/** The duration of the current measurement.
duration * 0.1 = the measurement duration in seconds.
*/
int measurementDuration;
/**
After the samples are filtered and squared, they need to be
accumulated.
For the measurement of the short-term loudness, the previous
3 seconds of samples need to be accumulated. For the other
measurements shorter windows are used.
This task could be solved using a ring buffer capable of
holding 3 seconds of (multi-channel) audio and accumulate the
samples every time the GUI wants to display the measurement.
But it's easier on the CPU and needs far less memory if not
all samples are stored but only the summation of all the
samples in a 1/(GUI refresh rate) s - e.g. 1/20 s - time window.
If we then need to determine the sum of all samples of the previous
3s, we only have to accumulate 60 values.
The downside of this method is that the displayed measurement
has an accuracy jitter of max. e.g. 1/20 s. I guess this isn't
an issue since GUI elements are refreshed asynchron anyway.
Just to be clear, the accuracy of the measurement isn't effected.
But if you ask for a measurement at time t, it will be the
accurate measurement at time t - dt, where dt \in e.g. [0, 1/20s].
*/
vector<vector<double>> bin;
int currentBin;
int numberOfSamplesInTheCurrentBin;
/*
The average of the filtered and squared samples of the last
3 seconds.
A value for each channel.
*/
vector<double> averageOfTheLast3s;
/*
The average of the filtered and squared samples of the last
400 milliseconds.
A value for each channel.
*/
vector<double> averageOfTheLast400ms;
vector<double> channelWeighting;
vector<float> momentaryLoudnessForIndividualChannels;
/** If there is no signal at all, the methods getShortTermLoudness() and
getMomentaryLoudness() would perform a log10(0) which would result in
a value -nan. To avoid this, the return value of this methods will be
set to minimalReturnValue.
*/
static const float minimalReturnValue;
/** A gated window needs to be bigger than this value to
contribute to the calculation of the relative threshold.
absoluteThreshold = Gamma_a = -70 LUFS.
*/
static const double absoluteThreshold;
int numberOfBlocksToCalculateRelativeThreshold;
double sumOfAllBlocksToCalculateRelativeThreshold;
double relativeThreshold;
int numberOfBlocksToCalculateRelativeThresholdLRA;
double sumOfAllBlocksToCalculateRelativeThresholdLRA;
double relativeThresholdLRA;
/** A lower bound for the histograms (for I and LRA).
If a measured block has a value lower than this, it will not be
considered in the calculation for I and LRA.
Without the possibility to increase the pre-measurement-gain at any
point after the measurement has started, this could have been set
to the absoluteThreshold = -70 LUFS.
*/
static const double lowestBlockLoudnessToConsider;
/** Storage for the loudnesses of all 400ms blocks since the last reset.
Because the relative threshold varies and all blocks with a loudness
bigger than the relative threshold are needed to calculate the gated
loudness (integrated loudness), it is mandatory to keep track of all
block loudnesses.
Adjacent bins are set apart by 0.1 LU which seems to be sufficient.
Key value = Loudness * 10 (to get an integer value).
*/
map<int,int> histogramOfBlockLoudness;
/** The main loudness value of interest. */
float integratedLoudness;
float shortTermLoudness;
float maximumShortTermLoudness;
float momentaryLoudness;
float maximumMomentaryLoudness;
/** Like histogramOfBlockLoudness, but for the measurement of the
loudness range.
The histogramOfBlockLoudness can't be used simultaneous for the
loudness range, because the measurement blocks for the loudness
range need to be of length 3s. Vs 400ms.
*/
map<int,int> histogramOfBlockLoudnessLRA;
/**
The return values for the corresponding get member functions.
Loudness Range = loudnessRangeEnd - loudnessRangeStart.
*/
float loudnessRangeStart;
float loudnessRangeEnd;
bool freezeLoudnessRangeOnSilence;
bool currentBlockIsSilent;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Ebu128LoudnessMeter);
};
#endif // __EBU128_LOUDNESS_METER__