Move panner bypass state up to the PannerShell so that it is preserved even when...
[ardour2.git] / libs / ardour / transient_detector.cc
blobcf8557de82740a602cf0ab792c6289323625b6b1
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;
132 int buff_size = 1024;
133 int step_size = 64;
135 data = new Sample[buff_size];
137 AnalysisFeatureList::iterator i = positions.begin();
139 while (i != positions.end()) {
141 framecnt_t to_read;
143 /* read from source */
144 to_read = buff_size;
146 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
147 break;
150 // Simple heuristic for locating approx correct cut position.
152 for (int j = 0; j < buff_size;){
154 Sample s = abs (data[j]);
155 Sample s2 = abs (data[j + step_size]);
157 if ((s2 - s) > threshold){
158 //cerr << "Thresh exceeded. Moving pos from: " << (*i) << " to: " << (*i) - buff_size + (j + 16) << endl;
159 (*i) = (*i) - buff_size + (j + 24);
160 break;
163 j = j + step_size;
166 ++i;
169 delete [] data;