To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / NoteHypothesis.h @ 58:9f50a5876dd3
History | View | Annotate | Download (4.18 KB)
| 1 | 32:c88a9972975b | Chris | /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
|---|---|---|---|
| 2 | 37:7bf67d2dfc30 | Chris | /*
|
| 3 | This file is Copyright (c) 2012 Chris Cannam
|
||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person
|
||
| 6 | obtaining a copy of this software and associated documentation
|
||
| 7 | files (the "Software"), to deal in the Software without
|
||
| 8 | restriction, including without limitation the rights to use, copy,
|
||
| 9 | modify, merge, publish, distribute, sublicense, and/or sell copies
|
||
| 10 | of the Software, and to permit persons to whom the Software is
|
||
| 11 | furnished to do so, subject to the following conditions:
|
||
| 12 | |||
| 13 | The above copyright notice and this permission notice shall be
|
||
| 14 | included in all copies or substantial portions of the Software.
|
||
| 15 | |||
| 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
| 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||
| 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
|
||
| 20 | ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
|
||
| 21 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||
| 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||
| 23 | */
|
||
| 24 | 32:c88a9972975b | Chris | |
| 25 | #ifndef _NOTE_HYPOTHESIS_H_
|
||
| 26 | #define _NOTE_HYPOTHESIS_H_
|
||
| 27 | |||
| 28 | 35:2f5b169e4a3b | Chris | #include "vamp-sdk/RealTime.h" |
| 29 | 32:c88a9972975b | Chris | #include <vector> |
| 30 | |||
| 31 | /**
|
||
| 32 | * An agent used to test an incoming series of instantaneous pitch
|
||
| 33 | * estimates to see whether they fit a consistent single-note
|
||
| 34 | * relationship. Contains rules specific to testing note pitch and
|
||
| 35 | * timing.
|
||
| 36 | */
|
||
| 37 | |||
| 38 | class NoteHypothesis |
||
| 39 | {
|
||
| 40 | public:
|
||
| 41 | enum State {
|
||
| 42 | |||
| 43 | 45:8db4a1f096f0 | Chris | /// Just constructed, will provisionally accept any estimate
|
| 44 | New, |
||
| 45 | 32:c88a9972975b | Chris | |
| 46 | 45:8db4a1f096f0 | Chris | /// Accepted at least one estimate, but not enough evidence to satisfy
|
| 47 | Provisional, |
||
| 48 | 32:c88a9972975b | Chris | |
| 49 | 45:8db4a1f096f0 | Chris | /// Could not find enough consistency in offered estimates
|
| 50 | Rejected, |
||
| 51 | 32:c88a9972975b | Chris | |
| 52 | 45:8db4a1f096f0 | Chris | /// Have accepted enough consistent estimates to satisfy hypothesis
|
| 53 | Satisfied, |
||
| 54 | 32:c88a9972975b | Chris | |
| 55 | 45:8db4a1f096f0 | Chris | /// Have been satisfied, but evidence has now changed: we're done
|
| 56 | Expired |
||
| 57 | 32:c88a9972975b | Chris | }; |
| 58 | |||
| 59 | /**
|
||
| 60 | * Construct an empty hypothesis. This will be in New state and
|
||
| 61 | * will provisionally accept any estimate.
|
||
| 62 | */
|
||
| 63 | NoteHypothesis(); |
||
| 64 | |||
| 65 | /**
|
||
| 66 | * Destroy the hypothesis
|
||
| 67 | */
|
||
| 68 | ~NoteHypothesis(); |
||
| 69 | |||
| 70 | struct Estimate {
|
||
| 71 | 58:9f50a5876dd3 | Chris | Estimate() : freq(0), time(), confidence(1) { } |
| 72 | 35:2f5b169e4a3b | Chris | Estimate(double _f, Vamp::RealTime _t, double _c) : |
| 73 | 33:5f2a57b1a75a | Chris | freq(_f), time(_t), confidence(_c) { }
|
| 74 | 34:3fb9c657d86b | Chris | bool operator==(const Estimate &e) const { |
| 75 | return e.freq == freq && e.time == time && e.confidence == confidence;
|
||
| 76 | } |
||
| 77 | 45:8db4a1f096f0 | Chris | double freq;
|
| 78 | 35:2f5b169e4a3b | Chris | Vamp::RealTime time; |
| 79 | 45:8db4a1f096f0 | Chris | double confidence;
|
| 80 | 32:c88a9972975b | Chris | }; |
| 81 | typedef std::vector<Estimate> Estimates;
|
||
| 82 | |||
| 83 | /**
|
||
| 84 | * Test the given estimate to see whether it is consistent with
|
||
| 85 | * this hypothesis, and adjust the hypothesis' internal state
|
||
| 86 | * accordingly. If the estimate is not inconsistent with the
|
||
| 87 | * hypothesis, return true.
|
||
| 88 | */
|
||
| 89 | bool accept(Estimate);
|
||
| 90 | |||
| 91 | /**
|
||
| 92 | * Return the current state of this hypothesis.
|
||
| 93 | */
|
||
| 94 | State getState() const;
|
||
| 95 | |||
| 96 | /**
|
||
| 97 | * If the hypothesis has been satisfied (i.e. is in Satisfied or
|
||
| 98 | * Expired state), return the series of estimates that it
|
||
| 99 | * accepted. Otherwise return an empty list
|
||
| 100 | */
|
||
| 101 | Estimates getAcceptedEstimates() const;
|
||
| 102 | |||
| 103 | struct Note {
|
||
| 104 | 33:5f2a57b1a75a | Chris | Note() : freq(0), time(), duration() { }
|
| 105 | 35:2f5b169e4a3b | Chris | Note(double _f, Vamp::RealTime _t, Vamp::RealTime _d) :
|
| 106 | 33:5f2a57b1a75a | Chris | freq(_f), time(_t), duration(_d) { }
|
| 107 | 34:3fb9c657d86b | Chris | bool operator==(const Note &e) const { |
| 108 | return e.freq == freq && e.time == time && e.duration == duration;
|
||
| 109 | } |
||
| 110 | 45:8db4a1f096f0 | Chris | double freq;
|
| 111 | Vamp::RealTime time; |
||
| 112 | Vamp::RealTime duration; |
||
| 113 | 32:c88a9972975b | Chris | }; |
| 114 | |||
| 115 | /**
|
||
| 116 | 52:34f42a384a7f | Chris | * Return the time of the first accepted estimate
|
| 117 | */
|
||
| 118 | Vamp::RealTime getStartTime() const;
|
||
| 119 | |||
| 120 | /**
|
||
| 121 | 34:3fb9c657d86b | Chris | * Return the mean frequency of the accepted estimates
|
| 122 | */
|
||
| 123 | double getMeanFrequency() const; |
||
| 124 | |||
| 125 | /**
|
||
| 126 | 32:c88a9972975b | Chris | * Return a single note roughly matching this hypothesis
|
| 127 | */
|
||
| 128 | Note getAveragedNote() const;
|
||
| 129 | |||
| 130 | private:
|
||
| 131 | bool isWithinTolerance(Estimate) const; |
||
| 132 | bool isOutOfDateFor(Estimate) const; |
||
| 133 | bool isSatisfied() const; |
||
| 134 | |||
| 135 | State m_state; |
||
| 136 | Estimates m_pending; |
||
| 137 | }; |
||
| 138 | |||
| 139 | #endif |