aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/jsyn/unitgen/TestConnections.java
blob: 9ce7726763bd8711b3976665d23488ec789e4633 (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
/*
 * 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.JSyn;
import com.jsyn.Synthesizer;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

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

public class TestConnections {
    private Synthesizer synth;

    private Add add1;
    private Add add2;
    private Add add3;

    @BeforeEach
    private void beforeEach() {
        synth = JSyn.createSynthesizer();
        synth.setRealTime(false);
        synth.start();

        synth.add(add1 = new Add());
        synth.add(add2 = new Add());
        synth.add(add3 = new Add());

        add1.start();
        add2.start();
        add3.start();

        add1.inputA.set(0.1);
        add1.inputB.set(0.2);

        add2.inputA.set(0.4);
        add2.inputB.set(0.8);

        add3.inputA.set(1.6);
        add3.inputB.set(3.2);
    }

    @Test
    public void testSet() throws InterruptedException {
        synth.sleepFor(0.01);
        assertEquals(0.3, add1.output.getValue(), 0.0001, "set inputs of adder");
    }

    @Test
    public void testConnect() throws InterruptedException {
        synth.sleepFor(0.01);
        assertEquals(0.3, add1.output.getValue(), 0.0001, "set inputs of adder");
        assertEquals(1.2, add2.output.getValue(), 0.0001, "set inputs of adder");

        // Test different ways of connecting.
        add1.output.connect(add2.inputB);
        checkConnection();

        add1.output.connect(0, add2.inputB, 0);
        checkConnection();

        add1.output.connect(add2.inputB.getConnectablePart(0));
        checkConnection();

        add1.output.getConnectablePart(0).connect(add2.inputB);
        checkConnection();

        add1.output.getConnectablePart(0).connect(add2.inputB.getConnectablePart(0));
        checkConnection();

        add2.inputB.connect(add1.output);
        checkConnection();

        add2.inputB.connect(0, add1.output, 0);
        checkConnection();

        add2.inputB.connect(add1.output.getConnectablePart(0));
        checkConnection();

        add2.inputB.getConnectablePart(0).connect(add1.output);
        checkConnection();

        add2.inputB.getConnectablePart(0).connect(add1.output.getConnectablePart(0));
        checkConnection();
    }

    private void checkConnection() throws InterruptedException {
        synth.sleepFor(0.01);
        assertEquals(0.3, add1.output.getValue(), 0.0001, "connection should not change output");
        assertEquals(0.7, add2.output.getValue(), 0.0001, "replace set value with output");

        // Revert to set value after disconnection.
        add1.output.disconnectAll();
        synth.sleepFor(0.01);
        assertEquals(0.3, add1.output.getValue(), 0.0001, "still the same");
        assertEquals(1.2, add2.output.getValue(), 0.0001, "should revert to original set() value");
    }

}