From c357d6caf3eb64d3fc2604868b696c1e34e8d16e Mon Sep 17 00:00:00 2001 From: John Glover Date: Sat, 15 Jun 2013 23:38:39 +0200 Subject: Add basic tests for SndObjPartialTracking. Fix reading invalid memory address when looping over partials. --- tests/test_partial_tracking.cpp | 129 ++++++++++++++++++++++++++++++++++++++++ tests/test_partial_tracking.h | 26 ++++++++ tests/tests.cpp | 1 + 3 files changed, 156 insertions(+) (limited to 'tests') diff --git a/tests/test_partial_tracking.cpp b/tests/test_partial_tracking.cpp index 31e0473..0c835fd 100644 --- a/tests/test_partial_tracking.cpp +++ b/tests/test_partial_tracking.cpp @@ -203,6 +203,135 @@ void TestSMSPartialTracking::test_streaming() { } +// --------------------------------------------------------------------------- +// TestSndObjPartialTracking +// --------------------------------------------------------------------------- +void TestSndObjPartialTracking::setUp() { + _sf = SndfileHandle(TEST_AUDIO_FILE); + + if(_sf.error() > 0) { + throw Exception(std::string("Could not open audio file: ") + + std::string(TEST_AUDIO_FILE)); + } + + _pt.max_partials(5); +} + +void TestSndObjPartialTracking::test_basic() { + int hop_size = 256; + int frame_size = 2048; + int num_samples = 4096; + + _pd.clear(); + _pt.reset(); + + _pd.hop_size(hop_size); + _pd.frame_size(frame_size); + + std::vector audio(_sf.frames(), 0.0); + _sf.read(&audio[0], (int)_sf.frames()); + + Frames frames = _pd.find_peaks(num_samples, + &(audio[(int)_sf.frames() / 2])); + frames = _pt.find_partials(frames); + + for(int i = 0; i < frames.size(); i++) { + CPPUNIT_ASSERT(frames[i]->num_peaks() > 0); + CPPUNIT_ASSERT(frames[i]->num_partials() > 0); + } +} + +void TestSndObjPartialTracking::test_change_num_partials() { + int hop_size = 256; + int frame_size = 2048; + int num_samples = 4096; + int max_partials = 10; + + _pd.clear(); + _pt.reset(); + + _pd.hop_size(hop_size); + _pd.frame_size(frame_size); + + _pd.max_peaks(max_partials); + _pt.max_partials(max_partials); + + std::vector audio(_sf.frames(), 0.0); + _sf.read(&audio[0], (int)_sf.frames()); + + Frames frames = _pd.find_peaks(num_samples, + &(audio[(int)_sf.frames() / 2])); + frames = _pt.find_partials(frames); + + for(int i = 0; i < frames.size(); i++) { + CPPUNIT_ASSERT(frames[i]->num_peaks() == max_partials); + CPPUNIT_ASSERT(frames[i]->num_partials() == max_partials); + } +} + +void TestSndObjPartialTracking::test_peaks() { + int num_frames = 8; + Frames frames; + + _pd.clear(); + _pt.reset(); + + for(int i = 0; i < num_frames; i++) { + Frame* f = new Frame(); + f->add_peak(0.4, 220, 0, 0); + f->add_peak(0.2, 440, 0, 0); + frames.push_back(f); + } + + _pt.find_partials(frames); + for(int i = 1; i < num_frames; i++) { + CPPUNIT_ASSERT(frames[i]->num_peaks() > 0); + CPPUNIT_ASSERT(frames[i]->num_partials() > 0); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.4, frames[i]->partial(0)->amplitude, + PRECISION); + CPPUNIT_ASSERT_DOUBLES_EQUAL(220, frames[i]->partial(0)->frequency, + PRECISION); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.2, frames[i]->partial(1)->amplitude, + PRECISION); + CPPUNIT_ASSERT_DOUBLES_EQUAL(440, frames[i]->partial(1)->frequency, + PRECISION); + } + + for(int i = 0; i < num_frames; i++) { + delete frames[i]; + } +} + +void TestSndObjPartialTracking::test_streaming() { + int hop_size = 256; + int frame_size = 2048; + int num_frames = 10; + int max_partials = 10; + + _pd.clear(); + _pt.reset(); + + _pd.hop_size(hop_size); + _pd.frame_size(frame_size); + + _pd.max_peaks(max_partials); + _pt.max_partials(max_partials); + + std::vector audio(_sf.frames(), 0.0); + _sf.read(&audio[0], (int)_sf.frames()); + + for(int i = 0, n = (int)_sf.frames() / 2; i < num_frames; i++, n += hop_size) { + Frame f(frame_size, true); + f.audio(&(audio[n]), frame_size); + + _pd.find_peaks_in_frame(&f); + _pt.update_partials(&f); + + CPPUNIT_ASSERT(f.num_partials() > 0); + } +} + + // --------------------------------------------------------------------------- // TestLorisPartialTracking // --------------------------------------------------------------------------- diff --git a/tests/test_partial_tracking.h b/tests/test_partial_tracking.h index c548984..e5715de 100644 --- a/tests/test_partial_tracking.h +++ b/tests/test_partial_tracking.h @@ -58,6 +58,32 @@ protected: void test_streaming(); }; + +// --------------------------------------------------------------------------- +// TestSndObjPartialTracking +// --------------------------------------------------------------------------- +class TestSndObjPartialTracking : public CPPUNIT_NS::TestCase { + CPPUNIT_TEST_SUITE(TestSndObjPartialTracking); + CPPUNIT_TEST(test_basic); + CPPUNIT_TEST(test_change_num_partials); + CPPUNIT_TEST(test_peaks); + CPPUNIT_TEST(test_streaming); + CPPUNIT_TEST_SUITE_END(); + +public: + void setUp(); + +protected: + SndObjPeakDetection _pd; + SndObjPartialTracking _pt; + SndfileHandle _sf; + + void test_basic(); + void test_change_num_partials(); + void test_peaks(); + void test_streaming(); +}; + // --------------------------------------------------------------------------- // TestLorisPartialTracking // --------------------------------------------------------------------------- diff --git a/tests/tests.cpp b/tests/tests.cpp index e52653d..11fbebf 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -15,6 +15,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestTWM); CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestLorisPeakDetection); CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestMQPartialTracking); CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSMSPartialTracking); +CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSndObjPartialTracking); CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestLorisPartialTracking); CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestMQSynthesis); CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestLorisSynthesis); -- cgit v1.2.3