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