forked from tst/mlg-soundboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overdrive.js
161 lines (138 loc) · 3.48 KB
/
overdrive.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* Overdrive effect module for the Web Audio API.
*
* @param {AudioContext} context
* @param {object} opts
* @param {number} opts.preBand
* @param {number} opts.color
* @param {number} opts.drive
* @param {number} opts.postCut
*/
function Overdrive (context, opts) {
this.input = context.createGain();
this.output = context.createGain();
// Internal AudioNodes
this._bandpass = context.createBiquadFilter();
this._bpWet = context.createGain();
this._bpDry = context.createGain();
this._ws = context.createWaveShaper();
this._lowpass = context.createBiquadFilter();
// AudioNode graph routing
this.input.connect(this._bandpass);
this._bandpass.connect(this._bpWet);
this._bandpass.connect(this._bpDry);
this._bpWet.connect(this._ws);
this._bpDry.connect(this._ws);
this._ws.connect(this._lowpass);
this._lowpass.connect(this.output);
// Defaults
var p = this.meta.params;
opts = opts || {};
this._bandpass.frequency.value = opts.color || p.color.defaultValue;
this._bpWet.gain.value = opts.preBand || p.preBand.defaultValue;
this._lowpass.frequency.value = opts.postCut || p.postCut.defaultValue;
this.drive = opts.drive || p.drive.defaultValue;
// Inverted preBand value
this._bpDry.gain.value = opts.preBand
? 1 - opts.preBand
: 1 - p.preBand.defaultValue;
}
Overdrive.prototype = Object.create(null, {
/**
* AudioNode prototype `connect` method.
*
* @param {AudioNode} dest
*/
connect: {
value: function (dest) {
this.output.connect( dest.input ? dest.input : dest );
}
},
/**
* AudioNode prototype `disconnect` method.
*/
disconnect: {
value: function () {
this.output.disconnect();
}
},
/**
* Module parameter metadata.
*/
meta: {
value: {
name: "Overdrive",
params: {
preBand: {
min: 0,
max: 1.0,
defaultValue: 0.5,
type: "float"
},
color: {
min: 0,
max: 22050,
defaultValue: 800,
type: "float"
},
drive: {
min: 0.0,
max: 1.0,
defaultValue: 0.5,
type: "float"
},
postCut: {
min: 0,
max: 22050,
defaultValue: 3000,
type: "float"
}
}
}
},
/**
* Public parameters
*/
preBand: {
enumerable: true,
get: function () { return this._bpWet.gain.value; },
set: function (value) {
this._bpWet.gain.setValueAtTime(value, 0);
this._bpDry.gain.setValueAtTime(1 - value, 0);
}
},
color: {
enumerable: true,
get: function () { return this._bandpass.frequency.value; },
set: function (value) {
this._bandpass.frequency.setValueAtTime(value, 0);
}
},
drive: {
enumerable: true,
get: function () { return this._drive; },
set: function (value) {
var k = value * 100
, n = 22050
, curve = new Float32Array(n)
, deg = Math.PI / 180;
this._drive = value;
for (var i = 0; i < n; i++) {
var x = i * 2 / n - 1;
curve[i] = (3 + k) * x * 20 * deg / (Math.PI + k * Math.abs(x));
}
this._ws.curve = curve;
}
},
postCut: {
enumerable: true,
get: function () { return this._lowpass.frequency.value; },
set: function (value) {
this._lowpass.frequency.setValueAtTime(value, 0);
}
}
});
/**
* Exports.
*/
/* module.exports = Overdrive; */