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@0: All rights reserved.
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@0: m_block(0),
c@0: m_stepDelay(0)
c@0: {
c@0: m_minMIDIPitch = 12;
c@0: m_maxMIDIPitch = 96;
c@0: m_tuningFrequency = 440;
c@0: m_normalized = true;
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@0: m_config.isNormalised = m_normalized;
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@0: ChromagramPlugin::getName() const
c@0: {
c@0: return "chromagram";
c@0: }
c@0:
c@0: string
c@0: ChromagramPlugin::getDescription() const
c@0: {
c@0: return "Chromagram";
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: {
c@0: return 2;
c@0: }
c@0:
c@0: string
c@0: ChromagramPlugin::getCopyright() const
c@0: {
c@0: return "Copyright (c) 2006 - 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@0: desc.name = "minpitch";
c@0: desc.description = "Minimum Pitch";
c@0: desc.unit = "MIDI units";
c@0: desc.minValue = 0;
c@0: desc.maxValue = 127;
c@0: desc.defaultValue = 12;
c@0: desc.isQuantized = true;
c@0: desc.quantizeStep = 1;
c@0: list.push_back(desc);
c@0:
c@0: desc.name = "maxpitch";
c@0: desc.description = "Maximum Pitch";
c@0: desc.unit = "MIDI units";
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@0: desc.name = "tuning";
c@0: desc.description = "Tuning Frequency";
c@0: desc.unit = "Hz";
c@0: desc.minValue = 420;
c@0: desc.maxValue = 460;
c@0: desc.defaultValue = 440;
c@0: desc.isQuantized = false;
c@0: list.push_back(desc);
c@0:
c@0: desc.name = "bpo";
c@0: desc.description = "Bins per Octave";
c@0: desc.unit = "bins";
c@0: desc.minValue = 2;
c@0: desc.maxValue = 36;
c@0: desc.defaultValue = 12;
c@0: desc.isQuantized = true;
c@0: desc.quantizeStep = 1;
c@0: list.push_back(desc);
c@0:
c@0: desc.name = "normalized";
c@0: desc.description = "Normalized";
c@0: desc.unit = "";
c@0: desc.minValue = 0;
c@0: desc.maxValue = 1;
c@0: desc.defaultValue = 1;
c@0: desc.isQuantized = true;
c@0: desc.quantizeStep = 1;
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@0: if (param == "normalized") {
c@0: return m_normalized;
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@0: } else if (param == "normalized") {
c@0: m_normalized = (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@0: if (stepSize != m_step) return false;
c@0: if (blockSize != m_block) return false;
c@0:
c@0: // m_stepDelay = (blockSize - stepSize) / 2;
c@0: // m_stepDelay = m_stepDelay / stepSize;
c@0: m_stepDelay = (blockSize - stepSize) / stepSize; //!!! why? seems about right to look at, but...
c@0:
c@0: std::cerr << "ChromagramPlugin::initialise: step " << stepSize << ", block "
c@0: << blockSize << ", delay " << m_stepDelay << std::endl;
c@0:
c@0: m_chromagram = new Chromagram(m_config);
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@0: }
c@0: while (!m_pending.empty()) m_pending.pop();
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@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@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@0: // d.name = "unnormalized";
c@0: d.name = "chromagram";
c@0: d.unit = "";
c@0: d.description = "Chromagram";
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@0: for (int j = 0; j < d.binCount / 12 - 1; ++j) {
c@0: d.binNames.push_back("");
c@0: }
c@0: }
c@0: } else {
c@0: d.binNames.push_back("C");
c@0: }
c@0:
c@0: d.hasKnownExtents = m_normalized;
c@0: d.minValue = 0.0;
c@0: d.maxValue = (m_normalized ? 1.0 : 0.0);
c@0: d.isQuantized = false;
c@0: d.sampleType = OutputDescriptor::OneSamplePerStep;
c@0: list.push_back(d);
c@0: /*
c@0: d.name = "normalized";
c@0: d.description = "Normalized Chromagram";
c@0: d.hasKnownExtents = true;
c@0: d.maxValue = 1.0;
c@0: list.push_back(d);
c@0: */
c@0: return list;
c@0: }
c@0:
c@0: ChromagramPlugin::Feature
c@0: ChromagramPlugin::normalize(const Feature &feature)
c@0: {
c@0: float min = 0.0, max = 0.0;
c@0:
c@0: for (size_t i = 0; i < feature.values.size(); ++i) {
c@0: if (i == 0 || feature.values[i] < min) min = feature.values[i];
c@0: if (i == 0 || feature.values[i] > max) max = feature.values[i];
c@0: }
c@0:
c@0: if (max == 0.0 || max == min) return feature;
c@0:
c@0: Feature normalized;
c@0: normalized.hasTimestamp = false;
c@0:
c@0: for (size_t i = 0; i < feature.values.size(); ++i) {
c@0: normalized.values.push_back((feature.values[i] - min) / (max - min));
c@0: }
c@0:
c@0: return normalized;
c@0: }
c@0:
c@0: ChromagramPlugin::FeatureSet
c@0: ChromagramPlugin::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
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@0: // convert float* to double*
c@0: double *tempBuffer = new double[m_block];
c@0: for (size_t i = 0; i < m_block; ++i) {
c@0: tempBuffer[i] = inputBuffers[0][i];
c@0: }
c@0:
c@0: double *output = m_chromagram->process(tempBuffer);
c@0: delete[] tempBuffer;
c@0:
c@0: Feature feature;
c@0: feature.hasTimestamp = false;
c@0: for (size_t i = 0; i < m_config.BPO; ++i) {
c@0: feature.values.push_back(output[i]);
c@0: }
c@0: feature.label = "";
c@0:
c@0: FeatureSet returnFeatures;
c@0:
c@0: if (m_stepDelay == 0) {
c@0: returnFeatures[0].push_back(feature);
c@0: // returnFeatures[1].push_back(normalize(feature));
c@0: return returnFeatures;
c@0: }
c@0:
c@0: if (m_pending.size() == m_stepDelay) {
c@0: returnFeatures[0].push_back(m_pending.front());
c@0: // returnFeatures[1].push_back(normalize(m_pending.front()));
c@0: m_pending.pop();
c@0: } else {
c@0: returnFeatures[0].push_back(Feature());
c@0: // returnFeatures[1].push_back(Feature());
c@0: }
c@0:
c@0: m_pending.push(feature);
c@0: return returnFeatures;
c@0: }
c@0:
c@0: ChromagramPlugin::FeatureSet
c@0: ChromagramPlugin::getRemainingFeatures()
c@0: {
c@0: FeatureSet returnFeatures;
c@0:
c@0: while (!m_pending.empty()) {
c@0: returnFeatures[0].push_back(m_pending.front());
c@0: // returnFeatures[1].push_back(normalize(m_pending.front()));
c@0: m_pending.pop();
c@0: }
c@0:
c@0: return returnFeatures;
c@0: }
c@0: