Fix broken whitespace. I'd apologize for the compile times if it was my fault :D
[ardour.git] / libs / ardour / transient_detector.cc
1 #include <cmath>
2
3 #include "ardour/readable.h"
4 #include "ardour/transient_detector.h"
5
6 #include "i18n.h"
7
8 using namespace Vamp;
9 using namespace ARDOUR;
10 using namespace std;
11
12 /* need a static initializer function for this */
13
14 string TransientDetector::_op_id = X_("libardourvampplugins:qm-onsetdetector:2");
15
16 TransientDetector::TransientDetector (float sr)
17         : AudioAnalyser (sr, X_("libardourvampplugins:qm-onsetdetector"))
18 {
19         /* update the op_id */
20
21         _op_id = X_("libardourvampplugins:qm-onsetdetector");
22
23         // XXX this should load the above-named plugin and get the current version
24
25         _op_id += ":2";
26
27         threshold = 0.00;
28 }
29
30 TransientDetector::~TransientDetector()
31 {
32 }
33
34 string
35 TransientDetector::operational_identifier()
36 {
37         return _op_id;
38 }
39
40 int
41 TransientDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
42 {
43         current_results = &results;
44         int ret = analyse (path, src, channel);
45
46         current_results = 0;
47
48         return ret;
49 }
50
51 int
52 TransientDetector::use_features (Plugin::FeatureSet& features, ostream* out)
53 {
54         const Plugin::FeatureList& fl (features[0]);
55
56         for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
57
58                 if (f->hasTimestamp) {
59
60                         if (out) {
61                                 (*out) << (*f).timestamp.toString() << endl;
62                         }
63
64                         current_results->push_back (RealTime::realTime2Frame (f->timestamp, (framecnt_t) floor(sample_rate)));
65                 }
66         }
67
68         return 0;
69 }
70
71 void
72 TransientDetector::set_threshold (float val)
73 {
74         threshold = val;
75 }
76
77 void
78 TransientDetector::set_sensitivity (float val)
79 {
80         if (plugin) {
81                 plugin->selectProgram ("Percussive onsets");
82                 plugin->setParameter ("sensitivity", val);
83         }
84 }
85
86 void
87 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
88 {
89         if (t.empty()) {
90                 return;
91         }
92
93         t.sort ();
94
95         /* remove duplicates or other things that are too close */
96
97         AnalysisFeatureList::iterator i = t.begin();
98         AnalysisFeatureList::iterator f, b;
99         const framecnt_t gap_frames = (framecnt_t) floor (gap_msecs * (sr / 1000.0));
100
101         while (i != t.end()) {
102
103                 // move front iterator to just past i, and back iterator the same place
104
105                 f = i;
106                 ++f;
107                 b = f;
108
109                 // move f until we find a new value that is far enough away
110
111                 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
112                         ++f;
113                 }
114
115                 i = f;
116
117                 // if f moved forward from b, we had duplicates/too-close points: get rid of them
118
119                 if (b != f) {
120                         t.erase (b, f);
121                 }
122         }
123 }
124
125 void
126 TransientDetector::update_positions (Readable* src, uint32_t channel, AnalysisFeatureList& positions)
127 {
128         Plugin::FeatureSet features;
129
130         Sample* data = 0;
131         float* bufs[1] = { 0 };
132
133         int buff_size = 1024;
134         int step_size = 64;
135
136         data = new Sample[buff_size];
137         bufs[0] = data;
138
139         AnalysisFeatureList::iterator i = positions.begin();
140
141         while (i != positions.end()) {
142
143                 framecnt_t to_read;
144
145                 /* read from source */
146                 to_read = buff_size;
147
148                 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
149                         break;
150                 }
151
152                 // Simple heuristic for locating approx correct cut position.
153
154                 for (int j = 0; j < buff_size;){
155
156                         Sample s = abs (data[j]);
157                         Sample s2 = abs (data[j + step_size]);
158
159                         if ((s2 - s) > threshold){
160                                 //cerr << "Thresh exceeded. Moving pos from: " << (*i) << " to: " << (*i) - buff_size + (j + 16) << endl;
161                                 (*i) = (*i) - buff_size + (j + 24);
162                                 break;
163                         }
164
165                         j = j + step_size;
166                 }
167
168                 ++i;
169         }
170
171         delete [] data;
172 }