wavesurfer.js 6.0.1 → 6.0.4

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 (41) hide show
  1. package/CHANGES.md +29 -5
  2. package/dist/plugin/wavesurfer.cursor.js +5 -5
  3. package/dist/plugin/wavesurfer.cursor.js.map +1 -1
  4. package/dist/plugin/wavesurfer.cursor.min.js +2 -2
  5. package/dist/plugin/wavesurfer.cursor.min.js.map +1 -1
  6. package/dist/plugin/wavesurfer.elan.js +1 -1
  7. package/dist/plugin/wavesurfer.elan.min.js +1 -1
  8. package/dist/plugin/wavesurfer.markers.js +1 -1
  9. package/dist/plugin/wavesurfer.markers.min.js +1 -1
  10. package/dist/plugin/wavesurfer.mediasession.js +1 -1
  11. package/dist/plugin/wavesurfer.mediasession.min.js +1 -1
  12. package/dist/plugin/wavesurfer.microphone.js +1 -1
  13. package/dist/plugin/wavesurfer.microphone.min.js +1 -1
  14. package/dist/plugin/wavesurfer.minimap.js +1 -1
  15. package/dist/plugin/wavesurfer.minimap.min.js +1 -1
  16. package/dist/plugin/wavesurfer.playhead.js +1 -1
  17. package/dist/plugin/wavesurfer.playhead.min.js +1 -1
  18. package/dist/plugin/wavesurfer.regions.js +1 -1
  19. package/dist/plugin/wavesurfer.regions.js.map +1 -1
  20. package/dist/plugin/wavesurfer.regions.min.js +1 -1
  21. package/dist/plugin/wavesurfer.regions.min.js.map +1 -1
  22. package/dist/plugin/wavesurfer.spectrogram.js +28 -9
  23. package/dist/plugin/wavesurfer.spectrogram.js.map +1 -1
  24. package/dist/plugin/wavesurfer.spectrogram.min.js +2 -2
  25. package/dist/plugin/wavesurfer.spectrogram.min.js.map +1 -1
  26. package/dist/plugin/wavesurfer.timeline.js +5 -3
  27. package/dist/plugin/wavesurfer.timeline.js.map +1 -1
  28. package/dist/plugin/wavesurfer.timeline.min.js +2 -2
  29. package/dist/plugin/wavesurfer.timeline.min.js.map +1 -1
  30. package/dist/wavesurfer-html-init.js +1 -1
  31. package/dist/wavesurfer-html-init.min.js +1 -1
  32. package/dist/wavesurfer.js +11 -8
  33. package/dist/wavesurfer.js.map +1 -1
  34. package/dist/wavesurfer.min.js +2 -2
  35. package/dist/wavesurfer.min.js.map +1 -1
  36. package/package.json +7 -7
  37. package/src/drawer.canvasentry.js +9 -6
  38. package/src/plugin/cursor/index.js +4 -4
  39. package/src/plugin/regions/index.js +1 -1
  40. package/src/plugin/spectrogram/index.js +23 -10
  41. package/src/plugin/timeline/index.js +7 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wavesurfer.js",
3
- "version": "6.0.1",
3
+ "version": "6.0.4",
4
4
  "description": "Interactive navigable audio visualization using Web Audio and Canvas",
5
5
  "main": "dist/wavesurfer.js",
6
6
  "directories": {
@@ -55,7 +55,7 @@
55
55
  "visualization"
56
56
  ],
57
57
  "devDependencies": {
58
- "@babel/core": "^7.17.2",
58
+ "@babel/core": "^7.17.5",
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",
@@ -74,11 +74,11 @@
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.10.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",
80
+ "jasmine-core": "^4.0.1",
81
+ "karma": "^6.3.17",
82
82
  "karma-chrome-launcher": "3.1.0",
83
83
  "karma-coverage": "^2.2.0",
84
84
  "karma-firefox-launcher": "^2.1.2",
@@ -86,10 +86,10 @@
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.3",
89
+ "lint-staged": "^12.3.5",
90
90
  "load-script": "^2.0.0",
91
91
  "pre-commit": "^1.2.2",
92
- "webpack": "^5.68.0",
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"
@@ -161,17 +161,20 @@ export default class CanvasEntry {
161
161
  /**
162
162
  * Utility function to handle wave color arguments
163
163
  *
164
- * When the color argument type is a string, it will be returned as is.
165
- * Otherwise, it will be treated as an array, and a canvas gradient will
166
- * be returned
164
+ * When the color argument type is a string or CanvasGradient instance,
165
+ * it will be returned as is. Otherwise, it will be treated as an array,
166
+ * and a new CanvasGradient will be returned
167
167
  *
168
168
  * @since 6.0.0
169
169
  * @param {CanvasRenderingContext2D} ctx Rendering context of target canvas
170
- * @param {string|string[]} color Fill color for the wave canvas, or an array of colors to apply as a gradient
171
- * @returns {string|CanvasGradient} Returns a string fillstyle value, or a canvas gradient
170
+ * @param {string|string[]|CanvasGradient} color Either a single fill color
171
+ * for the wave canvas, an existing CanvasGradient instance, or an array
172
+ * of colors to apply as a gradient
173
+ * @returns {string|CanvasGradient} Returns a string fillstyle value, or a
174
+ * canvas gradient
172
175
  */
173
176
  getFillStyle(ctx, color) {
174
- if (typeof color == 'string') {
177
+ if (typeof color == 'string' || color instanceof CanvasGradient) {
175
178
  return color;
176
179
  }
177
180
 
@@ -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);
@@ -10,7 +10,7 @@
10
10
  * @property {?number} snapToGridInterval Snap the regions to a grid of the specified multiples in seconds
11
11
  * @property {?number} snapToGridOffset Shift the snap-to-grid by the specified seconds. May also be negative.
12
12
  * @property {?boolean} deferInit Set to true to manually call
13
- * @property {number[]} maxRegions Maximum number of regions that may be created by the user at one time.
13
+ * @property {number} maxRegions Maximum number of regions that may be created by the user at one time.
14
14
  * `initPlugin('regions')`
15
15
  * @property {function} formatTimeCallback Allows custom formating for region tooltip.
16
16
  * @property {?number} edgeScrollWidth='5% from container edges' Optional width for edgeScroll to start
@@ -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;