comparison base/test/TestPitch.h @ 892:451f7f3ab6e7

Make octave numbering configurable, and change default to C4 = middle C
author Chris Cannam
date Thu, 27 Mar 2014 13:32:56 +0000
parents
children 88b54a185a0a
comparison
equal deleted inserted replaced
889:7f97a4d9d14f 892:451f7f3ab6e7
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef TEST_PITCH_H
16 #define TEST_PITCH_H
17
18 #include "../Pitch.h"
19 #include "../Preferences.h"
20
21 #include <QObject>
22 #include <QtTest>
23 #include <QDir>
24
25 #include <iostream>
26
27 using namespace std;
28
29 class TestPitch : public QObject
30 {
31 Q_OBJECT
32
33 private slots:
34 void init() {
35 Preferences::getInstance()->setOctaveOfMiddleC(4);
36 Preferences::getInstance()->setTuningFrequency(440);
37 }
38
39 void pitchLabel()
40 {
41 QCOMPARE(Pitch::getPitchLabel(60, 0, false), QString("C4"));
42 QCOMPARE(Pitch::getPitchLabel(69, 0, false), QString("A4"));
43 QCOMPARE(Pitch::getPitchLabel(61, 0, false), QString("C#4"));
44 QCOMPARE(Pitch::getPitchLabel(61, 0, true), QString("Db4"));
45 QCOMPARE(Pitch::getPitchLabel(59, 0, false), QString("B3"));
46 QCOMPARE(Pitch::getPitchLabel(59, 0, true), QString("B3"));
47 QCOMPARE(Pitch::getPitchLabel(0, 0, false), QString("C-1"));
48
49 QCOMPARE(Pitch::getPitchLabel(60, -40, false), QString("C4-40c"));
50 QCOMPARE(Pitch::getPitchLabel(60, 40, false), QString("C4+40c"));
51 QCOMPARE(Pitch::getPitchLabel(58, 4, false), QString("A#3+4c"));
52
53 Preferences::getInstance()->setOctaveOfMiddleC(3);
54
55 QCOMPARE(Pitch::getPitchLabel(60, 0, false), QString("C3"));
56 QCOMPARE(Pitch::getPitchLabel(69, 0, false), QString("A3"));
57 QCOMPARE(Pitch::getPitchLabel(61, 0, false), QString("C#3"));
58 QCOMPARE(Pitch::getPitchLabel(61, 0, true), QString("Db3"));
59 QCOMPARE(Pitch::getPitchLabel(59, 0, false), QString("B2"));
60 QCOMPARE(Pitch::getPitchLabel(59, 0, true), QString("B2"));
61 QCOMPARE(Pitch::getPitchLabel(0, 0, false), QString("C-2"));
62
63 QCOMPARE(Pitch::getPitchLabel(60, -40, false), QString("C3-40c"));
64 QCOMPARE(Pitch::getPitchLabel(60, 40, false), QString("C3+40c"));
65 QCOMPARE(Pitch::getPitchLabel(58, 4, false), QString("A#2+4c"));
66 }
67
68 void pitchLabelForFrequency()
69 {
70 QCOMPARE(Pitch::getPitchLabelForFrequency(440, 440, false), QString("A4"));
71 QCOMPARE(Pitch::getPitchLabelForFrequency(440, 220, false), QString("A5"));
72 QCOMPARE(Pitch::getPitchLabelForFrequency(261.63, 440, false), QString("C4"));
73 }
74
75 #define MIDDLE_C 261.6255653f
76
77 void frequencyForPitch()
78 {
79 QCOMPARE(Pitch::getFrequencyForPitch(60, 0), MIDDLE_C);
80 QCOMPARE(Pitch::getFrequencyForPitch(69, 0), 440.f);
81 QCOMPARE(Pitch::getFrequencyForPitch(60, 0, 220), MIDDLE_C / 2.f);
82 QCOMPARE(Pitch::getFrequencyForPitch(69, 0, 220), 220.f);
83 }
84
85 void pitchForFrequency()
86 {
87 float centsOffset = 0.f;
88 QCOMPARE(Pitch::getPitchForFrequency(MIDDLE_C, &centsOffset), 60);
89 QCOMPARE(centsOffset, 0.f);
90 QCOMPARE(Pitch::getPitchForFrequency(261.0, &centsOffset), 60);
91 QCOMPARE(int(centsOffset), -4);
92 QCOMPARE(Pitch::getPitchForFrequency(440.0, &centsOffset), 69);
93 QCOMPARE(centsOffset, 0.f);
94 }
95 };
96
97 #endif