summaryrefslogtreecommitdiff
path: root/libs/ardour/test/interpolation_test.cc
blob: c9fae7e64f0424381af6f8e292c0d4d72d8afe28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <sigc++/sigc++.h>
#include "interpolation_test.h"

CPPUNIT_TEST_SUITE_REGISTRATION(InterpolationTest);

using namespace std;
using namespace ARDOUR;

void
InterpolationTest::linearInterpolationTest ()
{
	samplecnt_t result = 0;
//	cout << "\nLinear Interpolation Test\n";

//	cout << "\nSpeed: 1/3";
	for (int i = 0; 3*i < NUM_SAMPLES - 1024;) {
		linear.set_speed (double(1.0)/double(3.0));
		linear.set_target_speed (double(1.0)/double(3.0));
		result = linear.interpolate (0, 1024, input + i, output + i*3);
		i += result;
	}

//	cout << "\nSpeed: 1.0";
	linear.reset();
	linear.set_speed (1.0);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * linear.speed()), result);
	for (int i = 0; i < NUM_SAMPLES; i += INTERVAL) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}

//	cout << "\nSpeed: 0.5";
	linear.reset();
	linear.set_speed (0.5);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * linear.speed()), result);
	for (int i = 0; i < NUM_SAMPLES; i += (INTERVAL / linear.speed() +0.5)) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}

//	cout << "\nSpeed: 0.2";
	linear.reset();
	linear.set_speed (0.2);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * linear.speed()), result);

//	cout << "\nSpeed: 0.02";
	linear.reset();
	linear.set_speed (0.02);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * linear.speed()), result);

//	cout << "\nSpeed: 0.002";
	linear.reset();
	linear.set_speed (0.002);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES, input, output);
	linear.speed();
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * linear.speed()), result);

//	cout << "\nSpeed: 2.0";
	linear.reset();
	linear.set_speed (2.0);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES / 2, input, output);
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES / 2 * linear.speed()), result);
	for (int i = 0; i < NUM_SAMPLES / 2; i += (INTERVAL / linear.speed() +0.5)) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}

//	cout << "\nSpeed: 10.0";
	linear.set_speed (10.0);
	linear.set_target_speed (linear.speed());
	result = linear.interpolate (0, NUM_SAMPLES / 10, input, output);
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES / 10 * linear.speed()), result);
	for (int i = 0; i < NUM_SAMPLES / 10; i += (INTERVAL / linear.speed() +0.5)) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}
	/*
	   for (int i=0; i < NUM_SAMPLES; ++i) {
	   cout  << i << " " << output[i] << endl;
	   }
	   */
}

void
InterpolationTest::cubicInterpolationTest ()
{
	samplecnt_t result = 0;
//	cout << "\nCubic Interpolation Test\n";

//	cout << "\nSpeed: 1/3";
	for (int i = 0; 3*i < NUM_SAMPLES - 1024;) {
		cubic.set_speed (double(1.0)/double(3.0));
		cubic.set_target_speed (double(1.0)/double(3.0));
		result = cubic.interpolate (0, 1024, input + i, output + i*3);
		i += result;
	}

//	cout << "\nSpeed: 1.0";
	cubic.reset();
	cubic.set_speed (1.0);
	cubic.set_target_speed (cubic.speed());
	result = cubic.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL (result, cubic.interpolate (0, NUM_SAMPLES, NULL, NULL));
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * cubic.speed()), result);
	for (int i = 0; i < NUM_SAMPLES; i += INTERVAL) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}

//	cout << "\nSpeed: 0.5";
	cubic.reset();
	cubic.set_speed (0.5);
	cubic.set_target_speed (cubic.speed());
	result = cubic.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL (result, cubic.interpolate (0, NUM_SAMPLES, NULL, NULL));
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * cubic.speed()), result);
	for (int i = 0; i < NUM_SAMPLES; i += (INTERVAL / cubic.speed() +0.5)) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}

//	cout << "\nSpeed: 0.2";
	cubic.reset();
	cubic.set_speed (0.2);
	cubic.set_target_speed (cubic.speed());
	result = cubic.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL (result, cubic.interpolate (0, NUM_SAMPLES, NULL, NULL));
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * cubic.speed()), result);

//	cout << "\nSpeed: 0.02";
	cubic.reset();
	cubic.set_speed (0.02);
	cubic.set_target_speed (cubic.speed());
	result = cubic.interpolate (0, NUM_SAMPLES, input, output);
	CPPUNIT_ASSERT_EQUAL (result, cubic.interpolate (0, NUM_SAMPLES, NULL, NULL));
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * cubic.speed()), result);

	/* This one fails due too error accumulation
	   cout << "\nSpeed: 0.002";
	   cubic.reset();
	   cubic.set_speed (0.002);
	   cubic.set_target_speed (cubic.speed());
	   result = cubic.interpolate (0, NUM_SAMPLES, input, output);
	   cubic.speed();
	   CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES * cubic.speed()), result);
	   */

//	cout << "\nSpeed: 2.0";
	cubic.reset();
	cubic.set_speed (2.0);
	cubic.set_target_speed (cubic.speed());
	result = cubic.interpolate (0, NUM_SAMPLES / 2, input, output);
	CPPUNIT_ASSERT_EQUAL (result, cubic.interpolate (0, NUM_SAMPLES / 2, NULL, NULL));
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES / 2 * cubic.speed()), result);
	for (int i = 0; i < NUM_SAMPLES / 2; i += (INTERVAL / cubic.speed() +0.5)) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}

//	cout << "\nSpeed: 10.0";
	cubic.set_speed (10.0);
	cubic.set_target_speed (cubic.speed());
	result = cubic.interpolate (0, NUM_SAMPLES / 10, input, output);
	CPPUNIT_ASSERT_EQUAL (result, cubic.interpolate (0, NUM_SAMPLES / 10, NULL, NULL));
	CPPUNIT_ASSERT_EQUAL ((samplecnt_t)(NUM_SAMPLES / 10 * cubic.speed()), result);
	for (int i = 0; i < NUM_SAMPLES / 10; i += (INTERVAL / cubic.speed() +0.5)) {
		CPPUNIT_ASSERT_EQUAL (1.0f, output[i]);
	}
}