-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBeatRootVampPlugin.cpp
346 lines (287 loc) · 9.03 KB
/
BeatRootVampPlugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Vamp feature extraction plugin for the BeatRoot beat tracker.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2011 Simon Dixon, Chris Cannam and QMUL.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version. See the file
COPYING included with this distribution for more information.
*/
#include "BeatRootVampPlugin.h"
#include "BeatRootProcessor.h"
#include "Event.h"
#include <vamp-sdk/RealTime.h>
#include <vamp-sdk/PluginAdapter.h>
BeatRootVampPlugin::BeatRootVampPlugin(float inputSampleRate) :
Plugin(inputSampleRate),
m_firstFrame(true)
{
m_processor = new BeatRootProcessor(inputSampleRate, AgentParameters());
}
BeatRootVampPlugin::~BeatRootVampPlugin()
{
delete m_processor;
}
string
BeatRootVampPlugin::getIdentifier() const
{
return "beatroot";
}
string
BeatRootVampPlugin::getName() const
{
return "BeatRoot Beat Tracker";
}
string
BeatRootVampPlugin::getDescription() const
{
return "Identify beat locations in music";
}
string
BeatRootVampPlugin::getMaker() const
{
return "Simon Dixon (plugin by Chris Cannam)";
}
int
BeatRootVampPlugin::getPluginVersion() const
{
// Increment this each time you release a version that behaves
// differently from the previous one
return 1;
}
string
BeatRootVampPlugin::getCopyright() const
{
return "GPL";
}
BeatRootVampPlugin::InputDomain
BeatRootVampPlugin::getInputDomain() const
{
return FrequencyDomain;
}
size_t
BeatRootVampPlugin::getPreferredBlockSize() const
{
return m_processor->getFFTSize();
}
size_t
BeatRootVampPlugin::getPreferredStepSize() const
{
return m_processor->getHopSize();
}
size_t
BeatRootVampPlugin::getMinChannelCount() const
{
return 1;
}
size_t
BeatRootVampPlugin::getMaxChannelCount() const
{
return 1;
}
BeatRootVampPlugin::ParameterList
BeatRootVampPlugin::getParameterDescriptors() const
{
ParameterList list;
ParameterDescriptor desc;
desc.identifier = "preMarginFactor";
desc.name = "Pre-Margin Factor";
desc.description = "The maximum amount by which a beat can be earlier than the predicted beat time, expressed as a fraction of the beat period.";
desc.minValue = 0;
desc.maxValue = 1;
desc.defaultValue = AgentParameters::DEFAULT_PRE_MARGIN_FACTOR;
desc.isQuantized = false;
list.push_back(desc);
desc.identifier = "postMarginFactor";
desc.name = "Post-Margin Factor";
desc.description = "The maximum amount by which a beat can be later than the predicted beat time, expressed as a fraction of the beat period.";
desc.minValue = 0;
desc.maxValue = 1;
desc.defaultValue = AgentParameters::DEFAULT_POST_MARGIN_FACTOR;
desc.isQuantized = false;
list.push_back(desc);
desc.identifier = "maxChange";
desc.name = "Maximum Change";
desc.description = "The maximum allowed deviation from the initial tempo, expressed as a fraction of the initial beat period.";
desc.minValue = 0;
desc.maxValue = 1;
desc.defaultValue = AgentParameters::DEFAULT_MAX_CHANGE;
desc.isQuantized = false;
list.push_back(desc);
desc.identifier = "expiryTime";
desc.name = "Expiry Time";
desc.description = "The default value of expiryTime, which is the time (in seconds) after which an Agent that has no Event matching its beat predictions will be destroyed.";
desc.minValue = 2;
desc.maxValue = 120;
desc.defaultValue = AgentParameters::DEFAULT_EXPIRY_TIME;
desc.isQuantized = false;
list.push_back(desc);
// Simon says...
// These are the parameters that should be exposed (Agent.cpp):
// If Pop, both margins should be lower (0.1). If classical
// music, post margin can be increased
//
// double Agent::POST_MARGIN_FACTOR = 0.3;
// double Agent::PRE_MARGIN_FACTOR = 0.15;
//
// Max Change tells us how much tempo can change - so for
// classical we should make it higher
//
// double Agent::MAX_CHANGE = 0.2;
//
// The EXPIRY TIME default should be defaulted to 100 (usual cause
// of agents dying....) it should also be exposed in order to
// troubleshoot eventual problems in songs with big silences in
// the beggining/end.
//
// const double Agent::DEFAULT_EXPIRY_TIME = 10.0;
return list;
}
float
BeatRootVampPlugin::getParameter(string identifier) const
{
if (identifier == "preMarginFactor") {
return m_parameters.preMarginFactor;
} else if (identifier == "postMarginFactor") {
return m_parameters.postMarginFactor;
} else if (identifier == "maxChange") {
return m_parameters.maxChange;
} else if (identifier == "expiryTime") {
return m_parameters.expiryTime;
}
return 0;
}
void
BeatRootVampPlugin::setParameter(string identifier, float value)
{
if (identifier == "preMarginFactor") {
m_parameters.preMarginFactor = value;
} else if (identifier == "postMarginFactor") {
m_parameters.postMarginFactor = value;
} else if (identifier == "maxChange") {
m_parameters.maxChange = value;
} else if (identifier == "expiryTime") {
m_parameters.expiryTime = value;
}
}
BeatRootVampPlugin::ProgramList
BeatRootVampPlugin::getPrograms() const
{
ProgramList list;
return list;
}
string
BeatRootVampPlugin::getCurrentProgram() const
{
return ""; // no programs
}
void
BeatRootVampPlugin::selectProgram(string name)
{
}
BeatRootVampPlugin::OutputList
BeatRootVampPlugin::getOutputDescriptors() const
{
OutputList list;
// See OutputDescriptor documentation for the possibilities here.
// Every plugin must have at least one output.
OutputDescriptor d;
d.identifier = "beats";
d.name = "Beats";
d.description = "Estimated beat locations";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 0;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::VariableSampleRate;
d.sampleRate = m_inputSampleRate;
d.hasDuration = false;
list.push_back(d);
d.identifier = "unfilled";
d.name = "Un-interpolated beats";
d.description = "Locations of detected beats, before agent interpolation occurs";
list.push_back(d);
return list;
}
bool
BeatRootVampPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
{
if (channels < getMinChannelCount() ||
channels > getMaxChannelCount()) {
std::cerr << "BeatRootVampPlugin::initialise: Unsupported number ("
<< channels << ") of channels" << std::endl;
return false;
}
if (stepSize != getPreferredStepSize()) {
std::cerr << "BeatRootVampPlugin::initialise: Unsupported step size "
<< "for sample rate (" << stepSize << ", required step is "
<< getPreferredStepSize() << " for rate " << m_inputSampleRate
<< ")" << std::endl;
return false;
}
if (blockSize != getPreferredBlockSize()) {
std::cerr << "BeatRootVampPlugin::initialise: Unsupported block size "
<< "for sample rate (" << blockSize << ", required size is "
<< getPreferredBlockSize() << " for rate " << m_inputSampleRate
<< ")" << std::endl;
return false;
}
// Delete the processor that was created with default parameters
// and used to determine the expected step and block size; replace
// with one using the actual parameters we have
delete m_processor;
m_processor = new BeatRootProcessor(m_inputSampleRate, m_parameters);
return true;
}
void
BeatRootVampPlugin::reset()
{
m_processor->reset();
m_firstFrame = true;
m_origin = Vamp::RealTime::zeroTime;
}
BeatRootVampPlugin::FeatureSet
BeatRootVampPlugin::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
{
if (m_firstFrame) {
m_origin = timestamp;
m_firstFrame = false;
}
m_processor->processFrame(inputBuffers);
return FeatureSet();
}
BeatRootVampPlugin::FeatureSet
BeatRootVampPlugin::getRemainingFeatures()
{
EventList unfilled;
EventList el = m_processor->beatTrack(&unfilled);
Feature f;
f.hasTimestamp = true;
f.hasDuration = false;
f.label = "";
f.values.clear();
FeatureSet fs;
for (EventList::const_iterator i = el.begin(); i != el.end(); ++i) {
f.timestamp = m_origin + Vamp::RealTime::fromSeconds(i->time);
fs[0].push_back(f);
}
for (EventList::const_iterator i = unfilled.begin();
i != unfilled.end(); ++i) {
f.timestamp = m_origin + Vamp::RealTime::fromSeconds(i->time);
fs[1].push_back(f);
}
return fs;
}
static Vamp::PluginAdapter<BeatRootVampPlugin> brAdapter;
const VampPluginDescriptor *vampGetPluginDescriptor(unsigned int version,
unsigned int index)
{
if (version < 1) return 0;
switch (index) {
case 0: return brAdapter.getDescriptor();
default: return 0;
}
}