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 @ 60:c06fe5350b34

History | View | Annotate | Download (4.99 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
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 59:82552664d471 Chris
    int lengthRequired = 100;
83 32:c88a9972975b Chris
    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 58:9f50a5876dd3 Chris
    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 60:c06fe5350b34 Chris
        if (m_pending.empty()) {
102
            m_state = Rejected;
103
        }
104 58:9f50a5876dd3 Chris
        return false;
105
    }
106
107 32:c88a9972975b Chris
    switch (m_state) {
108
109
    case New:
110
        m_state = Provisional;
111
        accept = true;
112
        break;
113
114
    case Provisional:
115
        if (isOutOfDateFor(s)) {
116
            m_state = Rejected;
117
        } else if (isWithinTolerance(s)) {
118
            accept = true;
119
        }
120
        break;
121
122
    case Satisfied:
123
        if (isOutOfDateFor(s)) {
124
            m_state = Expired;
125
        } else if (isWithinTolerance(s)) {
126
            accept = true;
127
        }
128
        break;
129
130
    case Rejected:
131
        break;
132
133
    case Expired:
134
        break;
135
    }
136
137
    if (accept) {
138
        m_pending.push_back(s);
139
        if (m_state == Provisional && isSatisfied()) {
140
            m_state = Satisfied;
141
        }
142
    }
143
144
    return accept;
145
}
146
147
NoteHypothesis::State
148
NoteHypothesis::getState() const
149
{
150
    return m_state;
151
}
152
153
NoteHypothesis::Estimates
154
NoteHypothesis::getAcceptedEstimates() const
155
{
156
    if (m_state == Satisfied || m_state == Expired) {
157
        return m_pending;
158
    } else {
159
        return Estimates();
160
    }
161
}
162
163 52:34f42a384a7f Chris
RealTime
164
NoteHypothesis::getStartTime() const
165
{
166
    if (!(m_state == Satisfied || m_state == Expired)) {
167
        return RealTime::zeroTime;
168
    } else {
169
        return m_pending.begin()->time;
170
    }
171
}
172
173 32:c88a9972975b Chris
double
174
NoteHypothesis::getMeanFrequency() const
175
{
176
    double acc = 0.0;
177 34:3fb9c657d86b Chris
    if (m_pending.empty()) return acc;
178 32:c88a9972975b Chris
    for (int i = 0; i < (int)m_pending.size(); ++i) {
179
        acc += m_pending[i].freq;
180
    }
181
    acc /= m_pending.size();
182
    return acc;
183
}
184
185
NoteHypothesis::Note
186
NoteHypothesis::getAveragedNote() const
187
{
188
    Note n;
189
190
    if (!(m_state == Satisfied || m_state == Expired)) {
191
        n.freq = 0.0;
192
        n.time = RealTime::zeroTime;
193
        n.duration = RealTime::zeroTime;
194
        return n;
195
    }
196
197
    n.time = m_pending.begin()->time;
198
199
    Estimates::const_iterator i = m_pending.end();
200
    --i;
201
    n.duration = i->time - n.time;
202
203
    // just mean frequency for now, but this isn't at all right perceptually
204
    n.freq = getMeanFrequency();
205
206
    return n;
207
}