wavesurfer.js 6.5.1 → 7.0.0-alpha.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 (132) hide show
  1. package/.eslintrc.json +24 -0
  2. package/.prettierrc +8 -0
  3. package/README.md +26 -117
  4. package/dist/base-plugin.d.ts +10 -0
  5. package/dist/base-plugin.js +13 -0
  6. package/dist/decoder.d.ts +10 -0
  7. package/dist/decoder.js +43 -0
  8. package/dist/event-emitter.d.ts +11 -0
  9. package/dist/event-emitter.js +21 -0
  10. package/dist/fetcher.d.ts +4 -0
  11. package/dist/fetcher.js +17 -0
  12. package/dist/index.d.ts +92 -0
  13. package/dist/index.js +166 -0
  14. package/dist/player-webaudio.d.ts +8 -0
  15. package/dist/player-webaudio.js +31 -0
  16. package/dist/player.d.ts +16 -0
  17. package/dist/player.js +40 -0
  18. package/dist/plugins/regions.d.ts +48 -0
  19. package/dist/plugins/regions.js +183 -0
  20. package/dist/renderer.d.ts +34 -0
  21. package/dist/renderer.js +193 -0
  22. package/dist/timer.d.ts +10 -0
  23. package/dist/timer.js +17 -0
  24. package/examples/audio.ogg +0 -0
  25. package/examples/bars.js +19 -0
  26. package/examples/basic.js +8 -0
  27. package/examples/gradient.js +28 -0
  28. package/examples/regions.js +63 -0
  29. package/examples/video.js +19 -0
  30. package/examples/webaudio.js +14 -0
  31. package/package.json +19 -99
  32. package/src/base-plugin.ts +20 -0
  33. package/src/decoder.ts +41 -0
  34. package/src/event-emitter.ts +35 -0
  35. package/src/fetcher.ts +7 -0
  36. package/src/index.ts +252 -0
  37. package/src/player-webaudio.ts +34 -0
  38. package/src/player.ts +50 -0
  39. package/src/plugins/regions.ts +240 -0
  40. package/src/renderer.ts +250 -0
  41. package/src/timer.ts +27 -0
  42. package/tsconfig.json +105 -0
  43. package/tutorial/index.html +47 -0
  44. package/tutorial/src/editor.js +70 -0
  45. package/tutorial/src/init.js +66 -0
  46. package/tutorial/src/url.js +25 -0
  47. package/tutorial/style.css +211 -0
  48. package/yarn-error.log +1049 -0
  49. package/LICENSE +0 -29
  50. package/dist/plugin/wavesurfer.cursor.js +0 -418
  51. package/dist/plugin/wavesurfer.cursor.js.map +0 -1
  52. package/dist/plugin/wavesurfer.cursor.min.js +0 -7
  53. package/dist/plugin/wavesurfer.cursor.min.js.map +0 -1
  54. package/dist/plugin/wavesurfer.elan.js +0 -381
  55. package/dist/plugin/wavesurfer.elan.js.map +0 -1
  56. package/dist/plugin/wavesurfer.elan.min.js +0 -7
  57. package/dist/plugin/wavesurfer.elan.min.js.map +0 -1
  58. package/dist/plugin/wavesurfer.markers.js +0 -520
  59. package/dist/plugin/wavesurfer.markers.js.map +0 -1
  60. package/dist/plugin/wavesurfer.markers.min.js +0 -7
  61. package/dist/plugin/wavesurfer.markers.min.js.map +0 -1
  62. package/dist/plugin/wavesurfer.mediasession.js +0 -181
  63. package/dist/plugin/wavesurfer.mediasession.js.map +0 -1
  64. package/dist/plugin/wavesurfer.mediasession.min.js +0 -7
  65. package/dist/plugin/wavesurfer.mediasession.min.js.map +0 -1
  66. package/dist/plugin/wavesurfer.microphone.js +0 -466
  67. package/dist/plugin/wavesurfer.microphone.js.map +0 -1
  68. package/dist/plugin/wavesurfer.microphone.min.js +0 -7
  69. package/dist/plugin/wavesurfer.microphone.min.js.map +0 -1
  70. package/dist/plugin/wavesurfer.minimap.js +0 -440
  71. package/dist/plugin/wavesurfer.minimap.js.map +0 -1
  72. package/dist/plugin/wavesurfer.minimap.min.js +0 -7
  73. package/dist/plugin/wavesurfer.minimap.min.js.map +0 -1
  74. package/dist/plugin/wavesurfer.playhead.js +0 -300
  75. package/dist/plugin/wavesurfer.playhead.js.map +0 -1
  76. package/dist/plugin/wavesurfer.playhead.min.js +0 -7
  77. package/dist/plugin/wavesurfer.playhead.min.js.map +0 -1
  78. package/dist/plugin/wavesurfer.regions.js +0 -1335
  79. package/dist/plugin/wavesurfer.regions.js.map +0 -1
  80. package/dist/plugin/wavesurfer.regions.min.js +0 -7
  81. package/dist/plugin/wavesurfer.regions.min.js.map +0 -1
  82. package/dist/plugin/wavesurfer.spectrogram.js +0 -736
  83. package/dist/plugin/wavesurfer.spectrogram.js.map +0 -1
  84. package/dist/plugin/wavesurfer.spectrogram.min.js +0 -7
  85. package/dist/plugin/wavesurfer.spectrogram.min.js.map +0 -1
  86. package/dist/plugin/wavesurfer.timeline.js +0 -649
  87. package/dist/plugin/wavesurfer.timeline.js.map +0 -1
  88. package/dist/plugin/wavesurfer.timeline.min.js +0 -7
  89. package/dist/plugin/wavesurfer.timeline.min.js.map +0 -1
  90. package/dist/wavesurfer-html-init.js +0 -381
  91. package/dist/wavesurfer-html-init.js.map +0 -1
  92. package/dist/wavesurfer-html-init.min.js +0 -7
  93. package/dist/wavesurfer-html-init.min.js.map +0 -1
  94. package/dist/wavesurfer.js +0 -6145
  95. package/dist/wavesurfer.js.map +0 -1
  96. package/dist/wavesurfer.min.js +0 -7
  97. package/dist/wavesurfer.min.js.map +0 -1
  98. package/src/drawer.canvasentry.js +0 -427
  99. package/src/drawer.js +0 -413
  100. package/src/drawer.multicanvas.js +0 -617
  101. package/src/html-init.js +0 -241
  102. package/src/mediaelement-webaudio.js +0 -77
  103. package/src/mediaelement.js +0 -443
  104. package/src/peakcache.js +0 -127
  105. package/src/plugin/cursor/index.js +0 -350
  106. package/src/plugin/elan/index.js +0 -307
  107. package/src/plugin/markers/index.js +0 -436
  108. package/src/plugin/mediasession/index.js +0 -94
  109. package/src/plugin/microphone/index.js +0 -415
  110. package/src/plugin/minimap/index.js +0 -403
  111. package/src/plugin/playhead/index.js +0 -226
  112. package/src/plugin/regions/index.js +0 -478
  113. package/src/plugin/regions/region.js +0 -875
  114. package/src/plugin/spectrogram/fft.js +0 -222
  115. package/src/plugin/spectrogram/index.js +0 -522
  116. package/src/plugin/timeline/index.js +0 -600
  117. package/src/util/absMax.js +0 -16
  118. package/src/util/clamp.js +0 -11
  119. package/src/util/fetch.js +0 -222
  120. package/src/util/frame.js +0 -13
  121. package/src/util/get-id.js +0 -22
  122. package/src/util/index.js +0 -14
  123. package/src/util/max.js +0 -16
  124. package/src/util/min.js +0 -16
  125. package/src/util/observer.js +0 -144
  126. package/src/util/orientation.js +0 -98
  127. package/src/util/prevent-click.js +0 -19
  128. package/src/util/request-animation-frame.js +0 -15
  129. package/src/util/silence-mode.js +0 -42
  130. package/src/util/style.js +0 -16
  131. package/src/wavesurfer.js +0 -1797
  132. package/src/webaudio.js +0 -779
@@ -1,427 +0,0 @@
1
- /**
2
- * @since 3.0.0
3
- */
4
-
5
- import style from './util/style';
6
- import getId from './util/get-id';
7
-
8
- /**
9
- * The `CanvasEntry` class represents an element consisting of a wave `canvas`
10
- * and an (optional) progress wave `canvas`.
11
- *
12
- * The `MultiCanvas` renderer uses one or more `CanvasEntry` instances to
13
- * render a waveform, depending on the zoom level.
14
- */
15
- export default class CanvasEntry {
16
- constructor() {
17
- /**
18
- * The wave node
19
- *
20
- * @type {HTMLCanvasElement}
21
- */
22
- this.wave = null;
23
- /**
24
- * The wave canvas rendering context
25
- *
26
- * @type {CanvasRenderingContext2D}
27
- */
28
- this.waveCtx = null;
29
- /**
30
- * The (optional) progress wave node
31
- *
32
- * @type {HTMLCanvasElement}
33
- */
34
- this.progress = null;
35
- /**
36
- * The (optional) progress wave canvas rendering context
37
- *
38
- * @type {CanvasRenderingContext2D}
39
- */
40
- this.progressCtx = null;
41
- /**
42
- * Start of the area the canvas should render, between 0 and 1
43
- *
44
- * @type {number}
45
- */
46
- this.start = 0;
47
- /**
48
- * End of the area the canvas should render, between 0 and 1
49
- *
50
- * @type {number}
51
- */
52
- this.end = 1;
53
- /**
54
- * Unique identifier for this entry
55
- *
56
- * @type {string}
57
- */
58
- this.id = getId(
59
- typeof this.constructor.name !== 'undefined'
60
- ? this.constructor.name.toLowerCase() + '_'
61
- : 'canvasentry_'
62
- );
63
- /**
64
- * Canvas 2d context attributes
65
- *
66
- * @type {object}
67
- */
68
- this.canvasContextAttributes = {};
69
- }
70
-
71
- /**
72
- * Store the wave canvas element and create the 2D rendering context
73
- *
74
- * @param {HTMLCanvasElement} element The wave `canvas` element.
75
- */
76
- initWave(element) {
77
- this.wave = element;
78
- this.waveCtx = this.wave.getContext('2d', this.canvasContextAttributes);
79
- }
80
-
81
- /**
82
- * Store the progress wave canvas element and create the 2D rendering
83
- * context
84
- *
85
- * @param {HTMLCanvasElement} element The progress wave `canvas` element.
86
- */
87
- initProgress(element) {
88
- this.progress = element;
89
- this.progressCtx = this.progress.getContext(
90
- '2d',
91
- this.canvasContextAttributes
92
- );
93
- }
94
-
95
- /**
96
- * Update the dimensions
97
- *
98
- * @param {number} elementWidth Width of the entry
99
- * @param {number} totalWidth Total width of the multi canvas renderer
100
- * @param {number} width The new width of the element
101
- * @param {number} height The new height of the element
102
- */
103
- updateDimensions(elementWidth, totalWidth, width, height) {
104
- // where the canvas starts and ends in the waveform, represented as a
105
- // decimal between 0 and 1
106
- this.start = this.wave.offsetLeft / totalWidth || 0;
107
- this.end = this.start + elementWidth / totalWidth;
108
-
109
- // set wave canvas dimensions
110
- this.wave.width = width;
111
- this.wave.height = height;
112
- let elementSize = { width: elementWidth + 'px' };
113
- style(this.wave, elementSize);
114
-
115
- if (this.hasProgressCanvas) {
116
- // set progress canvas dimensions
117
- this.progress.width = width;
118
- this.progress.height = height;
119
- style(this.progress, elementSize);
120
- }
121
- }
122
-
123
- /**
124
- * Clear the wave and progress rendering contexts
125
- */
126
- clearWave() {
127
- // wave
128
- this.waveCtx.clearRect(
129
- 0,
130
- 0,
131
- this.waveCtx.canvas.width,
132
- this.waveCtx.canvas.height
133
- );
134
-
135
- // progress
136
- if (this.hasProgressCanvas) {
137
- this.progressCtx.clearRect(
138
- 0,
139
- 0,
140
- this.progressCtx.canvas.width,
141
- this.progressCtx.canvas.height
142
- );
143
- }
144
- }
145
-
146
- /**
147
- * Set the fill styles for wave and progress
148
- * @param {string|string[]} waveColor Fill color for the wave canvas,
149
- * or an array of colors to apply as a gradient
150
- * @param {?string|string[]} progressColor Fill color for the progress canvas,
151
- * or an array of colors to apply as a gradient
152
- */
153
- setFillStyles(waveColor, progressColor) {
154
- this.waveCtx.fillStyle = this.getFillStyle(this.waveCtx, waveColor);
155
-
156
- if (this.hasProgressCanvas) {
157
- this.progressCtx.fillStyle = this.getFillStyle(this.progressCtx, progressColor);
158
- }
159
- }
160
-
161
- /**
162
- * Utility function to handle wave color arguments
163
- *
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
- *
168
- * @since 6.0.0
169
- * @param {CanvasRenderingContext2D} ctx Rendering context of target canvas
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
175
- */
176
- getFillStyle(ctx, color) {
177
- if (typeof color == 'string' || color instanceof CanvasGradient) {
178
- return color;
179
- }
180
-
181
- const waveGradient = ctx.createLinearGradient(0, 0, 0, ctx.canvas.height);
182
- color.forEach((value, index) => waveGradient.addColorStop((index / color.length), value));
183
-
184
- return waveGradient;
185
- }
186
-
187
- /**
188
- * Set the canvas transforms for wave and progress
189
- *
190
- * @param {boolean} vertical Whether to render vertically
191
- */
192
- applyCanvasTransforms(vertical) {
193
- if (vertical) {
194
- // Reflect the waveform across the line y = -x
195
- this.waveCtx.setTransform(0, 1, 1, 0, 0, 0);
196
-
197
- if (this.hasProgressCanvas) {
198
- this.progressCtx.setTransform(0, 1, 1, 0, 0, 0);
199
- }
200
- }
201
- }
202
-
203
- /**
204
- * Draw a rectangle for wave and progress
205
- *
206
- * @param {number} x X start position
207
- * @param {number} y Y start position
208
- * @param {number} width Width of the rectangle
209
- * @param {number} height Height of the rectangle
210
- * @param {number} radius Radius of the rectangle
211
- */
212
- fillRects(x, y, width, height, radius) {
213
- this.fillRectToContext(this.waveCtx, x, y, width, height, radius);
214
-
215
- if (this.hasProgressCanvas) {
216
- this.fillRectToContext(
217
- this.progressCtx,
218
- x,
219
- y,
220
- width,
221
- height,
222
- radius
223
- );
224
- }
225
- }
226
-
227
- /**
228
- * Draw the actual rectangle on a `canvas` element
229
- *
230
- * @param {CanvasRenderingContext2D} ctx Rendering context of target canvas
231
- * @param {number} x X start position
232
- * @param {number} y Y start position
233
- * @param {number} width Width of the rectangle
234
- * @param {number} height Height of the rectangle
235
- * @param {number} radius Radius of the rectangle
236
- */
237
- fillRectToContext(ctx, x, y, width, height, radius) {
238
- if (!ctx) {
239
- return;
240
- }
241
-
242
- if (radius) {
243
- this.drawRoundedRect(ctx, x, y, width, height, radius);
244
- } else {
245
- ctx.fillRect(x, y, width, height);
246
- }
247
- }
248
-
249
- /**
250
- * Draw a rounded rectangle on Canvas
251
- *
252
- * @param {CanvasRenderingContext2D} ctx Canvas context
253
- * @param {number} x X-position of the rectangle
254
- * @param {number} y Y-position of the rectangle
255
- * @param {number} width Width of the rectangle
256
- * @param {number} height Height of the rectangle
257
- * @param {number} radius Radius of the rectangle
258
- *
259
- * @return {void}
260
- * @example drawRoundedRect(ctx, 50, 50, 5, 10, 3)
261
- */
262
- drawRoundedRect(ctx, x, y, width, height, radius) {
263
- if (height === 0) {
264
- return;
265
- }
266
- // peaks are float values from -1 to 1. Use absolute height values in
267
- // order to correctly calculate rounded rectangle coordinates
268
- if (height < 0) {
269
- height *= -1;
270
- y -= height;
271
- }
272
- ctx.beginPath();
273
- ctx.moveTo(x + radius, y);
274
- ctx.lineTo(x + width - radius, y);
275
- ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
276
- ctx.lineTo(x + width, y + height - radius);
277
- ctx.quadraticCurveTo(
278
- x + width,
279
- y + height,
280
- x + width - radius,
281
- y + height
282
- );
283
- ctx.lineTo(x + radius, y + height);
284
- ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
285
- ctx.lineTo(x, y + radius);
286
- ctx.quadraticCurveTo(x, y, x + radius, y);
287
- ctx.closePath();
288
- ctx.fill();
289
- }
290
-
291
- /**
292
- * Render the actual wave and progress lines
293
- *
294
- * @param {number[]} peaks Array with peaks data
295
- * @param {number} absmax Maximum peak value (absolute)
296
- * @param {number} halfH Half the height of the waveform
297
- * @param {number} offsetY Offset to the top
298
- * @param {number} start The x-offset of the beginning of the area that
299
- * should be rendered
300
- * @param {number} end The x-offset of the end of the area that
301
- * should be rendered
302
- */
303
- drawLines(peaks, absmax, halfH, offsetY, start, end) {
304
- this.drawLineToContext(
305
- this.waveCtx,
306
- peaks,
307
- absmax,
308
- halfH,
309
- offsetY,
310
- start,
311
- end
312
- );
313
-
314
- if (this.hasProgressCanvas) {
315
- this.drawLineToContext(
316
- this.progressCtx,
317
- peaks,
318
- absmax,
319
- halfH,
320
- offsetY,
321
- start,
322
- end
323
- );
324
- }
325
- }
326
-
327
- /**
328
- * Render the actual waveform line on a `canvas` element
329
- *
330
- * @param {CanvasRenderingContext2D} ctx Rendering context of target canvas
331
- * @param {number[]} peaks Array with peaks data
332
- * @param {number} absmax Maximum peak value (absolute)
333
- * @param {number} halfH Half the height of the waveform
334
- * @param {number} offsetY Offset to the top
335
- * @param {number} start The x-offset of the beginning of the area that
336
- * should be rendered
337
- * @param {number} end The x-offset of the end of the area that
338
- * should be rendered
339
- */
340
- drawLineToContext(ctx, peaks, absmax, halfH, offsetY, start, end) {
341
- if (!ctx) {
342
- return;
343
- }
344
-
345
- const length = peaks.length / 2;
346
- const first = Math.round(length * this.start);
347
-
348
- // use one more peak value to make sure we join peaks at ends -- unless,
349
- // of course, this is the last canvas
350
- const last = Math.round(length * this.end) + 1;
351
-
352
- const canvasStart = first;
353
- const canvasEnd = last;
354
- const scale = this.wave.width / (canvasEnd - canvasStart - 1);
355
-
356
- // optimization
357
- const halfOffset = halfH + offsetY;
358
- const absmaxHalf = absmax / halfH;
359
-
360
- ctx.beginPath();
361
- ctx.moveTo((canvasStart - first) * scale, halfOffset);
362
-
363
- ctx.lineTo(
364
- (canvasStart - first) * scale,
365
- halfOffset - Math.round((peaks[2 * canvasStart] || 0) / absmaxHalf)
366
- );
367
-
368
- let i, peak, h;
369
- for (i = canvasStart; i < canvasEnd; i++) {
370
- peak = peaks[2 * i] || 0;
371
- h = Math.round(peak / absmaxHalf);
372
- ctx.lineTo((i - first) * scale + this.halfPixel, halfOffset - h);
373
- }
374
-
375
- // draw the bottom edge going backwards, to make a single
376
- // closed hull to fill
377
- let j = canvasEnd - 1;
378
- for (j; j >= canvasStart; j--) {
379
- peak = peaks[2 * j + 1] || 0;
380
- h = Math.round(peak / absmaxHalf);
381
- ctx.lineTo((j - first) * scale + this.halfPixel, halfOffset - h);
382
- }
383
-
384
- ctx.lineTo(
385
- (canvasStart - first) * scale,
386
- halfOffset -
387
- Math.round((peaks[2 * canvasStart + 1] || 0) / absmaxHalf)
388
- );
389
-
390
- ctx.closePath();
391
- ctx.fill();
392
- }
393
-
394
- /**
395
- * Destroys this entry
396
- */
397
- destroy() {
398
- this.waveCtx = null;
399
- this.wave = null;
400
-
401
- this.progressCtx = null;
402
- this.progress = null;
403
- }
404
-
405
- /**
406
- * Return image data of the wave `canvas` element
407
- *
408
- * When using a `type` of `'blob'`, this will return a `Promise` that
409
- * resolves with a `Blob` instance.
410
- *
411
- * @param {string} format='image/png' An optional value of a format type.
412
- * @param {number} quality=0.92 An optional value between 0 and 1.
413
- * @param {string} type='dataURL' Either 'dataURL' or 'blob'.
414
- * @return {string|Promise} When using the default `'dataURL'` `type` this
415
- * returns a data URL. When using the `'blob'` `type` this returns a
416
- * `Promise` that resolves with a `Blob` instance.
417
- */
418
- getImage(format, quality, type) {
419
- if (type === 'blob') {
420
- return new Promise(resolve => {
421
- this.wave.toBlob(resolve, format, quality);
422
- });
423
- } else if (type === 'dataURL') {
424
- return this.wave.toDataURL(format, quality);
425
- }
426
- }
427
- }