set negative timestamps to zero
[vamp-aubio-plugins.git] / plugins / Notes.cpp
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
3 /*
4     Vamp feature extraction plugins using Paul Brossier's Aubio library.
5
6     Centre for Digital Music, Queen Mary, University of London.
7     This file copyright 2006 Chris Cannam.
8     
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.
14
15 */
16
17 #include <math.h>
18 #include "Notes.h"
19
20 using std::string;
21 using std::vector;
22 using std::cerr;
23 using std::endl;
24
25 Notes::Notes(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_pitchdet(0),
35     m_pitchtype(aubio_pitch_yinfft),
36     m_pitchmode(aubio_pitchm_freq),
37     m_threshold(0.3),
38     m_silence(-90),
39     m_median(6)
40 {
41 }
42
43 Notes::~Notes()
44 {
45     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
46     if (m_pitchdet) del_aubio_pitchdetection(m_pitchdet);
47     if (m_ibuf) del_fvec(m_ibuf);
48     if (m_onset) del_fvec(m_onset);
49     if (m_fftgrain) del_cvec(m_fftgrain);
50     if (m_pv) del_aubio_pvoc(m_pv);
51     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
52 }
53
54 string
55 Notes::getName() const
56 {
57     return "aubionotes";
58 }
59
60 string
61 Notes::getDescription() const
62 {
63     return "Aubio Note Tracker";
64 }
65
66 string
67 Notes::getMaker() const
68 {
69     return "Paul Brossier (plugin by Chris Cannam)";
70 }
71
72 int
73 Notes::getPluginVersion() const
74 {
75     return 1;
76 }
77
78 string
79 Notes::getCopyright() const
80 {
81     return "GPL";
82 }
83
84 bool
85 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
86 {
87     m_channelCount = channels;
88     m_stepSize = stepSize;
89     m_blockSize = blockSize;
90
91     size_t processingBlockSize;
92     if (m_onsettype == aubio_onset_energy ||
93         m_onsettype == aubio_onset_hfc) {
94         processingBlockSize = stepSize * 2;
95     } else {
96         processingBlockSize = stepSize * 4;
97     }
98
99     m_ibuf = new_fvec(stepSize, channels);
100     m_onset = new_fvec(1, channels);
101     m_fftgrain = new_cvec(processingBlockSize, channels);
102     m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
103     m_peakpick = new_aubio_peakpicker(m_threshold);
104
105     m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
106
107     m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
108                                           stepSize,
109                                           channels,
110                                           lrintf(m_inputSampleRate),
111                                           m_pitchtype,
112                                           m_pitchmode);
113
114     m_count = 0;
115     m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
116                                        lrintf(m_inputSampleRate));
117     m_currentOnset = Vamp::RealTime::zeroTime;
118     m_haveCurrent = false;
119
120     return true;
121 }
122
123 void
124 Notes::reset()
125 {
126 }
127
128 size_t
129 Notes::getPreferredStepSize() const
130 {
131     return 512;
132 }
133
134 size_t
135 Notes::getPreferredBlockSize() const
136 {
137     return 4 * getPreferredStepSize();
138 }
139
140 Notes::ParameterList
141 Notes::getParameterDescriptors() const
142 {
143     ParameterList list;
144     
145     ParameterDescriptor desc;
146     desc.name = "onsettype";
147     desc.description = "Onset Detection Function Type";
148     desc.minValue = 0;
149     desc.maxValue = 6;
150     desc.defaultValue = (int)aubio_onset_complex;
151     desc.isQuantized = true;
152     desc.quantizeStep = 1;
153     desc.valueNames.push_back("Energy Based");
154     desc.valueNames.push_back("Spectral Difference");
155     desc.valueNames.push_back("High-Frequency Content");
156     desc.valueNames.push_back("Complex Domain");
157     desc.valueNames.push_back("Phase Deviation");
158     desc.valueNames.push_back("Kullback-Liebler");
159     desc.valueNames.push_back("Modified Kullback-Liebler");
160     list.push_back(desc);
161
162     desc = ParameterDescriptor();
163     desc.name = "pitchtype";
164     desc.description = "Pitch Detection Function Type";
165     desc.minValue = 0;
166     desc.maxValue = 4;
167     desc.defaultValue = (int)aubio_pitch_yinfft;
168     desc.isQuantized = true;
169     desc.quantizeStep = 1;
170     desc.valueNames.push_back("YIN Frequency Estimator");
171     desc.valueNames.push_back("Spectral Comb");
172     desc.valueNames.push_back("Schmitt");
173     desc.valueNames.push_back("Fast Harmonic Comb");
174     desc.valueNames.push_back("YIN with FFT");
175     list.push_back(desc);
176
177     desc = ParameterDescriptor();
178     desc.name = "peakpickthreshold";
179     desc.description = "Peak Picker Threshold";
180     desc.minValue = 0;
181     desc.maxValue = 1;
182     desc.defaultValue = 0.3;
183     desc.isQuantized = false;
184     list.push_back(desc);
185
186     desc = ParameterDescriptor();
187     desc.name = "silencethreshold";
188     desc.description = "Silence Threshold";
189     desc.minValue = -120;
190     desc.maxValue = 0;
191     desc.defaultValue = -90;
192     desc.unit = "dB";
193     desc.isQuantized = false;
194     list.push_back(desc);
195
196     return list;
197 }
198
199 float
200 Notes::getParameter(std::string param) const
201 {
202     if (param == "onsettype") {
203         return m_onsettype;
204     } else if (param == "pitchtype") {
205         return m_pitchtype;
206     } else if (param == "peakpickthreshold") {
207         return m_threshold;
208     } else if (param == "silencethreshold") {
209         return m_silence;
210     } else {
211         return 0.0;
212     }
213 }
214
215 void
216 Notes::setParameter(std::string param, float value)
217 {
218     if (param == "onsettype") {
219         switch (lrintf(value)) {
220         case 0: m_onsettype = aubio_onset_energy; break;
221         case 1: m_onsettype = aubio_onset_specdiff; break;
222         case 2: m_onsettype = aubio_onset_hfc; break;
223         case 3: m_onsettype = aubio_onset_complex; break;
224         case 4: m_onsettype = aubio_onset_phase; break;
225         case 5: m_onsettype = aubio_onset_kl; break;
226         case 6: m_onsettype = aubio_onset_mkl; break;
227         }
228     } else if (param == "pitchtype") {
229         switch (lrintf(value)) {
230         case 0: m_pitchtype = aubio_pitch_yin; break;
231         case 1: m_pitchtype = aubio_pitch_mcomb; break;
232         case 2: m_pitchtype = aubio_pitch_schmitt; break;
233         case 3: m_pitchtype = aubio_pitch_fcomb; break;
234         case 4: m_pitchtype = aubio_pitch_yinfft; break;
235         }
236     } else if (param == "peakpickthreshold") {
237         m_threshold = value;
238     } else if (param == "silencethreshold") {
239         m_silence = value;
240     }
241 }
242
243 Notes::OutputList
244 Notes::getOutputDescriptors() const
245 {
246     OutputList list;
247
248     OutputDescriptor d;
249     d.name = "notes";
250     d.unit = "Hz";
251     d.description = "Notes";
252     d.hasFixedBinCount = true;
253     d.binCount = 2;
254     d.binNames.push_back("Frequency");
255     d.binNames.push_back("Duration");
256     d.binNames.push_back("Velocity");
257     d.hasKnownExtents = false;
258     d.isQuantized = false;
259     d.sampleType = OutputDescriptor::VariableSampleRate;
260     d.sampleRate = 0;
261     list.push_back(d);
262
263     return list;
264 }
265
266 Notes::FeatureSet
267 Notes::process(float **inputBuffers, Vamp::RealTime timestamp)
268 {
269     for (size_t i = 0; i < m_stepSize; ++i) {
270         for (size_t j = 0; j < m_channelCount; ++j) {
271             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
272         }
273     }
274
275     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
276     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
277
278     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
279
280     float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
281
282     m_notebuf.push_back(frequency);
283     if (m_notebuf.size() > m_median) m_notebuf.pop_front();
284
285     float level = aubio_level_detection(m_ibuf, m_silence);
286
287     FeatureSet returnFeatures;
288
289     if (isonset) {
290         if (level == 1.) {
291             isonset = false;
292             m_count = 0;
293             if (m_haveCurrent) pushNote(returnFeatures, timestamp);
294         } else {
295             m_count = 1;
296         }
297     } else {
298         if (m_count > 0) ++m_count;
299         if (m_count == m_median) {
300             if (m_haveCurrent) pushNote(returnFeatures, timestamp);
301             m_currentOnset = timestamp;
302             m_currentLevel = level;
303             m_haveCurrent = true;
304         }
305     }
306
307     m_lastTimeStamp = timestamp;
308     return returnFeatures;
309 }
310
311 Notes::FeatureSet
312 Notes::getRemainingFeatures()
313 {
314     FeatureSet returnFeatures;
315     if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
316     return returnFeatures;
317 }
318
319 void
320 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
321 {
322     std::deque<float> toSort = m_notebuf;
323     std::sort(toSort.begin(), toSort.end());
324     float median = toSort[toSort.size()/2];
325     if (median < 45.0) return;
326
327     Feature feature;
328     feature.hasTimestamp = true;
329     if (m_currentOnset < m_delay) m_currentOnset = m_delay;
330     feature.timestamp = m_currentOnset - m_delay;
331     feature.values.push_back(median);
332 //    feature.values.push_back(FLOOR(aubio_freqtomidi(median) + 0.5));
333     feature.values.push_back
334         (Vamp::RealTime::realTime2Frame(offTime, lrintf(m_inputSampleRate)) -
335          Vamp::RealTime::realTime2Frame(m_currentOnset, lrintf(m_inputSampleRate)));
336     feature.values.push_back(m_currentLevel);
337     fs[0].push_back(feature);
338 }
339