1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
15 #include "ChromagramPlugin.h"
17 #include <base/Pitch.h>
18 #include <dsp/chromagram/Chromagram.h>
25 ChromagramPlugin::ChromagramPlugin(float inputSampleRate) :
26 Vamp::Plugin(inputSampleRate),
33 m_tuningFrequency = 440;
34 m_normalise = MathUtilities::NormaliseNone;
41 ChromagramPlugin::setupConfig()
43 m_config.FS = lrintf(m_inputSampleRate);
44 m_config.min = Pitch::getFrequencyForPitch
45 (m_minMIDIPitch, 0, m_tuningFrequency);
46 m_config.max = Pitch::getFrequencyForPitch
47 (m_maxMIDIPitch, 0, m_tuningFrequency);
49 m_config.CQThresh = 0.0054;
50 m_config.normalise = m_normalise;
56 ChromagramPlugin::~ChromagramPlugin()
62 ChromagramPlugin::getIdentifier() const
64 return "qm-chromagram";
68 ChromagramPlugin::getName() const
74 ChromagramPlugin::getDescription() const
76 return "Extract a series of tonal chroma vectors from the audio";
80 ChromagramPlugin::getMaker() const
82 return "Queen Mary, University of London";
86 ChromagramPlugin::getPluginVersion() const
92 ChromagramPlugin::getCopyright() const
94 return "Plugin by Chris Cannam and Christian Landone. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
97 ChromagramPlugin::ParameterList
98 ChromagramPlugin::getParameterDescriptors() const
102 ParameterDescriptor desc;
103 desc.identifier = "minpitch";
104 desc.name = "Minimum Pitch";
105 desc.unit = "MIDI units";
106 desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the chromagram";
109 desc.defaultValue = 36;
110 desc.isQuantized = true;
111 desc.quantizeStep = 1;
112 list.push_back(desc);
114 desc.identifier = "maxpitch";
115 desc.name = "Maximum Pitch";
116 desc.unit = "MIDI units";
117 desc.description = "MIDI pitch corresponding to the highest frequency to be included in the chromagram";
120 desc.defaultValue = 96;
121 desc.isQuantized = true;
122 desc.quantizeStep = 1;
123 list.push_back(desc);
125 desc.identifier = "tuning";
126 desc.name = "Tuning Frequency";
128 desc.description = "Frequency of concert A";
131 desc.defaultValue = 440;
132 desc.isQuantized = false;
133 list.push_back(desc);
135 desc.identifier = "bpo";
136 desc.name = "Bins per Octave";
138 desc.description = "Number of constant-Q transform bins per octave, and the number of bins for the chromagram outputs";
141 desc.defaultValue = 12;
142 desc.isQuantized = true;
143 desc.quantizeStep = 1;
144 list.push_back(desc);
146 desc.identifier = "normalization";
147 desc.name = "Normalization";
149 desc.description = "Normalization for each chromagram output column";
152 desc.defaultValue = 0;
153 desc.isQuantized = true;
154 desc.quantizeStep = 1;
155 desc.valueNames.push_back("None");
156 desc.valueNames.push_back("Unit Sum");
157 desc.valueNames.push_back("Unit Maximum");
158 list.push_back(desc);
164 ChromagramPlugin::getParameter(std::string param) const
166 if (param == "minpitch") {
167 return m_minMIDIPitch;
169 if (param == "maxpitch") {
170 return m_maxMIDIPitch;
172 if (param == "tuning") {
173 return m_tuningFrequency;
175 if (param == "bpo") {
178 if (param == "normalization") {
179 return int(m_normalise);
181 std::cerr << "WARNING: ChromagramPlugin::getParameter: unknown parameter \""
182 << param << "\"" << std::endl;
187 ChromagramPlugin::setParameter(std::string param, float value)
189 if (param == "minpitch") {
190 m_minMIDIPitch = lrintf(value);
191 } else if (param == "maxpitch") {
192 m_maxMIDIPitch = lrintf(value);
193 } else if (param == "tuning") {
194 m_tuningFrequency = value;
195 } else if (param == "bpo") {
196 m_bpo = lrintf(value);
197 } else if (param == "normalization") {
198 m_normalise = MathUtilities::NormaliseType(int(value + 0.0001));
200 std::cerr << "WARNING: ChromagramPlugin::setParameter: unknown parameter \""
201 << param << "\"" << std::endl;
209 ChromagramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize)
216 if (channels < getMinChannelCount() ||
217 channels > getMaxChannelCount()) return false;
219 m_chromagram = new Chromagram(m_config);
220 m_binsums = vector<double>(m_config.BPO);
222 for (int i = 0; i < m_config.BPO; ++i) {
228 m_step = m_chromagram->getHopSize();
229 m_block = m_chromagram->getFrameSize();
230 if (m_step < 1) m_step = 1;
232 if (blockSize != m_block) {
233 std::cerr << "ChromagramPlugin::initialise: ERROR: supplied block size " << blockSize << " differs from required block size " << m_block << ", initialise failing" << std::endl;
239 if (stepSize != m_step) {
240 std::cerr << "ChromagramPlugin::initialise: NOTE: supplied step size " << stepSize << " differs from expected step size " << m_step << " (for block size = " << m_block << ")" << std::endl;
247 ChromagramPlugin::reset()
251 m_chromagram = new Chromagram(m_config);
252 for (int i = 0; i < m_config.BPO; ++i) {
260 ChromagramPlugin::getPreferredStepSize() const
263 Chromagram chroma(m_config);
264 m_step = chroma.getHopSize();
265 m_block = chroma.getFrameSize();
266 if (m_step < 1) m_step = 1;
273 ChromagramPlugin::getPreferredBlockSize() const
276 Chromagram chroma(m_config);
277 m_step = chroma.getHopSize();
278 m_block = chroma.getFrameSize();
279 if (m_step < 1) m_step = 1;
285 ChromagramPlugin::OutputList
286 ChromagramPlugin::getOutputDescriptors() const
291 d.identifier = "chromagram";
292 d.name = "Chromagram";
294 d.description = "Output of chromagram, as a single vector per process block";
295 d.hasFixedBinCount = true;
296 d.binCount = m_config.BPO;
298 const char *names[] =
299 { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
301 if (d.binCount % 12 == 0) {
302 for (int i = 0; i < 12; ++i) {
303 int ipc = m_minMIDIPitch % 12;
304 int index = (i + ipc) % 12;
305 d.binNames.push_back(names[index]);
306 for (int j = 0; j < int(d.binCount) / 12 - 1; ++j) {
307 d.binNames.push_back("");
311 d.binNames.push_back(names[m_minMIDIPitch % 12]);
314 d.hasKnownExtents = (m_normalise != MathUtilities::NormaliseNone);
316 d.maxValue = (d.hasKnownExtents ? 1.0 : 0.0);
317 d.isQuantized = false;
318 d.sampleType = OutputDescriptor::OneSamplePerStep;
321 d.identifier = "chromameans";
322 d.name = "Chroma Means";
323 d.description = "Mean values of chromagram bins across the duration of the input audio";
324 d.sampleType = OutputDescriptor::FixedSampleRate;
331 ChromagramPlugin::FeatureSet
332 ChromagramPlugin::process(const float *const *inputBuffers,
333 Vamp::RealTime timestamp)
336 cerr << "ERROR: ChromagramPlugin::process: "
337 << "Chromagram has not been initialised"
342 double *real = new double[m_block];
343 double *imag = new double[m_block];
345 for (size_t i = 0; i <= m_block/2; ++i) {
346 real[i] = inputBuffers[0][i*2];
347 if (i > 0) real[m_block - i] = real[i];
348 imag[i] = inputBuffers[0][i*2+1];
349 if (i > 0) imag[m_block - i] = imag[i];
352 // cerr << "chromagram: timestamp = " << timestamp << endl;
354 bool printThis = false;
356 if (timestamp.sec == 3 && timestamp.nsec < 250000000) {
360 cerr << "\n\nchromagram: timestamp " << timestamp << ": input data starts:" << endl;
361 for (int i = 0; i < m_block && i < 1000; ++i) {
362 cerr << real[i] << "," << imag[i] << " ";
364 cerr << endl << "values:" << endl;
367 double *output = m_chromagram->process(real, imag);
373 feature.hasTimestamp = false;
374 for (size_t i = 0; i < m_config.BPO; ++i) {
375 double value = output[i];
378 cerr << value << " ";
381 if (ISNAN(value)) value = 0.0;
382 m_binsums[i] += value;
383 feature.values.push_back(value);
393 FeatureSet returnFeatures;
394 returnFeatures[0].push_back(feature);
395 return returnFeatures;
398 ChromagramPlugin::FeatureSet
399 ChromagramPlugin::getRemainingFeatures()
402 feature.hasTimestamp = true;
403 feature.timestamp = Vamp::RealTime::zeroTime;
405 for (size_t i = 0; i < m_config.BPO; ++i) {
406 double v = m_binsums[i];
407 if (m_count > 0) v /= m_count;
408 feature.values.push_back(v);
410 feature.label = "Chromagram bin means";
412 FeatureSet returnFeatures;
413 returnFeatures[1].push_back(feature);
414 return returnFeatures;