-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVampTestPlugin.cpp
More file actions
633 lines (551 loc) · 18.1 KB
/
VampTestPlugin.cpp
File metadata and controls
633 lines (551 loc) · 18.1 KB
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Vamp Test Plugin
Copyright (c) 2013-2016 Queen Mary, University of London
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the names of the Centre for
Digital Music and Queen Mary, University of London shall not be
used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior written authorization.
*/
#include "VampTestPlugin.h"
#include <vamp-sdk/FFT.h>
#include <iostream>
#include <sstream>
#include <cmath>
using namespace std;
using Vamp::RealTime;
VampTestPlugin::VampTestPlugin(float inputSampleRate, bool freq) :
Plugin(inputSampleRate),
m_frequencyDomain(freq),
m_produceOutput(true),
m_n(0),
m_channels(1),
m_stepSize(0),
m_blockSize(0)
{
for (int i = 0; i < 10; ++i) {
m_instants.push_back(RealTime::fromSeconds(1.5 * i));
}
}
VampTestPlugin::~VampTestPlugin()
{
}
string
VampTestPlugin::getIdentifier() const
{
if (m_frequencyDomain) {
return "vamp-test-plugin-freq";
} else {
return "vamp-test-plugin";
}
}
string
VampTestPlugin::getName() const
{
if (m_frequencyDomain) {
return "Vamp Test Plugin (Frequency-Domain Input)";
} else {
return "Vamp Test Plugin";
}
}
string
VampTestPlugin::getDescription() const
{
return "Test plugin for hosts handling various output types";
}
string
VampTestPlugin::getMaker() const
{
return "Chris Cannam";
}
int
VampTestPlugin::getPluginVersion() const
{
return 5;
}
string
VampTestPlugin::getCopyright() const
{
return "BSD";
}
VampTestPlugin::InputDomain
VampTestPlugin::getInputDomain() const
{
return m_frequencyDomain ? FrequencyDomain : TimeDomain;
}
size_t
VampTestPlugin::getPreferredBlockSize() const
{
return 0;
}
size_t
VampTestPlugin::getPreferredStepSize() const
{
return 0;
}
size_t
VampTestPlugin::getMinChannelCount() const
{
return 1;
}
size_t
VampTestPlugin::getMaxChannelCount() const
{
return 10;
}
VampTestPlugin::ParameterList
VampTestPlugin::getParameterDescriptors() const
{
ParameterList list;
// Provide one parameter, and make it so that we can easily tell
// whether it has been changed
ParameterDescriptor d;
d.identifier = "produce_output";
d.name = "Produce some output";
d.description = "Whether to produce any output. If this parameter is switched off, the plugin will produce no output. This is intended for basic testing of whether a host's parameter setting logic is functioning.";
d.unit = "";
d.minValue = 0;
d.maxValue = 1;
d.defaultValue = 1;
d.isQuantized = true;
d.quantizeStep = 1;
list.push_back(d);
return list;
}
float
VampTestPlugin::getParameter(string identifier) const
{
if (identifier == "produce_output") {
return m_produceOutput ? 1.f : 0.f;
}
return 0;
}
void
VampTestPlugin::setParameter(string identifier, float value)
{
if (identifier == "produce_output") {
m_produceOutput = (value > 0.5);
}
}
VampTestPlugin::ProgramList
VampTestPlugin::getPrograms() const
{
ProgramList list;
return list;
}
string
VampTestPlugin::getCurrentProgram() const
{
return ""; // no programs
}
void
VampTestPlugin::selectProgram(string)
{
}
VampTestPlugin::OutputList
VampTestPlugin::getOutputDescriptors() const
{
OutputList list;
int n = 0;
OutputDescriptor d;
d.identifier = "instants";
d.name = "Instants";
d.description = "Single time points without values";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 0;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::VariableSampleRate;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "curve-oss";
d.name = "Curve: OneSamplePerStep";
d.description = "A time series with one value per process block";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "curve-fsr";
d.name = "Curve: FixedSampleRate";
d.description = "A time series with equally-spaced values (independent of process step size)";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::FixedSampleRate;
d.sampleRate = 2.5;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "curve-fsr-timed";
d.name = "Curve: FixedSampleRate/Timed";
d.description = "A time series with a fixed sample rate (independent of process step size) but with timestamps on features";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::FixedSampleRate;
d.sampleRate = 2.5;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "curve-fsr-mixed";
d.name = "Curve: FixedSampleRate/Mixed";
d.description = "A time series with a fixed sample rate (independent of process step size) and with timestamps on some features";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::FixedSampleRate;
d.sampleRate = 2.5;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "curve-vsr";
d.name = "Curve: VariableSampleRate";
d.description = "A variably-spaced series of values";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::VariableSampleRate;
d.sampleRate = 0;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "grid-oss";
d.name = "Grid: OneSamplePerStep";
d.description = "A fixed-height grid of values with one column per process block";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 10;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
d.sampleRate = 0;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "grid-fsr";
d.name = "Grid: FixedSampleRate";
d.description = "A fixed-height grid of values with equally-spaced columns (independent of process step size)";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 10;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::FixedSampleRate;
d.sampleRate = 2.5;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "notes-regions";
d.name = "Notes or Regions";
d.description = "Variably-spaced features with one value and duration";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::VariableSampleRate;
d.sampleRate = 0;
d.hasDuration = true;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "input-summary";
d.name = "Data derived from inputs";
d.description = "One-sample-per-step features with n values, where n is the number of input channels. Each feature contains, for each input channel, the first sample value on that channel plus the total number of non-zero samples on that channel. (\"Non-zero\" is determined by comparison against a magnitude threshold which is actually 1e-6 rather than exactly zero.)";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = m_channels;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
d.identifier = "input-timestamp";
d.name = "Input timestamp";
d.description = "One-sample-per-step features with one value, containing the time in sample frames converted from the timestamp of the corresponding process input block.";
d.unit = "samples";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
d.hasDuration = false;
m_outputNumbers[d.identifier] = n++;
list.push_back(d);
return list;
}
bool
VampTestPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
{
if (channels < getMinChannelCount() ||
channels > getMaxChannelCount()) return false;
m_channels = channels;
m_stepSize = stepSize;
m_blockSize = blockSize;
return true;
}
void
VampTestPlugin::reset()
{
m_n = 0;
}
static Vamp::Plugin::Feature
instant(RealTime r, int i, int n)
{
stringstream s;
Vamp::Plugin::Feature f;
f.hasTimestamp = true;
f.timestamp = r;
f.hasDuration = false;
s << i+1 << " of " << n << " at " << r.toText();
f.label = s.str();
return f;
}
static Vamp::Plugin::Feature
untimedCurveValue(RealTime r, int i, int n)
{
stringstream s;
Vamp::Plugin::Feature f;
f.hasTimestamp = false;
f.hasDuration = false;
float v = float(i) / float(n);
f.values.push_back(v);
s << i+1 << " of " << n << ": " << v << " at " << r.toText();
f.label = s.str();
return f;
}
static Vamp::Plugin::Feature
timedCurveValue(RealTime r, int i, int n)
{
stringstream s;
Vamp::Plugin::Feature f;
f.hasTimestamp = true;
f.timestamp = r;
f.hasDuration = false;
float v = float(i) / float(n);
f.values.push_back(v);
s << i+1 << " of " << n << ": " << v << " at " << r.toText();
f.label = s.str();
return f;
}
static Vamp::Plugin::Feature
snappedCurveValue(RealTime r, RealTime sn, int i, int n)
{
stringstream s;
Vamp::Plugin::Feature f;
f.hasTimestamp = true;
f.timestamp = r;
f.hasDuration = false;
float v = float(i) / float(n);
f.values.push_back(v);
s << i+1 << " of " << n << ": " << v << " at " << r.toText() << " snap to " << sn.toText();
f.label = s.str();
return f;
}
static Vamp::Plugin::Feature
gridColumn(RealTime r, int i, int n)
{
stringstream s;
Vamp::Plugin::Feature f;
f.hasTimestamp = false;
f.hasDuration = false;
for (int j = 0; j < 10; ++j) {
float v = float(j + i + 2) / float(n + 10);
f.values.push_back(v);
}
s << i+1 << " of " << n << " at " << r.toText();
f.label = s.str();
return f;
}
static Vamp::Plugin::Feature
noteOrRegion(RealTime r, RealTime d, int i, int n)
{
stringstream s;
Vamp::Plugin::Feature f;
f.hasTimestamp = true;
f.timestamp = r;
f.hasDuration = true;
f.duration = d;
float v = float(i) / float(n);
f.values.push_back(v);
s << i+1 << " of " << n << ": " << v << " at " << r.toText() << " dur. " << d.toText();
f.label = s.str();
return f;
}
static
double snap(double x, double r)
{
double eps = 1e-9;
int n = int(x / r + 0.5 + eps);
return n * r;
}
Vamp::Plugin::FeatureSet
VampTestPlugin::featuresFrom(RealTime timestamp, bool final)
{
FeatureSet fs;
RealTime endTime = timestamp + RealTime::frame2RealTime
(m_stepSize, m_inputSampleRate);
for (int i = 0; i < (int)m_instants.size(); ++i) {
if (m_instants[i] >= timestamp && (final || m_instants[i] < endTime)) {
fs[m_outputNumbers["instants"]]
.push_back(instant(m_instants[i], i, m_instants.size()));
}
RealTime variCurveTime = m_instants[i] / 2;
if (variCurveTime >= timestamp && (final || variCurveTime < endTime)) {
fs[m_outputNumbers["curve-vsr"]]
.push_back(timedCurveValue(variCurveTime, i, m_instants.size()));
}
RealTime noteTime = (m_instants[i] + m_instants[i]) / 3;
RealTime noteDuration = RealTime::fromSeconds((i % 2 == 0) ? 1.75 : 0.5);
if (noteTime >= timestamp && (final || noteTime < endTime)) {
fs[m_outputNumbers["notes-regions"]]
.push_back(noteOrRegion(noteTime, noteDuration, i, m_instants.size()));
}
}
if (!final) {
if (m_n < 20) {
fs[m_outputNumbers["curve-oss"]]
.push_back(untimedCurveValue(timestamp, m_n, 20));
}
if (m_n < 5) {
fs[m_outputNumbers["curve-fsr"]]
.push_back(untimedCurveValue(RealTime::fromSeconds(m_n / 2.5), m_n, 10));
float s = (m_n / 4) * 2;
if ((m_n % 4) > 0) {
s += float((m_n % 4) - 1) / 6.0;
}
fs[m_outputNumbers["curve-fsr-timed"]]
.push_back(snappedCurveValue(RealTime::fromSeconds(s),
RealTime::fromSeconds(snap(s, 0.4)),
m_n, 10));
}
if (m_n < 20) {
fs[m_outputNumbers["grid-oss"]]
.push_back(gridColumn(timestamp, m_n, 20));
}
} else {
for (int i = (m_n > 5 ? 5 : m_n); i < 10; ++i) {
fs[m_outputNumbers["curve-fsr"]]
.push_back(untimedCurveValue(RealTime::fromSeconds(i / 2.5), i, 10));
float s = (i / 4) * 2;
if ((i % 4) > 0) {
s += float((i % 4) - 1) / 6.0;
}
fs[m_outputNumbers["curve-fsr-timed"]]
.push_back(snappedCurveValue(RealTime::fromSeconds(s),
RealTime::fromSeconds(snap(s, 0.4)),
i, 10));
}
for (int i = 0; i < 10; ++i) {
static std::vector<float> times {
2.4, 2.9, 3.14, 3.5, 4.0, 4.4, 3.9, 4.4, 4.8, 5
};
float s = times[i];
float sn = snap(s, 0.4) + 1e-5; // to avoid printing e.g. 2.799
if (i == 4 || i == 5 || i == 8) {
fs[m_outputNumbers["curve-fsr-mixed"]]
.push_back(untimedCurveValue(RealTime::fromSeconds(s),
i, 10));
} else {
fs[m_outputNumbers["curve-fsr-mixed"]]
.push_back(snappedCurveValue(RealTime::fromSeconds(s),
RealTime::fromSeconds(sn),
i, 10));
}
}
for (int i = 0; i < 10; ++i) {
fs[m_outputNumbers["grid-fsr"]]
.push_back(gridColumn(RealTime::fromSeconds(i / 2.5), i, 10));
}
}
m_lastTime = endTime;
m_n = m_n + 1;
return fs;
}
VampTestPlugin::FeatureSet
VampTestPlugin::process(const float *const *inputBuffers, RealTime timestamp)
{
if (!m_produceOutput) return FeatureSet();
FeatureSet fs = featuresFrom(timestamp, false);
Feature f;
float eps = 1e-6f;
for (int c = 0; c < m_channels; ++c) {
if (!m_frequencyDomain) {
// first value plus number of non-zero values
float sum = inputBuffers[c][0];
for (int i = 0; i < m_blockSize; ++i) {
if (fabsf(inputBuffers[c][i]) >= eps) sum += 1;
}
f.values.push_back(sum);
} else {
// If we're in frequency-domain mode, we convert back to
// time-domain to calculate the input-summary feature
// output. That should help the caller check that
// time-frequency conversion has gone more or less OK,
// though they'll still have to bear in mind windowing and
// FFT shift (i.e. phase shift which puts the first
// element in the middle of the frame)
vector<double> ri(m_blockSize, 0.0);
vector<double> ii(m_blockSize, 0.0);
vector<double> ro(m_blockSize, 0.0);
vector<double> io(m_blockSize, 0.0);
for (int i = 0; i <= m_blockSize/2; ++i) {
ri[i] = inputBuffers[c][i*2];
ii[i] = inputBuffers[c][i*2 + 1];
if (i > 0) ri[m_blockSize-i] = ri[i];
if (i > 0) ii[m_blockSize-i] = -ii[i];
}
Vamp::FFT::inverse(m_blockSize, &ri[0], &ii[0], &ro[0], &io[0]);
float sum = 0;
for (int i = 0; i < m_blockSize; ++i) {
if (fabs(ro[i]) >= eps) sum += 1;
}
sum += ro[0];
f.values.push_back(sum);
}
}
fs[m_outputNumbers["input-summary"]].push_back(f);
f.values.clear();
float frame = RealTime::realTime2Frame(timestamp, m_inputSampleRate);
f.values.push_back(frame);
fs[m_outputNumbers["input-timestamp"]].push_back(f);
return fs;
}
VampTestPlugin::FeatureSet
VampTestPlugin::getRemainingFeatures()
{
if (!m_produceOutput) return FeatureSet();
FeatureSet fs = featuresFrom(m_lastTime, true);
return fs;
}