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