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 "TonalChangeDetect.h"
c@0:
c@3: #include
c@3: #include
c@3: #include
c@0:
cannam@234: using std::cerr;
cannam@234: using std::endl;
cannam@234:
c@0: TonalChangeDetect::TonalChangeDetect(float fInputSampleRate)
c@0: : Vamp::Plugin(fInputSampleRate),
c@0: m_chromagram(0),
c@0: m_step(0),
c@0: m_block(0),
c@85: m_stepDelay(0),
c@85: m_origin(Vamp::RealTime::zeroTime),
c@85: m_haveOrigin(false)
c@0: {
c@0: m_minMIDIPitch = 32;
c@0: m_maxMIDIPitch = 108;
c@0: m_tuningFrequency = 440;
c@0: m_iSmoothingWidth = 5;
c@0:
c@0: setupConfig();
c@0: }
c@0:
c@0: TonalChangeDetect::~TonalChangeDetect()
c@0: {
c@0: }
c@0:
c@0: bool TonalChangeDetect::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()) {
c@0: std::cerr << "TonalChangeDetect::initialise: Given channel count " << channels << " outside acceptable range (" << getMinChannelCount() << " to " << getMaxChannelCount() << ")" << std::endl;
c@0: return false;
c@0: }
c@85:
c@15: m_chromagram = new Chromagram(m_config);
c@15: m_step = m_chromagram->getHopSize();
c@15: m_block = m_chromagram->getFrameSize();
c@15:
c@0: if (stepSize != m_step) {
c@0: std::cerr << "TonalChangeDetect::initialise: Given step size " << stepSize << " differs from only acceptable value " << m_step << std::endl;
c@15: delete m_chromagram;
c@15: m_chromagram = 0;
c@0: return false;
c@0: }
c@0: if (blockSize != m_block) {
c@0: std::cerr << "TonalChangeDetect::initialise: Given step size " << stepSize << " differs from only acceptable value " << m_step << std::endl;
c@15: delete m_chromagram;
c@15: m_chromagram = 0;
c@0: return false;
c@0: }
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@119: // std::cerr << "TonalChangeDetect::initialise: step " << stepSize << ", block "
c@119: // << blockSize << ", delay " << m_stepDelay << std::endl;
c@0:
c@0: m_vaCurrentVector.resize(12, 0.0);
c@0:
c@0: return true;
c@0:
c@0: }
c@0:
c@22: std::string TonalChangeDetect::getIdentifier() const
c@0: {
c@9: return "qm-tonalchange";
c@0: }
c@0:
c@22: std::string TonalChangeDetect::getName() const
c@22: {
c@22: return "Tonal Change";
c@22: }
c@22:
c@0: std::string TonalChangeDetect::getDescription() const
c@0: {
c@52: return "Detect and return the positions of harmonic changes such as chord boundaries";
c@0: }
c@0:
c@0: std::string TonalChangeDetect::getMaker() const
c@0: {
c@50: return "Queen Mary, University of London";
c@0: }
c@0:
c@0: int TonalChangeDetect::getPluginVersion() const
c@0: {
cannam@233: return 4;
c@0: }
c@0:
c@0: std::string TonalChangeDetect::getCopyright() const
c@0: {
c@118: return "Plugin by Martin Gasser and Christopher Harte. Copyright (c) 2006-2009 QMUL - All Rights Reserved";
c@0: }
c@0:
c@0: TonalChangeDetect::ParameterList TonalChangeDetect::getParameterDescriptors() const
c@0: {
c@0: ParameterList list;
c@0:
c@0: ParameterDescriptor desc;
c@23: desc.identifier = "smoothingwidth";
c@23: desc.name = "Gaussian smoothing";
c@119: desc.description = "Window length for the internal smoothing operation, in chroma analysis frames";
c@0: desc.unit = "frames";
c@0: desc.minValue = 0;
c@0: desc.maxValue = 20;
c@0: desc.defaultValue = 5;
c@0: desc.isQuantized = true;
c@0: desc.quantizeStep = 1;
c@0: list.push_back(desc);
c@0:
c@23: desc.identifier = "minpitch";
c@23: desc.name = "Chromagram minimum pitch";
c@0: desc.unit = "MIDI units";
c@119: desc.description = "Lowest pitch in MIDI units to be included in the chroma analysis";
c@0: desc.minValue = 0;
c@0: desc.maxValue = 127;
c@0: desc.defaultValue = 32;
c@0: desc.isQuantized = true;
c@0: desc.quantizeStep = 1;
c@0: list.push_back(desc);
c@0:
c@23: desc.identifier = "maxpitch";
c@23: desc.name = "Chromagram maximum pitch";
c@0: desc.unit = "MIDI units";
c@119: desc.description = "Highest pitch in MIDI units to be included in the chroma analysis";
c@0: desc.minValue = 0;
c@0: desc.maxValue = 127;
c@0: desc.defaultValue = 108;
c@0: desc.isQuantized = true;
c@0: desc.quantizeStep = 1;
c@0: list.push_back(desc);
c@0:
c@23: desc.identifier = "tuning";
c@23: desc.name = "Chromagram tuning frequency";
c@0: desc.unit = "Hz";
c@119: desc.description = "Frequency of concert A in the music under analysis";
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: return list;
c@0: }
c@0:
c@0: float
c@0: TonalChangeDetect::getParameter(std::string param) const
c@0: {
c@0: if (param == "smoothingwidth") {
c@0: return m_iSmoothingWidth;
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:
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: TonalChangeDetect::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: }
c@0: else if (param == "smoothingwidth") {
c@0: m_iSmoothingWidth = int(value);
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: void TonalChangeDetect::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 = 12;
c@0: m_config.CQThresh = 0.0054;
c@49: m_config.normalise = MathUtilities::NormaliseNone;
c@0:
c@0: m_step = 0;
c@0: m_block = 0;
c@0:
c@0:
c@0: }
c@0:
c@0: void
c@0: TonalChangeDetect::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@119: m_vaCurrentVector.clear();
c@171: m_TCSGram.clear();
c@85:
c@85: m_origin = Vamp::RealTime::zeroTime;
c@85: m_haveOrigin = false;
c@0: }
c@0:
c@0: size_t
c@0: TonalChangeDetect::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: TonalChangeDetect::getPreferredBlockSize() 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_block;
c@0: }
c@0:
c@0: TonalChangeDetect::OutputList TonalChangeDetect::getOutputDescriptors() const
c@0: {
c@0: OutputList list;
c@0:
c@0: OutputDescriptor hc;
c@23: hc.identifier = "tcstransform";
c@23: hc.name = "Transform to 6D Tonal Content Space";
c@0: hc.unit = "";
c@119: hc.description = "Representation of content in a six-dimensional tonal space";
c@0: hc.hasFixedBinCount = true;
c@0: hc.binCount = 6;
c@0: hc.hasKnownExtents = true;
c@0: hc.minValue = -1.0;
c@0: hc.maxValue = 1.0;
c@0: hc.isQuantized = false;
c@0: hc.sampleType = OutputDescriptor::OneSamplePerStep;
c@0:
c@0: OutputDescriptor d;
c@23: d.identifier = "tcfunction";
c@23: d.name = "Tonal Change Detection Function";
c@0: d.unit = "";
c@119: d.description = "Estimate of the likelihood of a tonal change occurring within each spectral frame";
c@0: d.minValue = 0;
c@0: d.minValue = 2;
c@0: d.hasFixedBinCount = true;
c@0: d.binCount = 1;
c@70: d.hasKnownExtents = false;
c@0: d.isQuantized = false;
c@0: d.sampleType = OutputDescriptor::VariableSampleRate;
c@70: double dStepSecs = double(getPreferredStepSize()) / m_inputSampleRate;
c@0: d.sampleRate = 1.0f / dStepSecs;
c@0:
c@0: OutputDescriptor changes;
c@23: changes.identifier = "changepositions";
c@23: changes.name = "Tonal Change Positions";
c@0: changes.unit = "";
c@119: changes.description = "Estimated locations of tonal changes";
c@0: changes.hasFixedBinCount = true;
c@0: changes.binCount = 0;
c@70: changes.hasKnownExtents = false;
c@70: changes.isQuantized = false;
c@0: changes.sampleType = OutputDescriptor::VariableSampleRate;
c@0: changes.sampleRate = 1.0 / dStepSecs;
c@0:
c@0: list.push_back(hc);
c@0: list.push_back(d);
c@0: list.push_back(changes);
c@0:
c@0: return list;
c@0: }
c@0:
c@18: TonalChangeDetect::FeatureSet
c@18: TonalChangeDetect::process(const float *const *inputBuffers,
c@222:
c@18: Vamp::RealTime timestamp)
c@0: {
c@0: if (!m_chromagram) {
c@0: cerr << "ERROR: TonalChangeDetect::process: "
c@0: << "Chromagram has not been initialised"
c@0: << endl;
c@0: return FeatureSet();
c@0: }
c@0:
c@222: if (!m_haveOrigin) {
c@222: m_origin = timestamp;
c@222: m_haveOrigin = true;
c@222: }
c@85:
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: for (size_t i = 0; i < 12; i++)
c@0: {
c@0: m_vaCurrentVector[i] = output[i];
c@0: }
c@0:
c@0:
c@0: FeatureSet returnFeatures;
c@0:
c@0: if (m_stepDelay == 0) {
c@0: m_vaCurrentVector.normalizeL1();
c@0: TCSVector tcsVector = m_TonalEstimator.transform2TCS(m_vaCurrentVector);
c@0: m_TCSGram.addTCSVector(tcsVector);
c@0:
c@0: Feature feature;
c@0: feature.hasTimestamp = false;
c@0: for (int i = 0; i < 6; i++)
c@0: { feature.values.push_back(static_cast(tcsVector[i])); }
c@0: feature.label = "";
c@0: returnFeatures[0].push_back(feature);
c@0:
c@0: return returnFeatures;
c@0: }
c@0:
c@0: if (m_pending.size() == m_stepDelay) {
c@0:
c@0: ChromaVector v = m_pending.front();
c@0: v.normalizeL1();
c@0: TCSVector tcsVector = m_TonalEstimator.transform2TCS(v);
c@0: m_TCSGram.addTCSVector(tcsVector);
c@0:
c@0: Feature feature;
c@0: feature.hasTimestamp = false;
c@0: for (int i = 0; i < 6; i++)
c@0: { feature.values.push_back(static_cast(tcsVector[i])); }
c@0: feature.label = "";
c@0: returnFeatures[0].push_back(feature);
c@0: m_pending.pop();
c@0:
c@0: } else {
c@0: returnFeatures[0].push_back(Feature());
c@0: m_TCSGram.addTCSVector(TCSVector());
c@0: }
c@0:
c@0: m_pending.push(m_vaCurrentVector);
c@0:
c@0:
c@0: return returnFeatures;
c@0: }
c@0:
c@0: TonalChangeDetect::FeatureSet TonalChangeDetect::getRemainingFeatures()
c@0: {
c@0: FeatureSet returnFeatures;
c@0:
c@0: while (!m_pending.empty()) {
c@0: ChromaVector v = m_pending.front();
c@0: v.normalizeL1();
c@0: TCSVector tcsVector = m_TonalEstimator.transform2TCS(v);
c@0: m_TCSGram.addTCSVector(tcsVector);
c@0:
c@0: Feature feature;
c@0: feature.hasTimestamp = false;
c@0: for (int i = 0; i < 6; i++)
c@0: { feature.values.push_back(static_cast(tcsVector[i])); }
c@0: feature.label = "";
c@0: returnFeatures[0].push_back(feature);
c@0: m_pending.pop();
c@0: }
c@0:
c@0: ChangeDFConfig dfc;
c@0: dfc.smoothingWidth = double(m_iSmoothingWidth);
c@0: ChangeDetectionFunction df(dfc);
c@0: ChangeDistance d = df.process(m_TCSGram);
c@0:
c@178: for (int i = 0; i < int(d.size()); i++)
c@0: {
c@0: double dCurrent = d[i];
c@0: double dPrevious = d[i > 0 ? i - 1 : i];
c@178: double dNext = d[i < int(d.size())-1 ? i + 1 : i];
c@0:
c@0: Feature feature;
c@0: feature.label = "";
c@0: feature.hasTimestamp = true;
c@85: feature.timestamp = m_origin +
c@85: Vamp::RealTime::frame2RealTime(i*m_step, m_inputSampleRate);
c@0: feature.values.push_back(dCurrent);
c@0: returnFeatures[1].push_back(feature);
c@0:
c@0:
c@0: if (dCurrent > dPrevious && dCurrent > dNext)
c@0: {
c@0: Feature featurePeak;
c@0: featurePeak.label = "";
c@0: featurePeak.hasTimestamp = true;
c@85: featurePeak.timestamp = m_origin +
c@85: Vamp::RealTime::frame2RealTime(i*m_step, m_inputSampleRate);
c@62: returnFeatures[2].push_back(featurePeak);
c@0: }
c@0:
c@0: }
c@0:
c@0:
c@0: return returnFeatures;
c@0:
c@0: }
c@0: