-
Notifications
You must be signed in to change notification settings - Fork 7
/
3BandEqualizer.js
84 lines (69 loc) · 1.92 KB
/
3BandEqualizer.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
function 3BandEqualizer(audioSource, context) {
var gain_node = context.createGain();
gain_node.gain.value = 1;
var lowpass = createFilter("lowshelf", 100, 1, 0);
var highpass = createFilter("highshelf", 4000, 0, 0);
var mid = createFilter("peaking", 2000, 2, 0);
lowpass.connect(mid);
mid.connect(highpass);
highpass.connect(gain_node);
function createFilter(type, freq, Q, gain) {
var filter = context.createBiquadFilter();
mid.type = type;
mid.frequency.value = freq;
mid.Q.value = Q;
mid.gain.value = gain;
return filter;
}
/*
* Range: [0; 50]
*/
function linear_to_db(linear) {
if (linear < 50) {
return -(db_to_linear(50.0 - linear));
} else if (linear === 50) {
return 0;
} else {
return db_to_linear(linear - 50.0);
}
}
function db_to_linear(db) {
return Math.pow(10, db / 30.0) - 1.0;
}
/*
* Range: [0, 2]
* Volume=1 produces sound without any gain.
* Volume=2 doubles the natural volume of the input sound.
*/
this.set_volume = function(newVolume) {
if (newVolume>=0 && newVolume<=2)
gain_node.gain.value = newVolume;
}
/*
* Range: [0; 2]
* Gain=0 is the maximum attenuation.
* Gain=2 doubles the natural gain of the input sound.
*/
this.set_low_gain = function(newGain) {
if (newGain>=0 && newGain<=2)
lowpass.gain.value = linear_to_db(50*newGain);
}
/*
* Range: [0; 2]
* Gain=0 is the maximum attenuation.
* Gain=2 doubles the natural gain of the input sound.
*/
this.set_mid_gain = function(newGain) {
if (newGain>=0 && newGain<=2)
mid.gain.value = linear_to_db(50*newGain);
}
/*
* Range: [0; 2]
* Gain=0 is the maximum attenuation.
* Gain=2 doubles the natural gain of the input sound.
*/
this.set_hi_gain = function(newGain) {
if (newGain>=0 && newGain<=2)
highpass.gain.value = linear_to_db(50*newGain);
}
}