wavesurfer.js 6.0.2 → 6.1.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.
Files changed (46) hide show
  1. package/CHANGES.md +22 -0
  2. package/README.md +1 -1
  3. package/dist/plugin/wavesurfer.cursor.js +5 -5
  4. package/dist/plugin/wavesurfer.cursor.js.map +1 -1
  5. package/dist/plugin/wavesurfer.cursor.min.js +2 -2
  6. package/dist/plugin/wavesurfer.cursor.min.js.map +1 -1
  7. package/dist/plugin/wavesurfer.elan.js +1 -1
  8. package/dist/plugin/wavesurfer.elan.min.js +1 -1
  9. package/dist/plugin/wavesurfer.markers.js +1 -1
  10. package/dist/plugin/wavesurfer.markers.min.js +1 -1
  11. package/dist/plugin/wavesurfer.mediasession.js +1 -3
  12. package/dist/plugin/wavesurfer.mediasession.js.map +1 -1
  13. package/dist/plugin/wavesurfer.mediasession.min.js +1 -1
  14. package/dist/plugin/wavesurfer.mediasession.min.js.map +1 -1
  15. package/dist/plugin/wavesurfer.microphone.js +1 -1
  16. package/dist/plugin/wavesurfer.microphone.min.js +1 -1
  17. package/dist/plugin/wavesurfer.minimap.js +24 -4
  18. package/dist/plugin/wavesurfer.minimap.js.map +1 -1
  19. package/dist/plugin/wavesurfer.minimap.min.js +2 -2
  20. package/dist/plugin/wavesurfer.minimap.min.js.map +1 -1
  21. package/dist/plugin/wavesurfer.playhead.js +1 -1
  22. package/dist/plugin/wavesurfer.playhead.min.js +1 -1
  23. package/dist/plugin/wavesurfer.regions.js +1 -1
  24. package/dist/plugin/wavesurfer.regions.min.js +1 -1
  25. package/dist/plugin/wavesurfer.spectrogram.js +28 -9
  26. package/dist/plugin/wavesurfer.spectrogram.js.map +1 -1
  27. package/dist/plugin/wavesurfer.spectrogram.min.js +2 -2
  28. package/dist/plugin/wavesurfer.spectrogram.min.js.map +1 -1
  29. package/dist/plugin/wavesurfer.timeline.js +5 -3
  30. package/dist/plugin/wavesurfer.timeline.js.map +1 -1
  31. package/dist/plugin/wavesurfer.timeline.min.js +2 -2
  32. package/dist/plugin/wavesurfer.timeline.min.js.map +1 -1
  33. package/dist/wavesurfer-html-init.js +1 -1
  34. package/dist/wavesurfer-html-init.min.js +1 -1
  35. package/dist/wavesurfer.js +33 -10
  36. package/dist/wavesurfer.js.map +1 -1
  37. package/dist/wavesurfer.min.js +2 -2
  38. package/dist/wavesurfer.min.js.map +1 -1
  39. package/package.json +10 -10
  40. package/src/plugin/cursor/index.js +4 -4
  41. package/src/plugin/mediasession/index.js +0 -2
  42. package/src/plugin/minimap/index.js +17 -2
  43. package/src/plugin/spectrogram/index.js +23 -10
  44. package/src/plugin/timeline/index.js +7 -1
  45. package/src/wavesurfer.js +4 -1
  46. package/src/webaudio.js +24 -7
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wavesurfer.js",
3
- "version": "6.0.2",
3
+ "version": "6.1.0",
4
4
  "description": "Interactive navigable audio visualization using Web Audio and Canvas",
5
5
  "main": "dist/wavesurfer.js",
6
6
  "directories": {
@@ -55,13 +55,13 @@
55
55
  "visualization"
56
56
  ],
57
57
  "devDependencies": {
58
- "@babel/core": "^7.17.5",
58
+ "@babel/core": "^7.17.8",
59
59
  "@babel/eslint-parser": "^7.17.0",
60
60
  "@babel/plugin-proposal-class-properties": "^7.16.7",
61
61
  "@babel/preset-env": "^7.16.11",
62
- "@babel/register": "^7.17.0",
62
+ "@babel/register": "^7.17.7",
63
63
  "@chiragrupani/karma-chromium-edge-launcher": "^2.2.1",
64
- "babel-loader": "^8.2.3",
64
+ "babel-loader": "^8.2.4",
65
65
  "babel-plugin-add-module-exports": "^1.0.4",
66
66
  "babel-plugin-istanbul": "^6.1.1",
67
67
  "babel-plugin-proxy": "^1.1.0",
@@ -74,22 +74,22 @@
74
74
  "esdoc-ecmascript-proposal-plugin": "^1.0.0",
75
75
  "esdoc-integrate-test-plugin": "^1.0.0",
76
76
  "esdoc-standard-plugin": "^1.0.0",
77
- "eslint": "^8.9.0",
77
+ "eslint": "^8.11.0",
78
78
  "htmlhint": "^1.1.2",
79
79
  "in-publish": "^2.0.1",
80
- "jasmine-core": "^4.0.0",
81
- "karma": "^6.3.16",
82
- "karma-chrome-launcher": "3.1.0",
80
+ "jasmine-core": "^4.0.1",
81
+ "karma": "^6.3.17",
82
+ "karma-chrome-launcher": "3.1.1",
83
83
  "karma-coverage": "^2.2.0",
84
84
  "karma-firefox-launcher": "^2.1.2",
85
85
  "karma-jasmine": "4.0.1",
86
86
  "karma-jasmine-matchers": "5.0.0",
87
87
  "karma-verbose-reporter": "0.0.8",
88
88
  "karma-webpack": "^5.0.0",
89
- "lint-staged": "^12.3.4",
89
+ "lint-staged": "^12.3.7",
90
90
  "load-script": "^2.0.0",
91
91
  "pre-commit": "^1.2.2",
92
- "webpack": "^5.69.1",
92
+ "webpack": "^5.70.0",
93
93
  "webpack-cli": "^4.9.2",
94
94
  "webpack-dev-server": "^4.7.4",
95
95
  "webpack-merge": "^5.8.0"
@@ -5,7 +5,7 @@
5
5
  * waveform
6
6
  * @property {string} width='1px' The width of the cursor
7
7
  * @property {string} color='black' The color of the cursor
8
- * @property {string} opacity='0.25' The opacity of the cursor
8
+ * @property {number|string} opacity='0.25' The opacity of the cursor
9
9
  * @property {string} style='solid' The border style of the cursor
10
10
  * @property {number} zIndex=3 The z-index of the cursor element
11
11
  * @property {object} customStyle An object with custom styles which are applied
@@ -13,7 +13,7 @@
13
13
  * @property {boolean} showTime=false Show the time on the cursor.
14
14
  * @property {object} customShowTimeStyle An object with custom styles which are
15
15
  * applied to the cursor time element.
16
- * @property {string} followCursorY=false Use `true` to make the time on
16
+ * @property {boolean} followCursorY=false Use `true` to make the time on
17
17
  * the cursor follow the x and the y-position of the mouse. Use `false` to make the
18
18
  * it only follow the x-position of the mouse.
19
19
  * @property {function} formatTimeCallback Formats the timestamp on the cursor.
@@ -237,9 +237,9 @@ export default class CursorPlugin {
237
237
  */
238
238
  destroy() {
239
239
  if (this.params.showTime) {
240
- this.cursor.parentNode.removeChild(this.showTime);
240
+ this.showTime.remove();
241
241
  }
242
- this.cursor.parentNode.removeChild(this.cursor);
242
+ this.cursor.remove();
243
243
  this.wrapper.removeEventListener('mousemove', this._onMousemove);
244
244
  if (this.params.hideOnBlur) {
245
245
  this.wrapper.removeEventListener('mouseenter', this._onMouseenter);
@@ -1,5 +1,3 @@
1
- /*global MediaMetadata*/
2
-
3
1
  /**
4
2
  * @typedef {Object} MediaSessionPluginParams
5
3
  * @property {MediaMetadata} metadata A MediaMetadata object: a representation
@@ -96,6 +96,7 @@ export default class MinimapPlugin {
96
96
  this.drawer = new ws.Drawer(this.params.container, this.params);
97
97
  this.wavesurfer = ws;
98
98
  this.util = ws.util;
99
+ this.cleared = false;
99
100
  this.renderEvent = 'redraw';
100
101
  this.overviewRegion = null;
101
102
  this.regionsPlugin = this.wavesurfer[this.params.regionsPluginName];
@@ -119,7 +120,7 @@ export default class MinimapPlugin {
119
120
  }
120
121
 
121
122
  if (this.regionsPlugin && this.params.showRegions) {
122
- this.regions();
123
+ this.drawRegions();
123
124
  }
124
125
  this.render();
125
126
  };
@@ -154,6 +155,18 @@ export default class MinimapPlugin {
154
155
  );
155
156
  }
156
157
  });
158
+ this._onLoading = percent => {
159
+ if (percent >= 100) {
160
+ this.cleared = false;
161
+ return;
162
+ }
163
+ if (this.cleared === true) {
164
+ return;
165
+ }
166
+ const len = this.drawer.getWidth();
167
+ this.drawer.drawPeaks([0], len, 0, len);
168
+ this.cleared = true;
169
+ };
157
170
  this._onZoom = e => {
158
171
  this.render();
159
172
  };
@@ -179,12 +192,13 @@ export default class MinimapPlugin {
179
192
  this.wavesurfer.un('scroll', this._onScroll);
180
193
  this.wavesurfer.un('audioprocess', this._onAudioprocess);
181
194
  this.wavesurfer.un('zoom', this._onZoom);
195
+ this.wavesurfer.un('loading', this._onLoading);
182
196
  this.drawer.destroy();
183
197
  this.overviewRegion = null;
184
198
  this.unAll();
185
199
  }
186
200
 
187
- regions() {
201
+ drawRegions() {
188
202
  this.regions = {};
189
203
 
190
204
  this.wavesurfer.on('region-created', region => {
@@ -267,6 +281,7 @@ export default class MinimapPlugin {
267
281
  window.addEventListener('orientationchange', this._onResize, true);
268
282
  this.wavesurfer.on('audioprocess', this._onAudioprocess);
269
283
  this.wavesurfer.on('seek', this._onSeek);
284
+ this.wavesurfer.on('loading', this._onLoading);
270
285
  if (this.params.showOverview) {
271
286
  this.wavesurfer.on('scroll', this._onScroll);
272
287
  this.wavesurfer.drawer.wrapper.addEventListener(
@@ -21,6 +21,9 @@ import FFT from './fft';
21
21
  * @property {number} pixelRatio=wavesurfer.params.pixelRatio to control the
22
22
  * size of the spectrogram in relation with its canvas. 1 = Draw on the whole
23
23
  * canvas. 2 = Draw on a quarter (1/2 the length and 1/2 the width)
24
+ * @property {number} frequencyMin=0 Min frequency to scale spectrogram.
25
+ * @property {number} frequencyMax=12000 Max frequency to scale spectrogram.
26
+ * Set this to samplerate/2 to draw whole range of spectrogram.
24
27
  * @property {?boolean} deferInit Set to true to manually call
25
28
  * `initPlugin('spectrogram')`
26
29
  * @property {?number[][]} colorMap A 256 long array of 4-element arrays.
@@ -132,6 +135,11 @@ export default class SpectrogramPlugin {
132
135
  this.splitChannels = params.splitChannels;
133
136
  this.channels = this.splitChannels ? ws.backend.buffer.numberOfChannels : 1;
134
137
 
138
+ // Getting file's original samplerate is difficult(#1248).
139
+ // So set 12kHz default to render like wavesurfer.js 5.x.
140
+ this.frequencyMin = params.frequencyMin || 0;
141
+ this.frequencyMax = params.frequencyMax || 12000;
142
+
135
143
  this.createWrapper();
136
144
  this.createCanvas();
137
145
  this.render();
@@ -261,6 +269,9 @@ export default class SpectrogramPlugin {
261
269
  const spectrCc = my.spectrCc;
262
270
  const height = my.height;
263
271
  const width = my.width;
272
+ const freqFrom = my.buffer.sampleRate / 2;
273
+ const freqMin = my.frequencyMin;
274
+ const freqMax = my.frequencyMax;
264
275
 
265
276
  if (!spectrCc) {
266
277
  return;
@@ -268,7 +279,7 @@ export default class SpectrogramPlugin {
268
279
 
269
280
  for (let c = 0; c < frequenciesData.length; c++) { // for each channel
270
281
  const pixels = my.resample(frequenciesData[c]);
271
- const imageData = spectrCc.createImageData(width, height);
282
+ const imageData = new ImageData(width, height);
272
283
 
273
284
  for (let i = 0; i < pixels.length; i++) {
274
285
  for (let j = 0; j < pixels[i].length; j++) {
@@ -281,8 +292,15 @@ export default class SpectrogramPlugin {
281
292
  }
282
293
  }
283
294
 
284
- // stack spectrograms
285
- spectrCc.putImageData(imageData, 0, height * c);
295
+ // scale and stack spectrograms
296
+ createImageBitmap(imageData).then(renderer =>
297
+ spectrCc.drawImage(renderer,
298
+ 0, height * (1 - freqMax / freqFrom), // source x, y
299
+ width, height * (freqMax - freqMin) / freqFrom, // source width, height
300
+ 0, height * c, // destination x, y
301
+ width, height // destination width, height
302
+ )
303
+ );
286
304
  }
287
305
  }
288
306
 
@@ -381,10 +399,8 @@ export default class SpectrogramPlugin {
381
399
  const bgWidth = 55;
382
400
  const getMaxY = frequenciesHeight || 512;
383
401
  const labelIndex = 5 * (getMaxY / 256);
384
- const freqStart = 0;
385
- const step =
386
- (this.wavesurfer.backend.ac.sampleRate / 2 - freqStart) /
387
- labelIndex;
402
+ const freqStart = this.frequencyMin;
403
+ const step = (this.frequencyMax - freqStart) / labelIndex;
388
404
 
389
405
  // prepare canvas element for labels
390
406
  const ctx = this.labelsEl.getContext('2d');
@@ -408,9 +424,6 @@ export default class SpectrogramPlugin {
408
424
  ctx.textBaseline = 'middle';
409
425
 
410
426
  const freq = freqStart + step * i;
411
- const index = Math.round(
412
- (freq / (this.sampleRate / 2)) * this.fftSamples
413
- );
414
427
  const label = this.freqType(freq);
415
428
  const units = this.unitType(freq);
416
429
  const yLabelOffset = 2;
@@ -371,7 +371,13 @@ export default class TimelinePlugin {
371
371
  // build an array of position data with index, second and pixel data,
372
372
  // this is then used multiple times below
373
373
  const positioning = [];
374
- for (i = 0; i < totalSeconds / timeInterval; i++) {
374
+
375
+ // render until end in case we have a negative offset
376
+ const renderSeconds = (this.params.offset < 0)
377
+ ? totalSeconds - this.params.offset
378
+ : totalSeconds;
379
+
380
+ for (i = 0; i < renderSeconds / timeInterval; i++) {
375
381
  positioning.push([i, curSeconds, curPixel]);
376
382
  curSeconds += timeInterval;
377
383
  curPixel += pixelsPerSecond * timeInterval;
package/src/wavesurfer.js CHANGED
@@ -505,7 +505,10 @@ export default class WaveSurfer extends util.Observer {
505
505
  !this.params.scrollParent
506
506
  ) {
507
507
  prevWidth = this.drawer.wrapper.clientWidth;
508
- this.drawer.fireEvent('redraw');
508
+ if (prevWidth) {
509
+ // redraw only if waveform container is rendered and has a width
510
+ this.drawer.fireEvent('redraw');
511
+ }
509
512
  }
510
513
  },
511
514
  typeof this.params.responsive === 'number'
package/src/webaudio.js CHANGED
@@ -145,6 +145,10 @@ export default class WebAudio extends util.Observer {
145
145
  this.state = null;
146
146
  /** @private */
147
147
  this.explicitDuration = params.duration;
148
+ /** @private */
149
+ this.sinkStreamDestination = null;
150
+ /** @private */
151
+ this.sinkAudioElement = null;
148
152
  /**
149
153
  * Boolean indicating if the backend was destroyed.
150
154
  */
@@ -298,19 +302,24 @@ export default class WebAudio extends util.Observer {
298
302
  * output. Here we create an HTMLAudioElement, connect the
299
303
  * webaudio stream to that element and setSinkId there.
300
304
  */
301
- let audio = new window.Audio();
302
- if (!audio.setSinkId) {
305
+ if (!this.sinkAudioElement) {
306
+ this.sinkAudioElement = new window.Audio();
307
+ // autoplay is necessary since we're not invoking .play()
308
+ this.sinkAudioElement.autoplay = true;
309
+ }
310
+ if (!this.sinkAudioElement.setSinkId) {
303
311
  return Promise.reject(
304
312
  new Error('setSinkId is not supported in your browser')
305
313
  );
306
314
  }
307
- audio.autoplay = true;
308
- const dest = this.ac.createMediaStreamDestination();
315
+ if (!this.sinkStreamDestination) {
316
+ this.sinkStreamDestination = this.ac.createMediaStreamDestination();
317
+ }
309
318
  this.gainNode.disconnect();
310
- this.gainNode.connect(dest);
311
- audio.srcObject = dest.stream;
319
+ this.gainNode.connect(this.sinkStreamDestination);
320
+ this.sinkAudioElement.srcObject = this.sinkStreamDestination.stream;
312
321
 
313
- return audio.setSinkId(deviceId);
322
+ return this.sinkAudioElement.setSinkId(deviceId);
314
323
  } else {
315
324
  return Promise.reject(new Error('Invalid deviceId: ' + deviceId));
316
325
  }
@@ -541,6 +550,14 @@ export default class WebAudio extends util.Observer {
541
550
  // clear the offlineAudioContext
542
551
  window.WaveSurferOfflineAudioContext = null;
543
552
  }
553
+
554
+ // disconnect resources used by setSinkId
555
+ if (this.sinkStreamDestination) {
556
+ this.sinkAudioElement.pause();
557
+ this.sinkAudioElement.srcObject = null;
558
+ this.sinkStreamDestination.disconnect();
559
+ this.sinkStreamDestination = null;
560
+ }
544
561
  }
545
562
  /**
546
563
  * This is called when wavesurfer is destroyed