diff options
author | John Glover <glover.john@gmail.com> | 2011-08-26 17:22:30 +0100 |
---|---|---|
committer | John Glover <glover.john@gmail.com> | 2011-08-26 17:22:30 +0100 |
commit | 9ec7303c71a5f2eb897f4f294fc1af564d005276 (patch) | |
tree | 4f50f1fe003498fc1da99a9b13a6f69157cb52f3 | |
parent | 5bce13cb39f99d1226dffde9e2649ae94b340a6a (diff) | |
download | simpl-9ec7303c71a5f2eb897f4f294fc1af564d005276.tar.gz simpl-9ec7303c71a5f2eb897f4f294fc1af564d005276.tar.bz2 simpl-9ec7303c71a5f2eb897f4f294fc1af564d005276.zip |
frame refactor
-rw-r--r-- | src/simpl/base.cpp | 130 | ||||
-rw-r--r-- | src/simpl/base.h | 42 | ||||
-rw-r--r-- | tests/testbase.cpp | 33 |
3 files changed, 174 insertions, 31 deletions
diff --git a/src/simpl/base.cpp b/src/simpl/base.cpp index 42832d6..bcfea65 100644 --- a/src/simpl/base.cpp +++ b/src/simpl/base.cpp @@ -17,6 +17,7 @@ * */ +#include <iostream> #include "base.h" using namespace std; @@ -24,7 +25,7 @@ using namespace std; namespace Simpl { // --------------------------------------------------------------------------- -// Peak +// Peak // --------------------------------------------------------------------------- Peak::Peak() { @@ -43,7 +44,8 @@ Peak::~Peak() } // Returns true iff this peak is unmatched in the given direction, and has positive amplitude -bool Peak::is_free(string direction) +bool Peak::is_free(const string direction) +throw(InvalidArgument) { if(amplitude <= 0.0) { @@ -66,14 +68,14 @@ bool Peak::is_free(string direction) } else { - Throw(InvalidArgument, "Invalid direction"); + Throw(InvalidArgument, "Invalid direction"); } return true; } // --------------------------------------------------------------------------- -// Frame +// Frame // --------------------------------------------------------------------------- Frame::Frame() { @@ -89,24 +91,26 @@ Frame::Frame(int frame_size) Frame::~Frame() { + _peaks.clear(); + _partials.clear(); } void Frame::init() { _max_peaks = 100; - peaks.resize(_max_peaks); _max_partials = 100; - partials.resize(_max_partials); + _audio = NULL; + _synth = NULL; + _residual = NULL; + _synth_residual = NULL; } -int Frame::size() -{ - return _size; -} +// Frame - peaks +// ------------- -void Frame::size(int new_size) +int Frame::num_peaks() { - _size = new_size; + return _peaks.size(); } int Frame::max_peaks() @@ -117,7 +121,35 @@ int Frame::max_peaks() void Frame::max_peaks(int new_max_peaks) { _max_peaks = new_max_peaks; - peaks.resize(_max_peaks); + + // potentially losing data here but the user shouldn't really do this + if((int)_peaks.size() > _max_peaks) + { + _peaks.resize(_max_peaks); + } +} + +void Frame::add_peak(Peak peak) +{ + _peaks.push_back(peak); +} + +Peak Frame::peak(int peak_number) +{ + return _peaks[peak_number]; +} + +Peaks::iterator Frame::peaks() +{ + return _peaks.begin(); +} + +// Frame - partials +// ---------------- + +int Frame::num_partials() +{ + return _partials.size(); } int Frame::max_partials() @@ -128,11 +160,79 @@ int Frame::max_partials() void Frame::max_partials(int new_max_partials) { _max_partials = new_max_partials; - partials.resize(_max_partials); + + // potentially losing data here but the user shouldn't really do this + if((int)_partials.size() > _max_partials) + { + _partials.resize(_max_partials); + } +} + +void Frame::add_partial(Partial partial) +{ + +} + +Partials::iterator Frame::partials() +{ + return _partials.begin(); +} + + +// Frame - audio buffers +// --------------------- + +int Frame::size() +{ + return _size; +} + +void Frame::size(int new_size) +{ + _size = new_size; +} +void Frame::audio(const number* new_audio) +{ + _audio = new_audio; +} + +const number* Frame::audio() +{ + return _audio; +} + +void Frame::synth(const number* new_synth) +{ + _synth = new_synth; +} + +const number* Frame::synth() +{ + return _synth; +} + +void Frame::residual(const number* new_residual) +{ + _residual = new_residual; +} + +const number* Frame::residual() +{ + return _residual; +} + +void Frame::synth_residual(const number* new_synth_residual) +{ + _synth_residual = new_synth_residual; +} + +const number* Frame::synth_residual() +{ + return _synth_residual; } // --------------------------------------------------------------------------- -// PeakDetection +// PeakDetection // --------------------------------------------------------------------------- PeakDetection::PeakDetection() diff --git a/src/simpl/base.h b/src/simpl/base.h index a0064f1..ad201b3 100644 --- a/src/simpl/base.h +++ b/src/simpl/base.h @@ -56,7 +56,7 @@ public: { return previous_peak == NULL; }; - bool is_free(string direction = string("forwards")); + bool is_free(const string direction = string("forwards")) throw(InvalidArgument); }; typedef std::vector<Peak> Peaks; @@ -81,29 +81,49 @@ typedef std::vector<Partial> Partials; // --------------------------------------------------------------------------- class Frame { -protected: +private: int _size; int _max_peaks; int _max_partials; + Peaks _peaks; + Partials _partials; + const number* _audio; + const number* _synth; + const number* _residual; + const number* _synth_residual; void init(); public: - Peaks peaks; - Partials partials; - number* audio; - number* synth; - number* residual; - number* synth_residual; - Frame(); Frame(int frame_size); ~Frame(); - int size(); - void size(int new_size); + + // peaks + int num_peaks(); int max_peaks(); void max_peaks(int new_max_peaks); + void add_peak(Peak peak); + Peak peak(int peak_number); + Peaks::iterator peaks(); + + // partials + int num_partials(); int max_partials(); void max_partials(int new_max_partials); + void add_partial(Partial partial); + Partials::iterator partials(); + + // audio buffers + int size(); + void size(int new_size); + void audio(const number* new_audio); + const number* audio(); + void synth(const number* new_synth); + const number* synth(); + void residual(const number* new_residual); + const number* residual(); + void synth_residual(const number* new_synth_residual); + const number* synth_residual(); }; typedef std::vector<Frame> Frames; diff --git a/tests/testbase.cpp b/tests/testbase.cpp index 4e1293a..02015bf 100644 --- a/tests/testbase.cpp +++ b/tests/testbase.cpp @@ -38,8 +38,9 @@ class TestPeak : public CPPUNIT_NS::TestCase { CPPUNIT_TEST_SUITE(TestPeak); CPPUNIT_TEST(test_constructor); - CPPUNIT_TEST(test_is_free); CPPUNIT_TEST(test_is_start_of_partial); + CPPUNIT_TEST(test_is_free); + CPPUNIT_TEST(test_is_free_invalid_argument); CPPUNIT_TEST_SUITE_END(); protected: @@ -90,8 +91,13 @@ protected: peak->previous_peak = NULL; delete tmp; + } + void test_is_free_invalid_argument() + { + peak->amplitude = 1.0; CPPUNIT_ASSERT_THROW(peak->is_free("random_text"), InvalidArgument); + peak->amplitude = 0.0; } public: @@ -116,6 +122,8 @@ class TestFrame : public CPPUNIT_NS::TestCase CPPUNIT_TEST(test_size); CPPUNIT_TEST(test_max_peaks); CPPUNIT_TEST(test_max_partials); + CPPUNIT_TEST(test_peaks); + CPPUNIT_TEST(test_partials); CPPUNIT_TEST_SUITE_END(); protected: @@ -126,9 +134,9 @@ protected: { CPPUNIT_ASSERT(frame->size() == 512); CPPUNIT_ASSERT(frame->max_peaks() == 100); - CPPUNIT_ASSERT(frame->peaks.size() == 100); + CPPUNIT_ASSERT(frame->num_peaks() == 0); CPPUNIT_ASSERT(frame->max_partials() == 100); - CPPUNIT_ASSERT(frame->partials.size() == 100); + CPPUNIT_ASSERT(frame->num_partials() == 0); } void test_size() @@ -142,7 +150,7 @@ protected: { frame->max_peaks(200); CPPUNIT_ASSERT(frame->max_peaks() == 200); - CPPUNIT_ASSERT(frame->peaks.size() == 200); + CPPUNIT_ASSERT(frame->num_peaks() == 0); frame->max_peaks(100); } @@ -150,10 +158,25 @@ protected: { frame->max_partials(200); CPPUNIT_ASSERT(frame->max_partials() == 200); - CPPUNIT_ASSERT(frame->partials.size() == 200); + CPPUNIT_ASSERT(frame->num_partials() == 0); frame->max_partials(100); } + void test_peaks() + { + Peak p = Peak(); + p.amplitude = 1.5; + frame->add_peak(p); + CPPUNIT_ASSERT(frame->max_peaks() == 100); + CPPUNIT_ASSERT(frame->num_peaks() == 1); + CPPUNIT_ASSERT(frame->peak(0).amplitude == 1.5); + } + + void test_partials() + { + + } + public: void setUp() { |