possible fix for race between diskstream buffer overwrite and channel setup
[ardour2.git] / libs / ardour / transient_detector.cc
blob1eca79e67de454a7433c5c594b616c37b2eb88f4
1 #include "ardour/transient_detector.h"
3 #include "i18n.h"
5 using namespace Vamp;
6 using namespace ARDOUR;
7 using namespace std;
9 /* need a static initializer function for this */
11 string TransientDetector::_op_id = X_("libardourvampplugins:percussiononsets:2");
13 TransientDetector::TransientDetector (float sr)
14 : AudioAnalyser (sr, X_("libardourvampplugins:percussiononsets"))
16 /* update the op_id */
18 _op_id = X_("libardourvampplugins:percussiononsets");
20 // XXX this should load the above-named plugin and get the current version
22 _op_id += ":2";
25 TransientDetector::~TransientDetector()
29 string
30 TransientDetector::operational_identifier()
32 return _op_id;
35 int
36 TransientDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
38 current_results = &results;
39 int ret = analyse (path, src, channel);
41 current_results = 0;
42 return ret;
45 int
46 TransientDetector::use_features (Plugin::FeatureSet& features, ostream* out)
48 const Plugin::FeatureList& fl (features[0]);
50 for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
52 if ((*f).hasTimestamp) {
54 if (out) {
55 (*out) << (*f).timestamp.toString() << endl;
58 current_results->push_back (RealTime::realTime2Frame ((*f).timestamp, (nframes_t) floor(sample_rate)));
62 return 0;
65 void
66 TransientDetector::set_threshold (float val)
68 if (plugin) {
69 plugin->setParameter ("threshold", val);
73 void
74 TransientDetector::set_sensitivity (float val)
76 if (plugin) {
77 plugin->setParameter ("sensitivity", val);
81 void
82 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
84 if (t.empty()) {
85 return;
88 t.sort ();
90 /* remove duplicates or other things that are too close */
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));
96 while (i != t.end()) {
98 // move front iterator to just past i, and back iterator the same place
100 f = i;
101 ++f;
102 b = f;
104 // move f until we find a new value that is far enough away
106 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
107 ++f;
110 i = f;
112 // if f moved forward from b, we had duplicates/too-close points: get rid of them
114 if (b != f) {
115 t.erase (b, f);