Tone.js/Tone/component/channel/Volume.ts

104 lines
2.2 KiB
TypeScript
Raw Normal View History

import { Gain } from "../../core/context/Gain";
2019-07-11 13:57:06 +00:00
import { Param } from "../../core/context/Param";
import { InputNode, ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode";
import { Decibels } from "../../core/type/Units";
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
2019-06-19 13:53:18 +00:00
* var vol = new Volume(-12);
2019-09-16 14:15:23 +00:00
* instrument.chain(vol, Tone.Destination);
* @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
*/
output: Gain<Decibels>;
2019-06-19 13:53:18 +00:00
/**
* Input and output are the same
*/
input: Gain;
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-06-19 13:53:18 +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"]);
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
* //mute the output
* volume.mute = true;
*/
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 {
super.dispose();
2019-06-19 13:53:18 +00:00
this.input.dispose();
this.volume.dispose();
return this;
}
}