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 @ 45:8db4a1f096f0

History | View | Annotate | Download (4.07 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
/*
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

    
25
#ifndef _NOTE_HYPOTHESIS_H_
26
#define _NOTE_HYPOTHESIS_H_
27

    
28
#include "vamp-sdk/RealTime.h"
29
#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
        /// Just constructed, will provisionally accept any estimate
44
        New,
45

    
46
        /// Accepted at least one estimate, but not enough evidence to satisfy
47
        Provisional,
48

    
49
        /// Could not find enough consistency in offered estimates
50
        Rejected,
51

    
52
        /// Have accepted enough consistent estimates to satisfy hypothesis
53
        Satisfied,
54

    
55
        /// Have been satisfied, but evidence has now changed: we're done
56
        Expired
57
    };
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
        Estimate() : freq(0), time(), confidence(0) { }
72
        Estimate(double _f, Vamp::RealTime _t, double _c) :
73
            freq(_f), time(_t), confidence(_c) { }
74
        bool operator==(const Estimate &e) const {
75
            return e.freq == freq && e.time == time && e.confidence == confidence;
76
        }
77
        double freq;
78
        Vamp::RealTime time;
79
        double confidence;
80
    };
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
        Note() : freq(0), time(), duration() { }
105
        Note(double _f, Vamp::RealTime _t, Vamp::RealTime _d) :
106
            freq(_f), time(_t), duration(_d) { }
107
        bool operator==(const Note &e) const {
108
            return e.freq == freq && e.time == time && e.duration == duration;
109
        }
110
        double freq;
111
        Vamp::RealTime time;
112
        Vamp::RealTime duration;
113
    };
114
    
115
    /**
116
     * Return the mean frequency of the accepted estimates
117
     */
118
    double getMeanFrequency() const;
119

    
120
    /**
121
     * Return a single note roughly matching this hypothesis
122
     */
123
    Note getAveragedNote() const;
124
    
125
private:
126
    bool isWithinTolerance(Estimate) const;
127
    bool isOutOfDateFor(Estimate) const;
128
    bool isSatisfied() const;
129
    
130
    State m_state;
131
    Estimates m_pending;
132
};
133

    
134
#endif