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