UNPKG

6.4 kBJavaScriptView Raw
1import { Param } from "../core/context/Param.js";
2import { ToneAudioNode, } from "../core/context/ToneAudioNode.js";
3import { connect } from "../core/context/ToneAudioNode.js";
4import { isAudioParam } from "../core/util/AdvancedTypeCheck.js";
5import { optionsFromArguments } from "../core/util/Defaults.js";
6import { ToneConstantSource } from "./ToneConstantSource.js";
7/**
8 * A signal is an audio-rate value. Tone.Signal is a core component of the library.
9 * Unlike a number, Signals can be scheduled with sample-level accuracy. Tone.Signal
10 * has all of the methods available to native Web Audio
11 * [AudioParam](http://webaudio.github.io/web-audio-api/#the-audioparam-interface)
12 * as well as additional conveniences. Read more about working with signals
13 * [here](https://github.com/Tonejs/Tone.js/wiki/Signals).
14 *
15 * @example
16 * const osc = new Tone.Oscillator().toDestination().start();
17 * // a scheduleable signal which can be connected to control an AudioParam or another Signal
18 * const signal = new Tone.Signal({
19 * value: "C4",
20 * units: "frequency"
21 * }).connect(osc.frequency);
22 * // the scheduled ramp controls the connected signal
23 * signal.rampTo("C2", 4, "+0.5");
24 * @category Signal
25 */
26export class Signal extends ToneAudioNode {
27 constructor() {
28 const options = optionsFromArguments(Signal.getDefaults(), arguments, [
29 "value",
30 "units",
31 ]);
32 super(options);
33 this.name = "Signal";
34 /**
35 * Indicates if the value should be overridden on connection.
36 */
37 this.override = true;
38 this.output = this._constantSource = new ToneConstantSource({
39 context: this.context,
40 convert: options.convert,
41 offset: options.value,
42 units: options.units,
43 minValue: options.minValue,
44 maxValue: options.maxValue,
45 });
46 this._constantSource.start(0);
47 this.input = this._param = this._constantSource.offset;
48 }
49 static getDefaults() {
50 return Object.assign(ToneAudioNode.getDefaults(), {
51 convert: true,
52 units: "number",
53 value: 0,
54 });
55 }
56 connect(destination, outputNum = 0, inputNum = 0) {
57 // start it only when connected to something
58 connectSignal(this, destination, outputNum, inputNum);
59 return this;
60 }
61 dispose() {
62 super.dispose();
63 this._param.dispose();
64 this._constantSource.dispose();
65 return this;
66 }
67 //-------------------------------------
68 // ABSTRACT PARAM INTERFACE
69 // just a proxy for the ConstantSourceNode's offset AudioParam
70 // all docs are generated from AbstractParam.ts
71 //-------------------------------------
72 setValueAtTime(value, time) {
73 this._param.setValueAtTime(value, time);
74 return this;
75 }
76 getValueAtTime(time) {
77 return this._param.getValueAtTime(time);
78 }
79 setRampPoint(time) {
80 this._param.setRampPoint(time);
81 return this;
82 }
83 linearRampToValueAtTime(value, time) {
84 this._param.linearRampToValueAtTime(value, time);
85 return this;
86 }
87 exponentialRampToValueAtTime(value, time) {
88 this._param.exponentialRampToValueAtTime(value, time);
89 return this;
90 }
91 exponentialRampTo(value, rampTime, startTime) {
92 this._param.exponentialRampTo(value, rampTime, startTime);
93 return this;
94 }
95 linearRampTo(value, rampTime, startTime) {
96 this._param.linearRampTo(value, rampTime, startTime);
97 return this;
98 }
99 targetRampTo(value, rampTime, startTime) {
100 this._param.targetRampTo(value, rampTime, startTime);
101 return this;
102 }
103 exponentialApproachValueAtTime(value, time, rampTime) {
104 this._param.exponentialApproachValueAtTime(value, time, rampTime);
105 return this;
106 }
107 setTargetAtTime(value, startTime, timeConstant) {
108 this._param.setTargetAtTime(value, startTime, timeConstant);
109 return this;
110 }
111 setValueCurveAtTime(values, startTime, duration, scaling) {
112 this._param.setValueCurveAtTime(values, startTime, duration, scaling);
113 return this;
114 }
115 cancelScheduledValues(time) {
116 this._param.cancelScheduledValues(time);
117 return this;
118 }
119 cancelAndHoldAtTime(time) {
120 this._param.cancelAndHoldAtTime(time);
121 return this;
122 }
123 rampTo(value, rampTime, startTime) {
124 this._param.rampTo(value, rampTime, startTime);
125 return this;
126 }
127 get value() {
128 return this._param.value;
129 }
130 set value(value) {
131 this._param.value = value;
132 }
133 get convert() {
134 return this._param.convert;
135 }
136 set convert(convert) {
137 this._param.convert = convert;
138 }
139 get units() {
140 return this._param.units;
141 }
142 get overridden() {
143 return this._param.overridden;
144 }
145 set overridden(overridden) {
146 this._param.overridden = overridden;
147 }
148 get maxValue() {
149 return this._param.maxValue;
150 }
151 get minValue() {
152 return this._param.minValue;
153 }
154 /**
155 * @see {@link Param.apply}.
156 */
157 apply(param) {
158 this._param.apply(param);
159 return this;
160 }
161}
162/**
163 * When connecting from a signal, it's necessary to zero out the node destination
164 * node if that node is also a signal. If the destination is not 0, then the values
165 * will be summed. This method insures that the output of the destination signal will
166 * be the same as the source signal, making the destination signal a pass through node.
167 * @param signal The output signal to connect from
168 * @param destination the destination to connect to
169 * @param outputNum the optional output number
170 * @param inputNum the input number
171 */
172export function connectSignal(signal, destination, outputNum, inputNum) {
173 if (destination instanceof Param ||
174 isAudioParam(destination) ||
175 (destination instanceof Signal && destination.override)) {
176 // cancel changes
177 destination.cancelScheduledValues(0);
178 // reset the value
179 destination.setValueAtTime(0, 0);
180 // mark the value as overridden
181 if (destination instanceof Signal) {
182 destination.overridden = true;
183 }
184 }
185 connect(signal, destination, outputNum, inputNum);
186}
187//# sourceMappingURL=Signal.js.map
\No newline at end of file