3 #include "ardour/readable.h"
4 #include "ardour/transient_detector.h"
9 using namespace ARDOUR;
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
30 TransientDetector::~TransientDetector()
35 TransientDetector::operational_identifier()
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);
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) {
61 (*out) << (*f).timestamp.toString() << endl;
64 current_results->push_back (RealTime::realTime2Frame (f->timestamp, (framecnt_t) floor(sample_rate)));
72 TransientDetector::set_threshold (float val)
78 TransientDetector::set_sensitivity (float val)
81 plugin->selectProgram ("Percussive onsets");
82 plugin->setParameter ("sensitivity", val);
87 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
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
109 // move f until we find a new value that is far enough away
111 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
117 // if f moved forward from b, we had duplicates/too-close points: get rid of them
126 TransientDetector::update_positions (Readable* src, uint32_t channel, AnalysisFeatureList& positions)
128 Plugin::FeatureSet features;
132 int buff_size = 1024;
135 data = new Sample[buff_size];
137 AnalysisFeatureList::iterator i = positions.begin();
139 while (i != positions.end()) {
143 /* read from source */
146 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
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);