2 * Copyright (C) 2008-2011 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2015-2016 Robin Gareus <robin@gareus.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "ardour/readable.h"
25 #include "ardour/transient_detector.h"
30 using namespace ARDOUR;
33 /* need a static initializer function for this */
35 string TransientDetector::_op_id = X_("qm-onset");
37 TransientDetector::TransientDetector (float sr)
38 : AudioAnalyser (sr, X_("libardourvampplugins:qm-onsetdetector"))
43 TransientDetector::~TransientDetector()
48 TransientDetector::operational_identifier()
54 TransientDetector::run (const std::string& path, Readable* src, uint32_t channel, AnalysisFeatureList& results)
56 current_results = &results;
57 int ret = analyse (path, src, channel);
65 TransientDetector::use_features (Plugin::FeatureSet& features, ostream* out)
67 const Plugin::FeatureList& fl (features[0]);
69 for (Plugin::FeatureList::const_iterator f = fl.begin(); f != fl.end(); ++f) {
71 if (f->hasTimestamp) {
74 (*out) << (*f).timestamp.toString() << endl;
77 current_results->push_back (RealTime::realTime2Frame (f->timestamp, (samplecnt_t) floor(sample_rate)));
85 TransientDetector::set_threshold (float val)
91 TransientDetector::set_sensitivity (uint32_t mode, float val)
94 // see libs/vamp-plugins/OnsetDetect.cpp
95 //plugin->selectProgram ("General purpose"); // dftype = 3, sensitivity = 50, whiten = 0 (default)
96 //plugin->selectProgram ("Percussive onsets"); // dftype = 4, sensitivity = 40, whiten = 0
97 plugin->setParameter ("dftype", mode);
98 plugin->setParameter ("sensitivity", std::min (100.f, std::max (0.f, val)));
99 plugin->setParameter ("whiten", 0);
104 TransientDetector::cleanup_transients (AnalysisFeatureList& t, float sr, float gap_msecs)
112 /* remove duplicates or other things that are too close */
114 AnalysisFeatureList::iterator i = t.begin();
115 AnalysisFeatureList::iterator f, b;
116 const samplecnt_t gap_samples = (samplecnt_t) floor (gap_msecs * (sr / 1000.0));
118 while (i != t.end()) {
120 // move front iterator to just past i, and back iterator the same place
126 // move f until we find a new value that is far enough away
128 while ((f != t.end()) && gap_samples > 0 && (((*f) - (*i)) < gap_samples)) {
134 // if f moved forward from b, we had duplicates/too-close points: get rid of them
143 TransientDetector::update_positions (Readable* src, uint32_t channel, AnalysisFeatureList& positions)
145 int const buff_size = 1024;
146 int const step_size = 64;
148 Sample* data = new Sample[buff_size];
150 AnalysisFeatureList::iterator i = positions.begin();
152 while (i != positions.end()) {
154 /* read from source */
155 samplecnt_t const to_read = buff_size;
157 if (src->read (data, (*i) - buff_size, to_read, channel) != to_read) {
161 // Simple heuristic for locating approx correct cut position.
163 for (int j = 0; j < (buff_size - step_size); ) {
165 Sample const s = abs (data[j]);
166 Sample const s2 = abs (data[j + step_size]);
168 if ((s2 - s) > threshold) {
169 //cerr << "Thresh exceeded. Moving pos from: " << (*i) << " to: " << (*i) - buff_size + (j + 16) << endl;
170 (*i) = (*i) - buff_size + (j + 24);