-
Notifications
You must be signed in to change notification settings - Fork 26
/
index.js
331 lines (287 loc) · 9.58 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* clubber.js 1.7.1 Copyright (c) 2016-2017, Yannis Gravezas All Rights Reserved.
* Available under the MIT license. See http://github.com/wizgrav/clubber for info.
*/
var Clubber = function (config) {
if (!config) config = {};
this.context = config.context || new (window.AudioContext || window.webkitAudioContext)();
var analyser = config.analyser || this.context.createAnalyser();
analyser.fftSize = config.analyser ? config.analyser.fftSize : (config.size || 2048);
config.mute = config.analyser ? true : config.mute;
this.fps = config.fps || 60;
Object.defineProperty(this, 'smoothing', {
get: function() {
return analyser.smoothingTimeConstant;
},
set: function(value) {
analyser.smoothingTimeConstant = value;
}
});
Object.defineProperty(this, 'fftSize', {
get: function() {
return analyser.fftSize;
},
set: function(value) {
analyser.fftSize = value;
}
});
this._muted = true;
Object.defineProperty(this, 'muted', {
get: function() {
return this._muted;
},
set: function(value) {
if(!this.analyser) return true;
if(this._muted) {
if(value === false){
this.analyser.connect(this.context.destination);
this._muted = false;
}
} else if(value === true){
this.analyser.disconnect(this.context.destination);
this._muted = true;
}
}
});
Object.defineProperty(this, 'sampleRate', {
get: function() {
return this.context.sampleRate;
}
});
this.analyser = analyser;
this.resize(analyser.frequencyBinCount);
this.muted = !!config.mute;
};
Clubber.prototype.resize = function(bins) {
if(this.bufferLength === bins) return;
this.maxBin = 0;
var lastkey=0,idx=0;
this.bufferLength = bins;
this.data = new Uint8Array(this.bufferLength);
this.keys = new Uint8Array(this.bufferLength);
this.noteSums = new Uint16Array(128);
this.notes = new Uint8Array(128);
this.weights = new Uint8Array(128);
for(var i = 0, inc=(this.sampleRate/2)/this.bufferLength; i < this.bufferLength;i++){
var freq = (i+0.5)*inc;
this.maxBin = i;
if(freq > 13280) {
break;
}
var key = Math.floor(17.3123405046 * Math.log(.12231220585 * freq));
this.keys[i] = key;
this.weights[key]++;
}
var holeIndex = 0;
for(i=0;i<128;i++){
if(!this.weights[i]) holeIndex = i;
}
this.holeIndex = holeIndex + 1;
};
Clubber.prototype.listen = function (obj) {
if (this.source) { this.source.disconnect(this.analyser); }
if ( obj instanceof AudioNode ) {
this.el = null;
this.source = obj;
} else {
this.el = obj;
if (obj._mediaElementSource) {
this.source = obj._mediaElementSource;
} else {
this.source = obj._mediaElementSource = this.context.createMediaElementSource(obj);
}
}
this.source.connect(this.analyser);
};
Clubber.prototype.band = function (config) {
var scope = this;
var parseConfig = function(config) {
var defaults = {
from:1, to:128, low:64, high:128,
smooth: [0.1, 0.1, 0.1, 0.1],
adapt: [1.0, 1.0, 1.0, 1.0],
snap: 0.33, template: [0, 1, 2, 3]
};
if(config){
for (var k in defaults) {
if (!config[k]) config[k] = defaults[k];
}
if(typeof config.template === "string") {
var t = [];
for(var i = 0; i < config.template.length; i++)
t.push(parseInt(config.template[i]));
config.template = t;
}
var rect = {
from: config.from,
to: config.to,
low: this.rect ? this.rect.low : config.low,
high: this.rect ? this.rect.high : config.high,
};
this.rect = rect;
var data = new Float32Array(config.template.length);
if (this.data) data.set(this.data);
this.config = config;
this.data = data;
}
return this;
};
var obj = parseConfig.call({}, config);
return function (output, offset) {
function fill(arr, output, offset) {
offset = offset || 0;
if (output) {
if (output instanceof Float32Array) {
output.set(arr, offset);
} else if(Array.isArray(output)){
var length = Math.min(output.length, arr.length) - offset;
for (var i = 0; i < length; i++) output[offset+i] = arr[i];
} else if(output.fromArray){
output.fromArray(arr);
}
}
};
var config = obj.config, data = obj.data, rect = obj.rect;
rect.high = Math.max(rect.high, rect.low + 1); // a bit ugly
if(typeof offset === "object"){
parseConfig.call(obj, offset);
offset = arguments[2];
}
offset = offset || 0;
if (obj.time > scope.time){
fill(data, output, offset);
return rect;
}
var s = config.smooth, snap = config.snap, idx=0, val=0, Val=0, midx=0, mval=128, Vsum, vsum=0, nsum=0, xsum=0, psum=0, osum = 0, cnt=0;
for(var i=config.from; i < config.to;i++){
var V = scope.notes[i] / 2;
var v = Math.min(rect.high, V);
if (v >= rect.low) {
// Sum musical keys and power.
v -= rect.low;
var x = i - config.from;
osum += Math.round( i / 12) * v;
nsum += ( i % 12 ) * v;
psum += x * v;
vsum += v;
xsum += x;
cnt++;
// Strongest note.
if (V > Val){
idx = i;
Val = V;
val = v;
} else if(v < mval) {
midx = i;
mval = v;
}
}
}
// Dont change note info if no activity was recorded
if(cnt) {
obj.midx=(midx % 12) / 12;
obj.idx=(idx % 12) / 12;
obj.avg=(nsum / vsum) / 12;
}
// Exponential smoothing. When negative: snap is used when value rises and abs(value) when falling.
function smoothFn (v, o, f, snap){
v = !v ? 0 : v;
f = f === undefined ? 0.1 : f;
f = Math.min(f, snap);
if (f < 0) { f = v > o ? Math.abs(snap) : -f; }
return f * v + (1 - f) * o;
};
var width = config.to - config.from, av = cnt ? vsum / cnt : 0;
var height = rect.high - rect.low, _height = config.high - config.low, area = width * height;
var ah = Math.min(config.high, config.low + av + config.adapt[2] * _height);
var al = Math.max(config.low, config.low + av - config.adapt[0] * _height);
var ocf = Math.floor(config.from / 12), oct = Math.ceil(config.to / 12);
val = height ? val / height : 0;
// fixed timestep
if (obj.time === undefined) obj.time = scope.time;
for (var t = obj.time, step = 1000 / scope.fps, tmax = scope.time ; t < tmax; t += step) {
config.template.forEach(function (k,i) {
switch (k) {
default:
data[i] = smoothFn(obj.idx, data[i], s[i], snap); break;
case 1:
data[i] = smoothFn(obj.midx, data[i], s[i], snap); break;
case 2:
data[i] = smoothFn(obj.avg , data[i], s[i], snap); break;
case 3:
data[i] = smoothFn(val, data[i], s[i], snap); break;
case 4:
data[i] = smoothFn(cnt && height ? av / height : 0, data[i], s[i], snap); break;
case 5:
data[i] = smoothFn(vsum ? ((psum / vsum)) / width : 0 , data[i], s[i], snap); break;
case 6:
data[i] = smoothFn(vsum ? ((osum / vsum - ocf)) / (oct - ocf) : 0, data[i], s[i], snap); break;
case 7:
data[i] = smoothFn(area ? vsum/area:0, data[i], s[i], snap); break;
case 8:
data[i] = smoothFn((rect.low - config.low) / _height, data[i], s[i], snap); break;
case 9:
data[i] = smoothFn((rect.high - config.low) / _height, data[i], s[i], snap); break;
}
});
rect.high = smoothFn(ah, rect.high, config.adapt[3], snap);
rect.low = smoothFn(al, rect.low, config.adapt[1], snap);
}
obj.time = t;
fill(data, output, offset);
return rect;
};
};
// You can pass the frequency data on your own using the second argument.
// isProcessed specifies whether the data are already in midi space.
Clubber.prototype.update = function (time, data, isProcessed) {
var c = this.cache, self=this;
if (data) {
if(isProcessed || data.length === 128) {
this.notes.set(data);
return;
}
this.resize(data.length);
} else {
this.analyser.getByteFrequencyData(this.data);
isProcessed = false;
data = this.data;
this.resize(this.analyser.frequencyBinCount);
}
// Calculate energy per midi note and fill holes in the lower octaves
for(var i = 0; i < this.notes.length; i++){
this.noteSums[i] = 0;
}
for(i = 0; i < this.maxBin; i++){
this.noteSums[this.keys[i]] += data[i];
}
var lastIndex = 0, lastVal=0;
for(i = 0; i < this.notes.length; i++){
var w = this.weights[i];
if(!w) continue;
var v = this.noteSums[i] / w;
this.notes[i] = v;
if (i > this.holeIndex) continue;
var di = i - lastIndex;
var dv = v - lastVal;
for(var j = lastIndex ? 1 : 0 ; j < di; j++) {
this.notes[lastIndex + j] = lastVal + j * dv/di;
}
lastVal = v;
lastIndex = i;
}
this.time = !isNaN(parseFloat(time)) ? parseFloat(time) : window.performance.now();
};
Clubber.prototype.descriptions = [
"Most powerful note index",
"Least powerfull note index",
"Power weighted note average",
"Power of the strongest note",
"Average power of active notes",
"Power weighted average midi index",
"Power weighted average octave index",
"Ratio of spectrum window area covered",
"Adaptive low threshold relative to bounds",
"Adaptive high threshold relative to bounds",
];
module.exports = window.Clubber = Clubber;