UNPKG

4.46 kBJavaScriptView Raw
1import { Effect } from "./Effect.js";
2import { Filter } from "../component/filter/Filter.js";
3import { Follower } from "../component/analysis/Follower.js";
4import { optionsFromArguments } from "../core/util/Defaults.js";
5import { Gain } from "../core/context/Gain.js";
6import { dbToGain, gainToDb } from "../core/type/Conversions.js";
7import { ScaleExp } from "../signal/ScaleExp.js";
8import { readOnly } from "../core/util/Interface.js";
9/**
10 * AutoWah connects a {@link Follower} to a {@link Filter}.
11 * The frequency of the filter, follows the input amplitude curve.
12 * Inspiration from [Tuna.js](https://github.com/Dinahmoe/tuna).
13 *
14 * @example
15 * const autoWah = new Tone.AutoWah(50, 6, -30).toDestination();
16 * // initialize the synth and connect to autowah
17 * const synth = new Tone.Synth().connect(autoWah);
18 * // Q value influences the effect of the wah - default is 2
19 * autoWah.Q.value = 6;
20 * // more audible on higher notes
21 * synth.triggerAttackRelease("C4", "8n");
22 * @category Effect
23 */
24export class AutoWah extends Effect {
25 constructor() {
26 const options = optionsFromArguments(AutoWah.getDefaults(), arguments, [
27 "baseFrequency",
28 "octaves",
29 "sensitivity",
30 ]);
31 super(options);
32 this.name = "AutoWah";
33 this._follower = new Follower({
34 context: this.context,
35 smoothing: options.follower,
36 });
37 this._sweepRange = new ScaleExp({
38 context: this.context,
39 min: 0,
40 max: 1,
41 exponent: 0.5,
42 });
43 this._baseFrequency = this.toFrequency(options.baseFrequency);
44 this._octaves = options.octaves;
45 this._inputBoost = new Gain({ context: this.context });
46 this._bandpass = new Filter({
47 context: this.context,
48 rolloff: -48,
49 frequency: 0,
50 Q: options.Q,
51 });
52 this._peaking = new Filter({
53 context: this.context,
54 type: "peaking",
55 });
56 this._peaking.gain.value = options.gain;
57 this.gain = this._peaking.gain;
58 this.Q = this._bandpass.Q;
59 // the control signal path
60 this.effectSend.chain(this._inputBoost, this._follower, this._sweepRange);
61 this._sweepRange.connect(this._bandpass.frequency);
62 this._sweepRange.connect(this._peaking.frequency);
63 // the filtered path
64 this.effectSend.chain(this._bandpass, this._peaking, this.effectReturn);
65 // set the initial value
66 this._setSweepRange();
67 this.sensitivity = options.sensitivity;
68 readOnly(this, ["gain", "Q"]);
69 }
70 static getDefaults() {
71 return Object.assign(Effect.getDefaults(), {
72 baseFrequency: 100,
73 octaves: 6,
74 sensitivity: 0,
75 Q: 2,
76 gain: 2,
77 follower: 0.2,
78 });
79 }
80 /**
81 * The number of octaves that the filter will sweep above the baseFrequency.
82 */
83 get octaves() {
84 return this._octaves;
85 }
86 set octaves(octaves) {
87 this._octaves = octaves;
88 this._setSweepRange();
89 }
90 /**
91 * The follower's smoothing time
92 */
93 get follower() {
94 return this._follower.smoothing;
95 }
96 set follower(follower) {
97 this._follower.smoothing = follower;
98 }
99 /**
100 * The base frequency from which the sweep will start from.
101 */
102 get baseFrequency() {
103 return this._baseFrequency;
104 }
105 set baseFrequency(baseFreq) {
106 this._baseFrequency = this.toFrequency(baseFreq);
107 this._setSweepRange();
108 }
109 /**
110 * The sensitivity to control how responsive to the input signal the filter is.
111 */
112 get sensitivity() {
113 return gainToDb(1 / this._inputBoost.gain.value);
114 }
115 set sensitivity(sensitivity) {
116 this._inputBoost.gain.value = 1 / dbToGain(sensitivity);
117 }
118 /**
119 * sets the sweep range of the scaler
120 */
121 _setSweepRange() {
122 this._sweepRange.min = this._baseFrequency;
123 this._sweepRange.max = Math.min(this._baseFrequency * Math.pow(2, this._octaves), this.context.sampleRate / 2);
124 }
125 dispose() {
126 super.dispose();
127 this._follower.dispose();
128 this._sweepRange.dispose();
129 this._bandpass.dispose();
130 this._peaking.dispose();
131 this._inputBoost.dispose();
132 return this;
133 }
134}
135//# sourceMappingURL=AutoWah.js.map
\No newline at end of file