To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / NoteHypothesis.h @ 66:7ad142c710c6

History | View | Annotate | Download (4.45 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 66:7ad142c710c6 Chris
     * Construct an empty hypothesis. The given slack (in
61
     * milliseconds) determines how long the hypothesis is prepared to
62
     * tolerate unacceptable estimates in between accepted estimates
63
     * before it becomes rejected. A reasonable default is 40ms.
64
     *
65
     * This hypothesis will be in New state and will provisionally
66
     * accept any estimate.
67 32:c88a9972975b Chris
     */
68 66:7ad142c710c6 Chris
    NoteHypothesis(float slack);
69 32:c88a9972975b Chris
70
    /**
71
     * Destroy the hypothesis
72
     */
73
    ~NoteHypothesis();
74
75
    struct Estimate {
76 58:9f50a5876dd3 Chris
        Estimate() : freq(0), time(), confidence(1) { }
77 35:2f5b169e4a3b Chris
        Estimate(double _f, Vamp::RealTime _t, double _c) :
78 33:5f2a57b1a75a Chris
            freq(_f), time(_t), confidence(_c) { }
79 34:3fb9c657d86b Chris
        bool operator==(const Estimate &e) const {
80
            return e.freq == freq && e.time == time && e.confidence == confidence;
81
        }
82 45:8db4a1f096f0 Chris
        double freq;
83 35:2f5b169e4a3b Chris
        Vamp::RealTime time;
84 45:8db4a1f096f0 Chris
        double confidence;
85 32:c88a9972975b Chris
    };
86
    typedef std::vector<Estimate> Estimates;
87
88
    /**
89
     * Test the given estimate to see whether it is consistent with
90
     * this hypothesis, and adjust the hypothesis' internal state
91
     * accordingly. If the estimate is not inconsistent with the
92
     * hypothesis, return true.
93
     */
94
    bool accept(Estimate);
95
96
    /**
97
     * Return the current state of this hypothesis.
98
     */
99
    State getState() const;
100
101
    /**
102
     * If the hypothesis has been satisfied (i.e. is in Satisfied or
103
     * Expired state), return the series of estimates that it
104
     * accepted. Otherwise return an empty list
105
     */
106
    Estimates getAcceptedEstimates() const;
107
108
    struct Note {
109 33:5f2a57b1a75a Chris
        Note() : freq(0), time(), duration() { }
110 35:2f5b169e4a3b Chris
        Note(double _f, Vamp::RealTime _t, Vamp::RealTime _d) :
111 33:5f2a57b1a75a Chris
            freq(_f), time(_t), duration(_d) { }
112 34:3fb9c657d86b Chris
        bool operator==(const Note &e) const {
113
            return e.freq == freq && e.time == time && e.duration == duration;
114
        }
115 45:8db4a1f096f0 Chris
        double freq;
116
        Vamp::RealTime time;
117
        Vamp::RealTime duration;
118 32:c88a9972975b Chris
    };
119
120
    /**
121 52:34f42a384a7f Chris
     * Return the time of the first accepted estimate
122
     */
123
    Vamp::RealTime getStartTime() const;
124
125
    /**
126 34:3fb9c657d86b Chris
     * Return the mean frequency of the accepted estimates
127
     */
128
    double getMeanFrequency() const;
129
130
    /**
131 32:c88a9972975b Chris
     * Return a single note roughly matching this hypothesis
132
     */
133
    Note getAveragedNote() const;
134
135
private:
136
    bool isWithinTolerance(Estimate) const;
137
    bool isOutOfDateFor(Estimate) const;
138
    bool isSatisfied() const;
139
140
    State m_state;
141
    Estimates m_pending;
142 66:7ad142c710c6 Chris
    float m_slack;
143 32:c88a9972975b Chris
};
144
145
#endif