c@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@0: c@0: /* c@0: QM Vamp Plugin Set c@0: c@0: Centre for Digital Music, Queen Mary, University of London. c@135: c@135: This program is free software; you can redistribute it and/or c@135: modify it under the terms of the GNU General Public License as c@135: published by the Free Software Foundation; either version 2 of the c@135: License, or (at your option) any later version. See the file c@135: COPYING included with this distribution for more information. c@0: */ c@0: c@0: #include "ChromagramPlugin.h" c@0: c@3: #include c@3: #include c@0: c@0: using std::string; c@0: using std::vector; c@0: using std::cerr; c@0: using std::endl; c@0: c@0: ChromagramPlugin::ChromagramPlugin(float inputSampleRate) : c@0: Vamp::Plugin(inputSampleRate), c@0: m_chromagram(0), c@0: m_step(0), c@7: m_block(0) c@0: { c@118: m_minMIDIPitch = 36; c@0: m_maxMIDIPitch = 96; c@0: m_tuningFrequency = 440; c@63: m_normalise = MathUtilities::NormaliseNone; c@0: m_bpo = 12; c@0: c@0: setupConfig(); c@0: } c@0: c@0: void c@0: ChromagramPlugin::setupConfig() c@0: { c@0: m_config.FS = lrintf(m_inputSampleRate); c@0: m_config.min = Pitch::getFrequencyForPitch c@0: (m_minMIDIPitch, 0, m_tuningFrequency); c@0: m_config.max = Pitch::getFrequencyForPitch c@0: (m_maxMIDIPitch, 0, m_tuningFrequency); c@0: m_config.BPO = m_bpo; c@0: m_config.CQThresh = 0.0054; c@49: m_config.normalise = m_normalise; c@0: c@0: m_step = 0; c@0: m_block = 0; c@0: } c@0: c@0: ChromagramPlugin::~ChromagramPlugin() c@0: { c@0: delete m_chromagram; c@0: } c@0: c@0: string c@22: ChromagramPlugin::getIdentifier() const c@0: { c@9: return "qm-chromagram"; c@0: } c@0: c@0: string c@22: ChromagramPlugin::getName() const c@22: { c@22: return "Chromagram"; c@22: } c@22: c@22: string c@0: ChromagramPlugin::getDescription() const c@0: { c@50: return "Extract a series of tonal chroma vectors from the audio"; c@0: } c@0: c@0: string c@0: ChromagramPlugin::getMaker() const c@0: { c@5: return "Queen Mary, University of London"; c@0: } c@0: c@0: int c@0: ChromagramPlugin::getPluginVersion() const c@0: { cannam@233: return 5; c@0: } c@0: c@0: string c@0: ChromagramPlugin::getCopyright() const c@0: { c@118: return "Plugin by Chris Cannam and Christian Landone. Copyright (c) 2006-2009 QMUL - All Rights Reserved"; c@0: } c@0: c@0: ChromagramPlugin::ParameterList c@0: ChromagramPlugin::getParameterDescriptors() const c@0: { c@0: ParameterList list; c@0: c@0: ParameterDescriptor desc; c@22: desc.identifier = "minpitch"; c@22: desc.name = "Minimum Pitch"; c@0: desc.unit = "MIDI units"; c@52: desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the chromagram"; c@0: desc.minValue = 0; c@0: desc.maxValue = 127; c@118: desc.defaultValue = 36; c@0: desc.isQuantized = true; c@0: desc.quantizeStep = 1; c@0: list.push_back(desc); c@0: c@22: desc.identifier = "maxpitch"; c@22: desc.name = "Maximum Pitch"; c@0: desc.unit = "MIDI units"; c@52: desc.description = "MIDI pitch corresponding to the highest frequency to be included in the chromagram"; c@0: desc.minValue = 0; c@0: desc.maxValue = 127; c@0: desc.defaultValue = 96; c@0: desc.isQuantized = true; c@0: desc.quantizeStep = 1; c@0: list.push_back(desc); c@0: c@22: desc.identifier = "tuning"; c@22: desc.name = "Tuning Frequency"; c@0: desc.unit = "Hz"; c@52: desc.description = "Frequency of concert A"; c@94: desc.minValue = 360; c@94: desc.maxValue = 500; c@0: desc.defaultValue = 440; c@0: desc.isQuantized = false; c@0: list.push_back(desc); c@0: c@22: desc.identifier = "bpo"; c@22: desc.name = "Bins per Octave"; c@0: desc.unit = "bins"; c@52: desc.description = "Number of constant-Q transform bins per octave, and the number of bins for the chromagram outputs"; c@0: desc.minValue = 2; c@118: desc.maxValue = 480; c@0: desc.defaultValue = 12; c@0: desc.isQuantized = true; c@0: desc.quantizeStep = 1; c@0: list.push_back(desc); c@0: c@49: desc.identifier = "normalization"; c@49: desc.name = "Normalization"; c@0: desc.unit = ""; c@52: desc.description = "Normalization for each chromagram output column"; c@0: desc.minValue = 0; c@49: desc.maxValue = 2; c@63: desc.defaultValue = 0; c@0: desc.isQuantized = true; c@0: desc.quantizeStep = 1; c@49: desc.valueNames.push_back("None"); c@49: desc.valueNames.push_back("Unit Sum"); c@49: desc.valueNames.push_back("Unit Maximum"); c@0: list.push_back(desc); c@0: c@0: return list; c@0: } c@0: c@0: float c@0: ChromagramPlugin::getParameter(std::string param) const c@0: { c@0: if (param == "minpitch") { c@0: return m_minMIDIPitch; c@0: } c@0: if (param == "maxpitch") { c@0: return m_maxMIDIPitch; c@0: } c@0: if (param == "tuning") { c@0: return m_tuningFrequency; c@0: } c@0: if (param == "bpo") { c@0: return m_bpo; c@0: } c@49: if (param == "normalization") { c@49: return int(m_normalise); c@0: } c@0: std::cerr << "WARNING: ChromagramPlugin::getParameter: unknown parameter \"" c@0: << param << "\"" << std::endl; c@0: return 0.0; c@0: } c@0: c@0: void c@0: ChromagramPlugin::setParameter(std::string param, float value) c@0: { c@0: if (param == "minpitch") { c@0: m_minMIDIPitch = lrintf(value); c@0: } else if (param == "maxpitch") { c@0: m_maxMIDIPitch = lrintf(value); c@0: } else if (param == "tuning") { c@0: m_tuningFrequency = value; c@0: } else if (param == "bpo") { c@0: m_bpo = lrintf(value); c@49: } else if (param == "normalization") { c@49: m_normalise = MathUtilities::NormaliseType(int(value + 0.0001)); c@0: } else { c@0: std::cerr << "WARNING: ChromagramPlugin::setParameter: unknown parameter \"" c@0: << param << "\"" << std::endl; c@0: } c@0: c@0: setupConfig(); c@0: } c@0: c@0: c@0: bool c@0: ChromagramPlugin::initialise(size_t channels, size_t stepSize, size_t blockSize) c@0: { c@0: if (m_chromagram) { c@0: delete m_chromagram; c@0: m_chromagram = 0; c@0: } c@0: c@0: if (channels < getMinChannelCount() || c@0: channels > getMaxChannelCount()) return false; c@0: c@199: if (m_inputSampleRate > 384000) { c@199: std::cerr << "ChromagramPlugin::initialise: Maximum input sample rate is 384000" << std::endl; c@199: return false; c@199: } c@199: c@0: m_chromagram = new Chromagram(m_config); c@45: m_binsums = vector(m_config.BPO); c@45: c@45: for (int i = 0; i < m_config.BPO; ++i) { c@45: m_binsums[i] = 0.0; c@45: } c@45: c@45: m_count = 0; c@13: c@13: m_step = m_chromagram->getHopSize(); c@13: m_block = m_chromagram->getFrameSize(); c@95: if (m_step < 1) m_step = 1; c@13: c@52: if (blockSize != m_block) { c@52: std::cerr << "ChromagramPlugin::initialise: ERROR: supplied block size " << blockSize << " differs from required block size " << m_block << ", initialise failing" << std::endl; c@13: delete m_chromagram; c@13: m_chromagram = 0; c@13: return false; c@13: } c@13: c@52: if (stepSize != m_step) { c@52: std::cerr << "ChromagramPlugin::initialise: NOTE: supplied step size " << stepSize << " differs from expected step size " << m_step << " (for block size = " << m_block << ")" << std::endl; c@52: } c@52: c@0: return true; c@0: } c@0: c@0: void c@0: ChromagramPlugin::reset() c@0: { c@0: if (m_chromagram) { c@0: delete m_chromagram; c@0: m_chromagram = new Chromagram(m_config); c@83: for (int i = 0; i < m_config.BPO; ++i) { c@83: m_binsums[i] = 0.0; c@83: } c@83: m_count = 0; c@0: } c@0: } c@0: c@0: size_t c@0: ChromagramPlugin::getPreferredStepSize() const c@0: { c@0: if (!m_step) { c@0: Chromagram chroma(m_config); c@0: m_step = chroma.getHopSize(); c@0: m_block = chroma.getFrameSize(); c@95: if (m_step < 1) m_step = 1; c@0: } c@0: c@0: return m_step; c@0: } c@0: c@0: size_t c@0: ChromagramPlugin::getPreferredBlockSize() const c@0: { c@0: if (!m_block) { c@0: Chromagram chroma(m_config); c@0: m_step = chroma.getHopSize(); c@0: m_block = chroma.getFrameSize(); c@95: if (m_step < 1) m_step = 1; c@0: } c@0: c@0: return m_block; c@0: } c@0: c@0: ChromagramPlugin::OutputList c@0: ChromagramPlugin::getOutputDescriptors() const c@0: { c@0: OutputList list; c@0: c@0: OutputDescriptor d; c@22: d.identifier = "chromagram"; c@22: d.name = "Chromagram"; c@0: d.unit = ""; c@52: d.description = "Output of chromagram, as a single vector per process block"; c@0: d.hasFixedBinCount = true; c@0: d.binCount = m_config.BPO; c@0: c@0: const char *names[] = c@0: { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" }; c@0: c@0: if (d.binCount % 12 == 0) { c@0: for (int i = 0; i < 12; ++i) { c@0: int ipc = m_minMIDIPitch % 12; c@0: int index = (i + ipc) % 12; c@0: d.binNames.push_back(names[index]); c@17: for (int j = 0; j < int(d.binCount) / 12 - 1; ++j) { c@0: d.binNames.push_back(""); c@0: } c@0: } c@0: } else { c@9: d.binNames.push_back(names[m_minMIDIPitch % 12]); c@0: } c@0: c@49: d.hasKnownExtents = (m_normalise != MathUtilities::NormaliseNone); c@0: d.minValue = 0.0; c@49: d.maxValue = (d.hasKnownExtents ? 1.0 : 0.0); c@0: d.isQuantized = false; c@0: d.sampleType = OutputDescriptor::OneSamplePerStep; c@0: list.push_back(d); c@9: c@45: d.identifier = "chromameans"; c@45: d.name = "Chroma Means"; c@52: d.description = "Mean values of chromagram bins across the duration of the input audio"; c@45: d.sampleType = OutputDescriptor::FixedSampleRate; c@45: d.sampleRate = 1; c@45: list.push_back(d); c@45: c@0: return list; c@0: } c@0: c@0: ChromagramPlugin::FeatureSet c@18: ChromagramPlugin::process(const float *const *inputBuffers, c@178: Vamp::RealTime ) c@0: { c@0: if (!m_chromagram) { c@0: cerr << "ERROR: ChromagramPlugin::process: " c@0: << "Chromagram has not been initialised" c@0: << endl; c@0: return FeatureSet(); c@0: } c@0: c@7: double *real = new double[m_block]; c@7: double *imag = new double[m_block]; c@7: c@75: for (size_t i = 0; i <= m_block/2; ++i) { c@7: real[i] = inputBuffers[0][i*2]; c@17: if (i > 0) real[m_block - i] = real[i]; c@7: imag[i] = inputBuffers[0][i*2+1]; c@17: if (i > 0) imag[m_block - i] = imag[i]; c@0: } c@0: c@75: // cerr << "chromagram: timestamp = " << timestamp << endl; c@75: /* c@75: bool printThis = false; c@75: c@75: if (timestamp.sec == 3 && timestamp.nsec < 250000000) { c@75: printThis = true; c@75: } c@75: if (printThis) { c@75: cerr << "\n\nchromagram: timestamp " << timestamp << ": input data starts:" << endl; c@75: for (int i = 0; i < m_block && i < 1000; ++i) { c@75: cerr << real[i] << "," << imag[i] << " "; c@75: } c@75: cerr << endl << "values:" << endl; c@75: } c@75: */ c@7: double *output = m_chromagram->process(real, imag); c@7: c@7: delete[] real; c@7: delete[] imag; c@7: c@0: Feature feature; c@0: feature.hasTimestamp = false; c@178: for (int i = 0; i < m_config.BPO; ++i) { c@16: double value = output[i]; c@75: /* c@75: if (printThis) { c@75: cerr << value << " "; c@75: } c@75: */ c@130: if (ISNAN(value)) value = 0.0; c@45: m_binsums[i] += value; c@16: feature.values.push_back(value); c@0: } c@0: feature.label = ""; c@45: ++m_count; c@75: /* c@75: if (printThis) { c@75: cerr << endl; c@75: } c@75: */ c@0: c@0: FeatureSet returnFeatures; c@7: returnFeatures[0].push_back(feature); c@0: return returnFeatures; c@0: } c@0: c@0: ChromagramPlugin::FeatureSet c@0: ChromagramPlugin::getRemainingFeatures() c@0: { c@45: Feature feature; c@45: feature.hasTimestamp = true; c@45: feature.timestamp = Vamp::RealTime::zeroTime; c@45: c@178: for (int i = 0; i < m_config.BPO; ++i) { c@45: double v = m_binsums[i]; c@45: if (m_count > 0) v /= m_count; c@45: feature.values.push_back(v); c@45: } c@45: feature.label = "Chromagram bin means"; c@45: c@45: FeatureSet returnFeatures; c@45: returnFeatures[1].push_back(feature); c@45: return returnFeatures; c@0: } c@0: