summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Glover <j@johnglover.net>2013-06-22 18:39:17 +0200
committerJohn Glover <j@johnglover.net>2013-06-22 18:39:17 +0200
commit142eb92a7669debb3748d85e56daa0e83d39464d (patch)
treea411cb183ab7b2dd80a605d8e6d699df708b51ca
parentf10e69931e1eb9d484bc7431645529d756e19861 (diff)
downloadsimpl-142eb92a7669debb3748d85e56daa0e83d39464d.tar.gz
simpl-142eb92a7669debb3748d85e56daa0e83d39464d.tar.bz2
simpl-142eb92a7669debb3748d85e56daa0e83d39464d.zip
[residual] Add basic test for SMSResidual
-rw-r--r--CMakeLists.txt3
-rw-r--r--src/simpl/residual.cpp30
-rw-r--r--src/simpl/residual.h6
-rw-r--r--tests/test_residual.cpp51
-rw-r--r--tests/test_residual.h37
-rw-r--r--tests/tests.cpp2
6 files changed, 124 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index f8e1877..38d93ba 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -48,7 +48,8 @@ if(BUILD_TESTS)
tests/test_base.cpp
tests/test_peak_detection.cpp
tests/test_partial_tracking.cpp
- tests/test_synthesis.cpp)
+ tests/test_synthesis.cpp
+ tests/test_residual.cpp)
add_executable(tests ${test_src})
target_link_libraries(tests ${libs})
diff --git a/src/simpl/residual.cpp b/src/simpl/residual.cpp
index d7d3181..38448ea 100644
--- a/src/simpl/residual.cpp
+++ b/src/simpl/residual.cpp
@@ -13,6 +13,24 @@ Residual::Residual() {
_sampling_rate = 44100;
}
+Residual::~Residual() {
+ clear();
+}
+
+void Residual::clear() {
+ for(int i = 0; i < _frames.size(); i++) {
+ if(_frames[i]) {
+ delete _frames[i];
+ _frames[i] = NULL;
+ }
+ }
+
+ _frames.clear();
+}
+
+void Residual::reset() {
+}
+
int Residual::frame_size() {
return _frame_size;
}
@@ -64,11 +82,12 @@ Frames Residual::synth(Frames& frames) {
}
Frames Residual::synth(int original_size, sample* original) {
- Frames frames;
+ clear();
unsigned int pos = 0;
+ bool alloc_memory_in_frame = true;
while(pos <= original_size - _hop_size) {
- Frame* f = new Frame(_frame_size, true);
+ Frame* f = new Frame(_frame_size, alloc_memory_in_frame);
if((int)pos <= (original_size - _frame_size)) {
f->audio(&(original[pos]), _frame_size);
@@ -78,12 +97,12 @@ Frames Residual::synth(int original_size, sample* original) {
}
synth_frame(f);
- frames.push_back(f);
+ _frames.push_back(f);
pos += _hop_size;
}
- return frames;
+ return _frames;
}
@@ -109,6 +128,9 @@ SMSResidual::~SMSResidual() {
sms_free();
}
+void SMSResidual::reset() {
+}
+
void SMSResidual::frame_size(int new_frame_size) {
_frame_size = new_frame_size;
_pd.frame_size(_frame_size);
diff --git a/src/simpl/residual.h b/src/simpl/residual.h
index e611fce..c162f84 100644
--- a/src/simpl/residual.h
+++ b/src/simpl/residual.h
@@ -27,9 +27,14 @@ class Residual {
int _frame_size;
int _hop_size;
int _sampling_rate;
+ Frames _frames;
+
+ void clear();
public:
Residual();
+ ~Residual();
+ virtual void reset();
int frame_size();
virtual void frame_size(int new_frame_size);
int hop_size();
@@ -62,6 +67,7 @@ class SMSResidual : public Residual {
public:
SMSResidual();
~SMSResidual();
+ void reset();
void frame_size(int new_frame_size);
void hop_size(int new_hop_size);
int num_stochastic_coeffs();
diff --git a/tests/test_residual.cpp b/tests/test_residual.cpp
new file mode 100644
index 0000000..a597317
--- /dev/null
+++ b/tests/test_residual.cpp
@@ -0,0 +1,51 @@
+#include "test_residual.h"
+
+using namespace simpl;
+
+// ---------------------------------------------------------------------------
+// test_basic
+// ---------------------------------------------------------------------------
+static void test_basic(Residual* residual, SndfileHandle *sf) {
+ int num_samples = 4096;
+ int hop_size = 256;
+ int frame_size = 512;
+
+ std::vector<sample> audio(sf->frames(), 0.0);
+ sf->read(&audio[0], (int)sf->frames());
+
+ residual->reset();
+ residual->frame_size(frame_size);
+ residual->hop_size(hop_size);
+
+ Frames frames = residual->synth(num_samples,
+ &(audio[(int)sf->frames() / 2]));
+
+ for(int i = 0; i < frames.size(); i++) {
+ CPPUNIT_ASSERT(frames[i]->num_peaks() > 0);
+ CPPUNIT_ASSERT(frames[i]->num_partials() > 0);
+
+ double energy = 0.f;
+ for(int j = 0; j < residual->hop_size(); j++) {
+ energy += frames[i]->synth_residual()[j] *
+ frames[i]->synth_residual()[j];
+ }
+ CPPUNIT_ASSERT(energy > 0.f);
+ }
+}
+
+
+// ---------------------------------------------------------------------------
+// TestSMSResidual
+// ---------------------------------------------------------------------------
+void TestSMSResidual::setUp() {
+ _sf = SndfileHandle(TEST_AUDIO_FILE);
+
+ if(_sf.error() > 0) {
+ throw Exception(std::string("Could not open audio file: ") +
+ std::string(TEST_AUDIO_FILE));
+ }
+}
+
+void TestSMSResidual::test_basic() {
+ ::test_basic(&_res, &_sf);
+}
diff --git a/tests/test_residual.h b/tests/test_residual.h
new file mode 100644
index 0000000..60f4eb9
--- /dev/null
+++ b/tests/test_residual.h
@@ -0,0 +1,37 @@
+#ifndef TEST_RESIDUAL_H
+#define TEST_RESIDUAL_H
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include "../src/simpl/base.h"
+#include "../src/simpl/peak_detection.h"
+#include "../src/simpl/partial_tracking.h"
+#include "../src/simpl/synthesis.h"
+#include "../src/simpl/residual.h"
+#include "test_common.h"
+
+namespace simpl
+{
+
+// ---------------------------------------------------------------------------
+// TestSMSResidual
+// ---------------------------------------------------------------------------
+class TestSMSResidual : public CPPUNIT_NS::TestCase {
+ CPPUNIT_TEST_SUITE(TestSMSResidual);
+ CPPUNIT_TEST(test_basic);
+ CPPUNIT_TEST_SUITE_END();
+
+public:
+ void setUp();
+
+protected:
+ SMSResidual _res;
+ SndfileHandle _sf;
+ Frames _frames;
+
+ void test_basic();
+};
+
+} // end of namespace simpl
+
+#endif
diff --git a/tests/tests.cpp b/tests/tests.cpp
index dc11c98..517b7f1 100644
--- a/tests/tests.cpp
+++ b/tests/tests.cpp
@@ -6,6 +6,7 @@
#include "test_peak_detection.h"
#include "test_partial_tracking.h"
#include "test_synthesis.h"
+#include "test_residual.h"
CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestPeak);
CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestFrame);
@@ -21,6 +22,7 @@ CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestMQSynthesis);
CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestLorisSynthesis);
CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSMSSynthesis);
CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSndObjSynthesis);
+CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestSMSResidual);
int main(int arg, char **argv) {
CppUnit::TextTestRunner runner;