c@27
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@27
|
2
|
c@27
|
3 /*
|
c@27
|
4 QM Vamp Plugin Set
|
c@27
|
5
|
c@27
|
6 Centre for Digital Music, Queen Mary, University of London.
|
c@27
|
7 All rights reserved.
|
c@27
|
8 */
|
c@27
|
9
|
c@27
|
10 #include "BeatTrack.h"
|
c@27
|
11
|
c@27
|
12 #include <dsp/onsets/DetectionFunction.h>
|
c@27
|
13 #include <dsp/onsets/PeakPicking.h>
|
c@27
|
14 #include <dsp/tempotracking/TempoTrack.h>
|
c@27
|
15
|
c@27
|
16 using std::string;
|
c@27
|
17 using std::vector;
|
c@27
|
18 using std::cerr;
|
c@27
|
19 using std::endl;
|
c@27
|
20
|
c@27
|
21 float BeatTracker::m_stepSecs = 0.01161;
|
c@27
|
22
|
c@27
|
23 class BeatTrackerData
|
c@27
|
24 {
|
c@27
|
25 public:
|
c@27
|
26 BeatTrackerData(const DFConfig &config) : dfConfig(config) {
|
c@27
|
27 df = new DetectionFunction(config);
|
c@27
|
28 }
|
c@27
|
29 ~BeatTrackerData() {
|
c@27
|
30 delete df;
|
c@27
|
31 }
|
c@27
|
32 void reset() {
|
c@27
|
33 delete df;
|
c@27
|
34 df = new DetectionFunction(dfConfig);
|
c@27
|
35 dfOutput.clear();
|
c@27
|
36 }
|
c@27
|
37
|
c@27
|
38 DFConfig dfConfig;
|
c@27
|
39 DetectionFunction *df;
|
c@27
|
40 vector<double> dfOutput;
|
c@27
|
41 };
|
c@27
|
42
|
c@27
|
43
|
c@27
|
44 BeatTracker::BeatTracker(float inputSampleRate) :
|
c@27
|
45 Vamp::Plugin(inputSampleRate),
|
c@27
|
46 m_d(0),
|
c@27
|
47 m_dfType(DF_COMPLEXSD)
|
c@27
|
48 {
|
c@27
|
49 }
|
c@27
|
50
|
c@27
|
51 BeatTracker::~BeatTracker()
|
c@27
|
52 {
|
c@27
|
53 delete m_d;
|
c@27
|
54 }
|
c@27
|
55
|
c@27
|
56 string
|
c@27
|
57 BeatTracker::getIdentifier() const
|
c@27
|
58 {
|
c@27
|
59 return "qm-tempotracker";
|
c@27
|
60 }
|
c@27
|
61
|
c@27
|
62 string
|
c@27
|
63 BeatTracker::getName() const
|
c@27
|
64 {
|
c@27
|
65 return "Tempo and Beat Tracker";
|
c@27
|
66 }
|
c@27
|
67
|
c@27
|
68 string
|
c@27
|
69 BeatTracker::getDescription() const
|
c@27
|
70 {
|
c@27
|
71 return "Estimate beat locations and tempo";
|
c@27
|
72 }
|
c@27
|
73
|
c@27
|
74 string
|
c@27
|
75 BeatTracker::getMaker() const
|
c@27
|
76 {
|
c@27
|
77 return "Christian Landone and Matthew Davies, Queen Mary, University of London";
|
c@27
|
78 }
|
c@27
|
79
|
c@27
|
80 int
|
c@27
|
81 BeatTracker::getPluginVersion() const
|
c@27
|
82 {
|
c@27
|
83 return 3;
|
c@27
|
84 }
|
c@27
|
85
|
c@27
|
86 string
|
c@27
|
87 BeatTracker::getCopyright() const
|
c@27
|
88 {
|
c@27
|
89 return "Copyright (c) 2006-2007 - All Rights Reserved";
|
c@27
|
90 }
|
c@27
|
91
|
c@27
|
92 BeatTracker::ParameterList
|
c@27
|
93 BeatTracker::getParameterDescriptors() const
|
c@27
|
94 {
|
c@27
|
95 ParameterList list;
|
c@27
|
96
|
c@27
|
97 ParameterDescriptor desc;
|
c@27
|
98 desc.identifier = "dftype";
|
c@27
|
99 desc.name = "Onset Detection Function Type";
|
c@27
|
100 desc.description = "Method used to calculate the onset detection function";
|
c@27
|
101 desc.minValue = 0;
|
c@27
|
102 desc.maxValue = 3;
|
c@27
|
103 desc.defaultValue = 3;
|
c@27
|
104 desc.isQuantized = true;
|
c@27
|
105 desc.quantizeStep = 1;
|
c@27
|
106 desc.valueNames.push_back("High-Frequency Content");
|
c@27
|
107 desc.valueNames.push_back("Spectral Difference");
|
c@27
|
108 desc.valueNames.push_back("Phase Deviation");
|
c@27
|
109 desc.valueNames.push_back("Complex Domain");
|
c@27
|
110 desc.valueNames.push_back("Broadband Energy Rise");
|
c@27
|
111 list.push_back(desc);
|
c@27
|
112
|
c@27
|
113 return list;
|
c@27
|
114 }
|
c@27
|
115
|
c@27
|
116 float
|
c@27
|
117 BeatTracker::getParameter(std::string name) const
|
c@27
|
118 {
|
c@27
|
119 if (name == "dftype") {
|
c@27
|
120 switch (m_dfType) {
|
c@27
|
121 case DF_HFC: return 0;
|
c@27
|
122 case DF_SPECDIFF: return 1;
|
c@27
|
123 case DF_PHASEDEV: return 2;
|
c@27
|
124 default: case DF_COMPLEXSD: return 3;
|
c@27
|
125 case DF_BROADBAND: return 4;
|
c@27
|
126 }
|
c@27
|
127 }
|
c@27
|
128 return 0.0;
|
c@27
|
129 }
|
c@27
|
130
|
c@27
|
131 void
|
c@27
|
132 BeatTracker::setParameter(std::string name, float value)
|
c@27
|
133 {
|
c@27
|
134 if (name == "dftype") {
|
c@27
|
135 switch (lrintf(value)) {
|
c@27
|
136 case 0: m_dfType = DF_HFC; break;
|
c@27
|
137 case 1: m_dfType = DF_SPECDIFF; break;
|
c@27
|
138 case 2: m_dfType = DF_PHASEDEV; break;
|
c@27
|
139 default: case 3: m_dfType = DF_COMPLEXSD; break;
|
c@27
|
140 case 4: m_dfType = DF_BROADBAND; break;
|
c@27
|
141 }
|
c@27
|
142 }
|
c@27
|
143 }
|
c@27
|
144
|
c@27
|
145 bool
|
c@27
|
146 BeatTracker::initialise(size_t channels, size_t stepSize, size_t blockSize)
|
c@27
|
147 {
|
c@27
|
148 if (m_d) {
|
c@27
|
149 delete m_d;
|
c@27
|
150 m_d = 0;
|
c@27
|
151 }
|
c@27
|
152
|
c@27
|
153 if (channels < getMinChannelCount() ||
|
c@27
|
154 channels > getMaxChannelCount()) {
|
c@27
|
155 std::cerr << "BeatTracker::initialise: Unsupported channel count: "
|
c@27
|
156 << channels << std::endl;
|
c@27
|
157 return false;
|
c@27
|
158 }
|
c@27
|
159
|
c@28
|
160 if (stepSize != getPreferredStepSize()) {
|
c@28
|
161 std::cerr << "ERROR: BeatTracker::initialise: Unsupported step size for this sample rate: "
|
c@28
|
162 << stepSize << " (wanted " << (getPreferredStepSize()) << ")" << std::endl;
|
c@27
|
163 return false;
|
c@27
|
164 }
|
c@27
|
165
|
c@28
|
166 if (blockSize != getPreferredBlockSize()) {
|
c@29
|
167 std::cerr << "WARNING: BeatTracker::initialise: Sub-optimal block size for this sample rate: "
|
c@28
|
168 << blockSize << " (wanted " << getPreferredBlockSize() << ")" << std::endl;
|
c@28
|
169 // return false;
|
c@27
|
170 }
|
c@27
|
171
|
c@27
|
172 DFConfig dfConfig;
|
c@27
|
173 dfConfig.DFType = m_dfType;
|
c@27
|
174 dfConfig.stepSecs = float(stepSize) / m_inputSampleRate;
|
c@27
|
175 dfConfig.stepSize = stepSize;
|
c@27
|
176 dfConfig.frameLength = blockSize;
|
c@27
|
177 dfConfig.dbRise = 3;
|
c@27
|
178
|
c@27
|
179 m_d = new BeatTrackerData(dfConfig);
|
c@27
|
180 return true;
|
c@27
|
181 }
|
c@27
|
182
|
c@27
|
183 void
|
c@27
|
184 BeatTracker::reset()
|
c@27
|
185 {
|
c@27
|
186 if (m_d) m_d->reset();
|
c@27
|
187 }
|
c@27
|
188
|
c@27
|
189 size_t
|
c@27
|
190 BeatTracker::getPreferredStepSize() const
|
c@27
|
191 {
|
c@27
|
192 size_t step = size_t(m_inputSampleRate * m_stepSecs + 0.0001);
|
c@27
|
193 // std::cerr << "BeatTracker::getPreferredStepSize: input sample rate is " << m_inputSampleRate << ", step size is " << step << std::endl;
|
c@27
|
194 return step;
|
c@27
|
195 }
|
c@27
|
196
|
c@27
|
197 size_t
|
c@27
|
198 BeatTracker::getPreferredBlockSize() const
|
c@27
|
199 {
|
c@28
|
200 size_t theoretical = getPreferredStepSize() * 2;
|
c@28
|
201
|
c@28
|
202 //!!! need power of 2
|
c@28
|
203 return theoretical;
|
c@27
|
204 }
|
c@27
|
205
|
c@27
|
206 BeatTracker::OutputList
|
c@27
|
207 BeatTracker::getOutputDescriptors() const
|
c@27
|
208 {
|
c@27
|
209 OutputList list;
|
c@27
|
210
|
c@27
|
211 OutputDescriptor beat;
|
c@27
|
212 beat.identifier = "beats";
|
c@27
|
213 beat.name = "Beats";
|
c@27
|
214 beat.description = "Estimated metrical beat locations";
|
c@27
|
215 beat.unit = "";
|
c@27
|
216 beat.hasFixedBinCount = true;
|
c@27
|
217 beat.binCount = 0;
|
c@27
|
218 beat.sampleType = OutputDescriptor::VariableSampleRate;
|
c@27
|
219 beat.sampleRate = 1.0 / m_stepSecs;
|
c@27
|
220
|
c@27
|
221 OutputDescriptor df;
|
c@27
|
222 df.identifier = "detection_fn";
|
c@27
|
223 df.name = "Onset Detection Function";
|
c@27
|
224 df.description = "Probability function of note onset likelihood";
|
c@27
|
225 df.unit = "";
|
c@27
|
226 df.hasFixedBinCount = true;
|
c@27
|
227 df.binCount = 1;
|
c@27
|
228 df.hasKnownExtents = false;
|
c@27
|
229 df.isQuantized = false;
|
c@27
|
230 df.sampleType = OutputDescriptor::OneSamplePerStep;
|
c@27
|
231
|
c@27
|
232 OutputDescriptor tempo;
|
c@27
|
233 tempo.identifier = "tempo";
|
c@27
|
234 tempo.name = "Tempo";
|
c@27
|
235 tempo.description = "Locked tempo estimates";
|
c@27
|
236 tempo.unit = "bpm";
|
c@27
|
237 tempo.hasFixedBinCount = true;
|
c@27
|
238 tempo.binCount = 1;
|
c@27
|
239 tempo.sampleType = OutputDescriptor::VariableSampleRate;
|
c@27
|
240 tempo.sampleRate = 1.0 / m_stepSecs;
|
c@27
|
241
|
c@27
|
242 list.push_back(beat);
|
c@27
|
243 list.push_back(df);
|
c@27
|
244 list.push_back(tempo);
|
c@27
|
245
|
c@27
|
246 return list;
|
c@27
|
247 }
|
c@27
|
248
|
c@27
|
249 BeatTracker::FeatureSet
|
c@27
|
250 BeatTracker::process(const float *const *inputBuffers,
|
c@27
|
251 Vamp::RealTime /* timestamp */)
|
c@27
|
252 {
|
c@27
|
253 if (!m_d) {
|
c@27
|
254 cerr << "ERROR: BeatTracker::process: "
|
c@27
|
255 << "BeatTracker has not been initialised"
|
c@27
|
256 << endl;
|
c@27
|
257 return FeatureSet();
|
c@27
|
258 }
|
c@27
|
259
|
c@27
|
260 size_t len = m_d->dfConfig.frameLength / 2;
|
c@27
|
261
|
c@27
|
262 double *magnitudes = new double[len];
|
c@27
|
263 double *phases = new double[len];
|
c@27
|
264
|
c@27
|
265 // We only support a single input channel
|
c@27
|
266
|
c@27
|
267 for (size_t i = 0; i < len; ++i) {
|
c@27
|
268
|
c@27
|
269 magnitudes[i] = sqrt(inputBuffers[0][i*2 ] * inputBuffers[0][i*2 ] +
|
c@27
|
270 inputBuffers[0][i*2+1] * inputBuffers[0][i*2+1]);
|
c@27
|
271
|
c@27
|
272 phases[i] = atan2(-inputBuffers[0][i*2+1], inputBuffers[0][i*2]);
|
c@27
|
273 }
|
c@27
|
274
|
c@27
|
275 double output = m_d->df->process(magnitudes, phases);
|
c@27
|
276
|
c@27
|
277 delete[] magnitudes;
|
c@27
|
278 delete[] phases;
|
c@27
|
279
|
c@27
|
280 m_d->dfOutput.push_back(output);
|
c@27
|
281
|
c@27
|
282 FeatureSet returnFeatures;
|
c@27
|
283
|
c@27
|
284 Feature feature;
|
c@27
|
285 feature.hasTimestamp = false;
|
c@27
|
286 feature.values.push_back(output);
|
c@27
|
287
|
c@27
|
288 returnFeatures[1].push_back(feature); // detection function is output 1
|
c@27
|
289 return returnFeatures;
|
c@27
|
290 }
|
c@27
|
291
|
c@27
|
292 BeatTracker::FeatureSet
|
c@27
|
293 BeatTracker::getRemainingFeatures()
|
c@27
|
294 {
|
c@27
|
295 if (!m_d) {
|
c@27
|
296 cerr << "ERROR: BeatTracker::getRemainingFeatures: "
|
c@27
|
297 << "BeatTracker has not been initialised"
|
c@27
|
298 << endl;
|
c@27
|
299 return FeatureSet();
|
c@27
|
300 }
|
c@27
|
301
|
c@27
|
302 double aCoeffs[] = { 1.0000, -0.5949, 0.2348 };
|
c@27
|
303 double bCoeffs[] = { 0.1600, 0.3200, 0.1600 };
|
c@27
|
304
|
c@27
|
305 TTParams ttParams;
|
c@27
|
306 ttParams.winLength = 512;
|
c@27
|
307 ttParams.lagLength = 128;
|
c@27
|
308 ttParams.LPOrd = 2;
|
c@27
|
309 ttParams.LPACoeffs = aCoeffs;
|
c@27
|
310 ttParams.LPBCoeffs = bCoeffs;
|
c@27
|
311 ttParams.alpha = 9;
|
c@27
|
312 ttParams.WinT.post = 8;
|
c@27
|
313 ttParams.WinT.pre = 7;
|
c@27
|
314
|
c@27
|
315 TempoTrack tempoTracker(ttParams);
|
c@27
|
316
|
c@27
|
317 vector<double> tempos;
|
c@27
|
318 vector<int> beats = tempoTracker.process(m_d->dfOutput, &tempos);
|
c@27
|
319
|
c@27
|
320 FeatureSet returnFeatures;
|
c@27
|
321
|
c@27
|
322 char label[100];
|
c@27
|
323
|
c@27
|
324 for (size_t i = 0; i < beats.size(); ++i) {
|
c@27
|
325
|
c@27
|
326 size_t frame = beats[i] * m_d->dfConfig.stepSize;
|
c@27
|
327
|
c@27
|
328 Feature feature;
|
c@27
|
329 feature.hasTimestamp = true;
|
c@27
|
330 feature.timestamp = Vamp::RealTime::frame2RealTime
|
c@27
|
331 (frame, lrintf(m_inputSampleRate));
|
c@27
|
332
|
c@27
|
333 float bpm = 0.0;
|
c@27
|
334 int frameIncrement = 0;
|
c@27
|
335
|
c@27
|
336 if (i < beats.size() - 1) {
|
c@27
|
337
|
c@27
|
338 frameIncrement = (beats[i+1] - beats[i]) * m_d->dfConfig.stepSize;
|
c@27
|
339
|
c@27
|
340 // one beat is frameIncrement frames, so there are
|
c@27
|
341 // samplerate/frameIncrement bps, so
|
c@27
|
342 // 60*samplerate/frameIncrement bpm
|
c@27
|
343
|
c@27
|
344 if (frameIncrement > 0) {
|
c@27
|
345 bpm = (60.0 * m_inputSampleRate) / frameIncrement;
|
c@27
|
346 bpm = int(bpm * 100.0 + 0.5) / 100.0;
|
c@27
|
347 sprintf(label, "%.2f bpm", bpm);
|
c@27
|
348 feature.label = label;
|
c@27
|
349 }
|
c@27
|
350 }
|
c@27
|
351
|
c@27
|
352 returnFeatures[0].push_back(feature); // beats are output 0
|
c@27
|
353 }
|
c@27
|
354
|
c@27
|
355 double prevTempo = 0.0;
|
c@27
|
356
|
c@27
|
357 for (size_t i = 0; i < tempos.size(); ++i) {
|
c@27
|
358
|
c@27
|
359 size_t frame = i * m_d->dfConfig.stepSize * ttParams.lagLength;
|
c@27
|
360
|
c@27
|
361 // std::cerr << "unit " << i << ", step size " << m_d->dfConfig.stepSize << ", hop " << ttParams.lagLength << ", frame = " << frame << std::endl;
|
c@27
|
362
|
c@27
|
363 if (tempos[i] > 1 && int(tempos[i] * 100) != int(prevTempo * 100)) {
|
c@27
|
364 Feature feature;
|
c@27
|
365 feature.hasTimestamp = true;
|
c@27
|
366 feature.timestamp = Vamp::RealTime::frame2RealTime
|
c@27
|
367 (frame, lrintf(m_inputSampleRate));
|
c@27
|
368 feature.values.push_back(tempos[i]);
|
c@27
|
369 sprintf(label, "%.2f bpm", tempos[i]);
|
c@27
|
370 feature.label = label;
|
c@27
|
371 returnFeatures[2].push_back(feature); // tempo is output 2
|
c@27
|
372 }
|
c@27
|
373 }
|
c@27
|
374
|
c@27
|
375 return returnFeatures;
|
c@27
|
376 }
|
c@27
|
377
|