Tone.js/Tone/source/OneShotSource.ts

222 lines
5.4 KiB
TypeScript
Raw Normal View History

2019-05-23 18:00:49 +00:00
import { Gain } from "../core/context/Gain";
import { ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
import { noOp } from "../core/util/Interface";
import { BasicPlaybackState } from "../core/util/StateTimeline";
2019-05-23 18:00:49 +00:00
export type OneShotSourceCurve = "linear" | "exponential";
export interface OneShotSourceOptions extends ToneAudioNodeOptions {
onended: () => void;
fadeIn: Time;
fadeOut: Time;
curve: OneShotSourceCurve;
}
2019-05-23 18:00:49 +00:00
export abstract class OneShotSource<Options extends ToneAudioNodeOptions> extends ToneAudioNode<Options> {
/**
* The callback to invoke after the
* source is done playing.
*/
onended: () => void = noOp;
2019-05-25 19:37:56 +00:00
/**
* Sources do not have input nodes
*/
input: undefined;
2019-05-23 18:00:49 +00:00
/**
* The start time
*/
protected _startTime: number = -1;
2019-05-23 18:00:49 +00:00
/**
* The stop time
*/
protected _stopTime: number = -1;
2019-05-23 18:00:49 +00:00
/**
* The id of the timeout
*/
private _timeout: number = -1;
/**
* The public output node
*/
2019-05-25 19:37:56 +00:00
output: Gain = new Gain({
context: this.context,
gain : 0,
});
2019-05-23 18:00:49 +00:00
/**
* The output gain node.
*/
2019-05-25 19:37:56 +00:00
protected _gainNode = this.output;
2019-05-23 18:00:49 +00:00
/**
* The fadeIn time of the amplitude envelope.
*/
protected _fadeIn: Time;
/**
* The fadeOut time of the amplitude envelope.
*/
protected _fadeOut: Time;
/**
* The curve applied to the fades, either "linear" or "exponential"
*/
protected _curve: OneShotSourceCurve;
constructor(options: OneShotSourceOptions) {
super(options);
this._fadeIn = options.fadeIn;
this._fadeOut = options.fadeOut;
this._curve = options.curve;
}
static getDefaults(): OneShotSourceOptions {
return Object.assign(ToneAudioNode.getDefaults(), {
curve: "linear" as OneShotSourceCurve,
fadeIn : 0,
fadeOut: 0,
onended : noOp,
});
}
/**
* Stop the source node
*/
2019-05-25 19:37:56 +00:00
protected abstract _stopSource(time: Seconds): void;
/**
* Start the source node at the given time
* @param time When to start the node
*/
protected abstract start(time?: Time): this;
2019-05-23 18:00:49 +00:00
/**
* Start the source at the given time
* @param time When to start the source
*/
protected _startGain(time: Seconds, gain: GainFactor = 1): this {
2019-05-23 18:00:49 +00:00
this.assert(this._startTime === -1, "Source cannot be started more than once");
// apply a fade in envelope
const fadeInTime = this.toSeconds(this._fadeIn);
// record the start time
this._startTime = time + fadeInTime;
2019-05-23 18:00:49 +00:00
this._startTime = Math.max(this._startTime, this.context.currentTime);
// schedule the envelope
if (fadeInTime > 0) {
this._gainNode.gain.setValueAtTime(0, time);
if (this._curve === "linear") {
this._gainNode.gain.linearRampToValueAtTime(gain, time + fadeInTime);
} else {
this._gainNode.gain.exponentialApproachValueAtTime(gain, time, fadeInTime);
}
} else {
this._gainNode.gain.setValueAtTime(gain, time);
}
2019-05-23 18:00:49 +00:00
return this;
}
2019-05-25 19:37:56 +00:00
/**
* Stop the source node at the given time.
* @param time When to stop the source
*/
stop(time?: Time): this {
this._stopGain(this.toSeconds(time));
2019-05-25 19:37:56 +00:00
return this;
}
2019-05-23 18:00:49 +00:00
/**
* Stop the source at the given time
* @param time When to stop the source
*/
protected _stopGain(time: Seconds): this {
2019-05-23 18:00:49 +00:00
this.assert(this._startTime !== -1, "'start' must be called before 'stop'");
// cancel the previous stop
this.cancelStop();
// the fadeOut time
const fadeOutTime = this.toSeconds(this._fadeOut);
// schedule the stop callback
this._stopTime = this.toSeconds(time) + fadeOutTime;
2019-05-23 18:00:49 +00:00
this._stopTime = Math.max(this._stopTime, this.context.currentTime);
if (fadeOutTime > 0) {
// start the fade out curve at the given time
if (this._curve === "linear") {
this._gainNode.gain.linearRampTo(0, fadeOutTime, time);
} else {
this._gainNode.gain.targetRampTo(0, fadeOutTime, time);
}
2019-05-23 18:00:49 +00:00
} else {
// stop any ongoing ramps, and set the value to 0
this._gainNode.gain.cancelAndHoldAtTime(time);
this._gainNode.gain.setValueAtTime(0, time);
2019-05-23 18:00:49 +00:00
}
this.context.clearTimeout(this._timeout);
this._timeout = this.context.setTimeout(() => {
// allow additional time for the exponential curve to fully decay
2019-07-24 23:30:31 +00:00
const additionalTail = this._curve === "exponential" ? fadeOutTime * 2 : 0;
this._stopSource(this.now() + additionalTail);
this._onended();
}, this._stopTime - this.context.currentTime);
2019-05-23 18:00:49 +00:00
return this;
}
/**
* Invoke the onended callback
*/
protected _onended(): void {
if (this.onended !== noOp) {
this.onended();
// overwrite onended to make sure it only is called once
this.onended = noOp;
// dispose when it's ended to free up for garbage collection
setTimeout(() => this.dispose(), 1000);
}
}
2019-05-23 18:00:49 +00:00
/**
* Get the playback state at the given time
*/
getStateAtTime = function(time: Time): BasicPlaybackState {
2019-05-23 18:00:49 +00:00
const computedTime = this.toSeconds(time);
if (this._startTime !== -1 && computedTime >= this._startTime &&
(this._stopTime === -1 || computedTime <= this._stopTime)) {
return "started";
} else {
return "stopped";
}
};
/**
* Get the playback state at the current time
*/
get state(): BasicPlaybackState {
2019-05-23 18:00:49 +00:00
return this.getStateAtTime(this.now());
}
/**
* Cancel a scheduled stop event
*/
2019-06-19 19:52:47 +00:00
cancelStop(): this {
2019-05-23 18:00:49 +00:00
this.assert(this._startTime !== -1, "Source is not started");
// cancel the stop envelope
this._gainNode.gain.cancelScheduledValues(this._startTime + this.sampleTime);
this.context.clearTimeout(this._timeout);
this._stopTime = -1;
return this;
}
dispose(): this {
super.dispose();
this._gainNode.disconnect();
return this;
}
2019-05-23 18:00:49 +00:00
}