reinstate thread buffer debug output
[ardour.git] / libs / ardour / onset_detector.cc
1 #include <cmath>
2 #include "ardour/onset_detector.h"
3
4 #include "i18n.h"
5
6 using namespace Vamp;
7 using namespace ARDOUR;
8 using namespace std;
9
10 /* need a static initializer function for this */
11
12 string OnsetDetector::_op_id = X_("libardourvampplugins:aubioonset:2");
13
14 OnsetDetector::OnsetDetector (float sr)
15         : AudioAnalyser (sr, X_("libardourvampplugins:aubioonset"))
16         , current_results (0)
17 {
18         /* update the op_id */
19
20         _op_id = X_("libardourvampplugins:aubioonset");
21
22         // XXX this should load the above-named plugin and get the current version
23
24         _op_id += ":2";
25 }
26
27 OnsetDetector::~OnsetDetector()
28 {
29 }
30
31 string
32 OnsetDetector::operational_identifier()
33 {
34         return _op_id;
35 }
36
37 int
38 OnsetDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
39 {
40         current_results = &results;
41         int ret = analyse (path, src, channel);
42
43         current_results = 0;
44         return ret;
45 }
46
47 int
48 OnsetDetector::use_features (Plugin::FeatureSet& features, ostream* out)
49 {
50         const Plugin::FeatureList& fl (features[0]);
51
52         for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
53
54                 if ((*f).hasTimestamp) {
55
56                         if (out) {
57                                 (*out) << (*f).timestamp.toString() << endl;
58                         }
59
60                         current_results->push_back (RealTime::realTime2Frame ((*f).timestamp, (framecnt_t) floor(sample_rate)));
61                 }
62         }
63
64         return 0;
65 }
66
67 void
68 OnsetDetector::set_silence_threshold (float val)
69 {
70         if (plugin) {
71                 plugin->setParameter ("silencethreshold", val);
72         }
73 }
74
75 void
76 OnsetDetector::set_peak_threshold (float val)
77 {
78         if (plugin) {
79                 plugin->setParameter ("peakpickthreshold", val);
80         }
81 }
82
83 void
84 OnsetDetector::set_function (int val)
85 {
86         if (plugin) {
87                 plugin->setParameter ("onsettype", (float) val);
88         }
89 }
90
91 void
92 OnsetDetector::cleanup_onsets (AnalysisFeatureList& t, float sr, float gap_msecs)
93 {
94         if (t.empty()) {
95                 return;
96         }
97
98         t.sort ();
99
100         /* remove duplicates or other things that are too close */
101
102         AnalysisFeatureList::iterator i = t.begin();
103         AnalysisFeatureList::iterator f, b;
104         const framecnt_t gap_frames = (framecnt_t) floor (gap_msecs * (sr / 1000.0));
105
106         while (i != t.end()) {
107
108                 // move front iterator to just past i, and back iterator the same place
109
110                 f = i;
111                 ++f;
112                 b = f;
113
114                 // move f until we find a new value that is far enough away
115
116                 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
117                         ++f;
118                 }
119
120                 i = f;
121
122                 // if f moved forward from b, we had duplicates/too-close points: get rid of them
123
124                 if (b != f) {
125                         t.erase (b, f);
126                 }
127         }
128 }