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