2019-06-23 10:53:57 +00:00
|
|
|
import { Gain } from "../../core/context/Gain";
|
2019-07-11 13:57:06 +00:00
|
|
|
import { Param } from "../../core/context/Param";
|
2019-10-31 19:43:16 +00:00
|
|
|
import { ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode";
|
2019-07-30 19:35:27 +00:00
|
|
|
import { Decibels } from "../../core/type/Units";
|
2019-06-23 10:53:57 +00:00
|
|
|
import { optionsFromArguments } from "../../core/util/Defaults";
|
|
|
|
import { readOnly } from "../../core/util/Interface";
|
2019-06-19 13:53:18 +00:00
|
|
|
|
|
|
|
interface VolumeOptions extends ToneAudioNodeOptions {
|
|
|
|
volume: Decibels;
|
|
|
|
mute: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-27 17:02:31 +00:00
|
|
|
* Volume is a simple volume node, useful for creating a volume fader.
|
2019-06-19 13:53:18 +00:00
|
|
|
*
|
2019-08-27 17:02:31 +00:00
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const vol = new Tone.Volume(-12).toDestination();
|
|
|
|
* const osc = new Tone.Oscillator().connect(vol).start();
|
2019-09-16 14:15:23 +00:00
|
|
|
* @category Component
|
2019-06-19 13:53:18 +00:00
|
|
|
*/
|
|
|
|
export class Volume extends ToneAudioNode<VolumeOptions> {
|
|
|
|
|
2019-09-04 23:18:44 +00:00
|
|
|
readonly name: string = "Volume";
|
2019-06-19 13:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* the output node
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
output: Gain<"decibels">;
|
2019-06-19 13:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Input and output are the same
|
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
input: Gain<"decibels">;
|
2019-06-19 13:53:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The unmuted volume
|
|
|
|
*/
|
|
|
|
private _unmutedVolume: Decibels;
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* The volume control in decibels.
|
2019-10-23 03:39:35 +00:00
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const vol = new Tone.Volume().toDestination();
|
|
|
|
* const osc = new Tone.Oscillator().connect(vol).start();
|
2019-10-23 03:39:35 +00:00
|
|
|
* vol.volume.value = -20;
|
2019-06-19 13:53:18 +00:00
|
|
|
*/
|
2019-10-28 15:37:53 +00:00
|
|
|
volume: Param<"decibels">;
|
2019-06-19 13:53:18 +00:00
|
|
|
|
2019-08-27 17:02:31 +00:00
|
|
|
/**
|
|
|
|
* @param volume the initial volume in decibels
|
|
|
|
*/
|
|
|
|
constructor(volume?: Decibels);
|
|
|
|
constructor(options?: Partial<VolumeOptions>);
|
2019-06-19 13:53:18 +00:00
|
|
|
constructor() {
|
|
|
|
|
|
|
|
super(optionsFromArguments(Volume.getDefaults(), arguments, ["volume"]));
|
|
|
|
const options = optionsFromArguments(Volume.getDefaults(), arguments, ["volume"]);
|
|
|
|
|
2019-08-08 18:15:56 +00:00
|
|
|
this.input = this.output = new Gain({
|
|
|
|
context: this.context,
|
|
|
|
gain: options.volume,
|
|
|
|
units: "decibels",
|
|
|
|
});
|
|
|
|
this.volume = this.output.gain;
|
2019-06-19 13:53:18 +00:00
|
|
|
readOnly(this, "volume");
|
|
|
|
this._unmutedVolume = options.volume;
|
|
|
|
|
|
|
|
// set the mute initially
|
|
|
|
this.mute = options.mute;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): VolumeOptions {
|
|
|
|
return Object.assign(ToneAudioNode.getDefaults(), {
|
|
|
|
mute: false,
|
|
|
|
volume: 0,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mute the output.
|
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const vol = new Tone.Volume(-12).toDestination();
|
|
|
|
* const osc = new Tone.Oscillator().connect(vol).start();
|
2019-10-23 03:04:52 +00:00
|
|
|
* // mute the output
|
|
|
|
* vol.mute = true;
|
2019-06-19 13:53:18 +00:00
|
|
|
*/
|
|
|
|
get mute(): boolean {
|
|
|
|
return this.volume.value === -Infinity;
|
|
|
|
}
|
|
|
|
set mute(mute: boolean) {
|
|
|
|
if (!this.mute && mute) {
|
|
|
|
this._unmutedVolume = this.volume.value;
|
|
|
|
// maybe it should ramp here?
|
|
|
|
this.volume.value = -Infinity;
|
|
|
|
} else if (this.mute && !mute) {
|
|
|
|
this.volume.value = this._unmutedVolume;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-14 20:39:18 +00:00
|
|
|
* clean up
|
2019-06-19 13:53:18 +00:00
|
|
|
*/
|
|
|
|
dispose(): this {
|
2019-07-23 16:11:57 +00:00
|
|
|
super.dispose();
|
2019-06-19 13:53:18 +00:00
|
|
|
this.input.dispose();
|
|
|
|
this.volume.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|