c@362
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
c@362
|
2
|
c@362
|
3 #include "Resampler.h"
|
c@362
|
4
|
c@362
|
5 #include <iostream>
|
c@362
|
6
|
c@362
|
7 #include <cmath>
|
c@362
|
8
|
c@362
|
9 #define BOOST_TEST_DYN_LINK
|
c@362
|
10 #define BOOST_TEST_MAIN
|
c@362
|
11
|
c@362
|
12 #include <boost/test/unit_test.hpp>
|
c@362
|
13
|
c@362
|
14 BOOST_AUTO_TEST_SUITE(TestResampler)
|
c@362
|
15
|
c@362
|
16 using std::cout;
|
c@362
|
17 using std::endl;
|
c@363
|
18 using std::vector;
|
c@363
|
19
|
c@363
|
20 void
|
c@363
|
21 testResamplerOneShot(int sourceRate,
|
c@363
|
22 int targetRate,
|
c@363
|
23 int n,
|
c@363
|
24 double *in,
|
c@363
|
25 int m,
|
c@363
|
26 double *expected)
|
c@363
|
27 {
|
c@363
|
28 vector<double> resampled = Resampler::resample(sourceRate, targetRate,
|
c@363
|
29 in, n);
|
c@363
|
30 BOOST_CHECK_EQUAL(resampled.size(), m);
|
c@363
|
31 for (int i = 0; i < m; ++i) {
|
c@363
|
32 BOOST_CHECK_SMALL(resampled[i] - expected[i], 1e-8);
|
c@363
|
33 }
|
c@363
|
34 }
|
c@362
|
35
|
c@362
|
36 void
|
c@362
|
37 testResampler(int sourceRate,
|
c@362
|
38 int targetRate,
|
c@362
|
39 int n,
|
c@362
|
40 double *in,
|
c@362
|
41 int m,
|
c@362
|
42 double *expected)
|
c@362
|
43 {
|
c@364
|
44 // Here we provide the input in chunks (of varying size)
|
c@363
|
45
|
c@362
|
46 Resampler r(sourceRate, targetRate);
|
c@362
|
47 int latency = r.getLatency();
|
c@362
|
48 std::cerr << "latency = " << latency << std::endl;
|
c@362
|
49
|
c@362
|
50 int m1 = m + latency;
|
c@362
|
51 int n1 = int((m1 * sourceRate) / targetRate);
|
c@362
|
52
|
c@362
|
53 double *inPadded = new double[n1];
|
c@362
|
54 double *outPadded = new double[m1];
|
c@362
|
55
|
c@362
|
56 for (int i = 0; i < n1; ++i) {
|
c@362
|
57 if (i < n) inPadded[i] = in[i];
|
c@362
|
58 else inPadded[i] = 0.0;
|
c@362
|
59 }
|
c@362
|
60
|
c@362
|
61 for (int i = 0; i < m1; ++i) {
|
c@362
|
62 outPadded[i] = -999.0;
|
c@362
|
63 }
|
c@362
|
64
|
c@364
|
65 int chunkSize = 1;
|
c@364
|
66 int got = 0;
|
c@364
|
67 int i = 0;
|
c@362
|
68
|
c@364
|
69 while (true) {
|
c@364
|
70 std::cerr << "i = " << i << ", n1 = " << n1 << ", chunkSize = " << chunkSize << std::endl;
|
c@364
|
71 got += r.process(inPadded + i, outPadded + got, chunkSize);
|
c@364
|
72 i = i + chunkSize;
|
c@364
|
73 chunkSize = chunkSize + 1;
|
c@364
|
74 if (i + 1 >= n1) {
|
c@364
|
75 break;
|
c@364
|
76 } else if (i + chunkSize >= n1) {
|
c@364
|
77 chunkSize = n1 - i;
|
c@364
|
78 }
|
c@364
|
79 }
|
c@364
|
80
|
c@364
|
81 // int got = r.process(inPadded, outPadded, n1);
|
c@364
|
82 std::cerr << i << " in, " << got << " out" << std::endl;
|
c@362
|
83
|
c@362
|
84 BOOST_CHECK_EQUAL(got, m1);
|
c@364
|
85 /*
|
c@362
|
86 std::cerr << "results including latency padding:" << std::endl;
|
c@362
|
87 for (int i = 0; i < m1; ++i) {
|
c@362
|
88 std::cerr << outPadded[i] << " ";
|
c@362
|
89 if (i % 6 == 5) std::cerr << "\n";
|
c@362
|
90 }
|
c@362
|
91 std::cerr << "\n";
|
c@364
|
92 */
|
c@362
|
93 for (int i = latency; i < m1; ++i) {
|
c@363
|
94 BOOST_CHECK_SMALL(outPadded[i] - expected[i-latency], 1e-8);
|
c@362
|
95 }
|
c@362
|
96 delete[] outPadded;
|
c@362
|
97 delete[] inPadded;
|
c@362
|
98 }
|
c@362
|
99
|
c@362
|
100 BOOST_AUTO_TEST_CASE(sameRate)
|
c@362
|
101 {
|
c@362
|
102 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 };
|
c@363
|
103 testResamplerOneShot(4, 4, 10, d, 10, d);
|
c@362
|
104 testResampler(4, 4, 10, d, 10, d);
|
c@362
|
105 }
|
c@362
|
106
|
c@362
|
107 BOOST_AUTO_TEST_SUITE_END()
|
c@362
|
108
|