c@9: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@9:
c@9: /*
c@9: QM Vamp Plugin Set
c@9:
c@9: 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@9: */
c@9:
c@9: #include "ConstantQSpectrogram.h"
c@9:
c@9: #include
c@9: #include
c@9:
c@9: using std::string;
c@9: using std::vector;
c@9: using std::cerr;
c@9: using std::endl;
c@9:
c@9: ConstantQSpectrogram::ConstantQSpectrogram(float inputSampleRate) :
c@9: Vamp::Plugin(inputSampleRate),
c@17: m_bins(1),
c@9: m_cq(0),
c@9: m_step(0),
c@17: m_block(0)
c@9: {
c@170: m_minMIDIPitch = 36;
c@76: m_maxMIDIPitch = 84;
c@9: m_tuningFrequency = 440;
c@63: m_normalized = false;
c@170: m_bpo = 12;
c@9:
c@9: setupConfig();
c@9: }
c@9:
c@9: void
c@9: ConstantQSpectrogram::setupConfig()
c@9: {
c@9: m_config.FS = lrintf(m_inputSampleRate);
c@9: m_config.min = Pitch::getFrequencyForPitch
c@9: (m_minMIDIPitch, 0, m_tuningFrequency);
c@9: m_config.max = Pitch::getFrequencyForPitch
c@9: (m_maxMIDIPitch, 0, m_tuningFrequency);
c@9: m_config.BPO = m_bpo;
c@9: m_config.CQThresh = 0.0054;
c@9:
c@9: m_step = 0;
c@9: m_block = 0;
c@9: }
c@9:
c@9: ConstantQSpectrogram::~ConstantQSpectrogram()
c@9: {
c@9: delete m_cq;
c@9: }
c@9:
c@9: string
c@22: ConstantQSpectrogram::getIdentifier() const
c@9: {
c@9: return "qm-constantq";
c@9: }
c@9:
c@9: string
c@22: ConstantQSpectrogram::getName() const
c@22: {
c@22: return "Constant-Q Spectrogram";
c@22: }
c@22:
c@22: string
c@9: ConstantQSpectrogram::getDescription() const
c@9: {
c@51: return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio";
c@9: }
c@9:
c@9: string
c@9: ConstantQSpectrogram::getMaker() const
c@9: {
c@9: return "Queen Mary, University of London";
c@9: }
c@9:
c@9: int
c@9: ConstantQSpectrogram::getPluginVersion() const
c@9: {
cannam@233: return 4;
c@9: }
c@9:
c@9: string
c@9: ConstantQSpectrogram::getCopyright() const
c@9: {
c@118: return "Plugin by Chris Cannam and Christian Landone. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
c@9: }
c@9:
c@9: ConstantQSpectrogram::ParameterList
c@9: ConstantQSpectrogram::getParameterDescriptors() const
c@9: {
c@9: ParameterList list;
c@9:
c@9: ParameterDescriptor desc;
c@22: desc.identifier = "minpitch";
c@22: desc.name = "Minimum Pitch";
c@9: desc.unit = "MIDI units";
c@52: desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the constant-Q transform";
c@9: desc.minValue = 0;
c@9: desc.maxValue = 127;
c@9: desc.defaultValue = 36;
c@9: desc.isQuantized = true;
c@9: desc.quantizeStep = 1;
c@9: list.push_back(desc);
c@9:
c@22: desc.identifier = "maxpitch";
c@22: desc.name = "Maximum Pitch";
c@9: desc.unit = "MIDI units";
c@52: desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform";
c@9: desc.minValue = 0;
c@9: desc.maxValue = 127;
c@9: desc.defaultValue = 84;
c@9: desc.isQuantized = true;
c@9: desc.quantizeStep = 1;
c@9: list.push_back(desc);
c@9:
c@22: desc.identifier = "tuning";
c@22: desc.name = "Tuning Frequency";
c@9: desc.unit = "Hz";
c@52: desc.description = "Frequency of concert A";
c@118: desc.minValue = 360;
c@118: desc.maxValue = 500;
c@9: desc.defaultValue = 440;
c@9: desc.isQuantized = false;
c@9: list.push_back(desc);
c@9:
c@22: desc.identifier = "bpo";
c@22: desc.name = "Bins per Octave";
c@9: desc.unit = "bins";
c@52: desc.description = "Number of constant-Q transform bins per octave";
c@9: desc.minValue = 2;
c@118: desc.maxValue = 480;
c@9: desc.defaultValue = 12;
c@9: desc.isQuantized = true;
c@9: desc.quantizeStep = 1;
c@9: list.push_back(desc);
c@9:
c@22: desc.identifier = "normalized";
c@22: desc.name = "Normalized";
c@9: desc.unit = "";
c@52: desc.description = "Whether to normalize each output column to unit maximum";
c@9: desc.minValue = 0;
c@9: desc.maxValue = 1;
c@51: desc.defaultValue = 0;
c@9: desc.isQuantized = true;
c@9: desc.quantizeStep = 1;
c@9: list.push_back(desc);
c@9:
c@9: return list;
c@9: }
c@9:
c@9: float
c@9: ConstantQSpectrogram::getParameter(std::string param) const
c@9: {
c@9: if (param == "minpitch") {
c@9: return m_minMIDIPitch;
c@9: }
c@9: if (param == "maxpitch") {
c@9: return m_maxMIDIPitch;
c@9: }
c@9: if (param == "tuning") {
c@9: return m_tuningFrequency;
c@9: }
c@9: if (param == "bpo") {
c@9: return m_bpo;
c@9: }
c@9: if (param == "normalized") {
c@9: return m_normalized;
c@9: }
c@9: std::cerr << "WARNING: ConstantQSpectrogram::getParameter: unknown parameter \""
c@9: << param << "\"" << std::endl;
c@9: return 0.0;
c@9: }
c@9:
c@9: void
c@9: ConstantQSpectrogram::setParameter(std::string param, float value)
c@9: {
c@9: if (param == "minpitch") {
c@9: m_minMIDIPitch = lrintf(value);
c@9: } else if (param == "maxpitch") {
c@9: m_maxMIDIPitch = lrintf(value);
c@9: } else if (param == "tuning") {
c@9: m_tuningFrequency = value;
c@9: } else if (param == "bpo") {
c@9: m_bpo = lrintf(value);
c@9: } else if (param == "normalized") {
c@9: m_normalized = (value > 0.0001);
c@9: } else {
c@9: std::cerr << "WARNING: ConstantQSpectrogram::setParameter: unknown parameter \""
c@9: << param << "\"" << std::endl;
c@9: }
c@9:
c@9: setupConfig();
c@9: }
c@9:
c@9:
c@9: bool
c@9: ConstantQSpectrogram::initialise(size_t channels, size_t stepSize, size_t blockSize)
c@9: {
c@9: if (m_cq) {
c@9: delete m_cq;
c@9: m_cq = 0;
c@9: }
c@9:
c@9: if (channels < getMinChannelCount() ||
c@9: channels > getMaxChannelCount()) return false;
c@9:
c@199: if (m_inputSampleRate > 384000) {
c@199: std::cerr << "ConstantQSpectrogram::initialise: Maximum input sample rate is 384000" << std::endl;
c@199: return false;
c@199: }
c@199:
c@24: setupConfig();
c@24:
c@9: m_cq = new ConstantQ(m_config);
c@50: m_bins = m_cq->getK();
c@9: m_cq->sparsekernel();
cannam@237: m_step = m_cq->getHop();
cannam@237: m_block = m_cq->getFFTLength();
c@13:
c@52: if (blockSize != m_block) {
c@52: std::cerr << "ConstantQSpectrogram::initialise: ERROR: supplied block size " << blockSize << " differs from required block size " << m_block << ", initialise failing" << std::endl;
c@13: delete m_cq;
c@13: m_cq = 0;
c@13: return false;
c@13: }
c@9:
c@52: if (stepSize != m_step) {
c@52: std::cerr << "ConstantQSpectrogram::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@9: return true;
c@9: }
c@9:
c@9: void
c@9: ConstantQSpectrogram::reset()
c@9: {
c@9: if (m_cq) {
c@9: delete m_cq;
c@9: m_cq = new ConstantQ(m_config);
c@83: m_bins = m_cq->getK();
c@83: m_cq->sparsekernel();
cannam@237: m_step = m_cq->getHop();
cannam@237: m_block = m_cq->getFFTLength();
c@9: }
c@9: }
c@9:
c@9: size_t
c@9: ConstantQSpectrogram::getPreferredStepSize() const
c@9: {
c@9: if (!m_step) {
c@9: ConstantQ cq(m_config);
cannam@237: m_step = cq.getHop();
cannam@237: m_block = cq.getFFTLength();
c@9: }
c@9:
c@9: return m_step;
c@9: }
c@9:
c@9: size_t
c@9: ConstantQSpectrogram::getPreferredBlockSize() const
c@9: {
c@9: if (!m_block) {
c@9: ConstantQ cq(m_config);
cannam@237: m_step = cq.getHop();
cannam@237: m_block = cq.getFFTLength();
c@9: }
c@9:
c@9: return m_block;
c@9: }
c@9:
c@9: ConstantQSpectrogram::OutputList
c@9: ConstantQSpectrogram::getOutputDescriptors() const
c@9: {
c@9: OutputList list;
c@9:
c@9: OutputDescriptor d;
c@22: d.identifier = "constantq";
c@22: d.name = "Constant-Q Spectrogram";
c@9: d.unit = "";
c@52: d.description = "Output of constant-Q transform, as a single vector per process block";
c@9: d.hasFixedBinCount = true;
c@9: d.binCount = m_bins;
c@9:
c@95: // std::cerr << "Bin count " << d.binCount << std::endl;
c@9:
c@9: const char *names[] =
c@9: { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };
c@9:
c@9: if (m_bpo == 12) {
c@17: for (int i = 0; i < int(d.binCount); ++i) {
c@9: int ipc = m_minMIDIPitch % 12;
c@9: int index = (i + ipc) % 12;
c@9: d.binNames.push_back(names[index]);
c@9: }
c@9: } else {
c@9: d.binNames.push_back(names[m_minMIDIPitch % 12]);
c@9: }
c@9:
c@9: d.hasKnownExtents = m_normalized;
c@9: d.minValue = 0.0;
c@9: d.maxValue = (m_normalized ? 1.0 : 0.0);
c@9: d.isQuantized = false;
c@9: d.sampleType = OutputDescriptor::OneSamplePerStep;
c@9: list.push_back(d);
c@9:
c@9: return list;
c@9: }
c@9:
c@9: ConstantQSpectrogram::Feature
c@9: ConstantQSpectrogram::normalize(const Feature &feature)
c@9: {
c@9: float min = 0.0, max = 0.0;
c@9:
c@9: for (size_t i = 0; i < feature.values.size(); ++i) {
c@9: if (i == 0 || feature.values[i] < min) min = feature.values[i];
c@9: if (i == 0 || feature.values[i] > max) max = feature.values[i];
c@9: }
c@9:
c@9: if (max == 0.0 || max == min) return feature;
c@9:
c@9: Feature normalized;
c@9: normalized.hasTimestamp = false;
c@9:
c@9: for (size_t i = 0; i < feature.values.size(); ++i) {
c@9: normalized.values.push_back((feature.values[i] - min) / (max - min));
c@9: }
c@9:
c@9: return normalized;
c@9: }
c@9:
c@9: ConstantQSpectrogram::FeatureSet
c@18: ConstantQSpectrogram::process(const float *const *inputBuffers,
c@18: Vamp::RealTime /* timestamp */)
c@9: {
c@9: if (!m_cq) {
c@9: cerr << "ERROR: ConstantQSpectrogram::process: "
c@9: << "Constant-Q has not been initialised"
c@9: << endl;
c@9: return FeatureSet();
c@9: }
c@9:
c@9: double *real = new double[m_block];
c@9: double *imag = new double[m_block];
c@9: double *cqre = new double[m_bins];
c@9: double *cqim = new double[m_bins];
c@9:
c@83: // std::cout << "in:" << std::endl;
c@75: for (size_t i = 0; i <= m_block/2; ++i) {
c@9: real[i] = inputBuffers[0][i*2];
c@17: if (i > 0) real[m_block - i] = real[i];
c@9: imag[i] = inputBuffers[0][i*2+1];
c@96: if (i > 0) imag[m_block - i] = imag[i]; //!!! huh? surely -imag[i] ?
c@83: // std::cout << real[i] << "," << imag[i] << " ";
c@9: }
c@9:
c@9: m_cq->process(real, imag, cqre, cqim);
c@9:
c@9: delete[] real;
c@9: delete[] imag;
c@9:
c@83: // std::cout << "\nout:" << std::endl;
c@9: Feature feature;
c@9: feature.hasTimestamp = false;
c@17: for (int i = 0; i < m_bins; ++i) {
c@16: double re = cqre[i];
c@16: double im = cqim[i];
c@83: // std::cout << re << "," << im << ":";
c@130: if (ISNAN(re)) re = 0.0;
c@130: if (ISNAN(im)) im = 0.0;
c@16: double value = sqrt(re * re + im * im);
c@83: // std::cout << value << " ";
c@16: feature.values.push_back(value);
c@9: }
c@9: feature.label = "";
c@9:
c@9: delete[] cqre;
c@9: delete[] cqim;
c@9:
c@9: FeatureSet returnFeatures;
c@9: if (m_normalized) returnFeatures[0].push_back(normalize(feature));
c@9: else returnFeatures[0].push_back(feature);
c@9: return returnFeatures;
c@9: }
c@9:
c@9: ConstantQSpectrogram::FeatureSet
c@9: ConstantQSpectrogram::getRemainingFeatures()
c@9: {
c@9: return FeatureSet();
c@9: }
c@9: