UNPKG

2.52 kBJavaScriptView Raw
1import { ToneAudioNode, } from "../../core/context/ToneAudioNode.js";
2import { GreaterThan } from "../../signal/GreaterThan.js";
3import { Gain } from "../../core/context/Gain.js";
4import { Follower } from "../analysis/Follower.js";
5import { optionsFromArguments } from "../../core/util/Defaults.js";
6import { dbToGain, gainToDb } from "../../core/type/Conversions.js";
7/**
8 * Gate only passes a signal through when the incoming
9 * signal exceeds a specified threshold. It uses {@link Follower} to follow the ampltiude
10 * of the incoming signal and compares it to the {@link threshold} value using {@link GreaterThan}.
11 *
12 * @example
13 * const gate = new Tone.Gate(-30, 0.2).toDestination();
14 * const mic = new Tone.UserMedia().connect(gate);
15 * // the gate will only pass through the incoming
16 * // signal when it's louder than -30db
17 * @category Component
18 */
19export class Gate extends ToneAudioNode {
20 constructor() {
21 const options = optionsFromArguments(Gate.getDefaults(), arguments, [
22 "threshold",
23 "smoothing",
24 ]);
25 super(options);
26 this.name = "Gate";
27 this._follower = new Follower({
28 context: this.context,
29 smoothing: options.smoothing,
30 });
31 this._gt = new GreaterThan({
32 context: this.context,
33 value: dbToGain(options.threshold),
34 });
35 this.input = new Gain({ context: this.context });
36 this._gate = this.output = new Gain({ context: this.context });
37 // connections
38 this.input.connect(this._gate);
39 // the control signal
40 this.input.chain(this._follower, this._gt, this._gate.gain);
41 }
42 static getDefaults() {
43 return Object.assign(ToneAudioNode.getDefaults(), {
44 smoothing: 0.1,
45 threshold: -40,
46 });
47 }
48 /**
49 * The threshold of the gate in decibels
50 */
51 get threshold() {
52 return gainToDb(this._gt.value);
53 }
54 set threshold(thresh) {
55 this._gt.value = dbToGain(thresh);
56 }
57 /**
58 * The attack/decay speed of the gate.
59 * @see {@link Follower.smoothing}
60 */
61 get smoothing() {
62 return this._follower.smoothing;
63 }
64 set smoothing(smoothingTime) {
65 this._follower.smoothing = smoothingTime;
66 }
67 dispose() {
68 super.dispose();
69 this.input.dispose();
70 this._follower.dispose();
71 this._gt.dispose();
72 this._gate.dispose();
73 return this;
74 }
75}
76//# sourceMappingURL=Gate.js.map
\No newline at end of file