-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathanalyzersilence.cpp
More file actions
279 lines (246 loc) · 10.4 KB
/
Copy pathanalyzersilence.cpp
File metadata and controls
279 lines (246 loc) · 10.4 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "analyzer/analyzersilence.h"
#include "analyzer/analyzertrack.h"
#include "analyzer/constants.h"
#include "track/track.h"
namespace {
// This threshold must not be changed, because this value is also used to
// verify that the track samples have not changed since the last analysis
constexpr CSAMPLE kSilenceThreshold = 0.001f; // -60 dB
// TODO: Change the above line to:
// constexpr CSAMPLE kSilenceThreshold = db2ratio(-60.0f);
// These are in dBV expressed as Volts RMS (which seems, sensibly,
// the way Mixxx works).
// Don't change these to const as they are used only to feed the
// fade thresholds which are consts below themselves while we work
// out which one is best, and you'll just be adding to exe size.
#define N_10DB_FADE_THRESHOLD 0.3162f
#define N_12DB_FADE_THRESHOLD 0.2511f
#define N_15DB_FADE_THRESHOLD 0.1778f
#define N_18DB_FADE_THRESHOLD 0.1259f
#define N_20DB_FADE_THRESHOLD 0.1f
#define N_24DB_FADE_THRESHOLD 0.0631f
#define N_25DB_FADE_THRESHOLD 0.0562f
#define N_27DB_FADE_THRESHOLD 0.0447f
#define N_30DB_FADE_THRESHOLD 0.0316f
#define N_40DB_FADE_THRESHOLD 0.01f
constexpr CSAMPLE kFadeInThreshold = N_27DB_FADE_THRESHOLD;
constexpr CSAMPLE kFadeOutThreshold = N_12DB_FADE_THRESHOLD;
bool shouldAnalyze(TrackPointer pTrack) {
CuePointer pIntroCue = pTrack->findCueByType(mixxx::CueType::Intro);
CuePointer pOutroCue = pTrack->findCueByType(mixxx::CueType::Outro);
CuePointer pN60dBSound = pTrack->findCueByType(mixxx::CueType::N60dBSound);
CuePointer pFadeIn = pTrack->findCueByType(mixxx::CueType::FadeIn);
CuePointer pFadeOut = pTrack->findCueByType(mixxx::CueType::FadeOut);
if (!pFadeIn || !pFadeOut || !pIntroCue || !pOutroCue || !pN60dBSound ||
pN60dBSound->getLengthFrames() <= 0) {
return true;
}
return false;
}
template<typename Iterator, typename Threshold>
Iterator find_first_above_threshold(Iterator begin,
Iterator end,
Threshold threshold) {
return std::find_if(begin, end, [threshold](const auto elem) {
return fabs(elem) >= threshold;
});
}
} // anonymous namespace
AnalyzerSilence::AnalyzerSilence(UserSettingsPointer pConfig)
: m_pConfig(pConfig),
m_framesProcessed(0),
m_signalStart(-1),
m_signalEnd(-1),
m_fadeThresholdFadeInEnd(-1),
m_fadeThresholdFadeOutStart(-1) {
}
bool AnalyzerSilence::initialize(const AnalyzerTrack& track,
mixxx::audio::SampleRate sampleRate,
mixxx::audio::ChannelCount channelCount,
SINT frameLength) {
Q_UNUSED(sampleRate);
Q_UNUSED(frameLength);
if (!shouldAnalyze(track.getTrack())) {
return false;
}
m_framesProcessed = 0;
m_signalStart = -1;
m_signalEnd = -1;
m_fadeThresholdFadeInEnd = -1;
m_fadeThresholdFadeOutStart = -1;
m_channelCount = channelCount;
return true;
}
// static
SINT AnalyzerSilence::findFirstSoundInChunk(std::span<const CSAMPLE> samples) {
return std::distance(samples.begin(),
find_first_above_threshold(
samples.begin(), samples.end(), kSilenceThreshold));
}
// static
SINT AnalyzerSilence::findLastSoundInChunk(std::span<const CSAMPLE> samples) {
// -1 is required, because the distance from the fist sample index (0) to crend() is 1,
SINT ret = std::distance(find_first_above_threshold(samples.rbegin(),
samples.rend(),
kSilenceThreshold),
samples.rend()) -
1;
return ret;
}
// Find the number of first sound sample where the sound is above kFadeInThreshold (-27db)
SINT AnalyzerSilence::findLastFadeInChunk(std::span<const CSAMPLE> samples) {
SINT ret = std::distance(samples.begin(),
find_first_above_threshold(
samples.begin(), samples.end(), kFadeInThreshold));
return ret;
}
// Find the number of last sound sample where the sound is above kFadeOutThreshold (-12db)
SINT AnalyzerSilence::findFirstFadeOutChunk(std::span<const CSAMPLE> samples) {
// Note we are searching backwards from the end here.
SINT ret = std::distance(find_first_above_threshold(samples.rbegin(),
samples.rend(),
kFadeOutThreshold),
samples.rend()) -
1;
return ret;
}
// static
bool AnalyzerSilence::verifyFirstSound(
std::span<const CSAMPLE> samples,
mixxx::audio::FramePos firstSoundFrame,
mixxx::audio::ChannelCount channelCount) {
const SINT firstSoundSample = findFirstSoundInChunk(samples);
if (firstSoundSample < static_cast<SINT>(samples.size())) {
return mixxx::audio::FramePos::fromSamplePos(firstSoundSample, channelCount)
.toLowerFrameBoundary() == firstSoundFrame.toLowerFrameBoundary();
}
return false;
}
bool AnalyzerSilence::processSamples(const CSAMPLE* pIn, SINT count) {
SINT numFrames = count / m_channelCount;
std::span<const CSAMPLE> samples = mixxx::spanutil::spanFromPtrLen(pIn, count);
if (m_signalStart < 0) {
const SINT firstSoundSample = findFirstSoundInChunk(samples);
if (firstSoundSample < count) {
m_signalStart = m_framesProcessed + firstSoundSample / m_channelCount;
}
}
if (m_fadeThresholdFadeInEnd < 0) {
const SINT lastSampleOfFadeIn = findLastFadeInChunk(samples);
if (lastSampleOfFadeIn < count) {
m_fadeThresholdFadeInEnd = m_framesProcessed + (lastSampleOfFadeIn / m_channelCount);
}
}
if (m_fadeThresholdFadeInEnd >= 0) {
const SINT lasttSampleBeforeFadeOut = findFirstFadeOutChunk(samples);
if (lasttSampleBeforeFadeOut >= 0) {
m_fadeThresholdFadeOutStart = m_framesProcessed +
(lasttSampleBeforeFadeOut / m_channelCount) + 1;
}
}
if (m_fadeThresholdFadeOutStart >= 0) {
const SINT lastSoundSample = findLastSoundInChunk(samples);
if (lastSoundSample >= 0) {
m_signalEnd = m_framesProcessed + (lastSoundSample / m_channelCount) + 1;
}
}
m_framesProcessed += numFrames;
return true;
}
void AnalyzerSilence::cleanup() {
}
void AnalyzerSilence::storeResults(TrackPointer pTrack) {
if (m_signalStart < 0) {
m_signalStart = 0;
}
if (m_signalEnd < 0) {
m_signalEnd = m_framesProcessed;
}
const auto firstSoundPosition = mixxx::audio::FramePos(m_signalStart);
const auto lastSoundPosition = mixxx::audio::FramePos(m_signalEnd);
CuePointer pN60dBSound = pTrack->findCueByType(mixxx::CueType::N60dBSound);
if (pN60dBSound == nullptr) {
pN60dBSound = pTrack->createAndAddCue(
mixxx::CueType::N60dBSound,
Cue::kNoHotCue,
firstSoundPosition,
lastSoundPosition);
} else {
// The user has no way to directly edit the N60dBSound cue. If the user
// has deleted the Intro or Outro Cue, this analysis will be rerun when
// the track is loaded again. In this case, adjust the N60dBSound Cue's
// positions. This could be helpful, for example, when the track length
// is changed in a different program.
pN60dBSound->setStartAndEndPosition(firstSoundPosition, lastSoundPosition);
}
setupMainAndIntroCue(pTrack.get(), firstSoundPosition, m_pConfig.data());
setupOutroCue(pTrack.get(), lastSoundPosition);
if (m_fadeThresholdFadeInEnd < 0) {
m_fadeThresholdFadeInEnd = 0;
}
if (m_fadeThresholdFadeOutStart < 0) {
m_fadeThresholdFadeOutStart = m_framesProcessed;
}
const auto fadeInEndPosition = mixxx::audio::FramePos(m_fadeThresholdFadeInEnd);
CuePointer pFadeIn = pTrack->findCueByType(mixxx::CueType::FadeIn);
if (pFadeIn == nullptr) {
pFadeIn = pTrack->createAndAddCue(
mixxx::CueType::FadeIn,
Cue::kNoHotCue,
firstSoundPosition,
fadeInEndPosition);
} else {
pFadeIn->setStartAndEndPosition(firstSoundPosition, fadeInEndPosition);
}
const auto fadeOutStartPosition = mixxx::audio::FramePos(m_fadeThresholdFadeOutStart);
CuePointer pFadeOut = pTrack->findCueByType(mixxx::CueType::FadeOut);
if (pFadeOut == nullptr) {
pFadeOut = pTrack->createAndAddCue(
mixxx::CueType::FadeOut,
Cue::kNoHotCue,
fadeOutStartPosition,
lastSoundPosition);
} else {
pFadeOut->setStartAndEndPosition(fadeOutStartPosition, lastSoundPosition);
}
}
// static
void AnalyzerSilence::setupMainAndIntroCue(
Track* pTrack, mixxx::audio::FramePos firstSoundPosition, UserSettings* pConfig) {
CuePointer pIntroCue = pTrack->findCueByType(mixxx::CueType::Intro);
mixxx::audio::FramePos mainCuePosition = pTrack->getMainCuePosition();
mixxx::audio::FramePos introStartPosition = firstSoundPosition;
// Before Mixxx 2.3, the default position for the main cue was 0.0. In this
// case, move the main cue point to the first sound. This case can be
// distinguished from a user intentionally setting the main cue position
// to 0.0 at a later time after analysis because in that case the intro cue
// would have already been created by this analyzer.
bool upgradingWithMainCueAtDefault =
(mainCuePosition == mixxx::audio::kStartFramePos &&
pIntroCue == nullptr);
if (!mainCuePosition.isValid() || upgradingWithMainCueAtDefault) {
pTrack->setMainCuePosition(firstSoundPosition);
// NOTE: the actual default for this ConfigValue is set in DlgPrefDeck.
} else if (pConfig->getValue(ConfigKey("[Controls]", "SetIntroStartAtMainCue"), false) &&
pIntroCue == nullptr) {
introStartPosition = mainCuePosition;
}
if (pIntroCue == nullptr) {
pIntroCue = pTrack->createAndAddCue(
mixxx::CueType::Intro,
Cue::kNoHotCue,
introStartPosition,
mixxx::audio::kInvalidFramePos);
}
}
// static
void AnalyzerSilence::setupOutroCue(Track* pTrack, mixxx::audio::FramePos lastSoundPosition) {
CuePointer pOutroCue = pTrack->findCueByType(mixxx::CueType::Outro);
if (pOutroCue == nullptr) {
pOutroCue = pTrack->createAndAddCue(
mixxx::CueType::Outro,
Cue::kNoHotCue,
mixxx::audio::kInvalidFramePos,
lastSoundPosition);
}
}