2019-07-11 13:57:06 +00:00
|
|
|
import { Volume } from "../component/channel/Volume";
|
2019-08-14 14:44:27 +00:00
|
|
|
import "../core/context/Destination";
|
2019-08-08 18:15:56 +00:00
|
|
|
import { Param } from "../core/context/Param";
|
|
|
|
import { OutputNode, ToneAudioNode, ToneAudioNodeOptions } from "../core/context/ToneAudioNode";
|
2019-07-30 18:51:07 +00:00
|
|
|
import { Decibels, Seconds, Time } from "../core/type/Units";
|
2019-08-10 03:00:51 +00:00
|
|
|
import { defaultArg } from "../core/util/Defaults";
|
2019-07-11 13:57:06 +00:00
|
|
|
import { noOp, readOnly } from "../core/util/Interface";
|
2019-07-23 17:43:35 +00:00
|
|
|
import { BasicPlaybackState, StateTimeline } from "../core/util/StateTimeline";
|
2019-07-11 13:57:06 +00:00
|
|
|
import { isUndef } from "../core/util/TypeCheck";
|
2019-06-19 14:18:06 +00:00
|
|
|
|
2019-08-10 15:51:35 +00:00
|
|
|
type onStopCallback = (source: Source<any>) => void;
|
|
|
|
|
2019-06-19 14:18:06 +00:00
|
|
|
export interface SourceOptions extends ToneAudioNodeOptions {
|
|
|
|
volume: Decibels;
|
|
|
|
mute: boolean;
|
2019-08-10 15:51:35 +00:00
|
|
|
onstop: onStopCallback;
|
2019-06-19 14:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 15:47:52 +00:00
|
|
|
* Base class for sources. Sources have start/stop methods
|
|
|
|
* and the ability to be synced to the
|
|
|
|
* start/stop of this.context.transport.
|
2019-06-19 14:18:06 +00:00
|
|
|
*
|
2019-08-27 15:47:52 +00:00
|
|
|
* @example
|
2019-06-19 14:18:06 +00:00
|
|
|
* //Multiple state change events can be chained together,
|
|
|
|
* //but must be set in the correct order and with ascending times
|
|
|
|
*
|
|
|
|
* // OK
|
|
|
|
* state.start().stop("+0.2");
|
|
|
|
* // AND
|
|
|
|
* state.start().stop("+0.2").start("+0.4").stop("+0.7")
|
|
|
|
*
|
|
|
|
* // BAD
|
|
|
|
* state.stop("+0.2").start();
|
|
|
|
* // OR
|
|
|
|
* state.start("+0.3").stop("+0.2");
|
|
|
|
*
|
|
|
|
*/
|
2019-06-19 19:53:00 +00:00
|
|
|
export abstract class Source<Options extends SourceOptions> extends ToneAudioNode<Options> {
|
2019-06-19 14:18:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The output volume node
|
|
|
|
*/
|
2019-08-08 18:15:56 +00:00
|
|
|
private _volume: Volume;
|
2019-06-19 14:18:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The output note
|
|
|
|
*/
|
2019-08-08 18:15:56 +00:00
|
|
|
output: OutputNode;
|
2019-06-19 14:18:06 +00:00
|
|
|
|
|
|
|
/**
|
2019-08-08 18:15:56 +00:00
|
|
|
* Sources have no inputs
|
2019-06-19 14:18:06 +00:00
|
|
|
*/
|
|
|
|
input = undefined;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The volume of the output in decibels.
|
|
|
|
* @example
|
|
|
|
* source.volume.value = -6;
|
|
|
|
*/
|
2019-08-08 18:15:56 +00:00
|
|
|
volume: Param<Decibels>;
|
2019-06-19 14:18:06 +00:00
|
|
|
|
2019-08-10 15:51:35 +00:00
|
|
|
/**
|
|
|
|
* The callback to invoke when the source is stopped.
|
|
|
|
*/
|
|
|
|
onstop: onStopCallback;
|
|
|
|
|
2019-06-19 14:18:06 +00:00
|
|
|
/**
|
|
|
|
* Keep track of the scheduled state.
|
|
|
|
*/
|
2019-07-23 17:43:35 +00:00
|
|
|
protected _state: StateTimeline<{
|
|
|
|
duration?: Seconds;
|
|
|
|
offset?: Seconds;
|
|
|
|
/**
|
|
|
|
* Either the buffer is explicitly scheduled to end using the stop method,
|
|
|
|
* or it's implicitly ended when the buffer is over.
|
|
|
|
*/
|
|
|
|
implicitEnd?: boolean;
|
|
|
|
}> = new StateTimeline("stopped");
|
2019-06-19 14:18:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The synced `start` callback function from the transport
|
|
|
|
* @type {Function}
|
|
|
|
* @private
|
|
|
|
*/
|
2019-07-23 15:27:43 +00:00
|
|
|
protected _synced = false;
|
2019-06-19 14:18:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Keep track of all of the scheduled event ids
|
|
|
|
*/
|
|
|
|
private _scheduled: number[] = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Placeholder functions for syncing/unsyncing to transport
|
|
|
|
*/
|
2019-06-23 19:03:54 +00:00
|
|
|
private _syncedStart: (time: Seconds, offset: Seconds) => void = noOp;
|
|
|
|
private _syncedStop: (time: Seconds) => void = noOp;
|
2019-06-19 14:18:06 +00:00
|
|
|
|
2019-07-25 03:33:31 +00:00
|
|
|
constructor(options: SourceOptions) {
|
|
|
|
super(options);
|
2019-06-19 14:18:06 +00:00
|
|
|
this._state.memory = 100;
|
|
|
|
|
2019-08-08 18:15:56 +00:00
|
|
|
this._volume = this.output = new Volume({
|
|
|
|
context: this.context,
|
|
|
|
mute: options.mute,
|
|
|
|
volume: options.volume,
|
|
|
|
});
|
|
|
|
this.volume = this._volume.volume;
|
|
|
|
readOnly(this, "volume");
|
2019-08-10 15:51:35 +00:00
|
|
|
this.onstop = options.onstop;
|
2019-06-19 14:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): SourceOptions {
|
|
|
|
return Object.assign(ToneAudioNode.getDefaults(), {
|
|
|
|
mute: false,
|
2019-08-10 15:51:35 +00:00
|
|
|
onstop: noOp,
|
2019-06-19 14:18:06 +00:00
|
|
|
volume: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the playback state of the source, either "started" or "stopped".
|
|
|
|
*/
|
2019-07-23 17:43:35 +00:00
|
|
|
get state(): BasicPlaybackState {
|
2019-06-23 19:03:54 +00:00
|
|
|
if (this._synced) {
|
2019-06-19 14:18:06 +00:00
|
|
|
if (this.context.transport.state === "started") {
|
2019-07-23 17:43:35 +00:00
|
|
|
return this._state.getValueAtTime(this.context.transport.seconds) as BasicPlaybackState;
|
2019-06-19 14:18:06 +00:00
|
|
|
} else {
|
|
|
|
return "stopped";
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-23 17:43:35 +00:00
|
|
|
return this._state.getValueAtTime(this.now()) as BasicPlaybackState;
|
2019-06-19 14:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mute the output.
|
|
|
|
* @example
|
|
|
|
* //mute the output
|
|
|
|
* source.mute = true;
|
|
|
|
*/
|
|
|
|
get mute(): boolean {
|
|
|
|
return this._volume.mute;
|
|
|
|
}
|
|
|
|
set mute(mute: boolean) {
|
|
|
|
this._volume.mute = mute;
|
|
|
|
}
|
|
|
|
|
|
|
|
// overwrite these functions
|
|
|
|
protected abstract _start(time: Time, offset?: Time, duration?: Time): void;
|
|
|
|
protected abstract _stop(time: Time): void;
|
|
|
|
abstract restart(time: Time, offset?: Time, duration?: Time): this;
|
|
|
|
|
2019-08-14 15:52:20 +00:00
|
|
|
/**
|
|
|
|
* Ensure that the scheduled time is not before the current time.
|
|
|
|
* Should only be used when scheduled unsynced.
|
|
|
|
*/
|
|
|
|
private _clampToCurrentTime(time: Seconds): Seconds {
|
|
|
|
if (this._synced) {
|
|
|
|
return time;
|
|
|
|
} else {
|
|
|
|
return Math.max(time, this.context.currentTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 14:18:06 +00:00
|
|
|
/**
|
|
|
|
* Start the source at the specified time. If no time is given,
|
|
|
|
* start the source now.
|
2019-07-23 15:27:43 +00:00
|
|
|
* @param time When the source should be started.
|
2019-06-19 14:18:06 +00:00
|
|
|
* @example
|
|
|
|
* source.start("+0.5"); //starts the source 0.5 seconds from now
|
|
|
|
*/
|
2019-07-30 18:51:07 +00:00
|
|
|
start(time?: Time, offset?: Time, duration?: Time): this {
|
2019-08-14 15:52:20 +00:00
|
|
|
let computedTime = isUndef(time) && this._synced ? this.context.transport.seconds : this.toSeconds(time);
|
|
|
|
computedTime = this._clampToCurrentTime(computedTime);
|
2019-08-13 23:35:40 +00:00
|
|
|
this.log("start", computedTime);
|
2019-06-19 14:18:06 +00:00
|
|
|
// if it's started, stop it and restart it
|
2019-07-30 18:51:07 +00:00
|
|
|
if (this._state.getValueAtTime(computedTime) === "started") {
|
|
|
|
this._state.cancel(computedTime);
|
|
|
|
this._state.setStateAtTime("started", computedTime);
|
|
|
|
this.restart(computedTime, offset, duration);
|
2019-06-19 14:18:06 +00:00
|
|
|
} else {
|
2019-07-30 18:51:07 +00:00
|
|
|
this._state.setStateAtTime("started", computedTime);
|
2019-06-23 19:03:54 +00:00
|
|
|
if (this._synced) {
|
2019-06-19 14:18:06 +00:00
|
|
|
// add the offset time to the event
|
2019-07-30 18:51:07 +00:00
|
|
|
const event = this._state.get(computedTime);
|
2019-06-19 14:18:06 +00:00
|
|
|
if (event) {
|
|
|
|
event.offset = this.toSeconds(defaultArg(offset, 0));
|
2019-07-30 19:53:19 +00:00
|
|
|
event.duration = duration ? this.toSeconds(duration) : undefined;
|
2019-06-19 14:18:06 +00:00
|
|
|
}
|
|
|
|
const sched = this.context.transport.schedule(t => {
|
|
|
|
this._start(t, offset, duration);
|
2019-07-30 18:51:07 +00:00
|
|
|
}, computedTime);
|
2019-06-19 14:18:06 +00:00
|
|
|
this._scheduled.push(sched);
|
|
|
|
|
|
|
|
// if it's already started
|
|
|
|
if (this.context.transport.state === "started") {
|
|
|
|
this._syncedStart(this.now(), this.context.transport.seconds);
|
|
|
|
}
|
|
|
|
} else {
|
2019-08-14 14:44:27 +00:00
|
|
|
this._start(computedTime, offset, duration);
|
2019-06-19 14:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop the source at the specified time. If no time is given,
|
|
|
|
* stop the source now.
|
2019-08-03 01:49:38 +00:00
|
|
|
* @param time When the source should be stopped.
|
2019-06-19 14:18:06 +00:00
|
|
|
* @example
|
|
|
|
* source.stop(); // stops the source immediately
|
|
|
|
*/
|
2019-06-24 17:41:38 +00:00
|
|
|
stop(time?: Time): this {
|
2019-08-14 15:52:20 +00:00
|
|
|
let computedTime = isUndef(time) && this._synced ? this.context.transport.seconds : this.toSeconds(time);
|
|
|
|
computedTime = this._clampToCurrentTime(computedTime);
|
2019-08-13 23:35:40 +00:00
|
|
|
this.log("stop", computedTime);
|
2019-06-19 14:18:06 +00:00
|
|
|
if (!this._synced) {
|
2019-08-14 14:44:27 +00:00
|
|
|
this._stop(computedTime);
|
2019-06-23 19:03:54 +00:00
|
|
|
} else {
|
2019-07-30 18:51:07 +00:00
|
|
|
const sched = this.context.transport.schedule(this._stop.bind(this), computedTime);
|
2019-06-19 14:18:06 +00:00
|
|
|
this._scheduled.push(sched);
|
|
|
|
}
|
2019-07-30 18:51:07 +00:00
|
|
|
this._state.cancel(computedTime);
|
|
|
|
this._state.setStateAtTime("stopped", computedTime);
|
2019-06-19 14:18:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sync the source to the Transport so that all subsequent
|
|
|
|
* calls to `start` and `stop` are synced to the TransportTime
|
|
|
|
* instead of the AudioContext time.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* //sync the source so that it plays between 0 and 0.3 on the Transport's timeline
|
|
|
|
* source.sync().start(0).stop(0.3);
|
|
|
|
* //start the transport.
|
|
|
|
* this.context.transport.start();
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* //start the transport with an offset and the sync'ed sources
|
|
|
|
* //will start in the correct position
|
|
|
|
* source.sync().start(0.1);
|
|
|
|
* //the source will be invoked with an offset of 0.4 = (0.5 - 0.1)
|
|
|
|
* this.context.transport.start("+0.5", 0.5);
|
|
|
|
*/
|
|
|
|
sync(): this {
|
2019-06-23 19:03:54 +00:00
|
|
|
if (!this._synced) {
|
2019-06-19 14:18:06 +00:00
|
|
|
this._synced = true;
|
|
|
|
this._syncedStart = (time, offset) => {
|
|
|
|
if (offset > 0) {
|
|
|
|
// get the playback state at that time
|
|
|
|
const stateEvent = this._state.get(offset);
|
|
|
|
// listen for start events which may occur in the middle of the sync'ed time
|
|
|
|
if (stateEvent && stateEvent.state === "started" && stateEvent.time !== offset) {
|
|
|
|
// get the offset
|
|
|
|
const startOffset = offset - this.toSeconds(stateEvent.time);
|
|
|
|
let duration;
|
|
|
|
if (stateEvent.duration) {
|
|
|
|
duration = this.toSeconds(stateEvent.duration) - startOffset;
|
|
|
|
}
|
|
|
|
this._start(time, this.toSeconds(stateEvent.offset) + startOffset, duration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._syncedStop = time => {
|
2019-06-23 19:03:54 +00:00
|
|
|
const seconds = this.context.transport.getSecondsAtTime(Math.max(time - this.sampleTime, 0));
|
|
|
|
if (this._state.getValueAtTime(seconds) === "started") {
|
|
|
|
this._stop(time);
|
2019-06-19 14:18:06 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
this.context.transport.on("start", this._syncedStart);
|
|
|
|
this.context.transport.on("loopStart", this._syncedStart);
|
|
|
|
this.context.transport.on("stop", this._syncedStop);
|
|
|
|
this.context.transport.on("pause", this._syncedStop);
|
|
|
|
this.context.transport.on("loopEnd", this._syncedStop);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unsync the source to the Transport. See Source.sync
|
|
|
|
*/
|
|
|
|
unsync(): this {
|
2019-06-24 17:41:38 +00:00
|
|
|
if (this._synced) {
|
2019-06-23 19:03:54 +00:00
|
|
|
this.context.transport.off("stop", this._syncedStop);
|
|
|
|
this.context.transport.off("pause", this._syncedStop);
|
|
|
|
this.context.transport.off("loopEnd", this._syncedStop);
|
|
|
|
this.context.transport.off("start", this._syncedStart);
|
|
|
|
this.context.transport.off("loopStart", this._syncedStart);
|
|
|
|
}
|
|
|
|
this._synced = false;
|
|
|
|
// clear all of the scheduled ids
|
2019-06-24 17:41:38 +00:00
|
|
|
this._scheduled.forEach(id => this.context.transport.clear(id));
|
2019-06-23 19:03:54 +00:00
|
|
|
this._scheduled = [];
|
|
|
|
this._state.cancel(0);
|
2019-06-19 14:18:06 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean up.
|
|
|
|
*/
|
|
|
|
dispose(): this {
|
2019-07-23 16:11:57 +00:00
|
|
|
super.dispose();
|
2019-08-10 15:51:35 +00:00
|
|
|
this.onstop = noOp;
|
2019-06-19 14:18:06 +00:00
|
|
|
this.unsync();
|
|
|
|
this._volume.dispose();
|
|
|
|
this._state.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|