diff options
author | John Glover <glover.john@gmail.com> | 2011-08-20 16:23:14 +0100 |
---|---|---|
committer | John Glover <glover.john@gmail.com> | 2011-08-20 16:23:14 +0100 |
commit | d224347b1b7a059ab14d4f170a70e735f84fc1e6 (patch) | |
tree | 1f93acaaef45166ec61cac989d16f244ecd86e7b | |
parent | 3969c0a1ade186d2786a64ee5b660c86016208fc (diff) | |
download | simpl-d224347b1b7a059ab14d4f170a70e735f84fc1e6.tar.gz simpl-d224347b1b7a059ab14d4f170a70e735f84fc1e6.tar.bz2 simpl-d224347b1b7a059ab14d4f170a70e735f84fc1e6.zip |
Add C++ frame class and tests
-rw-r--r-- | simpl/simplloris.i | 1 | ||||
-rw-r--r-- | src/simpl/base.cpp | 70 | ||||
-rw-r--r-- | src/simpl/base.h | 51 | ||||
-rw-r--r-- | tests/testbase.cpp | 117 |
4 files changed, 234 insertions, 5 deletions
diff --git a/simpl/simplloris.i b/simpl/simplloris.i index dea84a7..d142bc3 100644 --- a/simpl/simplloris.i +++ b/simpl/simplloris.i @@ -8,7 +8,6 @@ %include "common.i" -%feature("director") Simpl::Peak; %feature("director") Simpl::LorisPeakDetection; %init diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp index e126433..028aeaf 100644 --- a/src/simpl/base.cpp +++ b/src/simpl/base.cpp @@ -23,6 +23,9 @@ using namespace std; namespace Simpl { +// --------------------------------------------------------------------------- +// Peak +// --------------------------------------------------------------------------- Peak::Peak() { amplitude = 0.0; @@ -69,4 +72,71 @@ bool Peak::is_free(string direction) return true; } +// --------------------------------------------------------------------------- +// Frame +// --------------------------------------------------------------------------- +Frame::Frame() +{ + size = 512; + init(); +} + +Frame::Frame(int frame_size) +{ + size = frame_size; + init(); +} + +Frame::~Frame() +{ +} + +void Frame::init() +{ + audio.resize(size); + synth.resize(size); + residual.resize(size); + synth_residual.resize(size); + max_peaks = 100; + peaks.resize(max_peaks); + max_partials = 100; + partials.resize(max_partials); +} + +int Frame::get_size() +{ + return size; +} + +int Frame::get_max_peaks() +{ + return max_peaks; +} + +int Frame::get_max_partials() +{ + return max_partials; +} + +void Frame::set_size(int new_size) +{ + size = new_size; + audio.resize(size); + synth.resize(size); + residual.resize(size); + synth_residual.resize(size); +} + +void Frame::set_max_peaks(int new_max_peaks) +{ + max_peaks = new_max_peaks; + peaks.resize(max_peaks); +} + +void Frame::set_max_partials(int new_max_partials) +{ + max_partials = new_max_partials; + partials.resize(max_partials); +} + } // end of namespace Simpl diff --git a/src/simpl/base.h b/src/simpl/base.h index 59a969e..5415548 100644 --- a/src/simpl/base.h +++ b/src/simpl/base.h @@ -31,7 +31,11 @@ namespace Simpl typedef double number; -// A Spectral Peak +// --------------------------------------------------------------------------- +// Peak +// +// A spectral Peak +// --------------------------------------------------------------------------- class Peak { public: @@ -56,6 +60,51 @@ public: typedef std::vector<Peak> Peaks; +// --------------------------------------------------------------------------- +// Partial +// --------------------------------------------------------------------------- +class Partial {}; + +typedef std::vector<Partial> Partials; + +// --------------------------------------------------------------------------- +// Frame +// +// Represents a frame of audio information. +// This can be: - raw audio samples +// - an unordered list of sinusoidal peaks +// - an ordered list of partials +// - synthesised audio samples +// - residual samples +// - synthesised residual samples +// --------------------------------------------------------------------------- +class Frame +{ +protected: + int size; + int max_peaks; + int max_partials; + void init(); + +public: + Peaks peaks; + Partials partials; + std::vector<number> audio; + std::vector<number> synth; + std::vector<number> residual; + std::vector<number> synth_residual; + + Frame(); + Frame(int frame_size); + ~Frame(); + int get_size(); + int get_max_peaks(); + int get_max_partials(); + void set_size(int new_size); + void set_max_peaks(int new_max_peaks); + void set_max_partials(int new_max_partials); +}; + } // end of namespace Simpl #endif diff --git a/tests/testbase.cpp b/tests/testbase.cpp index e7cdd5b..9a9c699 100644 --- a/tests/testbase.cpp +++ b/tests/testbase.cpp @@ -26,14 +26,20 @@ #include <cppunit/extensions/TestFactoryRegistry.h> #include "../src/simpl/base.h" +#include "../src/simpl/exceptions.h" namespace Simpl { -class TestBase : public CPPUNIT_NS::TestCase +// --------------------------------------------------------------------------- +// TestPeak +// --------------------------------------------------------------------------- +class TestPeak : public CPPUNIT_NS::TestCase { - CPPUNIT_TEST_SUITE(TestBase); + CPPUNIT_TEST_SUITE(TestPeak); CPPUNIT_TEST(test_constructor); + CPPUNIT_TEST(test_is_free); + CPPUNIT_TEST(test_is_start_of_partial); CPPUNIT_TEST_SUITE_END(); protected: @@ -52,6 +58,42 @@ protected: CPPUNIT_ASSERT(peak->frame_number == 0); } + void test_is_start_of_partial() + { + CPPUNIT_ASSERT(peak->is_start_of_partial()); + Peak* tmp = new Peak(); + peak->previous_peak = tmp; + CPPUNIT_ASSERT(!peak->is_start_of_partial()); + peak->previous_peak = NULL; + delete tmp; + } + + void test_is_free() + { + peak->amplitude = 0.0; + CPPUNIT_ASSERT(!peak->is_free()); + peak->amplitude = 1.0; + CPPUNIT_ASSERT(peak->is_free()); + + Peak* tmp = new Peak(); + + peak->next_peak = tmp; + CPPUNIT_ASSERT(!peak->is_free()); + CPPUNIT_ASSERT(!peak->is_free("forwards")); + CPPUNIT_ASSERT(peak->is_free("backwards")); + peak->next_peak = NULL; + + peak->previous_peak = tmp; + CPPUNIT_ASSERT(peak->is_free()); + CPPUNIT_ASSERT(peak->is_free("forwards")); + CPPUNIT_ASSERT(!peak->is_free("backwards")); + peak->previous_peak = NULL; + + delete tmp; + + CPPUNIT_ASSERT_THROW(peak->is_free("random_text"), InvalidArgument); + } + public: void setUp() { @@ -64,9 +106,78 @@ public: } }; +// --------------------------------------------------------------------------- +// TestFrame +// --------------------------------------------------------------------------- +class TestFrame : public CPPUNIT_NS::TestCase +{ + CPPUNIT_TEST_SUITE(TestFrame); + CPPUNIT_TEST(test_constructor); + CPPUNIT_TEST(test_set_size); + CPPUNIT_TEST(test_set_max_peaks); + CPPUNIT_TEST(test_set_max_partials); + CPPUNIT_TEST_SUITE_END(); + +protected: + static const double PRECISION = 0.001; + Frame* frame; + + void test_constructor() + { + CPPUNIT_ASSERT(frame->get_size() == 512); + CPPUNIT_ASSERT(frame->audio.size() == 512); + CPPUNIT_ASSERT(frame->synth.size() == 512); + CPPUNIT_ASSERT(frame->residual.size() == 512); + CPPUNIT_ASSERT(frame->synth_residual.size() == 512); + CPPUNIT_ASSERT(frame->get_max_peaks() == 100); + CPPUNIT_ASSERT(frame->peaks.size() == 100); + CPPUNIT_ASSERT(frame->get_max_partials() == 100); + CPPUNIT_ASSERT(frame->partials.size() == 100); + } + + void test_set_size() + { + frame->set_size(1024); + CPPUNIT_ASSERT(frame->get_size() == 1024); + CPPUNIT_ASSERT(frame->audio.size() == 1024); + CPPUNIT_ASSERT(frame->synth.size() == 1024); + CPPUNIT_ASSERT(frame->residual.size() == 1024); + CPPUNIT_ASSERT(frame->synth_residual.size() == 1024); + frame->set_size(512); + } + + void test_set_max_peaks() + { + frame->set_max_peaks(200); + CPPUNIT_ASSERT(frame->get_max_peaks() == 200); + CPPUNIT_ASSERT(frame->peaks.size() == 200); + frame->set_max_peaks(100); + } + + void test_set_max_partials() + { + frame->set_max_partials(200); + CPPUNIT_ASSERT(frame->get_max_partials() == 200); + CPPUNIT_ASSERT(frame->partials.size() == 200); + frame->set_max_partials(100); + } + +public: + void setUp() + { + frame = new Frame(); + } + + void tearDown() + { + delete frame; + } +}; + } // end of namespace Simpl -CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestBase); +CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestPeak); +CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestFrame); int main(int arg, char **argv) { |