/
Limiter.ts
81 lines (69 loc) · 2.34 KB
/
Limiter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { InputNode, OutputNode, ToneAudioNode, ToneAudioNodeOptions } from "../../core/context/ToneAudioNode";
import { Decibels } from "../../core/type/Units";
import { optionsFromArguments } from "../../core/util/Defaults";
import { Compressor } from "./Compressor";
import { Param } from "../../core/context/Param";
import { readOnly } from "../../core/util/Interface";
export interface LimiterOptions extends ToneAudioNodeOptions {
threshold: Decibels;
};
/**
* Limiter will limit the loudness of an incoming signal.
* It is composed of a [[Compressor]] with a fast attack
* and release and max ratio. Limiters are commonly used to safeguard against
* signal clipping. Unlike a compressor, limiters do not provide
* smooth gain reduction and almost completely prevent
* additional gain above the threshold.
*
* @example
* const limiter = new Tone.Limiter(-20).toDestination();
* const oscillator = new Tone.Oscillator().connect(limiter);
* oscillator.start();
* @category Component
*/
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,
attack: 0,
release: 0,
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;
}
}