-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
153 lines (130 loc) · 3.29 KB
/
index.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
/**
* Lowpass feedback comb filter implementation.
*
* Based on the implementation used in the Freeverb algorithm.
* http://en.wikipedia.org/wiki/Comb_filter
* https://ccrma.stanford.edu/~jos/pasp/Lowpass_Feedback_Comb_Filter.html
*
* @param {AudioContext} context
* @param {object} opts
* @param {number} opts.delay
* @param {number} opts.feedback
* @param {number} opts.damping
* @param {number} opts.cutoff
*/
function Comb (context, opts) {
this.input = context.createGain();
this.output = context.createGain();
// Internal AudioNodes
this._delay = context.createDelay();
this._damping = context.createGain();
this._filter = context.createBiquadFilter();
this._feedback = context.createGain();
// AudioNode graph routing
this.input.connect(this._delay);
this._delay.connect(this._damping);
this._damping.connect(this.output);
this._damping.connect(this._filter);
this._filter.connect(this._feedback);
this._feedback.connect(this.input);
// Defaults
var p = this.meta.params;
opts = opts || {};
this._delay.delayTime.value = opts.delay || p.delay.defaultValue;
this._feedback.gain.value = opts.feedback || p.feedback.defaultValue;
this._damping.gain.value = opts.damping || p.damping.defaultValue;
this._filter.frequency.value = opts.cutoff || p.damping.defaultValue;
// Prevent positive feedback loops
if (this.feedback * this.damping >= 1.0) {
throw new Error("These parameter values will create a positive feedback loop.");
}
}
Comb.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: "Comb Filter",
params: {
delay: {
min: 0,
max: 3,
defaultValue: 0.027,
type: "float"
},
feedback: {
min: 0,
max: 1,
defaultValue: 0.84,
type: "float"
},
damping: {
min: 0,
max: 1,
defaultValue: 0.52,
type: "float"
},
cutoff: {
min: 0,
max: 22050,
defaultValue: 3000,
type: "float"
}
}
}
},
/**
* Public parameters.
*/
delay: {
enumerable: true,
get: function () { return this._delay.delayTime.value; },
set: function (value) {
this._delay.delayTime.setValueAtTime(value, 0);
}
},
feedback: {
enumerable: true,
get: function () { return this._feedback.gain.value; },
set: function (value) {
this._feedback.gain.setValueAtTime(value, 0);
}
},
damping: {
enumerable: true,
get: function () { return this._damping.gain.value; },
set: function (value) {
this._damping.gain.setValueAtTime(value, 0);
}
},
cutoff: {
enumerable: true,
get: function () { return this._filter.frequency.value; },
set: function (value) {
this._filter.frequency.setValueAtTime(value, 0);
}
}
});
/**
* Exports.
*/
module.exports = Comb;