max@1
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
max@1
|
2
|
max@1
|
3 /*
|
max@1
|
4 QM Vamp Plugin Set
|
max@1
|
5
|
max@1
|
6 Centre for Digital Music, Queen Mary, University of London.
|
max@1
|
7
|
max@1
|
8 This program is free software; you can redistribute it and/or
|
max@1
|
9 modify it under the terms of the GNU General Public License as
|
max@1
|
10 published by the Free Software Foundation; either version 2 of the
|
max@1
|
11 License, or (at your option) any later version. See the file
|
max@1
|
12 COPYING included with this distribution for more information.
|
max@1
|
13 */
|
max@1
|
14
|
max@1
|
15 #include "SongParts.h"
|
max@1
|
16
|
max@1
|
17 #include <base/Window.h>
|
max@1
|
18 #include <dsp/onsets/DetectionFunction.h>
|
max@1
|
19 #include <dsp/onsets/PeakPicking.h>
|
max@1
|
20 #include <dsp/transforms/FFT.h>
|
max@1
|
21 #include <dsp/tempotracking/TempoTrackV2.h>
|
max@1
|
22 #include <dsp/tempotracking/DownBeat.h>
|
max@1
|
23 #include <chromamethods.h>
|
max@1
|
24 #include <maths/MathUtilities.h>
|
max@1
|
25 #include <boost/numeric/ublas/matrix.hpp>
|
max@1
|
26 #include <boost/numeric/ublas/io.hpp>
|
max@1
|
27 #include <boost/math/distributions/normal.hpp>
|
max@1
|
28 #include "armadillo"
|
max@1
|
29 #include <fstream>
|
max@1
|
30 #include <sstream>
|
max@1
|
31 #include <cmath>
|
max@1
|
32 #include <vector>
|
max@1
|
33
|
max@1
|
34 #include <vamp-sdk/Plugin.h>
|
max@1
|
35
|
max@1
|
36 using namespace boost::numeric;
|
max@1
|
37 using namespace arma;
|
max@1
|
38 using std::string;
|
max@1
|
39 using std::vector;
|
max@1
|
40 using std::cerr;
|
max@1
|
41 using std::cout;
|
max@1
|
42 using std::endl;
|
max@1
|
43
|
max@1
|
44
|
max@1
|
45 #ifndef __GNUC__
|
max@1
|
46 #include <alloca.h>
|
max@1
|
47 #endif
|
max@1
|
48
|
max@1
|
49
|
max@1
|
50 // Result Struct
|
max@1
|
51 typedef struct Part {
|
max@1
|
52 int n;
|
Chris@21
|
53 vector<int> indices;
|
max@1
|
54 string letter;
|
Chris@21
|
55 int value;
|
max@1
|
56 int level;
|
max@1
|
57 int nInd;
|
max@1
|
58 }Part;
|
max@1
|
59
|
max@1
|
60
|
max@8
|
61
|
max@1
|
62 /* ------------------------------------ */
|
max@1
|
63 /* ----- BEAT DETECTOR CLASS ---------- */
|
max@1
|
64 /* ------------------------------------ */
|
max@1
|
65
|
max@1
|
66 class BeatTrackerData
|
max@1
|
67 {
|
max@1
|
68 /* --- ATTRIBUTES --- */
|
max@1
|
69 public:
|
max@1
|
70 DFConfig dfConfig;
|
max@1
|
71 DetectionFunction *df;
|
max@1
|
72 DownBeat *downBeat;
|
max@1
|
73 vector<double> dfOutput;
|
max@1
|
74 Vamp::RealTime origin;
|
max@1
|
75
|
max@1
|
76
|
max@1
|
77 /* --- METHODS --- */
|
max@1
|
78
|
max@1
|
79 /* --- Constructor --- */
|
max@1
|
80 public:
|
max@1
|
81 BeatTrackerData(float rate, const DFConfig &config) : dfConfig(config) {
|
Chris@22
|
82
|
max@1
|
83 df = new DetectionFunction(config);
|
max@1
|
84 // decimation factor aims at resampling to c. 3KHz; must be power of 2
|
max@1
|
85 int factor = MathUtilities::nextPowerOfTwo(rate / 3000);
|
max@1
|
86 // std::cerr << "BeatTrackerData: factor = " << factor << std::endl;
|
max@1
|
87 downBeat = new DownBeat(rate, factor, config.stepSize);
|
max@1
|
88 }
|
max@1
|
89
|
max@1
|
90 /* --- Desctructor --- */
|
max@1
|
91 ~BeatTrackerData() {
|
Chris@22
|
92 delete df;
|
max@1
|
93 delete downBeat;
|
max@1
|
94 }
|
max@1
|
95
|
max@1
|
96 void reset() {
|
max@1
|
97 delete df;
|
max@1
|
98 df = new DetectionFunction(dfConfig);
|
max@1
|
99 dfOutput.clear();
|
max@1
|
100 downBeat->resetAudioBuffer();
|
max@1
|
101 origin = Vamp::RealTime::zeroTime;
|
max@1
|
102 }
|
max@1
|
103 };
|
max@1
|
104
|
max@1
|
105
|
max@1
|
106 /* --------------------------------------- */
|
max@1
|
107 /* ----- CHROMA EXTRACTOR CLASS ---------- */
|
max@1
|
108 /* --------------------------------------- */
|
max@1
|
109
|
max@1
|
110 class ChromaData
|
max@1
|
111 {
|
max@1
|
112
|
max@1
|
113 /* --- ATTRIBUTES --- */
|
max@1
|
114
|
max@1
|
115 public:
|
max@1
|
116 int frameCount;
|
max@1
|
117 int nBPS;
|
max@1
|
118 Vamp::Plugin::FeatureList logSpectrum;
|
max@1
|
119 size_t blockSize;
|
max@1
|
120 int lengthOfNoteIndex;
|
max@1
|
121 vector<float> meanTunings;
|
max@1
|
122 vector<float> localTunings;
|
max@1
|
123 float whitening;
|
max@1
|
124 float preset;
|
max@1
|
125 float useNNLS;
|
max@1
|
126 vector<float> localTuning;
|
max@1
|
127 vector<float> kernelValue;
|
max@1
|
128 vector<int> kernelFftIndex;
|
max@1
|
129 vector<int> kernelNoteIndex;
|
max@1
|
130 float *dict;
|
max@1
|
131 bool tuneLocal;
|
max@1
|
132 float doNormalizeChroma;
|
max@1
|
133 float rollon;
|
max@1
|
134 float s;
|
max@1
|
135 vector<float> hw;
|
max@1
|
136 vector<float> sinvalues;
|
max@1
|
137 vector<float> cosvalues;
|
max@1
|
138 Window<float> window;
|
max@1
|
139 FFTReal fft;
|
max@1
|
140 size_t inputSampleRate;
|
max@1
|
141
|
max@1
|
142 /* --- METHODS --- */
|
max@1
|
143
|
max@1
|
144 /* --- Constructor --- */
|
max@1
|
145
|
max@1
|
146 public:
|
max@1
|
147 ChromaData(float inputSampleRate, size_t block_size) :
|
max@1
|
148 frameCount(0),
|
max@1
|
149 nBPS(3),
|
max@1
|
150 logSpectrum(0),
|
max@1
|
151 blockSize(0),
|
max@1
|
152 lengthOfNoteIndex(0),
|
max@1
|
153 meanTunings(0),
|
max@1
|
154 localTunings(0),
|
max@1
|
155 whitening(1.0),
|
max@1
|
156 preset(0.0),
|
max@1
|
157 useNNLS(1.0),
|
max@1
|
158 localTuning(0.0),
|
max@1
|
159 kernelValue(0),
|
max@1
|
160 kernelFftIndex(0),
|
max@1
|
161 kernelNoteIndex(0),
|
max@1
|
162 dict(0),
|
max@1
|
163 tuneLocal(0.0),
|
max@1
|
164 doNormalizeChroma(0),
|
max@1
|
165 rollon(0.0),
|
Chris@22
|
166 s(0.7),
|
Chris@22
|
167 sinvalues(0),
|
Chris@22
|
168 cosvalues(0),
|
Chris@22
|
169 window(HanningWindow, block_size),
|
Chris@22
|
170 fft(block_size),
|
Chris@22
|
171 inputSampleRate(inputSampleRate)
|
max@1
|
172 {
|
max@1
|
173 // make the *note* dictionary matrix
|
max@1
|
174 dict = new float[nNote * 84];
|
max@1
|
175 for (int i = 0; i < nNote * 84; ++i) dict[i] = 0.0;
|
max@1
|
176 blockSize = block_size;
|
max@1
|
177 }
|
max@1
|
178
|
max@1
|
179
|
max@1
|
180 /* --- Desctructor --- */
|
max@1
|
181
|
max@1
|
182 ~ChromaData() {
|
max@1
|
183 delete [] dict;
|
max@1
|
184 }
|
max@1
|
185
|
max@1
|
186 /* --- Public Methods --- */
|
max@1
|
187
|
max@1
|
188 void reset() {
|
max@1
|
189 frameCount = 0;
|
max@1
|
190 logSpectrum.clear();
|
max@1
|
191 for (int iBPS = 0; iBPS < 3; ++iBPS) {
|
max@1
|
192 meanTunings[iBPS] = 0;
|
max@1
|
193 localTunings[iBPS] = 0;
|
max@1
|
194 }
|
max@1
|
195 localTuning.clear();
|
max@1
|
196 }
|
max@1
|
197
|
max@1
|
198 void baseProcess(float *inputBuffers, Vamp::RealTime timestamp)
|
max@1
|
199 {
|
Chris@22
|
200
|
max@1
|
201 frameCount++;
|
max@1
|
202 float *magnitude = new float[blockSize/2];
|
max@1
|
203 double *fftReal = new double[blockSize];
|
max@1
|
204 double *fftImag = new double[blockSize];
|
max@1
|
205
|
max@1
|
206 // FFTReal wants doubles, so we need to make a local copy of inputBuffers
|
max@1
|
207 double *inputBuffersDouble = new double[blockSize];
|
max@1
|
208 for (size_t i = 0; i < blockSize; i++) inputBuffersDouble[i] = inputBuffers[i];
|
max@1
|
209
|
max@1
|
210 fft.process(false, inputBuffersDouble, fftReal, fftImag);
|
max@1
|
211
|
max@1
|
212 float energysum = 0;
|
max@1
|
213 // make magnitude
|
max@1
|
214 float maxmag = -10000;
|
max@1
|
215 for (int iBin = 0; iBin < static_cast<int>(blockSize/2); iBin++) {
|
max@1
|
216 magnitude[iBin] = sqrt(fftReal[iBin] * fftReal[iBin] +
|
max@1
|
217 fftImag[iBin] * fftImag[iBin]);
|
max@1
|
218 if (magnitude[iBin]>blockSize*1.0) magnitude[iBin] = blockSize;
|
max@1
|
219 // a valid audio signal (between -1 and 1) should not be limited here.
|
max@1
|
220 if (maxmag < magnitude[iBin]) maxmag = magnitude[iBin];
|
max@1
|
221 if (rollon > 0) {
|
max@1
|
222 energysum += pow(magnitude[iBin],2);
|
max@1
|
223 }
|
max@1
|
224 }
|
max@1
|
225
|
max@1
|
226 float cumenergy = 0;
|
max@1
|
227 if (rollon > 0) {
|
max@1
|
228 for (int iBin = 2; iBin < static_cast<int>(blockSize/2); iBin++) {
|
max@1
|
229 cumenergy += pow(magnitude[iBin],2);
|
max@1
|
230 if (cumenergy < energysum * rollon / 100) magnitude[iBin-2] = 0;
|
max@1
|
231 else break;
|
max@1
|
232 }
|
max@1
|
233 }
|
max@1
|
234
|
max@1
|
235 if (maxmag < 2) {
|
max@1
|
236 // cerr << "timestamp " << timestamp << ": very low magnitude, setting magnitude to all zeros" << endl;
|
max@1
|
237 for (int iBin = 0; iBin < static_cast<int>(blockSize/2); iBin++) {
|
max@1
|
238 magnitude[iBin] = 0;
|
max@1
|
239 }
|
max@1
|
240 }
|
max@1
|
241
|
max@1
|
242 // cerr << magnitude[200] << endl;
|
max@1
|
243
|
max@1
|
244 // note magnitude mapping using pre-calculated matrix
|
max@1
|
245 float *nm = new float[nNote]; // note magnitude
|
max@1
|
246 for (int iNote = 0; iNote < nNote; iNote++) {
|
max@1
|
247 nm[iNote] = 0; // initialise as 0
|
max@1
|
248 }
|
max@1
|
249 int binCount = 0;
|
max@1
|
250 for (vector<float>::iterator it = kernelValue.begin(); it != kernelValue.end(); ++it) {
|
max@1
|
251 nm[kernelNoteIndex[binCount]] += magnitude[kernelFftIndex[binCount]] * kernelValue[binCount];
|
max@1
|
252 binCount++;
|
max@1
|
253 }
|
max@1
|
254
|
max@1
|
255 float one_over_N = 1.0/frameCount;
|
max@1
|
256 // update means of complex tuning variables
|
max@1
|
257 for (int iBPS = 0; iBPS < nBPS; ++iBPS) meanTunings[iBPS] *= float(frameCount-1)*one_over_N;
|
max@1
|
258
|
max@1
|
259 for (int iTone = 0; iTone < round(nNote*0.62/nBPS)*nBPS+1; iTone = iTone + nBPS) {
|
max@1
|
260 for (int iBPS = 0; iBPS < nBPS; ++iBPS) meanTunings[iBPS] += nm[iTone + iBPS]*one_over_N;
|
max@1
|
261 float ratioOld = 0.997;
|
max@1
|
262 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
max@1
|
263 localTunings[iBPS] *= ratioOld;
|
max@1
|
264 localTunings[iBPS] += nm[iTone + iBPS] * (1 - ratioOld);
|
max@1
|
265 }
|
max@1
|
266 }
|
max@1
|
267
|
max@1
|
268 float localTuningImag = 0;
|
max@1
|
269 float localTuningReal = 0;
|
max@1
|
270 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
max@1
|
271 localTuningReal += localTunings[iBPS] * cosvalues[iBPS];
|
max@1
|
272 localTuningImag += localTunings[iBPS] * sinvalues[iBPS];
|
max@1
|
273 }
|
max@1
|
274
|
max@1
|
275 float normalisedtuning = atan2(localTuningImag, localTuningReal)/(2*M_PI);
|
max@1
|
276 localTuning.push_back(normalisedtuning);
|
max@1
|
277
|
max@1
|
278 Vamp::Plugin::Feature f1; // logfreqspec
|
max@1
|
279 f1.hasTimestamp = true;
|
max@1
|
280 f1.timestamp = timestamp;
|
max@1
|
281 for (int iNote = 0; iNote < nNote; iNote++) {
|
max@1
|
282 f1.values.push_back(nm[iNote]);
|
max@1
|
283 }
|
max@1
|
284
|
max@1
|
285 // deletes
|
max@1
|
286 delete[] inputBuffersDouble;
|
max@1
|
287 delete[] magnitude;
|
max@1
|
288 delete[] fftReal;
|
max@1
|
289 delete[] fftImag;
|
max@1
|
290 delete[] nm;
|
max@1
|
291
|
max@1
|
292 logSpectrum.push_back(f1); // remember note magnitude
|
max@1
|
293 }
|
max@1
|
294
|
max@1
|
295 bool initialise()
|
max@1
|
296 {
|
max@1
|
297 dictionaryMatrix(dict, s);
|
Chris@22
|
298
|
Chris@22
|
299 // make things for tuning estimation
|
Chris@22
|
300 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
max@1
|
301 sinvalues.push_back(sin(2*M_PI*(iBPS*1.0/nBPS)));
|
max@1
|
302 cosvalues.push_back(cos(2*M_PI*(iBPS*1.0/nBPS)));
|
max@1
|
303 }
|
max@1
|
304
|
Chris@22
|
305
|
Chris@22
|
306 // make hamming window of length 1/2 octave
|
Chris@22
|
307 int hamwinlength = nBPS * 6 + 1;
|
max@1
|
308 float hamwinsum = 0;
|
max@1
|
309 for (int i = 0; i < hamwinlength; ++i) {
|
max@1
|
310 hw.push_back(0.54 - 0.46 * cos((2*M_PI*i)/(hamwinlength-1)));
|
max@1
|
311 hamwinsum += 0.54 - 0.46 * cos((2*M_PI*i)/(hamwinlength-1));
|
max@1
|
312 }
|
max@1
|
313 for (int i = 0; i < hamwinlength; ++i) hw[i] = hw[i] / hamwinsum;
|
max@1
|
314
|
max@1
|
315
|
max@1
|
316 // initialise the tuning
|
max@1
|
317 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
max@1
|
318 meanTunings.push_back(0);
|
max@1
|
319 localTunings.push_back(0);
|
max@1
|
320 }
|
Chris@22
|
321
|
max@1
|
322 blockSize = blockSize;
|
max@1
|
323 frameCount = 0;
|
max@1
|
324 int tempn = nNote * blockSize/2;
|
max@1
|
325 // cerr << "length of tempkernel : " << tempn << endl;
|
max@1
|
326 float *tempkernel;
|
max@1
|
327
|
max@1
|
328 tempkernel = new float[tempn];
|
max@1
|
329
|
max@1
|
330 logFreqMatrix(inputSampleRate, blockSize, tempkernel);
|
max@1
|
331 kernelValue.clear();
|
max@1
|
332 kernelFftIndex.clear();
|
max@1
|
333 kernelNoteIndex.clear();
|
max@1
|
334 int countNonzero = 0;
|
max@1
|
335 for (int iNote = 0; iNote < nNote; ++iNote) {
|
max@1
|
336 // I don't know if this is wise: manually making a sparse matrix
|
max@1
|
337 for (int iFFT = 0; iFFT < static_cast<int>(blockSize/2); ++iFFT) {
|
max@1
|
338 if (tempkernel[iFFT + blockSize/2 * iNote] > 0) {
|
max@1
|
339 kernelValue.push_back(tempkernel[iFFT + blockSize/2 * iNote]);
|
max@1
|
340 if (tempkernel[iFFT + blockSize/2 * iNote] > 0) {
|
max@1
|
341 countNonzero++;
|
max@1
|
342 }
|
max@1
|
343 kernelFftIndex.push_back(iFFT);
|
Chris@23
|
344 kernelNoteIndex.push_back(iNote);
|
max@1
|
345 }
|
max@1
|
346 }
|
max@1
|
347 }
|
max@1
|
348 delete [] tempkernel;
|
max@1
|
349 }
|
max@1
|
350 };
|
max@1
|
351
|
max@1
|
352
|
max@1
|
353 /* --------------------------------- */
|
max@1
|
354 /* ----- SONG PARTITIONER ---------- */
|
max@1
|
355 /* --------------------------------- */
|
max@1
|
356
|
max@1
|
357
|
max@1
|
358 /* --- ATTRIBUTES --- */
|
max@1
|
359
|
max@1
|
360 float SongPartitioner::m_stepSecs = 0.01161; // 512 samples at 44100
|
max@1
|
361 size_t SongPartitioner::m_chromaFramesizeFactor = 16; // 16 times as long as beat tracker's
|
max@1
|
362 size_t SongPartitioner::m_chromaStepsizeFactor = 4; // 4 times as long as beat tracker's
|
max@1
|
363
|
max@1
|
364
|
max@1
|
365 /* --- METHODS --- */
|
max@1
|
366
|
max@1
|
367 /* --- Constructor --- */
|
max@1
|
368 SongPartitioner::SongPartitioner(float inputSampleRate) :
|
max@1
|
369 Vamp::Plugin(inputSampleRate),
|
max@1
|
370 m_d(0),
|
max@1
|
371 m_bpb(4),
|
max@1
|
372 m_pluginFrameCount(0)
|
max@1
|
373 {
|
max@1
|
374 }
|
max@1
|
375
|
max@1
|
376
|
max@1
|
377 /* --- Desctructor --- */
|
max@1
|
378 SongPartitioner::~SongPartitioner()
|
max@1
|
379 {
|
max@1
|
380 delete m_d;
|
max@1
|
381 }
|
max@1
|
382
|
max@1
|
383
|
max@1
|
384 /* --- Methods --- */
|
max@1
|
385 string SongPartitioner::getIdentifier() const
|
max@1
|
386 {
|
max@1
|
387 return "qm-songpartitioner";
|
max@1
|
388 }
|
max@1
|
389
|
max@1
|
390 string SongPartitioner::getName() const
|
max@1
|
391 {
|
max@1
|
392 return "Song Partitioner";
|
max@1
|
393 }
|
max@1
|
394
|
max@1
|
395 string SongPartitioner::getDescription() const
|
max@1
|
396 {
|
max@1
|
397 return "Estimate contiguous segments pertaining to song parts such as verse and chorus.";
|
max@1
|
398 }
|
max@1
|
399
|
max@1
|
400 string SongPartitioner::getMaker() const
|
max@1
|
401 {
|
max@1
|
402 return "Queen Mary, University of London";
|
max@1
|
403 }
|
max@1
|
404
|
max@1
|
405 int SongPartitioner::getPluginVersion() const
|
max@1
|
406 {
|
max@1
|
407 return 2;
|
max@1
|
408 }
|
max@1
|
409
|
max@1
|
410 string SongPartitioner::getCopyright() const
|
max@1
|
411 {
|
max@1
|
412 return "Plugin by Matthew Davies, Christian Landone, Chris Cannam, Matthias Mauch and Massimiliano Zanoni Copyright (c) 2006-2012 QMUL - All Rights Reserved";
|
max@1
|
413 }
|
max@1
|
414
|
max@1
|
415 SongPartitioner::ParameterList SongPartitioner::getParameterDescriptors() const
|
max@1
|
416 {
|
max@1
|
417 ParameterList list;
|
max@1
|
418
|
max@1
|
419 ParameterDescriptor desc;
|
max@1
|
420
|
max@1
|
421 desc.identifier = "bpb";
|
max@1
|
422 desc.name = "Beats per Bar";
|
max@1
|
423 desc.description = "The number of beats in each bar";
|
max@1
|
424 desc.minValue = 2;
|
max@1
|
425 desc.maxValue = 16;
|
max@1
|
426 desc.defaultValue = 4;
|
max@1
|
427 desc.isQuantized = true;
|
max@1
|
428 desc.quantizeStep = 1;
|
max@1
|
429 list.push_back(desc);
|
max@1
|
430
|
max@1
|
431 return list;
|
max@1
|
432 }
|
max@1
|
433
|
max@1
|
434 float SongPartitioner::getParameter(std::string name) const
|
max@1
|
435 {
|
max@1
|
436 if (name == "bpb") return m_bpb;
|
max@1
|
437 return 0.0;
|
max@1
|
438 }
|
max@1
|
439
|
max@1
|
440 void SongPartitioner::setParameter(std::string name, float value)
|
max@1
|
441 {
|
max@1
|
442 if (name == "bpb") m_bpb = lrintf(value);
|
max@1
|
443 }
|
max@1
|
444
|
max@1
|
445
|
max@1
|
446 // Return the StepSize for Chroma Extractor
|
max@1
|
447 size_t SongPartitioner::getPreferredStepSize() const
|
max@1
|
448 {
|
max@1
|
449 size_t step = size_t(m_inputSampleRate * m_stepSecs + 0.0001);
|
max@1
|
450 if (step < 1) step = 1;
|
max@1
|
451
|
max@1
|
452 return step;
|
max@1
|
453 }
|
max@1
|
454
|
max@1
|
455 // Return the BlockSize for Chroma Extractor
|
max@1
|
456 size_t SongPartitioner::getPreferredBlockSize() const
|
max@1
|
457 {
|
max@1
|
458 size_t theoretical = getPreferredStepSize() * 2;
|
max@1
|
459 theoretical *= m_chromaFramesizeFactor;
|
max@1
|
460
|
max@1
|
461 return theoretical;
|
max@1
|
462 }
|
max@1
|
463
|
max@1
|
464
|
max@1
|
465 // Initialize the plugin and define Beat Tracker and Chroma Extractor Objects
|
max@1
|
466 bool SongPartitioner::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
max@1
|
467 {
|
max@1
|
468 if (m_d) {
|
Chris@22
|
469 delete m_d;
|
Chris@22
|
470 m_d = 0;
|
max@1
|
471 }
|
max@1
|
472
|
max@1
|
473 if (channels < getMinChannelCount() ||
|
Chris@22
|
474 channels > getMaxChannelCount()) {
|
max@1
|
475 std::cerr << "SongPartitioner::initialise: Unsupported channel count: "
|
max@1
|
476 << channels << std::endl;
|
max@1
|
477 return false;
|
max@1
|
478 }
|
max@1
|
479
|
max@1
|
480 if (stepSize != getPreferredStepSize()) {
|
max@1
|
481 std::cerr << "ERROR: SongPartitioner::initialise: Unsupported step size for this sample rate: "
|
max@1
|
482 << stepSize << " (wanted " << (getPreferredStepSize()) << ")" << std::endl;
|
max@1
|
483 return false;
|
max@1
|
484 }
|
max@1
|
485
|
max@1
|
486 if (blockSize != getPreferredBlockSize()) {
|
max@1
|
487 std::cerr << "WARNING: SongPartitioner::initialise: Sub-optimal block size for this sample rate: "
|
max@1
|
488 << blockSize << " (wanted " << getPreferredBlockSize() << ")" << std::endl;
|
max@1
|
489 }
|
max@1
|
490
|
max@1
|
491 // Beat tracker and Chroma extractor has two different configuration parameters
|
max@1
|
492
|
max@1
|
493 // Configuration Parameters for Beat Tracker
|
max@1
|
494 DFConfig dfConfig;
|
max@1
|
495 dfConfig.DFType = DF_COMPLEXSD;
|
max@1
|
496 dfConfig.stepSize = stepSize;
|
max@1
|
497 dfConfig.frameLength = blockSize / m_chromaFramesizeFactor;
|
max@1
|
498 dfConfig.dbRise = 3;
|
max@1
|
499 dfConfig.adaptiveWhitening = false;
|
max@1
|
500 dfConfig.whiteningRelaxCoeff = -1;
|
max@1
|
501 dfConfig.whiteningFloor = -1;
|
max@1
|
502
|
max@1
|
503 // Initialise Beat Tracker
|
max@1
|
504 m_d = new BeatTrackerData(m_inputSampleRate, dfConfig);
|
max@1
|
505 m_d->downBeat->setBeatsPerBar(m_bpb);
|
max@1
|
506
|
max@1
|
507 // Initialise Chroma Extractor
|
max@1
|
508 m_chromadata = new ChromaData(m_inputSampleRate, blockSize);
|
max@1
|
509 m_chromadata->initialise();
|
max@1
|
510
|
max@1
|
511 return true;
|
max@1
|
512 }
|
max@1
|
513
|
max@1
|
514 void SongPartitioner::reset()
|
max@1
|
515 {
|
max@1
|
516 if (m_d) m_d->reset();
|
max@1
|
517 m_pluginFrameCount = 0;
|
max@1
|
518 }
|
max@1
|
519
|
max@1
|
520 SongPartitioner::OutputList SongPartitioner::getOutputDescriptors() const
|
max@1
|
521 {
|
max@1
|
522 OutputList list;
|
max@1
|
523 size_t outputCounter = 0;
|
max@1
|
524
|
max@1
|
525 OutputDescriptor beat;
|
max@1
|
526 beat.identifier = "beats";
|
max@1
|
527 beat.name = "Beats";
|
max@1
|
528 beat.description = "Beat locations labelled with metrical position";
|
max@1
|
529 beat.unit = "";
|
max@1
|
530 beat.hasFixedBinCount = true;
|
max@1
|
531 beat.binCount = 0;
|
max@1
|
532 beat.sampleType = OutputDescriptor::VariableSampleRate;
|
max@1
|
533 beat.sampleRate = 1.0 / m_stepSecs;
|
max@1
|
534 m_beatOutputNumber = outputCounter++;
|
max@1
|
535
|
max@1
|
536 OutputDescriptor bars;
|
max@1
|
537 bars.identifier = "bars";
|
max@1
|
538 bars.name = "Bars";
|
max@1
|
539 bars.description = "Bar locations";
|
max@1
|
540 bars.unit = "";
|
max@1
|
541 bars.hasFixedBinCount = true;
|
max@1
|
542 bars.binCount = 0;
|
max@1
|
543 bars.sampleType = OutputDescriptor::VariableSampleRate;
|
max@1
|
544 bars.sampleRate = 1.0 / m_stepSecs;
|
max@1
|
545 m_barsOutputNumber = outputCounter++;
|
max@1
|
546
|
max@1
|
547 OutputDescriptor beatcounts;
|
max@1
|
548 beatcounts.identifier = "beatcounts";
|
max@1
|
549 beatcounts.name = "Beat Count";
|
max@1
|
550 beatcounts.description = "Beat counter function";
|
max@1
|
551 beatcounts.unit = "";
|
max@1
|
552 beatcounts.hasFixedBinCount = true;
|
max@1
|
553 beatcounts.binCount = 1;
|
max@1
|
554 beatcounts.sampleType = OutputDescriptor::VariableSampleRate;
|
max@1
|
555 beatcounts.sampleRate = 1.0 / m_stepSecs;
|
max@1
|
556 m_beatcountsOutputNumber = outputCounter++;
|
max@1
|
557
|
max@1
|
558 OutputDescriptor beatsd;
|
max@1
|
559 beatsd.identifier = "beatsd";
|
max@1
|
560 beatsd.name = "Beat Spectral Difference";
|
max@1
|
561 beatsd.description = "Beat spectral difference function used for bar-line detection";
|
max@1
|
562 beatsd.unit = "";
|
max@1
|
563 beatsd.hasFixedBinCount = true;
|
max@1
|
564 beatsd.binCount = 1;
|
max@1
|
565 beatsd.sampleType = OutputDescriptor::VariableSampleRate;
|
max@1
|
566 beatsd.sampleRate = 1.0 / m_stepSecs;
|
max@1
|
567 m_beatsdOutputNumber = outputCounter++;
|
max@1
|
568
|
max@1
|
569 OutputDescriptor logscalespec;
|
max@1
|
570 logscalespec.identifier = "logscalespec";
|
max@1
|
571 logscalespec.name = "Log-Frequency Spectrum";
|
max@1
|
572 logscalespec.description = "Spectrum with linear frequency on a log scale.";
|
max@1
|
573 logscalespec.unit = "";
|
max@1
|
574 logscalespec.hasFixedBinCount = true;
|
max@1
|
575 logscalespec.binCount = nNote;
|
max@1
|
576 logscalespec.hasKnownExtents = false;
|
max@1
|
577 logscalespec.isQuantized = false;
|
max@1
|
578 logscalespec.sampleType = OutputDescriptor::FixedSampleRate;
|
max@1
|
579 logscalespec.hasDuration = false;
|
max@1
|
580 logscalespec.sampleRate = m_inputSampleRate/2048;
|
max@1
|
581 m_logscalespecOutputNumber = outputCounter++;
|
max@1
|
582
|
max@1
|
583 OutputDescriptor bothchroma;
|
max@1
|
584 bothchroma.identifier = "bothchroma";
|
max@1
|
585 bothchroma.name = "Chromagram and Bass Chromagram";
|
max@1
|
586 bothchroma.description = "Tuning-adjusted chromagram and bass chromagram (stacked on top of each other) from NNLS approximate transcription.";
|
max@1
|
587 bothchroma.unit = "";
|
max@1
|
588 bothchroma.hasFixedBinCount = true;
|
max@1
|
589 bothchroma.binCount = 24;
|
max@1
|
590 bothchroma.hasKnownExtents = false;
|
max@1
|
591 bothchroma.isQuantized = false;
|
max@1
|
592 bothchroma.sampleType = OutputDescriptor::FixedSampleRate;
|
max@1
|
593 bothchroma.hasDuration = false;
|
max@1
|
594 bothchroma.sampleRate = m_inputSampleRate/2048;
|
max@1
|
595 m_bothchromaOutputNumber = outputCounter++;
|
max@1
|
596
|
max@1
|
597 OutputDescriptor qchromafw;
|
max@1
|
598 qchromafw.identifier = "qchromafw";
|
max@1
|
599 qchromafw.name = "Pseudo-Quantised Chromagram and Bass Chromagram";
|
max@1
|
600 qchromafw.description = "Pseudo-Quantised Chromagram and Bass Chromagram (frames between two beats are identical).";
|
max@1
|
601 qchromafw.unit = "";
|
max@1
|
602 qchromafw.hasFixedBinCount = true;
|
max@1
|
603 qchromafw.binCount = 24;
|
max@1
|
604 qchromafw.hasKnownExtents = false;
|
max@1
|
605 qchromafw.isQuantized = false;
|
max@1
|
606 qchromafw.sampleType = OutputDescriptor::FixedSampleRate;
|
max@1
|
607 qchromafw.hasDuration = false;
|
max@1
|
608 qchromafw.sampleRate = m_inputSampleRate/2048;
|
max@1
|
609 m_qchromafwOutputNumber = outputCounter++;
|
max@1
|
610
|
max@1
|
611 OutputDescriptor qchroma;
|
max@1
|
612 qchroma.identifier = "qchroma";
|
max@1
|
613 qchroma.name = "Quantised Chromagram and Bass Chromagram";
|
max@1
|
614 qchroma.description = "Quantised Chromagram and Bass Chromagram.";
|
max@1
|
615 qchroma.unit = "";
|
max@1
|
616 qchroma.hasFixedBinCount = true;
|
max@1
|
617 qchroma.binCount = 24;
|
max@1
|
618 qchroma.hasKnownExtents = false;
|
max@1
|
619 qchroma.isQuantized = false;
|
max@1
|
620 qchroma.sampleType = OutputDescriptor::FixedSampleRate;
|
max@1
|
621 qchroma.hasDuration = true;
|
Chris@17
|
622 qchroma.sampleRate = m_inputSampleRate/2048;
|
max@1
|
623 m_qchromaOutputNumber = outputCounter++;
|
max@1
|
624
|
max@1
|
625 OutputDescriptor segm;
|
Chris@15
|
626 segm.identifier = "segmentation";
|
max@1
|
627 segm.name = "Segmentation";
|
max@1
|
628 segm.description = "Segmentation";
|
max@1
|
629 segm.unit = "segment-type";
|
max@1
|
630 segm.hasFixedBinCount = true;
|
max@1
|
631 //segm.binCount = 24;
|
max@1
|
632 segm.binCount = 1;
|
max@1
|
633 segm.hasKnownExtents = true;
|
max@1
|
634 segm.minValue = 1;
|
max@1
|
635 segm.maxValue = 5;
|
max@1
|
636 segm.isQuantized = true;
|
max@1
|
637 segm.quantizeStep = 1;
|
max@1
|
638 segm.sampleType = OutputDescriptor::VariableSampleRate;
|
Chris@17
|
639 segm.sampleRate = 1.0 / m_stepSecs;
|
max@1
|
640 segm.hasDuration = true;
|
max@1
|
641 m_segmOutputNumber = outputCounter++;
|
max@1
|
642
|
max@1
|
643
|
max@1
|
644 /*
|
max@1
|
645 OutputList list;
|
max@1
|
646 OutputDescriptor segmentation;
|
max@1
|
647 segmentation.identifier = "segmentation";
|
max@1
|
648 segmentation.name = "Segmentation";
|
max@1
|
649 segmentation.description = "Segmentation";
|
max@1
|
650 segmentation.unit = "segment-type";
|
max@1
|
651 segmentation.hasFixedBinCount = true;
|
max@1
|
652 segmentation.binCount = 1;
|
max@1
|
653 segmentation.hasKnownExtents = true;
|
max@1
|
654 segmentation.minValue = 1;
|
max@1
|
655 segmentation.maxValue = nSegmentTypes;
|
max@1
|
656 segmentation.isQuantized = true;
|
max@1
|
657 segmentation.quantizeStep = 1;
|
max@1
|
658 segmentation.sampleType = OutputDescriptor::VariableSampleRate;
|
max@1
|
659 segmentation.sampleRate = m_inputSampleRate / getPreferredStepSize();
|
max@1
|
660 list.push_back(segmentation);
|
max@1
|
661 return list;
|
max@1
|
662 */
|
max@1
|
663
|
max@1
|
664
|
max@1
|
665 list.push_back(beat);
|
max@1
|
666 list.push_back(bars);
|
max@1
|
667 list.push_back(beatcounts);
|
max@1
|
668 list.push_back(beatsd);
|
max@1
|
669 list.push_back(logscalespec);
|
max@1
|
670 list.push_back(bothchroma);
|
max@1
|
671 list.push_back(qchromafw);
|
max@1
|
672 list.push_back(qchroma);
|
max@1
|
673 list.push_back(segm);
|
max@1
|
674
|
max@1
|
675 return list;
|
max@1
|
676 }
|
max@1
|
677
|
max@1
|
678 // Executed for each frame - called from the host
|
max@1
|
679
|
max@1
|
680 // We use time domain input, because DownBeat requires it -- so we
|
max@1
|
681 // use the time-domain version of DetectionFunction::process which
|
max@1
|
682 // does its own FFT. It requires doubles as input, so we need to
|
max@1
|
683 // make a temporary copy
|
max@1
|
684
|
max@1
|
685 // We only support a single input channel
|
max@1
|
686 SongPartitioner::FeatureSet SongPartitioner::process(const float *const *inputBuffers,Vamp::RealTime timestamp)
|
max@1
|
687 {
|
max@1
|
688 if (!m_d) {
|
Chris@22
|
689 cerr << "ERROR: SongPartitioner::process: "
|
Chris@22
|
690 << "SongPartitioner has not been initialised"
|
Chris@22
|
691 << endl;
|
Chris@22
|
692 return FeatureSet();
|
max@1
|
693 }
|
max@1
|
694
|
max@1
|
695 const int fl = m_d->dfConfig.frameLength;
|
max@1
|
696 #ifndef __GNUC__
|
max@1
|
697 double *dfinput = (double *)alloca(fl * sizeof(double));
|
max@1
|
698 #else
|
max@1
|
699 double dfinput[fl];
|
max@1
|
700 #endif
|
max@1
|
701 int sampleOffset = ((m_chromaFramesizeFactor-1) * fl) / 2;
|
max@1
|
702
|
max@1
|
703 // Since chroma needs a much longer frame size, we only ever use the very
|
max@1
|
704 // beginning of the frame for beat tracking.
|
max@1
|
705 for (int i = 0; i < fl; ++i) dfinput[i] = inputBuffers[0][i];
|
max@1
|
706 double output = m_d->df->process(dfinput);
|
max@1
|
707
|
max@1
|
708 if (m_d->dfOutput.empty()) m_d->origin = timestamp;
|
max@1
|
709
|
max@1
|
710 // std::cerr << "df[" << m_d->dfOutput.size() << "] is " << output << std::endl;
|
max@1
|
711 m_d->dfOutput.push_back(output);
|
max@1
|
712
|
max@1
|
713 // Downsample and store the incoming audio block.
|
max@1
|
714 // We have an overlap on the incoming audio stream (step size is
|
max@1
|
715 // half block size) -- this function is configured to take only a
|
max@1
|
716 // step size's worth, so effectively ignoring the overlap. Note
|
max@1
|
717 // however that this means we omit the last blocksize - stepsize
|
max@1
|
718 // samples completely for the purposes of barline detection
|
max@1
|
719 // (hopefully not a problem)
|
max@1
|
720 m_d->downBeat->pushAudioBlock(inputBuffers[0]);
|
max@1
|
721
|
max@1
|
722 // The following is not done every time, but only every m_chromaFramesizeFactor times,
|
max@1
|
723 // because the chroma does not need dense time frames.
|
max@1
|
724
|
max@1
|
725 if (m_pluginFrameCount % m_chromaStepsizeFactor == 0)
|
max@1
|
726 {
|
max@1
|
727
|
max@1
|
728 // Window the full time domain, data, FFT it and process chroma stuff.
|
max@1
|
729
|
max@1
|
730 #ifndef __GNUC__
|
max@1
|
731 float *windowedBuffers = (float *)alloca(m_chromadata->blockSize * sizeof(float));
|
max@1
|
732 #else
|
max@1
|
733 float windowedBuffers[m_chromadata->blockSize];
|
max@1
|
734 #endif
|
max@1
|
735 m_chromadata->window.cut(&inputBuffers[0][0], &windowedBuffers[0]);
|
max@1
|
736
|
max@1
|
737 // adjust timestamp (we want the middle of the frame)
|
max@1
|
738 timestamp = timestamp + Vamp::RealTime::frame2RealTime(sampleOffset, lrintf(m_inputSampleRate));
|
max@1
|
739
|
max@1
|
740 m_chromadata->baseProcess(&windowedBuffers[0], timestamp);
|
max@1
|
741
|
max@1
|
742 }
|
max@1
|
743 m_pluginFrameCount++;
|
max@1
|
744
|
max@1
|
745 FeatureSet fs;
|
max@1
|
746 fs[m_logscalespecOutputNumber].push_back(
|
max@1
|
747 m_chromadata->logSpectrum.back());
|
max@1
|
748 return fs;
|
max@1
|
749 }
|
max@1
|
750
|
max@1
|
751 SongPartitioner::FeatureSet SongPartitioner::getRemainingFeatures()
|
max@1
|
752 {
|
max@1
|
753 if (!m_d) {
|
Chris@22
|
754 cerr << "ERROR: SongPartitioner::getRemainingFeatures: "
|
Chris@22
|
755 << "SongPartitioner has not been initialised"
|
Chris@22
|
756 << endl;
|
Chris@22
|
757 return FeatureSet();
|
max@1
|
758 }
|
max@1
|
759
|
Chris@16
|
760 FeatureSet masterFeatureset = beatTrack();
|
Chris@16
|
761 FeatureList chromaList = chromaFeatures();
|
max@1
|
762
|
max@1
|
763 for (size_t i = 0; i < chromaList.size(); ++i)
|
max@1
|
764 {
|
max@1
|
765 masterFeatureset[m_bothchromaOutputNumber].push_back(chromaList[i]);
|
max@1
|
766 }
|
max@1
|
767
|
max@1
|
768 // quantised and pseudo-quantised (beat-wise) chroma
|
Chris@16
|
769 std::vector<FeatureList> quantisedChroma = beatQuantiser(chromaList, masterFeatureset[m_beatOutputNumber]);
|
max@1
|
770
|
max@1
|
771 masterFeatureset[m_qchromafwOutputNumber] = quantisedChroma[0];
|
max@1
|
772 masterFeatureset[m_qchromaOutputNumber] = quantisedChroma[1];
|
max@1
|
773
|
max@1
|
774 // Segmentation
|
Chris@14
|
775 masterFeatureset[m_segmOutputNumber] = runSegmenter(quantisedChroma[1]);
|
max@1
|
776
|
max@1
|
777 return(masterFeatureset);
|
max@1
|
778 }
|
max@1
|
779
|
max@1
|
780 /* ------ Beat Tracker ------ */
|
max@1
|
781
|
Chris@16
|
782 SongPartitioner::FeatureSet SongPartitioner::beatTrack()
|
max@1
|
783 {
|
max@1
|
784 vector<double> df;
|
max@1
|
785 vector<double> beatPeriod;
|
max@1
|
786 vector<double> tempi;
|
max@1
|
787
|
max@1
|
788 for (size_t i = 2; i < m_d->dfOutput.size(); ++i) { // discard first two elts
|
max@1
|
789 df.push_back(m_d->dfOutput[i]);
|
max@1
|
790 beatPeriod.push_back(0.0);
|
max@1
|
791 }
|
max@1
|
792 if (df.empty()) return FeatureSet();
|
max@1
|
793
|
max@1
|
794 TempoTrackV2 tt(m_inputSampleRate, m_d->dfConfig.stepSize);
|
max@1
|
795 tt.calculateBeatPeriod(df, beatPeriod, tempi);
|
max@1
|
796
|
max@1
|
797 vector<double> beats;
|
max@1
|
798 tt.calculateBeats(df, beatPeriod, beats);
|
max@1
|
799
|
max@1
|
800 vector<int> downbeats;
|
max@1
|
801 size_t downLength = 0;
|
max@1
|
802 const float *downsampled = m_d->downBeat->getBufferedAudio(downLength);
|
max@1
|
803 m_d->downBeat->findDownBeats(downsampled, downLength, beats, downbeats);
|
max@1
|
804
|
max@1
|
805 vector<double> beatsd;
|
max@1
|
806 m_d->downBeat->getBeatSD(beatsd);
|
max@1
|
807
|
max@1
|
808 /*std::cout << "BeatTracker: found downbeats at: ";
|
max@1
|
809 for (int i = 0; i < downbeats.size(); ++i) std::cout << downbeats[i] << " " << std::endl;*/
|
max@1
|
810
|
max@1
|
811 FeatureSet returnFeatures;
|
max@1
|
812
|
max@1
|
813 char label[20];
|
max@1
|
814
|
max@1
|
815 int dbi = 0;
|
max@1
|
816 int beat = 0;
|
max@1
|
817 int bar = 0;
|
max@1
|
818
|
max@1
|
819 if (!downbeats.empty()) {
|
max@1
|
820 // get the right number for the first beat; this will be
|
max@1
|
821 // incremented before use (at top of the following loop)
|
max@1
|
822 int firstDown = downbeats[0];
|
max@1
|
823 beat = m_bpb - firstDown - 1;
|
max@1
|
824 if (beat == m_bpb) beat = 0;
|
max@1
|
825 }
|
max@1
|
826
|
max@1
|
827 for (size_t i = 0; i < beats.size(); ++i) {
|
max@1
|
828
|
max@1
|
829 size_t frame = beats[i] * m_d->dfConfig.stepSize;
|
max@1
|
830
|
max@1
|
831 if (dbi < downbeats.size() && i == downbeats[dbi]) {
|
max@1
|
832 beat = 0;
|
max@1
|
833 ++bar;
|
max@1
|
834 ++dbi;
|
max@1
|
835 } else {
|
max@1
|
836 ++beat;
|
max@1
|
837 }
|
max@1
|
838
|
max@1
|
839 /* Ooutput Section */
|
max@1
|
840
|
max@1
|
841 // outputs are:
|
max@1
|
842 //
|
max@1
|
843 // 0 -> beats
|
max@1
|
844 // 1 -> bars
|
max@1
|
845 // 2 -> beat counter function
|
max@1
|
846
|
max@1
|
847 Feature feature;
|
max@1
|
848 feature.hasTimestamp = true;
|
max@1
|
849 feature.timestamp = m_d->origin + Vamp::RealTime::frame2RealTime (frame, lrintf(m_inputSampleRate));
|
max@1
|
850
|
max@1
|
851 sprintf(label, "%d", beat + 1);
|
max@1
|
852 feature.label = label;
|
max@1
|
853 returnFeatures[m_beatOutputNumber].push_back(feature); // labelled beats
|
max@1
|
854
|
max@1
|
855 feature.values.push_back(beat + 1);
|
max@1
|
856 returnFeatures[m_beatcountsOutputNumber].push_back(feature); // beat function
|
max@1
|
857
|
max@1
|
858 if (i > 0 && i <= beatsd.size()) {
|
max@1
|
859 feature.values.clear();
|
max@1
|
860 feature.values.push_back(beatsd[i-1]);
|
max@1
|
861 feature.label = "";
|
max@1
|
862 returnFeatures[m_beatsdOutputNumber].push_back(feature); // beat spectral difference
|
max@1
|
863 }
|
max@1
|
864
|
max@1
|
865 if (beat == 0) {
|
max@1
|
866 feature.values.clear();
|
max@1
|
867 sprintf(label, "%d", bar);
|
max@1
|
868 feature.label = label;
|
max@1
|
869 returnFeatures[m_barsOutputNumber].push_back(feature); // bars
|
max@1
|
870 }
|
max@1
|
871 }
|
max@1
|
872
|
max@1
|
873 return returnFeatures;
|
max@1
|
874 }
|
max@1
|
875
|
max@1
|
876
|
max@1
|
877 /* ------ Chroma Extractor ------ */
|
max@1
|
878
|
Chris@16
|
879 SongPartitioner::FeatureList SongPartitioner::chromaFeatures()
|
max@1
|
880 {
|
max@1
|
881
|
max@1
|
882 FeatureList returnFeatureList;
|
max@1
|
883 FeatureList tunedlogfreqspec;
|
max@1
|
884
|
max@1
|
885 if (m_chromadata->logSpectrum.size() == 0) return returnFeatureList;
|
max@1
|
886
|
max@1
|
887 /** Calculate Tuning
|
max@1
|
888 calculate tuning from (using the angle of the complex number defined by the
|
max@1
|
889 cumulative mean real and imag values)
|
max@1
|
890 **/
|
max@1
|
891 float meanTuningImag = 0;
|
max@1
|
892 float meanTuningReal = 0;
|
max@1
|
893 for (int iBPS = 0; iBPS < nBPS; ++iBPS) {
|
max@1
|
894 meanTuningReal += m_chromadata->meanTunings[iBPS] * m_chromadata->cosvalues[iBPS];
|
max@1
|
895 meanTuningImag += m_chromadata->meanTunings[iBPS] * m_chromadata->sinvalues[iBPS];
|
max@1
|
896 }
|
max@1
|
897 float cumulativetuning = 440 * pow(2,atan2(meanTuningImag, meanTuningReal)/(24*M_PI));
|
max@1
|
898 float normalisedtuning = atan2(meanTuningImag, meanTuningReal)/(2*M_PI);
|
max@1
|
899 int intShift = floor(normalisedtuning * 3);
|
max@1
|
900 float floatShift = normalisedtuning * 3 - intShift; // floatShift is a really bad name for this
|
max@1
|
901
|
max@1
|
902 char buffer0 [50];
|
max@1
|
903
|
max@1
|
904 sprintf(buffer0, "estimated tuning: %0.1f Hz", cumulativetuning);
|
max@1
|
905
|
max@1
|
906 /** Tune Log-Frequency Spectrogram
|
max@1
|
907 calculate a tuned log-frequency spectrogram (f2): use the tuning estimated above (kinda f0) to
|
max@1
|
908 perform linear interpolation on the existing log-frequency spectrogram (kinda f1).
|
max@1
|
909 **/
|
max@1
|
910 cerr << endl << "[NNLS Chroma Plugin] Tuning Log-Frequency Spectrogram ... ";
|
max@1
|
911
|
max@1
|
912 float tempValue = 0;
|
max@1
|
913
|
max@1
|
914 int count = 0;
|
max@1
|
915
|
max@1
|
916 for (FeatureList::iterator i = m_chromadata->logSpectrum.begin(); i != m_chromadata->logSpectrum.end(); ++i)
|
max@1
|
917 {
|
max@1
|
918
|
max@1
|
919 Feature f1 = *i;
|
max@1
|
920 Feature f2; // tuned log-frequency spectrum
|
max@1
|
921
|
max@1
|
922 f2.hasTimestamp = true;
|
max@1
|
923 f2.timestamp = f1.timestamp;
|
max@1
|
924
|
max@1
|
925 f2.values.push_back(0.0);
|
max@1
|
926 f2.values.push_back(0.0); // set lower edge to zero
|
max@1
|
927
|
max@1
|
928 if (m_chromadata->tuneLocal) {
|
max@1
|
929 intShift = floor(m_chromadata->localTuning[count] * 3);
|
max@1
|
930 floatShift = m_chromadata->localTuning[count] * 3 - intShift;
|
max@1
|
931 // floatShift is a really bad name for this
|
max@1
|
932 }
|
max@1
|
933
|
max@1
|
934 for (int k = 2; k < (int)f1.values.size() - 3; ++k)
|
max@1
|
935 { // interpolate all inner bins
|
max@1
|
936 tempValue = f1.values[k + intShift] * (1-floatShift) + f1.values[k+intShift+1] * floatShift;
|
max@1
|
937 f2.values.push_back(tempValue);
|
max@1
|
938 }
|
max@1
|
939
|
max@1
|
940 f2.values.push_back(0.0);
|
max@1
|
941 f2.values.push_back(0.0);
|
max@1
|
942 f2.values.push_back(0.0); // upper edge
|
max@1
|
943
|
max@1
|
944 vector<float> runningmean = SpecialConvolution(f2.values,m_chromadata->hw);
|
max@1
|
945 vector<float> runningstd;
|
max@1
|
946 for (int i = 0; i < nNote; i++) { // first step: squared values into vector (variance)
|
max@1
|
947 runningstd.push_back((f2.values[i] - runningmean[i]) * (f2.values[i] - runningmean[i]));
|
max@1
|
948 }
|
max@1
|
949 runningstd = SpecialConvolution(runningstd,m_chromadata->hw); // second step convolve
|
max@1
|
950 for (int i = 0; i < nNote; i++)
|
max@1
|
951 {
|
max@1
|
952
|
max@1
|
953 runningstd[i] = sqrt(runningstd[i]);
|
max@1
|
954 // square root to finally have running std
|
max@1
|
955
|
max@1
|
956 if (runningstd[i] > 0)
|
max@1
|
957 {
|
max@1
|
958 f2.values[i] = (f2.values[i] - runningmean[i]) > 0 ?
|
max@1
|
959 (f2.values[i] - runningmean[i]) / pow(runningstd[i],m_chromadata->whitening) : 0;
|
max@1
|
960 }
|
max@1
|
961
|
max@1
|
962 if (f2.values[i] < 0) {
|
max@1
|
963
|
max@1
|
964 cerr << "ERROR: negative value in logfreq spectrum" << endl;
|
max@1
|
965
|
max@1
|
966 }
|
max@1
|
967 }
|
max@1
|
968 tunedlogfreqspec.push_back(f2);
|
max@1
|
969 count++;
|
max@1
|
970 }
|
max@1
|
971 cerr << "done." << endl;
|
max@1
|
972 /** Semitone spectrum and chromagrams
|
max@1
|
973 Semitone-spaced log-frequency spectrum derived
|
max@1
|
974 from the tuned log-freq spectrum above. the spectrum
|
max@1
|
975 is inferred using a non-negative least squares algorithm.
|
max@1
|
976 Three different kinds of chromagram are calculated, "treble", "bass", and "both" (which means
|
max@1
|
977 bass and treble stacked onto each other).
|
max@1
|
978 **/
|
max@1
|
979 if (m_chromadata->useNNLS == 0) {
|
max@1
|
980 cerr << "[NNLS Chroma Plugin] Mapping to semitone spectrum and chroma ... ";
|
max@1
|
981 } else {
|
max@1
|
982 cerr << "[NNLS Chroma Plugin] Performing NNLS and mapping to chroma ... ";
|
max@1
|
983 }
|
max@1
|
984
|
max@1
|
985 vector<float> oldchroma = vector<float>(12,0);
|
max@1
|
986 vector<float> oldbasschroma = vector<float>(12,0);
|
max@1
|
987 count = 0;
|
max@1
|
988
|
max@1
|
989 for (FeatureList::iterator it = tunedlogfreqspec.begin(); it != tunedlogfreqspec.end(); ++it) {
|
max@1
|
990 Feature logfreqsp = *it; // logfreq spectrum
|
max@1
|
991 Feature bothchroma; // treble and bass chromagram
|
max@1
|
992
|
max@1
|
993 bothchroma.hasTimestamp = true;
|
max@1
|
994 bothchroma.timestamp = logfreqsp.timestamp;
|
max@1
|
995
|
max@1
|
996 float b[nNote];
|
max@1
|
997
|
max@1
|
998 bool some_b_greater_zero = false;
|
max@1
|
999 float sumb = 0;
|
max@1
|
1000 for (int i = 0; i < nNote; i++) {
|
max@1
|
1001 b[i] = logfreqsp.values[i];
|
max@1
|
1002 sumb += b[i];
|
max@1
|
1003 if (b[i] > 0) {
|
max@1
|
1004 some_b_greater_zero = true;
|
max@1
|
1005 }
|
max@1
|
1006 }
|
max@1
|
1007
|
max@1
|
1008 // here's where the non-negative least squares algorithm calculates the note activation x
|
max@1
|
1009
|
max@1
|
1010 vector<float> chroma = vector<float>(12, 0);
|
max@1
|
1011 vector<float> basschroma = vector<float>(12, 0);
|
max@1
|
1012 float currval;
|
max@1
|
1013 int iSemitone = 0;
|
max@1
|
1014
|
max@1
|
1015 if (some_b_greater_zero) {
|
max@1
|
1016 if (m_chromadata->useNNLS == 0) {
|
max@1
|
1017 for (int iNote = nBPS/2 + 2; iNote < nNote - nBPS/2; iNote += nBPS) {
|
max@1
|
1018 currval = 0;
|
max@1
|
1019 for (int iBPS = -nBPS/2; iBPS < nBPS/2+1; ++iBPS) {
|
max@1
|
1020 currval += b[iNote + iBPS] * (1-abs(iBPS*1.0/(nBPS/2+1)));
|
max@1
|
1021 }
|
max@1
|
1022 chroma[iSemitone % 12] += currval * treblewindow[iSemitone];
|
max@1
|
1023 basschroma[iSemitone % 12] += currval * basswindow[iSemitone];
|
max@1
|
1024 iSemitone++;
|
max@1
|
1025 }
|
max@1
|
1026
|
max@1
|
1027 } else {
|
max@1
|
1028 float x[84+1000];
|
max@1
|
1029 for (int i = 1; i < 1084; ++i) x[i] = 1.0;
|
max@1
|
1030 vector<int> signifIndex;
|
max@1
|
1031 int index=0;
|
max@1
|
1032 sumb /= 84.0;
|
max@1
|
1033 for (int iNote = nBPS/2 + 2; iNote < nNote - nBPS/2; iNote += nBPS) {
|
max@1
|
1034 float currval = 0;
|
max@1
|
1035 for (int iBPS = -nBPS/2; iBPS < nBPS/2+1; ++iBPS) {
|
max@1
|
1036 currval += b[iNote + iBPS];
|
max@1
|
1037 }
|
max@1
|
1038 if (currval > 0) signifIndex.push_back(index);
|
max@1
|
1039 index++;
|
max@1
|
1040 }
|
max@1
|
1041 float rnorm;
|
max@1
|
1042 float w[84+1000];
|
max@1
|
1043 float zz[84+1000];
|
max@1
|
1044 int indx[84+1000];
|
max@1
|
1045 int mode;
|
max@1
|
1046 int dictsize = nNote*signifIndex.size();
|
max@1
|
1047
|
max@1
|
1048 float *curr_dict = new float[dictsize];
|
max@1
|
1049 for (int iNote = 0; iNote < (int)signifIndex.size(); ++iNote) {
|
max@1
|
1050 for (int iBin = 0; iBin < nNote; iBin++) {
|
max@1
|
1051 curr_dict[iNote * nNote + iBin] =
|
max@1
|
1052 1.0 * m_chromadata->dict[signifIndex[iNote] * nNote + iBin];
|
max@1
|
1053 }
|
max@1
|
1054 }
|
max@1
|
1055 nnls(curr_dict, nNote, nNote, signifIndex.size(), b, x, &rnorm, w, zz, indx, &mode);
|
max@1
|
1056 delete [] curr_dict;
|
max@1
|
1057 for (int iNote = 0; iNote < (int)signifIndex.size(); ++iNote) {
|
max@1
|
1058 // cerr << mode << endl;
|
max@1
|
1059 chroma[signifIndex[iNote] % 12] += x[iNote] * treblewindow[signifIndex[iNote]];
|
max@1
|
1060 basschroma[signifIndex[iNote] % 12] += x[iNote] * basswindow[signifIndex[iNote]];
|
max@1
|
1061 }
|
max@1
|
1062 }
|
max@1
|
1063 }
|
max@1
|
1064
|
max@1
|
1065 chroma.insert(chroma.begin(), basschroma.begin(), basschroma.end());
|
max@1
|
1066 // just stack the both chromas
|
max@1
|
1067
|
max@1
|
1068 bothchroma.values = chroma;
|
max@1
|
1069 returnFeatureList.push_back(bothchroma);
|
max@1
|
1070 count++;
|
max@1
|
1071 }
|
max@1
|
1072 cerr << "done." << endl;
|
max@1
|
1073
|
max@1
|
1074 return returnFeatureList;
|
max@1
|
1075 }
|
max@1
|
1076
|
max@1
|
1077 /* ------ Beat Quantizer ------ */
|
max@1
|
1078
|
max@4
|
1079 std::vector<Vamp::Plugin::FeatureList>
|
Chris@16
|
1080 SongPartitioner::beatQuantiser(Vamp::Plugin::FeatureList chromagram, Vamp::Plugin::FeatureList beats)
|
max@1
|
1081 {
|
max@1
|
1082 std::vector<FeatureList> returnVector;
|
max@1
|
1083
|
max@1
|
1084 FeatureList fwQchromagram; // frame-wise beat-quantised chroma
|
max@1
|
1085 FeatureList bwQchromagram; // beat-wise beat-quantised chroma
|
max@1
|
1086
|
max@4
|
1087 int nChromaFrame = (int) chromagram.size();
|
max@4
|
1088 int nBeat = (int) beats.size();
|
max@1
|
1089
|
max@1
|
1090 if (nBeat == 0 && nChromaFrame == 0) return returnVector;
|
max@1
|
1091
|
max@1
|
1092 size_t nBin = chromagram[0].values.size();
|
max@1
|
1093
|
max@1
|
1094 vector<float> tempChroma = vector<float>(nBin);
|
max@1
|
1095
|
max@1
|
1096 Vamp::RealTime beatTimestamp = Vamp::RealTime::zeroTime;
|
max@1
|
1097 int currBeatCount = -1; // start before first beat
|
max@1
|
1098 int framesInBeat = 0;
|
max@1
|
1099
|
max@4
|
1100 for (int iChroma = 0; iChroma < nChromaFrame; ++iChroma)
|
max@1
|
1101 {
|
max@4
|
1102 Vamp::RealTime frameTimestamp = chromagram[iChroma].timestamp;
|
Chris@24
|
1103 Vamp::RealTime newBeatTimestamp;
|
Chris@22
|
1104
|
Chris@24
|
1105 if (currBeatCount != beats.size()-1) newBeatTimestamp = beats[currBeatCount+1].timestamp;
|
Chris@24
|
1106 else newBeatTimestamp = chromagram[nChromaFrame-1].timestamp;
|
Chris@22
|
1107
|
Chris@24
|
1108 if (frameTimestamp > newBeatTimestamp ||
|
max@1
|
1109 iChroma == nChromaFrame-1)
|
max@1
|
1110 {
|
max@1
|
1111 // new beat (or last chroma frame)
|
max@1
|
1112 // 1. finish all the old beat processing
|
Chris@23
|
1113 if (framesInBeat > 0)
|
Chris@23
|
1114 {
|
Chris@23
|
1115 for (int i = 0; i < nBin; ++i) tempChroma[i] /= framesInBeat; // average
|
Chris@23
|
1116 }
|
max@1
|
1117
|
max@1
|
1118 Feature bwQchromaFrame;
|
max@1
|
1119 bwQchromaFrame.hasTimestamp = true;
|
max@1
|
1120 bwQchromaFrame.timestamp = beatTimestamp;
|
max@1
|
1121 bwQchromaFrame.values = tempChroma;
|
Chris@24
|
1122 bwQchromaFrame.duration = newBeatTimestamp - beatTimestamp;
|
max@1
|
1123 bwQchromagram.push_back(bwQchromaFrame);
|
max@1
|
1124
|
max@1
|
1125 for (int iFrame = -framesInBeat; iFrame < 0; ++iFrame)
|
max@1
|
1126 {
|
max@1
|
1127 Feature fwQchromaFrame;
|
max@1
|
1128 fwQchromaFrame.hasTimestamp = true;
|
max@1
|
1129 fwQchromaFrame.timestamp = chromagram[iChroma+iFrame].timestamp;
|
max@1
|
1130 fwQchromaFrame.values = tempChroma; // all between two beats get the same
|
max@1
|
1131 fwQchromagram.push_back(fwQchromaFrame);
|
max@1
|
1132 }
|
max@1
|
1133
|
max@1
|
1134 // 2. increments / resets for current (new) beat
|
max@1
|
1135 currBeatCount++;
|
Chris@24
|
1136 beatTimestamp = newBeatTimestamp;
|
max@1
|
1137 for (size_t i = 0; i < nBin; ++i) tempChroma[i] = 0; // average
|
max@1
|
1138 framesInBeat = 0;
|
max@1
|
1139 }
|
max@1
|
1140 framesInBeat++;
|
max@1
|
1141 for (size_t i = 0; i < nBin; ++i) tempChroma[i] += chromagram[iChroma].values[i];
|
max@1
|
1142 }
|
max@1
|
1143 returnVector.push_back(fwQchromagram);
|
max@1
|
1144 returnVector.push_back(bwQchromagram);
|
Chris@30
|
1145 return returnVector;
|
max@1
|
1146 }
|
max@1
|
1147
|
max@1
|
1148 /* -------------------------------- */
|
max@1
|
1149 /* ------ Support Functions ------ */
|
max@1
|
1150 /* -------------------------------- */
|
max@1
|
1151
|
max@1
|
1152 // one-dimesion median filter
|
max@1
|
1153 arma::vec medfilt1(arma::vec v, int medfilt_length)
|
max@1
|
1154 {
|
max@1
|
1155 int halfWin = medfilt_length/2;
|
max@1
|
1156
|
max@1
|
1157 // result vector
|
max@1
|
1158 arma::vec res = arma::zeros<arma::vec>(v.size());
|
max@1
|
1159
|
max@1
|
1160 // padding
|
max@1
|
1161 arma::vec padV = arma::zeros<arma::vec>(v.size()+medfilt_length-1);
|
max@1
|
1162
|
Chris@21
|
1163 for (int i=medfilt_length/2; i < medfilt_length/2+v.size(); ++ i)
|
max@1
|
1164 {
|
max@1
|
1165 padV(i) = v(i-medfilt_length/2);
|
max@1
|
1166 }
|
max@1
|
1167
|
max@1
|
1168 // Median filter
|
max@1
|
1169 arma::vec win = arma::zeros<arma::vec>(medfilt_length);
|
max@1
|
1170
|
Chris@21
|
1171 for (int i=0; i < v.size(); ++i)
|
max@1
|
1172 {
|
max@1
|
1173 win = padV.subvec(i,i+halfWin*2);
|
max@1
|
1174 win = sort(win);
|
max@1
|
1175 res(i) = win(halfWin);
|
max@1
|
1176 }
|
max@1
|
1177
|
max@1
|
1178 return res;
|
max@1
|
1179 }
|
max@1
|
1180
|
max@1
|
1181
|
max@1
|
1182 // Quantile
|
max@1
|
1183 double quantile(arma::vec v, double p)
|
max@1
|
1184 {
|
max@1
|
1185 arma::vec sortV = arma::sort(v);
|
max@1
|
1186 int n = sortV.size();
|
max@1
|
1187 arma::vec x = arma::zeros<vec>(n+2);
|
max@1
|
1188 arma::vec y = arma::zeros<vec>(n+2);
|
max@1
|
1189
|
max@1
|
1190 x(0) = 0;
|
max@1
|
1191 x(n+1) = 100;
|
max@1
|
1192
|
Chris@21
|
1193 for (int i=1; i<n+1; ++i)
|
max@1
|
1194 x(i) = 100*(0.5+(i-1))/n;
|
max@1
|
1195
|
max@1
|
1196 y(0) = sortV(0);
|
max@1
|
1197 y.subvec(1,n) = sortV;
|
max@1
|
1198 y(n+1) = sortV(n-1);
|
max@1
|
1199
|
max@1
|
1200 arma::uvec x2index = find(x>=p*100);
|
max@1
|
1201
|
max@1
|
1202 // Interpolation
|
max@1
|
1203 double x1 = x(x2index(0)-1);
|
max@1
|
1204 double x2 = x(x2index(0));
|
max@1
|
1205 double y1 = y(x2index(0)-1);
|
max@1
|
1206 double y2 = y(x2index(0));
|
max@1
|
1207
|
max@1
|
1208 double res = (y2-y1)/(x2-x1)*(p*100-x1)+y1;
|
max@1
|
1209
|
max@1
|
1210 return res;
|
max@1
|
1211 }
|
max@1
|
1212
|
max@1
|
1213 // Max Filtering
|
max@1
|
1214 arma::mat maxfilt1(arma::mat inmat, int len)
|
max@1
|
1215 {
|
max@1
|
1216 arma::mat outmat = inmat;
|
max@1
|
1217
|
max@1
|
1218 for (int i=0; i<inmat.n_rows; ++i)
|
max@1
|
1219 {
|
max@1
|
1220 if (arma::sum(inmat.row(i)) > 0)
|
max@1
|
1221 {
|
max@1
|
1222 // Take a window of rows
|
max@1
|
1223 int startWin;
|
max@1
|
1224 int endWin;
|
max@1
|
1225
|
max@1
|
1226 if (0 > i-len)
|
max@1
|
1227 startWin = 0;
|
max@1
|
1228 else
|
max@1
|
1229 startWin = i-len;
|
max@1
|
1230
|
max@1
|
1231 if (inmat.n_rows-1 < i+len-1)
|
max@1
|
1232 endWin = inmat.n_rows-1;
|
max@1
|
1233 else
|
max@1
|
1234 endWin = i+len-1;
|
max@1
|
1235
|
max@1
|
1236 outmat(i,span::all) = arma::max(inmat(span(startWin,endWin),span::all));
|
max@1
|
1237 }
|
max@1
|
1238 }
|
max@1
|
1239
|
max@1
|
1240 return outmat;
|
max@1
|
1241
|
max@1
|
1242 }
|
max@1
|
1243
|
max@1
|
1244 // Null Parts
|
max@1
|
1245 Part nullpart(vector<Part> parts, arma::vec barline)
|
max@1
|
1246 {
|
max@1
|
1247 arma::uvec nullindices = arma::ones<arma::uvec>(barline.size());
|
Chris@21
|
1248 for (int iPart=0; iPart<parts.size(); ++iPart)
|
max@1
|
1249 {
|
Chris@21
|
1250 //for (int iIndex=0; iIndex < parts[0].indices.size(); ++iIndex)
|
Chris@21
|
1251 for (int iIndex=0; iIndex < parts[iPart].indices.size(); ++iIndex)
|
Chris@21
|
1252 for (int i=0; i<parts[iPart].n; ++i)
|
max@1
|
1253 {
|
Chris@21
|
1254 int ind = parts[iPart].indices[iIndex]+i;
|
max@1
|
1255 nullindices(ind) = 0;
|
max@1
|
1256 }
|
max@1
|
1257 }
|
max@7
|
1258
|
max@1
|
1259 Part newPart;
|
max@1
|
1260 newPart.n = 1;
|
max@1
|
1261 uvec q = find(nullindices > 0);
|
max@1
|
1262
|
Chris@21
|
1263 for (int i=0; i<q.size();++i)
|
max@1
|
1264 newPart.indices.push_back(q(i));
|
max@7
|
1265
|
max@1
|
1266 newPart.letter = '-';
|
max@1
|
1267 newPart.value = 0;
|
max@1
|
1268 newPart.level = 0;
|
max@1
|
1269
|
max@1
|
1270 return newPart;
|
max@1
|
1271 }
|
max@1
|
1272
|
max@1
|
1273
|
max@1
|
1274 // Merge Nulls
|
max@1
|
1275 void mergenulls(vector<Part> &parts)
|
max@1
|
1276 {
|
Chris@21
|
1277 for (int iPart=0; iPart<parts.size(); ++iPart)
|
max@1
|
1278 {
|
max@1
|
1279
|
max@1
|
1280 vector<Part> newVectorPart;
|
max@1
|
1281
|
max@1
|
1282 if (parts[iPart].letter.compare("-")==0)
|
max@1
|
1283 {
|
max@1
|
1284 sort (parts[iPart].indices.begin(), parts[iPart].indices.end());
|
Chris@21
|
1285 int newpartind = -1;
|
max@1
|
1286
|
max@1
|
1287 vector<int> indices;
|
max@1
|
1288 indices.push_back(-2);
|
max@1
|
1289
|
Chris@21
|
1290 for (int iIndex=0; iIndex<parts[iPart].indices.size(); ++iIndex)
|
max@1
|
1291 indices.push_back(parts[iPart].indices[iIndex]);
|
max@1
|
1292
|
Chris@21
|
1293 for (int iInd=1; iInd < indices.size(); ++iInd)
|
max@1
|
1294 {
|
max@1
|
1295 if (indices[iInd] - indices[iInd-1] > 1)
|
max@1
|
1296 {
|
max@1
|
1297 newpartind++;
|
max@1
|
1298
|
max@1
|
1299 Part newPart;
|
max@1
|
1300 newPart.letter = 'n';
|
max@1
|
1301 std::stringstream out;
|
max@1
|
1302 out << newpartind+1;
|
max@1
|
1303 newPart.letter.append(out.str());
|
max@1
|
1304 newPart.value = 20+newpartind+1;
|
max@1
|
1305 newPart.n = 1;
|
max@1
|
1306 newPart.indices.push_back(indices[iInd]);
|
max@1
|
1307 newPart.level = 0;
|
max@1
|
1308
|
max@1
|
1309 newVectorPart.push_back(newPart);
|
max@1
|
1310 }
|
max@1
|
1311 else
|
max@1
|
1312 {
|
max@1
|
1313 newVectorPart[newpartind].n = newVectorPart[newpartind].n+1;
|
max@1
|
1314 }
|
max@1
|
1315 }
|
max@1
|
1316 parts.erase (parts.end());
|
max@1
|
1317
|
Chris@21
|
1318 for (int i=0; i<newVectorPart.size(); ++i)
|
max@1
|
1319 parts.push_back(newVectorPart[i]);
|
max@1
|
1320 }
|
max@1
|
1321 }
|
max@1
|
1322 }
|
max@1
|
1323
|
max@1
|
1324 /* ------ Segmentation ------ */
|
max@1
|
1325
|
Chris@19
|
1326 vector<Part> songSegment(Vamp::Plugin::FeatureList quantisedChromagram)
|
max@1
|
1327 {
|
max@1
|
1328
|
max@1
|
1329
|
max@1
|
1330 /* ------ Parameters ------ */
|
max@1
|
1331 double thresh_beat = 0.85;
|
max@1
|
1332 double thresh_seg = 0.80;
|
max@1
|
1333 int medfilt_length = 5;
|
max@1
|
1334 int minlength = 28;
|
max@1
|
1335 int maxlength = 128;
|
max@1
|
1336 double quantilePerc = 0.1;
|
max@1
|
1337 /* ------------------------ */
|
max@1
|
1338
|
max@1
|
1339
|
max@1
|
1340 // Collect Info
|
Chris@19
|
1341 int nBeat = quantisedChromagram.size(); // Number of feature vector
|
Chris@19
|
1342 int nFeatValues = quantisedChromagram[0].values.size(); // Number of values for each feature vector
|
max@1
|
1343
|
Chris@27
|
1344 if (nBeat < minlength) {
|
Chris@27
|
1345 // return a single part
|
Chris@27
|
1346 vector<Part> parts;
|
Chris@27
|
1347 Part newPart;
|
Chris@27
|
1348 newPart.n = 1;
|
Chris@27
|
1349 newPart.indices.push_back(0);
|
Chris@27
|
1350 newPart.letter = "n1";
|
Chris@27
|
1351 newPart.value = 20;
|
Chris@27
|
1352 newPart.level = 0;
|
Chris@27
|
1353 parts.push_back(newPart);
|
Chris@27
|
1354 return parts;
|
Chris@27
|
1355 }
|
Chris@27
|
1356
|
max@1
|
1357 arma::irowvec timeStamp = arma::zeros<arma::imat>(1,nBeat); // Vector of Time Stamps
|
max@1
|
1358
|
Chris@22
|
1359 // Save time stamp as a Vector
|
Chris@19
|
1360 if (quantisedChromagram[0].hasTimestamp)
|
max@1
|
1361 {
|
Chris@21
|
1362 for (int i = 0; i < nBeat; ++ i)
|
Chris@19
|
1363 timeStamp[i] = quantisedChromagram[i].timestamp.nsec;
|
max@1
|
1364 }
|
max@1
|
1365
|
max@1
|
1366
|
max@1
|
1367 // Build a ObservationTOFeatures Matrix
|
max@1
|
1368 arma::mat featVal = arma::zeros<mat>(nBeat,nFeatValues/2);
|
max@1
|
1369
|
Chris@21
|
1370 for (int i = 0; i < nBeat; ++ i)
|
Chris@21
|
1371 for (int j = 0; j < nFeatValues/2; ++ j)
|
max@1
|
1372 {
|
Chris@19
|
1373 featVal(i,j) = (quantisedChromagram[i].values[j]+quantisedChromagram[i].values[j+12]) * 0.8;
|
max@1
|
1374 }
|
max@1
|
1375
|
max@1
|
1376 // Set to arbitrary value to feature vectors with low std
|
max@1
|
1377 arma::mat a = stddev(featVal,1,1);
|
max@1
|
1378
|
max@1
|
1379 // Feature Colleration Matrix
|
max@1
|
1380 arma::mat simmat0 = 1-arma::cor(arma::trans(featVal));
|
max@1
|
1381
|
max@1
|
1382
|
Chris@21
|
1383 for (int i = 0; i < nBeat; ++ i)
|
max@1
|
1384 {
|
max@1
|
1385 if (a(i)<0.000001)
|
max@1
|
1386 {
|
max@1
|
1387 featVal(i,1) = 1000; // arbitrary
|
max@1
|
1388
|
Chris@21
|
1389 for (int j = 0; j < nFeatValues/2; ++j)
|
max@1
|
1390 {
|
max@1
|
1391 simmat0(i,j) = 1;
|
max@1
|
1392 simmat0(j,i) = 1;
|
max@1
|
1393 }
|
max@1
|
1394 }
|
max@1
|
1395 }
|
max@1
|
1396
|
max@1
|
1397 arma::mat simmat = 1-simmat0/2;
|
max@1
|
1398
|
max@1
|
1399 // -------- To delate when the proble with the add of beat will be solved -------
|
Chris@21
|
1400 for (int i = 0; i < nBeat; ++ i)
|
Chris@21
|
1401 for (int j = 0; j < nBeat; ++ j)
|
max@1
|
1402 if (!std::isfinite(simmat(i,j)))
|
max@1
|
1403 simmat(i,j)=0;
|
max@1
|
1404 // ------------------------------------------------------------------------------
|
max@1
|
1405
|
max@1
|
1406 // Median Filtering applied to the Correlation Matrix
|
max@1
|
1407 // The median filter is for each diagonal of the Matrix
|
max@1
|
1408 arma::mat median_simmat = arma::zeros<arma::mat>(nBeat,nBeat);
|
max@1
|
1409
|
Chris@21
|
1410 for (int i = 0; i < nBeat; ++ i)
|
max@1
|
1411 {
|
max@1
|
1412 arma::vec temp = medfilt1(simmat.diag(i),medfilt_length);
|
max@1
|
1413 median_simmat.diag(i) = temp;
|
max@1
|
1414 median_simmat.diag(-i) = temp;
|
max@1
|
1415 }
|
max@1
|
1416
|
Chris@21
|
1417 for (int i = 0; i < nBeat; ++ i)
|
Chris@21
|
1418 for (int j = 0; j < nBeat; ++ j)
|
max@1
|
1419 if (!std::isfinite(median_simmat(i,j)))
|
max@1
|
1420 median_simmat(i,j) = 0;
|
max@1
|
1421
|
max@1
|
1422 // -------------- NOT CONVERTED -------------------------------------
|
max@1
|
1423 // if param.seg.standardise
|
max@1
|
1424 // med_median_simmat = repmat(median(median_simmat),nBeat,1);
|
max@1
|
1425 // std_median_simmat = repmat(std(median_simmat),nBeat,1);
|
max@1
|
1426 // median_simmat = (median_simmat - med_median_simmat) ./ std_median_simmat;
|
max@1
|
1427 // end
|
max@1
|
1428 // --------------------------------------------------------
|
max@1
|
1429
|
max@1
|
1430 // Retrieve Bar Bounderies
|
max@1
|
1431 arma::uvec dup = find(median_simmat > thresh_beat);
|
max@1
|
1432 arma::mat potential_duplicates = arma::zeros<arma::mat>(nBeat,nBeat);
|
max@1
|
1433 potential_duplicates.elem(dup) = arma::ones<arma::vec>(dup.size());
|
max@1
|
1434 potential_duplicates = trimatu(potential_duplicates);
|
max@1
|
1435
|
Chris@21
|
1436 int nPartlengths = round((maxlength-minlength)/4)+1;
|
max@1
|
1437 arma::vec partlengths = zeros<arma::vec>(nPartlengths);
|
max@1
|
1438
|
Chris@21
|
1439 for (int i = 0; i < nPartlengths; ++ i)
|
max@1
|
1440 partlengths(i) = (i*4)+ minlength;
|
max@1
|
1441
|
max@1
|
1442 // initialise arrays
|
max@1
|
1443 arma::cube simArray = zeros<arma::cube>(nBeat,nBeat,nPartlengths);
|
max@1
|
1444 arma::cube decisionArray2 = zeros<arma::cube>(nBeat,nBeat,nPartlengths);
|
max@1
|
1445
|
max@1
|
1446 int conta = 0;
|
max@1
|
1447
|
Chris@21
|
1448 //for (int iLength = 0; iLength < nPartlengths; ++ iLength)
|
Chris@21
|
1449 for (int iLength = 0; iLength < 20; ++ iLength)
|
max@1
|
1450 {
|
Chris@21
|
1451 int len = partlengths(iLength);
|
Chris@21
|
1452 int nUsedBeat = nBeat - len + 1; // number of potential rep beginnings: they can't overlap at the end of the song
|
max@1
|
1453
|
Chris@21
|
1454 for (int iBeat = 0; iBeat < nUsedBeat; ++ iBeat) // looping over all columns (arbitrarily chosen columns)
|
max@1
|
1455 {
|
max@1
|
1456 arma::uvec help2 = find(potential_duplicates(span(0,nUsedBeat-1),iBeat)==1);
|
max@1
|
1457
|
Chris@21
|
1458 for (int i=0; i<help2.size(); ++i)
|
max@1
|
1459 {
|
max@1
|
1460
|
max@1
|
1461 // measure how well two length len segments go together
|
max@1
|
1462 int kBeat = help2(i);
|
max@1
|
1463 arma::vec distrib = median_simmat(span(iBeat,iBeat+len-1),span(kBeat,kBeat+len-1)).diag(0);
|
max@1
|
1464 simArray(iBeat,kBeat,iLength) = quantile(distrib,quantilePerc);
|
max@1
|
1465 }
|
max@1
|
1466 }
|
max@1
|
1467
|
max@1
|
1468 arma::mat tempM = simArray(span(0,nUsedBeat-1),span(0,nUsedBeat-1),span(iLength,iLength));
|
max@1
|
1469 simArray.slice(iLength)(span(0,nUsedBeat-1),span(0,nUsedBeat-1)) = tempM + arma::trans(tempM) - (eye<mat>(nUsedBeat,nUsedBeat)%tempM);
|
max@1
|
1470
|
max@1
|
1471 // convolution
|
max@1
|
1472 arma::vec K = arma::zeros<vec>(3);
|
max@1
|
1473 K << 0.01 << 0.98 << 0.01;
|
max@1
|
1474
|
max@1
|
1475
|
Chris@21
|
1476 for (int i=0; i<simArray.n_rows; ++i)
|
max@1
|
1477 {
|
max@1
|
1478 arma::rowvec t = arma::conv((arma::rowvec)simArray.slice(iLength).row(i),K);
|
max@1
|
1479 simArray.slice(iLength)(i,span::all) = t.subvec(1,t.size()-2);
|
max@1
|
1480 }
|
max@1
|
1481
|
max@1
|
1482 // take only over-average bars that do not overlap
|
max@1
|
1483
|
max@1
|
1484 arma::mat temp = arma::zeros<mat>(simArray.n_rows, simArray.n_cols);
|
max@1
|
1485 temp(span::all, span(0,nUsedBeat-1)) = simArray.slice(iLength)(span::all,span(0,nUsedBeat-1));
|
max@1
|
1486
|
Chris@21
|
1487 for (int i=0; i<temp.n_rows; ++i)
|
Chris@21
|
1488 for (int j=0; j<nUsedBeat; ++j)
|
max@1
|
1489 if (temp(i,j) < thresh_seg)
|
max@1
|
1490 temp(i,j) = 0;
|
max@1
|
1491
|
max@1
|
1492 decisionArray2.slice(iLength) = temp;
|
max@1
|
1493
|
max@1
|
1494 arma::mat maxMat = maxfilt1(decisionArray2.slice(iLength),len-1);
|
max@1
|
1495
|
Chris@21
|
1496 for (int i=0; i<decisionArray2.n_rows; ++i)
|
Chris@21
|
1497 for (int j=0; j<decisionArray2.n_cols; ++j)
|
max@1
|
1498 if (decisionArray2.slice(iLength)(i,j) < maxMat(i,j))
|
max@1
|
1499 decisionArray2.slice(iLength)(i,j) = 0;
|
max@1
|
1500
|
max@1
|
1501 decisionArray2.slice(iLength) = decisionArray2.slice(iLength) % arma::trans(decisionArray2.slice(iLength));
|
max@1
|
1502
|
Chris@21
|
1503 for (int i=0; i<simArray.n_rows; ++i)
|
Chris@21
|
1504 for (int j=0; j<simArray.n_cols; ++j)
|
max@1
|
1505 if (simArray.slice(iLength)(i,j) < thresh_seg)
|
max@1
|
1506 potential_duplicates(i,j) = 0;
|
max@1
|
1507 }
|
max@1
|
1508
|
max@1
|
1509 // Milk the data
|
max@1
|
1510
|
max@1
|
1511 arma::mat bestval;
|
max@1
|
1512
|
Chris@21
|
1513 for (int iLength=0; iLength<nPartlengths; ++iLength)
|
max@1
|
1514 {
|
max@1
|
1515 arma::mat temp = arma::zeros<arma::mat>(decisionArray2.n_rows,decisionArray2.n_cols);
|
max@1
|
1516
|
Chris@21
|
1517 for (int rows=0; rows<decisionArray2.n_rows; ++rows)
|
Chris@21
|
1518 for (int cols=0; cols<decisionArray2.n_cols; ++cols)
|
max@1
|
1519 if (decisionArray2.slice(iLength)(rows,cols) > 0)
|
max@1
|
1520 temp(rows,cols) = 1;
|
max@1
|
1521
|
max@1
|
1522 arma::vec currLogicSum = arma::sum(temp,1);
|
max@1
|
1523
|
Chris@21
|
1524 for (int iBeat=0; iBeat<nBeat; ++iBeat)
|
max@1
|
1525 if (currLogicSum(iBeat) > 1)
|
max@1
|
1526 {
|
max@1
|
1527 arma::vec t = decisionArray2.slice(iLength)(span::all,iBeat);
|
max@1
|
1528 double currSum = sum(t);
|
max@1
|
1529
|
Chris@21
|
1530 int count = 0;
|
Chris@21
|
1531 for (int i=0; i<t.size(); ++i)
|
max@1
|
1532 if (t(i)>0)
|
max@1
|
1533 count++;
|
max@1
|
1534
|
max@1
|
1535 currSum = (currSum/count)/2;
|
max@1
|
1536
|
max@1
|
1537 arma::rowvec t1;
|
max@1
|
1538 t1 << (currLogicSum(iBeat)-1) * partlengths(iLength) << currSum << iLength << iBeat << currLogicSum(iBeat);
|
max@1
|
1539
|
max@1
|
1540 bestval = join_cols(bestval,t1);
|
max@1
|
1541 }
|
max@1
|
1542 }
|
max@1
|
1543
|
max@1
|
1544 // Definition of the resulting vector
|
max@1
|
1545 vector<Part> parts;
|
max@1
|
1546
|
max@1
|
1547 // make a table of all valid sets of parts
|
max@1
|
1548
|
max@1
|
1549 char partletters[] = {'A','B','C','D','E','F','G', 'H','I','J','K','L','M','N','O','P','Q','R','S'};
|
Chris@21
|
1550 int partvalues[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
|
max@1
|
1551 arma::vec valid_sets = arma::ones<arma::vec>(bestval.n_rows);
|
max@1
|
1552
|
max@1
|
1553 if (!bestval.is_empty())
|
max@1
|
1554 {
|
max@1
|
1555
|
max@1
|
1556 // In questo punto viene introdotto un errore alla 3 cifra decimale
|
max@1
|
1557
|
max@1
|
1558 arma::colvec t = arma::zeros<arma::colvec>(bestval.n_rows);
|
Chris@21
|
1559 for (int i=0; i<bestval.n_rows; ++i)
|
max@1
|
1560 {
|
max@1
|
1561 t(i) = bestval(i,1)*2;
|
max@1
|
1562 }
|
max@1
|
1563
|
max@1
|
1564 double m = t.max();
|
max@1
|
1565
|
max@1
|
1566 bestval(span::all,1) = bestval(span::all,1) / m;
|
max@1
|
1567 bestval(span::all,0) = bestval(span::all,0) + bestval(span::all,1);
|
max@1
|
1568
|
max@1
|
1569 arma::mat bestval2;
|
Chris@21
|
1570 for (int i=0; i<bestval.n_cols; ++i)
|
max@1
|
1571 if (i!=1)
|
max@1
|
1572 bestval2 = join_rows(bestval2,bestval.col(i));
|
max@1
|
1573
|
Chris@21
|
1574 for (int kSeg=0; kSeg<6; ++kSeg)
|
max@1
|
1575 {
|
max@1
|
1576 arma::mat currbestvals = arma::zeros<arma::mat>(bestval2.n_rows, bestval2.n_cols);
|
Chris@21
|
1577 for (int i=0; i<bestval2.n_rows; ++i)
|
Chris@21
|
1578 for (int j=0; j<bestval2.n_cols; ++j)
|
max@1
|
1579 if (valid_sets(i))
|
max@1
|
1580 currbestvals(i,j) = bestval2(i,j);
|
max@1
|
1581
|
max@1
|
1582 arma::vec t1 = currbestvals.col(0);
|
max@1
|
1583 double ma;
|
max@1
|
1584 uword maIdx;
|
max@1
|
1585 ma = t1.max(maIdx);
|
max@6
|
1586
|
max@6
|
1587 if ((maIdx == 0)&&(ma == 0))
|
max@6
|
1588 break;
|
max@1
|
1589
|
Chris@28
|
1590 int bestLength = lrint(partlengths(currbestvals(maIdx,1)));
|
max@1
|
1591 arma::rowvec bestIndices = decisionArray2.slice(currbestvals(maIdx,1))(currbestvals(maIdx,2),span::all);
|
max@1
|
1592
|
max@1
|
1593 arma::rowvec bestIndicesMap = arma::zeros<arma::rowvec>(bestIndices.size());
|
Chris@21
|
1594 for (int i=0; i<bestIndices.size(); ++i)
|
max@1
|
1595 if (bestIndices(i)>0)
|
max@1
|
1596 bestIndicesMap(i) = 1;
|
max@1
|
1597
|
max@1
|
1598 arma::rowvec mask = arma::zeros<arma::rowvec>(bestLength*2-1);
|
Chris@21
|
1599 for (int i=0; i<bestLength; ++i)
|
max@1
|
1600 mask(i+bestLength-1) = 1;
|
max@1
|
1601
|
max@1
|
1602 arma::rowvec t2 = arma::conv(bestIndicesMap,mask);
|
max@1
|
1603 arma::rowvec island = t2.subvec(mask.size()/2,t2.size()-1-mask.size()/2);
|
max@1
|
1604
|
max@1
|
1605 // Save results in the structure
|
max@1
|
1606 Part newPart;
|
max@1
|
1607 newPart.n = bestLength;
|
max@1
|
1608 uvec q1 = find(bestIndices > 0);
|
max@1
|
1609
|
Chris@21
|
1610 for (int i=0; i<q1.size();++i)
|
max@1
|
1611 newPart.indices.push_back(q1(i));
|
max@1
|
1612
|
max@1
|
1613 newPart.letter = partletters[kSeg];
|
max@1
|
1614 newPart.value = partvalues[kSeg];
|
max@1
|
1615 newPart.level = kSeg+1;
|
max@1
|
1616 parts.push_back(newPart);
|
max@1
|
1617
|
max@1
|
1618 uvec q2 = find(valid_sets==1);
|
max@1
|
1619
|
Chris@21
|
1620 for (int i=0; i<q2.size(); ++i)
|
max@1
|
1621 {
|
Chris@21
|
1622 int iSet = q2(i);
|
Chris@21
|
1623 int s = partlengths(bestval2(iSet,1));
|
max@1
|
1624
|
max@1
|
1625 arma::rowvec mask1 = arma::zeros<arma::rowvec>(s*2-1);
|
Chris@21
|
1626 for (int i=0; i<s; ++i)
|
max@1
|
1627 mask1(i+s-1) = 1;
|
max@1
|
1628
|
max@1
|
1629 arma::rowvec Ind = decisionArray2.slice(bestval2(iSet,1))(bestval2(iSet,2),span::all);
|
max@1
|
1630 arma::rowvec IndMap = arma::zeros<arma::rowvec>(Ind.size());
|
Chris@21
|
1631 for (int i=0; i<Ind.size(); ++i)
|
max@1
|
1632 if (Ind(i)>0)
|
max@1
|
1633 IndMap(i) = 2;
|
max@1
|
1634
|
max@1
|
1635 arma::rowvec t3 = arma::conv(IndMap,mask1);
|
max@6
|
1636 arma::rowvec currislands = t3.subvec(mask1.size()/2,t3.size()-1-mask1.size()/2);
|
max@1
|
1637 arma::rowvec islandsdMult = currislands%island;
|
max@6
|
1638
|
max@1
|
1639 arma::uvec islandsIndex = find(islandsdMult > 0);
|
max@1
|
1640
|
max@6
|
1641 if (islandsIndex.size() > 0)
|
max@1
|
1642 valid_sets(iSet) = 0;
|
max@1
|
1643 }
|
max@1
|
1644 }
|
max@1
|
1645 }
|
max@1
|
1646 else
|
max@1
|
1647 {
|
max@1
|
1648 Part newPart;
|
max@1
|
1649 newPart.n = nBeat;
|
max@1
|
1650 newPart.indices.push_back(1);
|
max@1
|
1651 newPart.letter = 'A';
|
max@1
|
1652 newPart.value = 1;
|
max@1
|
1653 newPart.level = 1;
|
max@1
|
1654 parts.push_back(newPart);
|
max@1
|
1655 }
|
max@6
|
1656
|
max@1
|
1657 arma::vec bar = linspace(1,nBeat,nBeat);
|
max@1
|
1658 Part np = nullpart(parts,bar);
|
max@7
|
1659
|
max@1
|
1660 parts.push_back(np);
|
max@1
|
1661
|
max@1
|
1662 // -------------- NOT CONVERTED -------------------------------------
|
max@1
|
1663 // if param.seg.editor
|
max@1
|
1664 // [pa, ta] = partarray(parts);
|
max@1
|
1665 // parts = editorssearch(pa, ta, parts);
|
max@1
|
1666 // parts = [parts, nullpart(parts,1:nBeat)];
|
max@1
|
1667 // end
|
max@1
|
1668 // ------------------------------------------------------------------
|
max@1
|
1669
|
max@1
|
1670
|
max@1
|
1671 mergenulls(parts);
|
max@1
|
1672
|
max@1
|
1673
|
max@1
|
1674 // -------------- NOT CONVERTED -------------------------------------
|
max@1
|
1675 // if param.seg.editor
|
max@1
|
1676 // [pa, ta] = partarray(parts);
|
max@1
|
1677 // parts = editorssearch(pa, ta, parts);
|
max@1
|
1678 // parts = [parts, nullpart(parts,1:nBeat)];
|
max@1
|
1679 // end
|
max@1
|
1680 // ------------------------------------------------------------------
|
max@1
|
1681
|
max@1
|
1682 return parts;
|
max@1
|
1683 }
|
max@1
|
1684
|
max@1
|
1685
|
max@1
|
1686
|
Chris@19
|
1687 void songSegmentChroma(Vamp::Plugin::FeatureList quantisedChromagram, vector<Part> &parts)
|
max@1
|
1688 {
|
max@1
|
1689 // Collect Info
|
Chris@19
|
1690 int nBeat = quantisedChromagram.size(); // Number of feature vector
|
Chris@19
|
1691 int nFeatValues = quantisedChromagram[0].values.size(); // Number of values for each feature vector
|
max@1
|
1692
|
max@1
|
1693 arma::mat synchTreble = arma::zeros<mat>(nBeat,nFeatValues/2);
|
max@1
|
1694
|
Chris@21
|
1695 for (int i = 0; i < nBeat; ++ i)
|
Chris@21
|
1696 for (int j = 0; j < nFeatValues/2; ++ j)
|
max@1
|
1697 {
|
Chris@19
|
1698 synchTreble(i,j) = quantisedChromagram[i].values[j];
|
max@1
|
1699 }
|
max@1
|
1700
|
max@1
|
1701 arma::mat synchBass = arma::zeros<mat>(nBeat,nFeatValues/2);
|
max@1
|
1702
|
Chris@21
|
1703 for (int i = 0; i < nBeat; ++ i)
|
Chris@21
|
1704 for (int j = 0; j < nFeatValues/2; ++ j)
|
max@1
|
1705 {
|
Chris@19
|
1706 synchBass(i,j) = quantisedChromagram[i].values[j+12];
|
max@1
|
1707 }
|
max@1
|
1708
|
max@1
|
1709 // Process
|
max@1
|
1710
|
Chris@19
|
1711 arma::mat segTreble = arma::zeros<arma::mat>(quantisedChromagram.size(),quantisedChromagram[0].values.size()/2);
|
Chris@19
|
1712 arma::mat segBass = arma::zeros<arma::mat>(quantisedChromagram.size(),quantisedChromagram[0].values.size()/2);
|
max@1
|
1713
|
Chris@21
|
1714 for (int iPart=0; iPart<parts.size(); ++iPart)
|
max@1
|
1715 {
|
max@1
|
1716 parts[iPart].nInd = parts[iPart].indices.size();
|
max@1
|
1717
|
Chris@21
|
1718 for (int kOccur=0; kOccur<parts[iPart].nInd; ++kOccur)
|
max@1
|
1719 {
|
max@1
|
1720 int kStartIndex = parts[iPart].indices[kOccur];
|
max@1
|
1721 int kEndIndex = kStartIndex + parts[iPart].n-1;
|
max@1
|
1722
|
max@1
|
1723 segTreble.rows(kStartIndex,kEndIndex) = segTreble.rows(kStartIndex,kEndIndex) + synchTreble.rows(kStartIndex,kEndIndex);
|
max@1
|
1724 segBass.rows(kStartIndex,kEndIndex) = segBass.rows(kStartIndex,kEndIndex) + synchBass.rows(kStartIndex,kEndIndex);
|
max@1
|
1725 }
|
max@1
|
1726 }
|
max@1
|
1727 }
|
max@1
|
1728
|
max@1
|
1729
|
max@1
|
1730 // Segment Integration
|
max@1
|
1731 vector<Part> songSegmentIntegration(vector<Part> &parts)
|
max@1
|
1732 {
|
max@1
|
1733 // Break up parts (every part will have one instance)
|
max@1
|
1734 vector<Part> newPartVector;
|
max@1
|
1735 vector<int> partindices;
|
max@1
|
1736
|
Chris@21
|
1737 for (int iPart=0; iPart<parts.size(); ++iPart)
|
max@1
|
1738 {
|
max@1
|
1739 parts[iPart].nInd = parts[iPart].indices.size();
|
Chris@21
|
1740 for (int iInstance=0; iInstance<parts[iPart].nInd; ++iInstance)
|
max@1
|
1741 {
|
max@1
|
1742 Part newPart;
|
max@1
|
1743 newPart.n = parts[iPart].n;
|
max@1
|
1744 newPart.letter = parts[iPart].letter;
|
max@1
|
1745 newPart.value = parts[iPart].value;
|
max@1
|
1746 newPart.level = parts[iPart].level;
|
max@1
|
1747 newPart.indices.push_back(parts[iPart].indices[iInstance]);
|
max@1
|
1748 newPart.nInd = 1;
|
max@1
|
1749 partindices.push_back(parts[iPart].indices[iInstance]);
|
max@1
|
1750
|
max@1
|
1751 newPartVector.push_back(newPart);
|
max@1
|
1752 }
|
max@1
|
1753 }
|
max@1
|
1754
|
max@1
|
1755
|
max@1
|
1756 // Sort the parts in order of occurrence
|
max@1
|
1757 sort (partindices.begin(), partindices.end());
|
max@1
|
1758
|
Chris@21
|
1759 for (int i=0; i<partindices.size(); ++i)
|
max@1
|
1760 {
|
max@1
|
1761 bool found = false;
|
max@1
|
1762 int in=0;
|
max@1
|
1763 while (!found)
|
max@1
|
1764 {
|
max@1
|
1765 if (newPartVector[in].indices[0] == partindices[i])
|
max@1
|
1766 {
|
max@1
|
1767 newPartVector.push_back(newPartVector[in]);
|
max@1
|
1768 newPartVector.erase(newPartVector.begin()+in);
|
max@1
|
1769 found = true;
|
max@1
|
1770 }
|
max@1
|
1771 else
|
max@1
|
1772 in++;
|
max@1
|
1773 }
|
max@1
|
1774 }
|
max@1
|
1775
|
max@1
|
1776 // Clear the vector
|
Chris@21
|
1777 for (int iNewpart=1; iNewpart < newPartVector.size(); ++iNewpart)
|
max@1
|
1778 {
|
max@1
|
1779 if (newPartVector[iNewpart].n < 12)
|
max@1
|
1780 {
|
max@1
|
1781 newPartVector[iNewpart-1].n = newPartVector[iNewpart-1].n + newPartVector[iNewpart].n;
|
max@1
|
1782 newPartVector.erase(newPartVector.begin()+iNewpart);
|
max@1
|
1783 }
|
max@1
|
1784 }
|
max@1
|
1785
|
max@1
|
1786 return newPartVector;
|
max@1
|
1787 }
|
max@1
|
1788
|
max@1
|
1789 // Segmenter
|
Chris@19
|
1790 Vamp::Plugin::FeatureList SongPartitioner::runSegmenter(Vamp::Plugin::FeatureList quantisedChromagram)
|
max@1
|
1791 {
|
max@1
|
1792 /* --- Display Information --- */
|
Chris@19
|
1793 int numBeat = quantisedChromagram.size();
|
Chris@19
|
1794 int numFeats = quantisedChromagram[0].values.size();
|
max@1
|
1795
|
max@1
|
1796 vector<Part> parts;
|
max@1
|
1797 vector<Part> finalParts;
|
max@1
|
1798
|
Chris@19
|
1799 parts = songSegment(quantisedChromagram);
|
Chris@19
|
1800 songSegmentChroma(quantisedChromagram,parts);
|
max@7
|
1801
|
max@1
|
1802 finalParts = songSegmentIntegration(parts);
|
max@1
|
1803
|
max@1
|
1804
|
max@1
|
1805 // TEMP ----
|
Chris@21
|
1806 /*for (int i=0;i<finalParts.size(); ++i)
|
max@1
|
1807 {
|
max@6
|
1808 std::cout << "Parts n° " << i << std::endl;
|
max@6
|
1809 std::cout << "n°: " << finalParts[i].n << std::endl;
|
max@6
|
1810 std::cout << "letter: " << finalParts[i].letter << std::endl;
|
max@1
|
1811
|
max@6
|
1812 std::cout << "indices: ";
|
Chris@21
|
1813 for (int j=0;j<finalParts[i].indices.size(); ++j)
|
max@6
|
1814 std::cout << finalParts[i].indices[j] << " ";
|
max@6
|
1815
|
max@6
|
1816 std::cout << std::endl;
|
max@6
|
1817 std::cout << "level: " << finalParts[i].level << std::endl;
|
max@1
|
1818 }*/
|
max@1
|
1819
|
max@1
|
1820 // ---------
|
max@1
|
1821
|
max@1
|
1822
|
max@1
|
1823 // Output
|
max@1
|
1824
|
max@1
|
1825 Vamp::Plugin::FeatureList results;
|
max@1
|
1826
|
max@1
|
1827
|
max@1
|
1828 Feature seg;
|
max@1
|
1829
|
max@1
|
1830 arma::vec indices;
|
Chris@21
|
1831 int idx=0;
|
max@1
|
1832 vector<int> values;
|
max@1
|
1833 vector<string> letters;
|
max@1
|
1834
|
Chris@21
|
1835 for (int iPart=0; iPart<finalParts.size()-1; ++iPart)
|
max@1
|
1836 {
|
Chris@21
|
1837 int iInstance=0;
|
max@1
|
1838 seg.hasTimestamp = true;
|
max@1
|
1839
|
max@1
|
1840 int ind = finalParts[iPart].indices[iInstance];
|
max@1
|
1841 int ind1 = finalParts[iPart+1].indices[iInstance];
|
max@1
|
1842
|
Chris@19
|
1843 seg.timestamp = quantisedChromagram[ind].timestamp;
|
max@1
|
1844 seg.hasDuration = true;
|
Chris@19
|
1845 seg.duration = quantisedChromagram[ind1].timestamp-quantisedChromagram[ind].timestamp;
|
max@1
|
1846 seg.values.clear();
|
max@1
|
1847 seg.values.push_back(finalParts[iPart].value);
|
max@1
|
1848 seg.label = finalParts[iPart].letter;
|
max@1
|
1849
|
max@1
|
1850 results.push_back(seg);
|
max@1
|
1851 }
|
max@1
|
1852
|
max@1
|
1853 int ind = finalParts[finalParts.size()-1].indices[0];
|
Chris@19
|
1854 seg.timestamp = quantisedChromagram[ind].timestamp;
|
max@1
|
1855 seg.hasDuration = true;
|
Chris@19
|
1856 seg.duration = quantisedChromagram[quantisedChromagram.size()-1].timestamp-quantisedChromagram[ind].timestamp;
|
max@1
|
1857 seg.values.clear();
|
max@1
|
1858 seg.values.push_back(finalParts[finalParts.size()-1].value);
|
max@1
|
1859 seg.label = finalParts[finalParts.size()-1].letter;
|
max@1
|
1860
|
max@1
|
1861 results.push_back(seg);
|
max@1
|
1862
|
max@1
|
1863 return results;
|
max@1
|
1864 }
|
max@1
|
1865
|
max@1
|
1866
|
max@1
|
1867
|
max@1
|
1868
|
max@1
|
1869
|
max@1
|
1870
|
max@1
|
1871
|
max@1
|
1872
|
max@1
|
1873
|
max@1
|
1874
|
max@1
|
1875
|
max@1
|
1876
|
max@1
|
1877
|
max@1
|
1878
|
max@1
|
1879
|
max@1
|
1880
|
max@1
|
1881
|