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 / test / TestNoteHypothesis.cpp @ 60:c06fe5350b34

History | View | Annotate | Download (12.8 KB)

1 33:5f2a57b1a75a 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 33:5f2a57b1a75a Chris
25 38:944898c2e14e Chris
#include "NoteHypothesis.h"
26 33:5f2a57b1a75a Chris
27 38:944898c2e14e Chris
std::ostream &operator<<(std::ostream &out, const NoteHypothesis::Estimate &n)
28 33:5f2a57b1a75a Chris
{
29 38:944898c2e14e Chris
    return out << "[" << n.freq << "@" << n.time << ":" << n.confidence << "]" << std::endl;
30 33:5f2a57b1a75a Chris
}
31
32 38:944898c2e14e Chris
std::ostream &operator<<(std::ostream &out, const NoteHypothesis::Estimates &e)
33
{
34
    out << "( ";
35
    for (int i = 0; i < (int)e.size(); ++i) out << e[i] << "; ";
36
    out << " )";
37
    return out;
38
}
39
40
std::ostream &operator<<(std::ostream &out, const NoteHypothesis::Note &n)
41
{
42
    return out << "[" << n.freq << "@" << n.time << ":" << n.duration << "]" << std::endl;
43
}
44
45
#define BOOST_TEST_DYN_LINK
46
#define BOOST_TEST_MAIN
47
48
#include <boost/test/unit_test.hpp>
49
50
using Vamp::RealTime;
51
52
BOOST_AUTO_TEST_SUITE(TestNoteHypothesis)
53
54
BOOST_AUTO_TEST_CASE(emptyAccept)
55
{
56 58:9f50a5876dd3 Chris
    // An empty hypothesis should accept any estimate with a
57
    // non-negligible confidence, and enter provisional state
58 38:944898c2e14e Chris
    NoteHypothesis h;
59 58:9f50a5876dd3 Chris
    NoteHypothesis::Estimate e; // default estimate has confidence 1
60 38:944898c2e14e Chris
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
61
    BOOST_CHECK(h.accept(e));
62
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
63
}
64 58:9f50a5876dd3 Chris
65
BOOST_AUTO_TEST_CASE(noConfidence)
66
{
67
    // A hypothesis should reject any estimate that has a negligible
68
    // confidence
69
    NoteHypothesis h;
70
    NoteHypothesis::Estimate e;
71
    e.confidence = 0;
72
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
73
    BOOST_CHECK(!h.accept(e));
74
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Rejected);
75
}
76 60:c06fe5350b34 Chris
77
BOOST_AUTO_TEST_CASE(noConfidenceIgnore)
78
{
79
    // But if we're already in process we don't go to rejected state,
80
    // we just ignore this hypothesis
81
    NoteHypothesis h;
82
    NoteHypothesis::Estimate e;
83
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
84
    BOOST_CHECK(h.accept(e));
85
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
86
    e.confidence = 0;
87
    BOOST_CHECK(!h.accept(e));
88
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
89
}
90 45:8db4a1f096f0 Chris
91
BOOST_AUTO_TEST_CASE(tooSlow)
92
{
93
    // Having accepted a first estimate, a hypothesis should reject a
94
    // second (and enter rejected state) if there is too long a gap
95
    // between them for them to belong to a single note
96
    NoteHypothesis h;
97
    NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
98
    NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(50), 1);
99
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
100
    BOOST_CHECK(h.accept(e1));
101
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
102
    BOOST_CHECK(!h.accept(e2));
103
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Rejected);
104
}
105 38:944898c2e14e Chris
106
BOOST_AUTO_TEST_CASE(simpleSatisfy)
107
{
108 45:8db4a1f096f0 Chris
    // A hypothesis should enter satisfied state after accepting three
109
    // consistent estimates, and then remain satisfied while accepting
110
    // further consistent estimates
111
    NoteHypothesis h;
112
    NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
113
    NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(10), 1);
114
    NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 1);
115
    NoteHypothesis::Estimate e4(500, RealTime::fromMilliseconds(30), 1);
116
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
117
    BOOST_CHECK(h.accept(e1));
118
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
119
    BOOST_CHECK(h.accept(e2));
120
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
121
    BOOST_CHECK(h.accept(e3));
122
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
123
    BOOST_CHECK(h.accept(e4));
124
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
125
}
126
127
BOOST_AUTO_TEST_CASE(expiry)
128
{
129
    // A hypothesis that has been satisfied, but that is subsequently
130
    // offered an estimate that follows too long a gap, should enter
131
    // expired state rather than rejected state (showing that it has a
132
    // valid note but that the note has apparently finished)
133 38:944898c2e14e Chris
    NoteHypothesis h;
134
    NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
135
    NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(10), 1);
136
    NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 1);
137
    NoteHypothesis::Estimate e4(500, RealTime::fromMilliseconds(30), 1);
138
    NoteHypothesis::Estimate e5(500, RealTime::fromMilliseconds(80), 1);
139
    NoteHypothesis::Estimate e6(500, RealTime::fromMilliseconds(90), 1);
140
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
141
    BOOST_CHECK(h.accept(e1));
142
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
143
    BOOST_CHECK(h.accept(e2));
144
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
145
    BOOST_CHECK(h.accept(e3));
146
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
147
    BOOST_CHECK(h.accept(e4));
148
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
149
    BOOST_CHECK(!h.accept(e5));
150
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Expired);
151
    BOOST_CHECK(!h.accept(e6));
152
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Expired);
153
}
154
155 45:8db4a1f096f0 Chris
BOOST_AUTO_TEST_CASE(strayReject1)
156 38:944898c2e14e Chris
{
157 45:8db4a1f096f0 Chris
    // A wildly different frequency occurring in the middle of a
158
    // provisionally accepted note should be ignored
159 38:944898c2e14e Chris
    NoteHypothesis h;
160
    NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
161
    NoteHypothesis::Estimate e2(1000, RealTime::fromMilliseconds(10), 1);
162
    NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 1);
163
    NoteHypothesis::Estimate e4(500, RealTime::fromMilliseconds(30), 1);
164
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
165
    BOOST_CHECK(h.accept(e1));
166
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
167
    BOOST_CHECK(!h.accept(e2));
168
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
169
    BOOST_CHECK(h.accept(e3));
170
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
171
    BOOST_CHECK(h.accept(e4));
172
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
173
}
174 45:8db4a1f096f0 Chris
175
BOOST_AUTO_TEST_CASE(strayReject2)
176 38:944898c2e14e Chris
{
177 45:8db4a1f096f0 Chris
    // A wildly different frequency occurring in the middle of a
178
    // satisfied note should be ignored
179 38:944898c2e14e Chris
    NoteHypothesis h;
180
    NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
181 45:8db4a1f096f0 Chris
    NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(10), 1);
182
    NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 1);
183
    NoteHypothesis::Estimate e4(1000, RealTime::fromMilliseconds(30), 1);
184
    NoteHypothesis::Estimate e5(500, RealTime::fromMilliseconds(40), 1);
185 38:944898c2e14e Chris
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
186
    BOOST_CHECK(h.accept(e1));
187
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
188 45:8db4a1f096f0 Chris
    BOOST_CHECK(h.accept(e2));
189
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
190
    BOOST_CHECK(h.accept(e3));
191
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
192
    BOOST_CHECK(!h.accept(e4));
193
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
194
    BOOST_CHECK(h.accept(e5));
195
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
196 38:944898c2e14e Chris
}
197
198
BOOST_AUTO_TEST_CASE(weakSatisfy)
199
{
200 45:8db4a1f096f0 Chris
    // Behaviour with slightly varying frequencies should be as for
201
    // that with fixed frequency
202 38:944898c2e14e Chris
    NoteHypothesis h;
203
    NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 0.5);
204
    NoteHypothesis::Estimate e2(502, RealTime::fromMilliseconds(10), 0.5);
205
    NoteHypothesis::Estimate e3(504, RealTime::fromMilliseconds(20), 0.5);
206
    NoteHypothesis::Estimate e4(506, RealTime::fromMilliseconds(30), 0.5);
207
    NoteHypothesis::Estimate e5(508, RealTime::fromMilliseconds(40), 0.5);
208
    NoteHypothesis::Estimate e6(510, RealTime::fromMilliseconds(90), 0.5);
209
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
210
    BOOST_CHECK(h.accept(e1));
211
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
212
    BOOST_CHECK(h.accept(e2));
213
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
214
    BOOST_CHECK(h.accept(e3));
215
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
216
    BOOST_CHECK(h.accept(e4));
217
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
218
    BOOST_CHECK(h.accept(e5));
219
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
220
    BOOST_CHECK(!h.accept(e6));
221
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Expired);
222
}
223
224
BOOST_AUTO_TEST_CASE(frequencyRange)
225
{
226 45:8db4a1f096f0 Chris
    // But there's a limit: outside a certain range we should reject
227
    //!!! (but what is this range? is it part of the spec?)
228 38:944898c2e14e Chris
    NoteHypothesis h;
229
    NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
230
    NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
231
    NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
232
    NoteHypothesis::Estimate e4(470, RealTime::fromMilliseconds(30), 1);
233
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
234
    BOOST_CHECK(h.accept(e1));
235
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
236
    BOOST_CHECK(h.accept(e2));
237
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
238
    BOOST_CHECK(h.accept(e3));
239
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
240
    BOOST_CHECK(!h.accept(e4));
241
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
242
}
243
244
BOOST_AUTO_TEST_CASE(acceptedEstimates)
245
{
246 45:8db4a1f096f0 Chris
    // Check that getAcceptedEstimates() returns the right result
247 38:944898c2e14e Chris
    NoteHypothesis h;
248
    NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
249
    NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
250
    NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
251
    NoteHypothesis::Estimate e4(470, RealTime::fromMilliseconds(30), 1);
252
    NoteHypothesis::Estimate e5(444, RealTime::fromMilliseconds(90), 1);
253
    NoteHypothesis::Estimates es;
254
    es.push_back(e1);
255
    es.push_back(e2);
256
    es.push_back(e3);
257
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::New);
258
    BOOST_CHECK_EQUAL(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
259
    BOOST_CHECK(h.accept(e1));
260
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
261
    BOOST_CHECK_EQUAL(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
262
    BOOST_CHECK(h.accept(e2));
263
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Provisional);
264
    BOOST_CHECK_EQUAL(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
265
    BOOST_CHECK(h.accept(e3));
266
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
267
    BOOST_CHECK_EQUAL(h.getAcceptedEstimates(), es);
268
    BOOST_CHECK(!h.accept(e4));
269
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Satisfied);
270
    BOOST_CHECK_EQUAL(h.getAcceptedEstimates(), es);
271
    BOOST_CHECK(!h.accept(e5));
272
    BOOST_CHECK_EQUAL(h.getState(), NoteHypothesis::Expired);
273
    BOOST_CHECK_EQUAL(h.getAcceptedEstimates(), es);
274
}
275
276
BOOST_AUTO_TEST_CASE(meanFrequency)
277
{
278 45:8db4a1f096f0 Chris
    // Check that the mean frequency is the mean of the frequencies
279 38:944898c2e14e Chris
    NoteHypothesis h;
280
    NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
281
    NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
282
    NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
283
    BOOST_CHECK(h.accept(e1));
284
    BOOST_CHECK(h.accept(e2));
285
    BOOST_CHECK(h.accept(e3));
286
    BOOST_CHECK_EQUAL(h.getMeanFrequency(), 444.0);
287
}
288
289
BOOST_AUTO_TEST_CASE(averagedNote)
290
{
291 45:8db4a1f096f0 Chris
    // Check that getAveragedNote returns something sane
292 38:944898c2e14e Chris
    NoteHypothesis h;
293
    NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(10), 1);
294
    NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(20), 1);
295
    NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(30), 1);
296
    BOOST_CHECK(h.accept(e1));
297
    BOOST_CHECK(h.accept(e2));
298
    BOOST_CHECK(h.accept(e3));
299
    BOOST_CHECK_EQUAL(h.getAveragedNote(), NoteHypothesis::Note
300
                      (444,
301
                       RealTime::fromMilliseconds(10),
302
                       RealTime::fromMilliseconds(20)));
303
}
304
305 45:8db4a1f096f0 Chris
//!!! Not yet tested: Confidence scores
306
307 38:944898c2e14e Chris
BOOST_AUTO_TEST_SUITE_END()