Chris@181: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@181: Chris@181: /* Chris@181: Silvet Chris@181: Chris@181: A Vamp plugin for note transcription. Chris@181: Centre for Digital Music, Queen Mary University of London. Chris@181: This file Copyright 2012 Chris Cannam. Chris@181: Chris@181: This program is free software; you can redistribute it and/or Chris@181: modify it under the terms of the GNU General Public License as Chris@181: published by the Free Software Foundation; either version 2 of the Chris@181: License, or (at your option) any later version. See the file Chris@181: COPYING included with this distribution for more information. Chris@181: */ Chris@181: Chris@181: #ifndef NOTE_HYPOTHESIS_H Chris@181: #define NOTE_HYPOTHESIS_H Chris@181: Chris@181: #include "AgentHypothesis.h" Chris@181: Chris@181: #include Chris@181: #include Chris@181: Chris@181: /** Chris@181: * An AgentHypothesis which tests a series of instantaneous pitch Chris@181: * estimates to see whether they fit a single-note relationship. Chris@181: * Contains rules specific to testing note pitch and timing. Chris@181: */ Chris@181: Chris@181: class NoteHypothesis : public AgentHypothesis Chris@181: { Chris@181: public: Chris@181: /** Chris@181: * Construct an empty hypothesis. This will be in New state and Chris@181: * will provisionally accept any estimate. Chris@181: */ Chris@181: NoteHypothesis(); Chris@181: Chris@181: /** Chris@181: * Destroy the hypothesis Chris@181: */ Chris@181: ~NoteHypothesis(); Chris@181: Chris@181: virtual bool accept(Observation); Chris@181: virtual State getState() const; Chris@181: virtual Observations getAcceptedObservations() const; Chris@181: Chris@181: struct Note { Chris@184: Note() : freq(0), time(), duration(), confidence(1.0) { } Chris@184: Note(double _f, Vamp::RealTime _t, Vamp::RealTime _d, double _i) : Chris@184: freq(_f), time(_t), duration(_d), confidence(_i) { } Chris@181: bool operator==(const Note &e) const { Chris@184: return e.freq == freq && e.time == time && Chris@184: e.duration == duration && e.confidence == confidence; Chris@181: } Chris@181: double freq; Chris@181: Vamp::RealTime time; Chris@181: Vamp::RealTime duration; Chris@184: double confidence; Chris@181: }; Chris@181: Chris@181: /** Chris@181: * Return the mean frequency of the accepted observations Chris@181: */ Chris@181: double getMeanFrequency() const; Chris@184: Chris@184: /** Chris@184: * Return the median frequency of the accepted observations Chris@184: */ Chris@184: double getMedianFrequency() const; Chris@184: Chris@184: /** Chris@184: * Return the median confidence of the accepted observations Chris@184: */ Chris@184: double getMedianConfidence() const; Chris@181: Chris@181: /** Chris@181: * Return a single note roughly matching this hypothesis Chris@181: */ Chris@181: Note getAveragedNote() const; Chris@181: Chris@181: /** Chris@181: * Return the time of the first accepted observation Chris@181: */ Chris@181: Vamp::RealTime getStartTime() const; Chris@181: Chris@181: /** Chris@181: * Return the difference between the start time and the end of the Chris@181: * final accepted observation Chris@181: */ Chris@181: Vamp::RealTime getDuration() const; Chris@181: Chris@181: //!!! Chris@181: bool operator==(const NoteHypothesis &other) const { Chris@181: return m_state == other.m_state && m_pending == other.m_pending; Chris@181: } Chris@181: Chris@181: bool operator<(const NoteHypothesis &other) const { Chris@184: if (getStartTime() != other.getStartTime()) { Chris@184: return getStartTime() < other.getStartTime(); Chris@184: } else if (m_state != other.m_state) { Chris@184: return m_state < other.m_state; Chris@184: } else if (m_pending.size() != other.m_pending.size()) { Chris@184: return m_pending.size() < other.m_pending.size(); Chris@184: } else { Chris@184: Observations::const_iterator i = m_pending.begin(); Chris@184: Observations::const_iterator j = other.m_pending.begin(); Chris@184: while (i != m_pending.end()) { Chris@184: if (*i == *j) { Chris@184: ++i; Chris@184: ++j; Chris@184: } else { Chris@184: return *i < *j; Chris@184: } Chris@184: } Chris@184: return false; Chris@184: } Chris@181: } Chris@181: Chris@181: private: Chris@181: bool isWithinTolerance(Observation) const; Chris@181: bool isOutOfDateFor(Observation) const; Chris@181: bool isSatisfied() const; Chris@181: Chris@181: State m_state; Chris@181: Observations m_pending; Chris@181: }; Chris@181: Chris@181: #endif