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.cpp @ 59:82552664d471

History | View | Annotate | Download (4.95 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
#include "NoteHypothesis.h"
26

    
27
#include <cmath>
28

    
29
using Vamp::RealTime;
30

    
31
NoteHypothesis::NoteHypothesis()
32
{
33
    m_state = New;
34
}
35

    
36
NoteHypothesis::~NoteHypothesis()
37
{
38
}
39

    
40
bool
41
NoteHypothesis::isWithinTolerance(Estimate s) const
42
{
43
    if (m_pending.empty()) {
44
        return true;
45
    }
46

    
47
    // check we are within a relatively close tolerance of the last
48
    // candidate
49
    Estimate last = m_pending[m_pending.size()-1];
50
    double r = s.freq / last.freq;
51
    int cents = lrint(1200.0 * (log(r) / log(2.0)));
52
    if (cents < -60 || cents > 60) return false;
53

    
54
    // and within a slightly bigger tolerance of the current mean
55
    double meanFreq = getMeanFrequency();
56
    r = s.freq / meanFreq;
57
    cents = lrint(1200.0 * (log(r) / log(2.0)));
58
    if (cents < -80 || cents > 80) return false;
59
    
60
    return true;
61
}
62

    
63
bool
64
NoteHypothesis::isOutOfDateFor(Estimate s) const
65
{
66
    if (m_pending.empty()) return false;
67
    return ((s.time - m_pending[m_pending.size()-1].time) > 
68
            RealTime::fromMilliseconds(40));
69
}
70

    
71
bool 
72
NoteHypothesis::isSatisfied() const
73
{
74
    if (m_pending.empty()) return false;
75
    
76
    double meanConfidence = 0.0;
77
    for (int i = 0; i < (int)m_pending.size(); ++i) {
78
        meanConfidence += m_pending[i].confidence;
79
    }
80
    meanConfidence /= m_pending.size();
81

    
82
    int lengthRequired = 100;
83
    if (meanConfidence > 0.0) {
84
        lengthRequired = int(2.0 / meanConfidence + 0.5);
85
    }
86

    
87
    return ((int)m_pending.size() > lengthRequired);
88
}
89

    
90
bool
91
NoteHypothesis::accept(Estimate s)
92
{
93
    bool accept = false;
94

    
95
    static double negligibleConfidence = 0.0001;
96

    
97
    if (s.confidence < negligibleConfidence) {
98
        // avoid piling up a lengthy sequence of estimates that are
99
        // all acceptable but are in total not enough to cause us to
100
        // be satisfied
101
        m_state = Rejected;
102
        return false;
103
    }
104

    
105
    switch (m_state) {
106

    
107
    case New:
108
        m_state = Provisional;
109
        accept = true;
110
        break;
111

    
112
    case Provisional:
113
        if (isOutOfDateFor(s)) {
114
            m_state = Rejected;
115
        } else if (isWithinTolerance(s)) {
116
            accept = true;
117
        }
118
        break;
119
        
120
    case Satisfied:
121
        if (isOutOfDateFor(s)) {
122
            m_state = Expired;
123
        } else if (isWithinTolerance(s)) {
124
            accept = true;
125
        }
126
        break;
127

    
128
    case Rejected:
129
        break;
130

    
131
    case Expired:
132
        break;
133
    }
134

    
135
    if (accept) {
136
        m_pending.push_back(s);
137
        if (m_state == Provisional && isSatisfied()) {
138
            m_state = Satisfied;
139
        }
140
    }
141

    
142
    return accept;
143
}        
144

    
145
NoteHypothesis::State
146
NoteHypothesis::getState() const
147
{
148
    return m_state;
149
}
150

    
151
NoteHypothesis::Estimates
152
NoteHypothesis::getAcceptedEstimates() const
153
{
154
    if (m_state == Satisfied || m_state == Expired) {
155
        return m_pending;
156
    } else {
157
        return Estimates();
158
    }
159
}
160

    
161
RealTime
162
NoteHypothesis::getStartTime() const
163
{
164
    if (!(m_state == Satisfied || m_state == Expired)) {
165
        return RealTime::zeroTime;
166
    } else {
167
        return m_pending.begin()->time;
168
    }
169
}
170

    
171
double
172
NoteHypothesis::getMeanFrequency() const
173
{
174
    double acc = 0.0;
175
    if (m_pending.empty()) return acc;
176
    for (int i = 0; i < (int)m_pending.size(); ++i) {
177
        acc += m_pending[i].freq;
178
    }
179
    acc /= m_pending.size();
180
    return acc;
181
}
182

    
183
NoteHypothesis::Note
184
NoteHypothesis::getAveragedNote() const
185
{
186
    Note n;
187

    
188
    if (!(m_state == Satisfied || m_state == Expired)) {
189
        n.freq = 0.0;
190
        n.time = RealTime::zeroTime;
191
        n.duration = RealTime::zeroTime;
192
        return n;
193
    }
194

    
195
    n.time = m_pending.begin()->time;
196

    
197
    Estimates::const_iterator i = m_pending.end();
198
    --i;
199
    n.duration = i->time - n.time;
200

    
201
    // just mean frequency for now, but this isn't at all right perceptually
202
    n.freq = getMeanFrequency();
203
    
204
    return n;
205
}
206