aboutsummaryrefslogtreecommitdiffstats
path: root/tests/com/jsyn/unitgen/TestRamps.java
blob: 83ebacfe40847888c5ce52c849c5fccf53b03e69 (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
/*
 * 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;

public class TestRamps extends NonRealTimeTestCase {

    public void viewContinuousRamp(double duration, double startValue, double targetValue)
            throws InterruptedException {
        ContinuousRamp ramp = new ContinuousRamp();
        synthesisEngine.add(ramp);

        ramp.current.set(startValue);
        ramp.input.set(startValue);
        ramp.time.set(duration);

        synthesisEngine.setRealTime(false);
        synthesisEngine.start();
        ramp.start();
        synthesisEngine.sleepUntil(synthesisEngine.getCurrentTime() + 0.01);
        ramp.input.set(targetValue);

        double time = synthesisEngine.getCurrentTime();
        int numLoops = 20;
        double increment = duration / numLoops;
        for (int i = 0; i < (numLoops + 1); i++) {
            double value = ramp.output.getValue();
            System.out.printf("i = %d, t = %9.5f,  value = %8.4f\n", i, time, value);
            time += increment;
            synthesisEngine.sleepUntil(time);
        }

        synthesisEngine.stop();
    }

    public void checkContinuousRamp(double duration, double startValue, double targetValue)
            throws InterruptedException {
        ContinuousRamp ramp = new ContinuousRamp();
        synthesisEngine.add(ramp);

        ramp.current.set(startValue);
        ramp.input.set(startValue);
        ramp.time.set(duration);

        synthesisEngine.setRealTime(false);
        synthesisEngine.start();
        ramp.start();
        synthesisEngine.sleepUntil(synthesisEngine.getCurrentTime() + 0.01);
        assertEquals("start flat", ramp.input.getValue(), ramp.output.getValue());

        ramp.input.set(targetValue);
        double startTime = synthesisEngine.getCurrentTime();
        synthesisEngine.sleepUntil(startTime + (duration / 2));
        assertEquals("ramping up", (targetValue + startValue) / 2.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + duration);
        assertEquals("ramping up", targetValue, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + duration + 0.1);
        assertEquals("flat again", targetValue, ramp.output.getValue());

        synthesisEngine.stop();
    }

    public void testContinuousRamp() throws InterruptedException {
        viewContinuousRamp(4.0, 0.0, 1.0);
    }

    public void testExponentialRamp() throws InterruptedException {
        ExponentialRamp ramp = new ExponentialRamp();
        synthesisEngine.add(ramp);

        double duration = 0.3;
        ramp.current.set(1.0);
        ramp.input.set(1.0);
        ramp.time.set(duration);

        synthesisEngine.start();
        ramp.start();
        synthesisEngine.sleepUntil(synthesisEngine.getCurrentTime() + 0.01);
        assertEquals("start flat", ramp.input.getValue(), ramp.output.getValue());

        ramp.input.set(8.0);
        double startTime = synthesisEngine.getCurrentTime();
        synthesisEngine.sleepUntil(startTime + 0.1);
        assertEquals("ramping up", 2.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.2);
        assertEquals("ramping up", 4.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.3);
        assertEquals("ramping up", 8.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.4);
        assertEquals("flat again", 8.0, ramp.output.getValue());
    }

    public void testLinearRamp() throws InterruptedException {
        LinearRamp ramp = new LinearRamp();
        synthesisEngine.add(ramp);

        double duration = 0.4;
        ramp.current.set(0.0);
        ramp.input.set(0.0);
        ramp.time.set(duration);

        synthesisEngine.start();
        ramp.start();
        synthesisEngine.sleepUntil(synthesisEngine.getCurrentTime() + 0.01);
        assertEquals("start flat", ramp.input.getValue(), ramp.output.getValue());

        ramp.input.set(8.0);
        double startTime = synthesisEngine.getCurrentTime();
        synthesisEngine.sleepUntil(startTime + 0.1);
        assertEquals("ramping up", 2.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.2);
        assertEquals("ramping up", 4.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.3);
        assertEquals("ramping up", 6.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.4);
        assertEquals("flat again", 8.0, ramp.output.getValue());
    }

    public void testExponentialRampConnected() throws InterruptedException {
        ExponentialRamp ramp = new ExponentialRamp();
        PassThrough pass = new PassThrough();
        synthesisEngine.add(ramp);
        synthesisEngine.add(pass);

        double duration = 0.3;
        ramp.current.set(1.0);
        pass.input.set(1.0);
        ramp.time.set(duration);

        // Send value through a connected unit.
        pass.input.set(1.0);
        pass.output.connect(ramp.input);

        synthesisEngine.start();
        ramp.start();
        synthesisEngine.sleepUntil(synthesisEngine.getCurrentTime() + 0.01);
        assertEquals("start flat", ramp.input.getValue(), ramp.output.getValue());

        pass.input.set(8.0);
        double startTime = synthesisEngine.getCurrentTime();
        synthesisEngine.sleepUntil(startTime + 0.1);
        assertEquals("ramping up", 2.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.2);
        assertEquals("ramping up", 4.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.3);
        assertEquals("ramping up", 8.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.4);
        assertEquals("flat again", 8.0, ramp.output.getValue());
    }

    public void testLinearRampConnected() throws InterruptedException {
        LinearRamp ramp = new LinearRamp();
        PassThrough pass = new PassThrough();
        synthesisEngine.add(ramp);
        synthesisEngine.add(pass);

        double duration = 0.4;
        ramp.current.set(0.0);
        pass.input.set(0.0);
        ramp.time.set(duration);

        // Send value through a connected unit.
        pass.input.set(0.0);
        pass.output.connect(ramp.input);

        synthesisEngine.start();
        ramp.start();
        synthesisEngine.sleepUntil(synthesisEngine.getCurrentTime() + 0.01);
        assertEquals("start flat", ramp.input.getValue(), ramp.output.getValue());

        pass.input.set(8.0);
        double startTime = synthesisEngine.getCurrentTime();
        synthesisEngine.sleepUntil(startTime + 0.1);
        assertEquals("ramping up", 2.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.2);
        assertEquals("ramping up", 4.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.3);
        assertEquals("ramping up", 6.0, ramp.output.getValue(), 0.01);
        synthesisEngine.sleepUntil(startTime + 0.4);
        assertEquals("flat again", 8.0, ramp.output.getValue());
    }

}