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

75 lines
1.9 KiB
TypeScript
Raw Normal View History

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