aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/jsyn/ports/UnitInputPort.java
blob: 3eda1f6aea27be7fbc43ac2a71889cdf5fd9f423 (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
/*
 * 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.ports;

import java.io.PrintStream;

import com.softsynth.shared.time.ScheduledCommand;
import com.softsynth.shared.time.TimeStamp;

/**
 * A port that is used to pass values into a UnitGenerator.
 *
 * @author Phil Burk 2009 Mobileer Inc
 */
public class UnitInputPort extends UnitBlockPort implements ConnectableInput, SettablePort {
    private double minimum = 0.0;
    private double maximum = 1.0;
    private double defaultValue = 0.0;
    private double[] setValues;
    private boolean valueAdded = false;

    /**
     * @param numParts typically 1, use 2 for stereo ports
     * @param name name that may be used in GUIs
     * @param defaultValue
     */
    public UnitInputPort(int numParts, String name, double defaultValue) {
        super(numParts, name, defaultValue);
        setDefault(defaultValue);
        setValues = new double[numParts];
        for (int i = 0; i < numParts; i++) {
            setValues[i] = defaultValue;
        }
    }

    public UnitInputPort(String name, double defaultValue) {
        this(1, name, defaultValue);
    }

    public UnitInputPort(String name) {
        this(1, name, 0.0);
    }

    public UnitInputPort(int numParts, String name) {
        this(numParts, name, 0.0);
    }

    @Override
    protected void makeParts(int numParts, double defaultValue) {
        parts = new InputMixingBlockPart[numParts];
        for (int i = 0; i < numParts; i++) {
            parts[i] = new InputMixingBlockPart(this, defaultValue);
        }
    }

    /**
     * This is used internally by the SynthesisEngine to execute units based on their connections.
     *
     * @param frameCount
     * @param start
     * @param limit
     */
    @Override
    public void pullData(long frameCount, int start, int limit) {
        for (PortBlockPart part : parts) {
            ((InputMixingBlockPart) part).pullData(frameCount, start, limit);
        }
    }

    @Override
    protected void setValueInternal(int partNum, double value) {
        super.setValueInternal(partNum, value);
        setValues[partNum] = value;
    }

    public void set(double value) {
        set(0, value);
    }

    public void set(final int partNum, final double value) {
        // Trigger exception now if out of range.
        setValues[partNum] = value;
        queueCommand(new ScheduledCommand() {
            @Override
            public void run() {
                setValueInternal(partNum, value);
            }
        });
    }

    public void set(double value, TimeStamp time) {
        set(0, value, time);
    }

    public void set(double value, double time) {
        set(0, value, time);
    }

    public void set(int partNum, double value, double time) {
        set(partNum, value, new TimeStamp(time));
    }

    @Override
    public void set(final int partNum, final double value, TimeStamp timeStamp) {
        // Trigger exception now if out of range.
        getValue(partNum);
        scheduleCommand(timeStamp, new ScheduledCommand() {
            @Override
            public void run() {
                setValueInternal(partNum, value);
            }
        });
    }

    /**
     * Value of a port based on the set() calls. Not affected by connected ports.
     *
     * @param partNum
     * @return value as set
     */
    @Override
    public double get(int partNum) {
        return setValues[partNum];
    }

    public double getMaximum() {
        return maximum;
    }

    /**
     * The minimum and maximum are only used when setting up knobs or other control systems. The
     * internal values are not clipped to this range.
     *
     * @param maximum
     */
    public void setMaximum(double maximum) {
        this.maximum = maximum;
    }

    public double getMinimum() {
        return minimum;
    }

    public void setMinimum(double minimum) {
        this.minimum = minimum;
    }

    public double getDefault() {
        return defaultValue;
    }

    public void setDefault(double defaultValue) {
        this.defaultValue = defaultValue;
    }

    /**
     * Convenience function for setting limits on a port. These limits are recommended values when
     * setting up a GUI. It is possible to set a port to a value outside these limits.
     *
     * @param minimum
     * @param value default value, will be clipped to min/max
     * @param maximum
     */
    public void setup(double minimum, double value, double maximum) {
        setMinimum(minimum);
        setMaximum(maximum);
        setDefault(value);
        set(value);
    }

    // Grab min, max, default from another port.
    public void setup(UnitInputPort other) {
        setup(other.getMinimum(), other.getDefault(), other.getMaximum());
    }

    public boolean isValueAdded() {
        return valueAdded;
    }

    /**
     * If set false then the set() value will be ignored when other ports are connected to this port.
     * The sum of the connected port values will be used instead.
     *
     * If set true then the set() value will be added to the sum of the connected port values.
     * This is useful when you want to modulate the set value.
     *
     * The default is false.
     *
     * @param valueAdded
     */
    public void setValueAdded(boolean valueAdded) {
        this.valueAdded = valueAdded;
    }

    public void connect(int thisPartNum, UnitOutputPort otherPort, int otherPartNum,
            TimeStamp timeStamp) {
        otherPort.connect(otherPartNum, this, thisPartNum, timeStamp);
    }

    /** Connect an input to an output port. */
    public void connect(int thisPartNum, UnitOutputPort otherPort, int otherPartNum) {
        // Typically connections are made from output to input because it is
        // more intuitive.
        otherPort.connect(otherPartNum, this, thisPartNum);
    }

    public void connect(UnitOutputPort otherPort) {
        connect(0, otherPort, 0);
    }

    @Override
    public void connect(ConnectableOutput other) {
        other.connect(this);
    }

    public void disconnect(int thisPartNum, UnitOutputPort otherPort, int otherPartNum) {
        otherPort.disconnect(otherPartNum, this, thisPartNum);
    }

    @Override
    public PortBlockPart getPortBlockPart() {
        return parts[0];
    }

    public ConnectableInput getConnectablePart(int i) {
        return parts[i];
    }

    @Override
    public void disconnect(ConnectableOutput other) {
        other.disconnect(this);
    }

    public void printConnections(PrintStream out, int level) {
        for (PortBlockPart part : parts) {
            ((InputMixingBlockPart) part).printConnections(out, level);
        }
    }

}