c@35: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@69: /* c@69: Constant-Q library c@69: Copyright (c) 2013-2014 Queen Mary, University of London c@69: c@69: Permission is hereby granted, free of charge, to any person c@69: obtaining a copy of this software and associated documentation c@69: files (the "Software"), to deal in the Software without c@69: restriction, including without limitation the rights to use, copy, c@69: modify, merge, publish, distribute, sublicense, and/or sell copies c@69: of the Software, and to permit persons to whom the Software is c@69: furnished to do so, subject to the following conditions: c@69: c@69: The above copyright notice and this permission notice shall be c@69: included in all copies or substantial portions of the Software. c@69: c@69: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@69: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@69: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@69: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY c@69: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@69: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@69: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@69: c@69: Except as contained in this notice, the names of the Centre for c@69: Digital Music; Queen Mary, University of London; and Chris Cannam c@69: shall not be used in advertising or otherwise to promote the sale, c@69: use or other dealings in this Software without prior written c@69: authorization. c@69: */ c@35: c@35: #include "CQVamp.h" c@35: c@121: #include "Pitch.h" c@55: c@58: #include c@58: #include c@58: c@35: using std::string; c@35: using std::vector; c@35: using std::cerr; c@35: using std::endl; c@35: c@109: // The plugin offers either MIDI pitch or frequency range parameters, c@109: // depending on the midiPitchParameters option given to the c@109: // constructor. It never offers both. So they can have different c@109: // defaults; if we're using MIDI pitch, the min and max frequencies c@109: // will come from those rather than from the m_minFrequency and c@109: // m_maxFrequency members. c@109: static const int defaultMinMIDIPitch = 36; c@109: static const int defaultMaxMIDIPitch = 96; c@109: static const int defaultBPO = 36; c@154: static const float defaultMinFrequency = 110; c@154: static const float defaultMaxFrequency = 14700; c@109: static const float defaultTuningFrequency = 440.f; c@109: c@109: CQVamp::CQVamp(float inputSampleRate, bool midiPitchParameters) : c@35: Vamp::Plugin(inputSampleRate), c@109: m_midiPitchParameters(midiPitchParameters), c@109: m_minMIDIPitch(defaultMinMIDIPitch), c@109: m_maxMIDIPitch(defaultMaxMIDIPitch), c@109: m_tuningFrequency(defaultTuningFrequency), c@109: m_bpo(defaultBPO), c@190: m_atomOverlap(4), c@190: m_useDraftDecimator(false), c@90: m_interpolation(CQSpectrogram::InterpolateLinear), c@35: m_cq(0), c@154: m_maxFrequency(defaultMaxFrequency), c@154: m_minFrequency(defaultMinFrequency), c@53: m_haveStartTime(false), c@53: m_columnCount(0) c@35: { c@35: } c@35: c@35: CQVamp::~CQVamp() c@35: { c@35: delete m_cq; c@35: } c@35: c@35: string c@35: CQVamp::getIdentifier() const c@35: { c@109: if (m_midiPitchParameters) { c@109: return "cqvampmidi"; c@109: } else { c@109: return "cqvamp"; c@109: } c@35: } c@35: c@35: string c@35: CQVamp::getName() const c@35: { c@109: if (m_midiPitchParameters) { c@169: return "CQ Constant-Q Spectrogram (MIDI pitch range)"; c@109: } else { c@169: return "CQ Constant-Q Spectrogram (Hz range)"; c@109: } c@35: } c@35: c@35: string c@35: CQVamp::getDescription() const c@35: { c@109: if (m_midiPitchParameters) { c@109: return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in MIDI pitch units."; c@109: } else { c@109: return "Extract a spectrogram with constant ratio of centre frequency to resolution from the input audio, specifying the frequency range in Hz."; c@109: } c@35: } c@35: c@35: string c@35: CQVamp::getMaker() const c@35: { c@35: return "Queen Mary, University of London"; c@35: } c@35: c@35: int c@35: CQVamp::getPluginVersion() const c@35: { c@190: return 3; c@35: } c@35: c@35: string c@35: CQVamp::getCopyright() const c@35: { c@190: return "Plugin by Chris Cannam. Method by Christian Schörkhuber and Anssi Klapuri. Copyright (c) 2015-2017 QMUL. BSD/MIT licence."; c@35: } c@35: c@35: CQVamp::ParameterList c@35: CQVamp::getParameterDescriptors() const c@35: { c@35: ParameterList list; c@35: c@55: ParameterDescriptor desc; c@55: c@109: if (m_midiPitchParameters) { c@55: c@109: desc.identifier = "minpitch"; c@109: desc.name = "Minimum Pitch"; c@109: desc.unit = "MIDI units"; c@109: desc.description = "MIDI pitch corresponding to the lowest frequency to be included in the constant-Q transform. (The actual minimum frequency may be lower, as the range always covers an integral number of octaves below the highest frequency.)"; c@109: desc.minValue = 0; c@109: desc.maxValue = 127; c@151: desc.defaultValue = defaultMinMIDIPitch; c@109: desc.isQuantized = true; c@109: desc.quantizeStep = 1; c@109: list.push_back(desc); c@109: c@109: desc.identifier = "maxpitch"; c@109: desc.name = "Maximum Pitch"; c@109: desc.unit = "MIDI units"; c@109: desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform"; c@109: desc.minValue = 0; c@109: desc.maxValue = 127; c@151: desc.defaultValue = defaultMaxMIDIPitch; c@109: desc.isQuantized = true; c@109: desc.quantizeStep = 1; c@109: list.push_back(desc); c@109: c@109: desc.identifier = "tuning"; c@109: desc.name = "Tuning Frequency"; c@109: desc.unit = "Hz"; c@109: desc.description = "Frequency of concert A"; c@109: desc.minValue = 360; c@109: desc.maxValue = 500; c@151: desc.defaultValue = defaultTuningFrequency; c@109: desc.isQuantized = false; c@109: list.push_back(desc); c@109: c@109: } else { c@109: c@109: desc.identifier = "minfreq"; c@109: desc.name = "Minimum Frequency"; c@109: desc.unit = "Hz"; c@109: desc.description = "Lowest frequency to be included in the constant-Q transform. (The actual minimum frequency may be lower, as the range always covers an integral number of octaves below the highest frequency.)"; c@109: desc.minValue = 1; c@153: desc.maxValue = 22050; c@154: desc.defaultValue = defaultMinFrequency; c@109: desc.isQuantized = false; c@109: list.push_back(desc); c@109: c@109: desc.identifier = "maxfreq"; c@109: desc.name = "Maximum Frequency"; c@109: desc.unit = "Hz"; c@109: desc.description = "MIDI pitch corresponding to the highest frequency to be included in the constant-Q transform"; c@109: desc.minValue = 1; c@153: desc.maxValue = 22050; c@154: desc.defaultValue = defaultMaxFrequency; c@109: desc.isQuantized = false; c@109: list.push_back(desc); c@109: } c@35: c@35: desc.identifier = "bpo"; c@35: desc.name = "Bins per Octave"; c@35: desc.unit = "bins"; c@35: desc.description = "Number of constant-Q transform bins per octave"; c@35: desc.minValue = 2; c@35: desc.maxValue = 480; c@110: desc.defaultValue = defaultBPO; c@35: desc.isQuantized = true; c@35: desc.quantizeStep = 1; c@35: list.push_back(desc); c@35: c@190: desc.identifier = "atomoverlap"; c@190: desc.name = "Overlap"; c@190: desc.unit = ""; c@190: desc.description = "Overlap factor for CQ kernel atoms (higher = more output values per unit time)"; c@190: desc.minValue = 1; c@190: desc.maxValue = 8; c@190: desc.defaultValue = 4; c@190: desc.isQuantized = true; c@190: desc.quantizeStep = 1; c@190: list.push_back(desc); c@190: c@190: desc.identifier = "draftdecimator"; c@190: desc.name = "Use Draft Decimator"; c@190: desc.unit = ""; c@190: desc.description = "Trade off some decimator quality for faster speed"; c@190: desc.minValue = 0; c@190: desc.maxValue = 1; c@190: desc.defaultValue = 0; c@190: desc.isQuantized = true; c@190: desc.quantizeStep = 1; c@190: list.push_back(desc); c@190: c@75: desc.identifier = "interpolation"; c@75: desc.name = "Interpolation"; c@75: desc.unit = ""; c@75: desc.description = "Interpolation method used to fill empty cells in lower octaves"; c@75: desc.minValue = 0; c@75: desc.maxValue = 2; c@75: desc.defaultValue = 2; c@75: desc.isQuantized = true; c@75: desc.quantizeStep = 1; c@90: desc.valueNames.push_back("None, leave as zero"); c@75: desc.valueNames.push_back("None, repeat prior value"); c@75: desc.valueNames.push_back("Linear interpolation"); c@75: list.push_back(desc); c@75: c@35: return list; c@35: } c@35: c@35: float c@35: CQVamp::getParameter(std::string param) const c@35: { c@109: if (param == "minpitch" && m_midiPitchParameters) { c@55: return m_minMIDIPitch; c@55: } c@109: if (param == "maxpitch" && m_midiPitchParameters) { c@55: return m_maxMIDIPitch; c@55: } c@109: if (param == "tuning" && m_midiPitchParameters) { c@55: return m_tuningFrequency; c@55: } c@35: if (param == "bpo") { c@35: return m_bpo; c@35: } c@75: if (param == "interpolation") { c@75: return (float)m_interpolation; c@75: } c@109: if (param == "minfreq" && !m_midiPitchParameters) { c@109: return m_minFrequency; c@109: } c@109: if (param == "maxfreq" && !m_midiPitchParameters) { c@109: return m_maxFrequency; c@109: } c@190: if (param == "atomoverlap") { c@190: return m_atomOverlap; c@190: } c@190: if (param == "draftdecimator") { c@190: return m_useDraftDecimator ? 1.f : 0.f; c@190: } c@35: std::cerr << "WARNING: CQVamp::getParameter: unknown parameter \"" c@35: << param << "\"" << std::endl; c@35: return 0.0; c@35: } c@35: c@35: void c@35: CQVamp::setParameter(std::string param, float value) c@35: { c@109: if (param == "minpitch" && m_midiPitchParameters) { c@164: m_minMIDIPitch = int(value + 0.5f); c@109: } else if (param == "maxpitch" && m_midiPitchParameters) { c@164: m_maxMIDIPitch = int(value + 0.5f); c@109: } else if (param == "tuning" && m_midiPitchParameters) { c@55: m_tuningFrequency = value; c@75: } else if (param == "bpo") { c@164: m_bpo = int(value + 0.5f); c@75: } else if (param == "interpolation") { c@164: m_interpolation = (CQSpectrogram::Interpolation)int(value + 0.5f); c@109: } else if (param == "minfreq" && !m_midiPitchParameters) { c@109: m_minFrequency = value; c@109: } else if (param == "maxfreq" && !m_midiPitchParameters) { c@109: m_maxFrequency = value; c@190: } else if (param == "atomoverlap") { c@190: m_atomOverlap = int(value + 0.5f); c@190: } else if (param == "draftdecimator") { c@190: m_useDraftDecimator = (value > 0.5f); c@35: } else { c@35: std::cerr << "WARNING: CQVamp::setParameter: unknown parameter \"" c@35: << param << "\"" << std::endl; c@35: } c@35: } c@35: c@35: bool c@35: CQVamp::initialise(size_t channels, size_t stepSize, size_t blockSize) c@35: { c@35: if (m_cq) { c@35: delete m_cq; c@75: m_cq = 0; c@35: } c@35: c@35: if (channels < getMinChannelCount() || c@35: channels > getMaxChannelCount()) return false; c@35: c@35: m_stepSize = stepSize; c@35: m_blockSize = blockSize; c@35: c@109: if (m_midiPitchParameters) { c@109: m_minFrequency = Pitch::getFrequencyForPitch c@109: (m_minMIDIPitch, 0, m_tuningFrequency); c@109: m_maxFrequency = Pitch::getFrequencyForPitch c@109: (m_maxMIDIPitch, 0, m_tuningFrequency); c@109: } c@55: c@147: reset(); c@147: c@147: if (!m_cq || !m_cq->isValid()) { c@147: cerr << "CQVamp::initialise: Constant-Q parameters not valid! Not initialising" << endl; c@147: return false; c@147: } c@35: c@35: return true; c@35: } c@35: c@35: void c@35: CQVamp::reset() c@35: { c@147: delete m_cq; c@147: CQParameters p(m_inputSampleRate, m_minFrequency, m_maxFrequency, m_bpo); c@190: p.atomHopFactor = 1.0 / double(m_atomOverlap); c@190: p.decimator = (m_useDraftDecimator ? c@190: CQParameters::FasterDecimator : c@190: CQParameters::BetterDecimator); c@147: m_cq = new CQSpectrogram(p, m_interpolation); c@53: m_haveStartTime = false; c@53: m_columnCount = 0; c@35: } c@35: c@35: size_t c@35: CQVamp::getPreferredStepSize() const c@35: { c@35: return 0; c@35: } c@35: c@35: size_t c@35: CQVamp::getPreferredBlockSize() const c@35: { c@35: return 0; c@35: } c@35: c@109: std::string c@109: CQVamp::noteName(int i) const c@109: { c@109: static const char *names[] = { c@109: "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" c@109: }; c@109: c@109: const char *n = names[i % 12]; c@109: int oct = i / 12 - 1; c@109: char buf[20]; c@110: sprintf(buf, "%d %s%d", i, n, oct); c@109: c@109: return buf; c@109: } c@109: c@35: CQVamp::OutputList c@35: CQVamp::getOutputDescriptors() const c@35: { c@35: OutputList list; c@35: c@35: OutputDescriptor d; c@35: d.identifier = "constantq"; c@35: d.name = "Constant-Q Spectrogram"; c@35: d.unit = ""; c@35: d.description = "Output of constant-Q transform, as a single vector per process block"; c@35: d.hasFixedBinCount = true; c@35: d.binCount = (m_cq ? m_cq->getTotalBins() : (9 * 24)); c@58: c@58: if (m_cq) { c@58: char name[20]; c@94: for (int i = 0; i < (int)d.binCount; ++i) { c@168: float freq = m_cq->getBinFrequency(d.binCount - i - 1); c@58: sprintf(name, "%.1f Hz", freq); c@110: int note = Pitch::getPitchForFrequency(freq, 0, m_tuningFrequency); c@110: float nearestFreq = c@110: Pitch::getFrequencyForPitch(note, 0, m_tuningFrequency); c@110: if (fabs(freq - nearestFreq) < 0.01) { c@110: d.binNames.push_back(name + std::string(" ") + noteName(note)); c@109: } else { c@109: d.binNames.push_back(name); c@109: } c@58: } c@58: } c@58: c@35: d.hasKnownExtents = false; c@35: d.isQuantized = false; c@35: d.sampleType = OutputDescriptor::FixedSampleRate; c@35: d.sampleRate = m_inputSampleRate / (m_cq ? m_cq->getColumnHop() : 256); c@35: list.push_back(d); c@35: c@35: return list; c@35: } c@35: c@35: CQVamp::FeatureSet c@35: CQVamp::process(const float *const *inputBuffers, c@53: Vamp::RealTime timestamp) c@35: { c@35: if (!m_cq) { c@35: cerr << "ERROR: CQVamp::process: " c@35: << "Plugin has not been initialised" c@35: << endl; c@35: return FeatureSet(); c@35: } c@35: c@53: if (!m_haveStartTime) { c@53: m_startTime = timestamp; c@53: m_haveStartTime = true; c@53: } c@53: c@35: vector data; c@35: for (int i = 0; i < m_blockSize; ++i) data.push_back(inputBuffers[0][i]); c@35: c@35: vector > cqout = m_cq->process(data); c@36: return convertToFeatures(cqout); c@36: } c@35: c@36: CQVamp::FeatureSet c@36: CQVamp::getRemainingFeatures() c@36: { c@90: vector > cqout = m_cq->getRemainingOutput(); c@36: return convertToFeatures(cqout); c@36: } c@36: c@36: CQVamp::FeatureSet c@36: CQVamp::convertToFeatures(const vector > &cqout) c@36: { c@35: FeatureSet returnFeatures; c@35: c@75: int width = cqout.size(); c@75: int height = m_cq->getTotalBins(); c@35: c@75: for (int i = 0; i < width; ++i) { c@36: c@75: vector column(height, 0.f); c@75: int thisHeight = cqout[i].size(); c@75: for (int j = 0; j < thisHeight; ++j) { c@35: column[j] = cqout[i][j]; c@35: } c@36: c@58: // put low frequencies at the start c@58: std::reverse(column.begin(), column.end()); c@58: c@35: Feature feature; c@53: feature.hasTimestamp = true; c@53: feature.timestamp = m_startTime + Vamp::RealTime::frame2RealTime c@53: (m_columnCount * m_cq->getColumnHop() - m_cq->getLatency(), c@53: m_inputSampleRate); c@35: feature.values = column; c@35: feature.label = ""; c@53: c@56: // cerr << "timestamp = " << feature.timestamp << " (start time = " << m_startTime << ", column count = " << m_columnCount << ", latency = " << m_cq->getLatency() << ", sample rate " << m_inputSampleRate << ")" << endl; c@53: c@53: if (feature.timestamp >= m_startTime) { c@53: returnFeatures[0].push_back(feature); c@53: } c@53: c@53: ++m_columnCount; c@35: } c@35: c@35: return returnFeatures; c@35: } c@35: