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@9: All rights reserved. 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@9: m_cq(0), c@9: m_step(0), c@9: m_block(0), c@9: m_bins(1) c@9: { c@9: m_minMIDIPitch = 12; c@9: m_maxMIDIPitch = 96; c@9: m_tuningFrequency = 440; c@9: m_normalized = true; c@9: 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@9: ConstantQSpectrogram::getName() const c@9: { c@9: return "qm-constantq"; c@9: } c@9: c@9: string c@9: ConstantQSpectrogram::getDescription() const c@9: { c@9: return "Constant-Q Spectrogram"; 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: { c@9: return 1; c@9: } c@9: c@9: string c@9: ConstantQSpectrogram::getCopyright() const c@9: { c@9: return "Copyright (c) 2006 - 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@9: desc.name = "minpitch"; c@9: desc.description = "Minimum Pitch"; c@9: desc.unit = "MIDI units"; 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@9: desc.name = "maxpitch"; c@9: desc.description = "Maximum Pitch"; c@9: desc.unit = "MIDI units"; 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@9: desc.name = "tuning"; c@9: desc.description = "Tuning Frequency"; c@9: desc.unit = "Hz"; c@9: desc.minValue = 420; c@9: desc.maxValue = 460; c@9: desc.defaultValue = 440; c@9: desc.isQuantized = false; c@9: list.push_back(desc); c@9: c@9: desc.name = "bpo"; c@9: desc.description = "Bins per Octave"; c@9: desc.unit = "bins"; c@9: desc.minValue = 2; c@9: desc.maxValue = 36; c@9: desc.defaultValue = 12; c@9: desc.isQuantized = true; c@9: desc.quantizeStep = 1; c@9: list.push_back(desc); c@9: c@9: desc.name = "normalized"; c@9: desc.description = "Normalized"; c@9: desc.unit = ""; c@9: desc.minValue = 0; c@9: desc.maxValue = 1; c@9: desc.defaultValue = 1; 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@9: if (stepSize != m_step) return false; c@9: if (blockSize != m_block) return false; c@9: c@9: std::cerr << "ConstantQSpectrogram::initialise: step " << stepSize << ", block " c@9: << blockSize << std::endl; c@9: c@9: m_cq = new ConstantQ(m_config); c@9: m_bins = (int)ceil(m_bpo * log(m_config.max / m_config.min) / log(2.0)); c@9: m_cq->sparsekernel(); c@9: 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@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); c@9: m_step = cq.gethop(); c@9: 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); c@9: m_step = cq.gethop(); c@9: 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@9: d.name = "constantq"; c@9: d.unit = ""; c@9: d.description = "Constant-Q Spectrogram"; c@9: d.hasFixedBinCount = true; c@9: d.binCount = m_bins; c@9: c@9: 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@9: for (int i = 0; i < 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@9: ConstantQSpectrogram::process(float **inputBuffers, 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@9: for (size_t i = 0; i < m_block/2; ++i) { c@9: real[i] = inputBuffers[0][i*2]; c@9: real[m_block - i] = real[i]; c@9: imag[i] = inputBuffers[0][i*2+1]; c@9: imag[m_block - 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@9: Feature feature; c@9: feature.hasTimestamp = false; c@9: for (size_t i = 0; i < m_bins; ++i) { c@9: feature.values.push_back(sqrt(cqre[i] * cqre[i] + c@9: cqim[i] * cqim[i])); 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: