aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/jsyn/unitgen/TestDelay.java
blob: 7e1a0b14283d75ab34a8c9d036fdaa088e64d679 (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
/*
 * 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 com.jsyn.util.AudioStreamReader;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class TestDelay extends NonRealTimeTestCase {

    @Test
    public void testFloor() {
        double x = -7.3;
        int n = (int) Math.floor(x);
        assertEquals(-8, n, "int");
    }

    public void checkInterpolatingDelay(int maxFrames, double delayFrames)
            throws InterruptedException {
        synthesisEngine.start();

        System.out.printf("test delayFrames = %7.5f\n", delayFrames);
        InterpolatingDelay delay = new InterpolatingDelay();
        synthesisEngine.add(delay);
        delay.allocate(maxFrames);
        delay.delay.set(delayFrames / 44100.0);
        SawtoothOscillator osc = new SawtoothOscillator();
        synthesisEngine.add(osc);
        osc.frequency.set(synthesisEngine.getFrameRate() / 4.0);
        osc.amplitude.set(1.0);
        osc.output.connect(delay.input);

        int samplesPerFrame = 1;
        AudioStreamReader reader = new AudioStreamReader(synthesisEngine, samplesPerFrame);
        delay.output.connect(reader.getInput());

        delay.start();
        for (int i = 0; i < (3 * maxFrames); i++) {
            if (reader.available() == 0) {
                synthesisEngine.sleepFor(0.01);
            }
            double actual = reader.read();
            double expected = 1 + i - delayFrames;
            if (expected < 0.0) {
                expected = 0.0;
            }
            // System.out.printf( "[%d] expected = %7.3f, delayed = %7.3f\n", i, expected, actual );
            // assertEquals(expected, actual, 0.00001, "delayed output");
        }
    }

    @Test
    public void testSmall() throws InterruptedException {
        checkInterpolatingDelay(40, 7.0);
    }

    @Test
    public void testEven() throws InterruptedException {
        checkInterpolatingDelay(44100, 13671.0);
    }

    @Test
    public void testInterpolatingDelay() throws InterruptedException {
        checkInterpolatingDelay(44100, 13671.4);
    }
}