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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
|
#include "vampHost.h"
int vampHost::runPlugin(string myname, string soname, string id, string output,
int outputNo, string inputFile, ostream& out, bool useFrames)
{
PluginLoader *loader = PluginLoader::getInstance();
PluginLoader::PluginKey key = loader->composePluginKey(soname, id);
SNDFILE *sndfile;
SF_INFO sfinfo;
memset(&sfinfo, 0, sizeof(SF_INFO));
sndfile = sf_open(inputFile.c_str(), SFM_READ, &sfinfo);
if (!sndfile) {
cerr << myname << ": ERROR: Failed to open input file \""
<< inputFile << "\": " << sf_strerror(sndfile) << endl;
return 1;
}
Plugin *plugin = loader->loadPlugin
(key, sfinfo.samplerate, PluginLoader::ADAPT_ALL_SAFE);
if (!plugin) {
cerr << myname << ": ERROR: Failed to load plugin \"" << id
<< "\" from library \"" << soname << "\"" << endl;
sf_close(sndfile);
return 1;
}
cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;
// Note that the following would be much simpler if we used a
// PluginBufferingAdapter as well -- i.e. if we had passed
// PluginLoader::ADAPT_ALL to loader->loadPlugin() above, instead
// of ADAPT_ALL_SAFE. Then we could simply specify our own block
// size, keep the step size equal to the block size, and ignore
// the plugin's bleatings. However, there are some issues with
// using a PluginBufferingAdapter that make the results sometimes
// technically different from (if effectively the same as) the
// un-adapted plugin, so we aren't doing that here. See the
// PluginBufferingAdapter documentation for details.
int blockSize = plugin->getPreferredBlockSize();
int stepSize = plugin->getPreferredStepSize();
if (blockSize == 0) {
blockSize = 1024;
}
if (stepSize == 0) {
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
stepSize = blockSize/2;
} else {
stepSize = blockSize;
}
} else if (stepSize > blockSize) {
cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
blockSize = stepSize * 2;
} else {
blockSize = stepSize;
}
cerr << blockSize << endl;
}
int overlapSize = blockSize - stepSize;
sf_count_t currentStep = 0;
int finalStepsRemaining = max(1, (blockSize / stepSize) - 1); // at end of file, this many part-silent frames needed after we hit EOF
int channels = sfinfo.channels;
float *filebuf = new float[blockSize * channels];
float **plugbuf = new float*[channels];
for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
cerr << "Using block size = " << blockSize << ", step size = "
<< stepSize << endl;
// The channel queries here are for informational purposes only --
// a PluginChannelAdapter is being used automatically behind the
// scenes, and it will take case of any channel mismatch
int minch = plugin->getMinChannelCount();
int maxch = plugin->getMaxChannelCount();
cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;
Plugin::OutputList outputs = plugin->getOutputDescriptors();
Plugin::OutputDescriptor od;
int returnValue = 1;
int progress = 0;
RealTime rt;
PluginWrapper *wrapper = 0;
RealTime adjustment = RealTime::zeroTime;
if (outputs.empty()) {
cerr << "ERROR: Plugin has no outputs!" << endl;
goto done;
}
if (outputNo < 0) {
for (size_t oi = 0; oi < outputs.size(); ++oi) {
if (outputs[oi].identifier == output) {
outputNo = oi;
break;
}
}
if (outputNo < 0) {
cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
goto done;
}
} else {
if (int(outputs.size()) <= outputNo) {
cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
goto done;
}
}
od = outputs[outputNo];
cerr << "Output is: \"" << od.identifier << "\"" << endl;
if (!plugin->initialise(channels, stepSize, blockSize)) {
cerr << "ERROR: Plugin initialise (channels = " << channels
<< ", stepSize = " << stepSize << ", blockSize = "
<< blockSize << ") failed." << endl;
goto done;
}
wrapper = dynamic_cast<PluginWrapper *>(plugin);
if (wrapper) {
// See documentation for
// PluginInputDomainAdapter::getTimestampAdjustment
PluginInputDomainAdapter *ida =
wrapper->getWrapper<PluginInputDomainAdapter>();
if (ida) adjustment = ida->getTimestampAdjustment();
}
// Here we iterate over the frames, avoiding asking the numframes in case it's streaming input.
do {
int count;
if ((blockSize==stepSize) || (currentStep==0)) {
// read a full fresh block
if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
break;
}
if (count != blockSize) --finalStepsRemaining;
} else {
// otherwise shunt the existing data down and read the remainder.
memmove(filebuf, filebuf + (stepSize * channels), overlapSize * channels * sizeof(float));
if ((count = sf_readf_float(sndfile, filebuf + (overlapSize * channels), stepSize)) < 0) {
cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
break;
}
if (count != stepSize) --finalStepsRemaining;
count += overlapSize;
}
for (int c = 0; c < channels; ++c) {
int j = 0;
while (j < count) {
plugbuf[c][j] = filebuf[j * sfinfo.channels + c];
++j;
}
while (j < blockSize) {
plugbuf[c][j] = 0.0f;
++j;
}
}
rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);
vampHost::printFeatures
(RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),
sfinfo.samplerate, outputNo, plugin->process(plugbuf, rt),
out, useFrames);
if (sfinfo.frames > 0){
int pp = progress;
progress = lrintf((float(currentStep * stepSize) / sfinfo.frames) * 100.f);
if (progress != pp && out) {
cerr << "\r" << progress << "%";
}
}
++currentStep;
} while (finalStepsRemaining > 0);
if (out) cerr << "\rDone" << endl;
rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);
vampHost::printFeatures(RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),
sfinfo.samplerate, outputNo,
plugin->getRemainingFeatures(), out, useFrames);
returnValue = 0;
done:
delete plugin;
sf_close(sndfile);
return returnValue;
}
int vampHost::rotorRunPlugin(string soname, string id, string output,
int outputNo, string inputFile, vector<float>& out, float& progress)
{
PluginLoader *loader = PluginLoader::getInstance();
PluginLoader::PluginKey key = loader->composePluginKey(soname, id);
SNDFILE *sndfile;
SF_INFO sfinfo;
memset(&sfinfo, 0, sizeof(SF_INFO));
sndfile = sf_open(inputFile.c_str(), SFM_READ, &sfinfo);
if (!sndfile) {
cerr << ": ERROR: Failed to open input file \""
<< inputFile << "\": " << sf_strerror(sndfile) << endl;
return 1;
}
Plugin *plugin = loader->loadPlugin
(key, sfinfo.samplerate, PluginLoader::ADAPT_ALL_SAFE);
if (!plugin) {
cerr << ": ERROR: Failed to load plugin \"" << id
<< "\" from library \"" << soname << "\"" << endl;
sf_close(sndfile);
return 1;
}
cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;
// Note that the following would be much simpler if we used a
// PluginBufferingAdapter as well -- i.e. if we had passed
// PluginLoader::ADAPT_ALL to loader->loadPlugin() above, instead
// of ADAPT_ALL_SAFE. Then we could simply specify our own block
// size, keep the step size equal to the block size, and ignore
// the plugin's bleatings. However, there are some issues with
// using a PluginBufferingAdapter that make the results sometimes
// technically different from (if effectively the same as) the
// un-adapted plugin, so we aren't doing that here. See the
// PluginBufferingAdapter documentation for details.
int blockSize = plugin->getPreferredBlockSize();
int stepSize = plugin->getPreferredStepSize();
if (blockSize == 0) {
blockSize = 1024;
}
if (stepSize == 0) {
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
stepSize = blockSize/2;
} else {
stepSize = blockSize;
}
} else if (stepSize > blockSize) {
cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
blockSize = stepSize * 2;
} else {
blockSize = stepSize;
}
cerr << blockSize << endl;
}
int overlapSize = blockSize - stepSize;
sf_count_t currentStep = 0;
int finalStepsRemaining = max(1, (blockSize / stepSize) - 1); // at end of file, this many part-silent frames needed after we hit EOF
int channels = sfinfo.channels;
float *filebuf = new float[blockSize * channels];
float **plugbuf = new float*[channels];
for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
cerr << "Using block size = " << blockSize << ", step size = "
<< stepSize << endl;
// The channel queries here are for informational purposes only --
// a PluginChannelAdapter is being used automatically behind the
// scenes, and it will take case of any channel mismatch
int minch = plugin->getMinChannelCount();
int maxch = plugin->getMaxChannelCount();
cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;
Plugin::OutputList outputs = plugin->getOutputDescriptors();
Plugin::OutputDescriptor od;
int returnValue = 1;
int prog = 0;
RealTime rt;
PluginWrapper *wrapper = 0;
RealTime adjustment = RealTime::zeroTime;
if (outputs.empty()) {
cerr << "ERROR: Plugin has no outputs!" << endl;
goto done;
}
if (outputNo < 0) {
for (size_t oi = 0; oi < outputs.size(); ++oi) {
if (outputs[oi].identifier == output) {
outputNo = oi;
break;
}
}
if (outputNo < 0) {
cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
goto done;
}
} else {
if (int(outputs.size()) <= outputNo) {
cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
goto done;
}
}
od = outputs[outputNo];
cerr << "Output is: \"" << od.identifier << "\"" << endl;
if (!plugin->initialise(channels, stepSize, blockSize)) {
cerr << "ERROR: Plugin initialise (channels = " << channels
<< ", stepSize = " << stepSize << ", blockSize = "
<< blockSize << ") failed." << endl;
goto done;
}
wrapper = dynamic_cast<PluginWrapper *>(plugin);
if (wrapper) {
// See documentation for
// PluginInputDomainAdapter::getTimestampAdjustment
PluginInputDomainAdapter *ida =
wrapper->getWrapper<PluginInputDomainAdapter>();
if (ida) adjustment = ida->getTimestampAdjustment();
}
// Here we iterate over the frames, avoiding asking the numframes in case it's streaming input.
do {
int count;
if ((blockSize==stepSize) || (currentStep==0)) {
// read a full fresh block
if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
break;
}
if (count != blockSize) --finalStepsRemaining;
} else {
// otherwise shunt the existing data down and read the remainder.
memmove(filebuf, filebuf + (stepSize * channels), overlapSize * channels * sizeof(float));
if ((count = sf_readf_float(sndfile, filebuf + (overlapSize * channels), stepSize)) < 0) {
cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
break;
}
if (count != stepSize) --finalStepsRemaining;
count += overlapSize;
}
for (int c = 0; c < channels; ++c) {
int j = 0;
while (j < count) {
plugbuf[c][j] = filebuf[j * sfinfo.channels + c];
++j;
}
while (j < blockSize) {
plugbuf[c][j] = 0.0f;
++j;
}
}
rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);
vampHost::rotorGetFeatures(RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),sfinfo.samplerate, outputNo,plugin->getRemainingFeatures(), out, progress);
if (sfinfo.frames > 0){
int pp = prog;
prog = lrintf((float(currentStep * stepSize) / sfinfo.frames) * 100.f);
if (prog != pp ) {
cerr << "\r" << progress << "%";
}
}
++currentStep;
} while (finalStepsRemaining > 0);
cerr << "\rDone" << endl;
rt = RealTime::frame2RealTime(currentStep * stepSize, sfinfo.samplerate);
vampHost::rotorGetFeatures(RealTime::realTime2Frame(rt + adjustment, sfinfo.samplerate),sfinfo.samplerate, outputNo,plugin->getRemainingFeatures(), out, progress);
returnValue = 0;
done:
delete plugin;
sf_close(sndfile);
return returnValue;
}
void vampHost::printFeatures(int frame, int sr, int output,
Plugin::FeatureSet features, ostream& out, bool useFrames)
{
if (features[output].size()) {
cout << "." << features[output].size();
}
for (unsigned int i = 0; i < features[output].size(); ++i) {
if (useFrames) {
int displayFrame = frame;
if (features[output][i].hasTimestamp) {
displayFrame = RealTime::realTime2Frame
(features[output][i].timestamp, sr);
}
out << displayFrame;
if (features[output][i].hasDuration) {
displayFrame = RealTime::realTime2Frame
(features[output][i].duration, sr);
out << "," << displayFrame;
}
out << ":";
} else {
RealTime rt = RealTime::frame2RealTime(frame, sr);
if (features[output][i].hasTimestamp) {
rt = features[output][i].timestamp;
}
out << rt.toString();
if (features[output][i].hasDuration) {
rt = features[output][i].duration;
out<< "," << rt.toString();
}
out << ":";
}
for (unsigned int j = 0; j < features[output][i].values.size(); ++j) {
out<< " " << features[output][i].values[j];
}
out << " " << features[output][i].label;
out << endl;
}
}
void vampHost::rotorGetFeatures(int frame, int sr, int output,Plugin::FeatureSet features, vector<float>& out, float& progress)
{
if (features[output].size()) {
cout << "." << features[output].size();
}
for (unsigned int i = 0; i < features[output].size(); ++i) {
int displayFrame = frame;
if (features[output][i].hasTimestamp) {
displayFrame = RealTime::realTime2Frame
(features[output][i].timestamp, sr);
}
cout << displayFrame;
cout << endl;
}
}
int vampHost::QMAnalyser::process(const string inputFile){
//vampHost::runPlugin("",settings.soname,settings.filtername, "",0, settings.inputFile, ostr,true);
//would run the plugin, outputting progress to cerr and the data to ostr
//
//int runPlugin(string myname, string soname, string id, string output,int outputNo, string inputFile, ostream& out, bool frames);
//we want to run a specific plugin, outputting progress to a mutex-protected passed variable
//and ultimately passing the data back as a string?
//or capture it as an array of floats?
//get the progress as a float
//how to handle errors?
//debugger fucking up! program stalls after 1 request in debug!?
string soname="qm-vamp-plugins";
string id="qm-tempotracker";
string myname="";
string output="";
int outputNo=0;
vampHost::rotorRunPlugin(soname,id,output,outputNo,inputFile,beats,progress);
}
void vampHost::getTimestamps(int output,Plugin::FeatureSet features, vector<float>& out){
/*
vamp-simple-host qm-vamp-plugins:qm-tempotracker 01.wav
0.046439908: 156.61 bpm
0.429569160: 156.61 bpm
0.812698412: 161.50 bpm
1.184217686: 152.00 bpm
vamp-simple-host qm-vamp-plugins:qm-segmenter 01.wav
0.000000000: 4 4
23.800000000: 6 6
44.600000000: 5 5
55.000000000: 7 7
72.800000000: 1 1
90.600000000: 2 2
109.200000000: 5 5
116.000000000: 3 3
143.800000000: 5 5
153.400000000: 3 3
163.000000000: 8 8
seems to be FP seconds then another metric
for now we can just take the first part
features[output][i].timestamp is of type RealTime: represents time values to nanosecond precision
int sec;
int nsec;
1 sec = 10^9 nanosec
actually maybe this would be the way to go for rotor- avoiding rounding errors etc
for now - ideally will get a float representation
features[output][i].values is a vector of floats + a description
WE DON'T CARE ABOUT ANYTHING <.01 seconds
static long realTime2Frame(const RealTime &r, unsigned int sampleRate);
get a vector of floats out, using frames, presuming data has a timestamp
this is crashing with "Aborted (core dumped)"
if we check for timestamp
*/
cout << "." << features[output].size();
//if (!features[output][0].hasTimestamp) {
// cerr << output << " channel, getTimestamps: error, featureset doesn't support timestamp" << endl;
//}_
//else {
for (unsigned int i = 0; i < features[output].size(); ++i) {
out.push_back( ((float)RealTime::realTime2Frame(features[output][i].timestamp, 1000))*.001f);
cout << "feature found.\n";
}
//}
}
float vampHost::QMAnalyser::get_progress(){
float p;
mutex.lock();
p=progress;
mutex.unlock();
return p;
}
bool vampHost::Analyser::init(const string &soname,const string &id,const int &_channels,const int &_bits,const int &_samples,const int &_rate,int _outputNo,const map<string,float> ¶ms){
//stuff that only happens once
channels =_channels;
samples=_samples;
rate=_rate;
bits=_bits;
outputNo=_outputNo;
//output=_output;
//http://www.mega-nerd.com/libsndfile/api.html#note1
//libsndfile returns -1..1 for fp data
bytes=(bits>>3);
stride=channels*bytes;
scale=(1.0f/pow(2.0f,bits));
features[0.0f]=0;
loader = PluginLoader::getInstance();
key = loader->composePluginKey(soname, id);
plugin = loader->loadPlugin(key, _rate, PluginLoader::ADAPT_ALL_SAFE);
if (!plugin) {
cerr << ": ERROR: Failed to load plugin \"" << id
<< "\" from library \"" << soname << "\"" << endl;
return false;
}
cerr << "Running plugin: \"" << plugin->getIdentifier() << "\"..." << endl;
blockSize = plugin->getPreferredBlockSize();
stepSize = plugin->getPreferredStepSize();
if (blockSize == 0) {
blockSize = 1024;
}
if (stepSize == 0) {
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
stepSize = blockSize/2;
} else {
stepSize = blockSize;
}
}
else if (stepSize > blockSize) {
cerr << "WARNING: stepSize " << stepSize << " > blockSize " << blockSize << ", resetting blockSize to ";
if (plugin->getInputDomain() == Plugin::FrequencyDomain) {
blockSize = stepSize * 2;
} else {
blockSize = stepSize;
}
cerr << blockSize << endl;
}
overlapSize = blockSize - stepSize;
currentStep = 0;
finalStepsRemaining = max(1, (blockSize / stepSize) - 1); // at end of file, this many part-silent frames needed after we hit EOF
plugbuf = new float*[channels];
for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize + 2];
cerr << "Using block size = " << blockSize << ", step size = "
<< stepSize << endl;
// The channel queries here are for informational purposes only --
// a PluginChannelAdapter is being used automatically behind the
// scenes, and it will take case of any channel mismatch
int minch = plugin->getMinChannelCount();
int maxch = plugin->getMaxChannelCount();
cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cerr << "Sound file has " << channels << " (will mix/augment if necessary)" << endl;
Plugin::OutputList outputs = plugin->getOutputDescriptors();
Plugin::OutputDescriptor od;
int returnValue = 1;
int prog = 0;
RealTime rt;
PluginWrapper *wrapper = 0;
RealTime adjustment = RealTime::zeroTime;
if (outputs.empty()) {
cerr << "ERROR: Plugin has no outputs!" << endl;
return false;
}
if (outputNo < 0) {
for (size_t oi = 0; oi < outputs.size(); ++oi) {
if (outputs[oi].identifier == output) {
outputNo = oi;
break;
}
}
if (outputNo < 0) {
cerr << "ERROR: Non-existent output \"" << output << "\" requested" << endl;
return false;
}
}
else {
if (int(outputs.size()) <= outputNo) {
cerr << "ERROR: Output " << outputNo << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
return false;
}
}
od = outputs[outputNo];
cerr << "Output number "<<outputNo<<": \"" << od.identifier << "\"" << endl;
for (auto i:params){
plugin->setParameter(i.first,i.second);
cerr << "Set plugin parameter: "<<i.first<<" : "<<i.second<<endl;
}
if (!plugin->initialise(channels, stepSize, blockSize)) {
cerr << "ERROR: Plugin initialise (channels = " << channels
<< ", stepSize = " << stepSize << ", blockSize = "
<< blockSize << ") failed." << endl;
return false;
}
wrapper = dynamic_cast<PluginWrapper *>(plugin);
if (wrapper) {
// See documentation for
// PluginInputDomainAdapter::getTimestampAdjustment
PluginInputDomainAdapter *ida =wrapper->getWrapper<PluginInputDomainAdapter>();
if (ida) adjustment = ida->getTimestampAdjustment();
}
//everything is prepared to start consuming data in blocks
in_block=0;
blocks_processed=0;
currentStep=0;
featureNo=1;
return true;
}
void vampHost::Analyser::process_frame(uint8_t *data,int samples_in_frame){
int sample=0;
uint16_t *_data=(uint16_t*)data;
//process the whole frame which may be f>1<f blocks
//when the frame is finished leave the partial block for the next frame
while(sample<samples_in_frame) {
while(sample<samples_in_frame&&in_block<blockSize) {
for (int i=0;i<channels;i++) {
//unsigned int this_val=0;
// this_val+=data[(sample*stride)+(i*bytes)+j]<<((1-j)*8);
//}
//plugbuf[i][in_block]=((float)((int16_t)this_val))*scale;
plugbuf[i][in_block]=((float)_data[sample])*scale;
}
in_block++;
sample++;
}
if (in_block==blockSize) {
//block is ready to be processed
//cerr<<plugin->getIdentifier()<<" processed block "<<blocks_processed<<endl;
//I /think/ that the vamp plugin keeps processing through the plugbuf until it encounters 0s
rt = RealTime::frame2RealTime(currentStep * stepSize, rate); //48000); //setting different rate doesn't affect it
Plugin::FeatureSet feat=plugin->process(plugbuf, rt);
for (unsigned int i = 0; i < feat[outputNo].size(); ++i) {
features[((float)feat[outputNo][i].timestamp.sec)+(((float)feat[outputNo][i].timestamp.nsec)*.000000001)]=featureNo;
featureNo++;
}
//shunt it down
for (int i=0;i<blockSize-stepSize;i++){
for (int j=0;j<channels;j++){
plugbuf[j][i]=plugbuf[j][i+stepSize];
}
}
in_block-=stepSize;
currentStep++;
}
}
}
void vampHost::Analyser::cleanup(){
//process final block
while(in_block<blockSize) {
for (int i=0;i<channels;i++) {
plugbuf[i][in_block]=0.0f;
}
in_block++;
}
rt = RealTime::frame2RealTime(currentStep * stepSize, rate); // //setting different
Plugin::FeatureSet feat=plugin->process(plugbuf, rt);
for (unsigned int i = 0; i < feat[outputNo].size(); ++i) {
features[((float)feat[outputNo][i].timestamp.sec)+(((float)feat[outputNo][i].timestamp.nsec)*.000000001)]=featureNo;
featureNo++;
}
feat=plugin->getRemainingFeatures();
for (unsigned int i = 0; i < feat[outputNo].size(); ++i) {
features[((float)feat[outputNo][i].timestamp.sec)+(((float)feat[outputNo][i].timestamp.nsec)*.000000001)]=featureNo;
featureNo++;
}
//cerr<<plugin->getIdentifier()<<" found "<<(features.size()-1)<<" features"<<endl;
//deal with left over data?
for (int c = 0; c < channels; ++c) {
delete[] plugbuf[c];
}
delete[] plugbuf;
delete plugin;
}
|