Remove unnecessary 0 checks before delete; see http://www.parashift.com/c++-faq-lite...
[ardour.git] / libs / ardour / transient_detector.cc
1 #include <ardour/transient_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 TransientDetector::_op_id = X_("libardourvampplugins:percussiononsets:2");
12
13 TransientDetector::TransientDetector (float sr)
14         : AudioAnalyser (sr, X_("libardourvampplugins:percussiononsets"))
15 {
16         /* update the op_id */
17
18         _op_id = X_("libardourvampplugins:percussiononsets");
19         
20         // XXX this should load the above-named plugin and get the current version
21         
22         _op_id += ":2";
23 }
24
25 TransientDetector::~TransientDetector()
26 {
27 }
28
29 string
30 TransientDetector::operational_identifier()
31 {
32         return _op_id;
33 }
34
35 int
36 TransientDetector::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 TransientDetector::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 TransientDetector::set_threshold (float val)
67 {
68         if (plugin) {
69                 plugin->setParameter ("threshold", val);
70         }
71 }
72
73 void
74 TransientDetector::set_sensitivity (float val)
75 {
76         if (plugin) {
77                 plugin->setParameter ("sensitivity", val);
78         }
79 }
80
81 void
82 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
83 {
84         if (t.empty()) {
85                 return;
86         }
87
88         t.sort ();
89         
90         /* remove duplicates or other things that are too close */
91         
92         AnalysisFeatureList::iterator i = t.begin();
93         AnalysisFeatureList::iterator f, b;
94         const nframes64_t gap_frames = (nframes64_t) floor (gap_msecs * (sr / 1000.0));
95         
96         while (i != t.end()) {
97
98                 // move front iterator to just past i, and back iterator the same place
99                 
100                 f = i;
101                 ++f;
102                 b = f;
103
104                 // move f until we find a new value that is far enough away
105                 
106                 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
107                         ++f;
108                 }
109
110                 i = f;
111
112                 // if f moved forward from b, we had duplicates/too-close points: get rid of them
113
114                 if (b != f) {
115                         t.erase (b, f);
116                 }
117         }
118 }