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