summaryrefslogtreecommitdiff
path: root/libs/ardour/tests/interpolation-test.cc
blob: 6df46ad194f5b05118ae56cb8e45968b686978f2 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <sigc++/sigc++.h>
#include "interpolation-test.h"

CPPUNIT_TEST_SUITE_REGISTRATION( InterpolationTest );

using namespace std;
using namespace ARDOUR;

void
InterpolationTest::linearInterpolationTest ()
{
        nframes_t result = 0;
         cout << "\nLinear Interpolation Test\n";
         
         cout << "\nSpeed: 1/3";
         for (int i = 0; i < NUM_SAMPLES - 1024;) {
             linear.set_speed (double(1.0)/double(3.0));
             linear.set_target_speed (double(1.0)/double(3.0));
             //printf ("Interpolate: input: %d, output: %d, i: %d\n", input + i, output + i, i);
             result = linear.interpolate (0, 1024, input + i, output + i);
             printf ("Result: %d\n", result);
             //CPPUNIT_ASSERT_EQUAL ((uint32_t)((NUM_SAMPLES - 100) * interpolation.speed()), result);
             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 ((uint32_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 ((uint32_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 ((uint32_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 ((uint32_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();
         printf("BOOM!: expexted: %d, result = %d\n", (nframes_t)(NUM_SAMPLES * linear.speed()), result);
         CPPUNIT_ASSERT_EQUAL ((nframes_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 ((uint32_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 ((uint32_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 << "input[" << i << "] = " << input[i] << "  output[" << i << "] = " << output[i] << endl; 
     }
     */   
}

void
InterpolationTest::splineInterpolationTest ()
{
        nframes_t result = 0;
         cout << "\nspline Interpolation Test\n";
         
         cout << "\nSpeed: 1/2" << endl;
         spline.reset();
         spline.set_speed (0.5);
         int one_period = 1024;
         /*
         
         for (int i = 0; 2 * i < NUM_SAMPLES - one_period;) {
             result = spline.interpolate (0, one_period, input + i, output + int(2*i));
             i += result;
         }
         for (int i=0; i < NUM_SAMPLES - one_period; ++i) {
             //cout << "output[" << i << "] = " << output[i] << endl;    
             if (i % 200 == 0) { CPPUNIT_ASSERT_EQUAL (double(1.0), double(output[i])); }
             else if (i % 2 == 0) { CPPUNIT_ASSERT_EQUAL (double(0.0), double(output[i])); }
         }
         */
         
         /*
         // square function
         
         for (int i = 0; i < NUM_SAMPLES; ++i) {
             if (i % INTERVAL/8 < INTERVAL/16 ) {
                 input[i] = 1.0f;
             } else {
                 input[i] = 0.0f;
             }
             output[i] = 0.0f;
         }
         */
         
         cout << "\nSpeed: 1/60" << endl;
         spline.reset();
         spline.set_speed (1.0/60.0);
         
         one_period = 8192;
         
         for (int i = 0; 60 * i < NUM_SAMPLES - one_period;) {
             result = spline.interpolate (0, one_period, input + i, output + int(60*i));
             printf ("Result: %d\n", result);
             i += result;
         }
         for (int i=0; i < NUM_SAMPLES - one_period; ++i) {
             cout << "input[" << i << "] = " << input[i] << "  output[" << i << "] = " << output[i] << endl; 
             //if (i % 333 == 0) { CPPUNIT_ASSERT_EQUAL (double(1.0), double(output[i])); }
             //else if (i % 2 == 0) { CPPUNIT_ASSERT_EQUAL (double(0.0), double(output[i])); }
         }
}

void
InterpolationTest::libSamplerateInterpolationTest ()
{
    nframes_t result;
    
    cout << "\nLibSamplerate Interpolation Test\n";
/*
    cout << "\nSpeed: 1.0";
    interpolation.set_speed (1.0);
    for (int i = 0; i < NUM_SAMPLES;) {
        interpolation.set_speed (1.0);
        result = interpolation.interpolate (0, INTERVAL/10, input + i, output + i);
        CPPUNIT_ASSERT_EQUAL ((uint32_t)(INTERVAL/10 * interpolation.speed()), result);
        i += result;
    }
    
    for (int i = 0; i < NUM_SAMPLES; i += INTERVAL) {
        CPPUNIT_ASSERT_EQUAL (1.0f, output[i+1]);
    }
*/
    
    cout << "\nSpeed: 0.5";
    for (int i = 0; i < NUM_SAMPLES;) {
        interpolation.set_speed (0.5);
        //printf ("Interpolate: input: %d, output: %d, i: %d\n", input + i, output + i, i);
        result = interpolation.interpolate (0, NUM_SAMPLES - 100, input + i, output + i);
        printf ("Result: %d\n", result);
        //CPPUNIT_ASSERT_EQUAL ((uint32_t)((NUM_SAMPLES - 100) * interpolation.speed()), result);
        //i += result;
        break;
    }

    for (int i=0; i < NUM_SAMPLES; ++i) {
        cout << "input[" << i << "] = " << input[i] << "  output[" << i << "] = " << output[i] << endl;    
    }

    
    cout << "\nSpeed: 0.2";
    interpolation.set_speed (0.2);
    result = interpolation.interpolate (0, NUM_SAMPLES, input, output);
    CPPUNIT_ASSERT_EQUAL ((uint32_t)(NUM_SAMPLES * interpolation.speed()), result);

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

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