annotate base/test/TestPitch.h @ 1025:88b54a185a0a

Use double instead of float for frequencies in Pitch, just for confidence
author Chris Cannam
date Mon, 08 Dec 2014 15:37:12 +0000
parents 451f7f3ab6e7
children bbc4e4ee15d5
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@892 35 Preferences::getInstance()->setOctaveOfMiddleC(4);
Chris@892 36 Preferences::getInstance()->setTuningFrequency(440);
Chris@892 37 }
Chris@892 38
Chris@892 39 void pitchLabel()
Chris@892 40 {
Chris@892 41 QCOMPARE(Pitch::getPitchLabel(60, 0, false), QString("C4"));
Chris@892 42 QCOMPARE(Pitch::getPitchLabel(69, 0, false), QString("A4"));
Chris@892 43 QCOMPARE(Pitch::getPitchLabel(61, 0, false), QString("C#4"));
Chris@892 44 QCOMPARE(Pitch::getPitchLabel(61, 0, true), QString("Db4"));
Chris@892 45 QCOMPARE(Pitch::getPitchLabel(59, 0, false), QString("B3"));
Chris@892 46 QCOMPARE(Pitch::getPitchLabel(59, 0, true), QString("B3"));
Chris@892 47 QCOMPARE(Pitch::getPitchLabel(0, 0, false), QString("C-1"));
Chris@892 48
Chris@892 49 QCOMPARE(Pitch::getPitchLabel(60, -40, false), QString("C4-40c"));
Chris@892 50 QCOMPARE(Pitch::getPitchLabel(60, 40, false), QString("C4+40c"));
Chris@892 51 QCOMPARE(Pitch::getPitchLabel(58, 4, false), QString("A#3+4c"));
Chris@892 52
Chris@892 53 Preferences::getInstance()->setOctaveOfMiddleC(3);
Chris@892 54
Chris@892 55 QCOMPARE(Pitch::getPitchLabel(60, 0, false), QString("C3"));
Chris@892 56 QCOMPARE(Pitch::getPitchLabel(69, 0, false), QString("A3"));
Chris@892 57 QCOMPARE(Pitch::getPitchLabel(61, 0, false), QString("C#3"));
Chris@892 58 QCOMPARE(Pitch::getPitchLabel(61, 0, true), QString("Db3"));
Chris@892 59 QCOMPARE(Pitch::getPitchLabel(59, 0, false), QString("B2"));
Chris@892 60 QCOMPARE(Pitch::getPitchLabel(59, 0, true), QString("B2"));
Chris@892 61 QCOMPARE(Pitch::getPitchLabel(0, 0, false), QString("C-2"));
Chris@892 62
Chris@892 63 QCOMPARE(Pitch::getPitchLabel(60, -40, false), QString("C3-40c"));
Chris@892 64 QCOMPARE(Pitch::getPitchLabel(60, 40, false), QString("C3+40c"));
Chris@892 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@892 70 QCOMPARE(Pitch::getPitchLabelForFrequency(440, 440, false), QString("A4"));
Chris@892 71 QCOMPARE(Pitch::getPitchLabelForFrequency(440, 220, false), QString("A5"));
Chris@892 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@892 79 QCOMPARE(Pitch::getFrequencyForPitch(60, 0), MIDDLE_C);
Chris@1025 80 QCOMPARE(Pitch::getFrequencyForPitch(69, 0), 440.0);
Chris@1025 81 QCOMPARE(Pitch::getFrequencyForPitch(60, 0, 220), MIDDLE_C / 2.0);
Chris@1025 82 QCOMPARE(Pitch::getFrequencyForPitch(69, 0, 220), 220.0);
Chris@892 83 }
Chris@892 84
Chris@892 85 void pitchForFrequency()
Chris@892 86 {
Chris@1025 87 double centsOffset = 0.0;
Chris@1025 88 QCOMPARE(Pitch::getPitchForFrequency(MIDDLE_C, &centsOffset), 60);
Chris@1025 89 QCOMPARE(centsOffset, 0.0);
Chris@1025 90 QCOMPARE(Pitch::getPitchForFrequency(261.0, &centsOffset), 60);
Chris@1025 91 QCOMPARE(int(centsOffset), -4);
Chris@1025 92 QCOMPARE(Pitch::getPitchForFrequency(440.0, &centsOffset), 69);
Chris@1025 93 QCOMPARE(centsOffset, 0.0);
Chris@1025 94 }
Chris@1025 95
Chris@1025 96 void pitchForFrequencyF()
Chris@1025 97 {
Chris@892 98 float centsOffset = 0.f;
Chris@892 99 QCOMPARE(Pitch::getPitchForFrequency(MIDDLE_C, &centsOffset), 60);
Chris@892 100 QCOMPARE(centsOffset, 0.f);
Chris@892 101 QCOMPARE(Pitch::getPitchForFrequency(261.0, &centsOffset), 60);
Chris@892 102 QCOMPARE(int(centsOffset), -4);
Chris@892 103 QCOMPARE(Pitch::getPitchForFrequency(440.0, &centsOffset), 69);
Chris@892 104 QCOMPARE(centsOffset, 0.f);
Chris@892 105 }
Chris@892 106 };
Chris@892 107
Chris@892 108 #endif