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 @ 34:3fb9c657d86b

History | View | Annotate | Download (2.9 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 "base/RealTime.h"
8
#include <vector>
9

    
10
namespace Turbot {
11

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

    
19
class NoteHypothesis
20
{
21
public:
22
    enum State {
23

    
24
        /// Just constructed, will provisionally accept any estimate
25
        New,
26

    
27
        /// Accepted at least one estimate, but not enough evidence to satisfy
28
        Provisional,
29

    
30
        /// Could not find enough consistency in offered estimates
31
        Rejected,
32

    
33
        /// Have accepted enough consistent estimates to satisfy hypothesis
34
        Satisfied,
35

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

    
46
    /**
47
     * Destroy the hypothesis
48
     */
49
    ~NoteHypothesis();
50

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

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

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

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

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

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

    
115
}
116

    
117
#endif