aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/com/jsyn/unitgen/ContinuousRamp.java
blob: dd9044507e2b5ac658b95df3a6a31a0b63ad71f8 (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
/*
 * 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.ports.UnitInputPort;
import com.jsyn.ports.UnitVariablePort;

/**
 * A ramp whose function over time is continuous in value and in slope. Also called an "S curve".
 * 
 * @author Phil Burk (C) 2009 Mobileer Inc
 * @version 016
 * @see LinearRamp
 * @see ExponentialRamp
 * @see AsymptoticRamp
 */
public class ContinuousRamp extends UnitFilter {
    public UnitVariablePort current;
    /**
     * Time it takes to get from current value to input value when input is changed. Default value
     * is 1.0 seconds.
     */
    public UnitInputPort time;
    private double previousInput = Double.MIN_VALUE;
    // Coefficients for cubic polynomial.
    private double a;
    private double b;
    private double d;
    private int framesLeft;

    /* Define Unit Ports used by connect() and set(). */
    public ContinuousRamp() {
        addPort(time = new UnitInputPort(1, "Time", 1.0));
        addPort(current = new UnitVariablePort("Current"));
    }

    @Override
    public void generate(int start, int limit) {
        double[] outputs = output.getValues();
        double[] inputs = input.getValues();
        double currentTime = time.getValues()[0];
        double currentValue = current.getValue();
        double inputValue = currentValue;

        for (int i = start; i < limit; i++) {
            inputValue = inputs[i];
            double x;
            if (inputValue != previousInput) {
                x = framesLeft;
                // Calculate coefficients.
                double currentSlope = x * ((3 * a * x) + (2 * b));

                framesLeft = (int) (getSynthesisEngine().getFrameRate() * currentTime);
                if (framesLeft < 1) {
                    framesLeft = 1;
                }
                x = framesLeft;
                // Calculate coefficients.
                d = inputValue;
                double xsq = x * x;
                b = ((3 * currentValue) - (currentSlope * x) - (3 * d)) / xsq;
                a = (currentSlope - (2 * b * x)) / (3 * xsq);
                previousInput = inputValue;
            }

            if (framesLeft > 0) {
                x = --framesLeft;
                // Cubic polynomial. c==0
                currentValue = (x * (x * ((x * a) + b))) + d;
            }

            outputs[i] = currentValue;
        }

        current.setValue(currentValue);
    }
}