summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJohn Glover <j@johnglover.net>2012-08-22 11:21:29 +0100
committerJohn Glover <j@johnglover.net>2012-08-22 11:21:29 +0100
commite49430f96bd0a5858097f6dc631480d49baab7a0 (patch)
treee8826e8746b70965f2f026af7e6ad79888cbedc1 /tests
parent39e0005e226ed6f04562e9d5d7548782fef81c20 (diff)
downloadsimpl-e49430f96bd0a5858097f6dc631480d49baab7a0.tar.gz
simpl-e49430f96bd0a5858097f6dc631480d49baab7a0.tar.bz2
simpl-e49430f96bd0a5858097f6dc631480d49baab7a0.zip
[loris] Add C++ LorisPeakDetection.
Diffstat (limited to 'tests')
-rw-r--r--tests/test_base.cpp305
-rw-r--r--tests/test_peak_detection.cpp89
2 files changed, 115 insertions, 279 deletions
diff --git a/tests/test_base.cpp b/tests/test_base.cpp
index a37024f..2f7b5c4 100644
--- a/tests/test_base.cpp
+++ b/tests/test_base.cpp
@@ -7,363 +7,110 @@
#include <cppunit/extensions/TestFactoryRegistry.h>
#include "../src/simpl/base.h"
-#include "../src/simpl/exceptions.h"
-namespace Simpl
+namespace simpl
{
// ---------------------------------------------------------------------------
// TestPeak
// ---------------------------------------------------------------------------
-class TestPeak : public CPPUNIT_NS::TestCase
-{
+class TestPeak : public CPPUNIT_NS::TestCase {
CPPUNIT_TEST_SUITE(TestPeak);
- CPPUNIT_TEST(test_constructor);
- CPPUNIT_TEST(test_is_start_of_partial);
- CPPUNIT_TEST(test_is_free);
- CPPUNIT_TEST(test_is_free_invalid_argument);
CPPUNIT_TEST_SUITE_END();
protected:
static const double PRECISION = 0.001;
Peak* peak;
- void test_constructor()
- {
- CPPUNIT_ASSERT_DOUBLES_EQUAL(peak->amplitude, 0.0, PRECISION);
- CPPUNIT_ASSERT_DOUBLES_EQUAL(peak->frequency, 0.0, PRECISION);
- CPPUNIT_ASSERT_DOUBLES_EQUAL(peak->phase, 0.0, PRECISION);
- CPPUNIT_ASSERT(peak->next_peak == NULL);
- CPPUNIT_ASSERT(peak->previous_peak == NULL);
- CPPUNIT_ASSERT(peak->partial_id == 0);
- CPPUNIT_ASSERT(peak->partial_position == 0);
- 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;
- }
-
- void test_is_free_invalid_argument()
- {
- peak->amplitude = 1.0;
- CPPUNIT_ASSERT_THROW(peak->is_free("random_text"), InvalidArgument);
- peak->amplitude = 0.0;
- }
-
public:
- void setUp()
- {
+ void setUp() {
peak = new Peak();
}
- void tearDown()
- {
+ void tearDown() {
delete peak;
- }
+ }
};
// ---------------------------------------------------------------------------
// TestFrame
// ---------------------------------------------------------------------------
-class TestFrame : public CPPUNIT_NS::TestCase
-{
+class TestFrame : public CPPUNIT_NS::TestCase {
CPPUNIT_TEST_SUITE(TestFrame);
- CPPUNIT_TEST(test_constructor);
CPPUNIT_TEST(test_size);
CPPUNIT_TEST(test_max_peaks);
CPPUNIT_TEST(test_max_partials);
CPPUNIT_TEST(test_add_peak);
- CPPUNIT_TEST(test_add_peaks);
- CPPUNIT_TEST(test_peak_clear);
- CPPUNIT_TEST(test_peak_iteration);
+ CPPUNIT_TEST(test_clear);
CPPUNIT_TEST_SUITE_END();
protected:
static const double PRECISION = 0.001;
Frame* frame;
- void test_constructor()
- {
- CPPUNIT_ASSERT(frame->size() == 512);
- CPPUNIT_ASSERT(frame->max_peaks() == 100);
- CPPUNIT_ASSERT(frame->num_peaks() == 0);
- CPPUNIT_ASSERT(frame->max_partials() == 100);
- CPPUNIT_ASSERT(frame->num_partials() == 0);
- }
-
- void test_size()
- {
+ void test_size() {
frame->size(1024);
CPPUNIT_ASSERT(frame->size() == 1024);
frame->size(512);
}
- void test_max_peaks()
- {
+ void test_max_peaks() {
frame->max_peaks(200);
CPPUNIT_ASSERT(frame->max_peaks() == 200);
CPPUNIT_ASSERT(frame->num_peaks() == 0);
frame->max_peaks(100);
}
- void test_max_partials()
- {
+ void test_max_partials() {
frame->max_partials(200);
CPPUNIT_ASSERT(frame->max_partials() == 200);
CPPUNIT_ASSERT(frame->num_partials() == 0);
frame->max_partials(100);
}
- void test_add_peak()
- {
+ void test_add_peak() {
Peak p = Peak();
p.amplitude = 1.5;
- frame->add_peak(p);
+ frame->add_peak(&p);
CPPUNIT_ASSERT(frame->max_peaks() == 100);
CPPUNIT_ASSERT(frame->num_peaks() == 1);
- CPPUNIT_ASSERT_DOUBLES_EQUAL(1.5, frame->peak(0).amplitude, PRECISION);
- frame->clear_peaks();
- }
-
- void test_add_peaks()
- {
- Peaks* peaks = new Peaks();
-
- Peak p1 = Peak();
- p1.amplitude = 1.0;
- peaks->push_back(p1);
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(1.5, frame->peak(0)->amplitude, PRECISION);
Peak p2 = Peak();
p2.amplitude = 2.0;
- peaks->push_back(p2);
-
- frame->add_peaks(peaks);
+ frame->add_peak(&p2);
+ CPPUNIT_ASSERT(frame->max_peaks() == 100);
CPPUNIT_ASSERT(frame->num_peaks() == 2);
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, frame->peak(1)->amplitude, PRECISION);
- frame->clear_peaks();
- delete peaks;
+ frame->clear();
}
- void test_peak_clear()
- {
+ void test_clear() {
Peak p = Peak();
p.amplitude = 1.5;
- frame->add_peak(p);
+ frame->add_peak(&p);
CPPUNIT_ASSERT(frame->num_peaks() == 1);
- frame->clear_peaks();
+ frame->clear();
CPPUNIT_ASSERT(frame->num_peaks() == 0);
}
- void test_peak_iteration()
- {
- Peak p1 = Peak();
- p1.amplitude = 1.0;
- frame->add_peak(p1);
-
- Peak p2 = Peak();
- p2.amplitude = 2.0;
- frame->add_peak(p2);
-
- CPPUNIT_ASSERT(frame->num_peaks() == 2);
-
- int peak_num = 0;
- for(Peaks::iterator i = frame->peaks_begin(); i != frame->peaks_end(); i++)
- {
- if(peak_num == 0)
- {
- CPPUNIT_ASSERT_DOUBLES_EQUAL(1.0, i->amplitude, PRECISION);
- }
- else if(peak_num == 1)
- {
- CPPUNIT_ASSERT_DOUBLES_EQUAL(2.0, i->amplitude, PRECISION);
- }
- peak_num += 1;
- }
- frame->clear_peaks();
- }
-
public:
- void setUp()
- {
+ void setUp() {
frame = new Frame();
}
- void tearDown()
- {
+ void tearDown() {
delete frame;
- }
-};
-
-// ---------------------------------------------------------------------------
-// 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_next_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(test_find_peaks_in_frame);
- CPPUNIT_TEST(test_find_peaks);
- 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_next_frame_size()
- {
- CPPUNIT_ASSERT(pd->next_frame_size() == pd->frame_size());
- }
-
- 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);
- }
-
- void test_find_peaks_in_frame()
- {
- Frame* f = new Frame();
- Peaks* p = pd->find_peaks_in_frame(*f);
- CPPUNIT_ASSERT(p->size() == 0);
- delete p;
- delete f;
- }
-
- void test_find_peaks()
- {
- const samples audio = samples(1024);
- pd->frame_size(256);
- pd->hop_size(256);
- Frames* frames = pd->find_peaks(audio);
- CPPUNIT_ASSERT(frames->size() == 4);
- for(Frames::iterator i = frames->begin(); i != frames->end(); i++)
- {
- CPPUNIT_ASSERT(i->num_peaks() == 0);
- }
- }
-
-public:
- void setUp()
- {
- pd = new PeakDetection();
- }
-
- void tearDown()
- {
- delete pd;
- }
};
-} // end of namespace Simpl
+} // end of namespace simpl
-CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestPeak);
-CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestFrame);
-CPPUNIT_TEST_SUITE_REGISTRATION(Simpl::TestPeakDetection);
+CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestPeak);
+CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestFrame);
-int main(int arg, char **argv)
-{
+int main(int arg, char **argv) {
CppUnit::TextTestRunner runner;
runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
return runner.run("", false);
diff --git a/tests/test_peak_detection.cpp b/tests/test_peak_detection.cpp
new file mode 100644
index 0000000..41bc735
--- /dev/null
+++ b/tests/test_peak_detection.cpp
@@ -0,0 +1,89 @@
+#include <iostream>
+#include <cppunit/ui/text/TextTestRunner.h>
+#include <cppunit/TestResult.h>
+#include <cppunit/TestResultCollector.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/BriefTestProgressListener.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <sndfile.hh>
+
+#include "../src/simpl/base.h"
+#include "../src/simpl/peak_detection.h"
+
+namespace simpl
+{
+
+// ---------------------------------------------------------------------------
+// TestLorisPeakDetection
+// ---------------------------------------------------------------------------
+class TestLorisPeakDetection : public CPPUNIT_NS::TestCase {
+ CPPUNIT_TEST_SUITE(TestLorisPeakDetection);
+ CPPUNIT_TEST(test_find_peaks_in_frame_basic);
+ CPPUNIT_TEST(test_find_peaks_basic);
+ CPPUNIT_TEST(test_find_peaks_change_hop_frame_size);
+ CPPUNIT_TEST(test_find_peaks_audio);
+ CPPUNIT_TEST_SUITE_END();
+
+protected:
+ static const double PRECISION = 0.001;
+ LorisPeakDetection* pd;
+ SndfileHandle sf;
+ int num_samples;
+
+ void test_find_peaks_in_frame_basic() {
+ Frame* f = new Frame(2048, true);
+ Peaks p = pd->find_peaks_in_frame(f);
+ CPPUNIT_ASSERT(p.size() == 0);
+ delete f;
+ }
+
+ void test_find_peaks_basic() {
+ sample* audio = new sample[1024];
+ Frames frames = pd->find_peaks(1024, audio);
+ CPPUNIT_ASSERT(frames.size() == 2);
+ for(int i = 0; i < frames.size(); i++) {
+ CPPUNIT_ASSERT(frames[i]->num_peaks() == 0);
+ }
+ }
+
+ void test_find_peaks_change_hop_frame_size() {
+ sample* audio = new sample[1024];
+ pd->frame_size(256);
+ pd->hop_size(256);
+ Frames frames = pd->find_peaks(1024, audio);
+ CPPUNIT_ASSERT(frames.size() == 4);
+ for(int i = 0; i < frames.size(); i++) {
+ CPPUNIT_ASSERT(frames[i]->num_peaks() == 0);
+ }
+ }
+
+ void test_find_peaks_audio() {
+ sample* audio = new sample[(int)sf.frames()];
+ sf.read(audio, (int)sf.frames());
+ Frames frames = pd->find_peaks(num_samples, &(audio[(int)sf.frames() / 2]));
+ for(int i = 0; i < frames.size(); i++) {
+ CPPUNIT_ASSERT(frames[i]->num_peaks() > 0);
+ }
+ }
+
+public:
+ void setUp() {
+ pd = new LorisPeakDetection();
+ sf = SndfileHandle("../tests/audio/flute.wav");
+ num_samples = 4096;
+ }
+
+ void tearDown() {
+ delete pd;
+ }
+};
+
+} // end of namespace simpl
+
+CPPUNIT_TEST_SUITE_REGISTRATION(simpl::TestLorisPeakDetection);
+
+int main(int arg, char **argv) {
+ CppUnit::TextTestRunner runner;
+ runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
+ return runner.run("", false);
+}