UNPKG

2.17 kBJavaScriptView Raw
1import { Filter } from "../component/filter/Filter.js";
2import { optionsFromArguments } from "../core/util/Defaults.js";
3import { LFOEffect } from "./LFOEffect.js";
4/**
5 * AutoFilter is a Tone.Filter with a Tone.LFO connected to the filter cutoff frequency.
6 * Setting the LFO rate and depth allows for control over the filter modulation rate
7 * and depth.
8 *
9 * @example
10 * // create an autofilter and start it's LFO
11 * const autoFilter = new Tone.AutoFilter("4n").toDestination().start();
12 * // route an oscillator through the filter and start it
13 * const oscillator = new Tone.Oscillator().connect(autoFilter).start();
14 * @category Effect
15 */
16export class AutoFilter extends LFOEffect {
17 constructor() {
18 const options = optionsFromArguments(AutoFilter.getDefaults(), arguments, ["frequency", "baseFrequency", "octaves"]);
19 super(options);
20 this.name = "AutoFilter";
21 this.filter = new Filter(Object.assign(options.filter, {
22 context: this.context,
23 }));
24 // connections
25 this.connectEffect(this.filter);
26 this._lfo.connect(this.filter.frequency);
27 this.octaves = options.octaves;
28 this.baseFrequency = options.baseFrequency;
29 }
30 static getDefaults() {
31 return Object.assign(LFOEffect.getDefaults(), {
32 baseFrequency: 200,
33 octaves: 2.6,
34 filter: {
35 type: "lowpass",
36 rolloff: -12,
37 Q: 1,
38 },
39 });
40 }
41 /**
42 * The minimum value of the filter's cutoff frequency.
43 */
44 get baseFrequency() {
45 return this._lfo.min;
46 }
47 set baseFrequency(freq) {
48 this._lfo.min = this.toFrequency(freq);
49 // and set the max
50 this.octaves = this._octaves;
51 }
52 /**
53 * The maximum value of the filter's cutoff frequency.
54 */
55 get octaves() {
56 return this._octaves;
57 }
58 set octaves(oct) {
59 this._octaves = oct;
60 this._lfo.max = this._lfo.min * Math.pow(2, oct);
61 }
62 dispose() {
63 super.dispose();
64 this.filter.dispose();
65 return this;
66 }
67}
68//# sourceMappingURL=AutoFilter.js.map
\No newline at end of file