2 Copyright (C) 2012 Paul Davis
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.
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.
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.
22 #include "ardour/readable.h"
23 #include "ardour/transient_detector.h"
28 using namespace ARDOUR;
31 /* need a static initializer function for this */
33 string TransientDetector::_op_id = X_("libardourvampplugins:qm-onsetdetector:2");
35 TransientDetector::TransientDetector (float sr)
36 : AudioAnalyser (sr, X_("libardourvampplugins:qm-onsetdetector"))
38 /* update the op_id */
40 _op_id = X_("libardourvampplugins:qm-onsetdetector");
42 // XXX this should load the above-named plugin and get the current version
49 TransientDetector::~TransientDetector()
54 TransientDetector::operational_identifier()
60 TransientDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
62 current_results = &results;
63 int ret = analyse (path, src, channel);
71 TransientDetector::use_features (Plugin::FeatureSet& features, ostream* out)
73 const Plugin::FeatureList& fl (features[0]);
75 for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
77 if (f->hasTimestamp) {
80 (*out) << (*f).timestamp.toString() << endl;
83 current_results->push_back (RealTime::realTime2Frame (f->timestamp, (framecnt_t) floor(sample_rate)));
91 TransientDetector::set_threshold (float val)
97 TransientDetector::set_sensitivity (float val)
100 plugin->selectProgram ("Percussive onsets");
101 plugin->setParameter ("sensitivity", val);
106 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
114 /* remove duplicates or other things that are too close */
116 AnalysisFeatureList::iterator i = t.begin();
117 AnalysisFeatureList::iterator f, b;
118 const framecnt_t gap_frames = (framecnt_t) floor (gap_msecs * (sr / 1000.0));
120 while (i != t.end()) {
122 // move front iterator to just past i, and back iterator the same place
128 // move f until we find a new value that is far enough away
130 while ((f != t.end()) && (((*f) - (*i)) < gap_frames)) {
136 // if f moved forward from b, we had duplicates/too-close points: get rid of them
145 TransientDetector::update_positions (Readable* src, uint32_t channel, AnalysisFeatureList& positions)
147 int const buff_size = 1024;
148 int const step_size = 64;
150 Sample* data = new Sample[buff_size];
152 AnalysisFeatureList::iterator i = positions.begin();
154 while (i != positions.end()) {
156 /* read from source */
157 framecnt_t const to_read = buff_size;
159 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
163 // Simple heuristic for locating approx correct cut position.
165 for (int j = 0; j < (buff_size - step_size); ) {
167 Sample const s = abs (data[j]);
168 Sample const s2 = abs (data[j + step_size]);
170 if ((s2 - s) > threshold) {
171 //cerr << "Thresh exceeded. Moving pos from: " << (*i) << " to: " << (*i) - buff_size + (j + 16) << endl;
172 (*i) = (*i) - buff_size + (j + 24);