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 @ 37:7bf67d2dfc30

History | View | Annotate | Download (7.3 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
#ifndef TEST_NOTE_HYPOTHESIS_H
26
#define TEST_NOTE_HYPOTHESIS_H
27

    
28
#include "base/NoteHypothesis.h"
29

    
30
#include <QObject>
31
#include <QtTest>
32

    
33
namespace Turbot {
34

    
35
class TestNoteHypothesis : public QObject
36
{
37
    Q_OBJECT
38

    
39
private slots:
40
    void emptyAccept() {
41
        NoteHypothesis h;
42
        NoteHypothesis::Estimate e;
43
        QCOMPARE(h.getState(), NoteHypothesis::New);
44
        QVERIFY(h.accept(e));
45
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
46
    }
47

    
48
    void simpleSatisfy() {
49
        NoteHypothesis h;
50
        NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
51
        NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(10), 1);
52
        NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 1);
53
        NoteHypothesis::Estimate e4(500, RealTime::fromMilliseconds(30), 1);
54
        NoteHypothesis::Estimate e5(500, RealTime::fromMilliseconds(80), 1);
55
        NoteHypothesis::Estimate e6(500, RealTime::fromMilliseconds(90), 1);
56
        QCOMPARE(h.getState(), NoteHypothesis::New);
57
        QVERIFY(h.accept(e1));
58
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
59
        QVERIFY(h.accept(e2));
60
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
61
        QVERIFY(h.accept(e3));
62
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
63
        QVERIFY(h.accept(e4));
64
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
65
        QVERIFY(!h.accept(e5));
66
        QCOMPARE(h.getState(), NoteHypothesis::Expired);
67
        QVERIFY(!h.accept(e6));
68
        QCOMPARE(h.getState(), NoteHypothesis::Expired);
69
    }
70
        
71
    void strayReject() {
72
        NoteHypothesis h;
73
        NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
74
        NoteHypothesis::Estimate e2(1000, RealTime::fromMilliseconds(10), 1);
75
        NoteHypothesis::Estimate e3(500, RealTime::fromMilliseconds(20), 1);
76
        NoteHypothesis::Estimate e4(500, RealTime::fromMilliseconds(30), 1);
77
        QCOMPARE(h.getState(), NoteHypothesis::New);
78
        QVERIFY(h.accept(e1));
79
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
80
        QVERIFY(!h.accept(e2));
81
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
82
        QVERIFY(h.accept(e3));
83
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
84
        QVERIFY(h.accept(e4));
85
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
86
    }
87
                
88
    void tooSlow() {
89
        NoteHypothesis h;
90
        NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 1);
91
        NoteHypothesis::Estimate e2(500, RealTime::fromMilliseconds(50), 1);
92
        QCOMPARE(h.getState(), NoteHypothesis::New);
93
        QVERIFY(h.accept(e1));
94
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
95
        QVERIFY(!h.accept(e2));
96
        QCOMPARE(h.getState(), NoteHypothesis::Rejected);
97
    }
98
        
99
    void weakSatisfy() {
100
        NoteHypothesis h;
101
        NoteHypothesis::Estimate e1(500, RealTime::fromMilliseconds(0), 0.5);
102
        NoteHypothesis::Estimate e2(502, RealTime::fromMilliseconds(10), 0.5);
103
        NoteHypothesis::Estimate e3(504, RealTime::fromMilliseconds(20), 0.5);
104
        NoteHypothesis::Estimate e4(506, RealTime::fromMilliseconds(30), 0.5);
105
        NoteHypothesis::Estimate e5(508, RealTime::fromMilliseconds(40), 0.5);
106
        NoteHypothesis::Estimate e6(510, RealTime::fromMilliseconds(90), 0.5);
107
        QCOMPARE(h.getState(), NoteHypothesis::New);
108
        QVERIFY(h.accept(e1));
109
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
110
        QVERIFY(h.accept(e2));
111
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
112
        QVERIFY(h.accept(e3));
113
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
114
        QVERIFY(h.accept(e4));
115
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
116
        QVERIFY(h.accept(e5));
117
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
118
        QVERIFY(!h.accept(e6));
119
        QCOMPARE(h.getState(), NoteHypothesis::Expired);
120
    }
121
        
122
    void frequencyRange() {
123
        NoteHypothesis h;
124
        NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
125
        NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
126
        NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
127
        NoteHypothesis::Estimate e4(470, RealTime::fromMilliseconds(30), 1);
128
        QCOMPARE(h.getState(), NoteHypothesis::New);
129
        QVERIFY(h.accept(e1));
130
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
131
        QVERIFY(h.accept(e2));
132
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
133
        QVERIFY(h.accept(e3));
134
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
135
        QVERIFY(!h.accept(e4));
136
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
137
    }
138

    
139
    void acceptedEstimates() {
140
        NoteHypothesis h;
141
        NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
142
        NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
143
        NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
144
        NoteHypothesis::Estimate e4(470, RealTime::fromMilliseconds(30), 1);
145
        NoteHypothesis::Estimate e5(444, RealTime::fromMilliseconds(90), 1);
146
        NoteHypothesis::Estimates es;
147
        es.push_back(e1);
148
        es.push_back(e2);
149
        es.push_back(e3);
150
        QCOMPARE(h.getState(), NoteHypothesis::New);
151
        QCOMPARE(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
152
        QVERIFY(h.accept(e1));
153
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
154
        QCOMPARE(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
155
        QVERIFY(h.accept(e2));
156
        QCOMPARE(h.getState(), NoteHypothesis::Provisional);
157
        QCOMPARE(h.getAcceptedEstimates(), NoteHypothesis::Estimates());
158
        QVERIFY(h.accept(e3));
159
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
160
        QCOMPARE(h.getAcceptedEstimates(), es);
161
        QVERIFY(!h.accept(e4));
162
        QCOMPARE(h.getState(), NoteHypothesis::Satisfied);
163
        QCOMPARE(h.getAcceptedEstimates(), es);
164
        QVERIFY(!h.accept(e5));
165
        QCOMPARE(h.getState(), NoteHypothesis::Expired);
166
        QCOMPARE(h.getAcceptedEstimates(), es);
167
    }
168
        
169
    void meanFrequency() {
170
        NoteHypothesis h;
171
        NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(0), 1);
172
        NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(10), 1);
173
        NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(20), 1);
174
        QVERIFY(h.accept(e1));
175
        QVERIFY(h.accept(e2));
176
        QVERIFY(h.accept(e3));
177
        QCOMPARE(h.getMeanFrequency(), 444.0);
178
    }
179

    
180
    void averagedNote() {
181
        NoteHypothesis h;
182
        NoteHypothesis::Estimate e1(440, RealTime::fromMilliseconds(10), 1);
183
        NoteHypothesis::Estimate e2(448, RealTime::fromMilliseconds(20), 1);
184
        NoteHypothesis::Estimate e3(444, RealTime::fromMilliseconds(30), 1);
185
        QVERIFY(h.accept(e1));
186
        QVERIFY(h.accept(e2));
187
        QVERIFY(h.accept(e3));
188
        QCOMPARE(h.getAveragedNote(), NoteHypothesis::Note
189
                 (444,
190
                  RealTime::fromMilliseconds(10),
191
                  RealTime::fromMilliseconds(20)));
192
    }
193

    
194
        
195
    
196
};
197

    
198
}
199

    
200
#endif