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 @ 66:7ad142c710c6

History | View | Annotate | Download (5.03 KB)

1 32:c88a9972975b Chris
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
2 37:7bf67d2dfc30 Chris
/*
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 32:c88a9972975b Chris
25
#include "NoteHypothesis.h"
26
27
#include <cmath>
28
29 35:2f5b169e4a3b Chris
using Vamp::RealTime;
30 32:c88a9972975b Chris
31 66:7ad142c710c6 Chris
NoteHypothesis::NoteHypothesis(float slack) :
32
    m_state(New),
33
    m_slack(slack)
34 32:c88a9972975b Chris
{
35
}
36
37
NoteHypothesis::~NoteHypothesis()
38
{
39
}
40
41
bool
42
NoteHypothesis::isWithinTolerance(Estimate s) const
43
{
44
    if (m_pending.empty()) {
45
        return true;
46
    }
47
48
    // check we are within a relatively close tolerance of the last
49
    // candidate
50
    Estimate last = m_pending[m_pending.size()-1];
51
    double r = s.freq / last.freq;
52
    int cents = lrint(1200.0 * (log(r) / log(2.0)));
53
    if (cents < -60 || cents > 60) return false;
54
55
    // and within a slightly bigger tolerance of the current mean
56
    double meanFreq = getMeanFrequency();
57
    r = s.freq / meanFreq;
58
    cents = lrint(1200.0 * (log(r) / log(2.0)));
59
    if (cents < -80 || cents > 80) return false;
60
61
    return true;
62
}
63
64
bool
65
NoteHypothesis::isOutOfDateFor(Estimate s) const
66
{
67
    if (m_pending.empty()) return false;
68
    return ((s.time - m_pending[m_pending.size()-1].time) >
69 66:7ad142c710c6 Chris
            RealTime::fromMilliseconds(m_slack));
70 32:c88a9972975b Chris
}
71
72
bool
73
NoteHypothesis::isSatisfied() const
74
{
75
    if (m_pending.empty()) return false;
76
77
    double meanConfidence = 0.0;
78
    for (int i = 0; i < (int)m_pending.size(); ++i) {
79
        meanConfidence += m_pending[i].confidence;
80
    }
81
    meanConfidence /= m_pending.size();
82
83 59:82552664d471 Chris
    int lengthRequired = 100;
84 32:c88a9972975b Chris
    if (meanConfidence > 0.0) {
85
        lengthRequired = int(2.0 / meanConfidence + 0.5);
86
    }
87
88
    return ((int)m_pending.size() > lengthRequired);
89
}
90
91
bool
92
NoteHypothesis::accept(Estimate s)
93
{
94
    bool accept = false;
95
96 58:9f50a5876dd3 Chris
    static double negligibleConfidence = 0.0001;
97
98
    if (s.confidence < negligibleConfidence) {
99
        // avoid piling up a lengthy sequence of estimates that are
100
        // all acceptable but are in total not enough to cause us to
101
        // be satisfied
102 60:c06fe5350b34 Chris
        if (m_pending.empty()) {
103
            m_state = Rejected;
104
        }
105 58:9f50a5876dd3 Chris
        return false;
106
    }
107
108 32:c88a9972975b Chris
    switch (m_state) {
109
110
    case New:
111
        m_state = Provisional;
112
        accept = true;
113
        break;
114
115
    case Provisional:
116
        if (isOutOfDateFor(s)) {
117
            m_state = Rejected;
118
        } else if (isWithinTolerance(s)) {
119
            accept = true;
120
        }
121
        break;
122
123
    case Satisfied:
124
        if (isOutOfDateFor(s)) {
125
            m_state = Expired;
126
        } else if (isWithinTolerance(s)) {
127
            accept = true;
128
        }
129
        break;
130
131
    case Rejected:
132
        break;
133
134
    case Expired:
135
        break;
136
    }
137
138
    if (accept) {
139
        m_pending.push_back(s);
140
        if (m_state == Provisional && isSatisfied()) {
141
            m_state = Satisfied;
142
        }
143
    }
144
145
    return accept;
146
}
147
148
NoteHypothesis::State
149
NoteHypothesis::getState() const
150
{
151
    return m_state;
152
}
153
154
NoteHypothesis::Estimates
155
NoteHypothesis::getAcceptedEstimates() const
156
{
157
    if (m_state == Satisfied || m_state == Expired) {
158
        return m_pending;
159
    } else {
160
        return Estimates();
161
    }
162
}
163
164 52:34f42a384a7f Chris
RealTime
165
NoteHypothesis::getStartTime() const
166
{
167
    if (!(m_state == Satisfied || m_state == Expired)) {
168
        return RealTime::zeroTime;
169
    } else {
170
        return m_pending.begin()->time;
171
    }
172
}
173
174 32:c88a9972975b Chris
double
175
NoteHypothesis::getMeanFrequency() const
176
{
177
    double acc = 0.0;
178 34:3fb9c657d86b Chris
    if (m_pending.empty()) return acc;
179 32:c88a9972975b Chris
    for (int i = 0; i < (int)m_pending.size(); ++i) {
180
        acc += m_pending[i].freq;
181
    }
182
    acc /= m_pending.size();
183
    return acc;
184
}
185
186
NoteHypothesis::Note
187
NoteHypothesis::getAveragedNote() const
188
{
189
    Note n;
190
191
    if (!(m_state == Satisfied || m_state == Expired)) {
192
        n.freq = 0.0;
193
        n.time = RealTime::zeroTime;
194
        n.duration = RealTime::zeroTime;
195
        return n;
196
    }
197
198
    n.time = m_pending.begin()->time;
199
200
    Estimates::const_iterator i = m_pending.end();
201
    --i;
202
    n.duration = i->time - n.time;
203
204
    // just mean frequency for now, but this isn't at all right perceptually
205
    n.freq = getMeanFrequency();
206
207
    return n;
208
}