forked from mixxxdj/mixxx
/
analyzerebur128.cpp
85 lines (75 loc) · 2.54 KB
/
analyzerebur128.cpp
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
#include "analyzer/analyzerebur128.h"
#include <QtDebug>
#include "analyzer/analyzertrack.h"
#include "analyzer/constants.h"
#include "track/track.h"
#include "util/math.h"
#include "util/sample.h"
#include "util/timer.h"
namespace {
constexpr double kReplayGain2ReferenceLUFS = -18;
} // anonymous namespace
AnalyzerEbur128::AnalyzerEbur128(UserSettingsPointer pConfig)
: m_rgSettings(pConfig),
m_pState(nullptr) {
}
AnalyzerEbur128::~AnalyzerEbur128() {
cleanup(); // ...to prevent memory leaks
}
bool AnalyzerEbur128::initialize(
const AnalyzerTrack& track,
mixxx::audio::SampleRate sampleRate,
SINT frameLength) {
if (m_rgSettings.isAnalyzerDisabled(2, track.getTrack()) || frameLength <= 0) {
qDebug() << "Skipping AnalyzerEbur128";
return false;
}
DEBUG_ASSERT(m_pState == nullptr);
m_pState = ebur128_init(
mixxx::kAnalysisChannels,
sampleRate,
EBUR128_MODE_I);
return m_pState != nullptr;
}
void AnalyzerEbur128::cleanup() {
if (m_pState) {
ebur128_destroy(&m_pState);
// ebur128_destroy clears the pointer but let's not rely on that.
m_pState = nullptr;
}
}
bool AnalyzerEbur128::processSamples(const CSAMPLE* pIn, SINT count) {
VERIFY_OR_DEBUG_ASSERT(m_pState) {
return false;
}
ScopedTimer t(u"AnalyzerEbur128::processSamples()");
size_t frames = count / mixxx::kAnalysisChannels;
int e = ebur128_add_frames_float(m_pState, pIn, frames);
VERIFY_OR_DEBUG_ASSERT(e == EBUR128_SUCCESS) {
qWarning() << "AnalyzerEbur128::processSamples() failed with" << e;
return false;
}
return true;
}
void AnalyzerEbur128::storeResults(TrackPointer pTrack) {
VERIFY_OR_DEBUG_ASSERT(m_pState) {
return;
}
double averageLufs;
int e = ebur128_loudness_global(m_pState, &averageLufs);
VERIFY_OR_DEBUG_ASSERT(e == EBUR128_SUCCESS) {
qWarning() << "AnalyzerEbur128::storeResults() failed with" << e;
return;
}
if (averageLufs == -HUGE_VAL || averageLufs == 0.0) {
qWarning() << "AnalyzerEbur128::storeResults() averageLufs invalid:"
<< averageLufs;
return;
}
const double fReplayGain2 = kReplayGain2ReferenceLUFS - averageLufs;
mixxx::ReplayGain replayGain(pTrack->getReplayGain());
replayGain.setRatio(db2ratio(fReplayGain2));
pTrack->setReplayGain(replayGain);
qDebug() << "ReplayGain 2.0 (libebur128) result is" << fReplayGain2
<< "dB for" << pTrack->getFileInfo();
}