annotate 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
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@892 75 #define MIDDLE_C 261.6255653f
Chris@892 76
Chris@892 77 void frequencyForPitch()
Chris@892 78 {
Chris@892 79 QCOMPARE(Pitch::getFrequencyForPitch(60, 0), MIDDLE_C);
Chris@892 80 QCOMPARE(Pitch::getFrequencyForPitch(69, 0), 440.f);
Chris@892 81 QCOMPARE(Pitch::getFrequencyForPitch(60, 0, 220), MIDDLE_C / 2.f);
Chris@892 82 QCOMPARE(Pitch::getFrequencyForPitch(69, 0, 220), 220.f);
Chris@892 83 }
Chris@892 84
Chris@892 85 void pitchForFrequency()
Chris@892 86 {
Chris@892 87 float centsOffset = 0.f;
Chris@892 88 QCOMPARE(Pitch::getPitchForFrequency(MIDDLE_C, &centsOffset), 60);
Chris@892 89 QCOMPARE(centsOffset, 0.f);
Chris@892 90 QCOMPARE(Pitch::getPitchForFrequency(261.0, &centsOffset), 60);
Chris@892 91 QCOMPARE(int(centsOffset), -4);
Chris@892 92 QCOMPARE(Pitch::getPitchForFrequency(440.0, &centsOffset), 69);
Chris@892 93 QCOMPARE(centsOffset, 0.f);
Chris@892 94 }
Chris@892 95 };
Chris@892 96
Chris@892 97 #endif