aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/unitgen/TestMath.java
blob: cae1deacc6086e4204c0edf23d2af4d13c280111 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * Copyright 2009 Phil Burk, Mobileer Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jsyn.unitgen;

import junit.framework.TestCase;

import com.jsyn.engine.SynthesisEngine;
import com.softsynth.math.AudioMath;

/**
 * @author Phil Burk, (C) 2009 Mobileer Inc
 */
public class TestMath extends TestCase {
    SynthesisEngine synthesisEngine;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        synthesisEngine = new SynthesisEngine();
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testAdd() {
        Add add = new Add();
        add.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        add.inputA.setValueInternal(x);
        add.inputB.setValueInternal(y);

        add.generate();

        assertEquals("Add", x + y, add.output.getValue(), 0.001);
    }

    public void testPartialAdd() {
        Add add = new Add();
        add.setSynthesisEngine(synthesisEngine);

        double x = 2.5;
        double y = 9.7;
        add.inputA.setValueInternal(x);
        add.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        add.generate(2, 5);

        assertEquals("Add partial", 0.0, add.output.getValues()[0], 0.001);
        assertEquals("Add partial", 0.0, add.output.getValues()[1], 0.001);
        assertEquals("Add partial", x + y, add.output.getValues()[2], 0.001);
        assertEquals("Add partial", x + y, add.output.getValues()[3], 0.001);
        assertEquals("Add partial", x + y, add.output.getValues()[4], 0.001);
        assertEquals("Add partial", 0.0, add.output.getValues()[5], 0.001);
        assertEquals("Add partial", 0.0, add.output.getValues()[6], 0.001);
        assertEquals("Add partial", 0.0, add.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for Subtract.java - added by Lisa Tolentino 06/17/2009
     */
    public void testSubtract() {
        Subtract sub = new Subtract();
        sub.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        sub.inputA.setValueInternal(x);
        sub.inputB.setValueInternal(y);

        sub.generate();

        assertEquals("Subtract", x - y, sub.output.getValue(), 0.001);
    }

    public void testPartialSubtract() {
        Subtract sub = new Subtract();
        sub.setSynthesisEngine(synthesisEngine);

        double x = 2.5;
        double y = 9.7;
        sub.inputA.setValueInternal(x);
        sub.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        sub.generate(2, 5);

        assertEquals("Subtract partial", 0.0, sub.output.getValues()[0], 0.001);
        assertEquals("Subtract partial", 0.0, sub.output.getValues()[1], 0.001);
        assertEquals("Subtract partial", x - y, sub.output.getValues()[2], 0.001);
        assertEquals("Subtract partial", x - y, sub.output.getValues()[3], 0.001);
        assertEquals("Subtract partial", x - y, sub.output.getValues()[4], 0.001);
        assertEquals("Subtract partial", 0.0, sub.output.getValues()[5], 0.001);
        assertEquals("Subtract partial", 0.0, sub.output.getValues()[6], 0.001);
        assertEquals("Subtract partial", 0.0, sub.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for Multiply.java - added by Lisa Tolentino 06/19/2009
     */
    public void testMultiply() {
        Multiply mult = new Multiply();
        mult.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        mult.inputA.setValueInternal(x);
        mult.inputB.setValueInternal(y);

        mult.generate();

        assertEquals("Multiply", x * y, mult.output.getValue(), 0.001);
    }

    public void testPartialMultiply() {
        Multiply mult = new Multiply();
        mult.setSynthesisEngine(synthesisEngine);

        double x = 2.5;
        double y = 9.7;
        mult.inputA.setValueInternal(x);
        mult.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        mult.generate(2, 5);

        assertEquals("Multiply partial", 0.0, mult.output.getValues()[0], 0.001);
        assertEquals("Multiply partial", 0.0, mult.output.getValues()[1], 0.001);
        assertEquals("Multiply partial", x * y, mult.output.getValues()[2], 0.001);
        assertEquals("Multiply partial", x * y, mult.output.getValues()[3], 0.001);
        assertEquals("Multiply partial", x * y, mult.output.getValues()[4], 0.001);
        assertEquals("Multiply partial", 0.0, mult.output.getValues()[5], 0.001);
        assertEquals("Multiply partial", 0.0, mult.output.getValues()[6], 0.001);
        assertEquals("Multiply partial", 0.0, mult.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for Divide.java - added by Lisa Tolentino 06/19/2009
     */
    public void testDivide() {
        Divide divide = new Divide();
        divide.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        divide.inputA.setValueInternal(x);
        divide.inputB.setValueInternal(y);

        divide.generate();

        assertEquals("Divide", x / y, divide.output.getValue(), 0.001);
    }

    public void testPartialDivide() {
        Divide divide = new Divide();
        divide.setSynthesisEngine(synthesisEngine);

        double x = 2.5;
        double y = 9.7;
        divide.inputA.setValueInternal(x);
        divide.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        divide.generate(2, 5);

        assertEquals("Divide partial", 0.0, divide.output.getValues()[0], 0.001);
        assertEquals("Divide partial", 0.0, divide.output.getValues()[1], 0.001);
        assertEquals("Divide partial", x / y, divide.output.getValues()[2], 0.001);
        assertEquals("Divide partial", x / y, divide.output.getValues()[3], 0.001);
        assertEquals("Divide partial", x / y, divide.output.getValues()[4], 0.001);
        assertEquals("Divide partial", 0.0, divide.output.getValues()[5], 0.001);
        assertEquals("Divide partial", 0.0, divide.output.getValues()[6], 0.001);
        assertEquals("Divide partial", 0.0, divide.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for MultiplyAdd.java - added by Lisa Tolentino 06/19/2009
     */
    public void testMultiplyAdd() {
        MultiplyAdd multAdd = new MultiplyAdd();
        multAdd.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        double z = 2.28;
        multAdd.inputA.setValueInternal(x);
        multAdd.inputB.setValueInternal(y);
        multAdd.inputC.setValueInternal(z);

        multAdd.generate();

        assertEquals("MultiplyAdd", (x * y) + z, multAdd.output.getValue(), 0.001);
    }

    public void testPartialMultiplyAdd() {
        MultiplyAdd multAdd = new MultiplyAdd();
        multAdd.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        double z = 2.28;
        multAdd.inputA.setValueInternal(x);
        multAdd.inputB.setValueInternal(y);
        multAdd.inputC.setValueInternal(z);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        multAdd.generate(2, 5);

        assertEquals("MultiplyAdd partial", 0.0, multAdd.output.getValues()[0], 0.001);
        assertEquals("MultiplyAdd partial", 0.0, multAdd.output.getValues()[1], 0.001);
        assertEquals("MultiplyAdd partial", (x * y) + z, multAdd.output.getValues()[2], 0.001);
        assertEquals("MultiplyAdd partial", (x * y) + z, multAdd.output.getValues()[3], 0.001);
        assertEquals("MultiplyAdd partial", (x * y) + z, multAdd.output.getValues()[4], 0.001);
        assertEquals("MultiplyAdd partial", 0.0, multAdd.output.getValues()[5], 0.001);
        assertEquals("MultiplyAdd partial", 0.0, multAdd.output.getValues()[6], 0.001);
        assertEquals("MultiplyAdd partial", 0.0, multAdd.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for Compare.java - added by Lisa Tolentino 06/19/2009
     */
    public void testCompare() {
        UnitBinaryOperator compare = new Compare();
        compare.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        compare.inputA.setValueInternal(x);
        compare.inputB.setValueInternal(y);

        compare.generate();

        assertEquals("Compare", (x > y ? 1 : 0), compare.output.getValue(), 0.001);
    }

    public void testPartialCompare() {
        UnitBinaryOperator compare = new Compare();
        compare.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        compare.inputA.setValueInternal(x);
        compare.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        compare.generate(2, 5);

        assertEquals("Compare partial", 0.0, compare.output.getValues()[0], 0.001);
        assertEquals("Compare partial", 0.0, compare.output.getValues()[1], 0.001);
        assertEquals("Compare partial", (x > y ? 1 : 0), compare.output.getValues()[2], 0.001);
        assertEquals("Compare partial", (x > y ? 1 : 0), compare.output.getValues()[3], 0.001);
        assertEquals("Compare partial", (x > y ? 1 : 0), compare.output.getValues()[4], 0.001);
        assertEquals("Compare partial", 0.0, compare.output.getValues()[5], 0.001);
        assertEquals("Compare partial", 0.0, compare.output.getValues()[6], 0.001);
        assertEquals("Compare partial", 0.0, compare.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for Maximum.java - added by Lisa Tolentino 06/20/2009
     */
    public void testMaximum() {
        Maximum max = new Maximum();
        max.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        max.inputA.setValueInternal(x);
        max.inputB.setValueInternal(y);

        max.generate();

        assertEquals("Maximum", (x > y ? x : y), max.output.getValue(), 0.001);
    }

    public void testPartialMaximum() {
        Maximum max = new Maximum();
        max.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        max.inputA.setValueInternal(x);
        max.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        max.generate(2, 5);

        assertEquals("Maximum partial", 0.0, max.output.getValues()[0], 0.001);
        assertEquals("Maximum partial", 0.0, max.output.getValues()[1], 0.001);
        assertEquals("Maximum partial", (x > y ? x : y), max.output.getValues()[2], 0.001);
        assertEquals("Maximum partial", (x > y ? x : y), max.output.getValues()[3], 0.001);
        assertEquals("Maximum partial", (x > y ? x : y), max.output.getValues()[4], 0.001);
        assertEquals("Maximum partial", 0.0, max.output.getValues()[5], 0.001);
        assertEquals("Maximum partial", 0.0, max.output.getValues()[6], 0.001);
        assertEquals("Maximum partial", 0.0, max.output.getValues()[7], 0.001);

    }

    /**
     * Unit test for Minimum.java - added by Lisa Tolentino 06/20/2009
     */
    public void testMinimum() {
        Minimum min = new Minimum();
        min.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        min.inputA.setValueInternal(x);
        min.inputB.setValueInternal(y);

        min.generate();

        assertEquals("Minimum", (x < y ? x : y), min.output.getValue(), 0.001);
    }

    public void testPartialMinimum() {
        Minimum min = new Minimum();
        min.setSynthesisEngine(synthesisEngine);

        double x = 33.99;
        double y = 8.31;
        min.inputA.setValueInternal(x);
        min.inputB.setValueInternal(y);

        // Only generate a few values in the middle.
        // This is to test low latency feedback loops.
        // Only generate values for 2,3,4
        min.generate(2, 5);

        assertEquals("Maximum partial", 0.0, min.output.getValues()[0], 0.001);
        assertEquals("Maximum partial", 0.0, min.output.getValues()[1], 0.001);
        assertEquals("Maximum partial", (x < y ? x : y), min.output.getValues()[2], 0.001);
        assertEquals("Maximum partial", (x < y ? x : y), min.output.getValues()[3], 0.001);
        assertEquals("Maximum partial", (x < y ? x : y), min.output.getValues()[4], 0.001);
        assertEquals("Maximum partial", 0.0, min.output.getValues()[5], 0.001);
        assertEquals("Maximum partial", 0.0, min.output.getValues()[6], 0.001);
        assertEquals("Maximum partial", 0.0, min.output.getValues()[7], 0.001);

    }

    public void testPowerOfTwo() {
        PowerOfTwo powerOfTwo = new PowerOfTwo();
        powerOfTwo.setSynthesisEngine(synthesisEngine);
        final double smallValue = -1.5308084989341915E-17;
        double values[] = {
                0.0, 1.3, 4.5, -0.5, -1.0, -2.8, smallValue, -smallValue, 1.0 - smallValue,
                1.0 + smallValue
        };
        for (double in : values) {
            powerOfTwo.input.setValueInternal(in);
            powerOfTwo.generate();
            assertEquals("PowerOfTwo", Math.pow(2.0, in), powerOfTwo.output.getValue(), 0.001);
        }
    }
    public void testPitchToFrequency() {
        PitchToFrequency ugen = new PitchToFrequency();
        ugen.setSynthesisEngine(synthesisEngine);
        final double smallValue = -1.5308084989341915E-17;
        double values[] = {
                49.0, 49.5, 50.0 + smallValue,
                60.0 -smallValue,
                79.2, 12.9, 118.973
        };
        // Sanity check AudioMath
        assertEquals("PitchToFrequency", 440.0,  AudioMath.pitchToFrequency(69), 0.001);
        assertEquals("PitchToFrequency", 660.0,  AudioMath.pitchToFrequency(69+7.02), 0.1);

        for (double pitch : values) {
            ugen.input.setValueInternal(pitch);
            ugen.generate();
            assertEquals("PitchToFrequency", AudioMath.pitchToFrequency(pitch),
                    ugen.output.getValue(), 0.001);
        }
    }

}