fc186dc7c1af905451fc9a65ef55e7415758fe83
[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     m_minpitch(36),
41     m_maxpitch(95),
42     m_wrapRange(false),
43     m_avoidLeaps(false),
44     m_prevPitch(-1)
45 {
46 }
47
48 Notes::~Notes()
49 {
50     if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
51     if (m_pitchdet) del_aubio_pitchdetection(m_pitchdet);
52     if (m_ibuf) del_fvec(m_ibuf);
53     if (m_onset) del_fvec(m_onset);
54     if (m_fftgrain) del_cvec(m_fftgrain);
55     if (m_pv) del_aubio_pvoc(m_pv);
56     if (m_peakpick) del_aubio_peakpicker(m_peakpick);
57 }
58
59 string
60 Notes::getName() const
61 {
62     return "aubionotes";
63 }
64
65 string
66 Notes::getDescription() const
67 {
68     return "Aubio Note Tracker";
69 }
70
71 string
72 Notes::getMaker() const
73 {
74     return "Paul Brossier (plugin by Chris Cannam)";
75 }
76
77 int
78 Notes::getPluginVersion() const
79 {
80     return 1;
81 }
82
83 string
84 Notes::getCopyright() const
85 {
86     return "GPL";
87 }
88
89 bool
90 Notes::initialise(size_t channels, size_t stepSize, size_t blockSize)
91 {
92     m_channelCount = channels;
93     m_stepSize = stepSize;
94     m_blockSize = blockSize;
95
96     size_t processingBlockSize;
97     if (m_onsettype == aubio_onset_energy ||
98         m_onsettype == aubio_onset_hfc) {
99         processingBlockSize = stepSize * 2;
100     } else {
101         processingBlockSize = stepSize * 4;
102     }
103
104     m_ibuf = new_fvec(stepSize, channels);
105     m_onset = new_fvec(1, channels);
106     m_fftgrain = new_cvec(processingBlockSize, channels);
107     m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
108     m_peakpick = new_aubio_peakpicker(m_threshold);
109
110     m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
111
112     m_pitchdet = new_aubio_pitchdetection(processingBlockSize * 4,
113                                           stepSize,
114                                           channels,
115                                           lrintf(m_inputSampleRate),
116                                           m_pitchtype,
117                                           m_pitchmode);
118
119     m_count = 0;
120     m_delay = Vamp::RealTime::frame2RealTime((4 + m_median) * m_stepSize,
121                                        lrintf(m_inputSampleRate));
122     m_currentOnset = Vamp::RealTime::zeroTime;
123     m_haveCurrent = false;
124     m_prevPitch = -1;
125
126     return true;
127 }
128
129 void
130 Notes::reset()
131 {
132 }
133
134 size_t
135 Notes::getPreferredStepSize() const
136 {
137     return 512;
138 }
139
140 size_t
141 Notes::getPreferredBlockSize() const
142 {
143     return 4 * getPreferredStepSize();
144 }
145
146 Notes::ParameterList
147 Notes::getParameterDescriptors() const
148 {
149     ParameterList list;
150     
151     ParameterDescriptor desc;
152     desc.name = "onsettype";
153     desc.description = "Onset Detection Function Type";
154     desc.minValue = 0;
155     desc.maxValue = 6;
156     desc.defaultValue = (int)aubio_onset_complex;
157     desc.isQuantized = true;
158     desc.quantizeStep = 1;
159     desc.valueNames.push_back("Energy Based");
160     desc.valueNames.push_back("Spectral Difference");
161     desc.valueNames.push_back("High-Frequency Content");
162     desc.valueNames.push_back("Complex Domain");
163     desc.valueNames.push_back("Phase Deviation");
164     desc.valueNames.push_back("Kullback-Liebler");
165     desc.valueNames.push_back("Modified Kullback-Liebler");
166     list.push_back(desc);
167
168     desc = ParameterDescriptor();
169     desc.name = "pitchtype";
170     desc.description = "Pitch Detection Function Type";
171     desc.minValue = 0;
172     desc.maxValue = 4;
173     desc.defaultValue = (int)aubio_pitch_yinfft;
174     desc.isQuantized = true;
175     desc.quantizeStep = 1;
176     desc.valueNames.push_back("YIN Frequency Estimator");
177     desc.valueNames.push_back("Spectral Comb");
178     desc.valueNames.push_back("Schmitt");
179     desc.valueNames.push_back("Fast Harmonic Comb");
180     desc.valueNames.push_back("YIN with FFT");
181     list.push_back(desc);
182
183     desc = ParameterDescriptor();
184     desc.name = "minpitch";
185     desc.description = "Minimum Pitch";
186     desc.minValue = 0;
187     desc.maxValue = 127;
188     desc.defaultValue = 32;
189     desc.unit = "MIDI units";
190     desc.isQuantized = true;
191     desc.quantizeStep = 1;
192     list.push_back(desc);
193
194     desc = ParameterDescriptor();
195     desc.name = "maxpitch";
196     desc.description = "Maximum Pitch";
197     desc.minValue = 0;
198     desc.maxValue = 127;
199     desc.defaultValue = 95;
200     desc.unit = "MIDI units";
201     desc.isQuantized = true;
202     desc.quantizeStep = 1;
203     list.push_back(desc);
204
205     desc = ParameterDescriptor();
206     desc.name = "wraprange";
207     desc.description = "Fold Higher or Lower Notes into Range";
208     desc.minValue = 0;
209     desc.maxValue = 1;
210     desc.defaultValue = 0;
211     desc.isQuantized = true;
212     desc.quantizeStep = 1;
213     list.push_back(desc);
214
215     desc = ParameterDescriptor();
216     desc.name = "avoidleaps";
217     desc.description = "Avoid Multi-Octave Jumps";
218     desc.minValue = 0;
219     desc.maxValue = 1;
220     desc.defaultValue = 0;
221     desc.isQuantized = true;
222     desc.quantizeStep = 1;
223     list.push_back(desc);
224
225     desc = ParameterDescriptor();
226     desc.name = "peakpickthreshold";
227     desc.description = "Peak Picker Threshold";
228     desc.minValue = 0;
229     desc.maxValue = 1;
230     desc.defaultValue = 0.3;
231     desc.isQuantized = false;
232     list.push_back(desc);
233
234     desc = ParameterDescriptor();
235     desc.name = "silencethreshold";
236     desc.description = "Silence Threshold";
237     desc.minValue = -120;
238     desc.maxValue = 0;
239     desc.defaultValue = -90;
240     desc.unit = "dB";
241     desc.isQuantized = false;
242     list.push_back(desc);
243
244     return list;
245 }
246
247 float
248 Notes::getParameter(std::string param) const
249 {
250     if (param == "onsettype") {
251         return m_onsettype;
252     } else if (param == "pitchtype") {
253         return m_pitchtype;
254     } else if (param == "peakpickthreshold") {
255         return m_threshold;
256     } else if (param == "silencethreshold") {
257         return m_silence;
258     } else if (param == "minpitch") {
259         return m_minpitch;
260     } else if (param == "maxpitch") {
261         return m_maxpitch;
262     } else if (param == "wraprange") {
263         return m_wrapRange ? 1.0 : 0.0;
264     } else if (param == "avoidleaps") {
265         return m_avoidLeaps ? 1.0 : 0.0;
266     } else {
267         return 0.0;
268     }
269 }
270
271 void
272 Notes::setParameter(std::string param, float value)
273 {
274     if (param == "onsettype") {
275         switch (lrintf(value)) {
276         case 0: m_onsettype = aubio_onset_energy; break;
277         case 1: m_onsettype = aubio_onset_specdiff; break;
278         case 2: m_onsettype = aubio_onset_hfc; break;
279         case 3: m_onsettype = aubio_onset_complex; break;
280         case 4: m_onsettype = aubio_onset_phase; break;
281         case 5: m_onsettype = aubio_onset_kl; break;
282         case 6: m_onsettype = aubio_onset_mkl; break;
283         }
284     } else if (param == "pitchtype") {
285         switch (lrintf(value)) {
286         case 0: m_pitchtype = aubio_pitch_yin; break;
287         case 1: m_pitchtype = aubio_pitch_mcomb; break;
288         case 2: m_pitchtype = aubio_pitch_schmitt; break;
289         case 3: m_pitchtype = aubio_pitch_fcomb; break;
290         case 4: m_pitchtype = aubio_pitch_yinfft; break;
291         }
292     } else if (param == "peakpickthreshold") {
293         m_threshold = value;
294     } else if (param == "silencethreshold") {
295         m_silence = value;
296     } else if (param == "minpitch") {
297         m_minpitch = lrintf(value);
298     } else if (param == "maxpitch") {
299         m_maxpitch = lrintf(value);
300     } else if (param == "wraprange") {
301         m_wrapRange = (value > 0.5);
302     } else if (param == "avoidleaps") {
303         m_avoidLeaps = (value > 0.5);
304     }
305 }
306
307 Notes::OutputList
308 Notes::getOutputDescriptors() const
309 {
310     OutputList list;
311
312     OutputDescriptor d;
313     d.name = "notes";
314     d.unit = "Hz";
315     d.description = "Notes";
316     d.hasFixedBinCount = true;
317     d.binCount = 2;
318     d.binNames.push_back("Frequency");
319     d.binNames.push_back("Duration");
320     d.binNames.push_back("Velocity");
321     d.hasKnownExtents = false;
322     d.isQuantized = false;
323     d.sampleType = OutputDescriptor::VariableSampleRate;
324     d.sampleRate = 0;
325     list.push_back(d);
326
327     return list;
328 }
329
330 Notes::FeatureSet
331 Notes::process(float **inputBuffers, Vamp::RealTime timestamp)
332 {
333     for (size_t i = 0; i < m_stepSize; ++i) {
334         for (size_t j = 0; j < m_channelCount; ++j) {
335             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
336         }
337     }
338
339     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
340     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
341
342     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
343
344     float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
345
346     m_notebuf.push_back(frequency);
347     if (m_notebuf.size() > m_median) m_notebuf.pop_front();
348
349     float level = aubio_level_detection(m_ibuf, m_silence);
350
351     FeatureSet returnFeatures;
352
353     if (isonset) {
354         if (level == 1.) {
355             isonset = false;
356             m_count = 0;
357             if (m_haveCurrent) pushNote(returnFeatures, timestamp);
358         } else {
359             m_count = 1;
360         }
361     } else {
362         if (m_count > 0) ++m_count;
363         if (m_count == m_median) {
364             if (m_haveCurrent) pushNote(returnFeatures, timestamp);
365             m_currentOnset = timestamp;
366             m_currentLevel = level;
367             m_haveCurrent = true;
368         }
369     }
370
371     m_lastTimeStamp = timestamp;
372     return returnFeatures;
373 }
374
375 Notes::FeatureSet
376 Notes::getRemainingFeatures()
377 {
378     FeatureSet returnFeatures;
379     if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
380     return returnFeatures;
381 }
382
383 void
384 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
385 {
386     std::deque<float> toSort = m_notebuf;
387     std::sort(toSort.begin(), toSort.end());
388     float median = toSort[toSort.size()/2];
389     if (median < 45.0) return;
390
391     float freq = median;
392     int midiPitch = (int)FLOOR(aubio_freqtomidi(freq) + 0.5);
393     
394     if (m_avoidLeaps) {
395         if (m_prevPitch >= 0) {
396             while (midiPitch < m_prevPitch - 12) {
397                 midiPitch += 12;
398                 freq *= 2;
399             }
400             while (midiPitch > m_prevPitch + 12) {
401                 midiPitch -= 12;
402                 freq /= 2;
403             }
404         }
405     }
406
407     while (midiPitch < m_minpitch) {
408         if (!m_wrapRange) return;
409         midiPitch += 12;
410         freq *= 2;
411     }
412
413     while (midiPitch > m_maxpitch) {
414         if (!m_wrapRange) return;
415         midiPitch -= 12;
416         freq /= 2;
417     }
418
419     m_prevPitch = midiPitch;
420
421     Feature feature;
422     feature.hasTimestamp = true;
423     if (m_currentOnset < m_delay) m_currentOnset = m_delay;
424     feature.timestamp = m_currentOnset - m_delay;
425     feature.values.push_back(freq);
426     feature.values.push_back
427         (Vamp::RealTime::realTime2Frame(offTime, lrintf(m_inputSampleRate)) -
428          Vamp::RealTime::realTime2Frame(m_currentOnset, lrintf(m_inputSampleRate)));
429     feature.values.push_back(m_currentLevel);
430     fs[0].push_back(feature);
431 }
432