wavesurfer.js 5.0.1 → 5.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 (38) hide show
  1. package/CHANGES.md +11 -1
  2. package/dist/plugin/wavesurfer.cursor.js +1 -1
  3. package/dist/plugin/wavesurfer.cursor.min.js +1 -1
  4. package/dist/plugin/wavesurfer.elan.js +1 -1
  5. package/dist/plugin/wavesurfer.elan.min.js +1 -1
  6. package/dist/plugin/wavesurfer.markers.js +16 -6
  7. package/dist/plugin/wavesurfer.markers.js.map +1 -1
  8. package/dist/plugin/wavesurfer.markers.min.js +2 -2
  9. package/dist/plugin/wavesurfer.markers.min.js.map +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 +5 -3
  19. package/dist/plugin/wavesurfer.regions.js.map +1 -1
  20. package/dist/plugin/wavesurfer.regions.min.js +2 -2
  21. package/dist/plugin/wavesurfer.regions.min.js.map +1 -1
  22. package/dist/plugin/wavesurfer.spectrogram.js +66 -60
  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 +17 -5
  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 +2 -2
  33. package/dist/wavesurfer.min.js +2 -2
  34. package/package.json +15 -15
  35. package/src/plugin/markers/index.js +13 -5
  36. package/src/plugin/regions/region.js +2 -2
  37. package/src/plugin/spectrogram/index.js +65 -61
  38. package/src/plugin/timeline/index.js +20 -11
@@ -254,30 +254,32 @@ export default class SpectrogramPlugin {
254
254
  const width = my.width;
255
255
  const pixels = my.resample(frequenciesData);
256
256
  const heightFactor = my.buffer ? 2 / my.buffer.numberOfChannels : 1;
257
- const imageData = spectrCc.createImageData(width, height);
258
- let i;
259
- let j;
260
- let k;
261
-
262
- for (i = 0; i < pixels.length; i++) {
263
- for (j = 0; j < pixels[i].length; j++) {
264
- const colorMap = my.colorMap[pixels[i][j]];
265
- /* eslint-disable max-depth */
266
- for (k = 0; k < heightFactor; k++) {
267
- let y = height - j * heightFactor;
268
- if (heightFactor === 2 && k === 1) {
269
- y--;
257
+ if (spectrCc) {
258
+ const imageData = spectrCc.createImageData(width, height);
259
+ let i;
260
+ let j;
261
+ let k;
262
+
263
+ for (i = 0; i < pixels.length; i++) {
264
+ for (j = 0; j < pixels[i].length; j++) {
265
+ const colorMap = my.colorMap[pixels[i][j]];
266
+ /* eslint-disable max-depth */
267
+ for (k = 0; k < heightFactor; k++) {
268
+ let y = height - j * heightFactor;
269
+ if (heightFactor === 2 && k === 1) {
270
+ y--;
271
+ }
272
+ const redIndex = y * (width * 4) + i * 4;
273
+ imageData.data[redIndex] = colorMap[0] * 255;
274
+ imageData.data[redIndex + 1] = colorMap[1] * 255;
275
+ imageData.data[redIndex + 2] = colorMap[2] * 255;
276
+ imageData.data[redIndex + 3] = colorMap[3] * 255;
270
277
  }
271
- const redIndex = y * (width * 4) + i * 4;
272
- imageData.data[redIndex] = colorMap[0] * 255;
273
- imageData.data[redIndex + 1] = colorMap[1] * 255;
274
- imageData.data[redIndex + 2] = colorMap[2] * 255;
275
- imageData.data[redIndex + 3] = colorMap[3] * 255;
278
+ /* eslint-enable max-depth */
276
279
  }
277
- /* eslint-enable max-depth */
278
280
  }
281
+ spectrCc.putImageData(imageData, 0, 0);
279
282
  }
280
- spectrCc.putImageData(imageData, 0, 0);
281
283
  }
282
284
 
283
285
  getFrequencies(callback) {
@@ -378,47 +380,49 @@ export default class SpectrogramPlugin {
378
380
  this.labelsEl.height = this.height;
379
381
  this.labelsEl.width = bgWidth;
380
382
 
381
- // fill background
382
- ctx.fillStyle = bgFill;
383
- ctx.fillRect(0, 0, bgWidth, getMaxY);
384
- ctx.fill();
385
- let i;
386
-
387
- // render labels
388
- for (i = 0; i <= labelIndex; i++) {
389
- ctx.textAlign = textAlign;
390
- ctx.textBaseline = 'middle';
391
-
392
- const freq = freqStart + step * i;
393
- const index = Math.round(
394
- (freq / (this.sampleRate / 2)) * this.fftSamples
395
- );
396
- const label = this.freqType(freq);
397
- const units = this.unitType(freq);
398
- const yLabelOffset = 2;
399
- const x = 16;
400
- let y;
401
-
402
- if (i == 0) {
403
- y = getMaxY + i - 10;
404
- // unit label
405
- ctx.fillStyle = textColorUnit;
406
- ctx.font = fontSizeUnit + ' ' + fontType;
407
- ctx.fillText(units, x + 24, y);
408
- // freq label
409
- ctx.fillStyle = textColorFreq;
410
- ctx.font = fontSizeFreq + ' ' + fontType;
411
- ctx.fillText(label, x, y);
412
- } else {
413
- y = getMaxY - i * 50 + yLabelOffset;
414
- // unit label
415
- ctx.fillStyle = textColorUnit;
416
- ctx.font = fontSizeUnit + ' ' + fontType;
417
- ctx.fillText(units, x + 24, y);
418
- // freq label
419
- ctx.fillStyle = textColorFreq;
420
- ctx.font = fontSizeFreq + ' ' + fontType;
421
- ctx.fillText(label, x, y);
383
+ if (ctx) {
384
+ // fill background
385
+ ctx.fillStyle = bgFill;
386
+ ctx.fillRect(0, 0, bgWidth, getMaxY);
387
+ ctx.fill();
388
+ let i;
389
+
390
+ // render labels
391
+ for (i = 0; i <= labelIndex; i++) {
392
+ ctx.textAlign = textAlign;
393
+ ctx.textBaseline = 'middle';
394
+
395
+ const freq = freqStart + step * i;
396
+ const index = Math.round(
397
+ (freq / (this.sampleRate / 2)) * this.fftSamples
398
+ );
399
+ const label = this.freqType(freq);
400
+ const units = this.unitType(freq);
401
+ const yLabelOffset = 2;
402
+ const x = 16;
403
+ let y;
404
+
405
+ if (i == 0) {
406
+ y = getMaxY + i - 10;
407
+ // unit label
408
+ ctx.fillStyle = textColorUnit;
409
+ ctx.font = fontSizeUnit + ' ' + fontType;
410
+ ctx.fillText(units, x + 24, y);
411
+ // freq label
412
+ ctx.fillStyle = textColorFreq;
413
+ ctx.font = fontSizeFreq + ' ' + fontType;
414
+ ctx.fillText(label, x, y);
415
+ } else {
416
+ y = getMaxY - i * 50 + yLabelOffset;
417
+ // unit label
418
+ ctx.fillStyle = textColorUnit;
419
+ ctx.font = fontSizeUnit + ' ' + fontType;
420
+ ctx.fillText(units, x + 24, y);
421
+ // freq label
422
+ ctx.fillStyle = textColorFreq;
423
+ ctx.font = fontSizeFreq + ' ' + fontType;
424
+ ctx.fillText(label, x, y);
425
+ }
422
426
  }
423
427
  }
424
428
  }
@@ -434,7 +434,10 @@ export default class TimelinePlugin {
434
434
  */
435
435
  setFillStyles(fillStyle) {
436
436
  this.canvases.forEach(canvas => {
437
- canvas.getContext('2d').fillStyle = fillStyle;
437
+ const context = canvas.getContext('2d');
438
+ if (context) {
439
+ context.fillStyle = fillStyle;
440
+ }
438
441
  });
439
442
  }
440
443
 
@@ -445,7 +448,10 @@ export default class TimelinePlugin {
445
448
  */
446
449
  setFonts(font) {
447
450
  this.canvases.forEach(canvas => {
448
- canvas.getContext('2d').font = font;
451
+ const context = canvas.getContext('2d');
452
+ if (context) {
453
+ context.font = font;
454
+ }
449
455
  });
450
456
  }
451
457
 
@@ -471,14 +477,17 @@ export default class TimelinePlugin {
471
477
  };
472
478
 
473
479
  if (intersection.x1 < intersection.x2) {
474
- canvas
475
- .getContext('2d')
476
- .fillRect(
477
- intersection.x1 - leftOffset,
478
- intersection.y1,
479
- intersection.x2 - intersection.x1,
480
- intersection.y2 - intersection.y1
481
- );
480
+ const context = canvas
481
+ .getContext('2d');
482
+ if (context) {
483
+ context
484
+ .fillRect(
485
+ intersection.x1 - leftOffset,
486
+ intersection.y1,
487
+ intersection.x2 - intersection.x1,
488
+ intersection.y2 - intersection.y1
489
+ );
490
+ }
482
491
  }
483
492
  });
484
493
  }
@@ -502,7 +511,7 @@ export default class TimelinePlugin {
502
511
  return;
503
512
  }
504
513
 
505
- if (xOffset + canvasWidth > x) {
514
+ if (xOffset + canvasWidth > x && context) {
506
515
  textWidth = context.measureText(text).width;
507
516
  context.fillText(text, x - xOffset, y);
508
517
  }