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@41
|
18
|
c@41
|
19 using std::string;
|
c@41
|
20 using std::vector;
|
c@41
|
21 using std::cerr;
|
c@41
|
22 using std::endl;
|
c@41
|
23 using std::ostringstream;
|
c@41
|
24
|
c@41
|
25 SimilarityPlugin::SimilarityPlugin(float inputSampleRate) :
|
c@41
|
26 Plugin(inputSampleRate),
|
c@42
|
27 m_type(TypeMFCC),
|
c@41
|
28 m_mfcc(0),
|
c@42
|
29 m_chromagram(0),
|
c@41
|
30 m_decimator(0),
|
c@42
|
31 m_featureColumnSize(20),
|
c@41
|
32 m_blockSize(0),
|
c@41
|
33 m_channels(0)
|
c@41
|
34 {
|
c@41
|
35
|
c@41
|
36 }
|
c@41
|
37
|
c@41
|
38 SimilarityPlugin::~SimilarityPlugin()
|
c@41
|
39 {
|
c@41
|
40 delete m_mfcc;
|
c@42
|
41 delete m_chromagram;
|
c@41
|
42 delete m_decimator;
|
c@41
|
43 }
|
c@41
|
44
|
c@41
|
45 string
|
c@41
|
46 SimilarityPlugin::getIdentifier() const
|
c@41
|
47 {
|
c@41
|
48 return "qm-similarity";
|
c@41
|
49 }
|
c@41
|
50
|
c@41
|
51 string
|
c@41
|
52 SimilarityPlugin::getName() const
|
c@41
|
53 {
|
c@41
|
54 return "Similarity";
|
c@41
|
55 }
|
c@41
|
56
|
c@41
|
57 string
|
c@41
|
58 SimilarityPlugin::getDescription() const
|
c@41
|
59 {
|
c@42
|
60 return "Return a distance matrix for similarity between the input audio channels";
|
c@41
|
61 }
|
c@41
|
62
|
c@41
|
63 string
|
c@41
|
64 SimilarityPlugin::getMaker() const
|
c@41
|
65 {
|
c@41
|
66 return "Chris Cannam, Queen Mary, University of London";
|
c@41
|
67 }
|
c@41
|
68
|
c@41
|
69 int
|
c@41
|
70 SimilarityPlugin::getPluginVersion() const
|
c@41
|
71 {
|
c@41
|
72 return 1;
|
c@41
|
73 }
|
c@41
|
74
|
c@41
|
75 string
|
c@41
|
76 SimilarityPlugin::getCopyright() const
|
c@41
|
77 {
|
c@41
|
78 return "Copyright (c) 2008 - All Rights Reserved";
|
c@41
|
79 }
|
c@41
|
80
|
c@41
|
81 size_t
|
c@41
|
82 SimilarityPlugin::getMinChannelCount() const
|
c@41
|
83 {
|
c@43
|
84 return 1;
|
c@41
|
85 }
|
c@41
|
86
|
c@41
|
87 size_t
|
c@41
|
88 SimilarityPlugin::getMaxChannelCount() const
|
c@41
|
89 {
|
c@41
|
90 return 1024;
|
c@44
|
91 // return 1;
|
c@41
|
92 }
|
c@41
|
93
|
c@41
|
94 bool
|
c@41
|
95 SimilarityPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@41
|
96 {
|
c@41
|
97 if (channels < getMinChannelCount() ||
|
c@41
|
98 channels > getMaxChannelCount()) return false;
|
c@41
|
99
|
c@41
|
100 if (stepSize != getPreferredStepSize()) {
|
c@43
|
101 //!!! actually this perhaps shouldn't be an error... similarly
|
c@43
|
102 //using more than getMaxChannelCount channels
|
c@41
|
103 std::cerr << "SimilarityPlugin::initialise: supplied step size "
|
c@41
|
104 << stepSize << " differs from required step size "
|
c@41
|
105 << getPreferredStepSize() << std::endl;
|
c@41
|
106 return false;
|
c@41
|
107 }
|
c@41
|
108
|
c@41
|
109 if (blockSize != getPreferredBlockSize()) {
|
c@41
|
110 std::cerr << "SimilarityPlugin::initialise: supplied block size "
|
c@41
|
111 << blockSize << " differs from required block size "
|
c@41
|
112 << getPreferredBlockSize() << std::endl;
|
c@41
|
113 return false;
|
c@41
|
114 }
|
c@41
|
115
|
c@41
|
116 m_blockSize = blockSize;
|
c@41
|
117 m_channels = channels;
|
c@41
|
118
|
c@44
|
119 m_lastNonEmptyFrame = std::vector<int>(m_channels);
|
c@44
|
120 for (int i = 0; i < m_channels; ++i) m_lastNonEmptyFrame[i] = -1;
|
c@44
|
121 m_frameNo = 0;
|
c@44
|
122
|
c@41
|
123 int decimationFactor = getDecimationFactor();
|
c@41
|
124 if (decimationFactor > 1) {
|
c@42
|
125 m_decimator = new Decimator(m_blockSize, decimationFactor);
|
c@41
|
126 }
|
c@41
|
127
|
c@42
|
128 if (m_type == TypeMFCC) {
|
c@42
|
129
|
c@42
|
130 m_featureColumnSize = 20;
|
c@42
|
131
|
c@42
|
132 MFCCConfig config;
|
c@42
|
133 config.FS = lrintf(m_inputSampleRate) / decimationFactor;
|
c@42
|
134 config.fftsize = 2048;
|
c@42
|
135 config.nceps = m_featureColumnSize - 1;
|
c@42
|
136 config.want_c0 = true;
|
c@42
|
137 m_mfcc = new MFCC(config);
|
c@42
|
138 m_fftSize = m_mfcc->getfftlength();
|
c@42
|
139
|
c@43
|
140 std::cerr << "MFCC FS = " << config.FS << ", FFT size = " << m_fftSize<< std::endl;
|
c@43
|
141
|
c@42
|
142 } else if (m_type == TypeChroma) {
|
c@42
|
143
|
c@42
|
144 m_featureColumnSize = 12;
|
c@42
|
145
|
c@42
|
146 ChromaConfig config;
|
c@42
|
147 config.FS = lrintf(m_inputSampleRate) / decimationFactor;
|
c@42
|
148 config.min = Pitch::getFrequencyForPitch(24, 0, 440);
|
c@42
|
149 config.max = Pitch::getFrequencyForPitch(96, 0, 440);
|
c@42
|
150 config.BPO = 12;
|
c@42
|
151 config.CQThresh = 0.0054;
|
c@42
|
152 config.isNormalised = true;
|
c@42
|
153 m_chromagram = new Chromagram(config);
|
c@42
|
154 m_fftSize = m_chromagram->getFrameSize();
|
c@42
|
155
|
c@42
|
156 std::cerr << "min = "<< config.min << ", max = " << config.max << std::endl;
|
c@42
|
157
|
c@42
|
158 } else {
|
c@42
|
159
|
c@42
|
160 std::cerr << "SimilarityPlugin::initialise: internal error: unknown type " << m_type << std::endl;
|
c@42
|
161 return false;
|
c@42
|
162 }
|
c@41
|
163
|
c@41
|
164 for (int i = 0; i < m_channels; ++i) {
|
c@42
|
165 m_values.push_back(FeatureMatrix());
|
c@41
|
166 }
|
c@41
|
167
|
c@41
|
168 return true;
|
c@41
|
169 }
|
c@41
|
170
|
c@41
|
171 void
|
c@41
|
172 SimilarityPlugin::reset()
|
c@41
|
173 {
|
c@41
|
174 //!!!
|
c@41
|
175 }
|
c@41
|
176
|
c@41
|
177 int
|
c@41
|
178 SimilarityPlugin::getDecimationFactor() const
|
c@41
|
179 {
|
c@41
|
180 int rate = lrintf(m_inputSampleRate);
|
c@41
|
181 int internalRate = 22050;
|
c@41
|
182 int decimationFactor = rate / internalRate;
|
c@41
|
183 if (decimationFactor < 1) decimationFactor = 1;
|
c@41
|
184
|
c@41
|
185 // must be a power of two
|
c@41
|
186 while (decimationFactor & (decimationFactor - 1)) ++decimationFactor;
|
c@41
|
187
|
c@41
|
188 return decimationFactor;
|
c@41
|
189 }
|
c@41
|
190
|
c@41
|
191 size_t
|
c@41
|
192 SimilarityPlugin::getPreferredStepSize() const
|
c@41
|
193 {
|
c@42
|
194 if (m_blockSize == 0) calculateBlockSize();
|
c@43
|
195 if (m_type == TypeChroma) {
|
c@43
|
196 return m_blockSize/2;
|
c@43
|
197 } else {
|
c@43
|
198 // for compatibility with old-skool Soundbite, which doesn't
|
c@43
|
199 // overlap blocks on input
|
c@43
|
200 return m_blockSize;
|
c@43
|
201 }
|
c@41
|
202 }
|
c@41
|
203
|
c@41
|
204 size_t
|
c@41
|
205 SimilarityPlugin::getPreferredBlockSize() const
|
c@41
|
206 {
|
c@42
|
207 if (m_blockSize == 0) calculateBlockSize();
|
c@42
|
208 return m_blockSize;
|
c@42
|
209 }
|
c@42
|
210
|
c@42
|
211 void
|
c@42
|
212 SimilarityPlugin::calculateBlockSize() const
|
c@42
|
213 {
|
c@42
|
214 if (m_blockSize != 0) return;
|
c@42
|
215 int decimationFactor = getDecimationFactor();
|
c@42
|
216 if (m_type == TypeChroma) {
|
c@42
|
217 ChromaConfig config;
|
c@42
|
218 config.FS = lrintf(m_inputSampleRate) / decimationFactor;
|
c@42
|
219 config.min = Pitch::getFrequencyForPitch(24, 0, 440);
|
c@42
|
220 config.max = Pitch::getFrequencyForPitch(96, 0, 440);
|
c@42
|
221 config.BPO = 12;
|
c@42
|
222 config.CQThresh = 0.0054;
|
c@42
|
223 config.isNormalised = false;
|
c@42
|
224 Chromagram *c = new Chromagram(config);
|
c@42
|
225 size_t sz = c->getFrameSize();
|
c@42
|
226 delete c;
|
c@42
|
227 m_blockSize = sz * decimationFactor;
|
c@42
|
228 } else {
|
c@42
|
229 m_blockSize = 2048 * decimationFactor;
|
c@42
|
230 }
|
c@41
|
231 }
|
c@41
|
232
|
c@41
|
233 SimilarityPlugin::ParameterList SimilarityPlugin::getParameterDescriptors() const
|
c@41
|
234 {
|
c@41
|
235 ParameterList list;
|
c@42
|
236
|
c@42
|
237 ParameterDescriptor desc;
|
c@42
|
238 desc.identifier = "featureType";
|
c@42
|
239 desc.name = "Feature Type";
|
c@42
|
240 desc.description = "";//!!!
|
c@42
|
241 desc.unit = "";
|
c@42
|
242 desc.minValue = 0;
|
c@42
|
243 desc.maxValue = 1;
|
c@42
|
244 desc.defaultValue = 0;
|
c@42
|
245 desc.isQuantized = true;
|
c@42
|
246 desc.quantizeStep = 1;
|
c@42
|
247 desc.valueNames.push_back("Timbral (MFCC)");
|
c@42
|
248 desc.valueNames.push_back("Chromatic (Chroma)");
|
c@42
|
249 list.push_back(desc);
|
c@42
|
250
|
c@41
|
251 return list;
|
c@41
|
252 }
|
c@41
|
253
|
c@41
|
254 float
|
c@41
|
255 SimilarityPlugin::getParameter(std::string param) const
|
c@41
|
256 {
|
c@42
|
257 if (param == "featureType") {
|
c@42
|
258 if (m_type == TypeMFCC) return 0;
|
c@42
|
259 else if (m_type == TypeChroma) return 1;
|
c@42
|
260 else return 0;
|
c@42
|
261 }
|
c@42
|
262
|
c@41
|
263 std::cerr << "WARNING: SimilarityPlugin::getParameter: unknown parameter \""
|
c@41
|
264 << param << "\"" << std::endl;
|
c@41
|
265 return 0.0;
|
c@41
|
266 }
|
c@41
|
267
|
c@41
|
268 void
|
c@41
|
269 SimilarityPlugin::setParameter(std::string param, float value)
|
c@41
|
270 {
|
c@42
|
271 if (param == "featureType") {
|
c@42
|
272 int v = int(value + 0.1);
|
c@42
|
273 Type prevType = m_type;
|
c@42
|
274 if (v == 0) m_type = TypeMFCC;
|
c@42
|
275 else if (v == 1) m_type = TypeChroma;
|
c@42
|
276 if (m_type != prevType) m_blockSize = 0;
|
c@42
|
277 return;
|
c@42
|
278 }
|
c@42
|
279
|
c@41
|
280 std::cerr << "WARNING: SimilarityPlugin::setParameter: unknown parameter \""
|
c@41
|
281 << param << "\"" << std::endl;
|
c@41
|
282 }
|
c@41
|
283
|
c@41
|
284 SimilarityPlugin::OutputList
|
c@41
|
285 SimilarityPlugin::getOutputDescriptors() const
|
c@41
|
286 {
|
c@41
|
287 OutputList list;
|
c@41
|
288
|
c@41
|
289 OutputDescriptor similarity;
|
c@43
|
290 similarity.identifier = "distancematrix";
|
c@43
|
291 similarity.name = "Distance Matrix";
|
c@43
|
292 similarity.description = "Distance matrix for similarity metric. Smaller = more similar. Should be symmetrical.";
|
c@41
|
293 similarity.unit = "";
|
c@41
|
294 similarity.hasFixedBinCount = true;
|
c@41
|
295 similarity.binCount = m_channels;
|
c@41
|
296 similarity.hasKnownExtents = false;
|
c@41
|
297 similarity.isQuantized = false;
|
c@41
|
298 similarity.sampleType = OutputDescriptor::FixedSampleRate;
|
c@41
|
299 similarity.sampleRate = 1;
|
c@41
|
300
|
c@43
|
301 m_distanceMatrixOutput = list.size();
|
c@41
|
302 list.push_back(similarity);
|
c@41
|
303
|
c@43
|
304 OutputDescriptor simvec;
|
c@43
|
305 simvec.identifier = "distancevector";
|
c@43
|
306 simvec.name = "Distance from First Channel";
|
c@43
|
307 simvec.description = "Distance vector for similarity of each channel to the first channel. Smaller = more similar.";
|
c@43
|
308 simvec.unit = "";
|
c@43
|
309 simvec.hasFixedBinCount = true;
|
c@43
|
310 simvec.binCount = m_channels;
|
c@43
|
311 simvec.hasKnownExtents = false;
|
c@43
|
312 simvec.isQuantized = false;
|
c@43
|
313 simvec.sampleType = OutputDescriptor::FixedSampleRate;
|
c@43
|
314 simvec.sampleRate = 1;
|
c@43
|
315
|
c@43
|
316 m_distanceVectorOutput = list.size();
|
c@43
|
317 list.push_back(simvec);
|
c@43
|
318
|
c@44
|
319 OutputDescriptor sortvec;
|
c@44
|
320 sortvec.identifier = "sorteddistancevector";
|
c@44
|
321 sortvec.name = "Ordered Distances from First Channel";
|
c@44
|
322 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
|
323 sortvec.unit = "";
|
c@44
|
324 sortvec.hasFixedBinCount = true;
|
c@44
|
325 sortvec.binCount = m_channels;
|
c@44
|
326 sortvec.hasKnownExtents = false;
|
c@44
|
327 sortvec.isQuantized = false;
|
c@44
|
328 sortvec.sampleType = OutputDescriptor::FixedSampleRate;
|
c@44
|
329 sortvec.sampleRate = 1;
|
c@44
|
330
|
c@44
|
331 m_sortedVectorOutput = list.size();
|
c@44
|
332 list.push_back(sortvec);
|
c@44
|
333
|
c@41
|
334 OutputDescriptor means;
|
c@41
|
335 means.identifier = "means";
|
c@42
|
336 means.name = "Feature Means";
|
c@43
|
337 means.description = "Means of the feature bins. Feature time (sec) corresponds to input channel. Number of bins depends on selected feature type.";
|
c@41
|
338 means.unit = "";
|
c@41
|
339 means.hasFixedBinCount = true;
|
c@43
|
340 means.binCount = m_featureColumnSize;
|
c@41
|
341 means.hasKnownExtents = false;
|
c@41
|
342 means.isQuantized = false;
|
c@43
|
343 means.sampleType = OutputDescriptor::FixedSampleRate;
|
c@43
|
344 means.sampleRate = 1;
|
c@41
|
345
|
c@43
|
346 m_meansOutput = list.size();
|
c@41
|
347 list.push_back(means);
|
c@41
|
348
|
c@41
|
349 OutputDescriptor variances;
|
c@41
|
350 variances.identifier = "variances";
|
c@42
|
351 variances.name = "Feature Variances";
|
c@43
|
352 variances.description = "Variances of the feature bins. Feature time (sec) corresponds to input channel. Number of bins depends on selected feature type.";
|
c@41
|
353 variances.unit = "";
|
c@41
|
354 variances.hasFixedBinCount = true;
|
c@43
|
355 variances.binCount = m_featureColumnSize;
|
c@41
|
356 variances.hasKnownExtents = false;
|
c@41
|
357 variances.isQuantized = false;
|
c@43
|
358 variances.sampleType = OutputDescriptor::FixedSampleRate;
|
c@43
|
359 variances.sampleRate = 1;
|
c@41
|
360
|
c@43
|
361 m_variancesOutput = list.size();
|
c@41
|
362 list.push_back(variances);
|
c@41
|
363
|
c@41
|
364 return list;
|
c@41
|
365 }
|
c@41
|
366
|
c@41
|
367 SimilarityPlugin::FeatureSet
|
c@41
|
368 SimilarityPlugin::process(const float *const *inputBuffers, Vamp::RealTime /* timestamp */)
|
c@41
|
369 {
|
c@41
|
370 double *dblbuf = new double[m_blockSize];
|
c@41
|
371 double *decbuf = dblbuf;
|
c@42
|
372 if (m_decimator) decbuf = new double[m_fftSize];
|
c@42
|
373
|
c@42
|
374 double *raw = 0;
|
c@42
|
375 bool ownRaw = false;
|
c@42
|
376
|
c@42
|
377 if (m_type == TypeMFCC) {
|
c@42
|
378 raw = new double[m_featureColumnSize];
|
c@42
|
379 ownRaw = true;
|
c@42
|
380 }
|
c@41
|
381
|
c@43
|
382 float threshold = 1e-10;
|
c@43
|
383
|
c@41
|
384 for (size_t c = 0; c < m_channels; ++c) {
|
c@41
|
385
|
c@43
|
386 bool empty = true;
|
c@43
|
387
|
c@41
|
388 for (int i = 0; i < m_blockSize; ++i) {
|
c@43
|
389 float val = inputBuffers[c][i];
|
c@43
|
390 if (fabs(val) > threshold) empty = false;
|
c@43
|
391 dblbuf[i] = val;
|
c@41
|
392 }
|
c@41
|
393
|
c@43
|
394 if (empty) continue;
|
c@44
|
395 m_lastNonEmptyFrame[c] = m_frameNo;
|
c@43
|
396
|
c@41
|
397 if (m_decimator) {
|
c@41
|
398 m_decimator->process(dblbuf, decbuf);
|
c@41
|
399 }
|
c@42
|
400
|
c@42
|
401 if (m_type == TypeMFCC) {
|
c@42
|
402 m_mfcc->process(m_fftSize, decbuf, raw);
|
c@42
|
403 } else if (m_type == TypeChroma) {
|
c@42
|
404 raw = m_chromagram->process(decbuf);
|
c@42
|
405 }
|
c@41
|
406
|
c@42
|
407 FeatureColumn mf(m_featureColumnSize);
|
c@44
|
408 // std::cout << m_frameNo << ":" << c << ": ";
|
c@44
|
409 for (int i = 0; i < m_featureColumnSize; ++i) {
|
c@44
|
410 mf[i] = raw[i];
|
c@44
|
411 // std::cout << raw[i] << " ";
|
c@44
|
412 }
|
c@44
|
413 // std::cout << std::endl;
|
c@41
|
414
|
c@42
|
415 m_values[c].push_back(mf);
|
c@41
|
416 }
|
c@41
|
417
|
c@41
|
418 if (m_decimator) delete[] decbuf;
|
c@41
|
419 delete[] dblbuf;
|
c@42
|
420
|
c@42
|
421 if (ownRaw) delete[] raw;
|
c@41
|
422
|
c@44
|
423 ++m_frameNo;
|
c@44
|
424
|
c@41
|
425 return FeatureSet();
|
c@41
|
426 }
|
c@41
|
427
|
c@41
|
428 SimilarityPlugin::FeatureSet
|
c@41
|
429 SimilarityPlugin::getRemainingFeatures()
|
c@41
|
430 {
|
c@42
|
431 std::vector<FeatureColumn> m(m_channels);
|
c@42
|
432 std::vector<FeatureColumn> v(m_channels);
|
c@41
|
433
|
c@41
|
434 for (int i = 0; i < m_channels; ++i) {
|
c@41
|
435
|
c@42
|
436 FeatureColumn mean(m_featureColumnSize), variance(m_featureColumnSize);
|
c@41
|
437
|
c@42
|
438 for (int j = 0; j < m_featureColumnSize; ++j) {
|
c@41
|
439
|
c@43
|
440 mean[j] = 0.0;
|
c@43
|
441 variance[j] = 0.0;
|
c@41
|
442 int count;
|
c@41
|
443
|
c@44
|
444 // We want to take values up to, but not including, the
|
c@44
|
445 // last non-empty frame (which may be partial)
|
c@43
|
446
|
c@44
|
447 int sz = m_lastNonEmptyFrame[i];
|
c@44
|
448 if (sz < 0) sz = 0;
|
c@43
|
449
|
c@43
|
450 // std::cout << "\nBin " << j << ":" << std::endl;
|
c@42
|
451
|
c@41
|
452 count = 0;
|
c@43
|
453 for (int k = 0; k < sz; ++k) {
|
c@42
|
454 double val = m_values[i][k][j];
|
c@42
|
455 // std::cout << val << " ";
|
c@41
|
456 if (isnan(val) || isinf(val)) continue;
|
c@41
|
457 mean[j] += val;
|
c@41
|
458 ++count;
|
c@41
|
459 }
|
c@41
|
460 if (count > 0) mean[j] /= count;
|
c@43
|
461 // std::cout << "\n" << count << " non-NaN non-inf values, so mean = " << mean[j] << std::endl;
|
c@41
|
462
|
c@41
|
463 count = 0;
|
c@43
|
464 for (int k = 0; k < sz; ++k) {
|
c@42
|
465 double val = ((m_values[i][k][j] - mean[j]) *
|
c@42
|
466 (m_values[i][k][j] - mean[j]));
|
c@41
|
467 if (isnan(val) || isinf(val)) continue;
|
c@41
|
468 variance[j] += val;
|
c@41
|
469 ++count;
|
c@41
|
470 }
|
c@41
|
471 if (count > 0) variance[j] /= count;
|
c@43
|
472 // std::cout << "... and variance = " << variance[j] << std::endl;
|
c@41
|
473 }
|
c@41
|
474
|
c@41
|
475 m[i] = mean;
|
c@41
|
476 v[i] = variance;
|
c@41
|
477 }
|
c@41
|
478
|
c@42
|
479 // we want to return a matrix of the distances between channels,
|
c@41
|
480 // but Vamp doesn't have a matrix return type so we actually
|
c@41
|
481 // return a series of vectors
|
c@41
|
482
|
c@41
|
483 std::vector<std::vector<double> > distances;
|
c@41
|
484
|
c@42
|
485 // "Despite the fact that MFCCs extracted from music are clearly
|
c@42
|
486 // not Gaussian, [14] showed, somewhat surprisingly, that a
|
c@42
|
487 // similarity function comparing single Gaussians modelling MFCCs
|
c@42
|
488 // for each track can perform as well as mixture models. A great
|
c@42
|
489 // advantage of using single Gaussians is that a simple closed
|
c@42
|
490 // form exists for the KL divergence." -- Mark Levy, "Lightweight
|
c@42
|
491 // measures for timbral similarity of musical audio"
|
c@42
|
492 // (http://www.elec.qmul.ac.uk/easaier/papers/mlevytimbralsimilarity.pdf)
|
c@42
|
493 //
|
c@42
|
494 // This code calculates a symmetrised distance metric based on the
|
c@42
|
495 // KL divergence of Gaussian models of the MFCC values.
|
c@42
|
496
|
c@41
|
497 for (int i = 0; i < m_channels; ++i) {
|
c@41
|
498 distances.push_back(std::vector<double>());
|
c@41
|
499 for (int j = 0; j < m_channels; ++j) {
|
c@42
|
500 double d = -2.0 * m_featureColumnSize;
|
c@42
|
501 for (int k = 0; k < m_featureColumnSize; ++k) {
|
c@42
|
502 // m[i][k] is the mean of feature bin k for channel i
|
c@42
|
503 // v[i][k] is the variance of feature bin k for channel i
|
c@41
|
504 d += v[i][k] / v[j][k] + v[j][k] / v[i][k];
|
c@41
|
505 d += (m[i][k] - m[j][k])
|
c@41
|
506 * (1.0 / v[i][k] + 1.0 / v[j][k])
|
c@41
|
507 * (m[i][k] - m[j][k]);
|
c@41
|
508 }
|
c@41
|
509 d /= 2.0;
|
c@41
|
510 distances[i].push_back(d);
|
c@41
|
511 }
|
c@41
|
512 }
|
c@41
|
513
|
c@44
|
514 // We give all features a timestamp, otherwise hosts will tend to
|
c@44
|
515 // stamp them at the end of the file, which is annoying
|
c@44
|
516
|
c@41
|
517 FeatureSet returnFeatures;
|
c@41
|
518
|
c@44
|
519 Feature feature;
|
c@44
|
520 feature.hasTimestamp = true;
|
c@44
|
521
|
c@43
|
522 Feature distanceVectorFeature;
|
c@43
|
523 distanceVectorFeature.label = "Distance from first channel";
|
c@44
|
524 distanceVectorFeature.hasTimestamp = true;
|
c@44
|
525 distanceVectorFeature.timestamp = Vamp::RealTime::zeroTime;
|
c@44
|
526
|
c@44
|
527 std::map<double, int> sorted;
|
c@44
|
528
|
c@44
|
529 char labelBuffer[100];
|
c@43
|
530
|
c@41
|
531 for (int i = 0; i < m_channels; ++i) {
|
c@41
|
532
|
c@41
|
533 feature.timestamp = Vamp::RealTime(i, 0);
|
c@41
|
534
|
c@44
|
535 sprintf(labelBuffer, "Means for channel %d", i+1);
|
c@44
|
536 feature.label = labelBuffer;
|
c@44
|
537
|
c@41
|
538 feature.values.clear();
|
c@42
|
539 for (int k = 0; k < m_featureColumnSize; ++k) {
|
c@41
|
540 feature.values.push_back(m[i][k]);
|
c@41
|
541 }
|
c@41
|
542
|
c@43
|
543 returnFeatures[m_meansOutput].push_back(feature);
|
c@41
|
544
|
c@44
|
545 sprintf(labelBuffer, "Variances for channel %d", i+1);
|
c@44
|
546 feature.label = labelBuffer;
|
c@44
|
547
|
c@41
|
548 feature.values.clear();
|
c@42
|
549 for (int k = 0; k < m_featureColumnSize; ++k) {
|
c@41
|
550 feature.values.push_back(v[i][k]);
|
c@41
|
551 }
|
c@41
|
552
|
c@43
|
553 returnFeatures[m_variancesOutput].push_back(feature);
|
c@41
|
554
|
c@41
|
555 feature.values.clear();
|
c@41
|
556 for (int j = 0; j < m_channels; ++j) {
|
c@41
|
557 feature.values.push_back(distances[i][j]);
|
c@41
|
558 }
|
c@43
|
559
|
c@44
|
560 sprintf(labelBuffer, "Distances from channel %d", i+1);
|
c@44
|
561 feature.label = labelBuffer;
|
c@41
|
562
|
c@43
|
563 returnFeatures[m_distanceMatrixOutput].push_back(feature);
|
c@43
|
564
|
c@43
|
565 distanceVectorFeature.values.push_back(distances[0][i]);
|
c@44
|
566
|
c@44
|
567 sorted[distances[0][i]] = i;
|
c@41
|
568 }
|
c@41
|
569
|
c@43
|
570 returnFeatures[m_distanceVectorOutput].push_back(distanceVectorFeature);
|
c@43
|
571
|
c@44
|
572 feature.label = "Order of channels by similarity to first channel";
|
c@44
|
573 feature.values.clear();
|
c@44
|
574 feature.timestamp = Vamp::RealTime(0, 0);
|
c@44
|
575
|
c@44
|
576 for (std::map<double, int>::iterator i = sorted.begin();
|
c@44
|
577 i != sorted.end(); ++i) {
|
c@44
|
578 feature.values.push_back(i->second);
|
c@44
|
579 }
|
c@44
|
580
|
c@44
|
581 returnFeatures[m_sortedVectorOutput].push_back(feature);
|
c@44
|
582
|
c@44
|
583 feature.label = "Ordered distances of channels from first channel";
|
c@44
|
584 feature.values.clear();
|
c@44
|
585 feature.timestamp = Vamp::RealTime(1, 0);
|
c@44
|
586
|
c@44
|
587 for (std::map<double, int>::iterator i = sorted.begin();
|
c@44
|
588 i != sorted.end(); ++i) {
|
c@44
|
589 feature.values.push_back(i->first);
|
c@44
|
590 }
|
c@44
|
591
|
c@44
|
592 returnFeatures[m_sortedVectorOutput].push_back(feature);
|
c@44
|
593
|
c@41
|
594 return returnFeatures;
|
c@41
|
595 }
|