2019-11-14 16:23:22 +00:00
|
|
|
import { InputNode, OutputNode, ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode";
|
|
|
|
import { Decibels } from "../../core/type/Units";
|
|
|
|
import { optionsFromArguments } from "../../core/util/Defaults";
|
2019-11-14 16:23:00 +00:00
|
|
|
import { Compressor } from "./Compressor";
|
2019-11-14 16:23:22 +00:00
|
|
|
import { Param } from "../../core/context/Param";
|
|
|
|
import { readOnly } from "../../core/util/Interface";
|
2019-11-14 16:23:00 +00:00
|
|
|
|
|
|
|
export interface LimiterOptions extends ToneAudioNodeOptions {
|
|
|
|
threshold: Decibels;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Limiter will limit the loudness of an incoming signal.
|
2020-10-25 17:06:08 +00:00
|
|
|
* Under the hood it's composed of a [[Compressor]] with a fast attack
|
|
|
|
* and release and max compression ratio.
|
2019-11-14 16:23:00 +00:00
|
|
|
*
|
|
|
|
* @example
|
2020-04-17 02:24:18 +00:00
|
|
|
* const limiter = new Tone.Limiter(-20).toDestination();
|
|
|
|
* const oscillator = new Tone.Oscillator().connect(limiter);
|
2019-11-14 16:23:00 +00:00
|
|
|
* oscillator.start();
|
2020-09-02 20:53:38 +00:00
|
|
|
* @category Component
|
2019-11-14 16:23:00 +00:00
|
|
|
*/
|
|
|
|
export class Limiter extends ToneAudioNode<LimiterOptions> {
|
|
|
|
|
|
|
|
readonly name: string = "Limiter";
|
|
|
|
|
|
|
|
readonly input: InputNode;
|
|
|
|
readonly output: OutputNode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The compressor which does the limiting
|
|
|
|
*/
|
|
|
|
private _compressor: Compressor;
|
|
|
|
|
|
|
|
readonly threshold: Param<"decibels">
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param threshold The threshold above which the gain reduction is applied.
|
|
|
|
*/
|
|
|
|
constructor(threshold?: Decibels);
|
|
|
|
constructor(options?: Partial<LimiterOptions>);
|
|
|
|
constructor() {
|
|
|
|
super(Object.assign(optionsFromArguments(Limiter.getDefaults(), arguments, ["threshold"])));
|
|
|
|
const options = optionsFromArguments(Limiter.getDefaults(), arguments, ["threshold"]);
|
|
|
|
|
|
|
|
this._compressor = this.input = this.output = new Compressor({
|
|
|
|
context: this.context,
|
|
|
|
ratio: 20,
|
2020-10-25 17:06:08 +00:00
|
|
|
attack: 0.003,
|
|
|
|
release: 0.01,
|
2019-11-14 16:23:00 +00:00
|
|
|
threshold: options.threshold
|
|
|
|
});
|
|
|
|
|
|
|
|
this.threshold = this._compressor.threshold;
|
|
|
|
readOnly(this, "threshold");
|
|
|
|
}
|
|
|
|
|
|
|
|
static getDefaults(): LimiterOptions {
|
|
|
|
return Object.assign(ToneAudioNode.getDefaults(), {
|
|
|
|
threshold: -12
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A read-only decibel value for metering purposes, representing the current amount of gain
|
|
|
|
* reduction that the compressor is applying to the signal.
|
|
|
|
*/
|
|
|
|
get reduction(): Decibels {
|
|
|
|
return this._compressor.reduction;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispose(): this {
|
|
|
|
super.dispose();
|
|
|
|
this._compressor.dispose();
|
|
|
|
this.threshold.dispose();
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|