-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathrenderer.ts
More file actions
819 lines (698 loc) · 25.6 KB
/
Copy pathrenderer.ts
File metadata and controls
819 lines (698 loc) · 25.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import { makeDraggable } from './draggable.js'
import EventEmitter from './event-emitter.js'
import type { WaveSurferOptions } from './wavesurfer.js'
type RendererEvents = {
click: [relativeX: number, relativeY: number]
dblclick: [relativeX: number, relativeY: number]
drag: [relativeX: number]
dragstart: [relativeX: number]
dragend: [relativeX: number]
scroll: [relativeStart: number, relativeEnd: number, scrollLeft: number, scrollRight: number]
render: []
rendered: []
}
class Renderer extends EventEmitter<RendererEvents> {
private static MAX_CANVAS_WIDTH = 8000
private static MAX_NODES = 10
private options: WaveSurferOptions
private parent: HTMLElement
private container: HTMLElement
private scrollContainer: HTMLElement
private wrapper: HTMLElement
private canvasWrapper: HTMLElement
private progressWrapper: HTMLElement
private cursor: HTMLElement
private timeouts: Array<() => void> = []
private isScrollable = false
private audioData: AudioBuffer | null = null
private resizeObserver: ResizeObserver | null = null
private lastContainerWidth = 0
private isDragging = false
private subscriptions: (() => void)[] = []
private unsubscribeOnScroll: (() => void)[] = []
private dragUnsubscribe: (() => void) | null = null
constructor(options: WaveSurferOptions, audioElement?: HTMLElement) {
super()
this.subscriptions = []
this.options = options
const parent = this.parentFromOptionsContainer(options.container)
this.parent = parent
const [div, shadow] = this.initHtml()
parent.appendChild(div)
this.container = div
this.scrollContainer = shadow.querySelector('.scroll') as HTMLElement
this.wrapper = shadow.querySelector('.wrapper') as HTMLElement
this.canvasWrapper = shadow.querySelector('.canvases') as HTMLElement
this.progressWrapper = shadow.querySelector('.progress') as HTMLElement
this.cursor = shadow.querySelector('.cursor') as HTMLElement
if (audioElement) {
shadow.appendChild(audioElement)
}
this.initEvents()
}
private parentFromOptionsContainer(container: WaveSurferOptions['container']) {
let parent
if (typeof container === 'string') {
parent = document.querySelector(container) satisfies HTMLElement | null
} else if (container instanceof HTMLElement) {
parent = container
}
if (!parent) {
throw new Error('Container not found')
}
return parent
}
private initEvents() {
const getClickPosition = (e: MouseEvent): [number, number] => {
const rect = this.wrapper.getBoundingClientRect()
const x = e.clientX - rect.left
const y = e.clientY - rect.top
const relativeX = x / rect.width
const relativeY = y / rect.height
return [relativeX, relativeY]
}
// Add a click listener
this.wrapper.addEventListener('click', (e) => {
const [x, y] = getClickPosition(e)
this.emit('click', x, y)
})
// Add a double click listener
this.wrapper.addEventListener('dblclick', (e) => {
const [x, y] = getClickPosition(e)
this.emit('dblclick', x, y)
})
// Drag
if (this.options.dragToSeek === true || typeof this.options.dragToSeek === 'object') {
this.initDrag()
}
// Add a scroll listener
this.scrollContainer.addEventListener('scroll', () => {
const { scrollLeft, scrollWidth, clientWidth } = this.scrollContainer
const startX = scrollLeft / scrollWidth
const endX = (scrollLeft + clientWidth) / scrollWidth
this.emit('scroll', startX, endX, scrollLeft, scrollLeft + clientWidth)
})
// Re-render the waveform on container resize
if (typeof ResizeObserver === 'function') {
const delay = this.createDelay(100)
this.resizeObserver = new ResizeObserver(() => {
delay()
.then(() => this.onContainerResize())
.catch(() => undefined)
})
this.resizeObserver.observe(this.scrollContainer)
}
}
private onContainerResize() {
const width = this.parent.clientWidth
if (width === this.lastContainerWidth && this.options.height !== 'auto') return
this.lastContainerWidth = width
this.reRender()
}
private initDrag() {
// Don't initialize drag if it's already set up
if (this.dragUnsubscribe) return
this.dragUnsubscribe = makeDraggable(
this.wrapper,
// On drag
(_, __, x) => {
this.emit('drag', Math.max(0, Math.min(1, x / this.wrapper.getBoundingClientRect().width)))
},
// On start drag
(x) => {
this.isDragging = true
this.emit('dragstart', Math.max(0, Math.min(1, x / this.wrapper.getBoundingClientRect().width)))
},
// On end drag
(x) => {
this.isDragging = false
this.emit('dragend', Math.max(0, Math.min(1, x / this.wrapper.getBoundingClientRect().width)))
},
)
this.subscriptions.push(this.dragUnsubscribe)
}
private getHeight(
optionsHeight?: WaveSurferOptions['height'],
optionsSplitChannel?: WaveSurferOptions['splitChannels'],
): number {
const defaultHeight = 128
const numberOfChannels = this.audioData?.numberOfChannels || 1
if (optionsHeight == null) return defaultHeight
if (!isNaN(Number(optionsHeight))) return Number(optionsHeight)
if (optionsHeight === 'auto') {
const height = this.parent.clientHeight || defaultHeight
if (optionsSplitChannel?.every((channel) => !channel.overlay)) return height / numberOfChannels
return height
}
return defaultHeight
}
private initHtml(): [HTMLElement, ShadowRoot] {
const div = document.createElement('div')
const shadow = div.attachShadow({ mode: 'open' })
const cspNonce =
this.options.cspNonce && typeof this.options.cspNonce === 'string' ? this.options.cspNonce.replace(/"/g, '') : ''
shadow.innerHTML = `
<style${cspNonce ? ` nonce="${cspNonce}"` : ''}>
:host {
user-select: none;
min-width: 1px;
}
:host audio {
display: block;
width: 100%;
}
:host .scroll {
overflow-x: auto;
overflow-y: hidden;
width: 100%;
position: relative;
}
:host .noScrollbar {
scrollbar-color: transparent;
scrollbar-width: none;
}
:host .noScrollbar::-webkit-scrollbar {
display: none;
-webkit-appearance: none;
}
:host .wrapper {
position: relative;
overflow: visible;
z-index: 2;
}
:host .canvases {
min-height: ${this.getHeight(this.options.height, this.options.splitChannels)}px;
}
:host .canvases > div {
position: relative;
}
:host canvas {
display: block;
position: absolute;
top: 0;
image-rendering: pixelated;
}
:host .progress {
pointer-events: none;
position: absolute;
z-index: 2;
top: 0;
left: 0;
width: 0;
height: 100%;
overflow: hidden;
}
:host .progress > div {
position: relative;
}
:host .cursor {
pointer-events: none;
position: absolute;
z-index: 5;
top: 0;
left: 0;
height: 100%;
border-radius: 2px;
}
</style>
<div class="scroll" part="scroll">
<div class="wrapper" part="wrapper">
<div class="canvases" part="canvases"></div>
<div class="progress" part="progress"></div>
<div class="cursor" part="cursor"></div>
</div>
</div>
`
return [div, shadow]
}
/** Wavesurfer itself calls this method. Do not call it manually. */
setOptions(options: WaveSurferOptions) {
if (this.options.container !== options.container) {
const newParent = this.parentFromOptionsContainer(options.container)
newParent.appendChild(this.container)
this.parent = newParent
}
if (options.dragToSeek === true || typeof this.options.dragToSeek === 'object') {
this.initDrag()
}
this.options = options
// Re-render the waveform
this.reRender()
}
getWrapper(): HTMLElement {
return this.wrapper
}
getWidth(): number {
return this.scrollContainer.clientWidth
}
getScroll(): number {
return this.scrollContainer.scrollLeft
}
setScroll(pixels: number) {
this.scrollContainer.scrollLeft = pixels
}
setScrollPercentage(percent: number) {
const { scrollWidth } = this.scrollContainer
const scrollStart = scrollWidth * percent
this.setScroll(scrollStart)
}
destroy() {
this.subscriptions.forEach((unsubscribe) => unsubscribe())
this.container.remove()
if (this.resizeObserver) {
this.resizeObserver.disconnect()
this.resizeObserver = null
}
this.unsubscribeOnScroll?.forEach((unsubscribe) => unsubscribe())
this.unsubscribeOnScroll = []
}
private createDelay(delayMs = 10): () => Promise<void> {
let timeout: ReturnType<typeof setTimeout> | undefined
let reject: (() => void) | undefined
const onClear = () => {
if (timeout) clearTimeout(timeout)
if (reject) reject()
}
this.timeouts.push(onClear)
return () => {
return new Promise((resolveFn, rejectFn) => {
onClear()
reject = rejectFn
timeout = setTimeout(() => {
timeout = undefined
reject = undefined
resolveFn()
}, delayMs)
})
}
}
// Convert array of color values to linear gradient
private convertColorValues(color?: WaveSurferOptions['waveColor']): string | CanvasGradient {
if (!Array.isArray(color)) return color || ''
if (color.length === 0) return '#999' // Return default color for empty array
if (color.length < 2) return color[0] || ''
const canvasElement = document.createElement('canvas')
const ctx = canvasElement.getContext('2d') as CanvasRenderingContext2D
const gradientHeight = canvasElement.height * (window.devicePixelRatio || 1)
const gradient = ctx.createLinearGradient(0, 0, 0, gradientHeight)
const colorStopPercentage = 1 / (color.length - 1)
color.forEach((color, index) => {
const offset = index * colorStopPercentage
gradient.addColorStop(offset, color)
})
return gradient
}
private getPixelRatio() {
return Math.max(1, window.devicePixelRatio || 1)
}
private renderBarWaveform(
channelData: Array<Float32Array | number[]>,
options: WaveSurferOptions,
ctx: CanvasRenderingContext2D,
vScale: number,
) {
const topChannel = channelData[0]
const bottomChannel = channelData[1] || channelData[0]
const length = topChannel.length
const { width, height } = ctx.canvas
const halfHeight = height / 2
const pixelRatio = this.getPixelRatio()
const barWidth = options.barWidth ? options.barWidth * pixelRatio : 1
const barGap = options.barGap ? options.barGap * pixelRatio : options.barWidth ? barWidth / 2 : 0
const barRadius = options.barRadius || 0
const barIndexScale = width / (barWidth + barGap) / length
const rectFn = barRadius && 'roundRect' in ctx ? 'roundRect' : 'rect'
ctx.beginPath()
let prevX = 0
let maxTop = 0
let maxBottom = 0
for (let i = 0; i <= length; i++) {
const x = Math.round(i * barIndexScale)
if (x > prevX) {
const topBarHeight = Math.round(maxTop * halfHeight * vScale)
const bottomBarHeight = Math.round(maxBottom * halfHeight * vScale)
const barHeight = topBarHeight + bottomBarHeight || 1
// Vertical alignment
let y = halfHeight - topBarHeight
if (options.barAlign === 'top') {
y = 0
} else if (options.barAlign === 'bottom') {
y = height - barHeight
}
ctx[rectFn](prevX * (barWidth + barGap), y, barWidth, barHeight, barRadius)
prevX = x
maxTop = 0
maxBottom = 0
}
const magnitudeTop = Math.abs(topChannel[i] || 0)
const magnitudeBottom = Math.abs(bottomChannel[i] || 0)
if (magnitudeTop > maxTop) maxTop = magnitudeTop
if (magnitudeBottom > maxBottom) maxBottom = magnitudeBottom
}
ctx.fill()
ctx.closePath()
}
private renderLineWaveform(
channelData: Array<Float32Array | number[]>,
_options: WaveSurferOptions,
ctx: CanvasRenderingContext2D,
vScale: number,
) {
const drawChannel = (index: number) => {
const channel = channelData[index] || channelData[0]
const length = channel.length
const { height } = ctx.canvas
const halfHeight = height / 2
const hScale = ctx.canvas.width / length
ctx.moveTo(0, halfHeight)
let prevX = 0
let max = 0
for (let i = 0; i <= length; i++) {
const x = Math.round(i * hScale)
if (x > prevX) {
const h = Math.round(max * halfHeight * vScale) || 1
const y = halfHeight + h * (index === 0 ? -1 : 1)
ctx.lineTo(prevX, y)
prevX = x
max = 0
}
const value = Math.abs(channel[i] || 0)
if (value > max) max = value
}
ctx.lineTo(prevX, halfHeight)
}
ctx.beginPath()
drawChannel(0)
drawChannel(1)
ctx.fill()
ctx.closePath()
}
private renderWaveform(
channelData: Array<Float32Array | number[]>,
options: WaveSurferOptions,
ctx: CanvasRenderingContext2D,
) {
ctx.fillStyle = this.convertColorValues(options.waveColor)
// Custom rendering function
if (options.renderFunction) {
options.renderFunction(channelData, ctx)
return
}
// Vertical scaling
let vScale = options.barHeight || 1
if (options.normalize) {
const max = Array.from(channelData[0]).reduce((max, value) => Math.max(max, Math.abs(value)), 0)
vScale = max ? vScale / max : vScale
}
// Render waveform as bars
if (options.barWidth || options.barGap || options.barAlign) {
this.renderBarWaveform(channelData, options, ctx, vScale)
return
}
// Render waveform as a polyline
this.renderLineWaveform(channelData, options, ctx, vScale)
}
private renderSingleCanvas(
data: Array<Float32Array | number[]>,
options: WaveSurferOptions,
width: number,
height: number,
offset: number,
canvasContainer: HTMLElement,
progressContainer: HTMLElement,
) {
const pixelRatio = this.getPixelRatio()
const canvas = document.createElement('canvas')
canvas.width = Math.round(width * pixelRatio)
canvas.height = Math.round(height * pixelRatio)
canvas.style.width = `${width}px`
canvas.style.height = `${height}px`
canvas.style.left = `${Math.round(offset)}px`
canvasContainer.appendChild(canvas)
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
this.renderWaveform(data, options, ctx)
// Draw a progress canvas
if (canvas.width > 0 && canvas.height > 0) {
const progressCanvas = canvas.cloneNode() as HTMLCanvasElement
const progressCtx = progressCanvas.getContext('2d') as CanvasRenderingContext2D
progressCtx.drawImage(canvas, 0, 0)
// Set the composition method to draw only where the waveform is drawn
progressCtx.globalCompositeOperation = 'source-in'
progressCtx.fillStyle = this.convertColorValues(options.progressColor)
// This rectangle acts as a mask thanks to the composition method
progressCtx.fillRect(0, 0, canvas.width, canvas.height)
progressContainer.appendChild(progressCanvas)
}
}
private renderMultiCanvas(
channelData: Array<Float32Array | number[]>,
options: WaveSurferOptions,
width: number,
height: number,
canvasContainer: HTMLElement,
progressContainer: HTMLElement,
) {
const pixelRatio = this.getPixelRatio()
const { clientWidth } = this.scrollContainer
const totalWidth = width / pixelRatio
let singleCanvasWidth = Math.min(Renderer.MAX_CANVAS_WIDTH, clientWidth, totalWidth)
let drawnIndexes: Record<number, boolean> = {}
// Adjust width to avoid gaps between canvases when using bars
if (options.barWidth || options.barGap) {
const barWidth = options.barWidth || 0.5
const barGap = options.barGap || barWidth / 2
const totalBarWidth = barWidth + barGap
if (singleCanvasWidth % totalBarWidth !== 0) {
singleCanvasWidth = Math.floor(singleCanvasWidth / totalBarWidth) * totalBarWidth
}
}
// Nothing to render
if (singleCanvasWidth === 0) return
// Draw a single canvas
const draw = (index: number) => {
if (index < 0 || index >= numCanvases) return
if (drawnIndexes[index]) return
drawnIndexes[index] = true
const offset = index * singleCanvasWidth
let clampedWidth = Math.min(totalWidth - offset, singleCanvasWidth)
// Clamp the width to the bar grid to avoid empty canvases at the end
if (options.barWidth || options.barGap) {
const barWidth = options.barWidth || 0.5
const barGap = options.barGap || barWidth / 2
const totalBarWidth = barWidth + barGap
clampedWidth = Math.floor(clampedWidth / totalBarWidth) * totalBarWidth
}
if (clampedWidth <= 0) return
const data = channelData.map((channel) => {
const start = Math.floor((offset / totalWidth) * channel.length)
const end = Math.floor(((offset + clampedWidth) / totalWidth) * channel.length)
return channel.slice(start, end)
})
this.renderSingleCanvas(data, options, clampedWidth, height, offset, canvasContainer, progressContainer)
}
// Clear canvases to avoid too many DOM nodes
const clearCanvases = () => {
if (Object.keys(drawnIndexes).length > Renderer.MAX_NODES) {
canvasContainer.innerHTML = ''
progressContainer.innerHTML = ''
drawnIndexes = {}
}
}
// Calculate how many canvases to render
const numCanvases = Math.ceil(totalWidth / singleCanvasWidth)
// Render all canvases if the waveform doesn't scroll
if (!this.isScrollable) {
for (let i = 0; i < numCanvases; i++) {
draw(i)
}
return
}
// Lazy rendering
const viewPosition = this.scrollContainer.scrollLeft / totalWidth
const startCanvas = Math.floor(viewPosition * numCanvases)
// Draw the canvases in the viewport first
draw(startCanvas - 1)
draw(startCanvas)
draw(startCanvas + 1)
// Subscribe to the scroll event to draw additional canvases
if (numCanvases > 1) {
const unsubscribe = this.on('scroll', () => {
const { scrollLeft } = this.scrollContainer
const canvasIndex = Math.floor((scrollLeft / totalWidth) * numCanvases)
clearCanvases()
draw(canvasIndex - 1)
draw(canvasIndex)
draw(canvasIndex + 1)
})
this.unsubscribeOnScroll.push(unsubscribe)
}
}
private renderChannel(
channelData: Array<Float32Array | number[]>,
{ overlay, ...options }: WaveSurferOptions & { overlay?: boolean },
width: number,
channelIndex: number,
) {
// A container for canvases
const canvasContainer = document.createElement('div')
const height = this.getHeight(options.height, options.splitChannels)
canvasContainer.style.height = `${height}px`
if (overlay && channelIndex > 0) {
canvasContainer.style.marginTop = `-${height}px`
}
this.canvasWrapper.style.minHeight = `${height}px`
this.canvasWrapper.appendChild(canvasContainer)
// A container for progress canvases
const progressContainer = canvasContainer.cloneNode() as HTMLElement
this.progressWrapper.appendChild(progressContainer)
// Render the waveform
this.renderMultiCanvas(channelData, options, width, height, canvasContainer, progressContainer)
}
async render(audioData: AudioBuffer) {
// Clear previous timeouts
this.timeouts.forEach((clear) => clear())
this.timeouts = []
// Clear the canvases
this.canvasWrapper.innerHTML = ''
this.progressWrapper.innerHTML = ''
// Width
if (this.options.width != null) {
this.scrollContainer.style.width =
typeof this.options.width === 'number' ? `${this.options.width}px` : this.options.width
}
// Determine the width of the waveform
const pixelRatio = this.getPixelRatio()
const parentWidth = this.scrollContainer.clientWidth
const scrollWidth = Math.ceil(audioData.duration * (this.options.minPxPerSec || 0))
// Whether the container should scroll
this.isScrollable = scrollWidth > parentWidth
const useParentWidth = this.options.fillParent && !this.isScrollable
// Width of the waveform in pixels
const width = (useParentWidth ? parentWidth : scrollWidth) * pixelRatio
// Set the width of the wrapper
this.wrapper.style.width = useParentWidth ? '100%' : `${scrollWidth}px`
// Set additional styles
this.scrollContainer.style.overflowX = this.isScrollable ? 'auto' : 'hidden'
this.scrollContainer.classList.toggle('noScrollbar', !!this.options.hideScrollbar)
this.cursor.style.backgroundColor = `${this.options.cursorColor || this.options.progressColor}`
this.cursor.style.width = `${this.options.cursorWidth}px`
this.audioData = audioData
this.emit('render')
// Render the waveform
if (this.options.splitChannels) {
// Render a waveform for each channel
for (let i = 0; i < audioData.numberOfChannels; i++) {
const options = { ...this.options, ...this.options.splitChannels?.[i] }
this.renderChannel([audioData.getChannelData(i)], options, width, i)
}
} else {
// Render a single waveform for the first two channels (left and right)
const channels = [audioData.getChannelData(0)]
if (audioData.numberOfChannels > 1) channels.push(audioData.getChannelData(1))
this.renderChannel(channels, this.options, width, 0)
}
// Must be emitted asynchronously for backward compatibility
Promise.resolve().then(() => this.emit('rendered'))
}
reRender() {
this.unsubscribeOnScroll.forEach((unsubscribe) => unsubscribe())
this.unsubscribeOnScroll = []
// Return if the waveform has not been rendered yet
if (!this.audioData) return
// Remember the current cursor position
const { scrollWidth } = this.scrollContainer
const { right: before } = this.progressWrapper.getBoundingClientRect()
// Re-render the waveform
this.render(this.audioData)
// Adjust the scroll position so that the cursor stays in the same place
if (this.isScrollable && scrollWidth !== this.scrollContainer.scrollWidth) {
const { right: after } = this.progressWrapper.getBoundingClientRect()
let delta = after - before
// to limit compounding floating-point drift
// we need to round to the half px furthest from 0
delta *= 2
delta = delta < 0 ? Math.floor(delta) : Math.ceil(delta)
delta /= 2
this.scrollContainer.scrollLeft += delta
}
}
zoom(minPxPerSec: number) {
this.options.minPxPerSec = minPxPerSec
this.reRender()
}
private scrollIntoView(progress: number, isPlaying = false) {
const { scrollLeft, scrollWidth, clientWidth } = this.scrollContainer
const progressWidth = progress * scrollWidth
const startEdge = scrollLeft
const endEdge = scrollLeft + clientWidth
const middle = clientWidth / 2
if (this.isDragging) {
// Scroll when dragging close to the edge of the viewport
const minGap = 30
if (progressWidth + minGap > endEdge) {
this.scrollContainer.scrollLeft += minGap
} else if (progressWidth - minGap < startEdge) {
this.scrollContainer.scrollLeft -= minGap
}
} else {
if (progressWidth < startEdge || progressWidth > endEdge) {
this.scrollContainer.scrollLeft = progressWidth - (this.options.autoCenter ? middle : 0)
}
// Keep the cursor centered when playing
const center = progressWidth - scrollLeft - middle
if (isPlaying && this.options.autoCenter && center > 0) {
this.scrollContainer.scrollLeft += Math.min(center, 10)
}
}
// Emit the scroll event
{
const newScroll = this.scrollContainer.scrollLeft
const startX = newScroll / scrollWidth
const endX = (newScroll + clientWidth) / scrollWidth
this.emit('scroll', startX, endX, newScroll, newScroll + clientWidth)
}
}
renderProgress(progress: number, isPlaying?: boolean) {
if (isNaN(progress)) return
const percents = progress * 100
this.canvasWrapper.style.clipPath = `polygon(${percents}% 0%, 100% 0%, 100% 100%, ${percents}% 100%)`
this.progressWrapper.style.width = `${percents}%`
this.cursor.style.left = `${percents}%`
this.cursor.style.transform = this.options.cursorWidth
? `translateX(-${progress * this.options.cursorWidth}px)`
: ''
if (this.isScrollable && this.options.autoScroll) {
this.scrollIntoView(progress, isPlaying)
}
}
async exportImage(format: string, quality: number, type: 'dataURL' | 'blob'): Promise<string[] | Blob[]> {
const canvases = this.canvasWrapper.querySelectorAll('canvas')
if (!canvases.length) {
throw new Error('No waveform data')
}
// Data URLs
if (type === 'dataURL') {
const images = Array.from(canvases).map((canvas) => canvas.toDataURL(format, quality))
return Promise.resolve(images)
}
// Blobs
return Promise.all(
Array.from(canvases).map((canvas) => {
return new Promise<Blob>((resolve, reject) => {
canvas.toBlob(
(blob) => {
if (blob) {
resolve(blob)
} else {
reject(new Error('Could not export image'))
}
},
format,
quality,
)
})
}),
)
}
}
export default Renderer