ignore unpaired noteoff's when writing part of a MidiModel to a new source. in realit...
[ardour2.git] / libs / vamp-plugins / Onset.cpp
blobd475b11be93af1ef989da64bda702a9b5f45a2ba
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 Vamp feature extraction plugins using Paul Brossier's Aubio library.
6 Centre for Digital Music, Queen Mary, University of London.
7 This file copyright 2006 Chris Cannam.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
17 #include <math.h>
18 #include "Onset.h"
20 using std::string;
21 using std::vector;
22 using std::cerr;
23 using std::endl;
25 Onset::Onset(float inputSampleRate) :
26 Plugin(inputSampleRate),
27 m_ibuf(0),
28 m_fftgrain(0),
29 m_onset(0),
30 m_pv(0),
31 m_peakpick(0),
32 m_onsetdet(0),
33 m_onsettype(aubio_onset_complex),
34 m_threshold(0.3),
35 m_silence(-90),
36 m_channelCount(1)
40 Onset::~Onset()
42 if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
43 if (m_ibuf) del_fvec(m_ibuf);
44 if (m_onset) del_fvec(m_onset);
45 if (m_fftgrain) del_cvec(m_fftgrain);
46 if (m_pv) del_aubio_pvoc(m_pv);
47 if (m_peakpick) del_aubio_peakpicker(m_peakpick);
50 string
51 Onset::getIdentifier() const
53 return "aubioonset";
56 string
57 Onset::getName() const
59 return "Aubio Onset Detector";
62 string
63 Onset::getDescription() const
65 return "Estimate note onset times";
68 string
69 Onset::getMaker() const
71 return "Paul Brossier (plugin by Chris Cannam)";
74 int
75 Onset::getPluginVersion() const
77 return 1;
80 string
81 Onset::getCopyright() const
83 return "GPL";
86 bool
87 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
89 m_channelCount = channels;
90 m_stepSize = stepSize;
91 m_blockSize = blockSize;
93 m_ibuf = new_fvec(stepSize, channels);
94 m_onset = new_fvec(1, channels);
95 m_fftgrain = new_cvec(blockSize, channels);
96 m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
97 m_peakpick = new_aubio_peakpicker(m_threshold);
99 m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
101 m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
102 lrintf(m_inputSampleRate));
104 m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
106 return true;
109 void
110 Onset::reset()
114 size_t
115 Onset::getPreferredStepSize() const
117 return 512;
120 size_t
121 Onset::getPreferredBlockSize() const
123 return 2 * getPreferredStepSize();
126 Onset::ParameterList
127 Onset::getParameterDescriptors() const
129 ParameterList list;
131 ParameterDescriptor desc;
132 desc.identifier = "onsettype";
133 desc.name = "Onset Detection Function Type";
134 desc.minValue = 0;
135 desc.maxValue = 6;
136 desc.defaultValue = (int)aubio_onset_complex;
137 desc.isQuantized = true;
138 desc.quantizeStep = 1;
139 desc.valueNames.push_back("Energy Based");
140 desc.valueNames.push_back("Spectral Difference");
141 desc.valueNames.push_back("High-Frequency Content");
142 desc.valueNames.push_back("Complex Domain");
143 desc.valueNames.push_back("Phase Deviation");
144 desc.valueNames.push_back("Kullback-Liebler");
145 desc.valueNames.push_back("Modified Kullback-Liebler");
146 list.push_back(desc);
148 desc = ParameterDescriptor();
149 desc.identifier = "peakpickthreshold";
150 desc.name = "Peak Picker Threshold";
151 desc.minValue = 0;
152 desc.maxValue = 1;
153 desc.defaultValue = 0.3;
154 desc.isQuantized = false;
155 list.push_back(desc);
157 desc = ParameterDescriptor();
158 desc.identifier = "silencethreshold";
159 desc.name = "Silence Threshold";
160 desc.minValue = -120;
161 desc.maxValue = 0;
162 desc.defaultValue = -90;
163 desc.unit = "dB";
164 desc.isQuantized = false;
165 list.push_back(desc);
167 return list;
170 float
171 Onset::getParameter(std::string param) const
173 if (param == "onsettype") {
174 return m_onsettype;
175 } else if (param == "peakpickthreshold") {
176 return m_threshold;
177 } else if (param == "silencethreshold") {
178 return m_silence;
179 } else {
180 return 0.0;
184 void
185 Onset::setParameter(std::string param, float value)
187 if (param == "onsettype") {
188 switch (lrintf(value)) {
189 case 0: m_onsettype = aubio_onset_energy; break;
190 case 1: m_onsettype = aubio_onset_specdiff; break;
191 case 2: m_onsettype = aubio_onset_hfc; break;
192 case 3: m_onsettype = aubio_onset_complex; break;
193 case 4: m_onsettype = aubio_onset_phase; break;
194 case 5: m_onsettype = aubio_onset_kl; break;
195 case 6: m_onsettype = aubio_onset_mkl; break;
197 } else if (param == "peakpickthreshold") {
198 m_threshold = value;
199 } else if (param == "silencethreshold") {
200 m_silence = value;
204 Onset::OutputList
205 Onset::getOutputDescriptors() const
207 OutputList list;
209 OutputDescriptor d;
210 d.identifier = "onsets";
211 d.name = "Onsets";
212 d.unit = "";
213 d.hasFixedBinCount = true;
214 d.binCount = 0;
215 d.sampleType = OutputDescriptor::VariableSampleRate;
216 d.sampleRate = 0;
217 list.push_back(d);
219 d = OutputDescriptor();
220 d.identifier = "detectionfunction";
221 d.name = "Onset Detection Function";
222 d.unit = "";
223 d.hasFixedBinCount = true;
224 d.binCount = m_channelCount;
225 d.hasKnownExtents = false;
226 d.isQuantized = false;
227 d.sampleType = OutputDescriptor::OneSamplePerStep;
228 list.push_back(d);
230 return list;
233 Onset::FeatureSet
234 Onset::process(const float *const *inputBuffers,
235 Vamp::RealTime timestamp)
237 for (size_t i = 0; i < m_stepSize; ++i) {
238 for (size_t j = 0; j < m_channelCount; ++j) {
239 fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
243 aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
244 aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
246 bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
248 if (isonset) {
249 if (aubio_silence_detection(m_ibuf, m_silence)) {
250 isonset = false;
254 FeatureSet returnFeatures;
256 if (isonset) {
257 if (timestamp - m_lastOnset >= m_delay) {
258 Feature onsettime;
259 onsettime.hasTimestamp = true;
260 if (timestamp < m_delay) timestamp = m_delay;
261 onsettime.timestamp = timestamp - m_delay;
262 returnFeatures[0].push_back(onsettime);
263 m_lastOnset = timestamp;
266 Feature feature;
267 for (size_t j = 0; j < m_channelCount; ++j) {
268 feature.values.push_back(m_onset->data[j][0]);
270 returnFeatures[1].push_back(feature);
272 return returnFeatures;
275 Onset::FeatureSet
276 Onset::getRemainingFeatures()
278 return FeatureSet();