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