Tone.js/Tone/component/channel/Panner.ts

90 lines
2.5 KiB
TypeScript
Raw Normal View History

2019-08-21 05:50:12 +00:00
import { Param } from "../../core/context/Param";
import { ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode";
2019-10-31 19:43:16 +00:00
import { AudioRange } from "../../core/type/Units";
2019-08-21 05:50:12 +00:00
import { optionsFromArguments } from "../../core/util/Defaults";
import { readOnly } from "../../core/util/Interface";
interface TonePannerOptions extends ToneAudioNodeOptions {
pan: AudioRange;
channelCount: number;
2019-08-21 05:50:12 +00:00
}
/**
* Panner is an equal power Left/Right Panner. It is a wrapper around the StereoPannerNode.
* @example
2020-07-26 20:55:06 +00:00
* return Tone.Offline(() => {
2020-09-21 13:23:12 +00:00
* // move the input signal from right to left
* const panner = new Tone.Panner(1).toDestination();
* panner.pan.rampTo(-1, 0.5);
* const osc = new Tone.Oscillator(100).connect(panner).start();
2020-07-26 20:55:06 +00:00
* }, 0.5, 2);
2019-09-16 14:15:23 +00:00
* @category Component
2019-08-21 05:50:12 +00:00
*/
export class Panner extends ToneAudioNode<TonePannerOptions> {
2019-09-04 23:18:44 +00:00
readonly name: string = "Panner";
2019-08-21 05:50:12 +00:00
/**
2019-09-14 20:39:18 +00:00
* the panner node
2019-08-21 05:50:12 +00:00
*/
private _panner: StereoPannerNode = this.context.createStereoPanner();
readonly input: StereoPannerNode = this._panner;
readonly output: StereoPannerNode = this._panner;
/**
2019-09-14 20:39:18 +00:00
* The pan control. -1 = hard left, 1 = hard right.
* @min -1
* @max 1
* @example
2020-07-26 20:55:06 +00:00
* return Tone.Offline(() => {
* // pan hard right
* const panner = new Tone.Panner(1).toDestination();
* // pan hard left
* panner.pan.setValueAtTime(-1, 0.25);
* const osc = new Tone.Oscillator(50, "triangle").connect(panner).start();
* }, 0.5, 2);
2019-08-21 05:50:12 +00:00
*/
readonly pan: Param<"audioRange">;
2019-08-21 05:50:12 +00:00
constructor(options?: Partial<TonePannerOptions>);
2019-08-21 20:00:44 +00:00
/**
2019-08-27 17:02:31 +00:00
* @param pan The initial panner value (Defaults to 0 = "center").
2019-08-21 20:00:44 +00:00
*/
2019-08-21 05:51:08 +00:00
constructor(pan?: AudioRange);
2019-08-21 05:50:12 +00:00
constructor() {
super(Object.assign(optionsFromArguments(Panner.getDefaults(), arguments, ["pan"])));
const options = optionsFromArguments(Panner.getDefaults(), arguments, ["pan"]);
this.pan = new Param({
context: this.context,
param: this._panner.pan,
value: options.pan,
minValue: -1,
maxValue: 1,
2019-08-21 05:50:12 +00:00
});
// this is necessary for standardized-audio-context
// doesn't make any difference for the native AudioContext
// https://github.com/chrisguttandin/standardized-audio-context/issues/647
this._panner.channelCount = options.channelCount;
2019-08-21 05:50:12 +00:00
this._panner.channelCountMode = "explicit";
// initial value
readOnly(this, "pan");
}
static getDefaults(): TonePannerOptions {
return Object.assign(ToneAudioNode.getDefaults(), {
pan: 0,
channelCount: 1,
2019-08-21 05:50:12 +00:00
});
}
dispose(): this {
super.dispose();
this._panner.disconnect();
this.pan.dispose();
return this;
}
}