wavesurfer.js 6.2.0 → 6.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +1 -10
- package/dist/plugin/wavesurfer.cursor.js +29 -49
- package/dist/plugin/wavesurfer.cursor.js.map +1 -1
- package/dist/plugin/wavesurfer.cursor.min.js +2 -2
- package/dist/plugin/wavesurfer.cursor.min.js.map +1 -1
- package/dist/plugin/wavesurfer.elan.js +19 -49
- package/dist/plugin/wavesurfer.elan.js.map +1 -1
- package/dist/plugin/wavesurfer.elan.min.js +1 -1
- package/dist/plugin/wavesurfer.elan.min.js.map +1 -1
- package/dist/plugin/wavesurfer.markers.js +87 -67
- package/dist/plugin/wavesurfer.markers.js.map +1 -1
- package/dist/plugin/wavesurfer.markers.min.js +2 -2
- package/dist/plugin/wavesurfer.markers.min.js.map +1 -1
- package/dist/plugin/wavesurfer.mediasession.js +5 -15
- package/dist/plugin/wavesurfer.mediasession.js.map +1 -1
- package/dist/plugin/wavesurfer.mediasession.min.js +1 -1
- package/dist/plugin/wavesurfer.mediasession.min.js.map +1 -1
- package/dist/plugin/wavesurfer.microphone.js +44 -62
- package/dist/plugin/wavesurfer.microphone.js.map +1 -1
- package/dist/plugin/wavesurfer.microphone.min.js +1 -1
- package/dist/plugin/wavesurfer.microphone.min.js.map +1 -1
- package/dist/plugin/wavesurfer.minimap.js +16 -68
- package/dist/plugin/wavesurfer.minimap.js.map +1 -1
- package/dist/plugin/wavesurfer.minimap.min.js +1 -1
- package/dist/plugin/wavesurfer.minimap.min.js.map +1 -1
- package/dist/plugin/wavesurfer.playhead.js +2 -29
- package/dist/plugin/wavesurfer.playhead.js.map +1 -1
- package/dist/plugin/wavesurfer.playhead.min.js +1 -1
- package/dist/plugin/wavesurfer.playhead.min.js.map +1 -1
- package/dist/plugin/wavesurfer.regions.js +99 -253
- package/dist/plugin/wavesurfer.regions.js.map +1 -1
- package/dist/plugin/wavesurfer.regions.min.js +2 -2
- package/dist/plugin/wavesurfer.regions.min.js.map +1 -1
- package/dist/plugin/wavesurfer.spectrogram.js +52 -147
- package/dist/plugin/wavesurfer.spectrogram.js.map +1 -1
- package/dist/plugin/wavesurfer.spectrogram.min.js +2 -2
- package/dist/plugin/wavesurfer.spectrogram.min.js.map +1 -1
- package/dist/plugin/wavesurfer.timeline.js +42 -85
- package/dist/plugin/wavesurfer.timeline.js.map +1 -1
- package/dist/plugin/wavesurfer.timeline.min.js +1 -1
- package/dist/plugin/wavesurfer.timeline.min.js.map +1 -1
- package/dist/wavesurfer-html-init.js +31 -53
- package/dist/wavesurfer-html-init.js.map +1 -1
- package/dist/wavesurfer-html-init.min.js +1 -1
- package/dist/wavesurfer-html-init.min.js.map +1 -1
- package/dist/wavesurfer.js +448 -1008
- package/dist/wavesurfer.js.map +1 -1
- package/dist/wavesurfer.min.js +2 -2
- package/dist/wavesurfer.min.js.map +1 -1
- package/package.json +18 -19
- package/src/drawer.multicanvas.js +4 -4
- package/src/plugin/cursor/index.js +15 -3
- package/src/plugin/markers/index.js +70 -5
- package/src/plugin/regions/index.js +1 -1
- package/src/plugin/spectrogram/index.js +1 -1
- package/src/wavesurfer.js +12 -9
- package/CHANGES.md +0 -474
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wavesurfer.js spectrogram plugin 6.
|
|
2
|
+
* wavesurfer.js spectrogram plugin 6.4.0 (2022-11-05)
|
|
3
3
|
* https://wavesurfer-js.org
|
|
4
4
|
* @license BSD-3-Clause
|
|
5
5
|
*/
|
|
@@ -29,7 +29,6 @@ Object.defineProperty(exports, "__esModule", ({
|
|
|
29
29
|
value: true
|
|
30
30
|
}));
|
|
31
31
|
exports["default"] = FFT;
|
|
32
|
-
|
|
33
32
|
/* eslint-disable complexity, no-redeclare, no-var, one-var */
|
|
34
33
|
|
|
35
34
|
/**
|
|
@@ -51,153 +50,118 @@ function FFT(bufferSize, sampleRate, windowFunc, alpha) {
|
|
|
51
50
|
this.peakBand = 0;
|
|
52
51
|
this.peak = 0;
|
|
53
52
|
var i;
|
|
54
|
-
|
|
55
53
|
switch (windowFunc) {
|
|
56
54
|
case 'bartlett':
|
|
57
55
|
for (i = 0; i < bufferSize; i++) {
|
|
58
56
|
this.windowValues[i] = 2 / (bufferSize - 1) * ((bufferSize - 1) / 2 - Math.abs(i - (bufferSize - 1) / 2));
|
|
59
57
|
}
|
|
60
|
-
|
|
61
58
|
break;
|
|
62
|
-
|
|
63
59
|
case 'bartlettHann':
|
|
64
60
|
for (i = 0; i < bufferSize; i++) {
|
|
65
61
|
this.windowValues[i] = 0.62 - 0.48 * Math.abs(i / (bufferSize - 1) - 0.5) - 0.38 * Math.cos(Math.PI * 2 * i / (bufferSize - 1));
|
|
66
62
|
}
|
|
67
|
-
|
|
68
63
|
break;
|
|
69
|
-
|
|
70
64
|
case 'blackman':
|
|
71
65
|
alpha = alpha || 0.16;
|
|
72
|
-
|
|
73
66
|
for (i = 0; i < bufferSize; i++) {
|
|
74
67
|
this.windowValues[i] = (1 - alpha) / 2 - 0.5 * Math.cos(Math.PI * 2 * i / (bufferSize - 1)) + alpha / 2 * Math.cos(4 * Math.PI * i / (bufferSize - 1));
|
|
75
68
|
}
|
|
76
|
-
|
|
77
69
|
break;
|
|
78
|
-
|
|
79
70
|
case 'cosine':
|
|
80
71
|
for (i = 0; i < bufferSize; i++) {
|
|
81
72
|
this.windowValues[i] = Math.cos(Math.PI * i / (bufferSize - 1) - Math.PI / 2);
|
|
82
73
|
}
|
|
83
|
-
|
|
84
74
|
break;
|
|
85
|
-
|
|
86
75
|
case 'gauss':
|
|
87
76
|
alpha = alpha || 0.25;
|
|
88
|
-
|
|
89
77
|
for (i = 0; i < bufferSize; i++) {
|
|
90
78
|
this.windowValues[i] = Math.pow(Math.E, -0.5 * Math.pow((i - (bufferSize - 1) / 2) / (alpha * (bufferSize - 1) / 2), 2));
|
|
91
79
|
}
|
|
92
|
-
|
|
93
80
|
break;
|
|
94
|
-
|
|
95
81
|
case 'hamming':
|
|
96
82
|
for (i = 0; i < bufferSize; i++) {
|
|
97
83
|
this.windowValues[i] = 0.54 - 0.46 * Math.cos(Math.PI * 2 * i / (bufferSize - 1));
|
|
98
84
|
}
|
|
99
|
-
|
|
100
85
|
break;
|
|
101
|
-
|
|
102
86
|
case 'hann':
|
|
103
87
|
case undefined:
|
|
104
88
|
for (i = 0; i < bufferSize; i++) {
|
|
105
89
|
this.windowValues[i] = 0.5 * (1 - Math.cos(Math.PI * 2 * i / (bufferSize - 1)));
|
|
106
90
|
}
|
|
107
|
-
|
|
108
91
|
break;
|
|
109
|
-
|
|
110
92
|
case 'lanczoz':
|
|
111
93
|
for (i = 0; i < bufferSize; i++) {
|
|
112
94
|
this.windowValues[i] = Math.sin(Math.PI * (2 * i / (bufferSize - 1) - 1)) / (Math.PI * (2 * i / (bufferSize - 1) - 1));
|
|
113
95
|
}
|
|
114
|
-
|
|
115
96
|
break;
|
|
116
|
-
|
|
117
97
|
case 'rectangular':
|
|
118
98
|
for (i = 0; i < bufferSize; i++) {
|
|
119
99
|
this.windowValues[i] = 1;
|
|
120
100
|
}
|
|
121
|
-
|
|
122
101
|
break;
|
|
123
|
-
|
|
124
102
|
case 'triangular':
|
|
125
103
|
for (i = 0; i < bufferSize; i++) {
|
|
126
104
|
this.windowValues[i] = 2 / bufferSize * (bufferSize / 2 - Math.abs(i - (bufferSize - 1) / 2));
|
|
127
105
|
}
|
|
128
|
-
|
|
129
106
|
break;
|
|
130
|
-
|
|
131
107
|
default:
|
|
132
108
|
throw Error("No such window function '" + windowFunc + "'");
|
|
133
109
|
}
|
|
134
|
-
|
|
135
110
|
var limit = 1;
|
|
136
111
|
var bit = bufferSize >> 1;
|
|
137
112
|
var i;
|
|
138
|
-
|
|
139
113
|
while (limit < bufferSize) {
|
|
140
114
|
for (i = 0; i < limit; i++) {
|
|
141
115
|
this.reverseTable[i + limit] = this.reverseTable[i] + bit;
|
|
142
116
|
}
|
|
143
|
-
|
|
144
117
|
limit = limit << 1;
|
|
145
118
|
bit = bit >> 1;
|
|
146
119
|
}
|
|
147
|
-
|
|
148
120
|
for (i = 0; i < bufferSize; i++) {
|
|
149
121
|
this.sinTable[i] = Math.sin(-Math.PI / i);
|
|
150
122
|
this.cosTable[i] = Math.cos(-Math.PI / i);
|
|
151
123
|
}
|
|
152
|
-
|
|
153
124
|
this.calculateSpectrum = function (buffer) {
|
|
154
125
|
// Locally scope variables for speed up
|
|
155
126
|
var bufferSize = this.bufferSize,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
127
|
+
cosTable = this.cosTable,
|
|
128
|
+
sinTable = this.sinTable,
|
|
129
|
+
reverseTable = this.reverseTable,
|
|
130
|
+
real = new Float32Array(bufferSize),
|
|
131
|
+
imag = new Float32Array(bufferSize),
|
|
132
|
+
bSi = 2 / this.bufferSize,
|
|
133
|
+
sqrt = Math.sqrt,
|
|
134
|
+
rval,
|
|
135
|
+
ival,
|
|
136
|
+
mag,
|
|
137
|
+
spectrum = new Float32Array(bufferSize / 2);
|
|
167
138
|
var k = Math.floor(Math.log(bufferSize) / Math.LN2);
|
|
168
|
-
|
|
169
139
|
if (Math.pow(2, k) !== bufferSize) {
|
|
170
140
|
throw 'Invalid buffer size, must be a power of 2.';
|
|
171
141
|
}
|
|
172
|
-
|
|
173
142
|
if (bufferSize !== buffer.length) {
|
|
174
143
|
throw 'Supplied buffer is not the same size as defined FFT. FFT Size: ' + bufferSize + ' Buffer Size: ' + buffer.length;
|
|
175
144
|
}
|
|
176
|
-
|
|
177
145
|
var halfSize = 1,
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
146
|
+
phaseShiftStepReal,
|
|
147
|
+
phaseShiftStepImag,
|
|
148
|
+
currentPhaseShiftReal,
|
|
149
|
+
currentPhaseShiftImag,
|
|
150
|
+
off,
|
|
151
|
+
tr,
|
|
152
|
+
ti,
|
|
153
|
+
tmpReal;
|
|
187
154
|
for (var i = 0; i < bufferSize; i++) {
|
|
188
155
|
real[i] = buffer[reverseTable[i]] * this.windowValues[reverseTable[i]];
|
|
189
156
|
imag[i] = 0;
|
|
190
157
|
}
|
|
191
|
-
|
|
192
158
|
while (halfSize < bufferSize) {
|
|
193
159
|
phaseShiftStepReal = cosTable[halfSize];
|
|
194
160
|
phaseShiftStepImag = sinTable[halfSize];
|
|
195
161
|
currentPhaseShiftReal = 1;
|
|
196
162
|
currentPhaseShiftImag = 0;
|
|
197
|
-
|
|
198
163
|
for (var fftStep = 0; fftStep < halfSize; fftStep++) {
|
|
199
164
|
var i = fftStep;
|
|
200
|
-
|
|
201
165
|
while (i < bufferSize) {
|
|
202
166
|
off = i + halfSize;
|
|
203
167
|
tr = currentPhaseShiftReal * real[off] - currentPhaseShiftImag * imag[off];
|
|
@@ -208,32 +172,25 @@ function FFT(bufferSize, sampleRate, windowFunc, alpha) {
|
|
|
208
172
|
imag[i] += ti;
|
|
209
173
|
i += halfSize << 1;
|
|
210
174
|
}
|
|
211
|
-
|
|
212
175
|
tmpReal = currentPhaseShiftReal;
|
|
213
176
|
currentPhaseShiftReal = tmpReal * phaseShiftStepReal - currentPhaseShiftImag * phaseShiftStepImag;
|
|
214
177
|
currentPhaseShiftImag = tmpReal * phaseShiftStepImag + currentPhaseShiftImag * phaseShiftStepReal;
|
|
215
178
|
}
|
|
216
|
-
|
|
217
179
|
halfSize = halfSize << 1;
|
|
218
180
|
}
|
|
219
|
-
|
|
220
181
|
for (var i = 0, N = bufferSize / 2; i < N; i++) {
|
|
221
182
|
rval = real[i];
|
|
222
183
|
ival = imag[i];
|
|
223
184
|
mag = bSi * sqrt(rval * rval + ival * ival);
|
|
224
|
-
|
|
225
185
|
if (mag > this.peak) {
|
|
226
186
|
this.peakBand = i;
|
|
227
187
|
this.peak = mag;
|
|
228
188
|
}
|
|
229
|
-
|
|
230
189
|
spectrum[i] = mag;
|
|
231
190
|
}
|
|
232
|
-
|
|
233
191
|
return spectrum;
|
|
234
192
|
};
|
|
235
193
|
}
|
|
236
|
-
|
|
237
194
|
module.exports = exports.default;
|
|
238
195
|
|
|
239
196
|
/***/ }),
|
|
@@ -250,17 +207,11 @@ Object.defineProperty(exports, "__esModule", ({
|
|
|
250
207
|
value: true
|
|
251
208
|
}));
|
|
252
209
|
exports["default"] = void 0;
|
|
253
|
-
|
|
254
210
|
var _fft = _interopRequireDefault(__webpack_require__(/*! ./fft */ "./src/plugin/spectrogram/fft.js"));
|
|
255
|
-
|
|
256
211
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
257
|
-
|
|
258
212
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
259
|
-
|
|
260
213
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
261
|
-
|
|
262
214
|
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
263
|
-
|
|
264
215
|
/**
|
|
265
216
|
* @typedef {Object} SpectrogramPluginParams
|
|
266
217
|
* @property {string|HTMLElement} container Selector of element or element in
|
|
@@ -291,7 +242,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
291
242
|
* Each entry should contain a float between 0 and 1 and specify
|
|
292
243
|
* r, g, b, and alpha.
|
|
293
244
|
*/
|
|
294
|
-
|
|
295
245
|
/**
|
|
296
246
|
* Render a spectrogram visualisation of the audio.
|
|
297
247
|
*
|
|
@@ -320,58 +270,44 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
320
270
|
var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
321
271
|
function SpectrogramPlugin(params, ws) {
|
|
322
272
|
var _this = this;
|
|
323
|
-
|
|
324
273
|
_classCallCheck(this, SpectrogramPlugin);
|
|
325
|
-
|
|
326
274
|
this.params = params;
|
|
327
275
|
this.wavesurfer = ws;
|
|
328
276
|
this.util = ws.util;
|
|
329
277
|
this.frequenciesDataUrl = params.frequenciesDataUrl;
|
|
330
|
-
|
|
331
278
|
this._onScroll = function (e) {
|
|
332
279
|
_this.updateScroll(e);
|
|
333
280
|
};
|
|
334
|
-
|
|
335
281
|
this._onRender = function () {
|
|
336
282
|
_this.render();
|
|
337
283
|
};
|
|
338
|
-
|
|
339
284
|
this._onWrapperClick = function (e) {
|
|
340
285
|
_this._wrapperClickHandler(e);
|
|
341
286
|
};
|
|
342
|
-
|
|
343
287
|
this._onReady = function () {
|
|
344
288
|
var drawer = _this.drawer = ws.drawer;
|
|
345
289
|
_this.container = 'string' == typeof params.container ? document.querySelector(params.container) : params.container;
|
|
346
|
-
|
|
347
290
|
if (!_this.container) {
|
|
348
291
|
throw Error('No container for WaveSurfer spectrogram');
|
|
349
292
|
}
|
|
350
|
-
|
|
351
293
|
if (params.colorMap) {
|
|
352
294
|
if (params.colorMap.length < 256) {
|
|
353
295
|
throw new Error('Colormap must contain 256 elements');
|
|
354
296
|
}
|
|
355
|
-
|
|
356
297
|
for (var i = 0; i < params.colorMap.length; i++) {
|
|
357
298
|
var cmEntry = params.colorMap[i];
|
|
358
|
-
|
|
359
299
|
if (cmEntry.length !== 4) {
|
|
360
300
|
throw new Error('ColorMap entries must contain 4 values');
|
|
361
301
|
}
|
|
362
302
|
}
|
|
363
|
-
|
|
364
303
|
_this.colorMap = params.colorMap;
|
|
365
304
|
} else {
|
|
366
305
|
_this.colorMap = [];
|
|
367
|
-
|
|
368
306
|
for (var _i = 0; _i < 256; _i++) {
|
|
369
307
|
var val = (255 - _i) / 256;
|
|
370
|
-
|
|
371
308
|
_this.colorMap.push([val, val, val, 1]);
|
|
372
309
|
}
|
|
373
310
|
}
|
|
374
|
-
|
|
375
311
|
_this.width = drawer.width;
|
|
376
312
|
_this.pixelRatio = _this.params.pixelRatio || ws.params.pixelRatio;
|
|
377
313
|
_this.fftSamples = _this.params.fftSamples || ws.params.fftSamples || 512;
|
|
@@ -380,23 +316,19 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
380
316
|
_this.windowFunc = params.windowFunc;
|
|
381
317
|
_this.alpha = params.alpha;
|
|
382
318
|
_this.splitChannels = params.splitChannels;
|
|
383
|
-
_this.channels = _this.splitChannels ? ws.backend.buffer.numberOfChannels : 1;
|
|
384
|
-
// So set 12kHz default to render like wavesurfer.js 5.x.
|
|
319
|
+
_this.channels = _this.splitChannels ? ws.backend.buffer.numberOfChannels : 1;
|
|
385
320
|
|
|
321
|
+
// Getting file's original samplerate is difficult(#1248).
|
|
322
|
+
// So set 12kHz default to render like wavesurfer.js 5.x.
|
|
386
323
|
_this.frequencyMin = params.frequencyMin || 0;
|
|
387
324
|
_this.frequencyMax = params.frequencyMax || 12000;
|
|
388
|
-
|
|
389
325
|
_this.createWrapper();
|
|
390
|
-
|
|
391
326
|
_this.createCanvas();
|
|
392
|
-
|
|
393
327
|
_this.render();
|
|
394
|
-
|
|
395
328
|
drawer.wrapper.addEventListener('scroll', _this._onScroll);
|
|
396
329
|
ws.on('redraw', _this._onRender);
|
|
397
330
|
};
|
|
398
331
|
}
|
|
399
|
-
|
|
400
332
|
_createClass(SpectrogramPlugin, [{
|
|
401
333
|
key: "init",
|
|
402
334
|
value: function init() {
|
|
@@ -417,7 +349,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
417
349
|
this.wavesurfer = null;
|
|
418
350
|
this.util = null;
|
|
419
351
|
this.params = null;
|
|
420
|
-
|
|
421
352
|
if (this.wrapper) {
|
|
422
353
|
this.wrapper.removeEventListener('click', this._onWrapperClick);
|
|
423
354
|
this.wrapper.parentNode.removeChild(this.wrapper);
|
|
@@ -428,19 +359,17 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
428
359
|
key: "createWrapper",
|
|
429
360
|
value: function createWrapper() {
|
|
430
361
|
var prevSpectrogram = this.container.querySelector('spectrogram');
|
|
431
|
-
|
|
432
362
|
if (prevSpectrogram) {
|
|
433
363
|
this.container.removeChild(prevSpectrogram);
|
|
434
364
|
}
|
|
435
|
-
|
|
436
365
|
var wsParams = this.wavesurfer.params;
|
|
437
|
-
this.wrapper = document.createElement('spectrogram');
|
|
438
|
-
|
|
366
|
+
this.wrapper = document.createElement('spectrogram');
|
|
367
|
+
// if labels are active
|
|
439
368
|
if (this.params.labels) {
|
|
440
369
|
var labelsEl = this.labelsEl = document.createElement('canvas');
|
|
441
370
|
labelsEl.classList.add('spec-labels');
|
|
442
371
|
this.drawer.style(labelsEl, {
|
|
443
|
-
position: '
|
|
372
|
+
position: 'absolute',
|
|
444
373
|
zIndex: 9,
|
|
445
374
|
height: "".concat(this.height * this.channels, "px"),
|
|
446
375
|
width: "55px"
|
|
@@ -448,7 +377,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
448
377
|
this.wrapper.appendChild(labelsEl);
|
|
449
378
|
this.loadLabels('rgba(68,68,68,0.5)', '12px', '10px', '', '#fff', '#f7f7f7', 'center', '#specLabels');
|
|
450
379
|
}
|
|
451
|
-
|
|
452
380
|
this.drawer.style(this.wrapper, {
|
|
453
381
|
display: 'block',
|
|
454
382
|
position: 'relative',
|
|
@@ -456,7 +384,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
456
384
|
webkitUserSelect: 'none',
|
|
457
385
|
height: "".concat(this.height * this.channels, "px")
|
|
458
386
|
});
|
|
459
|
-
|
|
460
387
|
if (wsParams.fillParent || wsParams.scrollParent) {
|
|
461
388
|
this.drawer.style(this.wrapper, {
|
|
462
389
|
width: '100%',
|
|
@@ -464,7 +391,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
464
391
|
overflowY: 'hidden'
|
|
465
392
|
});
|
|
466
393
|
}
|
|
467
|
-
|
|
468
394
|
this.container.appendChild(this.wrapper);
|
|
469
395
|
this.wrapper.addEventListener('click', this._onWrapperClick);
|
|
470
396
|
}
|
|
@@ -489,7 +415,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
489
415
|
key: "render",
|
|
490
416
|
value: function render() {
|
|
491
417
|
this.updateCanvasStyle();
|
|
492
|
-
|
|
493
418
|
if (this.frequenciesDataUrl) {
|
|
494
419
|
this.loadFrequenciesData(this.frequenciesDataUrl);
|
|
495
420
|
} else {
|
|
@@ -513,23 +438,19 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
513
438
|
// to [channel, sample, freq] format
|
|
514
439
|
frequenciesData = [frequenciesData];
|
|
515
440
|
}
|
|
516
|
-
|
|
517
441
|
var spectrCc = my.spectrCc;
|
|
518
442
|
var height = my.fftSamples / 2;
|
|
519
443
|
var width = my.width;
|
|
520
444
|
var freqFrom = my.buffer.sampleRate / 2;
|
|
521
445
|
var freqMin = my.frequencyMin;
|
|
522
446
|
var freqMax = my.frequencyMax;
|
|
523
|
-
|
|
524
447
|
if (!spectrCc) {
|
|
525
448
|
return;
|
|
526
449
|
}
|
|
527
|
-
|
|
528
450
|
var _loop = function _loop(c) {
|
|
529
451
|
// for each channel
|
|
530
452
|
var pixels = my.resample(frequenciesData[c]);
|
|
531
453
|
var imageData = new ImageData(width, height);
|
|
532
|
-
|
|
533
454
|
for (var i = 0; i < pixels.length; i++) {
|
|
534
455
|
for (var j = 0; j < pixels[i].length; j++) {
|
|
535
456
|
var colorMap = my.colorMap[pixels[i][j]];
|
|
@@ -539,18 +460,20 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
539
460
|
imageData.data[redIndex + 2] = colorMap[2] * 255;
|
|
540
461
|
imageData.data[redIndex + 3] = colorMap[3] * 255;
|
|
541
462
|
}
|
|
542
|
-
}
|
|
543
|
-
|
|
463
|
+
}
|
|
544
464
|
|
|
465
|
+
// scale and stack spectrograms
|
|
545
466
|
createImageBitmap(imageData).then(function (renderer) {
|
|
546
|
-
return spectrCc.drawImage(renderer, 0, height * (1 - freqMax / freqFrom),
|
|
547
|
-
|
|
548
|
-
|
|
467
|
+
return spectrCc.drawImage(renderer, 0, height * (1 - freqMax / freqFrom),
|
|
468
|
+
// source x, y
|
|
469
|
+
width, height * (freqMax - freqMin) / freqFrom,
|
|
470
|
+
// source width, height
|
|
471
|
+
0, height * c,
|
|
472
|
+
// destination x, y
|
|
549
473
|
width, height // destination width, height
|
|
550
474
|
);
|
|
551
475
|
});
|
|
552
476
|
};
|
|
553
|
-
|
|
554
477
|
for (var c = 0; c < frequenciesData.length; c++) {
|
|
555
478
|
_loop(c);
|
|
556
479
|
}
|
|
@@ -561,46 +484,40 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
561
484
|
var fftSamples = this.fftSamples;
|
|
562
485
|
var buffer = this.buffer = this.wavesurfer.backend.buffer;
|
|
563
486
|
var channels = this.channels;
|
|
564
|
-
|
|
565
487
|
if (!buffer) {
|
|
566
488
|
this.fireEvent('error', 'Web Audio buffer is not available');
|
|
567
489
|
return;
|
|
568
|
-
}
|
|
569
|
-
|
|
490
|
+
}
|
|
570
491
|
|
|
492
|
+
// This may differ from file samplerate. Browser resamples audio.
|
|
571
493
|
var sampleRate = buffer.sampleRate;
|
|
572
494
|
var frequencies = [];
|
|
573
495
|
var noverlap = this.noverlap;
|
|
574
|
-
|
|
575
496
|
if (!noverlap) {
|
|
576
497
|
var uniqueSamplesPerPx = buffer.length / this.canvas.width;
|
|
577
498
|
noverlap = Math.max(0, Math.round(fftSamples - uniqueSamplesPerPx));
|
|
578
499
|
}
|
|
579
|
-
|
|
580
500
|
var fft = new _fft.default(fftSamples, sampleRate, this.windowFunc, this.alpha);
|
|
581
|
-
|
|
582
501
|
for (var c = 0; c < channels; c++) {
|
|
583
502
|
// for each channel
|
|
584
503
|
var channelData = buffer.getChannelData(c);
|
|
585
504
|
var channelFreq = [];
|
|
586
505
|
var currentOffset = 0;
|
|
587
|
-
|
|
588
506
|
while (currentOffset + fftSamples < channelData.length) {
|
|
589
507
|
var segment = channelData.slice(currentOffset, currentOffset + fftSamples);
|
|
590
508
|
var spectrum = fft.calculateSpectrum(segment);
|
|
591
509
|
var array = new Uint8Array(fftSamples / 2);
|
|
592
510
|
var j = void 0;
|
|
593
|
-
|
|
594
511
|
for (j = 0; j < fftSamples / 2; j++) {
|
|
595
512
|
array[j] = Math.max(-255, Math.log10(spectrum[j]) * 45);
|
|
596
513
|
}
|
|
597
|
-
|
|
598
|
-
|
|
514
|
+
channelFreq.push(array);
|
|
515
|
+
// channelFreq: [sample, freq]
|
|
599
516
|
|
|
600
517
|
currentOffset += fftSamples - noverlap;
|
|
601
518
|
}
|
|
602
|
-
|
|
603
|
-
|
|
519
|
+
frequencies.push(channelFreq);
|
|
520
|
+
// frequencies: [channel, sample, freq]
|
|
604
521
|
}
|
|
605
522
|
|
|
606
523
|
callback(frequencies, this);
|
|
@@ -609,7 +526,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
609
526
|
key: "loadFrequenciesData",
|
|
610
527
|
value: function loadFrequenciesData(url) {
|
|
611
528
|
var _this2 = this;
|
|
612
|
-
|
|
613
529
|
var request = this.util.fetchFile({
|
|
614
530
|
url: url
|
|
615
531
|
});
|
|
@@ -647,26 +563,26 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
647
563
|
var getMaxY = frequenciesHeight || 512;
|
|
648
564
|
var labelIndex = 5 * (getMaxY / 256);
|
|
649
565
|
var freqStart = this.frequencyMin;
|
|
650
|
-
var step = (this.frequencyMax - freqStart) / labelIndex;
|
|
566
|
+
var step = (this.frequencyMax - freqStart) / labelIndex;
|
|
651
567
|
|
|
568
|
+
// prepare canvas element for labels
|
|
652
569
|
var ctx = this.labelsEl.getContext('2d');
|
|
653
570
|
var dispScale = window.devicePixelRatio;
|
|
654
571
|
this.labelsEl.height = this.height * this.channels * dispScale;
|
|
655
572
|
this.labelsEl.width = bgWidth * dispScale;
|
|
656
573
|
ctx.scale(dispScale, dispScale);
|
|
657
|
-
|
|
658
574
|
if (!ctx) {
|
|
659
575
|
return;
|
|
660
576
|
}
|
|
661
|
-
|
|
662
577
|
for (var c = 0; c < this.channels; c++) {
|
|
663
578
|
// for each channel
|
|
664
579
|
// fill background
|
|
665
580
|
ctx.fillStyle = bgFill;
|
|
666
581
|
ctx.fillRect(0, c * getMaxY, bgWidth, (1 + c) * getMaxY);
|
|
667
582
|
ctx.fill();
|
|
668
|
-
var i = void 0;
|
|
583
|
+
var i = void 0;
|
|
669
584
|
|
|
585
|
+
// render labels
|
|
670
586
|
for (i = 0; i <= labelIndex; i++) {
|
|
671
587
|
ctx.textAlign = textAlign;
|
|
672
588
|
ctx.textBaseline = 'middle';
|
|
@@ -676,24 +592,23 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
676
592
|
var yLabelOffset = 2;
|
|
677
593
|
var x = 16;
|
|
678
594
|
var y = void 0;
|
|
679
|
-
|
|
680
595
|
if (i == 0) {
|
|
681
|
-
y = (1 + c) * getMaxY + i - 10;
|
|
682
|
-
|
|
596
|
+
y = (1 + c) * getMaxY + i - 10;
|
|
597
|
+
// unit label
|
|
683
598
|
ctx.fillStyle = textColorUnit;
|
|
684
599
|
ctx.font = fontSizeUnit + ' ' + fontType;
|
|
685
|
-
ctx.fillText(units, x + 24, y);
|
|
686
|
-
|
|
600
|
+
ctx.fillText(units, x + 24, y);
|
|
601
|
+
// freq label
|
|
687
602
|
ctx.fillStyle = textColorFreq;
|
|
688
603
|
ctx.font = fontSizeFreq + ' ' + fontType;
|
|
689
604
|
ctx.fillText(label, x, y);
|
|
690
605
|
} else {
|
|
691
|
-
y = (1 + c) * getMaxY - i * 50 + yLabelOffset;
|
|
692
|
-
|
|
606
|
+
y = (1 + c) * getMaxY - i * 50 + yLabelOffset;
|
|
607
|
+
// unit label
|
|
693
608
|
ctx.fillStyle = textColorUnit;
|
|
694
609
|
ctx.font = fontSizeUnit + ' ' + fontType;
|
|
695
|
-
ctx.fillText(units, x + 24, y);
|
|
696
|
-
|
|
610
|
+
ctx.fillText(units, x + 24, y);
|
|
611
|
+
// freq label
|
|
697
612
|
ctx.fillStyle = textColorFreq;
|
|
698
613
|
ctx.font = fontSizeFreq + ' ' + fontType;
|
|
699
614
|
ctx.fillText(label, x, y);
|
|
@@ -716,11 +631,9 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
716
631
|
var oldPiece = 1 / oldMatrix.length;
|
|
717
632
|
var newPiece = 1 / columnsNumber;
|
|
718
633
|
var i;
|
|
719
|
-
|
|
720
634
|
for (i = 0; i < columnsNumber; i++) {
|
|
721
635
|
var column = new Array(oldMatrix[0].length);
|
|
722
636
|
var j = void 0;
|
|
723
|
-
|
|
724
637
|
for (j = 0; j < oldMatrix.length; j++) {
|
|
725
638
|
var oldStart = j * oldPiece;
|
|
726
639
|
var oldEnd = oldStart + oldPiece;
|
|
@@ -729,30 +642,24 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
729
642
|
var overlap = oldEnd <= newStart || newEnd <= oldStart ? 0 : Math.min(Math.max(oldEnd, newStart), Math.max(newEnd, oldStart)) - Math.max(Math.min(oldEnd, newStart), Math.min(newEnd, oldStart));
|
|
730
643
|
var k = void 0;
|
|
731
644
|
/* eslint-disable max-depth */
|
|
732
|
-
|
|
733
645
|
if (overlap > 0) {
|
|
734
646
|
for (k = 0; k < oldMatrix[0].length; k++) {
|
|
735
647
|
if (column[k] == null) {
|
|
736
648
|
column[k] = 0;
|
|
737
649
|
}
|
|
738
|
-
|
|
739
650
|
column[k] += overlap / newPiece * oldMatrix[j][k];
|
|
740
651
|
}
|
|
741
652
|
}
|
|
742
653
|
/* eslint-enable max-depth */
|
|
743
|
-
|
|
744
654
|
}
|
|
745
655
|
|
|
746
656
|
var intColumn = new Uint8Array(oldMatrix[0].length);
|
|
747
657
|
var m = void 0;
|
|
748
|
-
|
|
749
658
|
for (m = 0; m < oldMatrix[0].length; m++) {
|
|
750
659
|
intColumn[m] = column[m];
|
|
751
660
|
}
|
|
752
|
-
|
|
753
661
|
newMatrix.push(intColumn);
|
|
754
662
|
}
|
|
755
|
-
|
|
756
663
|
return newMatrix;
|
|
757
664
|
}
|
|
758
665
|
}], [{
|
|
@@ -779,10 +686,8 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
779
686
|
};
|
|
780
687
|
}
|
|
781
688
|
}]);
|
|
782
|
-
|
|
783
689
|
return SpectrogramPlugin;
|
|
784
690
|
}();
|
|
785
|
-
|
|
786
691
|
exports["default"] = SpectrogramPlugin;
|
|
787
692
|
module.exports = exports.default;
|
|
788
693
|
|