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 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) (limited to 'tests/test_partial_tracking.cpp') 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 // --------------------------------------------------------------------------- -- cgit v1.2.3