UNPKG

3.71 kBJavaScriptView Raw
1import { Param } from "../../core/context/Param.js";
2import { ToneAudioNode, } from "../../core/context/ToneAudioNode.js";
3import { optionsFromArguments } from "../../core/util/Defaults.js";
4import { readOnly } from "../../core/util/Interface.js";
5/**
6 * Compressor is a thin wrapper around the Web Audio
7 * [DynamicsCompressorNode](http://webaudio.github.io/web-audio-api/#the-dynamicscompressornode-interface).
8 * Compression reduces the volume of loud sounds or amplifies quiet sounds
9 * by narrowing or "compressing" an audio signal's dynamic range.
10 * Read more on [Wikipedia](https://en.wikipedia.org/wiki/Dynamic_range_compression).
11 * @example
12 * const comp = new Tone.Compressor(-30, 3);
13 * @category Component
14 */
15export class Compressor extends ToneAudioNode {
16 constructor() {
17 const options = optionsFromArguments(Compressor.getDefaults(), arguments, ["threshold", "ratio"]);
18 super(options);
19 this.name = "Compressor";
20 /**
21 * the compressor node
22 */
23 this._compressor = this.context.createDynamicsCompressor();
24 this.input = this._compressor;
25 this.output = this._compressor;
26 this.threshold = new Param({
27 minValue: this._compressor.threshold.minValue,
28 maxValue: this._compressor.threshold.maxValue,
29 context: this.context,
30 convert: false,
31 param: this._compressor.threshold,
32 units: "decibels",
33 value: options.threshold,
34 });
35 this.attack = new Param({
36 minValue: this._compressor.attack.minValue,
37 maxValue: this._compressor.attack.maxValue,
38 context: this.context,
39 param: this._compressor.attack,
40 units: "time",
41 value: options.attack,
42 });
43 this.release = new Param({
44 minValue: this._compressor.release.minValue,
45 maxValue: this._compressor.release.maxValue,
46 context: this.context,
47 param: this._compressor.release,
48 units: "time",
49 value: options.release,
50 });
51 this.knee = new Param({
52 minValue: this._compressor.knee.minValue,
53 maxValue: this._compressor.knee.maxValue,
54 context: this.context,
55 convert: false,
56 param: this._compressor.knee,
57 units: "decibels",
58 value: options.knee,
59 });
60 this.ratio = new Param({
61 minValue: this._compressor.ratio.minValue,
62 maxValue: this._compressor.ratio.maxValue,
63 context: this.context,
64 convert: false,
65 param: this._compressor.ratio,
66 units: "positive",
67 value: options.ratio,
68 });
69 // set the defaults
70 readOnly(this, ["knee", "release", "attack", "ratio", "threshold"]);
71 }
72 static getDefaults() {
73 return Object.assign(ToneAudioNode.getDefaults(), {
74 attack: 0.003,
75 knee: 30,
76 ratio: 12,
77 release: 0.25,
78 threshold: -24,
79 });
80 }
81 /**
82 * A read-only decibel value for metering purposes, representing the current amount of gain
83 * reduction that the compressor is applying to the signal. If fed no signal the value will be 0 (no gain reduction).
84 */
85 get reduction() {
86 return this._compressor.reduction;
87 }
88 dispose() {
89 super.dispose();
90 this._compressor.disconnect();
91 this.attack.dispose();
92 this.release.dispose();
93 this.threshold.dispose();
94 this.ratio.dispose();
95 this.knee.dispose();
96 return this;
97 }
98}
99//# sourceMappingURL=Compressor.js.map
\No newline at end of file