2
0
Fork 0
mirror of https://github.com/Tonejs/Tone.js synced 2025-02-15 12:08:29 +00:00
Tone.js/Tone/core/context/Gain.ts

75 lines
1.8 KiB
TypeScript
Raw Normal View History

2019-05-23 14:00:49 -04:00
import { Param } from "../context/Param";
import { GainFactor, Unit, UnitName } from "../type/Units";
2019-05-23 14:00:49 -04:00
import { optionsFromArguments } from "../util/Defaults";
import { readOnly } from "../util/Interface";
import { ToneAudioNode, ToneAudioNodeOptions } from "./ToneAudioNode";
2019-04-12 10:37:47 -04:00
interface GainOptions extends ToneAudioNodeOptions {
gain: number;
2019-07-15 15:37:25 -04:00
units: UnitName;
2019-04-12 10:37:47 -04: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 10:44:43 -07:00
* @category Core
2019-04-12 10:37:47 -04:00
*/
2019-07-15 15:37:25 -04:00
export class Gain<Type extends Unit = GainFactor> extends ToneAudioNode<GainOptions> {
2019-04-12 10:37:47 -04:00
2019-09-04 19:18:44 -04:00
readonly name: string = "Gain";
2019-04-12 10:37:47 -04:00
/**
2019-09-14 16:39:18 -04:00
* The gain parameter of the gain node.
2019-04-12 10:37:47 -04: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 10:02:31 -07:00
/**
* @param gain The initial gain of the GainNode
* @param units The units of the gain parameter.
*/
2019-04-12 10:37:47 -04: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-15 23:32:40 -04:00
context: this.context,
convert: options.convert,
param: this._gainNode.gain,
units: options.units,
value: options.gain as Type,
2019-04-12 10:37:47 -04:00
});
readOnly(this, "gain");
}
static getDefaults(): GainOptions {
return Object.assign(ToneAudioNode.getDefaults(), {
2019-09-15 23:32:40 -04:00
convert: true,
gain: 1,
units: "gain" as UnitName,
2019-04-12 10:37:47 -04:00
});
}
/**
2019-09-14 16:39:18 -04:00
* Clean up.
2019-04-12 10:37:47 -04:00
*/
dispose(): this {
super.dispose();
this._gainNode.disconnect();
this.gain.dispose();
return this;
}
}