Tone.js/Tone/core/context/Gain.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-05-23 18:00:49 +00:00
import { Param } from "../context/Param";
import { GainFactor, Unit, UnitName } 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
interface GainOptions extends ToneAudioNodeOptions {
gain: number;
2019-07-15 19:37:25 +00:00
units: UnitName;
2019-04-12 14:37:47 +00:00
convert: boolean;
}
/**
* A thin wrapper around the Native Web Audio GainNode.
* The GainNode is a basic building block of the Web Audio
* API and is useful for routing audio and adjusting gains.
2019-08-26 17:44:43 +00:00
* @category Core
2019-04-12 14:37:47 +00:00
*/
2019-07-15 19:37:25 +00:00
export class Gain<Type extends Unit = GainFactor> extends ToneAudioNode<GainOptions> {
2019-04-12 14:37:47 +00:00
2019-09-04 23:18:44 +00:00
readonly name: string = "Gain";
2019-04-12 14:37:47 +00:00
/**
2019-09-14 20:39:18 +00:00
* The gain parameter of the gain node.
2019-04-12 14:37:47 +00:00
*/
readonly gain: Param<Type>;
/**
* The wrapped GainNode.
*/
private _gainNode: GainNode = this.context.createGain();
// input = output
readonly input: GainNode = this._gainNode;
readonly output: GainNode = this._gainNode;
2019-08-27 17:02:31 +00:00
/**
* @param gain The initial gain of the GainNode
* @param units The units of the gain parameter.
*/
2019-04-12 14:37:47 +00:00
constructor(gain?: GainFactor, units?: Unit);
constructor(options?: Partial<GainOptions>);
constructor() {
super(optionsFromArguments(Gain.getDefaults(), arguments, ["gain", "units"]));
const options = optionsFromArguments(Gain.getDefaults(), arguments, ["gain", "units"]);
this.gain = new Param({
2019-09-16 03:32:40 +00:00
context: this.context,
convert: options.convert,
param: this._gainNode.gain,
units: options.units,
value: options.gain as Type,
2019-04-12 14:37:47 +00:00
});
readOnly(this, "gain");
}
static getDefaults(): GainOptions {
return Object.assign(ToneAudioNode.getDefaults(), {
2019-09-16 03:32:40 +00:00
convert: true,
gain: 1,
units: "gain" as UnitName,
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._gainNode.disconnect();
this.gain.dispose();
return this;
}
}