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

75 lines
1.9 KiB
TypeScript
Raw Normal View History

2019-05-23 18:00:49 +00:00
import { Param } from "../context/Param";
2019-10-28 15:38:17 +00:00
import { UnitMap, 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<TypeName extends UnitName> extends ToneAudioNodeOptions {
gain: UnitMap[TypeName];
units: TypeName;
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
*/
export class Gain<TypeName extends "gain" | "decibels" | "normalRange" = "gain"> extends ToneAudioNode<GainOptions<TypeName>> {
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<TypeName>;
2019-04-12 14:37:47 +00:00
/**
* 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.
*/
constructor(gain?: UnitMap[TypeName], units?: TypeName);
constructor(options?: Partial<GainOptions<TypeName>>);
2019-04-12 14:37:47 +00:00
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,
2019-04-12 14:37:47 +00:00
});
readOnly(this, "gain");
}
static getDefaults(): GainOptions<any> {
2019-04-12 14:37:47 +00:00
return Object.assign(ToneAudioNode.getDefaults(), {
2019-09-16 03:32:40 +00:00
convert: true,
gain: 1,
units: "gain",
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;
}
}