1 | import { ToneAudioNode, } from "../../core/context/ToneAudioNode.js";
|
2 | import { GreaterThan } from "../../signal/GreaterThan.js";
|
3 | import { Gain } from "../../core/context/Gain.js";
|
4 | import { Follower } from "../analysis/Follower.js";
|
5 | import { optionsFromArguments } from "../../core/util/Defaults.js";
|
6 | import { dbToGain, gainToDb } from "../../core/type/Conversions.js";
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | export 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 |
|
38 | this.input.connect(this._gate);
|
39 |
|
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 |
|
50 |
|
51 | get threshold() {
|
52 | return gainToDb(this._gt.value);
|
53 | }
|
54 | set threshold(thresh) {
|
55 | this._gt.value = dbToGain(thresh);
|
56 | }
|
57 | |
58 |
|
59 |
|
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 |
|
\ | No newline at end of file |