Tone.js/Tone/core/context/Delay.ts

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-05-23 18:00:49 +00:00
import { Param } from "../context/Param";
2019-12-11 04:34:12 +00:00
import { Seconds, 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 {
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
2020-04-29 18:54:35 +00:00
* @example
2020-07-26 20:55:06 +00:00
* return Tone.Offline(() => {
* const delay = new Tone.Delay(0.1).toDestination();
* // connect the signal to both the delay and the destination
* const pulse = new Tone.PulseOscillator().connect(delay).toDestination();
* // start and stop the pulse
* pulse.start(0).stop(0.01);
* }, 0.5, 1);
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
/**
2019-12-11 04:34:12 +00:00
* Private holder of the max delay time
2019-04-12 14:37:47 +00:00
*/
2019-12-11 04:34:12 +00:00
private _maxDelay: Seconds;
2019-04-12 14:37:47 +00:00
/**
2019-09-14 20:39:18 +00:00
* The amount of time the incoming signal is delayed.
2020-04-29 18:54:35 +00:00
* @example
* const delay = new Tone.Delay().toDestination();
2020-05-27 01:09:32 +00:00
* // modulate the delayTime between 0.1 and 1 seconds
* const delayLFO = new Tone.LFO(0.5, 0.1, 1).start().connect(delay.delayTime);
* const pulse = new Tone.PulseOscillator().connect(delay).start();
* // the change in delayTime causes the pitch to go up and down
2019-04-12 14:37:47 +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"]);
const maxDelayInSeconds = this.toSeconds(options.maxDelay);
2019-12-11 04:34:12 +00:00
this._maxDelay = Math.max(maxDelayInSeconds, this.toSeconds(options.delayTime));
2019-04-12 14:37:47 +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,
2019-09-16 03:32:40 +00:00
param: this._delayNode.delayTime,
units: "time",
value: options.delayTime,
2019-12-11 04:34:12 +00:00
minValue: 0,
maxValue: this.maxDelay,
2019-04-12 14:37:47 +00:00
});
readOnly(this, "delayTime");
}
static getDefaults(): DelayOptions {
return Object.assign(ToneAudioNode.getDefaults(), {
2019-09-16 03:32:40 +00:00
delayTime: 0,
2019-04-12 14:37:47 +00:00
maxDelay: 1,
});
}
2019-12-11 04:34:12 +00:00
/**
* The maximum delay time. This cannot be changed after
* the value is passed into the constructor.
*/
get maxDelay(): Seconds {
return this._maxDelay;
}
2019-04-12 14:37:47 +00:00
/**
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;
}
}