c@41
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@41
|
2
|
c@41
|
3 /*
|
c@41
|
4 * SegmenterPlugin.cpp
|
c@41
|
5 *
|
c@41
|
6 * Copyright 2008 Centre for Digital Music, Queen Mary, University of London.
|
c@41
|
7 * All rights reserved.
|
c@41
|
8 */
|
c@41
|
9
|
c@41
|
10 #include <iostream>
|
c@44
|
11 #include <cstdio>
|
c@41
|
12
|
c@41
|
13 #include "SimilarityPlugin.h"
|
c@42
|
14 #include "base/Pitch.h"
|
c@41
|
15 #include "dsp/mfcc/MFCC.h"
|
c@42
|
16 #include "dsp/chromagram/Chromagram.h"
|
c@41
|
17 #include "dsp/rateconversion/Decimator.h"
|
c@47
|
18 #include "dsp/rhythm/BeatSpectrum.h"
|
c@47
|
19 #include "maths/KLDivergence.h"
|
c@47
|
20 #include "maths/CosineDistance.h"
|
c@49
|
21 #include "maths/MathUtilities.h"
|
c@41
|
22
|
c@41
|
23 using std::string;
|
c@41
|
24 using std::vector;
|
c@41
|
25 using std::cerr;
|
c@41
|
26 using std::endl;
|
c@41
|
27 using std::ostringstream;
|
c@41
|
28
|
c@47
|
29 const float
|
c@47
|
30 SimilarityPlugin::m_noRhythm = 0.009;
|
c@47
|
31
|
c@47
|
32 const float
|
c@47
|
33 SimilarityPlugin::m_allRhythm = 0.991;
|
c@47
|
34
|
c@41
|
35 SimilarityPlugin::SimilarityPlugin(float inputSampleRate) :
|
c@41
|
36 Plugin(inputSampleRate),
|
c@42
|
37 m_type(TypeMFCC),
|
c@41
|
38 m_mfcc(0),
|
c@47
|
39 m_rhythmfcc(0),
|
c@42
|
40 m_chromagram(0),
|
c@41
|
41 m_decimator(0),
|
c@42
|
42 m_featureColumnSize(20),
|
c@48
|
43 m_rhythmWeighting(0.5f),
|
c@47
|
44 m_rhythmClipDuration(4.f), // seconds
|
c@47
|
45 m_rhythmClipOrigin(40.f), // seconds
|
c@47
|
46 m_rhythmClipFrameSize(0),
|
c@47
|
47 m_rhythmClipFrames(0),
|
c@47
|
48 m_rhythmColumnSize(20),
|
c@41
|
49 m_blockSize(0),
|
c@47
|
50 m_channels(0),
|
c@47
|
51 m_processRate(0),
|
c@47
|
52 m_frameNo(0),
|
c@47
|
53 m_done(false)
|
c@41
|
54 {
|
c@47
|
55 int rate = lrintf(m_inputSampleRate);
|
c@47
|
56 int internalRate = 22050;
|
c@47
|
57 int decimationFactor = rate / internalRate;
|
c@47
|
58 if (decimationFactor < 1) decimationFactor = 1;
|
c@47
|
59
|
c@47
|
60 // must be a power of two
|
c@47
|
61 while (decimationFactor & (decimationFactor - 1)) ++decimationFactor;
|
c@47
|
62
|
c@47
|
63 m_processRate = rate / decimationFactor; // may be 22050, 24000 etc
|
c@41
|
64 }
|
c@41
|
65
|
c@41
|
66 SimilarityPlugin::~SimilarityPlugin()
|
c@41
|
67 {
|
c@41
|
68 delete m_mfcc;
|
c@47
|
69 delete m_rhythmfcc;
|
c@42
|
70 delete m_chromagram;
|
c@41
|
71 delete m_decimator;
|
c@41
|
72 }
|
c@41
|
73
|
c@41
|
74 string
|
c@41
|
75 SimilarityPlugin::getIdentifier() const
|
c@41
|
76 {
|
c@41
|
77 return "qm-similarity";
|
c@41
|
78 }
|
c@41
|
79
|
c@41
|
80 string
|
c@41
|
81 SimilarityPlugin::getName() const
|
c@41
|
82 {
|
c@41
|
83 return "Similarity";
|
c@41
|
84 }
|
c@41
|
85
|
c@41
|
86 string
|
c@41
|
87 SimilarityPlugin::getDescription() const
|
c@41
|
88 {
|
c@42
|
89 return "Return a distance matrix for similarity between the input audio channels";
|
c@41
|
90 }
|
c@41
|
91
|
c@41
|
92 string
|
c@41
|
93 SimilarityPlugin::getMaker() const
|
c@41
|
94 {
|
c@50
|
95 return "Queen Mary, University of London";
|
c@41
|
96 }
|
c@41
|
97
|
c@41
|
98 int
|
c@41
|
99 SimilarityPlugin::getPluginVersion() const
|
c@41
|
100 {
|
c@41
|
101 return 1;
|
c@41
|
102 }
|
c@41
|
103
|
c@41
|
104 string
|
c@41
|
105 SimilarityPlugin::getCopyright() const
|
c@41
|
106 {
|
c@50
|
107 return "Plugin by Mark Levy, Kurt Jacobson and Chris Cannam. Copyright (c) 2008 QMUL - All Rights Reserved";
|
c@41
|
108 }
|
c@41
|
109
|
c@41
|
110 size_t
|
c@41
|
111 SimilarityPlugin::getMinChannelCount() const
|
c@41
|
112 {
|
c@43
|
113 return 1;
|
c@41
|
114 }
|
c@41
|
115
|
c@41
|
116 size_t
|
c@41
|
117 SimilarityPlugin::getMaxChannelCount() const
|
c@41
|
118 {
|
c@41
|
119 return 1024;
|
c@41
|
120 }
|
c@41
|
121
|
c@41
|
122 bool
|
c@41
|
123 SimilarityPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@41
|
124 {
|
c@52
|
125 if (channels < getMinChannelCount()) return false;
|
c@52
|
126
|
c@52
|
127 // Using more than getMaxChannelCount is not actually a problem
|
c@52
|
128 // for us. Using "incorrect" step and block sizes would be fine
|
c@52
|
129 // for timbral or chroma similarity, but will break rhythmic
|
c@52
|
130 // similarity, so we'd better enforce these.
|
c@41
|
131
|
c@41
|
132 if (stepSize != getPreferredStepSize()) {
|
c@41
|
133 std::cerr << "SimilarityPlugin::initialise: supplied step size "
|
c@41
|
134 << stepSize << " differs from required step size "
|
c@41
|
135 << getPreferredStepSize() << std::endl;
|
c@41
|
136 return false;
|
c@41
|
137 }
|
c@41
|
138
|
c@41
|
139 if (blockSize != getPreferredBlockSize()) {
|
c@41
|
140 std::cerr << "SimilarityPlugin::initialise: supplied block size "
|
c@41
|
141 << blockSize << " differs from required block size "
|
c@41
|
142 << getPreferredBlockSize() << std::endl;
|
c@41
|
143 return false;
|
c@41
|
144 }
|
c@41
|
145
|
c@41
|
146 m_blockSize = blockSize;
|
c@41
|
147 m_channels = channels;
|
c@41
|
148
|
c@44
|
149 m_lastNonEmptyFrame = std::vector<int>(m_channels);
|
c@44
|
150 for (int i = 0; i < m_channels; ++i) m_lastNonEmptyFrame[i] = -1;
|
c@44
|
151 m_frameNo = 0;
|
c@44
|
152
|
c@41
|
153 int decimationFactor = getDecimationFactor();
|
c@41
|
154 if (decimationFactor > 1) {
|
c@42
|
155 m_decimator = new Decimator(m_blockSize, decimationFactor);
|
c@41
|
156 }
|
c@41
|
157
|
c@42
|
158 if (m_type == TypeMFCC) {
|
c@42
|
159
|
c@42
|
160 m_featureColumnSize = 20;
|
c@42
|
161
|
c@47
|
162 MFCCConfig config(m_processRate);
|
c@42
|
163 config.fftsize = 2048;
|
c@42
|
164 config.nceps = m_featureColumnSize - 1;
|
c@42
|
165 config.want_c0 = true;
|
c@45
|
166 config.logpower = 1;
|
c@42
|
167 m_mfcc = new MFCC(config);
|
c@42
|
168 m_fftSize = m_mfcc->getfftlength();
|
c@47
|
169 m_rhythmClipFrameSize = m_fftSize / 4;
|
c@42
|
170
|
c@53
|
171 // std::cerr << "MFCC FS = " << config.FS << ", FFT size = " << m_fftSize<< std::endl;
|
c@43
|
172
|
c@42
|
173 } else if (m_type == TypeChroma) {
|
c@42
|
174
|
c@42
|
175 m_featureColumnSize = 12;
|
c@42
|
176
|
c@42
|
177 ChromaConfig config;
|
c@47
|
178 config.FS = m_processRate;
|
c@42
|
179 config.min = Pitch::getFrequencyForPitch(24, 0, 440);
|
c@42
|
180 config.max = Pitch::getFrequencyForPitch(96, 0, 440);
|
c@42
|
181 config.BPO = 12;
|
c@42
|
182 config.CQThresh = 0.0054;
|
c@49
|
183 // We don't normalise the chromagram's columns individually;
|
c@49
|
184 // we normalise the mean at the end instead
|
c@49
|
185 config.normalise = MathUtilities::NormaliseNone;
|
c@42
|
186 m_chromagram = new Chromagram(config);
|
c@42
|
187 m_fftSize = m_chromagram->getFrameSize();
|
c@42
|
188
|
c@53
|
189 // std::cerr << "fftsize = " << m_fftSize << std::endl;
|
c@47
|
190
|
c@47
|
191 m_rhythmClipFrameSize = m_fftSize / 16;
|
c@47
|
192 while (m_rhythmClipFrameSize < 512) m_rhythmClipFrameSize *= 2;
|
c@53
|
193 // std::cerr << "m_rhythmClipFrameSize = " << m_rhythmClipFrameSize << std::endl;
|
c@47
|
194
|
c@53
|
195 // std::cerr << "min = "<< config.min << ", max = " << config.max << std::endl;
|
c@42
|
196
|
c@42
|
197 } else {
|
c@42
|
198
|
c@42
|
199 std::cerr << "SimilarityPlugin::initialise: internal error: unknown type " << m_type << std::endl;
|
c@42
|
200 return false;
|
c@42
|
201 }
|
c@41
|
202
|
c@47
|
203 if (needRhythm()) {
|
c@47
|
204 m_rhythmClipFrames =
|
c@47
|
205 int(ceil((m_rhythmClipDuration * m_processRate)
|
c@47
|
206 / m_rhythmClipFrameSize));
|
c@47
|
207 std::cerr << "SimilarityPlugin::initialise: rhythm clip is "
|
c@47
|
208 << m_rhythmClipFrames << " frames of size "
|
c@47
|
209 << m_rhythmClipFrameSize << " at process rate "
|
c@47
|
210 << m_processRate << " ( = "
|
c@47
|
211 << (float(m_rhythmClipFrames * m_rhythmClipFrameSize) / m_processRate) << " sec )"
|
c@47
|
212 << std::endl;
|
c@47
|
213
|
c@47
|
214 MFCCConfig config(m_processRate);
|
c@47
|
215 config.fftsize = m_rhythmClipFrameSize;
|
c@47
|
216 config.nceps = m_featureColumnSize - 1;
|
c@47
|
217 config.want_c0 = true;
|
c@47
|
218 config.logpower = 1;
|
c@47
|
219 config.window = RectangularWindow; // because no overlap
|
c@47
|
220 m_rhythmfcc = new MFCC(config);
|
c@47
|
221 }
|
c@47
|
222
|
c@41
|
223 for (int i = 0; i < m_channels; ++i) {
|
c@47
|
224
|
c@42
|
225 m_values.push_back(FeatureMatrix());
|
c@47
|
226
|
c@47
|
227 if (needRhythm()) {
|
c@47
|
228 m_rhythmValues.push_back(FeatureColumnQueue());
|
c@47
|
229 }
|
c@41
|
230 }
|
c@41
|
231
|
c@47
|
232 m_done = false;
|
c@47
|
233
|
c@41
|
234 return true;
|
c@41
|
235 }
|
c@41
|
236
|
c@41
|
237 void
|
c@41
|
238 SimilarityPlugin::reset()
|
c@41
|
239 {
|
c@57
|
240 for (int i = 0; i < m_values.size(); ++i) {
|
c@57
|
241 m_values[i].clear();
|
c@57
|
242 }
|
c@57
|
243
|
c@57
|
244 for (int i = 0; i < m_rhythmValues.size(); ++i) {
|
c@57
|
245 m_rhythmValues[i].clear();
|
c@57
|
246 }
|
c@57
|
247
|
c@47
|
248 m_done = false;
|
c@41
|
249 }
|
c@41
|
250
|
c@41
|
251 int
|
c@41
|
252 SimilarityPlugin::getDecimationFactor() const
|
c@41
|
253 {
|
c@41
|
254 int rate = lrintf(m_inputSampleRate);
|
c@47
|
255 return rate / m_processRate;
|
c@41
|
256 }
|
c@41
|
257
|
c@41
|
258 size_t
|
c@41
|
259 SimilarityPlugin::getPreferredStepSize() const
|
c@41
|
260 {
|
c@42
|
261 if (m_blockSize == 0) calculateBlockSize();
|
c@47
|
262
|
c@47
|
263 // there is also an assumption to this effect in process()
|
c@47
|
264 // (referring to m_fftSize/2 instead of a literal post-decimation
|
c@47
|
265 // step size):
|
c@45
|
266 return m_blockSize/2;
|
c@41
|
267 }
|
c@41
|
268
|
c@41
|
269 size_t
|
c@41
|
270 SimilarityPlugin::getPreferredBlockSize() const
|
c@41
|
271 {
|
c@42
|
272 if (m_blockSize == 0) calculateBlockSize();
|
c@42
|
273 return m_blockSize;
|
c@42
|
274 }
|
c@42
|
275
|
c@42
|
276 void
|
c@42
|
277 SimilarityPlugin::calculateBlockSize() const
|
c@42
|
278 {
|
c@42
|
279 if (m_blockSize != 0) return;
|
c@42
|
280 int decimationFactor = getDecimationFactor();
|
c@42
|
281 if (m_type == TypeChroma) {
|
c@42
|
282 ChromaConfig config;
|
c@47
|
283 config.FS = m_processRate;
|
c@42
|
284 config.min = Pitch::getFrequencyForPitch(24, 0, 440);
|
c@42
|
285 config.max = Pitch::getFrequencyForPitch(96, 0, 440);
|
c@42
|
286 config.BPO = 12;
|
c@42
|
287 config.CQThresh = 0.0054;
|
c@49
|
288 config.normalise = MathUtilities::NormaliseNone;
|
c@42
|
289 Chromagram *c = new Chromagram(config);
|
c@42
|
290 size_t sz = c->getFrameSize();
|
c@42
|
291 delete c;
|
c@42
|
292 m_blockSize = sz * decimationFactor;
|
c@42
|
293 } else {
|
c@42
|
294 m_blockSize = 2048 * decimationFactor;
|
c@42
|
295 }
|
c@41
|
296 }
|
c@41
|
297
|
c@41
|
298 SimilarityPlugin::ParameterList SimilarityPlugin::getParameterDescriptors() const
|
c@41
|
299 {
|
c@41
|
300 ParameterList list;
|
c@42
|
301
|
c@42
|
302 ParameterDescriptor desc;
|
c@42
|
303 desc.identifier = "featureType";
|
c@42
|
304 desc.name = "Feature Type";
|
c@48
|
305 desc.description = "Audio feature used for similarity measure. Timbral: use the first 20 MFCCs (19 plus C0). Chromatic: use 12 bin-per-octave chroma. Rhythmic: compare beat spectra of short regions.";
|
c@42
|
306 desc.unit = "";
|
c@42
|
307 desc.minValue = 0;
|
c@48
|
308 desc.maxValue = 4;
|
c@48
|
309 desc.defaultValue = 1;
|
c@42
|
310 desc.isQuantized = true;
|
c@42
|
311 desc.quantizeStep = 1;
|
c@48
|
312 desc.valueNames.push_back("Timbre");
|
c@48
|
313 desc.valueNames.push_back("Timbre and Rhythm");
|
c@48
|
314 desc.valueNames.push_back("Chroma");
|
c@48
|
315 desc.valueNames.push_back("Chroma and Rhythm");
|
c@48
|
316 desc.valueNames.push_back("Rhythm only");
|
c@42
|
317 list.push_back(desc);
|
c@48
|
318 /*
|
c@47
|
319 desc.identifier = "rhythmWeighting";
|
c@47
|
320 desc.name = "Influence of Rhythm";
|
c@47
|
321 desc.description = "Proportion of similarity measure made up from rhythmic similarity component, from 0 (entirely timbral or chromatic) to 100 (entirely rhythmic).";
|
c@47
|
322 desc.unit = "%";
|
c@47
|
323 desc.minValue = 0;
|
c@47
|
324 desc.maxValue = 100;
|
c@47
|
325 desc.defaultValue = 0;
|
c@48
|
326 desc.isQuantized = false;
|
c@47
|
327 desc.valueNames.clear();
|
c@47
|
328 list.push_back(desc);
|
c@48
|
329 */
|
c@41
|
330 return list;
|
c@41
|
331 }
|
c@41
|
332
|
c@41
|
333 float
|
c@41
|
334 SimilarityPlugin::getParameter(std::string param) const
|
c@41
|
335 {
|
c@42
|
336 if (param == "featureType") {
|
c@48
|
337
|
c@48
|
338 if (m_rhythmWeighting > m_allRhythm) {
|
c@48
|
339 return 4;
|
c@48
|
340 }
|
c@48
|
341
|
c@48
|
342 switch (m_type) {
|
c@48
|
343
|
c@48
|
344 case TypeMFCC:
|
c@48
|
345 if (m_rhythmWeighting < m_noRhythm) return 0;
|
c@48
|
346 else return 1;
|
c@48
|
347 break;
|
c@48
|
348
|
c@48
|
349 case TypeChroma:
|
c@48
|
350 if (m_rhythmWeighting < m_noRhythm) return 2;
|
c@48
|
351 else return 3;
|
c@48
|
352 break;
|
c@48
|
353 }
|
c@48
|
354
|
c@48
|
355 return 1;
|
c@48
|
356
|
c@48
|
357 // } else if (param == "rhythmWeighting") {
|
c@48
|
358 // return nearbyint(m_rhythmWeighting * 100.0);
|
c@42
|
359 }
|
c@42
|
360
|
c@41
|
361 std::cerr << "WARNING: SimilarityPlugin::getParameter: unknown parameter \""
|
c@41
|
362 << param << "\"" << std::endl;
|
c@41
|
363 return 0.0;
|
c@41
|
364 }
|
c@41
|
365
|
c@41
|
366 void
|
c@41
|
367 SimilarityPlugin::setParameter(std::string param, float value)
|
c@41
|
368 {
|
c@42
|
369 if (param == "featureType") {
|
c@48
|
370
|
c@42
|
371 int v = int(value + 0.1);
|
c@48
|
372
|
c@48
|
373 Type newType = m_type;
|
c@48
|
374
|
c@48
|
375 switch (v) {
|
c@48
|
376 case 0: newType = TypeMFCC; m_rhythmWeighting = 0.0f; break;
|
c@48
|
377 case 1: newType = TypeMFCC; m_rhythmWeighting = 0.5f; break;
|
c@48
|
378 case 2: newType = TypeChroma; m_rhythmWeighting = 0.0f; break;
|
c@48
|
379 case 3: newType = TypeChroma; m_rhythmWeighting = 0.5f; break;
|
c@48
|
380 case 4: newType = TypeMFCC; m_rhythmWeighting = 1.f; break;
|
c@48
|
381 }
|
c@48
|
382
|
c@48
|
383 if (newType != m_type) m_blockSize = 0;
|
c@48
|
384
|
c@48
|
385 m_type = newType;
|
c@42
|
386 return;
|
c@48
|
387
|
c@48
|
388 // } else if (param == "rhythmWeighting") {
|
c@48
|
389 // m_rhythmWeighting = value / 100;
|
c@48
|
390 // return;
|
c@42
|
391 }
|
c@42
|
392
|
c@41
|
393 std::cerr << "WARNING: SimilarityPlugin::setParameter: unknown parameter \""
|
c@41
|
394 << param << "\"" << std::endl;
|
c@41
|
395 }
|
c@41
|
396
|
c@41
|
397 SimilarityPlugin::OutputList
|
c@41
|
398 SimilarityPlugin::getOutputDescriptors() const
|
c@41
|
399 {
|
c@41
|
400 OutputList list;
|
c@41
|
401
|
c@41
|
402 OutputDescriptor similarity;
|
c@43
|
403 similarity.identifier = "distancematrix";
|
c@43
|
404 similarity.name = "Distance Matrix";
|
c@43
|
405 similarity.description = "Distance matrix for similarity metric. Smaller = more similar. Should be symmetrical.";
|
c@41
|
406 similarity.unit = "";
|
c@41
|
407 similarity.hasFixedBinCount = true;
|
c@41
|
408 similarity.binCount = m_channels;
|
c@41
|
409 similarity.hasKnownExtents = false;
|
c@41
|
410 similarity.isQuantized = false;
|
c@41
|
411 similarity.sampleType = OutputDescriptor::FixedSampleRate;
|
c@41
|
412 similarity.sampleRate = 1;
|
c@41
|
413
|
c@43
|
414 m_distanceMatrixOutput = list.size();
|
c@41
|
415 list.push_back(similarity);
|
c@41
|
416
|
c@43
|
417 OutputDescriptor simvec;
|
c@43
|
418 simvec.identifier = "distancevector";
|
c@43
|
419 simvec.name = "Distance from First Channel";
|
c@43
|
420 simvec.description = "Distance vector for similarity of each channel to the first channel. Smaller = more similar.";
|
c@43
|
421 simvec.unit = "";
|
c@43
|
422 simvec.hasFixedBinCount = true;
|
c@43
|
423 simvec.binCount = m_channels;
|
c@43
|
424 simvec.hasKnownExtents = false;
|
c@43
|
425 simvec.isQuantized = false;
|
c@43
|
426 simvec.sampleType = OutputDescriptor::FixedSampleRate;
|
c@43
|
427 simvec.sampleRate = 1;
|
c@43
|
428
|
c@43
|
429 m_distanceVectorOutput = list.size();
|
c@43
|
430 list.push_back(simvec);
|
c@43
|
431
|
c@44
|
432 OutputDescriptor sortvec;
|
c@44
|
433 sortvec.identifier = "sorteddistancevector";
|
c@44
|
434 sortvec.name = "Ordered Distances from First Channel";
|
c@44
|
435 sortvec.description = "Vector of the order of other channels in similarity to the first, followed by distance vector for similarity of each to the first. Smaller = more similar.";
|
c@44
|
436 sortvec.unit = "";
|
c@44
|
437 sortvec.hasFixedBinCount = true;
|
c@44
|
438 sortvec.binCount = m_channels;
|
c@44
|
439 sortvec.hasKnownExtents = false;
|
c@44
|
440 sortvec.isQuantized = false;
|
c@44
|
441 sortvec.sampleType = OutputDescriptor::FixedSampleRate;
|
c@44
|
442 sortvec.sampleRate = 1;
|
c@44
|
443
|
c@44
|
444 m_sortedVectorOutput = list.size();
|
c@44
|
445 list.push_back(sortvec);
|
c@44
|
446
|
c@41
|
447 OutputDescriptor means;
|
c@41
|
448 means.identifier = "means";
|
c@42
|
449 means.name = "Feature Means";
|
c@43
|
450 means.description = "Means of the feature bins. Feature time (sec) corresponds to input channel. Number of bins depends on selected feature type.";
|
c@41
|
451 means.unit = "";
|
c@41
|
452 means.hasFixedBinCount = true;
|
c@43
|
453 means.binCount = m_featureColumnSize;
|
c@41
|
454 means.hasKnownExtents = false;
|
c@41
|
455 means.isQuantized = false;
|
c@43
|
456 means.sampleType = OutputDescriptor::FixedSampleRate;
|
c@43
|
457 means.sampleRate = 1;
|
c@41
|
458
|
c@43
|
459 m_meansOutput = list.size();
|
c@41
|
460 list.push_back(means);
|
c@41
|
461
|
c@41
|
462 OutputDescriptor variances;
|
c@41
|
463 variances.identifier = "variances";
|
c@42
|
464 variances.name = "Feature Variances";
|
c@43
|
465 variances.description = "Variances of the feature bins. Feature time (sec) corresponds to input channel. Number of bins depends on selected feature type.";
|
c@41
|
466 variances.unit = "";
|
c@41
|
467 variances.hasFixedBinCount = true;
|
c@43
|
468 variances.binCount = m_featureColumnSize;
|
c@41
|
469 variances.hasKnownExtents = false;
|
c@41
|
470 variances.isQuantized = false;
|
c@43
|
471 variances.sampleType = OutputDescriptor::FixedSampleRate;
|
c@43
|
472 variances.sampleRate = 1;
|
c@41
|
473
|
c@43
|
474 m_variancesOutput = list.size();
|
c@41
|
475 list.push_back(variances);
|
c@41
|
476
|
c@47
|
477 OutputDescriptor beatspectrum;
|
c@47
|
478 beatspectrum.identifier = "beatspectrum";
|
c@47
|
479 beatspectrum.name = "Beat Spectra";
|
c@47
|
480 beatspectrum.description = "Rhythmic self-similarity vectors (beat spectra) for the input channels. Feature time (sec) corresponds to input channel. Not returned if rhythm weighting is zero.";
|
c@47
|
481 beatspectrum.unit = "";
|
c@47
|
482 if (m_rhythmClipFrames > 0) {
|
c@47
|
483 beatspectrum.hasFixedBinCount = true;
|
c@47
|
484 beatspectrum.binCount = m_rhythmClipFrames / 2;
|
c@47
|
485 } else {
|
c@47
|
486 beatspectrum.hasFixedBinCount = false;
|
c@47
|
487 }
|
c@47
|
488 beatspectrum.hasKnownExtents = false;
|
c@47
|
489 beatspectrum.isQuantized = false;
|
c@47
|
490 beatspectrum.sampleType = OutputDescriptor::FixedSampleRate;
|
c@47
|
491 beatspectrum.sampleRate = 1;
|
c@47
|
492
|
c@47
|
493 m_beatSpectraOutput = list.size();
|
c@47
|
494 list.push_back(beatspectrum);
|
c@47
|
495
|
c@41
|
496 return list;
|
c@41
|
497 }
|
c@41
|
498
|
c@41
|
499 SimilarityPlugin::FeatureSet
|
c@41
|
500 SimilarityPlugin::process(const float *const *inputBuffers, Vamp::RealTime /* timestamp */)
|
c@41
|
501 {
|
c@47
|
502 if (m_done) {
|
c@47
|
503 return FeatureSet();
|
c@47
|
504 }
|
c@47
|
505
|
c@41
|
506 double *dblbuf = new double[m_blockSize];
|
c@41
|
507 double *decbuf = dblbuf;
|
c@42
|
508 if (m_decimator) decbuf = new double[m_fftSize];
|
c@42
|
509
|
c@47
|
510 double *raw = new double[std::max(m_featureColumnSize,
|
c@47
|
511 m_rhythmColumnSize)];
|
c@41
|
512
|
c@43
|
513 float threshold = 1e-10;
|
c@43
|
514
|
c@47
|
515 bool someRhythmFrameNeeded = false;
|
c@47
|
516
|
c@41
|
517 for (size_t c = 0; c < m_channels; ++c) {
|
c@41
|
518
|
c@43
|
519 bool empty = true;
|
c@43
|
520
|
c@41
|
521 for (int i = 0; i < m_blockSize; ++i) {
|
c@43
|
522 float val = inputBuffers[c][i];
|
c@43
|
523 if (fabs(val) > threshold) empty = false;
|
c@43
|
524 dblbuf[i] = val;
|
c@41
|
525 }
|
c@41
|
526
|
c@47
|
527 if (empty) {
|
c@47
|
528 if (needRhythm() && ((m_frameNo % 2) == 0)) {
|
c@47
|
529 for (int i = 0; i < m_fftSize / m_rhythmClipFrameSize; ++i) {
|
c@47
|
530 if (m_rhythmValues[c].size() < m_rhythmClipFrames) {
|
c@47
|
531 FeatureColumn mf(m_rhythmColumnSize);
|
c@47
|
532 for (int i = 0; i < m_rhythmColumnSize; ++i) {
|
c@47
|
533 mf[i] = 0.0;
|
c@47
|
534 }
|
c@47
|
535 m_rhythmValues[c].push_back(mf);
|
c@47
|
536 }
|
c@47
|
537 }
|
c@47
|
538 }
|
c@47
|
539 continue;
|
c@47
|
540 }
|
c@47
|
541
|
c@44
|
542 m_lastNonEmptyFrame[c] = m_frameNo;
|
c@43
|
543
|
c@41
|
544 if (m_decimator) {
|
c@41
|
545 m_decimator->process(dblbuf, decbuf);
|
c@41
|
546 }
|
c@42
|
547
|
c@47
|
548 if (needTimbre()) {
|
c@47
|
549
|
c@47
|
550 if (m_type == TypeMFCC) {
|
c@47
|
551 m_mfcc->process(decbuf, raw);
|
c@47
|
552 } else if (m_type == TypeChroma) {
|
c@47
|
553 raw = m_chromagram->process(decbuf);
|
c@47
|
554 }
|
c@41
|
555
|
c@47
|
556 FeatureColumn mf(m_featureColumnSize);
|
c@47
|
557 for (int i = 0; i < m_featureColumnSize; ++i) {
|
c@47
|
558 mf[i] = raw[i];
|
c@47
|
559 }
|
c@47
|
560
|
c@47
|
561 m_values[c].push_back(mf);
|
c@44
|
562 }
|
c@41
|
563
|
c@47
|
564 // std::cerr << "needRhythm = " << needRhythm() << ", frame = " << m_frameNo << std::endl;
|
c@47
|
565
|
c@47
|
566 if (needRhythm() && ((m_frameNo % 2) == 0)) {
|
c@47
|
567
|
c@47
|
568 // The incoming frames are overlapping; we only use every
|
c@47
|
569 // other one, because we don't want the overlap (it would
|
c@47
|
570 // screw up the rhythm)
|
c@47
|
571
|
c@47
|
572 int frameOffset = 0;
|
c@47
|
573
|
c@47
|
574 while (frameOffset + m_rhythmClipFrameSize <= m_fftSize) {
|
c@47
|
575
|
c@47
|
576 bool needRhythmFrame = true;
|
c@47
|
577
|
c@47
|
578 if (m_rhythmValues[c].size() >= m_rhythmClipFrames) {
|
c@47
|
579
|
c@47
|
580 needRhythmFrame = false;
|
c@47
|
581
|
c@47
|
582 // assumes hopsize = framesize/2
|
c@47
|
583 float current = m_frameNo * (m_fftSize/2) + frameOffset;
|
c@47
|
584 current = current / m_processRate;
|
c@47
|
585 if (current - m_rhythmClipDuration < m_rhythmClipOrigin) {
|
c@47
|
586 needRhythmFrame = true;
|
c@47
|
587 m_rhythmValues[c].pop_front();
|
c@47
|
588 }
|
c@47
|
589
|
c@53
|
590 // if (needRhythmFrame) {
|
c@53
|
591 // std::cerr << "at current = " <<current << " (frame = " << m_frameNo << "), have " << m_rhythmValues[c].size() << ", need rhythm = " << needRhythmFrame << std::endl;
|
c@53
|
592 // }
|
c@47
|
593
|
c@47
|
594 }
|
c@47
|
595
|
c@47
|
596 if (needRhythmFrame) {
|
c@47
|
597
|
c@47
|
598 someRhythmFrameNeeded = true;
|
c@47
|
599
|
c@47
|
600 m_rhythmfcc->process(decbuf + frameOffset, raw);
|
c@47
|
601
|
c@47
|
602 FeatureColumn mf(m_rhythmColumnSize);
|
c@47
|
603 for (int i = 0; i < m_rhythmColumnSize; ++i) {
|
c@47
|
604 mf[i] = raw[i];
|
c@47
|
605 }
|
c@47
|
606
|
c@47
|
607 m_rhythmValues[c].push_back(mf);
|
c@47
|
608 }
|
c@47
|
609
|
c@47
|
610 frameOffset += m_rhythmClipFrameSize;
|
c@47
|
611 }
|
c@47
|
612 }
|
c@47
|
613 }
|
c@47
|
614
|
c@47
|
615 if (!needTimbre() && !someRhythmFrameNeeded && ((m_frameNo % 2) == 0)) {
|
c@53
|
616 // std::cerr << "done!" << std::endl;
|
c@47
|
617 m_done = true;
|
c@41
|
618 }
|
c@41
|
619
|
c@41
|
620 if (m_decimator) delete[] decbuf;
|
c@41
|
621 delete[] dblbuf;
|
c@47
|
622 delete[] raw;
|
c@41
|
623
|
c@44
|
624 ++m_frameNo;
|
c@44
|
625
|
c@41
|
626 return FeatureSet();
|
c@41
|
627 }
|
c@41
|
628
|
c@47
|
629 SimilarityPlugin::FeatureMatrix
|
c@47
|
630 SimilarityPlugin::calculateTimbral(FeatureSet &returnFeatures)
|
c@41
|
631 {
|
c@47
|
632 FeatureMatrix m(m_channels); // means
|
c@47
|
633 FeatureMatrix v(m_channels); // variances
|
c@41
|
634
|
c@41
|
635 for (int i = 0; i < m_channels; ++i) {
|
c@41
|
636
|
c@42
|
637 FeatureColumn mean(m_featureColumnSize), variance(m_featureColumnSize);
|
c@41
|
638
|
c@42
|
639 for (int j = 0; j < m_featureColumnSize; ++j) {
|
c@41
|
640
|
c@43
|
641 mean[j] = 0.0;
|
c@43
|
642 variance[j] = 0.0;
|
c@41
|
643 int count;
|
c@41
|
644
|
c@44
|
645 // We want to take values up to, but not including, the
|
c@44
|
646 // last non-empty frame (which may be partial)
|
c@43
|
647
|
c@44
|
648 int sz = m_lastNonEmptyFrame[i];
|
c@44
|
649 if (sz < 0) sz = 0;
|
c@43
|
650
|
c@41
|
651 count = 0;
|
c@43
|
652 for (int k = 0; k < sz; ++k) {
|
c@42
|
653 double val = m_values[i][k][j];
|
c@41
|
654 if (isnan(val) || isinf(val)) continue;
|
c@41
|
655 mean[j] += val;
|
c@41
|
656 ++count;
|
c@41
|
657 }
|
c@41
|
658 if (count > 0) mean[j] /= count;
|
c@41
|
659
|
c@41
|
660 count = 0;
|
c@43
|
661 for (int k = 0; k < sz; ++k) {
|
c@42
|
662 double val = ((m_values[i][k][j] - mean[j]) *
|
c@42
|
663 (m_values[i][k][j] - mean[j]));
|
c@41
|
664 if (isnan(val) || isinf(val)) continue;
|
c@41
|
665 variance[j] += val;
|
c@41
|
666 ++count;
|
c@41
|
667 }
|
c@41
|
668 if (count > 0) variance[j] /= count;
|
c@41
|
669 }
|
c@41
|
670
|
c@41
|
671 m[i] = mean;
|
c@41
|
672 v[i] = variance;
|
c@41
|
673 }
|
c@41
|
674
|
c@47
|
675 FeatureMatrix distances(m_channels);
|
c@42
|
676
|
c@48
|
677 if (m_type == TypeMFCC) {
|
c@48
|
678
|
c@48
|
679 // "Despite the fact that MFCCs extracted from music are
|
c@48
|
680 // clearly not Gaussian, [14] showed, somewhat surprisingly,
|
c@48
|
681 // that a similarity function comparing single Gaussians
|
c@48
|
682 // modelling MFCCs for each track can perform as well as
|
c@48
|
683 // mixture models. A great advantage of using single
|
c@48
|
684 // Gaussians is that a simple closed form exists for the KL
|
c@48
|
685 // divergence." -- Mark Levy, "Lightweight measures for
|
c@48
|
686 // timbral similarity of musical audio"
|
c@48
|
687 // (http://www.elec.qmul.ac.uk/easaier/papers/mlevytimbralsimilarity.pdf)
|
c@48
|
688
|
c@48
|
689 KLDivergence kld;
|
c@48
|
690
|
c@48
|
691 for (int i = 0; i < m_channels; ++i) {
|
c@48
|
692 for (int j = 0; j < m_channels; ++j) {
|
c@48
|
693 double d = kld.distanceGaussian(m[i], v[i], m[j], v[j]);
|
c@48
|
694 distances[i].push_back(d);
|
c@48
|
695 }
|
c@48
|
696 }
|
c@48
|
697
|
c@48
|
698 } else {
|
c@48
|
699
|
c@49
|
700 // We use the KL divergence for distributions of discrete
|
c@49
|
701 // variables, as chroma are histograms already. Or at least,
|
c@49
|
702 // they will be when we've normalised them like this:
|
c@49
|
703 for (int i = 0; i < m_channels; ++i) {
|
c@49
|
704 MathUtilities::normalise(m[i], MathUtilities::NormaliseUnitSum);
|
c@49
|
705 }
|
c@48
|
706
|
c@48
|
707 KLDivergence kld;
|
c@48
|
708
|
c@48
|
709 for (int i = 0; i < m_channels; ++i) {
|
c@48
|
710 for (int j = 0; j < m_channels; ++j) {
|
c@48
|
711 double d = kld.distanceDistribution(m[i], m[j], true);
|
c@48
|
712 distances[i].push_back(d);
|
c@48
|
713 }
|
c@41
|
714 }
|
c@41
|
715 }
|
c@47
|
716
|
c@44
|
717 Feature feature;
|
c@44
|
718 feature.hasTimestamp = true;
|
c@44
|
719
|
c@44
|
720 char labelBuffer[100];
|
c@43
|
721
|
c@41
|
722 for (int i = 0; i < m_channels; ++i) {
|
c@41
|
723
|
c@41
|
724 feature.timestamp = Vamp::RealTime(i, 0);
|
c@41
|
725
|
c@44
|
726 sprintf(labelBuffer, "Means for channel %d", i+1);
|
c@44
|
727 feature.label = labelBuffer;
|
c@44
|
728
|
c@41
|
729 feature.values.clear();
|
c@42
|
730 for (int k = 0; k < m_featureColumnSize; ++k) {
|
c@41
|
731 feature.values.push_back(m[i][k]);
|
c@41
|
732 }
|
c@41
|
733
|
c@43
|
734 returnFeatures[m_meansOutput].push_back(feature);
|
c@41
|
735
|
c@44
|
736 sprintf(labelBuffer, "Variances for channel %d", i+1);
|
c@44
|
737 feature.label = labelBuffer;
|
c@44
|
738
|
c@41
|
739 feature.values.clear();
|
c@42
|
740 for (int k = 0; k < m_featureColumnSize; ++k) {
|
c@41
|
741 feature.values.push_back(v[i][k]);
|
c@41
|
742 }
|
c@41
|
743
|
c@43
|
744 returnFeatures[m_variancesOutput].push_back(feature);
|
c@47
|
745 }
|
c@47
|
746
|
c@47
|
747 return distances;
|
c@47
|
748 }
|
c@47
|
749
|
c@47
|
750 SimilarityPlugin::FeatureMatrix
|
c@47
|
751 SimilarityPlugin::calculateRhythmic(FeatureSet &returnFeatures)
|
c@47
|
752 {
|
c@47
|
753 if (!needRhythm()) return FeatureMatrix();
|
c@47
|
754
|
c@47
|
755 BeatSpectrum bscalc;
|
c@47
|
756 CosineDistance cd;
|
c@47
|
757
|
c@47
|
758 // Our rhythm feature matrix is a deque of vectors for practical
|
c@47
|
759 // reasons, but BeatSpectrum::process wants a vector of vectors
|
c@47
|
760 // (which is what FeatureMatrix happens to be).
|
c@47
|
761
|
c@47
|
762 FeatureMatrixSet bsinput(m_channels);
|
c@47
|
763 for (int i = 0; i < m_channels; ++i) {
|
c@47
|
764 for (int j = 0; j < m_rhythmValues[i].size(); ++j) {
|
c@47
|
765 bsinput[i].push_back(m_rhythmValues[i][j]);
|
c@47
|
766 }
|
c@47
|
767 }
|
c@47
|
768
|
c@47
|
769 FeatureMatrix bs(m_channels);
|
c@47
|
770 for (int i = 0; i < m_channels; ++i) {
|
c@47
|
771 bs[i] = bscalc.process(bsinput[i]);
|
c@47
|
772 }
|
c@47
|
773
|
c@47
|
774 FeatureMatrix distances(m_channels);
|
c@47
|
775 for (int i = 0; i < m_channels; ++i) {
|
c@47
|
776 for (int j = 0; j < m_channels; ++j) {
|
c@47
|
777 double d = cd.distance(bs[i], bs[j]);
|
c@47
|
778 distances[i].push_back(d);
|
c@47
|
779 }
|
c@47
|
780 }
|
c@47
|
781
|
c@47
|
782 Feature feature;
|
c@47
|
783 feature.hasTimestamp = true;
|
c@47
|
784
|
c@47
|
785 char labelBuffer[100];
|
c@47
|
786
|
c@47
|
787 for (int i = 0; i < m_channels; ++i) {
|
c@47
|
788
|
c@47
|
789 feature.timestamp = Vamp::RealTime(i, 0);
|
c@47
|
790
|
c@47
|
791 sprintf(labelBuffer, "Beat spectrum for channel %d", i+1);
|
c@47
|
792 feature.label = labelBuffer;
|
c@47
|
793
|
c@47
|
794 feature.values.clear();
|
c@47
|
795 for (int j = 0; j < bs[i].size(); ++j) {
|
c@47
|
796 feature.values.push_back(bs[i][j]);
|
c@47
|
797 }
|
c@47
|
798
|
c@47
|
799 returnFeatures[m_beatSpectraOutput].push_back(feature);
|
c@47
|
800 }
|
c@47
|
801
|
c@47
|
802 return distances;
|
c@47
|
803 }
|
c@47
|
804
|
c@47
|
805 double
|
c@47
|
806 SimilarityPlugin::getDistance(const FeatureMatrix &timbral,
|
c@47
|
807 const FeatureMatrix &rhythmic,
|
c@47
|
808 int i, int j)
|
c@47
|
809 {
|
c@47
|
810 double distance = 1.0;
|
c@47
|
811 if (needTimbre()) distance *= timbral[i][j];
|
c@47
|
812 if (needRhythm()) distance *= rhythmic[i][j];
|
c@47
|
813 return distance;
|
c@47
|
814 }
|
c@47
|
815
|
c@47
|
816 SimilarityPlugin::FeatureSet
|
c@47
|
817 SimilarityPlugin::getRemainingFeatures()
|
c@47
|
818 {
|
c@47
|
819 FeatureSet returnFeatures;
|
c@47
|
820
|
c@47
|
821 // We want to return a matrix of the distances between channels,
|
c@47
|
822 // but Vamp doesn't have a matrix return type so we will actually
|
c@47
|
823 // return a series of vectors
|
c@47
|
824
|
c@47
|
825 FeatureMatrix timbralDistances, rhythmicDistances;
|
c@47
|
826
|
c@47
|
827 if (needTimbre()) {
|
c@47
|
828 timbralDistances = calculateTimbral(returnFeatures);
|
c@47
|
829 }
|
c@47
|
830
|
c@47
|
831 if (needRhythm()) {
|
c@47
|
832 rhythmicDistances = calculateRhythmic(returnFeatures);
|
c@47
|
833 }
|
c@47
|
834
|
c@47
|
835 // We give all features a timestamp, otherwise hosts will tend to
|
c@47
|
836 // stamp them at the end of the file, which is annoying
|
c@47
|
837
|
c@47
|
838 Feature feature;
|
c@47
|
839 feature.hasTimestamp = true;
|
c@47
|
840
|
c@47
|
841 Feature distanceVectorFeature;
|
c@47
|
842 distanceVectorFeature.label = "Distance from first channel";
|
c@47
|
843 distanceVectorFeature.hasTimestamp = true;
|
c@47
|
844 distanceVectorFeature.timestamp = Vamp::RealTime::zeroTime;
|
c@47
|
845
|
c@47
|
846 std::map<double, int> sorted;
|
c@47
|
847
|
c@47
|
848 char labelBuffer[100];
|
c@47
|
849
|
c@47
|
850 for (int i = 0; i < m_channels; ++i) {
|
c@47
|
851
|
c@47
|
852 feature.timestamp = Vamp::RealTime(i, 0);
|
c@41
|
853
|
c@41
|
854 feature.values.clear();
|
c@41
|
855 for (int j = 0; j < m_channels; ++j) {
|
c@47
|
856 double dist = getDistance(timbralDistances, rhythmicDistances, i, j);
|
c@47
|
857 feature.values.push_back(dist);
|
c@41
|
858 }
|
c@43
|
859
|
c@44
|
860 sprintf(labelBuffer, "Distances from channel %d", i+1);
|
c@44
|
861 feature.label = labelBuffer;
|
c@41
|
862
|
c@43
|
863 returnFeatures[m_distanceMatrixOutput].push_back(feature);
|
c@43
|
864
|
c@47
|
865 double fromFirst =
|
c@47
|
866 getDistance(timbralDistances, rhythmicDistances, 0, i);
|
c@44
|
867
|
c@47
|
868 distanceVectorFeature.values.push_back(fromFirst);
|
c@47
|
869 sorted[fromFirst] = i;
|
c@41
|
870 }
|
c@41
|
871
|
c@43
|
872 returnFeatures[m_distanceVectorOutput].push_back(distanceVectorFeature);
|
c@43
|
873
|
c@44
|
874 feature.label = "Order of channels by similarity to first channel";
|
c@44
|
875 feature.values.clear();
|
c@44
|
876 feature.timestamp = Vamp::RealTime(0, 0);
|
c@44
|
877
|
c@44
|
878 for (std::map<double, int>::iterator i = sorted.begin();
|
c@44
|
879 i != sorted.end(); ++i) {
|
c@45
|
880 feature.values.push_back(i->second + 1);
|
c@44
|
881 }
|
c@44
|
882
|
c@44
|
883 returnFeatures[m_sortedVectorOutput].push_back(feature);
|
c@44
|
884
|
c@44
|
885 feature.label = "Ordered distances of channels from first channel";
|
c@44
|
886 feature.values.clear();
|
c@44
|
887 feature.timestamp = Vamp::RealTime(1, 0);
|
c@44
|
888
|
c@44
|
889 for (std::map<double, int>::iterator i = sorted.begin();
|
c@44
|
890 i != sorted.end(); ++i) {
|
c@44
|
891 feature.values.push_back(i->first);
|
c@44
|
892 }
|
c@44
|
893
|
c@44
|
894 returnFeatures[m_sortedVectorOutput].push_back(feature);
|
c@44
|
895
|
c@41
|
896 return returnFeatures;
|
c@41
|
897 }
|