remove unused SHUTTLE_FRACT constant
[ardour2.git] / libs / ardour / transient_detector.cc
blob1fda65bde14659e8495e742c4cfe22f4c6ae3d1e
1 #include <cmath>
3 #include "ardour/readable.h"
4 #include "ardour/transient_detector.h"
6 #include "i18n.h"
8 using namespace Vamp;
9 using namespace ARDOUR;
10 using namespace std;
12 /* need a static initializer function for this */
14 string TransientDetector::_op_id = X_("libardourvampplugins:qm-onsetdetector:2");
16 TransientDetector::TransientDetector (float sr)
17 : AudioAnalyser (sr, X_("libardourvampplugins:qm-onsetdetector"))
19 /* update the op_id */
21 _op_id = X_("libardourvampplugins:qm-onsetdetector");
23 // XXX this should load the above-named plugin and get the current version
25 _op_id += ":2";
27 threshold = 0.00;
30 TransientDetector::~TransientDetector()
34 string
35 TransientDetector::operational_identifier()
37 return _op_id;
40 int
41 TransientDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
43 current_results = &results;
44 int ret = analyse (path, src, channel);
46 current_results = 0;
48 return ret;
51 int
52 TransientDetector::use_features (Plugin::FeatureSet& features, ostream* out)
54 const Plugin::FeatureList& fl (features[0]);
56 for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
58 if (f->hasTimestamp) {
60 if (out) {
61 (*out) << (*f).timestamp.toString() << endl;
64 current_results->push_back (RealTime::realTime2Frame (f->timestamp, (framecnt_t) floor(sample_rate)));
68 return 0;
71 void
72 TransientDetector::set_threshold (float val)
74 threshold = val;
77 void
78 TransientDetector::set_sensitivity (float val)
80 if (plugin) {
81 plugin->selectProgram ("Percussive onsets");
82 plugin->setParameter ("sensitivity", val);
86 void
87 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
89 if (t.empty()) {
90 return;
93 t.sort ();
95 /* remove duplicates or other things that are too close */
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));
101 while (i != t.end()) {
103 // move front iterator to just past i, and back iterator the same place
105 f = i;
106 ++f;
107 b = f;
109 // move f until we find a new value that is far enough away
111 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
112 ++f;
115 i = f;
117 // if f moved forward from b, we had duplicates/too-close points: get rid of them
119 if (b != f) {
120 t.erase (b, f);
125 void
126 TransientDetector::update_positions (Readable* src, uint32_t channel, AnalysisFeatureList& positions)
128 Plugin::FeatureSet features;
130 Sample* data = 0;
131 float* bufs[1] = { 0 };
133 int buff_size = 1024;
134 int step_size = 64;
136 data = new Sample[buff_size];
137 bufs[0] = data;
139 AnalysisFeatureList::iterator i = positions.begin();
141 while (i != positions.end()) {
143 framecnt_t to_read;
145 /* read from source */
146 to_read = buff_size;
148 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
149 break;
152 // Simple heuristic for locating approx correct cut position.
154 for (int j = 0; j < buff_size;){
156 Sample s = abs (data[j]);
157 Sample s2 = abs (data[j + step_size]);
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;
165 j = j + step_size;
168 ++i;
171 delete [] data;