-
Notifications
You must be signed in to change notification settings - Fork 98
/
tempo.cpp
4353 lines (3321 loc) · 157 KB
/
tempo.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
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright (C) 2000-2010 George Tzanetakis <gtzan@cs.uvic.ca>
**
** 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.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <marsyas/common_source.h>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <marsyas/FileName.h>
#include <marsyas/Collection.h>
#include <marsyas/system/MarSystemManager.h>
#include <marsyas/CommandLineOptions.h>
#include <marsyas/marsystems/Esitar.h>
#include <marsyas/mididevices.h>
#include <string>
#include <string.h> // This file also uses C-style string functions like strcmp().
#include <fstream>
#include <iostream>
#include <iomanip>
#ifdef MARSYAS_PNG
#include "pngwriter.h"
#endif
#ifdef MARSYAS_WIN32
#pragma warning(disable: 4251)
#endif
#define BPM_HYPOTHESES 6 //Nr. of initial BPM hypotheses (must be <= than the nr. of agents) (6)
#define PHASE_HYPOTHESES 30//Nr. of phases per BPM hypothesis (30)
#define MIN_BPM 40 //minimum tempo considered, in BPMs (50)
#define MAX_BPM 180 //maximum tempo considered, in BPMs (250)
#define NR_AGENTS 30 //Nr. of agents in the pool (30)
#define LFT_OUTTER_MARGIN 0.20 //The size of the outer half-window (in % of the IBI) before the predicted beat time (0.20)
#define RGT_OUTTER_MARGIN 0.40 //The size of the outer half-window (in % of the IBI) after the predicted beat time (0.30)
#define INNER_MARGIN 4.0 //Inner tolerance window margin size (= half inner window size -> in ticks) (3.0)
#define OBSOLETE_FACTOR 0.8 //An agent is killed if, at any time (after the initial Xsecs-defined in BeatReferee), the difference between its score and the bestScore is below OBSOLETE_FACTOR * bestScore (0.8)
#define LOST_FACTOR 8 //An agent is killed if it become lost, i.e. if it found LOST_FACTOR consecutive beat predictions outside its inner tolerance window (8)
#define CHILDREN_SCORE_FACTOR 0.8 //(Inertia1) Each created agent imports its father score multiplied (or divided if negative) by this factor (0.8)
#define BEST_FACTOR 1.0 //(Inertia2) Mutiple of the bestScore an agent's score must have for replacing the current best agent (1.0)
#define CORRECTION_FACTOR 0.25 //correction factor for compensating each agents' own {phase, period} hypothesis errors (0.25)
#define EQ_PERIOD 1 //Period threshold which identifies two agents as predicting the same period (IBI, in ticks) (1)
#define EQ_PHASE 2 //Phase threshold which identifies two agents as predicting the same phase (phase, in ticks) (2)
#define CHILD1_FACTOR 1.0 //correction factor (error proportion-[0.0-1.0]) for compensating its father's {phase, period} hypothesis - used by child1 (2.0 - only full phase adjustment; -1 - no child considered) (1.0)
#define CHILD2_FACTOR 2.0 //correction factor (error proportion-[0.0-1.0]) for compensating its father's {phase, period} hypothesis - used by child2 (2.0 - only full phase adjustment; -1 - no child considered) (2.0)
#define CHILD3_FACTOR 0.5 //correction factor (error proportion-[0.0-1.0]) for compensating its father's {phase, period} hypothesis - used by child3 (2.0 - only full phase adjustment; -1 - no child considered) (0.5)
#define TRIGGER_BEST_FACTOR 1.0 //Proportion of the current best agent score which is inherited by the agents created at each triggered induction [shouldn't be much higher than 1, for not inflating scores two much] (1.0)
#define SUPERVISED_TRIGGER_THRES 0.03 //Degree (in percentage) of mean bestScore decrease to trigger a new induction in supervised induction mode (0.03)
#define BEAT_TRANSITION_TOL 0.6 //Tolerance for handling beats at transitions between agents [-1 for unconsider it]: (0.6)
//In causal mode, if between two consecutive beats there is over a BEAT_TRANSITION_TOL decrease in current IBI the second beat is unconsidered;
//In non-causal mode, if between a son's first beat and its father's last there is over a BEAT_TRANSITION_TOL descrease on the father last IBI the son's first beat is unconsidered;
//In non-causal mode, if between a son's first beat and its father's last there is over a BEAT_TRANSITION_TOL increase on the father last IBI the son's first beat shall be its father's next beat, and the second beat shall be its assigned first.
// 0: no doubling at all
// 1: single threshold (bpm > x => double)
// 2: SVM-based doubling
#define POST_DOUBLING 2
#define WRITE_INTERMEDIATE 0
#define DISPLAY_SVM 0
#define WINSIZE 1024 //(2048?)
#define HOPSIZE 512 //(512)
using namespace std;
using namespace Marsyas;
MarSystemManager mng;
vector<mrs_string> predicted_filenames_;
vector<float> predicted_filenames_tempos_;
vector<mrs_string> wrong_filenames_;
vector<float> wrong_filenames_tempos_;
vector<float> wrong_ground_truth_tempos_;
vector<float> ground_truth_tempos_;
mrs_string output;
mrs_bool audiofileopt;
mrs_real toleranceopt_;
mrs_real induction_time;
mrs_real metrical_change_time;
mrs_string groundtruth_induction = "-1";
mrs_string induction_mode = "-1";
mrs_string groundtruth_file = "-1";
mrs_natural triggergt_tol = 5;
mrs_string score_function;
mrs_bool audioopt;
mrs_bool backtraceopt;
mrs_real phase_;
mrs_realvec errors_;
mrs_string fileName;
mrs_string pluginName = EMPTYSTRING;
mrs_string methodopt;
mrs_string predictedopt_;
mrs_string predictedOutopt_;
mrs_string wrong_filename_opt_;
mrs_natural minBPM_;
mrs_natural maxBPM_;
mrs_real sup_thres = SUPERVISED_TRIGGER_THRES;
mrs_bool avoid_metrical_changes = false;
CommandLineOptions cmd_options;
bool beatsopt_;
int helpopt;
int usageopt;
long offset = 0;
long duration = 1000 * 44100;
long band;
mrs_natural bandopt = 0;
float start = 0.0f;
float length = 1000.0f;
float gain = 1.0f;
float repetitions = 1;
int correct_predictions;
int correct_harmonic_predictions;
int correct_harmonic_mirex_predictions;
int correct_mirex_predictions;
int total_instances;
int total_errors;
float total_differences;
mrs_real
next_power_two(mrs_real x)
{
return pow(2, ceil( log(x) / log( (mrs_real) 2.0)));
}
int
printUsage(mrs_string progName)
{
MRSDIAG("tempo.cpp - printUsage");
cerr << "Usage : " << progName << " [-m method] [-g gain] [-o offset(samples)] [-d duration(samples)] [-s start(seconds)] [-l length(seconds)] [-f outputfile] [-p pluginName] [-r repetitions] file1 file2 file3" << endl;
cerr << endl;
cerr << "where file1, ..., fileN are sound files in a MARSYAS supported format or collections " << endl;
return 1;
}
int
printHelp(mrs_string progName)
{
MRSDIAG("tempo.cpp - printHelp");
cerr << "tempo, MARSYAS, Copyright George Tzanetakis " << endl;
cerr << "--------------------------------------------" << endl;
cerr << "Estimates the tempo for the sound files provided as arguments " << endl;
cerr << endl;
cerr << "Usage : " << progName << "file1 file2 file3" << endl;
cerr << endl;
cerr << "where file1, ..., fileN are sound files in a Marsyas supported format" << endl;
cerr << "Help Options:" << endl;
cerr << "-u --usage : display short usage info" << endl;
cerr << "-h --help : display this information " << endl;
cerr << "-v --verbose : verbose output " << endl;
cerr << "-f --filename : output to file " << endl;
cerr << "-m --method : tempo induction method " << endl;
cerr << "-b --band : set band" << endl;
cerr << "-g --gain : linear volume gain " << endl;
cerr << "-o --offset : playback start offset in samples " << endl;
cerr << "-d --duration : playback duration in samples " << endl;
cerr << "-s --start : playback start offset in seconds " << endl;
cerr << "-l --length : playback length in seconds " << endl;
cerr << "-p --plugin : output plugin name " << endl;
cerr << "-r --repetitions: number of repetitions " << endl;
cerr << "-b --beats : output beat locations " << endl;
cerr << "Available methods: " << endl;
cerr << "MEDIAN_SUMBANDS" << endl;
cerr << "MEDIAN_MULTIBANDS" << endl;
cerr << "HISTO_SUMBANDS" << endl;
cerr << "HISTO_SUMBANDSQ" << endl;
cerr << "STEM" << endl;
cerr << "TEST_OSS_FLUX" << endl;
cerr << "AIM_FLUX" << endl;
cerr << "AIM" << endl;
cerr << "PREDICTED" << endl;
cerr << "BOOMCHICK_WAVELET" << endl;
cerr << "BOOMCHICK_FILTER" << endl;
return 1;
}
void
evaluate_estimated_tempo(mrs_string sfName, mrs_realvec tempos, float ground_truth_tempo, mrs_real tolerance)
{
mrs_real predicted_tempo = tempos(0);
float diff1 = fabs(predicted_tempo - ground_truth_tempo);
float diff2 = fabs(2 * predicted_tempo - ground_truth_tempo);
float diff3 = fabs(0.5 * predicted_tempo - ground_truth_tempo);
float diff4 = fabs(3 * predicted_tempo - ground_truth_tempo);
float diff5 = fabs(0.33 * predicted_tempo - ground_truth_tempo);
// float diff1 = fabs(predicted_tempo - ground_truth_tempo);
// float diff2 = fabs(predicted_tempo - 2 * ground_truth_tempo);
// float diff3 = fabs(predicted_tempo - 0.5 * ground_truth_tempo);
// float diff4 = fabs(predicted_tempo - 3 * ground_truth_tempo);
// float diff5 = fabs(predicted_tempo - 0.33 * ground_truth_tempo);
cout << sfName << "\t" << predicted_tempo << ":" << ground_truth_tempo << "---" << diff1 << ":" << diff2 << ":" << diff3 << ":" << diff4 << ":" << diff5 << endl;
cout << sfName << "\t" << predicted_tempo << endl;
//mrs_real accPerc = 4.0;
if (diff1 < 0.5)
correct_predictions++;
// if ((predicted_tempo >= ground_truth_tempo * (1 - accPerc / 100.0))
// &&(predicted_tempo <= ground_truth_tempo * (1 + accPerc/ 100.0)))
if (diff1 <= tolerance * ground_truth_tempo)
correct_mirex_predictions++;
if (diff2 <= tolerance * ground_truth_tempo)
errors_(0) = errors_(0) + 1;
if (diff3 <= tolerance * ground_truth_tempo)
{
errors_(1) = errors_(1) + 1;
}
if (diff4 <= tolerance * ground_truth_tempo)
errors_(2) = errors_(2) + 1;
if (diff5 <= tolerance * ground_truth_tempo)
errors_(3) = errors_(3) + 1;
if ((diff1 <= tolerance * ground_truth_tempo)||(diff2 <= tolerance * ground_truth_tempo)||(diff3 <= tolerance * ground_truth_tempo)||(diff4 <= tolerance * ground_truth_tempo)||(diff5 <= tolerance * ground_truth_tempo))
{
correct_harmonic_mirex_predictions++;
}
else
{
wrong_filenames_.push_back(sfName);
wrong_filenames_tempos_.push_back(predicted_tempo);
wrong_ground_truth_tempos_.push_back(ground_truth_tempo);
}
predicted_filenames_.push_back(sfName);
predicted_filenames_tempos_.push_back(predicted_tempo);
ground_truth_tempos_.push_back(ground_truth_tempo);
if ((diff1 < 0.5)||(diff2 < 0.5)||(diff3 < 0.5)||(diff4 < 0.5)||(diff5 < 0.5))
{
correct_harmonic_predictions++;
if ((diff1 < diff2)&&(diff1 < diff3))
total_differences += diff1;
if ((diff2 < diff3)&&(diff1 < diff1))
total_differences += diff2;
if ((diff3 < diff2)&&(diff3 < diff1))
total_differences += diff3;
total_errors++;
}
else
{
}
total_instances++;
cout << "Correct Predictions = " << correct_predictions << "/" << total_instances << " - " << correct_predictions * 1.0 / total_instances * 100.0 << endl;
cout << "Correct MIREX Predictions = " << correct_mirex_predictions << "/" << total_instances << " - " << correct_mirex_predictions * 1.0 / total_instances * 100.0 << endl;
cout << "Correct Harmonic Predictions = " << correct_harmonic_predictions << "/" << total_instances << " - " << correct_harmonic_predictions * 1.0 / total_instances * 100.0 << endl;
cout << "Correct Harmonic MIREX predictions = " << correct_harmonic_mirex_predictions << "/" << total_instances << " - " << correct_harmonic_mirex_predictions * 1.0 / total_instances * 100.0 << endl;
cout << "Average error difference = " << total_differences << "/" << total_errors << "=" << total_differences / total_errors << endl;
cout << "# Half predicted tempos = " << errors_(0) * 1.0 / total_instances * 100.0 << " - " << errors_(0) << endl;;
cout << "# Double predicted tempos = " << errors_(1) * 1.0 / total_instances * 100.0 << " - " << errors_(1) << endl;;
cout << "# triple predicted tempos = " << errors_(2) * 1.0 / total_instances * 100.0 << endl;;
cout << "# third predicted tempos = " << errors_(3) * 1.0 / total_instances * 100.0 << endl;;
}
// Play soundfile given by sfName, msys contains the playback
// network of MarSystem objects
void tempo_medianMultiBands(mrs_string sfName, float ground_truth_tempo, mrs_string resName, bool haveCollections, mrs_real tolerance)
{
(void) resName;
(void) haveCollections;
// prepare network
MarSystem *total = mng.create("Series", "src");
total->addMarSystem(mng.create("Stereo2Mono", "s2m"));
total->addMarSystem(mng.create("SoundFileSource", "src"));
total->addMarSystem(mng.create("ShiftInput", "si"));
total->addMarSystem(mng.create("WaveletPyramid", "wvpt"));
total->addMarSystem(mng.create("WaveletBands", "wvbnds"));
total->addMarSystem(mng.create("FullWaveRectifier", "fwr"));
total->addMarSystem(mng.create("OnePole", "lpf"));
total->addMarSystem(mng.create("Norm", "norm"));
// total->addMarSystem(mng.create("Sum", "sum"));
total->addMarSystem(mng.create("DownSampler", "ds"));
total->addMarSystem(mng.create("Windowing", "autowin"));
total->updControl("Windowing/autowin/mrs_string/type", "Hanning");
total->addMarSystem(mng.create("AutoCorrelation", "acr"));
total->addMarSystem(mng.create("Peaker", "pkr"));
total->addMarSystem(mng.create("MaxArgMax", "mxr"));
total->addMarSystem(mng.create("PeakPeriods2BPM", "p2bpm"));
total->updControl("SoundFileSource/src/mrs_string/filename", sfName);
// update the controls
mrs_real srate = total->getctrl("SoundFileSource/src/mrs_real/israte")->to<mrs_real>();
// input filename with hopSize/winSize
mrs_natural winSize = (mrs_natural)(srate / 22050.0) * 65536;
mrs_natural hopSize = winSize / 16;
total->updControl("mrs_natural/inSamples", hopSize);
total->updControl("SoundFileSource/src/mrs_natural/pos", offset);
total->updControl("ShiftInput/si/mrs_natural/winSize", winSize);
// wavelet filterbank envelope extraction controls
total->updControl("WaveletPyramid/wvpt/mrs_bool/forward", true);
total->updControl("OnePole/lpf/mrs_real/alpha", 0.99f);
mrs_natural factor = 32;
total->updControl("DownSampler/ds/mrs_natural/factor", factor);
// Peak picker 4BPMs at 60BPM resolution from 50 BPM to 250 BPM
mrs_natural pkinS = total->getctrl("Peaker/pkr/mrs_natural/onSamples")->to<mrs_natural>();
mrs_real peakSpacing = ((mrs_natural)(srate * 60.0 / (factor *60.0)) -
(mrs_natural)(srate * 60.0 / (factor*80.0))) / pkinS;
mrs_natural peakStart = (mrs_natural)(srate * 60.0 / (factor * 180.0));
mrs_natural peakEnd = (mrs_natural)(srate * 60.0 / (factor * 40.0));
total->updControl("Peaker/pkr/mrs_real/peakSpacing", peakSpacing);
total->updControl("Peaker/pkr/mrs_real/peakStrength", 0.75);
total->updControl("Peaker/pkr/mrs_natural/peakStart", peakStart);
total->updControl("Peaker/pkr/mrs_natural/peakEnd", peakEnd);
// prepare vectors for processing
realvec iwin(total->getctrl("mrs_natural/inObservations")->to<mrs_natural>(),
total->getctrl("mrs_natural/inSamples")->to<mrs_natural>());
realvec estimate(total->getctrl("mrs_natural/onObservations")->to<mrs_natural>(),
total->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
mrs_natural bin;
mrs_natural onSamples, nChannels;
int numPlayed =0;
mrs_natural wc=0;
mrs_natural samplesPlayed = 0;
//mrs_natural repeatId = 1;
// vector of bpm estimate used to calculate median
vector<int> bpms;
onSamples = total->getctrl("ShiftInput/si/mrs_natural/onSamples")->to<mrs_natural>();
nChannels = total->getctrl("SoundFileSource/src/mrs_natural/onObservations")->to<mrs_natural>();
total->updControl("MaxArgMax/mxr/mrs_natural/nMaximums", 4);
total->updControl("MaxArgMax/mxr/mrs_natural/interpolation", 1);
total->updControl("Peaker/pkr/mrs_natural/interpolation", 1);
// playback offset & duration
offset = (mrs_natural) (start * srate * nChannels);
duration = (mrs_natural) (length * srate * nChannels);
while (total->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
{
total->process(iwin, estimate);
// convert highest peak to BPMs and add to vector
for (int b=0; b < 4; b++)
{
// pitch = srate * 60.0 / (estimate(b,1) * factor);
// bin = (mrs_natural) (pitch);
bin = (mrs_natural)(estimate(b,1));
// cout << "max bpm(" << b << ") = " << bin << endl;
bpms.push_back(bin);
}
numPlayed++;
wc ++;
samplesPlayed += onSamples;
}
// sort bpm estimates for median filtering
sort(bpms.begin(), bpms.end());
float predicted_tempo;
predicted_tempo = bpms[bpms.size()/2];
evaluate_estimated_tempo(sfName, realvec(1,1,predicted_tempo),
ground_truth_tempo, tolerance);
delete total;
}
void
tempo_wavelets(mrs_string sfName, mrs_string resName, bool haveCollections, mrs_real tolerance)
{
(void) resName;
(void) haveCollections;
(void) tolerance;
//MarSystemManager mng;
mrs_real srate = 0.0;
// prepare network
MarSystem *total = mng.create("Series", "src");
total->addMarSystem(mng.create("SoundFileSource", "src"));
total->addMarSystem(mng.create("Stereo2Mono", "s2m"));
total->addMarSystem(mng.create("ShiftInput", "si"));
total->addMarSystem(mng.create("DownSampler", "initds"));
total->addMarSystem(mng.create("WaveletPyramid", "wvpt"));
// implicit fanout
total->addMarSystem(mng.create("WaveletBands", "wvbnds"));
total->addMarSystem(mng.create("FullWaveRectifier", "fwr"));
total->addMarSystem(mng.create("OnePole", "lpf"));
total->addMarSystem(mng.create("Norm", "norm"));
{
// Extra gain added for compensating the cleanup of the Norm Marsystem,
// which used a 0.05 internal gain for some unknown reason.
// \todo is this weird gain factor actually required?
total->addMarSystem(mng.create("Gain", "normGain"));
total->updControl("Gain/normGain/mrs_real/gain", 0.05);
}
// implicit fanin
total->addMarSystem(mng.create("Sum", "sum"));
total->addMarSystem(mng.create("DownSampler", "ds"));
total->addMarSystem(mng.create("AutoCorrelation", "acr"));
// total->addMarSystem(mng.create("PlotSink", "psink1"));
total->addMarSystem(mng.create("Peaker", "pkr"));
// total->addMarSystem(mng.create("PlotSink", "psink2"));
total->addMarSystem(mng.create("MaxArgMax", "mxr"));
total->addMarSystem(mng.create("PeakPeriods2BPM", "p2bpm"));
total->addMarSystem(mng.create("BeatHistogramFromPeaks", "histo"));
// total->addMarSystem(mng.create("Peaker", "pkr1"));
// total->addMarSystem(mng.create("PlotSink", "psink3"));
// total->addMarSystem(mng.create("Reassign", "reassign"));
// total->addMarSystem(mng.create("PlotSink", "psink4"));
total->addMarSystem(mng.create("HarmonicEnhancer", "harm"));
// total->addMarSystem(mng.create("HarmonicEnhancer", "harm"));
// total->addMarSystem(mng.create("PlotSink", "psink4"));
// total->addMarSystem(mng.create("MaxArgMax", "mxr1"));
mrs_natural ifactor = 8;
total->updControl("DownSampler/initds/mrs_natural/factor", ifactor);
total->updControl("SoundFileSource/src/mrs_string/filename", sfName);
srate = total->getctrl("SoundFileSource/src/mrs_real/osrate")->to<mrs_real>();
// srate = total->getctrl("DownSampler/initds/mrs_real/osrate")->to<mrs_real>();
// cout << "srate = " << srate << endl;
// update the controls
// input filename with hopSize/winSize
mrs_natural winSize = (mrs_natural)((srate / 22050.0) * 2 * 65536);
mrs_natural hopSize = winSize / 16;
// cout << "winSize = " << winSize << endl;
// cout << "hopSize = " << hopSize << endl;
offset = (mrs_natural) (start * srate);
duration = (mrs_natural) (length * srate);
// total->updControl("PlotSink/psink1/mrs_string/filename", "acr");
// total->updControl("PlotSink/psink2/mrs_string/filename", "peaks");
// total->updControl("PlotSink/psink3/mrs_string/filename", "histo");
// total->updControl("PlotSink/psink4/mrs_string/filename", "rhisto");
total->updControl("mrs_natural/inSamples", hopSize);
total->updControl("SoundFileSource/src/mrs_natural/pos", offset);
total->updControl("MaxArgMax/mxr/mrs_natural/nMaximums", 5);
// total->updControl("MaxArgMax/mxr1/mrs_natural/nMaximums", 2);
total->updControl("ShiftInput/si/mrs_natural/winSize", winSize);
// wavelet filterbank envelope extraction controls
total->updControl("WaveletPyramid/wvpt/mrs_bool/forward", true);
total->updControl("OnePole/lpf/mrs_real/alpha", 0.99f);
mrs_natural factor = 32;
total->updControl("DownSampler/ds/mrs_natural/factor", factor);
srate = total->getctrl("DownSampler/initds/mrs_real/osrate")->to<mrs_real>();
// Peak picker 4BPMs at 60BPM resolution from 50 BPM to 250 BPM
mrs_natural pkinS = total->getctrl("Peaker/pkr/mrs_natural/onSamples")->to<mrs_natural>();
mrs_real peakSpacing = ((mrs_natural)(srate * 60.0 / (factor *60.0)) -
(mrs_natural)(srate * 60.0 / (factor*62.0))) / (pkinS * 1.0);
mrs_natural peakStart = (mrs_natural)(srate * 60.0 / (factor * 230.0));
mrs_natural peakEnd = (mrs_natural)(srate * 60.0 / (factor * 30.0));
total->updControl("Peaker/pkr/mrs_real/peakSpacing", peakSpacing);
total->updControl("Peaker/pkr/mrs_real/peakStrength", 0.5);
total->updControl("Peaker/pkr/mrs_natural/peakStart", peakStart);
total->updControl("Peaker/pkr/mrs_natural/peakEnd", peakEnd);
total->updControl("Peaker/pkr/mrs_real/peakGain", 2.0);
/* total->updControl("Peaker/pkr1/mrs_real/peakSpacing", 0.1);
total->updControl("Peaker/pkr1/mrs_real/peakStrength", 1.2);
total->updControl("Peaker/pkr1/mrs_natural/peakStart", 60);
total->updControl("Peaker/pkr1/mrs_natural/peakEnd", 180);
*/
total->updControl("BeatHistogramFromPeaks/histo/mrs_natural/startBin", 0);
total->updControl("BeatHistogramFromPeaks/histo/mrs_natural/endBin", 230);
// prepare vectors for processing
realvec iwin(total->getctrl("mrs_natural/inObservations")->to<mrs_natural>(),
total->getctrl("mrs_natural/inSamples")->to<mrs_natural>());
realvec estimate(total->getctrl("mrs_natural/onObservations")->to<mrs_natural>(),
total->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
// mrs_natural bin;
mrs_natural onSamples;
int numPlayed =0;
mrs_natural wc=0;
mrs_natural samplesPlayed = 0;
mrs_natural repeatId = 1;
onSamples = total->getctrl("ShiftInput/si/mrs_natural/onSamples")->to<mrs_natural>();
while (total->getctrl("SoundFileSource/src/mrs_bool/hasData")->to<mrs_bool>())
{
total->process(iwin, estimate);
numPlayed++;
if (samplesPlayed > repeatId * duration)
{
total->updControl("SoundFileSource/src/mrs_natural/pos", offset);
repeatId++;
}
wc ++;
samplesPlayed += onSamples;
}
// phase calculation
MarSystem *total1 = mng.create("Series", "total1");
total1->addMarSystem(mng.create("SoundFileSource", "src1"));
total1->addMarSystem(mng.create("FullWaveRectifier", "fwr1"));
// implicit fanin
total1->addMarSystem(mng.create("Sum", "sum1"));
total1->addMarSystem(mng.create("DownSampler", "ds1"));
total1->updControl("SoundFileSource/src1/mrs_string/filename", sfName);
srate = total1->getctrl("SoundFileSource/src1/mrs_real/osrate")->to<mrs_real>();
// update the controls
// input filename with hopSize/winSize
winSize = (mrs_natural)(srate / 22050.0) * 8 * 65536;
total1->updControl("mrs_natural/inSamples", winSize);
total1->updControl("SoundFileSource/src1/mrs_natural/pos", 0);
// wavelt filterbank envelope extraction controls
// total1->updControl("OnePole/lpf1/mrs_real/alpha", 0.99f);
factor = 4;
total1->updControl("DownSampler/ds1/mrs_natural/factor", factor);
realvec iwin1(total1->getctrl("mrs_natural/inObservations")->to<mrs_natural>(),
total1->getctrl("mrs_natural/inSamples")->to<mrs_natural>());
realvec estimate1(total1->getctrl("mrs_natural/onObservations")->to<mrs_natural>(),
total1->getctrl("mrs_natural/onSamples")->to<mrs_natural>());
total1->process(iwin1, estimate1);
mrs_real s1 = estimate(0);
mrs_real s2 = estimate(2);
mrs_real t1 = estimate(1);
mrs_real t2 = estimate(3);
mrs_natural p1 = (mrs_natural)((int)((srate * 60.0) / (factor * t1)+0.5));
mrs_natural p2 = (mrs_natural)((int)((srate * 60.0) / (factor * t2)+0.5));
mrs_real mx = 0.0;
mrs_natural imx = 0;
mrs_real sum = 0.0;
for (mrs_natural i = 0; i < p1; ++i)
{
sum = 0.0;
sum += estimate1(0,i);
sum += estimate1(0,i+p1);
sum += estimate1(0,i+p1-1);
sum += estimate1(0,i+p1+1);
sum += estimate1(0,i+2*p1);
sum += estimate1(0,i+2*p1-1);
sum += estimate1(0,i+2*p1+1);
sum += estimate1(0,i+3*p1);
sum += estimate1(0,i+3*p1-1);
sum += estimate1(0,i+3*p1+1);
if (sum > mx)
{
mx = sum;
imx = i;
}
}
mrs_real ph1 = (imx * factor * 1.0) / srate;
for (mrs_natural i = 0; i < p2; ++i)
{
sum = 0.0;
sum += estimate1(0,i);
sum += estimate1(0,i+p2);
sum += estimate1(0,i+p2-1);
sum += estimate1(0,i+p2+1);
sum += estimate1(0,i+2*p2);
sum += estimate1(0,i+2*p2-1);
sum += estimate1(0,i+2*p2+1);
sum += estimate1(0,i+3*p2);
sum += estimate1(0,i+3*p2-1);
sum += estimate1(0,i+3*p2+1);
if (sum > mx)
{
mx = sum;
imx = i;
}
}
mrs_real ph2 = (imx * factor * 1.0) / srate;
mrs_real st = s1 / (s1 + s2);
ofstream os(resName.c_str());
os << fixed << setprecision(1)
<< t1 << "\t"
<< t2 << "\t"
<< setprecision(2)
<< st << "\t"
<< setprecision(3)
<< ph1 << "\t"
<< ph2 << "\t"
<< endl;
cout << "Estimated tempo = " << t1 << endl;
cout << sfName << " " << t1 << " " << s1 << endl;
delete total;
delete total1;
}
void
tempo_aim(mrs_string sfName, float ground_truth_tempo, mrs_string resName, bool haveCollections, mrs_real tolerance)
{
(void) resName;
cout << "Tempo-aim" << endl;
//MarSystemManager mng;
MarSystem* net = mng.create("Series/net");
net->addMarSystem(mng.create("SoundFileSource/src"));
net->addMarSystem(mng.create("AimPZFC2/aimpzfc"));
net->addMarSystem(mng.create("AimHCL2/aimhcl2"));
net->addMarSystem(mng.create("Sum/sum"));
net->addMarSystem(mng.create("AutoCorrelation/acr"));
net->addMarSystem(mng.create("BeatHistogram/histo"));
MarSystem* hfanout = mng.create("Fanout", "hfanout");
hfanout->addMarSystem(mng.create("Gain", "id1"));
hfanout->addMarSystem(mng.create("TimeStretch", "tsc1"));
net->addMarSystem(hfanout);
net->addMarSystem(mng.create("Sum", "hsum"));
net->addMarSystem(mng.create("Peaker/pkr"));
net->addMarSystem(mng.create("MaxArgMax/mxr"));
net->updControl("SoundFileSource/src/mrs_string/filename",
sfName);
net->updControl("mrs_natural/inSamples", 16 * 4096);
net->updControl("Fanout/hfanout/TimeStretch/tsc1/mrs_real/factor", 0.5);
net->updControl("Sum/sum/mrs_string/mode","sum_samples");
net->updControl("AutoCorrelation/acr/mrs_real/magcompress", 0.5);
net->updControl("BeatHistogram/histo/mrs_natural/startBin", 0);
net->updControl("BeatHistogram/histo/mrs_natural/endBin", 200);
net->updControl("BeatHistogram/histo/mrs_bool/tempoWeighting", true);
net->updControl("Peaker/pkr/mrs_natural/peakStart", 50);
net->updControl("Peaker/pkr/mrs_natural/peakEnd", 160);
ofstream ofs2;
ofs2.open("tempo.mpl");
ofs2 << *net << endl;
ofs2.close();
for (int i=0; i < 4; i++)
{
net->tick();
}
mrs_realvec amp_tempo = net->getControl("mrs_realvec/processedData")->to<mrs_realvec>();
cout << "Tempo = " << amp_tempo(1) << endl;
mrs_real bpm_estimate = amp_tempo(1);
mrs_realvec tempos(1);
tempos(0) = bpm_estimate;
if (haveCollections)
{
evaluate_estimated_tempo(sfName, tempos, ground_truth_tempo, tolerance);
}
ofstream ofs;
ofs.open(fileName.c_str());
cout << bpm_estimate << endl;
ofs << bpm_estimate << endl;
ofs.close();
delete net;
}
MarSystem *onset_strength_signal_flux(mrs_string sfName)
{
//MarSystemManager mng;
/* Onset strength calcuates the onset strength signal whose
individual values are computed using the fluxnet. The
resulting onset strength signal is filtered to smooth out
adjacent peaks. This moves the locations of the true peaks
by a fixed number of samples
*/
MarSystem *onset_strength = mng.create("Series/onset_strength");
MarSystem *accum = mng.create("Accumulator/accum");
MarSystem *fluxnet = mng.create("Series/fluxnet");
fluxnet->addMarSystem(mng.create("SoundFileSource/src"));
fluxnet->addMarSystem(mng.create("MixToMono/m2m"));
// fluxnet->addMarSystem(mng.create("DownSampler/tds"));
fluxnet->addMarSystem(mng.create("ShiftInput/si")); // overlap for the spectral flux
fluxnet->addMarSystem(mng.create("Windowing/windowing1"));
fluxnet->addMarSystem(mng.create("Spectrum/spk"));
fluxnet->addMarSystem(mng.create("PowerSpectrum/pspk"));
//fluxnet->addMarSystem(mng.create("TriangularFilterBank/tfb"));
// fluxnet->addMarSystem(mng.create("Sum/triangsum"));
fluxnet->addMarSystem(mng.create("Flux/flux"));
//fluxnet->addMarSystem(mng.create("Gain/gain5"));
//fluxnet->updControl("Flux/flux/mrs_string/mode", "multichannel");
//fluxnet->addMarSystem(mng.create("Delta/delta"));
//fluxnet->updControl("Delta/delta/mrs_bool/positive", true);
fluxnet->addMarSystem(mng.create("Filter", "filt1"));
accum->addMarSystem(fluxnet);
onset_strength->addMarSystem(accum);
// parameters for the onset strength signal
onset_strength->updControl("Accumulator/accum/Series/fluxnet/PowerSpectrum/pspk/mrs_string/spectrumType", "logmagnitude");
// onset_strength->updControl("Accumulator/accum/Series/fluxnet/Windowing/windowing1/mrs_string/type", "Blackman-Harris");
onset_strength->updControl("Accumulator/accum/Series/fluxnet/Flux/flux/mrs_string/mode", "Laroche2003");
// The filter object in Marsyas is implemented as a direct form II
// structure. This is a canonical form which has the minimum number
// of delay elements.
// 15th order
// import scipy.signal
// b,a = scipy.signal.firwin(16, 3.0 / (344.53125/2.0))
/*
mrs_realvec bcoeffs(1, 16);
bcoeffs(0) = 0.0095530348472755;
bcoeffs(1) = 0.0144032200641954;
bcoeffs(2) = 0.0280928524023415;
bcoeffs(3) = 0.0483763511689087;
bcoeffs(4) = 0.0717941373871591;
bcoeffs(5) = 0.0942851675260588;
bcoeffs(6) = 0.1119142142273117;
bcoeffs(7) = 0.1215810223767492;
bcoeffs(8) = 0.1215810223767492;
bcoeffs(9) = 0.1119142142273117;
bcoeffs(10) = 0.0942851675260588;
bcoeffs(11) = 0.0717941373871592;
bcoeffs(12) = 0.0483763511689087;
bcoeffs(13) = 0.0280928524023415;
bcoeffs(14) = 0.0144032200641954;
bcoeffs(15) = 0.0095530348472755;
*/
/*
// b,a = scipy.signal.firwin(16, 10.0 / (344.53125/2.0))
mrs_realvec bcoeffs(1, 16);
bcoeffs(0) = 0.0073773298534980;
bcoeffs(1) = 0.0120567511070207;
bcoeffs(2) = 0.0251341506152936;
bcoeffs(3) = 0.0456735164211478;
bcoeffs(4) = 0.0706923687612440;
bcoeffs(5) = 0.0957577801640946;
bcoeffs(6) = 0.1160031971306242;
bcoeffs(7) = 0.1273049059470771;
bcoeffs(8) = 0.1273049059470771;
bcoeffs(9) = 0.1160031971306242;
bcoeffs(10) = 0.0957577801640946;
bcoeffs(11) = 0.0706923687612441;
bcoeffs(12) = 0.0456735164211478;
bcoeffs(13) = 0.0251341506152936;
bcoeffs(14) = 0.0120567511070207;
bcoeffs(15) = 0.0073773298534980;
*/
// b,a = scipy.signal.firwin(15, 6.0 / (344.53125/2.0))
mrs_realvec bcoeffs(1, 15);
bcoeffs(0) = 0.0096350145101721;
bcoeffs(1) = 0.0155332463596257;
bcoeffs(2) = 0.0320375094304201;
bcoeffs(3) = 0.0563964822180710;
bcoeffs(4) = 0.0839538807274150;
bcoeffs(5) = 0.1091494735026997;
bcoeffs(6) = 0.1267578095297140;
bcoeffs(7) = 0.1330731674437646;
bcoeffs(8) = 0.1267578095297140;
bcoeffs(9) = 0.1091494735026998;
bcoeffs(10) = 0.0839538807274150;
bcoeffs(11) = 0.0563964822180711;
bcoeffs(12) = 0.0320375094304201;
bcoeffs(13) = 0.0155332463596257;
bcoeffs(14) = 0.0096350145101721;
fluxnet->updControl("Filter/filt1/mrs_realvec/ncoeffs", bcoeffs);
//fluxnet->updControl("Filter/filt1/mrs_realvec/dcoeffs", acoeffs);
//onset_strength->linkControl(
// "mrs_string/filename",
// "Accumulator/accum/Series/fluxnet/SoundFileSource/src/mrs_string/filename");
onset_strength->updControl("Accumulator/accum/Series/fluxnet/SoundFileSource/src/mrs_string/filename", sfName);
onset_strength->linkControl(
"Accumulator/accum/Series/fluxnet/SoundFileSource/src/mrs_real/osrate",
"mrs_real/file_srate");
onset_strength->linkControl(
"Accumulator/accum/Series/fluxnet/SoundFileSource/src/mrs_bool/hasData",
"mrs_bool/hasData");
onset_strength->linkControl("Accumulator/accum/Series/fluxnet/ShiftInput/si/mrs_natural/winSize",
"mrs_natural/winSize");
// updated values, for variable sample rates. ms = milliseconds
// these will be rounded up to the nearest power of 2 (in samples)
mrs_real oss_hop_ms = 2.9; // for flux calculation
mrs_real oss_win_ms = 5.8; // for flux calculation