-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTemplateDetector.cpp
470 lines (408 loc) · 14 KB
/
TemplateDetector.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
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
#include "TemplateDetector.h"
#include <iostream>
#include <numeric>
#include <algorithm>
#include <cmath>
using namespace std;
#include "templates.h"
static const int kDebugExtraHeight = 0;
static const float kDefaultLowPassWeight = 0.06;
TemplateDetector::TemplateDetector(float inputSampleRate) : Plugin(inputSampleRate) {
m_blockSize = 512;
m_sensitivity = 2.0;
m_startBin = 3;
m_maxShiftDown = 2;
m_maxShiftUp = 2;
m_hysterisisFactor = 1.5;
m_lowPassWeight = kDefaultLowPassWeight;
m_minFrames = 20;
m_template = 0;
}
TemplateDetector::~TemplateDetector() {
}
string TemplateDetector::getIdentifier() const {
return "templatedetector";
}
string TemplateDetector::getName() const {
return "Template detector";
}
string TemplateDetector::getDescription() const {
// Return something helpful here!
return "Detects continuous sounds based on a template";
}
string TemplateDetector::getMaker() const {
// Your name here
return "Tristan Hume";
}
int TemplateDetector::getPluginVersion() const {
// Increment this each time you release a version that behaves
// differently from the previous one
return 1;
}
string TemplateDetector::getCopyright() const {
// This function is not ideally named. It does not necessarily
// need to say who made the plugin -- getMaker does that -- but it
// should indicate the terms under which it is distributed. For
// example, "Copyright (year). All Rights Reserved", or "GPL"
return "MIT";
}
TemplateDetector::InputDomain TemplateDetector::getInputDomain() const {
return FrequencyDomain;
}
size_t TemplateDetector::getPreferredBlockSize() const {
return m_blockSize;
}
size_t TemplateDetector::getPreferredStepSize() const {
return m_blockSize/4;
}
size_t TemplateDetector::getMinChannelCount() const {
return 1;
}
size_t TemplateDetector::getMaxChannelCount() const {
return 1;
}
TemplateDetector::ParameterList TemplateDetector::getParameterDescriptors() const {
ParameterList list;
// If the plugin has no adjustable parameters, return an empty
// list here (and there's no need to provide implementations of
// getParameter and setParameter in that case either).
// Note that it is your responsibility to make sure the parameters
// start off having their default values (e.g. in the constructor
// above). The host needs to know the default value so it can do
// things like provide a "reset to default" function, but it will
// not explicitly set your parameters to their defaults for you if
// they have not changed in the mean time.
ParameterDescriptor d;
d.identifier = "tempindex";
d.name = "Template index";
d.description = "Index of the template to be used in the templates list";
d.unit = "";
d.minValue = 0;
d.maxValue = kNumTemplates-1;
d.defaultValue = 0;
d.isQuantized = true;
d.quantizeStep = 1.0;
list.push_back(d);
d.identifier = "sensitivity";
d.name = "Trigger threshold";
d.description = "The activation threshold below which a pop is registered";
d.unit = "";
d.minValue = 0;
d.maxValue = 15;
d.defaultValue = 2.0;
d.isQuantized = false;
list.push_back(d);
d.identifier = "hysterisis";
d.name = "Trigger hysterisis";
d.description = "The factor of the trigger threshold required to untrigger";
d.unit = "";
d.minValue = 0;
d.maxValue = 3;
d.defaultValue = 1.5;
d.isQuantized = false;
list.push_back(d);
d.identifier = "lowpass";
d.name = "Low Pass Filter Weight";
d.description = "The factor to give new samples in the weighted average";
d.unit = "";
d.minValue = 0;
d.maxValue = 1;
d.defaultValue = kDefaultLowPassWeight;
d.isQuantized = false;
list.push_back(d);
d.identifier = "minframes";
d.name = "Minimum time";
d.description = "The minimum number of frames of matchiness to consider a match";
d.unit = "";
d.minValue = 0;
d.maxValue = 100;
d.defaultValue = 20;
d.isQuantized = true;
d.quantizeStep = 1.0;
list.push_back(d);
d.identifier = "startbin";
d.name = "High pass filter";
d.description = "Ignore bins below this bin number.";
d.unit = "";
d.minValue = 0;
d.maxValue = 5;
d.defaultValue = 3;
d.isQuantized = true;
d.quantizeStep = 1.0;
list.push_back(d);
d.identifier = "maxshiftdown";
d.name = "Maximum Template Shift Down";
d.description = "Largest number of bins in down direction template can be shifted to match.";
d.unit = "";
d.minValue = 0;
d.maxValue = 10;
d.defaultValue = 4;
d.isQuantized = true;
d.quantizeStep = 1.0;
list.push_back(d);
d.identifier = "maxshiftup";
d.name = "Maximum Template Shift Up";
d.description = "Largest number of bins in down direction template can be shifted to match.";
d.unit = "";
d.minValue = 0;
d.maxValue = 10;
d.defaultValue = 2;
d.isQuantized = true;
d.quantizeStep = 1.0;
list.push_back(d);
return list;
}
float TemplateDetector::getParameter(string identifier) const {
if (identifier == "sensitivity") {
return m_sensitivity; // return the ACTUAL current value of your parameter here!
} else if(identifier == "hysterisis") {
return m_hysterisisFactor;
} else if(identifier == "tempindex") {
return m_template;
} else if(identifier == "lowpass") {
return m_lowPassWeight;
} else if(identifier == "minframes") {
return m_minFrames;
} else if(identifier == "startbin") {
return m_startBin;
} else if(identifier == "maxshiftdown") {
return m_maxShiftDown;
} else if(identifier == "maxshiftup") {
return m_maxShiftUp;
}
return 0;
}
void TemplateDetector::setParameter(string identifier, float value) {
if (identifier == "sensitivity") {
m_sensitivity = value;
} else if(identifier == "hysterisis") {
m_hysterisisFactor = value;
} else if(identifier == "tempindex") {
m_template = value;
} else if(identifier == "lowpass") {
m_lowPassWeight = value;
} else if(identifier == "minframes") {
m_minFrames = value;
} else if(identifier == "startbin") {
m_startBin = value;
} else if(identifier == "maxshiftdown") {
m_maxShiftDown = value;
} else if(identifier == "maxshiftup") {
m_maxShiftUp = value;
}
}
TemplateDetector::ProgramList TemplateDetector::getPrograms() const {
ProgramList list;
list.push_back("blow1");
list.push_back("blow2");
// If you have no programs, return an empty list (or simply don't
// implement this function or getCurrentProgram/selectProgram)
return list;
}
string TemplateDetector::getCurrentProgram() const {
return "blow1"; // no programs
}
void TemplateDetector::selectProgram(string prog) {
if(prog == "blow2") {
m_template = 1;
m_startBin = 5;
m_sensitivity = 3.7;
m_hysterisisFactor = 1.4;
m_maxShiftUp = 4;
m_maxShiftDown = 4;
}
}
bool TemplateDetector::initialise(size_t channels, size_t, size_t blockSize) {
if (channels < getMinChannelCount() ||
channels > getMaxChannelCount()) return false;
// Real initialisation work goes here!
m_blockSize = blockSize;
lowPassBuffer.resize(m_blockSize / 2 + 1, 0.0);
m_templateMax = bufferMax(kTemplates[m_template].data);
buffer.clear();
for(unsigned i = 0; i < kTemplates[m_template].size(); ++i) {
buffer.push_back(0.0);
}
m_consecutiveMatches = 0;
return true;
}
void TemplateDetector::reset() {
// clear buffer
for(auto &&x : buffer) {
x = 0.0;
}
}
TemplateDetector::FeatureSet TemplateDetector::getRemainingFeatures() {
return FeatureSet();
}
TemplateDetector::OutputList TemplateDetector::getOutputDescriptors() const {
OutputList list;
OutputDescriptor d;
d.identifier = "powerspectrum";
d.name = "Power Spectrum";
d.description = "Power values of the frequency spectrum bins calculated from the input signal";
d.unit = "";
d.hasFixedBinCount = true;
if (m_blockSize == 0) {
// Just so as not to return "1". This is the bin count that
// would result from a block size of 1024, which is a likely
// default -- but the host should always set the block size
// before querying the bin count for certain.
d.binCount = 257;
} else {
d.binCount = m_blockSize / 2 + 1;
}
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
list.push_back(d);
d.identifier = "debugspectrum";
d.name = "Debug Spectrum";
d.description = "Spectrum containing special debugging info";
d.binCount = kTemplates[m_template].height()-m_startBin+kDebugExtraHeight;
// all attributes are already set to the right value
list.push_back(d);
d.identifier = "diff";
d.name = "Template difference";
d.description = "The extent to which the template pop matches the buffer.";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 1;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::OneSamplePerStep;
list.push_back(d);
d.identifier = "starts";
d.name = "Trigger onsets";
d.description = "Instants where a real-time recognizer could recognize a tss had occured.";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 0;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::VariableSampleRate;
d.sampleRate = m_inputSampleRate;
list.push_back(d);
d.identifier = "stops";
d.name = "Off instants";
d.description = "Instants where a real-time recognizer could recognize a tss had stopped.";
d.unit = "";
d.hasFixedBinCount = true;
d.binCount = 0;
d.hasKnownExtents = false;
d.isQuantized = false;
d.sampleType = OutputDescriptor::VariableSampleRate;
d.sampleRate = m_inputSampleRate;
list.push_back(d);
return list;
}
TemplateDetector::FeatureSet TemplateDetector::process(const float *const *inputBuffers, Vamp::RealTime timestamp) {
FeatureSet fs;
if (m_blockSize == 0) {
cerr << "ERROR: TemplateDetector::process: Not initialised" << endl;
return fs;
}
size_t n = m_blockSize / 2 + 1;
const float *fbuf = inputBuffers[0];
for (size_t i = 0; i < n; ++i) {
double real = fbuf[i * 2];
double imag = fbuf[i * 2 + 1];
double newVal = real * real + imag * imag;
lowPassBuffer[i] = lowPassBuffer[i]*(1.0-m_lowPassWeight) + newVal*m_lowPassWeight;
}
Feature spectrum;
spectrum.hasTimestamp = false;
spectrum.values = lowPassBuffer;
fs[0].push_back(spectrum);
const TemplateInfo *tplate = &(kTemplates[m_template]);
// update buffer forward one time step
for(unsigned i = 0; i < tplate->primaryHeight; ++i) {
buffer.pop_front();
buffer.push_back(spectrum.values[i]);
}
// high frequencies aren't useful so we bin them all together
buffer.pop_front();
float highSum = accumulate(spectrum.values.begin()+tplate->primaryHeight,spectrum.values.end(),0.0);
buffer.push_back(highSum);
// compute max ignoring lower bins
float maxVal = -1000.0;
for(auto it = buffer.begin(); it != buffer.end(); ++it) {
if((it-buffer.begin()) % tplate->height() < m_startBin) continue;
if(*it >= maxVal) {
maxVal = *it;
}
}
// positive shift makes peak lower frequency, negative makes it higher
float minDiff = 10000000.0;
for(int i = -m_maxShiftUp; i < m_maxShiftDown; ++i) {
float diff = templateDiff(maxVal, i);
if(diff < minDiff) minDiff = diff;
}
Feature diffFeat;
diffFeat.hasTimestamp = false;
diffFeat.values.push_back(minDiff);
fs[2].push_back(diffFeat);
if(minDiff < m_sensitivity ||
(m_consecutiveMatches > 0 && minDiff < m_sensitivity*m_hysterisisFactor)) {
m_consecutiveMatches += 1;
if(m_consecutiveMatches == m_minFrames) {
Feature instant;
instant.hasTimestamp = true;
instant.timestamp = timestamp;
fs[3].push_back(instant);
}
} else {
if(m_consecutiveMatches >= m_minFrames) {
Feature instant;
instant.hasTimestamp = true;
instant.timestamp = timestamp;
fs[4].push_back(instant);
}
m_consecutiveMatches = 0;
}
Feature debug;
debug.hasTimestamp = false;
for (size_t i = m_startBin; i < tplate->height(); ++i) {
float val = buffer[(tplate->size()-tplate->height())+i];
debug.values.push_back(val);
}
fs[1].push_back(debug);
return fs;
}
float TemplateDetector::templateAt(int i, int shift) {
const TemplateInfo *tplate = &(kTemplates[m_template]);
int bin = i % tplate->height();
if(i % tplate->height() >= tplate->primaryHeight) {
return tplate->data[i]/m_templateMax;
}
if(bin+shift < 0 || bin+shift >= (int)(tplate->primaryHeight)) {
return 0.0;
}
return tplate->data[i+shift]/m_templateMax;
}
float TemplateDetector::diffCol(int templStart, int bufStart, float maxVal, int shift) {
float diff = 0;
for(unsigned i = m_startBin; i < kTemplates[m_template].height(); ++i) {
float d = templateAt(templStart+i, shift) - buffer[bufStart+i]/maxVal;
diff += abs(d);
}
return diff;
}
float TemplateDetector::templateDiff(float maxVal, int shift) {
float diff = 0;
for(unsigned i = 0; i < kTemplates[m_template].size(); i += kTemplates[m_template].height()) {
diff += diffCol(i,i, maxVal,shift);
}
return diff;
}
float TemplateDetector::bufferMax(const float * const buf) const {
float maxVal = -1000.0;
const TemplateInfo *tplate = &(kTemplates[m_template]);
for(unsigned i = 0; i < tplate->size(); ++i) {
if(i % tplate->height() < m_startBin) continue;
if(buf[i] >= maxVal) {
maxVal = buf[i];
}
}
return maxVal;
}