-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrequencyBand.js
More file actions
47 lines (39 loc) · 1.41 KB
/
frequencyBand.js
File metadata and controls
47 lines (39 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
export class FrequencyBand {
constructor (context, type, order = 1) {
if (!context) {
throw new Error("FrequencyBand: context must be a valid audio context");
} // if
this.context = context;
this.input = context.createGain();
this.output = context.createGain();
this.order = order;
this.type = type;
this._low = [];
this._high = [];
if (type === "peaking") {
this._low = createFilters(context, order, "highpass", this.input, this.output);
this._high = createFilters(context, order, "lowpass", this.input, this.output);
} else if (type === "lowshelf") {
this._low = createFilters(context, order, "lowpass", this.input, this.output);
} else if(type === "highshelf") {
this._high = createFilters(context, order, "highpass", this.input, this.output);
} // if
} // constructor
set low (frequency) {this.setFrequency(this._low, frequency);}
set high (frequency) {this.setFrequency(this._high, frequency);}
set gain (gain) {this.output.gain.value = gain;}
setFrequency(filters, frequency) {filters.forEach(f => f.frequency.value = Number(frequency));}
} // class FrequencyBand
function createFilters (context, count, type, input, output) {
const filters = [];
for (let i=0; i<count; i++) {
filters[i] = context.createBiquadFilter();
filters[i].type = type;
} // for
for (let i=0; i<count-1; i++) {
filters[i].connect(filters[i+1]);
} // for
input.connect(filters[0]);
filters[count-1].connect(output);
return filters;
} // createFilters