annotate plugins/ChromagramPlugin.cpp @ 17:d4a92aab8147

* Fix a buffer overrun and some compile warnings
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 30 Nov 2006 14:53:29 +0000
parents 2fbfe5e42ad4
children 99dadc93042e
rev   line source
c@0 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@0 2
c@0 3 /*
c@0 4 QM Vamp Plugin Set
c@0 5
c@0 6 Centre for Digital Music, Queen Mary, University of London.
c@0 7 All rights reserved.
c@0 8 */
c@0 9
c@0 10 #include "ChromagramPlugin.h"
c@0 11
c@3 12 #include <base/Pitch.h>
c@3 13 #include <dsp/chromagram/Chromagram.h>
c@0 14
c@0 15 using std::string;
c@0 16 using std::vector;
c@0 17 using std::cerr;
c@0 18 using std::endl;
c@0 19
c@0 20 ChromagramPlugin::ChromagramPlugin(float inputSampleRate) :
c@0 21 Vamp::Plugin(inputSampleRate),
c@0 22 m_chromagram(0),
c@0 23 m_step(0),
c@7 24 m_block(0)
c@0 25 {
c@0 26 m_minMIDIPitch = 12;
c@0 27 m_maxMIDIPitch = 96;
c@0 28 m_tuningFrequency = 440;
c@0 29 m_normalized = true;
c@0 30 m_bpo = 12;
c@0 31
c@0 32 setupConfig();
c@0 33 }
c@0 34
c@0 35 void
c@0 36 ChromagramPlugin::setupConfig()
c@0 37 {
c@0 38 m_config.FS = lrintf(m_inputSampleRate);
c@0 39 m_config.min = Pitch::getFrequencyForPitch
c@0 40 (m_minMIDIPitch, 0, m_tuningFrequency);
c@0 41 m_config.max = Pitch::getFrequencyForPitch
c@0 42 (m_maxMIDIPitch, 0, m_tuningFrequency);
c@0 43 m_config.BPO = m_bpo;
c@0 44 m_config.CQThresh = 0.0054;
c@0 45 m_config.isNormalised = m_normalized;
c@0 46
c@0 47 m_step = 0;
c@0 48 m_block = 0;
c@0 49 }
c@0 50
c@0 51 ChromagramPlugin::~ChromagramPlugin()
c@0 52 {
c@0 53 delete m_chromagram;
c@0 54 }
c@0 55
c@0 56 string
c@0 57 ChromagramPlugin::getName() const
c@0 58 {
c@9 59 return "qm-chromagram";
c@0 60 }
c@0 61
c@0 62 string
c@0 63 ChromagramPlugin::getDescription() const
c@0 64 {
c@0 65 return "Chromagram";
c@0 66 }
c@0 67
c@0 68 string
c@0 69 ChromagramPlugin::getMaker() const
c@0 70 {
c@5 71 return "Queen Mary, University of London";
c@0 72 }
c@0 73
c@0 74 int
c@0 75 ChromagramPlugin::getPluginVersion() const
c@0 76 {
c@0 77 return 2;
c@0 78 }
c@0 79
c@0 80 string
c@0 81 ChromagramPlugin::getCopyright() const
c@0 82 {
c@0 83 return "Copyright (c) 2006 - All Rights Reserved";
c@0 84 }
c@0 85
c@0 86 ChromagramPlugin::ParameterList
c@0 87 ChromagramPlugin::getParameterDescriptors() const
c@0 88 {
c@0 89 ParameterList list;
c@0 90
c@0 91 ParameterDescriptor desc;
c@0 92 desc.name = "minpitch";
c@0 93 desc.description = "Minimum Pitch";
c@0 94 desc.unit = "MIDI units";
c@0 95 desc.minValue = 0;
c@0 96 desc.maxValue = 127;
c@0 97 desc.defaultValue = 12;
c@0 98 desc.isQuantized = true;
c@0 99 desc.quantizeStep = 1;
c@0 100 list.push_back(desc);
c@0 101
c@0 102 desc.name = "maxpitch";
c@0 103 desc.description = "Maximum Pitch";
c@0 104 desc.unit = "MIDI units";
c@0 105 desc.minValue = 0;
c@0 106 desc.maxValue = 127;
c@0 107 desc.defaultValue = 96;
c@0 108 desc.isQuantized = true;
c@0 109 desc.quantizeStep = 1;
c@0 110 list.push_back(desc);
c@0 111
c@0 112 desc.name = "tuning";
c@0 113 desc.description = "Tuning Frequency";
c@0 114 desc.unit = "Hz";
c@0 115 desc.minValue = 420;
c@0 116 desc.maxValue = 460;
c@0 117 desc.defaultValue = 440;
c@0 118 desc.isQuantized = false;
c@0 119 list.push_back(desc);
c@0 120
c@0 121 desc.name = "bpo";
c@0 122 desc.description = "Bins per Octave";
c@0 123 desc.unit = "bins";
c@0 124 desc.minValue = 2;
c@0 125 desc.maxValue = 36;
c@0 126 desc.defaultValue = 12;
c@0 127 desc.isQuantized = true;
c@0 128 desc.quantizeStep = 1;
c@0 129 list.push_back(desc);
c@0 130
c@0 131 desc.name = "normalized";
c@0 132 desc.description = "Normalized";
c@0 133 desc.unit = "";
c@0 134 desc.minValue = 0;
c@0 135 desc.maxValue = 1;
c@0 136 desc.defaultValue = 1;
c@0 137 desc.isQuantized = true;
c@0 138 desc.quantizeStep = 1;
c@0 139 list.push_back(desc);
c@0 140
c@0 141 return list;
c@0 142 }
c@0 143
c@0 144 float
c@0 145 ChromagramPlugin::getParameter(std::string param) const
c@0 146 {
c@0 147 if (param == "minpitch") {
c@0 148 return m_minMIDIPitch;
c@0 149 }
c@0 150 if (param == "maxpitch") {
c@0 151 return m_maxMIDIPitch;
c@0 152 }
c@0 153 if (param == "tuning") {
c@0 154 return m_tuningFrequency;
c@0 155 }
c@0 156 if (param == "bpo") {
c@0 157 return m_bpo;
c@0 158 }
c@0 159 if (param == "normalized") {
c@0 160 return m_normalized;
c@0 161 }
c@0 162 std::cerr << "WARNING: ChromagramPlugin::getParameter: unknown parameter \""
c@0 163 << param << "\"" << std::endl;
c@0 164 return 0.0;
c@0 165 }
c@0 166
c@0 167 void
c@0 168 ChromagramPlugin::setParameter(std::string param, float value)
c@0 169 {
c@0 170 if (param == "minpitch") {
c@0 171 m_minMIDIPitch = lrintf(value);
c@0 172 } else if (param == "maxpitch") {
c@0 173 m_maxMIDIPitch = lrintf(value);
c@0 174 } else if (param == "tuning") {
c@0 175 m_tuningFrequency = value;
c@0 176 } else if (param == "bpo") {
c@0 177 m_bpo = lrintf(value);
c@0 178 } else if (param == "normalized") {
c@0 179 m_normalized = (value > 0.0001);
c@0 180 } else {
c@0 181 std::cerr << "WARNING: ChromagramPlugin::setParameter: unknown parameter \""
c@0 182 << param << "\"" << std::endl;
c@0 183 }
c@0 184
c@0 185 setupConfig();
c@0 186 }
c@0 187
c@0 188
c@0 189 bool
c@0 190 ChromagramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@0 191 {
c@0 192 if (m_chromagram) {
c@0 193 delete m_chromagram;
c@0 194 m_chromagram = 0;
c@0 195 }
c@0 196
c@0 197 if (channels < getMinChannelCount() ||
c@0 198 channels > getMaxChannelCount()) return false;
c@0 199
c@0 200 std::cerr << "ChromagramPlugin::initialise: step " << stepSize << ", block "
c@7 201 << blockSize << std::endl;
c@0 202
c@0 203 m_chromagram = new Chromagram(m_config);
c@13 204
c@13 205 m_step = m_chromagram->getHopSize();
c@13 206 m_block = m_chromagram->getFrameSize();
c@13 207
c@13 208 if (stepSize != m_step ||
c@13 209 blockSize != m_block) {
c@13 210 delete m_chromagram;
c@13 211 m_chromagram = 0;
c@13 212 return false;
c@13 213 }
c@13 214
c@0 215 return true;
c@0 216 }
c@0 217
c@0 218 void
c@0 219 ChromagramPlugin::reset()
c@0 220 {
c@0 221 if (m_chromagram) {
c@0 222 delete m_chromagram;
c@0 223 m_chromagram = new Chromagram(m_config);
c@0 224 }
c@0 225 }
c@0 226
c@0 227 size_t
c@0 228 ChromagramPlugin::getPreferredStepSize() const
c@0 229 {
c@0 230 if (!m_step) {
c@0 231 Chromagram chroma(m_config);
c@0 232 m_step = chroma.getHopSize();
c@0 233 m_block = chroma.getFrameSize();
c@0 234 }
c@0 235
c@0 236 return m_step;
c@0 237 }
c@0 238
c@0 239 size_t
c@0 240 ChromagramPlugin::getPreferredBlockSize() const
c@0 241 {
c@0 242 if (!m_block) {
c@0 243 Chromagram chroma(m_config);
c@0 244 m_step = chroma.getHopSize();
c@0 245 m_block = chroma.getFrameSize();
c@0 246 }
c@0 247
c@0 248 return m_block;
c@0 249 }
c@0 250
c@0 251 ChromagramPlugin::OutputList
c@0 252 ChromagramPlugin::getOutputDescriptors() const
c@0 253 {
c@0 254 OutputList list;
c@0 255
c@0 256 OutputDescriptor d;
c@0 257 d.name = "chromagram";
c@0 258 d.unit = "";
c@0 259 d.description = "Chromagram";
c@0 260 d.hasFixedBinCount = true;
c@0 261 d.binCount = m_config.BPO;
c@0 262
c@0 263 const char *names[] =
c@0 264 { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
c@0 265
c@0 266 if (d.binCount % 12 == 0) {
c@0 267 for (int i = 0; i < 12; ++i) {
c@0 268 int ipc = m_minMIDIPitch % 12;
c@0 269 int index = (i + ipc) % 12;
c@0 270 d.binNames.push_back(names[index]);
c@17 271 for (int j = 0; j < int(d.binCount) / 12 - 1; ++j) {
c@0 272 d.binNames.push_back("");
c@0 273 }
c@0 274 }
c@0 275 } else {
c@9 276 d.binNames.push_back(names[m_minMIDIPitch % 12]);
c@0 277 }
c@0 278
c@0 279 d.hasKnownExtents = m_normalized;
c@0 280 d.minValue = 0.0;
c@0 281 d.maxValue = (m_normalized ? 1.0 : 0.0);
c@0 282 d.isQuantized = false;
c@0 283 d.sampleType = OutputDescriptor::OneSamplePerStep;
c@0 284 list.push_back(d);
c@9 285
c@0 286 return list;
c@0 287 }
c@0 288
c@0 289 ChromagramPlugin::Feature
c@0 290 ChromagramPlugin::normalize(const Feature &feature)
c@0 291 {
c@0 292 float min = 0.0, max = 0.0;
c@0 293
c@0 294 for (size_t i = 0; i < feature.values.size(); ++i) {
c@0 295 if (i == 0 || feature.values[i] < min) min = feature.values[i];
c@0 296 if (i == 0 || feature.values[i] > max) max = feature.values[i];
c@0 297 }
c@0 298
c@0 299 if (max == 0.0 || max == min) return feature;
c@0 300
c@0 301 Feature normalized;
c@0 302 normalized.hasTimestamp = false;
c@0 303
c@0 304 for (size_t i = 0; i < feature.values.size(); ++i) {
c@0 305 normalized.values.push_back((feature.values[i] - min) / (max - min));
c@0 306 }
c@0 307
c@0 308 return normalized;
c@0 309 }
c@0 310
c@0 311 ChromagramPlugin::FeatureSet
c@0 312 ChromagramPlugin::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
c@0 313 {
c@0 314 if (!m_chromagram) {
c@0 315 cerr << "ERROR: ChromagramPlugin::process: "
c@0 316 << "Chromagram has not been initialised"
c@0 317 << endl;
c@0 318 return FeatureSet();
c@0 319 }
c@0 320
c@7 321 double *real = new double[m_block];
c@7 322 double *imag = new double[m_block];
c@7 323
c@7 324 for (size_t i = 0; i < m_block/2; ++i) {
c@7 325 real[i] = inputBuffers[0][i*2];
c@17 326 if (i > 0) real[m_block - i] = real[i];
c@7 327 imag[i] = inputBuffers[0][i*2+1];
c@17 328 if (i > 0) imag[m_block - i] = imag[i];
c@0 329 }
c@0 330
c@7 331 double *output = m_chromagram->process(real, imag);
c@7 332
c@7 333 delete[] real;
c@7 334 delete[] imag;
c@7 335
c@0 336 Feature feature;
c@0 337 feature.hasTimestamp = false;
c@0 338 for (size_t i = 0; i < m_config.BPO; ++i) {
c@16 339 double value = output[i];
c@16 340 if (isnan(value)) value = 0.0;
c@16 341 feature.values.push_back(value);
c@0 342 }
c@0 343 feature.label = "";
c@0 344
c@0 345 FeatureSet returnFeatures;
c@7 346 returnFeatures[0].push_back(feature);
c@0 347 return returnFeatures;
c@0 348 }
c@0 349
c@0 350 ChromagramPlugin::FeatureSet
c@0 351 ChromagramPlugin::getRemainingFeatures()
c@0 352 {
c@7 353 return FeatureSet();
c@0 354 }
c@0 355