diff options
-rw-r--r-- | src/simpl/base.cpp | 160 | ||||
-rw-r--r-- | src/simpl/base.h | 62 | ||||
-rw-r--r-- | tests/testbase.cpp | 138 |
3 files changed, 305 insertions, 55 deletions
diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp index 028aeaf..50b4654 100644 --- a/src/simpl/base.cpp +++ b/src/simpl/base.cpp @@ -77,13 +77,13 @@ bool Peak::is_free(string direction) // --------------------------------------------------------------------------- Frame::Frame() { - size = 512; + _size = 512; init(); } Frame::Frame(int frame_size) { - size = frame_size; + _size = frame_size; init(); } @@ -93,50 +93,154 @@ 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); + 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() +int Frame::size() { - return size; + return _size; } -int Frame::get_max_peaks() +void Frame::size(int new_size) { - return max_peaks; + _size = new_size; + audio.resize(_size); + synth.resize(_size); + residual.resize(_size); + synth_residual.resize(_size); } -int Frame::get_max_partials() +int Frame::max_peaks() { - return max_partials; + return _max_peaks; } -void Frame::set_size(int new_size) +void Frame::max_peaks(int new_max_peaks) { - size = new_size; - audio.resize(size); - synth.resize(size); - residual.resize(size); - synth_residual.resize(size); + _max_peaks = new_max_peaks; + peaks.resize(_max_peaks); } -void Frame::set_max_peaks(int new_max_peaks) +int Frame::max_partials() { - max_peaks = new_max_peaks; - peaks.resize(max_peaks); + return _max_partials; } -void Frame::set_max_partials(int new_max_partials) +void Frame::max_partials(int new_max_partials) { - max_partials = new_max_partials; - partials.resize(max_partials); + _max_partials = new_max_partials; + partials.resize(_max_partials); +} + +// --------------------------------------------------------------------------- +// PeakDetection +// --------------------------------------------------------------------------- + +PeakDetection::PeakDetection() +{ + _sampling_rate = 44100; + _frame_size = 2048; + _static_frame_size = true; + _hop_size = 512; + _max_peaks = 100; + _window_type = "hamming"; + _window_size = 2048; + _min_peak_separation = 1.0; // in Hz +} + +PeakDetection::~PeakDetection() +{ +} + +int PeakDetection::sampling_rate() +{ + return _sampling_rate; +} + +void PeakDetection::sampling_rate(int new_sampling_rate) +{ + _sampling_rate = new_sampling_rate; +} + +int PeakDetection::frame_size() +{ + return _frame_size; +} +void PeakDetection::frame_size(int new_frame_size) +{ + _frame_size = new_frame_size; +} + +bool PeakDetection::static_frame_size() +{ + return _static_frame_size; +} + +void PeakDetection::static_frame_size(bool new_static_frame_size) +{ + _static_frame_size = new_static_frame_size; +} + +int PeakDetection::hop_size() +{ + return _hop_size; +} + +void PeakDetection::hop_size(int new_hop_size) +{ + _hop_size = new_hop_size; +} + +int PeakDetection::max_peaks() +{ + return _max_peaks; +} + +void PeakDetection::max_peaks(int new_max_peaks) +{ + _max_peaks = new_max_peaks; +} + +std::string PeakDetection::window_type() +{ + return _window_type; +} + +void PeakDetection::window_type(std::string new_window_type) +{ + _window_type = new_window_type; +} + +int PeakDetection::window_size() +{ + return _window_size; +} + +void PeakDetection::window_size(int new_window_size) +{ + _window_size = new_window_size; +} + +number PeakDetection::min_peak_separation() +{ + return _min_peak_separation; +} + +void PeakDetection::min_peak_separation(number new_min_peak_separation) +{ + _min_peak_separation = new_min_peak_separation; +} + +Frames* PeakDetection::frames() +{ + return &_frames; } } // end of namespace Simpl diff --git a/src/simpl/base.h b/src/simpl/base.h index 5415548..735094b 100644 --- a/src/simpl/base.h +++ b/src/simpl/base.h @@ -81,9 +81,9 @@ typedef std::vector<Partial> Partials; class Frame { protected: - int size; - int max_peaks; - int max_partials; + int _size; + int _max_peaks; + int _max_partials; void init(); public: @@ -97,12 +97,56 @@ public: 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); + int size(); + void size(int new_size); + int max_peaks(); + void max_peaks(int new_max_peaks); + int max_partials(); + void max_partials(int new_max_partials); +}; + +typedef std::vector<Frame> Frames; + +// --------------------------------------------------------------------------- +// PeakDetection +// +// Detect spectral peaks +// --------------------------------------------------------------------------- + +class PeakDetection +{ +protected: + int _sampling_rate; + int _frame_size; + bool _static_frame_size; + int _hop_size; + int _max_peaks; + std::string _window_type; + int _window_size; + number _min_peak_separation; + Frames _frames; + +public: + PeakDetection(); + ~PeakDetection(); + + int sampling_rate(); + void sampling_rate(int new_sampling_rate); + int frame_size(); + void frame_size(int new_frame_size); + bool static_frame_size(); + void static_frame_size(bool new_static_frame_size); + int hop_size(); + void hop_size(int new_hop_size); + int max_peaks(); + void max_peaks(int new_max_peaks); + std::string window_type(); + void window_type(std::string new_window_type); + int window_size(); + void window_size(int new_window_size); + number min_peak_separation(); + void min_peak_separation(number new_min_peak_separation); + Frames* frames(); }; } // end of namespace Simpl diff --git a/tests/testbase.cpp b/tests/testbase.cpp index 9a9c699..c79cf18 100644 --- a/tests/testbase.cpp +++ b/tests/testbase.cpp @@ -113,9 +113,9 @@ 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(test_size); + CPPUNIT_TEST(test_max_peaks); + CPPUNIT_TEST(test_max_partials); CPPUNIT_TEST_SUITE_END(); protected: @@ -124,42 +124,42 @@ protected: void test_constructor() { - CPPUNIT_ASSERT(frame->get_size() == 512); + CPPUNIT_ASSERT(frame->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->max_peaks() == 100); CPPUNIT_ASSERT(frame->peaks.size() == 100); - CPPUNIT_ASSERT(frame->get_max_partials() == 100); + CPPUNIT_ASSERT(frame->max_partials() == 100); CPPUNIT_ASSERT(frame->partials.size() == 100); } - void test_set_size() + void test_size() { - frame->set_size(1024); - CPPUNIT_ASSERT(frame->get_size() == 1024); + frame->size(1024); + CPPUNIT_ASSERT(frame->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); + frame->size(512); } - void test_set_max_peaks() + void test_max_peaks() { - frame->set_max_peaks(200); - CPPUNIT_ASSERT(frame->get_max_peaks() == 200); + frame->max_peaks(200); + CPPUNIT_ASSERT(frame->max_peaks() == 200); CPPUNIT_ASSERT(frame->peaks.size() == 200); - frame->set_max_peaks(100); + frame->max_peaks(100); } - void test_set_max_partials() + void test_max_partials() { - frame->set_max_partials(200); - CPPUNIT_ASSERT(frame->get_max_partials() == 200); + frame->max_partials(200); + CPPUNIT_ASSERT(frame->max_partials() == 200); CPPUNIT_ASSERT(frame->partials.size() == 200); - frame->set_max_partials(100); + frame->max_partials(100); } public: @@ -174,10 +174,112 @@ public: } }; +// --------------------------------------------------------------------------- +// TestPeakDetection +// --------------------------------------------------------------------------- +class TestPeakDetection : public CPPUNIT_NS::TestCase +{ + CPPUNIT_TEST_SUITE(TestPeakDetection); + CPPUNIT_TEST(test_constructor); + CPPUNIT_TEST(test_frame_size); + CPPUNIT_TEST(test_static_frame_size); + CPPUNIT_TEST(test_hop_size); + CPPUNIT_TEST(test_max_peaks); + CPPUNIT_TEST(test_window_type); + CPPUNIT_TEST(test_window_size); + CPPUNIT_TEST(test_min_peak_separation); + CPPUNIT_TEST_SUITE_END(); + +protected: + static const double PRECISION = 0.001; + PeakDetection* pd; + + void test_constructor() + { + CPPUNIT_ASSERT(pd->sampling_rate() == 44100); + CPPUNIT_ASSERT(pd->frame_size() == 2048); + CPPUNIT_ASSERT(pd->static_frame_size()); + CPPUNIT_ASSERT(pd->hop_size() == 512); + CPPUNIT_ASSERT(pd->max_peaks() == 100); + CPPUNIT_ASSERT(pd->window_type() == "hamming"); + CPPUNIT_ASSERT(pd->window_size() == 2048); + CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, pd->min_peak_separation(), 0.00001); + CPPUNIT_ASSERT(pd->frames()->size() == 0); + } + + void test_sampling_rate() + { + pd->sampling_rate(96000); + CPPUNIT_ASSERT(pd->sampling_rate() == 96000); + pd->sampling_rate(44100); + } + + void test_frame_size() + { + pd->frame_size(1024); + CPPUNIT_ASSERT(pd->frame_size() == 1024); + pd->frame_size(2048); + } + + void test_static_frame_size() + { + pd->static_frame_size(false); + CPPUNIT_ASSERT(!pd->static_frame_size()); + pd->static_frame_size(true); + } + + void test_hop_size() + { + pd->hop_size(128); + CPPUNIT_ASSERT(pd->hop_size() == 128); + pd->hop_size(512); + } + + void test_max_peaks() + { + pd->max_peaks(20); + CPPUNIT_ASSERT(pd->max_peaks() == 20); + pd->max_peaks(100); + } + + void test_window_type() + { + pd->window_type("hanning"); + CPPUNIT_ASSERT(pd->window_type() == "hanning"); + pd->window_type("hamming"); + } + + void test_window_size() + { + pd->window_size(2048); + CPPUNIT_ASSERT(pd->window_size() == 2048); + pd->window_size(2048); + } + + void test_min_peak_separation() + { + pd->min_peak_separation(0.5); + CPPUNIT_ASSERT_DOUBLES_EQUAL(0.5, pd->min_peak_separation(), 0.00001); + pd->min_peak_separation(1.0); + } + +public: + void setUp() + { + pd = new PeakDetection(); + } + + void tearDown() + { + delete pd; + } +}; + } // end of namespace Simpl CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestPeak); CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestFrame); +CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestPeakDetection); int main(int arg, char **argv) { |