35f4be9720d743c2db8b859e774dc0c5dd7ad831
[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_currentOnset = Vamp::RealTime::zeroTime;
116     m_haveCurrent = false;
117
118     return true;
119 }
120
121 void
122 Notes::reset()
123 {
124 }
125
126 size_t
127 Notes::getPreferredStepSize() const
128 {
129     return 512;
130 }
131
132 size_t
133 Notes::getPreferredBlockSize() const
134 {
135     return 4*getPreferredStepSize();
136 }
137
138 Notes::ParameterList
139 Notes::getParameterDescriptors() const
140 {
141     ParameterList list;
142     
143     ParameterDescriptor desc;
144     desc.name = "onsettype";
145     desc.description = "Onset Detection Function Type";
146     desc.minValue = 0;
147     desc.maxValue = 6;
148     desc.defaultValue = (int)aubio_onset_complex;
149     desc.isQuantized = true;
150     desc.quantizeStep = 1;
151     desc.valueNames.push_back("Energy Based");
152     desc.valueNames.push_back("Spectral Difference");
153     desc.valueNames.push_back("High-Frequency Content");
154     desc.valueNames.push_back("Complex Domain");
155     desc.valueNames.push_back("Phase Deviation");
156     desc.valueNames.push_back("Kullback-Liebler");
157     desc.valueNames.push_back("Modified Kullback-Liebler");
158     list.push_back(desc);
159
160     desc = ParameterDescriptor();
161     desc.name = "pitchtype";
162     desc.description = "Pitch Detection Function Type";
163     desc.minValue = 0;
164     desc.maxValue = 4;
165     desc.defaultValue = (int)aubio_pitch_yinfft;
166     desc.isQuantized = true;
167     desc.quantizeStep = 1;
168     desc.valueNames.push_back("YIN Frequency Estimator");
169     desc.valueNames.push_back("Spectral Comb");
170     desc.valueNames.push_back("Schmitt");
171     desc.valueNames.push_back("Fast Harmonic Comb");
172     desc.valueNames.push_back("YIN with FFT");
173     list.push_back(desc);
174
175     desc = ParameterDescriptor();
176     desc.name = "peakpickthreshold";
177     desc.description = "Peak Picker Threshold";
178     desc.minValue = 0;
179     desc.maxValue = 1;
180     desc.defaultValue = 0.3;
181     desc.isQuantized = false;
182     list.push_back(desc);
183
184     desc = ParameterDescriptor();
185     desc.name = "silencethreshold";
186     desc.description = "Silence Threshold";
187     desc.minValue = -120;
188     desc.maxValue = 0;
189     desc.defaultValue = -90;
190     desc.unit = "dB";
191     desc.isQuantized = false;
192     list.push_back(desc);
193
194     return list;
195 }
196
197 float
198 Notes::getParameter(std::string param) const
199 {
200     if (param == "onsettype") {
201         return m_onsettype;
202     } else if (param == "pitchtype") {
203         return m_pitchtype;
204     } else if (param == "peakpickthreshold") {
205         return m_threshold;
206     } else if (param == "silencethreshold") {
207         return m_silence;
208     } else {
209         return 0.0;
210     }
211 }
212
213 void
214 Notes::setParameter(std::string param, float value)
215 {
216     if (param == "onsettype") {
217         switch (lrintf(value)) {
218         case 0: m_onsettype = aubio_onset_energy; break;
219         case 1: m_onsettype = aubio_onset_specdiff; break;
220         case 2: m_onsettype = aubio_onset_hfc; break;
221         case 3: m_onsettype = aubio_onset_complex; break;
222         case 4: m_onsettype = aubio_onset_phase; break;
223         case 5: m_onsettype = aubio_onset_kl; break;
224         case 6: m_onsettype = aubio_onset_mkl; break;
225         }
226     } else if (param == "pitchtype") {
227         switch (lrintf(value)) {
228         case 0: m_pitchtype = aubio_pitch_yin; break;
229         case 1: m_pitchtype = aubio_pitch_mcomb; break;
230         case 2: m_pitchtype = aubio_pitch_schmitt; break;
231         case 3: m_pitchtype = aubio_pitch_fcomb; break;
232         case 4: m_pitchtype = aubio_pitch_yinfft; break;
233         }
234     } else if (param == "peakpickthreshold") {
235         m_threshold = value;
236     } else if (param == "silencethreshold") {
237         m_silence = value;
238     }
239 }
240
241 Notes::OutputList
242 Notes::getOutputDescriptors() const
243 {
244     OutputList list;
245
246     OutputDescriptor d;
247     d.name = "notes";
248     d.unit = "Hz";
249     d.description = "Notes";
250     d.hasFixedBinCount = true;
251     d.binCount = 2;
252     d.binNames.push_back("Frequency");
253     d.binNames.push_back("Duration");
254     d.binNames.push_back("Velocity");
255     d.hasKnownExtents = false;
256     d.isQuantized = false;
257     d.sampleType = OutputDescriptor::VariableSampleRate;
258     d.sampleRate = 0;
259     list.push_back(d);
260
261     return list;
262 }
263
264 Notes::FeatureSet
265 Notes::process(float **inputBuffers, Vamp::RealTime timestamp)
266 {
267     for (size_t i = 0; i < m_stepSize; ++i) {
268         for (size_t j = 0; j < m_channelCount; ++j) {
269             fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
270         }
271     }
272
273     aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
274     aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
275
276     bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
277
278     float frequency = aubio_pitchdetection(m_pitchdet, m_ibuf);
279
280     m_notebuf.push_back(frequency);
281     if (m_notebuf.size() > m_median) m_notebuf.pop_front();
282
283     float level = aubio_level_detection(m_ibuf, m_silence);
284
285     FeatureSet returnFeatures;
286
287     if (isonset) {
288         if (level == 1.) {
289             isonset = false;
290             m_count = 0;
291             if (m_haveCurrent) pushNote(returnFeatures, timestamp);
292         } else {
293             m_count = 1;
294         }
295     } else {
296         if (m_count > 0) ++m_count;
297         if (m_count == m_median) {
298             if (m_haveCurrent) pushNote(returnFeatures, timestamp);
299             m_currentOnset = timestamp;
300             m_currentLevel = level;
301             m_haveCurrent = true;
302         }
303     }
304
305     m_lastTimeStamp = timestamp;
306     return returnFeatures;
307 }
308
309 Notes::FeatureSet
310 Notes::getRemainingFeatures()
311 {
312     FeatureSet returnFeatures;
313     if (m_haveCurrent) pushNote(returnFeatures, m_lastTimeStamp);
314     return returnFeatures;
315 }
316
317 void
318 Notes::pushNote(FeatureSet &fs, const Vamp::RealTime &offTime)
319 {
320     std::deque<float> toSort = m_notebuf;
321     std::sort(toSort.begin(), toSort.end());
322     float median = toSort[toSort.size()/2];
323     if (median < 45.0) return;
324
325     Feature feature;
326     feature.hasTimestamp = true;
327     feature.timestamp = m_currentOnset;
328     feature.values.push_back(median);
329 //    feature.values.push_back(FLOOR(aubio_freqtomidi(median) + 0.5));
330     feature.values.push_back
331         (Vamp::RealTime::realTime2Frame(offTime, lrintf(m_inputSampleRate)) -
332          Vamp::RealTime::realTime2Frame(m_currentOnset, lrintf(m_inputSampleRate)));
333     feature.values.push_back(m_currentLevel);
334     fs[0].push_back(feature);
335 }
336