changeset 470:dd132354ea02

Expand tests a bit, make them fail if they fail
author Chris Cannam <cannam@all-day-breakfast.com>
date Thu, 30 May 2019 14:10:33 +0100
parents 8d84e5d16314
children e3335cb213da
files tests/TestChromagram.cpp tests/TestGetKeyMode.cpp
diffstat 2 files changed, 47 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/tests/TestChromagram.cpp	Thu May 30 14:10:15 2019 +0100
+++ b/tests/TestChromagram.cpp	Thu May 30 14:10:33 2019 +0100
@@ -45,41 +45,43 @@
     return concertA * pow(2.0, (midiPitch - 69.0) / 12.0);
 }
 
-BOOST_AUTO_TEST_CASE(sinusoid_12tET)
+void test_sinusoid_12tET(double concertA, double sampleRate, int bpo)
 {
-    double concertA = 440.0;
-    double sampleRate = 44100.0;
-    int bpo = 60;
+    int chromaMinPitch = 36;
+    int chromaMaxPitch = 108;
 
+    int probeMinPitch = 36;
+    int probeMaxPitch = 108;
+    
     ChromaConfig config {
         sampleRate,
-        frequencyForPitch(36, concertA),
-        frequencyForPitch(108, concertA),
+        frequencyForPitch(chromaMinPitch, concertA),
+        frequencyForPitch(chromaMaxPitch, concertA),
         bpo,
         0.0054,
         MathUtilities::NormaliseNone
     };
     
     Chromagram chroma(config);
+
+    int binsPerSemi = bpo / 12;
     
-    for (int midiPitch = 36; midiPitch < 108; ++midiPitch) {
-
-        cout << endl;
+    for (int midiPitch = probeMinPitch;
+         midiPitch < probeMaxPitch;
+         ++midiPitch) {
 
         int blockSize = chroma.getFrameSize();
-        int hopSize = chroma.getHopSize();
-        cerr << "blockSize = " << blockSize
-             << ", hopSize = " << hopSize << endl;
 
         double frequency = frequencyForPitch(midiPitch, concertA);
-        int expectedPeakBin = ((midiPitch - 36) * 5) % bpo;
-
+        int expectedPeakBin =
+            ((midiPitch - chromaMinPitch) * binsPerSemi) % bpo;
+/*
         cout << "midiPitch = " << midiPitch
              << ", name = " << midiPitchName(midiPitch)
              << ", frequency = " << frequency
              << ", expected peak bin = "
              << expectedPeakBin << endl;
-        
+*/        
         vector<double> signal = generateSinusoid(frequency,
                                                  sampleRate,
                                                  blockSize);
@@ -95,14 +97,13 @@
                 peakBin = i;
             }
         }
-
+/*
         cout << "peak value = " << peakValue << " at bin " << peakBin << endl;
         cout << "(neighbouring values are "
              << (peakBin > 0 ? output[peakBin-1] : output[bpo-1])
              << " and "
              << (peakBin+1 < bpo ? output[peakBin+1] : output[0])
              << ")" << endl;
-
         if (peakBin != expectedPeakBin) {
             cout << "NOTE: peak bin " << peakBin << " does not match expected " << expectedPeakBin << endl;
             cout << "bin values are: ";
@@ -111,7 +112,30 @@
             }
             cout << endl;
         }
+*/
+        
+        BOOST_CHECK_EQUAL(peakBin, expectedPeakBin);
     }
 }
 
+BOOST_AUTO_TEST_CASE(sinusoid_12tET_440_44100_36)
+{
+    test_sinusoid_12tET(440.0, 44100.0, 36);
+}
+    
+BOOST_AUTO_TEST_CASE(sinusoid_12tET_440_44100_60)
+{
+    test_sinusoid_12tET(440.0, 44100.0, 60);
+}
+
+BOOST_AUTO_TEST_CASE(sinusoid_12tET_397_44100_60)
+{
+    test_sinusoid_12tET(397.0, 44100.0, 60);
+}
+
+BOOST_AUTO_TEST_CASE(sinusoid_12tET_440_48000_60)
+{
+    test_sinusoid_12tET(440.0, 48000.0, 60);
+}
+
 BOOST_AUTO_TEST_SUITE_END()
--- a/tests/TestGetKeyMode.cpp	Thu May 30 14:10:15 2019 +0100
+++ b/tests/TestGetKeyMode.cpp	Thu May 30 14:10:33 2019 +0100
@@ -70,19 +70,16 @@
     
     for (int midiPitch = 48; midiPitch < 96; ++midiPitch) {
 
-        cout << endl;
-
         GetKeyMode gkm(sampleRate, concertA, 10, 10);
         int blockSize = gkm.getBlockSize();
         int hopSize = gkm.getHopSize();
-        cerr << "blockSize = " << blockSize
-             << ", hopSize = " << hopSize << endl;
 
         double frequency = concertA * pow(2.0, (midiPitch - 69.0) / 12.0);
+/*
         cout << "midiPitch = " << midiPitch
              << ", name = " << midiPitchName(midiPitch)
              << ", frequency = " << frequency << endl;
-
+*/
         int blocks = 4;
         int totalLength = blockSize * blocks;
         vector<double> signal = generateSinusoid(frequency, sampleRate,
@@ -105,8 +102,10 @@
         int tonic = key;
         if (minor) tonic -= 12;
 
-        string name = keyName(tonic, minor);
-        cout << "key value = " << key << ", name = " << name << endl;
+        BOOST_CHECK_EQUAL(tonic, 1 + (midiPitch % 12));
+        
+//        string name = keyName(tonic, minor);
+//        cout << "key value = " << key << ", name = " << name << endl;
     }
 }