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 @ 33:5f2a57b1a75a

History | View | Annotate | Download (2.55 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
        double freq;
56
        RealTime time;
57
        double confidence;
58
    };
59
    typedef std::vector<Estimate> Estimates;
60

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

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

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

    
81
    struct Note {
82
        Note() : freq(0), time(), duration() { }
83
        Note(double _f, RealTime _t, RealTime _d) :
84
            freq(_f), time(_t), duration(_d) { }
85
        double freq;
86
        RealTime time;
87
        RealTime duration;
88
    };
89
    
90
    /**
91
     * Return a single note roughly matching this hypothesis
92
     */
93
    Note getAveragedNote() const;
94
    
95
private:
96
    bool isWithinTolerance(Estimate) const;
97
    bool isOutOfDateFor(Estimate) const;
98
    bool isSatisfied() const;
99
    double getMeanFrequency() const;
100
    
101
    State m_state;
102
    Estimates m_pending;
103
};
104

    
105
}
106

    
107
#endif