-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalyzer.cpp
145 lines (121 loc) · 6.15 KB
/
analyzer.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
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
/*
This file is part of Waver
Copyright (C) 2021 Peter Papp
Please visit https://launchpad.net/waver for details
*/
#include "analyzer.h"
Analyzer::Analyzer(QAudioFormat format, QObject *parent) : QObject(parent)
{
this->format = format;
decoderFinished = false;
resultLastCalculated = 0;
replayGainFilter = nullptr;
replayGainCalculator = nullptr;
sampleType = IIRFilter::Unknown;
}
Analyzer::~Analyzer()
{
if (replayGainFilter != nullptr) {
delete replayGainFilter;
}
if (replayGainCalculator != nullptr) {
delete replayGainCalculator;
}
}
void Analyzer::bufferAvailable()
{
while (bufferQueue->count() > 0) {
QAudioBuffer *buffer = bufferQueue->at(0);
if (replayGainFilter != nullptr) {
replayGainFilter->processPCMData(buffer->data(), buffer->byteCount(), sampleType, buffer->format().channelCount());
if ((!decoderFinished && (buffer->startTime() >= resultLastCalculated + REPLAY_GAIN_UPDATE_INTERVAL_MICROSECONDS)) || (decoderFinished && (bufferQueue->count() == 1))) {
resultLastCalculated = buffer->startTime();
emit replayGain(replayGainCalculator->calculateResult());
emit silences(replayGainCalculator->getSilences(decoderFinished));
}
}
bufferQueueMutex->lock();
bufferQueue->remove(0);
bufferQueueMutex->unlock();
delete buffer;
}
}
void Analyzer::decoderDone()
{
decoderFinished = true;
}
void Analyzer::silencesRequested(bool addFinalSilence)
{
emit silences(replayGainCalculator->getSilences(addFinalSilence));
}
void Analyzer::resetReplayGain()
{
if (replayGainCalculator != nullptr) {
replayGainCalculator->reset();
}
}
void Analyzer::run()
{
sampleType = IIRFilter::getSampleTypeFromAudioFormat(format);
if ((sampleType != IIRFilter::Unknown) && QVector<int>({ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000 }).contains(format.sampleRate())) {
replayGainFilter = new IIRFilterChain();
replayGainCalculator = new ReplayGainCalculator(sampleType, format.sampleRate());
switch (format.sampleRate()) {
case 96000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_96000_YULEWALK_A, REPLAYGAIN_96000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_96000_BUTTERWORTH_A, REPLAYGAIN_96000_BUTTERWORTH_B));
break;
case 88200:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_88200_YULEWALK_A, REPLAYGAIN_88200_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_88200_BUTTERWORTH_A, REPLAYGAIN_88200_BUTTERWORTH_B));
break;
case 64000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_64000_YULEWALK_A, REPLAYGAIN_64000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_64000_BUTTERWORTH_A, REPLAYGAIN_64000_BUTTERWORTH_B));
break;
case 48000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_48000_YULEWALK_A, REPLAYGAIN_48000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_48000_BUTTERWORTH_A, REPLAYGAIN_48000_BUTTERWORTH_B));
break;
case 44100:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_44100_YULEWALK_A, REPLAYGAIN_44100_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_44100_BUTTERWORTH_A, REPLAYGAIN_44100_BUTTERWORTH_B));
break;
case 32000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_32000_YULEWALK_A, REPLAYGAIN_32000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_32000_BUTTERWORTH_A, REPLAYGAIN_32000_BUTTERWORTH_B));
break;
case 24000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_24000_YULEWALK_A, REPLAYGAIN_24000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_24000_BUTTERWORTH_A, REPLAYGAIN_24000_BUTTERWORTH_B));
break;
case 22050:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_22050_YULEWALK_A, REPLAYGAIN_22050_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_22050_BUTTERWORTH_A, REPLAYGAIN_22050_BUTTERWORTH_B));
break;
case 16000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_16000_YULEWALK_A, REPLAYGAIN_16000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_16000_BUTTERWORTH_A, REPLAYGAIN_16000_BUTTERWORTH_B));
break;
case 12000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_12000_YULEWALK_A, REPLAYGAIN_12000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_12000_BUTTERWORTH_A, REPLAYGAIN_12000_BUTTERWORTH_B));
break;
case 11025:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_11025_YULEWALK_A, REPLAYGAIN_11025_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_11025_BUTTERWORTH_A, REPLAYGAIN_11025_BUTTERWORTH_B));
break;
case 8000:
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_8000_YULEWALK_A, REPLAYGAIN_8000_YULEWALK_B));
replayGainFilter->appendFilter(CoefficientList(REPLAYGAIN_8000_BUTTERWORTH_A, REPLAYGAIN_8000_BUTTERWORTH_B));
break;
}
replayGainFilter->getFilter(1)->setCallbackFiltered((IIRFilterCallback *)replayGainCalculator, (IIRFilterCallback::FilterCallbackPointer)&ReplayGainCalculator::filterCallback);
replayGainFilter->getFilter(1)->disableUpdateData();
}
}
void Analyzer::setBufferQueue(BufferQueue *bufferQueue, QMutex *bufferQueueMutex)
{
this->bufferQueue = bufferQueue;
this->bufferQueueMutex = bufferQueueMutex;
}