c@170: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@170: /* c@170: Constant-Q library c@170: Copyright (c) 2013-2015 Queen Mary, University of London c@170: c@170: Permission is hereby granted, free of charge, to any person c@170: obtaining a copy of this software and associated documentation c@170: files (the "Software"), to deal in the Software without c@170: restriction, including without limitation the rights to use, copy, c@170: modify, merge, publish, distribute, sublicense, and/or sell copies c@170: of the Software, and to permit persons to whom the Software is c@170: furnished to do so, subject to the following conditions: c@170: c@170: The above copyright notice and this permission notice shall be c@170: included in all copies or substantial portions of the Software. c@170: c@170: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, c@170: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF c@170: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND c@170: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY c@170: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF c@170: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION c@170: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. c@170: c@170: Except as contained in this notice, the names of the Centre for c@170: Digital Music; Queen Mary, University of London; and Chris Cannam c@170: shall not be used in advertising or otherwise to promote the sale, c@170: use or other dealings in this Software without prior written c@170: authorization. c@170: */ c@170: c@170: #include "Chromagram.h" c@170: #include "CQSpectrogram.h" c@170: #include "Pitch.h" c@170: c@170: #include c@170: c@170: using namespace std; c@170: c@170: Chromagram::Chromagram(Parameters params) : c@170: m_params(params), c@170: m_cq(0) c@170: { c@171: int highestOctave = m_params.lowestOctave + m_params.octaveCount - 1; c@170: c@170: int midiPitchLimit = (1 + highestOctave) * 12 + 12; // C just beyond top c@170: double midiPitchLimitFreq = Pitch::getFrequencyForPitch c@170: (midiPitchLimit, 0, m_params.tuningFrequency); c@170: c@170: // Max frequency is frequency of the MIDI pitch just beyond the c@170: // top octave range (midiPitchLimit) minus one bin, then minus c@170: // floor(bins per semitone / 2) c@171: int bps = m_params.binsPerOctave / 12; c@170: m_maxFrequency = midiPitchLimitFreq / c@180: pow(2.0, (1.0 + floor(bps/2)) / m_params.binsPerOctave); c@170: c@170: // Min frequency is frequency of midiPitchLimit lowered by the c@171: // appropriate number of octaveCount. c@170: m_minFrequency = midiPitchLimitFreq / c@180: pow(2.0, m_params.octaveCount + 1); c@170: c@170: CQParameters p c@171: (params.sampleRate, m_minFrequency, m_maxFrequency, params.binsPerOctave); c@171: c@171: p.q = params.q; c@171: p.atomHopFactor = params.atomHopFactor; c@171: p.threshold = params.threshold; c@171: p.window = params.window; c@170: c@170: m_cq = new CQSpectrogram(p, CQSpectrogram::InterpolateLinear); c@170: } c@170: c@170: Chromagram::~Chromagram() c@170: { c@170: delete m_cq; c@170: } c@170: c@170: bool c@170: Chromagram::isValid() const c@170: { c@170: return m_cq->isValid(); c@170: } c@170: c@170: int c@170: Chromagram::getColumnHop() const c@170: { c@170: return m_cq->getColumnHop(); c@170: } c@170: c@170: int c@170: Chromagram::getLatency() const c@170: { c@170: return m_cq->getLatency(); c@170: } c@170: c@170: string c@170: Chromagram::getBinName(int bin) const c@170: { c@170: static const char *names[] = { c@170: "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" c@170: }; c@170: c@171: float freq = m_cq->getBinFrequency(m_params.binsPerOctave - bin - 1); c@170: int note = Pitch::getPitchForFrequency(freq, 0, m_params.tuningFrequency); c@170: float nearestFreq = c@170: Pitch::getFrequencyForPitch(note, 0, m_params.tuningFrequency); c@170: c@170: char name[40]; c@170: sprintf(name, "%d", bin); c@170: if (fabs(freq - nearestFreq) < 0.01) { c@170: return (name + std::string(" ") + names[note % 12]); c@170: } else { c@170: return (name); c@170: } c@170: } c@170: c@170: CQBase::RealBlock c@170: Chromagram::process(const CQBase::RealSequence &data) c@170: { c@170: return convert(m_cq->process(data)); c@170: } c@170: c@170: CQBase::RealBlock c@170: Chromagram::getRemainingOutput() c@170: { c@170: return convert(m_cq->getRemainingOutput()); c@170: } c@170: c@170: CQBase::RealBlock c@170: Chromagram::convert(const CQBase::RealBlock &cqout) c@170: { c@170: CQBase::RealBlock chroma; c@170: c@170: int width = cqout.size(); c@170: c@170: for (int i = 0; i < width; ++i) { c@170: c@171: CQBase::RealSequence column(m_params.binsPerOctave, 0.); c@170: c@170: // fold and invert to put low frequencies at the start c@170: c@170: int thisHeight = cqout[i].size(); c@170: c@170: for (int j = 0; j < thisHeight; ++j) { c@171: column[m_params.binsPerOctave - (j % m_params.binsPerOctave) - 1] c@171: += cqout[i][j]; c@170: } c@170: c@170: chroma.push_back(column); c@170: } c@170: c@170: return chroma; c@170: } c@170: