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