-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-reverb.js
134 lines (115 loc) · 2.69 KB
/
simple-reverb.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
/**
* Simple Reverb constructor.
*
* @param {AudioContext} context
* @param {object} opts
* @param {number} opts.seconds
* @param {number} opts.decay
* @param {boolean} opts.reverse
*/
function SimpleReverb (context, opts) {
this.input = this.output = context.createConvolver();
this._context = context;
var p = this.meta.params;
opts = opts || {};
this._seconds = opts.seconds || p.seconds.defaultValue;
this._decay = opts.decay || p.decay.defaultValue;
this._reverse = opts.reverse || p.reverse.defaultValue;
this._buildImpulse();
}
SimpleReverb.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();
}
},
/**
* Utility function for building an impulse response
* from the module parameters.
*/
_buildImpulse: {
value: function () {
var rate = this._context.sampleRate
, length = rate * this.seconds
, decay = this.decay
, impulse = this._context.createBuffer(2, length, rate)
, impulseL = impulse.getChannelData(0)
, impulseR = impulse.getChannelData(1)
, n, i;
for (i = 0; i < length; i++) {
n = this.reverse ? length - i : i;
impulseL[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
impulseR[i] = (Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
}
this.input.buffer = impulse;
}
},
/**
* Module parameter metadata.
*/
meta: {
value: {
name: "SimpleReverb",
params: {
seconds: {
min: 1,
max: 50,
defaultValue: 3,
type: "float"
},
decay: {
min: 0,
max: 100,
defaultValue: 2,
type: "float"
},
reverse: {
min: 0,
max: 1,
defaultValue: 0,
type: "bool"
}
}
}
},
/**
* Public parameters.
*/
seconds: {
enumerable: true,
get: function () { return this._seconds; },
set: function (value) {
this._seconds = value;
this._buildImpulse();
}
},
decay: {
enumerable: true,
get: function () { return this._decay; },
set: function (value) {
this._decay = value;
this._buildImpulse();
}
},
reverse: {
enumerable: true,
get: function () { return this._reverse; },
set: function (value) {
this._reverse = value;
this._buildImpulse();
}
}
});