comparison tests/TestChromagram.cpp @ 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 1db23b9a8da4
children
comparison
equal deleted inserted replaced
469:8d84e5d16314 470:dd132354ea02
43 double frequencyForPitch(int midiPitch, double concertA) 43 double frequencyForPitch(int midiPitch, double concertA)
44 { 44 {
45 return concertA * pow(2.0, (midiPitch - 69.0) / 12.0); 45 return concertA * pow(2.0, (midiPitch - 69.0) / 12.0);
46 } 46 }
47 47
48 BOOST_AUTO_TEST_CASE(sinusoid_12tET) 48 void test_sinusoid_12tET(double concertA, double sampleRate, int bpo)
49 { 49 {
50 double concertA = 440.0; 50 int chromaMinPitch = 36;
51 double sampleRate = 44100.0; 51 int chromaMaxPitch = 108;
52 int bpo = 60;
53 52
53 int probeMinPitch = 36;
54 int probeMaxPitch = 108;
55
54 ChromaConfig config { 56 ChromaConfig config {
55 sampleRate, 57 sampleRate,
56 frequencyForPitch(36, concertA), 58 frequencyForPitch(chromaMinPitch, concertA),
57 frequencyForPitch(108, concertA), 59 frequencyForPitch(chromaMaxPitch, concertA),
58 bpo, 60 bpo,
59 0.0054, 61 0.0054,
60 MathUtilities::NormaliseNone 62 MathUtilities::NormaliseNone
61 }; 63 };
62 64
63 Chromagram chroma(config); 65 Chromagram chroma(config);
66
67 int binsPerSemi = bpo / 12;
64 68
65 for (int midiPitch = 36; midiPitch < 108; ++midiPitch) { 69 for (int midiPitch = probeMinPitch;
66 70 midiPitch < probeMaxPitch;
67 cout << endl; 71 ++midiPitch) {
68 72
69 int blockSize = chroma.getFrameSize(); 73 int blockSize = chroma.getFrameSize();
70 int hopSize = chroma.getHopSize();
71 cerr << "blockSize = " << blockSize
72 << ", hopSize = " << hopSize << endl;
73 74
74 double frequency = frequencyForPitch(midiPitch, concertA); 75 double frequency = frequencyForPitch(midiPitch, concertA);
75 int expectedPeakBin = ((midiPitch - 36) * 5) % bpo; 76 int expectedPeakBin =
76 77 ((midiPitch - chromaMinPitch) * binsPerSemi) % bpo;
78 /*
77 cout << "midiPitch = " << midiPitch 79 cout << "midiPitch = " << midiPitch
78 << ", name = " << midiPitchName(midiPitch) 80 << ", name = " << midiPitchName(midiPitch)
79 << ", frequency = " << frequency 81 << ", frequency = " << frequency
80 << ", expected peak bin = " 82 << ", expected peak bin = "
81 << expectedPeakBin << endl; 83 << expectedPeakBin << endl;
82 84 */
83 vector<double> signal = generateSinusoid(frequency, 85 vector<double> signal = generateSinusoid(frequency,
84 sampleRate, 86 sampleRate,
85 blockSize); 87 blockSize);
86 88
87 double *output = chroma.process(signal.data()); 89 double *output = chroma.process(signal.data());
93 if (i == 0 || output[i] > peakValue) { 95 if (i == 0 || output[i] > peakValue) {
94 peakValue = output[i]; 96 peakValue = output[i];
95 peakBin = i; 97 peakBin = i;
96 } 98 }
97 } 99 }
98 100 /*
99 cout << "peak value = " << peakValue << " at bin " << peakBin << endl; 101 cout << "peak value = " << peakValue << " at bin " << peakBin << endl;
100 cout << "(neighbouring values are " 102 cout << "(neighbouring values are "
101 << (peakBin > 0 ? output[peakBin-1] : output[bpo-1]) 103 << (peakBin > 0 ? output[peakBin-1] : output[bpo-1])
102 << " and " 104 << " and "
103 << (peakBin+1 < bpo ? output[peakBin+1] : output[0]) 105 << (peakBin+1 < bpo ? output[peakBin+1] : output[0])
104 << ")" << endl; 106 << ")" << endl;
105
106 if (peakBin != expectedPeakBin) { 107 if (peakBin != expectedPeakBin) {
107 cout << "NOTE: peak bin " << peakBin << " does not match expected " << expectedPeakBin << endl; 108 cout << "NOTE: peak bin " << peakBin << " does not match expected " << expectedPeakBin << endl;
108 cout << "bin values are: "; 109 cout << "bin values are: ";
109 for (int i = 0; i < bpo; ++i) { 110 for (int i = 0; i < bpo; ++i) {
110 cout << i << ": " << output[i] << " "; 111 cout << i << ": " << output[i] << " ";
111 } 112 }
112 cout << endl; 113 cout << endl;
113 } 114 }
115 */
116
117 BOOST_CHECK_EQUAL(peakBin, expectedPeakBin);
114 } 118 }
115 } 119 }
116 120
121 BOOST_AUTO_TEST_CASE(sinusoid_12tET_440_44100_36)
122 {
123 test_sinusoid_12tET(440.0, 44100.0, 36);
124 }
125
126 BOOST_AUTO_TEST_CASE(sinusoid_12tET_440_44100_60)
127 {
128 test_sinusoid_12tET(440.0, 44100.0, 60);
129 }
130
131 BOOST_AUTO_TEST_CASE(sinusoid_12tET_397_44100_60)
132 {
133 test_sinusoid_12tET(397.0, 44100.0, 60);
134 }
135
136 BOOST_AUTO_TEST_CASE(sinusoid_12tET_440_48000_60)
137 {
138 test_sinusoid_12tET(440.0, 48000.0, 60);
139 }
140
117 BOOST_AUTO_TEST_SUITE_END() 141 BOOST_AUTO_TEST_SUITE_END()