wavesurfer.js 6.3.0 → 6.5.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 +36 -47
- package/dist/plugin/wavesurfer.cursor.js +45 -54
- 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 +24 -51
- package/dist/plugin/wavesurfer.elan.js.map +1 -1
- package/dist/plugin/wavesurfer.elan.min.js +2 -2
- package/dist/plugin/wavesurfer.elan.min.js.map +1 -1
- package/dist/plugin/wavesurfer.markers.js +62 -74
- 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 +8 -16
- package/dist/plugin/wavesurfer.mediasession.js.map +1 -1
- package/dist/plugin/wavesurfer.mediasession.min.js +2 -2
- package/dist/plugin/wavesurfer.mediasession.min.js.map +1 -1
- package/dist/plugin/wavesurfer.microphone.js +48 -63
- package/dist/plugin/wavesurfer.microphone.js.map +1 -1
- package/dist/plugin/wavesurfer.microphone.min.js +2 -2
- package/dist/plugin/wavesurfer.microphone.min.js.map +1 -1
- package/dist/plugin/wavesurfer.minimap.js +20 -69
- package/dist/plugin/wavesurfer.minimap.js.map +1 -1
- package/dist/plugin/wavesurfer.minimap.min.js +2 -2
- package/dist/plugin/wavesurfer.minimap.min.js.map +1 -1
- package/dist/plugin/wavesurfer.playhead.js +6 -30
- package/dist/plugin/wavesurfer.playhead.js.map +1 -1
- package/dist/plugin/wavesurfer.playhead.min.js +2 -2
- package/dist/plugin/wavesurfer.playhead.min.js.map +1 -1
- package/dist/plugin/wavesurfer.regions.js +203 -260
- 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 +55 -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 +53 -88
- package/dist/plugin/wavesurfer.timeline.js.map +1 -1
- package/dist/plugin/wavesurfer.timeline.min.js +2 -2
- package/dist/plugin/wavesurfer.timeline.min.js.map +1 -1
- package/dist/wavesurfer-html-init.js +34 -54
- package/dist/wavesurfer-html-init.js.map +1 -1
- package/dist/wavesurfer-html-init.min.js +2 -2
- package/dist/wavesurfer-html-init.min.js.map +1 -1
- package/dist/wavesurfer.js +490 -1003
- 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 +11 -11
- package/src/mediaelement.js +3 -0
- package/src/plugin/cursor/index.js +15 -3
- package/src/plugin/markers/index.js +40 -1
- package/src/plugin/regions/index.js +4 -0
- package/src/plugin/regions/region.js +76 -1
- package/src/util/silence-mode.js +7 -0
- package/src/wavesurfer.js +4 -0
- package/CHANGES.md +0 -482
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
* wavesurfer.js spectrogram plugin 6.
|
|
2
|
+
* wavesurfer.js spectrogram plugin 6.5.0 (2023-03-11)
|
|
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,14 @@ 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
|
-
|
|
212
|
+
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
|
|
258
213
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
259
|
-
|
|
260
|
-
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
|
-
|
|
214
|
+
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, _toPropertyKey(descriptor.key), descriptor); } }
|
|
262
215
|
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
|
-
|
|
216
|
+
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
|
217
|
+
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
|
264
218
|
/**
|
|
265
219
|
* @typedef {Object} SpectrogramPluginParams
|
|
266
220
|
* @property {string|HTMLElement} container Selector of element or element in
|
|
@@ -291,7 +245,6 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
291
245
|
* Each entry should contain a float between 0 and 1 and specify
|
|
292
246
|
* r, g, b, and alpha.
|
|
293
247
|
*/
|
|
294
|
-
|
|
295
248
|
/**
|
|
296
249
|
* Render a spectrogram visualisation of the audio.
|
|
297
250
|
*
|
|
@@ -320,58 +273,44 @@ function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _d
|
|
|
320
273
|
var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
321
274
|
function SpectrogramPlugin(params, ws) {
|
|
322
275
|
var _this = this;
|
|
323
|
-
|
|
324
276
|
_classCallCheck(this, SpectrogramPlugin);
|
|
325
|
-
|
|
326
277
|
this.params = params;
|
|
327
278
|
this.wavesurfer = ws;
|
|
328
279
|
this.util = ws.util;
|
|
329
280
|
this.frequenciesDataUrl = params.frequenciesDataUrl;
|
|
330
|
-
|
|
331
281
|
this._onScroll = function (e) {
|
|
332
282
|
_this.updateScroll(e);
|
|
333
283
|
};
|
|
334
|
-
|
|
335
284
|
this._onRender = function () {
|
|
336
285
|
_this.render();
|
|
337
286
|
};
|
|
338
|
-
|
|
339
287
|
this._onWrapperClick = function (e) {
|
|
340
288
|
_this._wrapperClickHandler(e);
|
|
341
289
|
};
|
|
342
|
-
|
|
343
290
|
this._onReady = function () {
|
|
344
291
|
var drawer = _this.drawer = ws.drawer;
|
|
345
292
|
_this.container = 'string' == typeof params.container ? document.querySelector(params.container) : params.container;
|
|
346
|
-
|
|
347
293
|
if (!_this.container) {
|
|
348
294
|
throw Error('No container for WaveSurfer spectrogram');
|
|
349
295
|
}
|
|
350
|
-
|
|
351
296
|
if (params.colorMap) {
|
|
352
297
|
if (params.colorMap.length < 256) {
|
|
353
298
|
throw new Error('Colormap must contain 256 elements');
|
|
354
299
|
}
|
|
355
|
-
|
|
356
300
|
for (var i = 0; i < params.colorMap.length; i++) {
|
|
357
301
|
var cmEntry = params.colorMap[i];
|
|
358
|
-
|
|
359
302
|
if (cmEntry.length !== 4) {
|
|
360
303
|
throw new Error('ColorMap entries must contain 4 values');
|
|
361
304
|
}
|
|
362
305
|
}
|
|
363
|
-
|
|
364
306
|
_this.colorMap = params.colorMap;
|
|
365
307
|
} else {
|
|
366
308
|
_this.colorMap = [];
|
|
367
|
-
|
|
368
309
|
for (var _i = 0; _i < 256; _i++) {
|
|
369
310
|
var val = (255 - _i) / 256;
|
|
370
|
-
|
|
371
311
|
_this.colorMap.push([val, val, val, 1]);
|
|
372
312
|
}
|
|
373
313
|
}
|
|
374
|
-
|
|
375
314
|
_this.width = drawer.width;
|
|
376
315
|
_this.pixelRatio = _this.params.pixelRatio || ws.params.pixelRatio;
|
|
377
316
|
_this.fftSamples = _this.params.fftSamples || ws.params.fftSamples || 512;
|
|
@@ -380,23 +319,19 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
380
319
|
_this.windowFunc = params.windowFunc;
|
|
381
320
|
_this.alpha = params.alpha;
|
|
382
321
|
_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.
|
|
322
|
+
_this.channels = _this.splitChannels ? ws.backend.buffer.numberOfChannels : 1;
|
|
385
323
|
|
|
324
|
+
// Getting file's original samplerate is difficult(#1248).
|
|
325
|
+
// So set 12kHz default to render like wavesurfer.js 5.x.
|
|
386
326
|
_this.frequencyMin = params.frequencyMin || 0;
|
|
387
327
|
_this.frequencyMax = params.frequencyMax || 12000;
|
|
388
|
-
|
|
389
328
|
_this.createWrapper();
|
|
390
|
-
|
|
391
329
|
_this.createCanvas();
|
|
392
|
-
|
|
393
330
|
_this.render();
|
|
394
|
-
|
|
395
331
|
drawer.wrapper.addEventListener('scroll', _this._onScroll);
|
|
396
332
|
ws.on('redraw', _this._onRender);
|
|
397
333
|
};
|
|
398
334
|
}
|
|
399
|
-
|
|
400
335
|
_createClass(SpectrogramPlugin, [{
|
|
401
336
|
key: "init",
|
|
402
337
|
value: function init() {
|
|
@@ -417,7 +352,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
417
352
|
this.wavesurfer = null;
|
|
418
353
|
this.util = null;
|
|
419
354
|
this.params = null;
|
|
420
|
-
|
|
421
355
|
if (this.wrapper) {
|
|
422
356
|
this.wrapper.removeEventListener('click', this._onWrapperClick);
|
|
423
357
|
this.wrapper.parentNode.removeChild(this.wrapper);
|
|
@@ -428,14 +362,12 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
428
362
|
key: "createWrapper",
|
|
429
363
|
value: function createWrapper() {
|
|
430
364
|
var prevSpectrogram = this.container.querySelector('spectrogram');
|
|
431
|
-
|
|
432
365
|
if (prevSpectrogram) {
|
|
433
366
|
this.container.removeChild(prevSpectrogram);
|
|
434
367
|
}
|
|
435
|
-
|
|
436
368
|
var wsParams = this.wavesurfer.params;
|
|
437
|
-
this.wrapper = document.createElement('spectrogram');
|
|
438
|
-
|
|
369
|
+
this.wrapper = document.createElement('spectrogram');
|
|
370
|
+
// if labels are active
|
|
439
371
|
if (this.params.labels) {
|
|
440
372
|
var labelsEl = this.labelsEl = document.createElement('canvas');
|
|
441
373
|
labelsEl.classList.add('spec-labels');
|
|
@@ -448,7 +380,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
448
380
|
this.wrapper.appendChild(labelsEl);
|
|
449
381
|
this.loadLabels('rgba(68,68,68,0.5)', '12px', '10px', '', '#fff', '#f7f7f7', 'center', '#specLabels');
|
|
450
382
|
}
|
|
451
|
-
|
|
452
383
|
this.drawer.style(this.wrapper, {
|
|
453
384
|
display: 'block',
|
|
454
385
|
position: 'relative',
|
|
@@ -456,7 +387,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
456
387
|
webkitUserSelect: 'none',
|
|
457
388
|
height: "".concat(this.height * this.channels, "px")
|
|
458
389
|
});
|
|
459
|
-
|
|
460
390
|
if (wsParams.fillParent || wsParams.scrollParent) {
|
|
461
391
|
this.drawer.style(this.wrapper, {
|
|
462
392
|
width: '100%',
|
|
@@ -464,7 +394,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
464
394
|
overflowY: 'hidden'
|
|
465
395
|
});
|
|
466
396
|
}
|
|
467
|
-
|
|
468
397
|
this.container.appendChild(this.wrapper);
|
|
469
398
|
this.wrapper.addEventListener('click', this._onWrapperClick);
|
|
470
399
|
}
|
|
@@ -489,7 +418,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
489
418
|
key: "render",
|
|
490
419
|
value: function render() {
|
|
491
420
|
this.updateCanvasStyle();
|
|
492
|
-
|
|
493
421
|
if (this.frequenciesDataUrl) {
|
|
494
422
|
this.loadFrequenciesData(this.frequenciesDataUrl);
|
|
495
423
|
} else {
|
|
@@ -513,23 +441,19 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
513
441
|
// to [channel, sample, freq] format
|
|
514
442
|
frequenciesData = [frequenciesData];
|
|
515
443
|
}
|
|
516
|
-
|
|
517
444
|
var spectrCc = my.spectrCc;
|
|
518
445
|
var height = my.fftSamples / 2;
|
|
519
446
|
var width = my.width;
|
|
520
447
|
var freqFrom = my.buffer.sampleRate / 2;
|
|
521
448
|
var freqMin = my.frequencyMin;
|
|
522
449
|
var freqMax = my.frequencyMax;
|
|
523
|
-
|
|
524
450
|
if (!spectrCc) {
|
|
525
451
|
return;
|
|
526
452
|
}
|
|
527
|
-
|
|
528
453
|
var _loop = function _loop(c) {
|
|
529
454
|
// for each channel
|
|
530
455
|
var pixels = my.resample(frequenciesData[c]);
|
|
531
456
|
var imageData = new ImageData(width, height);
|
|
532
|
-
|
|
533
457
|
for (var i = 0; i < pixels.length; i++) {
|
|
534
458
|
for (var j = 0; j < pixels[i].length; j++) {
|
|
535
459
|
var colorMap = my.colorMap[pixels[i][j]];
|
|
@@ -539,18 +463,20 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
539
463
|
imageData.data[redIndex + 2] = colorMap[2] * 255;
|
|
540
464
|
imageData.data[redIndex + 3] = colorMap[3] * 255;
|
|
541
465
|
}
|
|
542
|
-
}
|
|
543
|
-
|
|
466
|
+
}
|
|
544
467
|
|
|
468
|
+
// scale and stack spectrograms
|
|
545
469
|
createImageBitmap(imageData).then(function (renderer) {
|
|
546
|
-
return spectrCc.drawImage(renderer, 0, height * (1 - freqMax / freqFrom),
|
|
547
|
-
|
|
548
|
-
|
|
470
|
+
return spectrCc.drawImage(renderer, 0, height * (1 - freqMax / freqFrom),
|
|
471
|
+
// source x, y
|
|
472
|
+
width, height * (freqMax - freqMin) / freqFrom,
|
|
473
|
+
// source width, height
|
|
474
|
+
0, height * c,
|
|
475
|
+
// destination x, y
|
|
549
476
|
width, height // destination width, height
|
|
550
477
|
);
|
|
551
478
|
});
|
|
552
479
|
};
|
|
553
|
-
|
|
554
480
|
for (var c = 0; c < frequenciesData.length; c++) {
|
|
555
481
|
_loop(c);
|
|
556
482
|
}
|
|
@@ -561,46 +487,40 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
561
487
|
var fftSamples = this.fftSamples;
|
|
562
488
|
var buffer = this.buffer = this.wavesurfer.backend.buffer;
|
|
563
489
|
var channels = this.channels;
|
|
564
|
-
|
|
565
490
|
if (!buffer) {
|
|
566
491
|
this.fireEvent('error', 'Web Audio buffer is not available');
|
|
567
492
|
return;
|
|
568
|
-
}
|
|
569
|
-
|
|
493
|
+
}
|
|
570
494
|
|
|
495
|
+
// This may differ from file samplerate. Browser resamples audio.
|
|
571
496
|
var sampleRate = buffer.sampleRate;
|
|
572
497
|
var frequencies = [];
|
|
573
498
|
var noverlap = this.noverlap;
|
|
574
|
-
|
|
575
499
|
if (!noverlap) {
|
|
576
500
|
var uniqueSamplesPerPx = buffer.length / this.canvas.width;
|
|
577
501
|
noverlap = Math.max(0, Math.round(fftSamples - uniqueSamplesPerPx));
|
|
578
502
|
}
|
|
579
|
-
|
|
580
503
|
var fft = new _fft.default(fftSamples, sampleRate, this.windowFunc, this.alpha);
|
|
581
|
-
|
|
582
504
|
for (var c = 0; c < channels; c++) {
|
|
583
505
|
// for each channel
|
|
584
506
|
var channelData = buffer.getChannelData(c);
|
|
585
507
|
var channelFreq = [];
|
|
586
508
|
var currentOffset = 0;
|
|
587
|
-
|
|
588
509
|
while (currentOffset + fftSamples < channelData.length) {
|
|
589
510
|
var segment = channelData.slice(currentOffset, currentOffset + fftSamples);
|
|
590
511
|
var spectrum = fft.calculateSpectrum(segment);
|
|
591
512
|
var array = new Uint8Array(fftSamples / 2);
|
|
592
513
|
var j = void 0;
|
|
593
|
-
|
|
594
514
|
for (j = 0; j < fftSamples / 2; j++) {
|
|
595
515
|
array[j] = Math.max(-255, Math.log10(spectrum[j]) * 45);
|
|
596
516
|
}
|
|
597
|
-
|
|
598
|
-
|
|
517
|
+
channelFreq.push(array);
|
|
518
|
+
// channelFreq: [sample, freq]
|
|
599
519
|
|
|
600
520
|
currentOffset += fftSamples - noverlap;
|
|
601
521
|
}
|
|
602
|
-
|
|
603
|
-
|
|
522
|
+
frequencies.push(channelFreq);
|
|
523
|
+
// frequencies: [channel, sample, freq]
|
|
604
524
|
}
|
|
605
525
|
|
|
606
526
|
callback(frequencies, this);
|
|
@@ -609,7 +529,6 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
609
529
|
key: "loadFrequenciesData",
|
|
610
530
|
value: function loadFrequenciesData(url) {
|
|
611
531
|
var _this2 = this;
|
|
612
|
-
|
|
613
532
|
var request = this.util.fetchFile({
|
|
614
533
|
url: url
|
|
615
534
|
});
|
|
@@ -647,26 +566,26 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
647
566
|
var getMaxY = frequenciesHeight || 512;
|
|
648
567
|
var labelIndex = 5 * (getMaxY / 256);
|
|
649
568
|
var freqStart = this.frequencyMin;
|
|
650
|
-
var step = (this.frequencyMax - freqStart) / labelIndex;
|
|
569
|
+
var step = (this.frequencyMax - freqStart) / labelIndex;
|
|
651
570
|
|
|
571
|
+
// prepare canvas element for labels
|
|
652
572
|
var ctx = this.labelsEl.getContext('2d');
|
|
653
573
|
var dispScale = window.devicePixelRatio;
|
|
654
574
|
this.labelsEl.height = this.height * this.channels * dispScale;
|
|
655
575
|
this.labelsEl.width = bgWidth * dispScale;
|
|
656
576
|
ctx.scale(dispScale, dispScale);
|
|
657
|
-
|
|
658
577
|
if (!ctx) {
|
|
659
578
|
return;
|
|
660
579
|
}
|
|
661
|
-
|
|
662
580
|
for (var c = 0; c < this.channels; c++) {
|
|
663
581
|
// for each channel
|
|
664
582
|
// fill background
|
|
665
583
|
ctx.fillStyle = bgFill;
|
|
666
584
|
ctx.fillRect(0, c * getMaxY, bgWidth, (1 + c) * getMaxY);
|
|
667
585
|
ctx.fill();
|
|
668
|
-
var i = void 0;
|
|
586
|
+
var i = void 0;
|
|
669
587
|
|
|
588
|
+
// render labels
|
|
670
589
|
for (i = 0; i <= labelIndex; i++) {
|
|
671
590
|
ctx.textAlign = textAlign;
|
|
672
591
|
ctx.textBaseline = 'middle';
|
|
@@ -676,24 +595,23 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
676
595
|
var yLabelOffset = 2;
|
|
677
596
|
var x = 16;
|
|
678
597
|
var y = void 0;
|
|
679
|
-
|
|
680
598
|
if (i == 0) {
|
|
681
|
-
y = (1 + c) * getMaxY + i - 10;
|
|
682
|
-
|
|
599
|
+
y = (1 + c) * getMaxY + i - 10;
|
|
600
|
+
// unit label
|
|
683
601
|
ctx.fillStyle = textColorUnit;
|
|
684
602
|
ctx.font = fontSizeUnit + ' ' + fontType;
|
|
685
|
-
ctx.fillText(units, x + 24, y);
|
|
686
|
-
|
|
603
|
+
ctx.fillText(units, x + 24, y);
|
|
604
|
+
// freq label
|
|
687
605
|
ctx.fillStyle = textColorFreq;
|
|
688
606
|
ctx.font = fontSizeFreq + ' ' + fontType;
|
|
689
607
|
ctx.fillText(label, x, y);
|
|
690
608
|
} else {
|
|
691
|
-
y = (1 + c) * getMaxY - i * 50 + yLabelOffset;
|
|
692
|
-
|
|
609
|
+
y = (1 + c) * getMaxY - i * 50 + yLabelOffset;
|
|
610
|
+
// unit label
|
|
693
611
|
ctx.fillStyle = textColorUnit;
|
|
694
612
|
ctx.font = fontSizeUnit + ' ' + fontType;
|
|
695
|
-
ctx.fillText(units, x + 24, y);
|
|
696
|
-
|
|
613
|
+
ctx.fillText(units, x + 24, y);
|
|
614
|
+
// freq label
|
|
697
615
|
ctx.fillStyle = textColorFreq;
|
|
698
616
|
ctx.font = fontSizeFreq + ' ' + fontType;
|
|
699
617
|
ctx.fillText(label, x, y);
|
|
@@ -716,11 +634,9 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
716
634
|
var oldPiece = 1 / oldMatrix.length;
|
|
717
635
|
var newPiece = 1 / columnsNumber;
|
|
718
636
|
var i;
|
|
719
|
-
|
|
720
637
|
for (i = 0; i < columnsNumber; i++) {
|
|
721
638
|
var column = new Array(oldMatrix[0].length);
|
|
722
639
|
var j = void 0;
|
|
723
|
-
|
|
724
640
|
for (j = 0; j < oldMatrix.length; j++) {
|
|
725
641
|
var oldStart = j * oldPiece;
|
|
726
642
|
var oldEnd = oldStart + oldPiece;
|
|
@@ -729,30 +645,24 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
729
645
|
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
646
|
var k = void 0;
|
|
731
647
|
/* eslint-disable max-depth */
|
|
732
|
-
|
|
733
648
|
if (overlap > 0) {
|
|
734
649
|
for (k = 0; k < oldMatrix[0].length; k++) {
|
|
735
650
|
if (column[k] == null) {
|
|
736
651
|
column[k] = 0;
|
|
737
652
|
}
|
|
738
|
-
|
|
739
653
|
column[k] += overlap / newPiece * oldMatrix[j][k];
|
|
740
654
|
}
|
|
741
655
|
}
|
|
742
656
|
/* eslint-enable max-depth */
|
|
743
|
-
|
|
744
657
|
}
|
|
745
658
|
|
|
746
659
|
var intColumn = new Uint8Array(oldMatrix[0].length);
|
|
747
660
|
var m = void 0;
|
|
748
|
-
|
|
749
661
|
for (m = 0; m < oldMatrix[0].length; m++) {
|
|
750
662
|
intColumn[m] = column[m];
|
|
751
663
|
}
|
|
752
|
-
|
|
753
664
|
newMatrix.push(intColumn);
|
|
754
665
|
}
|
|
755
|
-
|
|
756
666
|
return newMatrix;
|
|
757
667
|
}
|
|
758
668
|
}], [{
|
|
@@ -779,10 +689,8 @@ var SpectrogramPlugin = /*#__PURE__*/function () {
|
|
|
779
689
|
};
|
|
780
690
|
}
|
|
781
691
|
}]);
|
|
782
|
-
|
|
783
692
|
return SpectrogramPlugin;
|
|
784
693
|
}();
|
|
785
|
-
|
|
786
694
|
exports["default"] = SpectrogramPlugin;
|
|
787
695
|
module.exports = exports.default;
|
|
788
696
|
|