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 @ 35:2f5b169e4a3b

History | View | Annotate | Download (2.92 KB)

1
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2
/* Copyright Chris Cannam - All Rights Reserved */
3

    
4
#ifndef _NOTE_HYPOTHESIS_H_
5
#define _NOTE_HYPOTHESIS_H_
6

    
7
#include "vamp-sdk/RealTime.h"
8
#include <vector>
9

    
10
/**
11
 * An agent used to test an incoming series of instantaneous pitch
12
 * estimates to see whether they fit a consistent single-note
13
 * relationship. Contains rules specific to testing note pitch and
14
 * timing.
15
 */
16

    
17
class NoteHypothesis
18
{
19
public:
20
    enum State {
21

    
22
        /// Just constructed, will provisionally accept any estimate
23
        New,
24

    
25
        /// Accepted at least one estimate, but not enough evidence to satisfy
26
        Provisional,
27

    
28
        /// Could not find enough consistency in offered estimates
29
        Rejected,
30

    
31
        /// Have accepted enough consistent estimates to satisfy hypothesis
32
        Satisfied,
33

    
34
        /// Have been satisfied, but evidence has now changed: we're done
35
        Expired
36
    };
37
    
38
    /**
39
     * Construct an empty hypothesis. This will be in New state and
40
     * will provisionally accept any estimate.
41
     */
42
    NoteHypothesis();
43

    
44
    /**
45
     * Destroy the hypothesis
46
     */
47
    ~NoteHypothesis();
48

    
49
    struct Estimate {
50
        Estimate() : freq(0), time(), confidence(0) { }
51
        Estimate(double _f, Vamp::RealTime _t, double _c) :
52
            freq(_f), time(_t), confidence(_c) { }
53
        bool operator==(const Estimate &e) const {
54
            return e.freq == freq && e.time == time && e.confidence == confidence;
55
        }
56
        double freq;
57
        Vamp::RealTime time;
58
        double confidence;
59
    };
60
    typedef std::vector<Estimate> Estimates;
61

    
62
    /**
63
     * Test the given estimate to see whether it is consistent with
64
     * this hypothesis, and adjust the hypothesis' internal state
65
     * accordingly. If the estimate is not inconsistent with the
66
     * hypothesis, return true.
67
     */
68
    bool accept(Estimate);
69

    
70
    /**
71
     * Return the current state of this hypothesis.
72
     */
73
    State getState() const;
74

    
75
    /**
76
     * If the hypothesis has been satisfied (i.e. is in Satisfied or
77
     * Expired state), return the series of estimates that it
78
     * accepted. Otherwise return an empty list
79
     */
80
    Estimates getAcceptedEstimates() const;
81

    
82
    struct Note {
83
        Note() : freq(0), time(), duration() { }
84
        Note(double _f, Vamp::RealTime _t, Vamp::RealTime _d) :
85
            freq(_f), time(_t), duration(_d) { }
86
        bool operator==(const Note &e) const {
87
            return e.freq == freq && e.time == time && e.duration == duration;
88
        }
89
        double freq;
90
        Vamp::RealTime time;
91
        Vamp::RealTime duration;
92
    };
93
    
94
    /**
95
     * Return the mean frequency of the accepted estimates
96
     */
97
    double getMeanFrequency() const;
98

    
99
    /**
100
     * Return a single note roughly matching this hypothesis
101
     */
102
    Note getAveragedNote() const;
103
    
104
private:
105
    bool isWithinTolerance(Estimate) const;
106
    bool isOutOfDateFor(Estimate) const;
107
    bool isSatisfied() const;
108
    
109
    State m_state;
110
    Estimates m_pending;
111
};
112

    
113
#endif