annotate base/test/TestPitch.h @ 1693:718ce5fb9fec single-point

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