2019-05-23 18:00:49 +00:00
|
|
|
import { Param } from "../context/Param";
|
2019-07-30 19:35:27 +00:00
|
|
|
import { Time } from "../type/Units";
|
2019-05-23 18:00:49 +00:00
|
|
|
import { optionsFromArguments } from "../util/Defaults";
|
|
|
|
import { readOnly } from "../util/Interface";
|
|
|
|
import { ToneAudioNode, ToneAudioNodeOptions } from "./ToneAudioNode";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
export interface DelayOptions extends ToneAudioNodeOptions {
|
2019-07-25 14:44:32 +00:00
|
|
|
delayTime: Time;
|
|
|
|
maxDelay: Time;
|
2019-04-12 14:37:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 17:02:31 +00:00
|
|
|
* Wrapper around Web Audio's native [DelayNode](http://webaudio.github.io/web-audio-api/#the-delaynode-interface).
|
2019-08-26 17:44:43 +00:00
|
|
|
* @category Core
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
|
|
|
export class Delay extends ToneAudioNode<DelayOptions> {
|
|
|
|
|
2019-09-04 23:18:44 +00:00
|
|
|
readonly name: string = "Delay";
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum delay time. This cannot be changed after
|
|
|
|
* the value is passed into the constructor.
|
|
|
|
*/
|
|
|
|
readonly maxDelay: Time;
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The amount of time the incoming signal is delayed.
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
2019-07-15 19:37:25 +00:00
|
|
|
readonly delayTime: Param<Time>;
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Private reference to the internal DelayNode
|
|
|
|
*/
|
|
|
|
private _delayNode: DelayNode;
|
|
|
|
readonly input: DelayNode;
|
|
|
|
readonly output: DelayNode;
|
|
|
|
|
2019-08-27 17:02:31 +00:00
|
|
|
/**
|
|
|
|
* @param delayTime The delay applied to the incoming signal.
|
|
|
|
* @param maxDelay The maximum delay time.
|
|
|
|
*/
|
|
|
|
constructor(delayTime?: Time, maxDelay?: Time);
|
|
|
|
constructor(options?: Partial<DelayOptions>);
|
2019-04-12 14:37:47 +00:00
|
|
|
constructor() {
|
|
|
|
super(optionsFromArguments(Delay.getDefaults(), arguments, ["delayTime", "maxDelay"]));
|
|
|
|
|
|
|
|
const options = optionsFromArguments(Delay.getDefaults(), arguments, ["delayTime", "maxDelay"]);
|
|
|
|
|
2019-07-25 14:44:32 +00:00
|
|
|
const maxDelayInSeconds = this.toSeconds(options.maxDelay);
|
|
|
|
this.maxDelay = Math.max(maxDelayInSeconds, this.toSeconds(options.delayTime));
|
2019-04-12 14:37:47 +00:00
|
|
|
|
2019-07-25 14:44:32 +00:00
|
|
|
this._delayNode = this.input = this.output = this.context.createDelay(maxDelayInSeconds);
|
2019-04-12 14:37:47 +00:00
|
|
|
|
|
|
|
this.delayTime = new Param({
|
|
|
|
context: this.context,
|
|
|
|
param : this._delayNode.delayTime,
|
|
|
|
units : "time",
|
|
|
|
value : options.delayTime,
|
|
|
|
});
|
|
|
|
|
|
|
|
readOnly(this, "delayTime");
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): DelayOptions {
|
|
|
|
return Object.assign(ToneAudioNode.getDefaults(), {
|
|
|
|
delayTime : 0,
|
|
|
|
maxDelay: 1,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* Clean up.
|
2019-04-12 14:37:47 +00:00
|
|
|
*/
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this._delayNode.disconnect();
|
|
|
|
this.delayTime.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|