fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / transient_detector.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <cmath>
21
22 #include "ardour/readable.h"
23 #include "ardour/transient_detector.h"
24
25 #include "pbd/i18n.h"
26
27 using namespace Vamp;
28 using namespace ARDOUR;
29 using namespace std;
30
31 /* need a static initializer function for this */
32
33 string TransientDetector::_op_id = X_("qm-onset");
34
35 TransientDetector::TransientDetector (float sr)
36         : AudioAnalyser (sr, X_("libardourvampplugins:qm-onsetdetector"))
37 {
38         threshold = 0.00;
39 }
40
41 TransientDetector::~TransientDetector()
42 {
43 }
44
45 string
46 TransientDetector::operational_identifier()
47 {
48         return _op_id;
49 }
50
51 int
52 TransientDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
53 {
54         current_results = &results;
55         int ret = analyse (path, src, channel);
56
57         current_results = 0;
58
59         return ret;
60 }
61
62 int
63 TransientDetector::use_features (Plugin::FeatureSet& features, ostream* out)
64 {
65         const Plugin::FeatureList& fl (features[0]);
66
67         for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
68
69                 if (f->hasTimestamp) {
70
71                         if (out) {
72                                 (*out) << (*f).timestamp.toString() << endl;
73                         }
74
75                         current_results->push_back (RealTime::realTime2Frame (f->timestamp, (framecnt_t) floor(sample_rate)));
76                 }
77         }
78
79         return 0;
80 }
81
82 void
83 TransientDetector::set_threshold (float val)
84 {
85         threshold = val;
86 }
87
88 void
89 TransientDetector::set_sensitivity (uint32_t mode, float val)
90 {
91         if (plugin) {
92                 // see libs/vamp-plugins/OnsetDetect.cpp
93                 //plugin->selectProgram ("General purpose"); // dftype = 3, sensitivity = 50, whiten = 0 (default)
94                 //plugin->selectProgram ("Percussive onsets"); // dftype = 4, sensitivity = 40, whiten = 0
95                 plugin->setParameter ("dftype", mode);
96                 plugin->setParameter ("sensitivity", std::min (100.f, std::max (0.f, val)));
97                 plugin->setParameter ("whiten", 0);
98         }
99 }
100
101 void
102 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
103 {
104         if (t.empty()) {
105                 return;
106         }
107
108         t.sort ();
109
110         /* remove duplicates or other things that are too close */
111
112         AnalysisFeatureList::iterator i = t.begin();
113         AnalysisFeatureList::iterator f, b;
114         const framecnt_t gap_frames = (framecnt_t) floor (gap_msecs * (sr / 1000.0));
115
116         while (i != t.end()) {
117
118                 // move front iterator to just past i, and back iterator the same place
119
120                 f = i;
121                 ++f;
122                 b = f;
123
124                 // move f until we find a new value that is far enough away
125
126                 while ((f != t.end()) && gap_frames > 0 && (((*f) - (*i)) < gap_frames)) {
127                         ++f;
128                 }
129
130                 i = f;
131
132                 // if f moved forward from b, we had duplicates/too-close points: get rid of them
133
134                 if (b != f) {
135                         t.erase (b, f);
136                 }
137         }
138 }
139
140 void
141 TransientDetector::update_positions (Readable* src, uint32_t channel, AnalysisFeatureList& positions)
142 {
143         int const buff_size = 1024;
144         int const step_size = 64;
145
146         Sample* data = new Sample[buff_size];
147
148         AnalysisFeatureList::iterator i = positions.begin();
149
150         while (i != positions.end()) {
151
152                 /* read from source */
153                 framecnt_t const to_read = buff_size;
154
155                 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
156                         break;
157                 }
158
159                 // Simple heuristic for locating approx correct cut position.
160
161                 for (int j = 0; j < (buff_size - step_size); ) {
162
163                         Sample const s = abs (data[j]);
164                         Sample const s2 = abs (data[j + step_size]);
165
166                         if ((s2 - s) > threshold) {
167                                 //cerr << "Thresh exceeded. Moving pos from: " << (*i) << " to: " << (*i) - buff_size + (j + 16) << endl;
168                                 (*i) = (*i) - buff_size + (j + 24);
169                                 break;
170                         }
171
172                         j += step_size;
173                 }
174
175                 ++i;
176         }
177
178         delete [] data;
179 }