From e49430f96bd0a5858097f6dc631480d49baab7a0 Mon Sep 17 00:00:00 2001
From: John Glover <j@johnglover.net>
Date: Wed, 22 Aug 2012 11:21:29 +0100
Subject: [loris] Add C++ LorisPeakDetection.

---
 tests/test_peak_detection.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 tests/test_peak_detection.cpp

(limited to 'tests/test_peak_detection.cpp')

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);
+}
-- 
cgit v1.2.3