c@451: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@451: c@451: #include "dsp/keydetection/GetKeyMode.h" c@451: c@451: #include c@451: c@451: #include c@451: c@451: #define BOOST_TEST_DYN_LINK c@451: #define BOOST_TEST_MAIN c@451: c@451: #include c@451: c@451: BOOST_AUTO_TEST_SUITE(TestGetKeyMode) c@451: c@451: using std::cout; c@451: using std::endl; c@451: using std::string; c@451: using std::vector; c@451: c@451: string keyName(int index, bool minor) c@451: { c@451: static string namesMajor[] = { c@451: "C", "Db", "D", "Eb", c@451: "E", "F", "F# / Gb", "G", c@451: "Ab", "A", "Bb", "B" c@451: }; c@451: c@451: static string namesMinor[] = { c@451: "C", "C#", "D", "Eb / D#", c@451: "E", "F", "F#", "G", c@451: "G#", "A", "Bb", "B" c@451: }; c@451: c@451: if (index < 1 || index > 12) return ""; c@451: c@451: std::string name; c@451: if (minor) name = namesMinor[index - 1] + " minor"; c@451: else name = namesMajor[index - 1] + " major"; c@451: return name; c@451: } c@451: c@451: string midiPitchName(int midiPitch) c@451: { c@451: static string names[] = { c@451: "C", "C#", "D", "D#", c@451: "E", "F", "F#", "G", c@451: "G#", "A", "A#", "B" c@451: }; c@451: c@451: return names[midiPitch % 12]; c@451: } c@451: c@451: vector generateSinusoid(double frequency, c@451: int sampleRate, c@451: int length) c@451: { c@451: vector buffer; c@451: buffer.reserve(length); c@451: for (int i = 0; i < length; ++i) { c@451: buffer.push_back(sin(i * M_PI * 2.0 * frequency / sampleRate)); c@451: } c@451: return buffer; c@451: } c@451: c@451: BOOST_AUTO_TEST_CASE(sinusoid_12tET) c@451: { c@451: double concertA = 440.0; c@451: int sampleRate = 44100; c@451: c@451: for (int midiPitch = 48; midiPitch < 96; ++midiPitch) { c@451: c@451: cout << endl; c@451: c@451: GetKeyMode gkm(sampleRate, concertA, 10, 10); c@451: int blockSize = gkm.getBlockSize(); c@451: int hopSize = gkm.getHopSize(); c@451: cerr << "blockSize = " << blockSize c@451: << ", hopSize = " << hopSize << endl; c@451: c@451: double frequency = concertA * pow(2.0, (midiPitch - 69.0) / 12.0); c@451: cout << "midiPitch = " << midiPitch c@451: << ", name = " << midiPitchName(midiPitch) c@451: << ", frequency = " << frequency << endl; c@451: c@451: int blocks = 4; c@451: int totalLength = blockSize * blocks; c@451: vector signal = generateSinusoid(frequency, sampleRate, c@451: totalLength); c@451: c@451: int key; c@451: c@451: for (int offset = 0; offset + blockSize < totalLength; c@451: offset += hopSize) { c@451: int k = gkm.process(signal.data() + offset); c@451: if (offset == 0) { c@451: key = k; c@451: } else { c@451: BOOST_CHECK_EQUAL(key, k); c@451: } c@451: } c@451: c@451: bool minor = (key > 12); c@451: c@451: int tonic = key; c@451: if (minor) tonic -= 12; c@451: c@451: string name = keyName(tonic, minor); c@451: cout << "key value = " << key << ", name = " << name << endl; c@451: } c@451: } c@451: c@451: BOOST_AUTO_TEST_SUITE_END()