wavesurfer.js 7.0.0-alpha.15 → 7.0.0-alpha.17
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.
- package/dist/base-plugin.d.ts +2 -1
- package/dist/base-plugin.js +1 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +7 -0
- package/dist/player.d.ts +1 -0
- package/dist/player.js +3 -0
- package/dist/plugins/minimap.d.ts +25 -0
- package/dist/plugins/minimap.js +52 -0
- package/dist/plugins/multitrack.d.ts +3 -1
- package/dist/plugins/multitrack.js +24 -18
- package/dist/plugins/regions.d.ts +3 -3
- package/dist/plugins/regions.js +24 -19
- package/dist/plugins/timeline.d.ts +3 -3
- package/dist/plugins/timeline.js +7 -7
- package/dist/renderer.d.ts +4 -1
- package/dist/renderer.js +14 -3
- package/dist/wavesurfer.Minimap.min.js +1 -0
- package/dist/wavesurfer.Multitrack.min.js +1 -1
- package/dist/wavesurfer.Regions.min.js +1 -1
- package/dist/wavesurfer.Timeline.min.js +1 -1
- package/dist/wavesurfer.min.js +1 -1
- package/package.json +1 -1
package/dist/base-plugin.d.ts
CHANGED
|
@@ -2,7 +2,8 @@ import EventEmitter, { type GeneralEventTypes } from './event-emitter.js';
|
|
|
2
2
|
import type { WaveSurfer, WaveSurferPluginParams } from './index.js';
|
|
3
3
|
export declare class BasePlugin<EventTypes extends GeneralEventTypes, Options> extends EventEmitter<EventTypes> {
|
|
4
4
|
protected wavesurfer: WaveSurfer;
|
|
5
|
-
protected container: HTMLElement;
|
|
5
|
+
protected container: ShadowRoot | HTMLElement;
|
|
6
|
+
protected wrapper: HTMLElement;
|
|
6
7
|
protected subscriptions: (() => void)[];
|
|
7
8
|
protected options: Options;
|
|
8
9
|
constructor(params: WaveSurferPluginParams, options: Options);
|
package/dist/base-plugin.js
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -9,8 +9,10 @@ export type WaveSurferOptions = {
|
|
|
9
9
|
waveColor?: string;
|
|
10
10
|
/** The color of the progress mask */
|
|
11
11
|
progressColor?: string;
|
|
12
|
-
/** The color of the playpack
|
|
12
|
+
/** The color of the playpack cursor */
|
|
13
13
|
cursorColor?: string;
|
|
14
|
+
/** The cursor with */
|
|
15
|
+
cursorWidth?: number;
|
|
14
16
|
/** If set, the waveform will be rendered in bars like so: ▁ ▂ ▇ ▃ ▅ ▂ */
|
|
15
17
|
barWidth?: number;
|
|
16
18
|
/** Spacing between bars in pixels */
|
|
@@ -59,7 +61,8 @@ export type WaveSurferEvents = {
|
|
|
59
61
|
};
|
|
60
62
|
export type WaveSurferPluginParams = {
|
|
61
63
|
wavesurfer: WaveSurfer;
|
|
62
|
-
container:
|
|
64
|
+
container: ShadowRoot;
|
|
65
|
+
wrapper: HTMLElement;
|
|
63
66
|
};
|
|
64
67
|
export declare class WaveSurfer extends EventEmitter<WaveSurferEvents> {
|
|
65
68
|
private options;
|
|
@@ -111,6 +114,8 @@ export declare class WaveSurfer extends EventEmitter<WaveSurferEvents> {
|
|
|
111
114
|
registerPlugin<T extends BasePlugin<GeneralEventTypes, Options>, Options>(CustomPlugin: new (params: WaveSurferPluginParams, options: Options) => T, options: Options): T;
|
|
112
115
|
/** Get the decoded audio data */
|
|
113
116
|
getDecodedData(): AudioBuffer | null;
|
|
117
|
+
/** Get the raw media element */
|
|
118
|
+
getMediaElement(): HTMLMediaElement | null;
|
|
114
119
|
/** Unmount wavesurfer */
|
|
115
120
|
destroy(): void;
|
|
116
121
|
}
|
package/dist/index.js
CHANGED
|
@@ -17,6 +17,7 @@ const defaultOptions = {
|
|
|
17
17
|
height: 128,
|
|
18
18
|
waveColor: '#999',
|
|
19
19
|
progressColor: '#555',
|
|
20
|
+
cursorWidth: 1,
|
|
20
21
|
minPxPerSec: 0,
|
|
21
22
|
fillParent: true,
|
|
22
23
|
interactive: true,
|
|
@@ -47,6 +48,7 @@ export class WaveSurfer extends EventEmitter {
|
|
|
47
48
|
waveColor: this.options.waveColor,
|
|
48
49
|
progressColor: this.options.progressColor,
|
|
49
50
|
cursorColor: this.options.cursorColor,
|
|
51
|
+
cursorWidth: this.options.cursorWidth,
|
|
50
52
|
minPxPerSec: this.options.minPxPerSec,
|
|
51
53
|
fillParent: this.options.fillParent,
|
|
52
54
|
barWidth: this.options.barWidth,
|
|
@@ -205,6 +207,7 @@ export class WaveSurfer extends EventEmitter {
|
|
|
205
207
|
const plugin = new CustomPlugin({
|
|
206
208
|
wavesurfer: this,
|
|
207
209
|
container: this.renderer.getContainer(),
|
|
210
|
+
wrapper: this.renderer.getWrapper(),
|
|
208
211
|
}, options);
|
|
209
212
|
this.plugins.push(plugin);
|
|
210
213
|
return plugin;
|
|
@@ -213,6 +216,10 @@ export class WaveSurfer extends EventEmitter {
|
|
|
213
216
|
getDecodedData() {
|
|
214
217
|
return this.decodedData;
|
|
215
218
|
}
|
|
219
|
+
/** Get the raw media element */
|
|
220
|
+
getMediaElement() {
|
|
221
|
+
return this.player.getMediaElement();
|
|
222
|
+
}
|
|
216
223
|
/** Unmount wavesurfer */
|
|
217
224
|
destroy() {
|
|
218
225
|
this.emit('destroy');
|
package/dist/player.d.ts
CHANGED
package/dist/player.js
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import BasePlugin from '../base-plugin.js';
|
|
2
|
+
import { type WaveSurferPluginParams, type WaveSurferOptions } from '../index.js';
|
|
3
|
+
export type MinimapPluginOptions = {
|
|
4
|
+
overlayColor?: string;
|
|
5
|
+
} & WaveSurferOptions;
|
|
6
|
+
declare const defaultOptions: {
|
|
7
|
+
height: number;
|
|
8
|
+
overlayColor: string;
|
|
9
|
+
};
|
|
10
|
+
type MinimapPluginEvents = {
|
|
11
|
+
ready: void;
|
|
12
|
+
};
|
|
13
|
+
declare class TimelinePlugin extends BasePlugin<MinimapPluginEvents, MinimapPluginOptions> {
|
|
14
|
+
protected options: MinimapPluginOptions & typeof defaultOptions;
|
|
15
|
+
private minimapWrapper;
|
|
16
|
+
private miniWavesurfer;
|
|
17
|
+
private overlay;
|
|
18
|
+
constructor(params: WaveSurferPluginParams, options: MinimapPluginOptions);
|
|
19
|
+
private initMinimapWrapper;
|
|
20
|
+
private initOverlay;
|
|
21
|
+
private initMinimap;
|
|
22
|
+
/** Unmount */
|
|
23
|
+
destroy(): void;
|
|
24
|
+
}
|
|
25
|
+
export default TimelinePlugin;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import BasePlugin from '../base-plugin.js';
|
|
2
|
+
import { WaveSurfer } from '../index.js';
|
|
3
|
+
const defaultOptions = {
|
|
4
|
+
height: 50,
|
|
5
|
+
overlayColor: 'rgba(100, 100, 100, 0.1)',
|
|
6
|
+
};
|
|
7
|
+
class TimelinePlugin extends BasePlugin {
|
|
8
|
+
constructor(params, options) {
|
|
9
|
+
super(params, options);
|
|
10
|
+
this.miniWavesurfer = null;
|
|
11
|
+
this.options = Object.assign({}, defaultOptions, options);
|
|
12
|
+
this.minimapWrapper = this.initMinimapWrapper();
|
|
13
|
+
this.overlay = this.initOverlay();
|
|
14
|
+
this.subscriptions.push(this.wavesurfer.on('decode', () => {
|
|
15
|
+
this.initMinimap();
|
|
16
|
+
}));
|
|
17
|
+
}
|
|
18
|
+
initMinimapWrapper() {
|
|
19
|
+
const div = document.createElement('div');
|
|
20
|
+
div.style.position = 'relative';
|
|
21
|
+
this.container.appendChild(div);
|
|
22
|
+
return div;
|
|
23
|
+
}
|
|
24
|
+
initOverlay() {
|
|
25
|
+
const div = document.createElement('div');
|
|
26
|
+
div.setAttribute('style', 'position: absolute; z-index: 2; left: 0; top: 0; bottom: 0;');
|
|
27
|
+
div.style.backgroundColor = this.options.overlayColor;
|
|
28
|
+
this.minimapWrapper.appendChild(div);
|
|
29
|
+
return div;
|
|
30
|
+
}
|
|
31
|
+
initMinimap() {
|
|
32
|
+
const data = this.wavesurfer.getDecodedData();
|
|
33
|
+
const media = this.wavesurfer.getMediaElement();
|
|
34
|
+
if (!data || !media)
|
|
35
|
+
return;
|
|
36
|
+
this.miniWavesurfer = WaveSurfer.create(Object.assign(Object.assign({}, this.options), { container: this.minimapWrapper, minPxPerSec: 1, fillParent: true, media, url: media.src, peaks: [data.getChannelData(0)], duration: data.duration }));
|
|
37
|
+
const overlayWidth = Math.round((this.minimapWrapper.clientWidth / this.wrapper.clientWidth) * 100);
|
|
38
|
+
this.overlay.style.width = `${overlayWidth}%`;
|
|
39
|
+
this.wavesurfer.on('timeupdate', ({ currentTime }) => {
|
|
40
|
+
const offset = Math.max(0, Math.min((currentTime / this.wavesurfer.getDuration()) * 100 - overlayWidth / 2, 100 - overlayWidth)).toFixed(2);
|
|
41
|
+
this.overlay.style.left = `${offset}%`;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
/** Unmount */
|
|
45
|
+
destroy() {
|
|
46
|
+
var _a;
|
|
47
|
+
(_a = this.miniWavesurfer) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
48
|
+
this.minimapWrapper.remove();
|
|
49
|
+
super.destroy();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
export default TimelinePlugin;
|
|
@@ -21,6 +21,7 @@ type MultitrackOptions = {
|
|
|
21
21
|
trackBackground?: string;
|
|
22
22
|
trackBorderColor?: string;
|
|
23
23
|
cueColor?: string;
|
|
24
|
+
onTrackPositionUpdate?: (id: string | number, startPosition: number) => void;
|
|
24
25
|
};
|
|
25
26
|
declare class MultiTrack {
|
|
26
27
|
private tracks;
|
|
@@ -39,13 +40,14 @@ declare class MultiTrack {
|
|
|
39
40
|
private initWavesurfers;
|
|
40
41
|
private initTimeline;
|
|
41
42
|
private updatePosition;
|
|
42
|
-
private onSeek;
|
|
43
43
|
private onDrag;
|
|
44
44
|
private findCurrentTracks;
|
|
45
45
|
private startSync;
|
|
46
46
|
play(): void;
|
|
47
47
|
pause(): void;
|
|
48
48
|
isPlaying(): boolean;
|
|
49
|
+
seekTo(time: number): void;
|
|
50
|
+
zoom(pxPerSec: number): void;
|
|
49
51
|
destroy(): void;
|
|
50
52
|
}
|
|
51
53
|
export default MultiTrack;
|
|
@@ -23,7 +23,9 @@ class MultiTrack {
|
|
|
23
23
|
}, 0);
|
|
24
24
|
this.rendering.setMainWidth(durations, maxDuration);
|
|
25
25
|
this.rendering.addClickHandler((position) => {
|
|
26
|
-
this.
|
|
26
|
+
if (this.isDragging)
|
|
27
|
+
return;
|
|
28
|
+
this.seekTo(position * maxDuration);
|
|
27
29
|
});
|
|
28
30
|
this.maxDuration = maxDuration;
|
|
29
31
|
this.initWavesurfers();
|
|
@@ -40,11 +42,11 @@ class MultiTrack {
|
|
|
40
42
|
this.audios = this.tracks.map((track) => new Audio(track.url));
|
|
41
43
|
return Promise.all(this.audios.map((audio) => {
|
|
42
44
|
return new Promise((resolve) => {
|
|
43
|
-
audio.src
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
45
|
+
if (!audio.src)
|
|
46
|
+
return resolve(0);
|
|
47
|
+
audio.addEventListener('canplay', () => {
|
|
48
|
+
resolve(audio.duration);
|
|
49
|
+
}, { once: true });
|
|
48
50
|
});
|
|
49
51
|
}));
|
|
50
52
|
}
|
|
@@ -108,15 +110,8 @@ class MultiTrack {
|
|
|
108
110
|
audio.volume = newVolume;
|
|
109
111
|
});
|
|
110
112
|
}
|
|
111
|
-
onSeek(time) {
|
|
112
|
-
if (this.isDragging)
|
|
113
|
-
return;
|
|
114
|
-
const wasPlaying = this.isPlaying();
|
|
115
|
-
this.updatePosition(time);
|
|
116
|
-
if (wasPlaying)
|
|
117
|
-
this.play();
|
|
118
|
-
}
|
|
119
113
|
onDrag(index, delta) {
|
|
114
|
+
var _a, _b;
|
|
120
115
|
const track = this.tracks[index];
|
|
121
116
|
if (!track.draggable)
|
|
122
117
|
return;
|
|
@@ -132,6 +127,7 @@ class MultiTrack {
|
|
|
132
127
|
track.startPosition = newTime;
|
|
133
128
|
this.rendering.setContainerOffsets();
|
|
134
129
|
this.updatePosition(this.currentTime);
|
|
130
|
+
(_b = (_a = this.options).onTrackPositionUpdate) === null || _b === void 0 ? void 0 : _b.call(_a, track.id, track.startPosition);
|
|
135
131
|
}
|
|
136
132
|
}
|
|
137
133
|
findCurrentTracks() {
|
|
@@ -179,6 +175,18 @@ class MultiTrack {
|
|
|
179
175
|
isPlaying() {
|
|
180
176
|
return this.audios.some((audio) => !audio.paused);
|
|
181
177
|
}
|
|
178
|
+
seekTo(time) {
|
|
179
|
+
const wasPlaying = this.isPlaying();
|
|
180
|
+
this.updatePosition(time);
|
|
181
|
+
if (wasPlaying)
|
|
182
|
+
this.play();
|
|
183
|
+
}
|
|
184
|
+
zoom(pxPerSec) {
|
|
185
|
+
this.options.minPxPerSec = pxPerSec;
|
|
186
|
+
this.wavesurfers.forEach((ws, index) => this.tracks[index].url && ws.zoom(pxPerSec));
|
|
187
|
+
this.rendering.setMainWidth(this.durations, this.maxDuration);
|
|
188
|
+
this.rendering.setContainerOffsets();
|
|
189
|
+
}
|
|
182
190
|
destroy() {
|
|
183
191
|
if (this.frameRequest)
|
|
184
192
|
cancelAnimationFrame(this.frameRequest);
|
|
@@ -207,6 +215,7 @@ function initRendering(tracks, options) {
|
|
|
207
215
|
cursor.setAttribute('style', 'width: 1px; height: 100%; position: absolute; z-index: 10; top: 0; left: 0');
|
|
208
216
|
cursor.style.backgroundColor = options.cursorColor || '#000';
|
|
209
217
|
wrapper.appendChild(cursor);
|
|
218
|
+
const { clientWidth } = wrapper;
|
|
210
219
|
// Create containers for each track
|
|
211
220
|
const containers = tracks.map((_, index) => {
|
|
212
221
|
const container = document.createElement('div');
|
|
@@ -238,12 +247,9 @@ function initRendering(tracks, options) {
|
|
|
238
247
|
// Set the container width
|
|
239
248
|
setMainWidth: (trackDurations, maxDuration) => {
|
|
240
249
|
durations = trackDurations;
|
|
241
|
-
const { clientWidth } = wrapper;
|
|
242
250
|
pxPerSec = Math.max(options.minPxPerSec || 0, clientWidth / maxDuration);
|
|
243
251
|
const width = pxPerSec * maxDuration;
|
|
244
|
-
|
|
245
|
-
wrapper.style.width = `${width}px`;
|
|
246
|
-
}
|
|
252
|
+
wrapper.style.width = `${width}px`;
|
|
247
253
|
setContainerOffsets();
|
|
248
254
|
},
|
|
249
255
|
// Update cursor position
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BasePlugin from '../base-plugin.js';
|
|
2
|
-
import { WaveSurferPluginParams } from '../index.js';
|
|
2
|
+
import type { WaveSurferPluginParams } from '../index.js';
|
|
3
3
|
export type RegionsPluginOptions = {
|
|
4
4
|
dragSelection?: boolean;
|
|
5
5
|
draggable?: boolean;
|
|
@@ -26,7 +26,7 @@ export type RegionsPluginEvents = {
|
|
|
26
26
|
};
|
|
27
27
|
declare class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPluginOptions> {
|
|
28
28
|
private dragStart;
|
|
29
|
-
private
|
|
29
|
+
private regionsContainer;
|
|
30
30
|
private regions;
|
|
31
31
|
private createdRegion;
|
|
32
32
|
private modifiedRegion;
|
|
@@ -36,7 +36,7 @@ declare class RegionsPlugin extends BasePlugin<RegionsPluginEvents, RegionsPlugi
|
|
|
36
36
|
constructor(params: WaveSurferPluginParams, options: RegionsPluginOptions);
|
|
37
37
|
/** Unmount */
|
|
38
38
|
destroy(): void;
|
|
39
|
-
private
|
|
39
|
+
private initRegionsContainer;
|
|
40
40
|
private handleMouseDown;
|
|
41
41
|
private handleMouseMove;
|
|
42
42
|
private handleMouseUp;
|
package/dist/plugins/regions.js
CHANGED
|
@@ -27,11 +27,11 @@ class RegionsPlugin extends BasePlugin {
|
|
|
27
27
|
this.isMoving = false;
|
|
28
28
|
this.handleMouseDown = (e) => {
|
|
29
29
|
if (this.options.draggable || this.options.resizable || this.options.dragSelection) {
|
|
30
|
-
this.dragStart = e.clientX - this.
|
|
30
|
+
this.dragStart = e.clientX - this.wrapper.getBoundingClientRect().left;
|
|
31
31
|
}
|
|
32
32
|
};
|
|
33
33
|
this.handleMouseMove = (e) => {
|
|
34
|
-
const dragEnd = e.clientX - this.
|
|
34
|
+
const dragEnd = e.clientX - this.wrapper.getBoundingClientRect().left;
|
|
35
35
|
if (this.options.draggable && this.modifiedRegion && this.isMoving) {
|
|
36
36
|
this.moveRegion(this.modifiedRegion, dragEnd - this.dragStart);
|
|
37
37
|
this.dragStart = dragEnd;
|
|
@@ -42,10 +42,10 @@ class RegionsPlugin extends BasePlugin {
|
|
|
42
42
|
return;
|
|
43
43
|
}
|
|
44
44
|
if (this.options.dragSelection && !isNaN(this.dragStart)) {
|
|
45
|
-
const dragEnd = e.clientX - this.
|
|
45
|
+
const dragEnd = e.clientX - this.regionsContainer.getBoundingClientRect().left;
|
|
46
46
|
if (dragEnd - this.dragStart >= MIN_WIDTH) {
|
|
47
47
|
if (!this.createdRegion) {
|
|
48
|
-
this.
|
|
48
|
+
this.wrapper.style.pointerEvents = 'none';
|
|
49
49
|
this.createdRegion = this.createRegion(this.dragStart, dragEnd);
|
|
50
50
|
}
|
|
51
51
|
else {
|
|
@@ -62,27 +62,27 @@ class RegionsPlugin extends BasePlugin {
|
|
|
62
62
|
this.modifiedRegion = null;
|
|
63
63
|
this.isMoving = false;
|
|
64
64
|
this.dragStart = NaN;
|
|
65
|
-
this.
|
|
65
|
+
this.wrapper.style.pointerEvents = '';
|
|
66
66
|
};
|
|
67
67
|
this.options = Object.assign({}, defaultOptions, options);
|
|
68
|
-
this.
|
|
68
|
+
this.regionsContainer = this.initRegionsContainer();
|
|
69
69
|
const unsubscribe = this.wavesurfer.once('decode', () => {
|
|
70
|
-
this.
|
|
70
|
+
this.wrapper.appendChild(this.regionsContainer);
|
|
71
71
|
});
|
|
72
72
|
this.subscriptions.push(unsubscribe);
|
|
73
|
-
this.
|
|
73
|
+
this.wrapper.addEventListener('mousedown', this.handleMouseDown);
|
|
74
74
|
document.addEventListener('mousemove', this.handleMouseMove);
|
|
75
75
|
document.addEventListener('mouseup', this.handleMouseUp);
|
|
76
76
|
}
|
|
77
77
|
/** Unmount */
|
|
78
78
|
destroy() {
|
|
79
|
-
this.
|
|
79
|
+
this.wrapper.removeEventListener('mousedown', this.handleMouseDown);
|
|
80
80
|
document.removeEventListener('mousemove', this.handleMouseMove);
|
|
81
81
|
document.removeEventListener('mouseup', this.handleMouseUp, true);
|
|
82
|
-
this.
|
|
82
|
+
this.regionsContainer.remove();
|
|
83
83
|
super.destroy();
|
|
84
84
|
}
|
|
85
|
-
|
|
85
|
+
initRegionsContainer() {
|
|
86
86
|
return el('div', {
|
|
87
87
|
position: 'absolute',
|
|
88
88
|
top: '0',
|
|
@@ -103,8 +103,7 @@ class RegionsPlugin extends BasePlugin {
|
|
|
103
103
|
backgroundColor: noWidth ? '' : 'rgba(0, 0, 0, 0.1)',
|
|
104
104
|
borderRadius: '2px',
|
|
105
105
|
boxSizing: 'border-box',
|
|
106
|
-
borderLeft: '2px solid rgba(0, 0, 0, 0.5)',
|
|
107
|
-
borderRight: noWidth ? '' : '2px solid rgba(0, 0, 0, 0.5)',
|
|
106
|
+
borderLeft: noWidth ? '2px solid rgba(0, 0, 0, 0.5)' : '',
|
|
108
107
|
transition: 'background-color 0.2s ease',
|
|
109
108
|
cursor: this.options.draggable ? 'move' : '',
|
|
110
109
|
pointerEvents: 'all',
|
|
@@ -115,10 +114,13 @@ class RegionsPlugin extends BasePlugin {
|
|
|
115
114
|
const leftHandle = el('div', {
|
|
116
115
|
position: 'absolute',
|
|
117
116
|
left: '0',
|
|
117
|
+
top: '0',
|
|
118
118
|
width: '6px',
|
|
119
119
|
height: '100%',
|
|
120
120
|
cursor: this.options.resizable ? 'ew-resize' : '',
|
|
121
121
|
pointerEvents: 'all',
|
|
122
|
+
borderLeft: noWidth ? '' : '2px solid rgba(0, 0, 0, 0.5)',
|
|
123
|
+
borderRadius: '2px 0 0 2px',
|
|
122
124
|
});
|
|
123
125
|
div.appendChild(leftHandle);
|
|
124
126
|
const rightHandle = leftHandle.cloneNode();
|
|
@@ -126,6 +128,8 @@ class RegionsPlugin extends BasePlugin {
|
|
|
126
128
|
left: '',
|
|
127
129
|
right: '0',
|
|
128
130
|
borderLeft: '',
|
|
131
|
+
borderRight: noWidth ? '' : '2px solid rgba(0, 0, 0, 0.5)',
|
|
132
|
+
borderRadius: '0 2px 2px 0',
|
|
129
133
|
});
|
|
130
134
|
div.appendChild(rightHandle);
|
|
131
135
|
leftHandle.addEventListener('mousedown', (e) => {
|
|
@@ -156,12 +160,12 @@ class RegionsPlugin extends BasePlugin {
|
|
|
156
160
|
this.emit('region-clicked', { region });
|
|
157
161
|
}
|
|
158
162
|
});
|
|
159
|
-
this.
|
|
163
|
+
this.regionsContainer.appendChild(div);
|
|
160
164
|
return div;
|
|
161
165
|
}
|
|
162
166
|
createRegion(start, end, title = '') {
|
|
163
167
|
const duration = this.wavesurfer.getDuration();
|
|
164
|
-
const width = this.
|
|
168
|
+
const width = this.regionsContainer.clientWidth;
|
|
165
169
|
start = Math.max(0, Math.min(start, width - 1));
|
|
166
170
|
end = Math.max(0, Math.min(end, width - 1));
|
|
167
171
|
return {
|
|
@@ -178,18 +182,19 @@ class RegionsPlugin extends BasePlugin {
|
|
|
178
182
|
this.emit('region-created', { region });
|
|
179
183
|
}
|
|
180
184
|
updateRegion(region, start, end) {
|
|
181
|
-
const width = this.
|
|
185
|
+
const width = this.regionsContainer.clientWidth;
|
|
182
186
|
if (start != null) {
|
|
183
187
|
start = Math.max(0, Math.min(start, width));
|
|
184
188
|
region.start = start;
|
|
185
189
|
region.element.style.left = `${region.start}px`;
|
|
186
|
-
region.
|
|
190
|
+
region.element.style.width = `${region.end - region.start}px`;
|
|
191
|
+
region.startTime = (start / this.regionsContainer.clientWidth) * this.wavesurfer.getDuration();
|
|
187
192
|
}
|
|
188
193
|
if (end != null) {
|
|
189
194
|
end = Math.max(0, Math.min(end, width));
|
|
190
195
|
region.end = end;
|
|
191
196
|
region.element.style.width = `${region.end - region.start}px`;
|
|
192
|
-
region.endTime = (end / this.
|
|
197
|
+
region.endTime = (end / this.regionsContainer.clientWidth) * this.wavesurfer.getDuration();
|
|
193
198
|
}
|
|
194
199
|
this.emit('region-updated', { region });
|
|
195
200
|
}
|
|
@@ -199,7 +204,7 @@ class RegionsPlugin extends BasePlugin {
|
|
|
199
204
|
/** Create a region at a given start and end time, with an optional title */
|
|
200
205
|
add(startTime, endTime, title = '', color = '') {
|
|
201
206
|
const duration = this.wavesurfer.getDuration();
|
|
202
|
-
const width = this.
|
|
207
|
+
const width = this.regionsContainer.clientWidth;
|
|
203
208
|
const start = (startTime / duration) * width;
|
|
204
209
|
const end = (endTime / duration) * width;
|
|
205
210
|
const region = this.createRegion(start, end, title);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import BasePlugin from '../base-plugin.js';
|
|
2
|
-
import { WaveSurferPluginParams } from '../index.js';
|
|
2
|
+
import type { WaveSurferPluginParams } from '../index.js';
|
|
3
3
|
export type TimelinePluginOptions = {
|
|
4
4
|
/** The height of the timeline in pixels, defaults to 20 */
|
|
5
5
|
height?: number;
|
|
@@ -21,12 +21,12 @@ type TimelinePluginEvents = {
|
|
|
21
21
|
ready: void;
|
|
22
22
|
};
|
|
23
23
|
declare class TimelinePlugin extends BasePlugin<TimelinePluginEvents, TimelinePluginOptions> {
|
|
24
|
-
private
|
|
24
|
+
private timelineWrapper;
|
|
25
25
|
protected options: TimelinePluginOptions & typeof defaultOptions;
|
|
26
26
|
constructor(params: WaveSurferPluginParams, options: TimelinePluginOptions);
|
|
27
27
|
/** Unmount */
|
|
28
28
|
destroy(): void;
|
|
29
|
-
private
|
|
29
|
+
private initTimelineWrapper;
|
|
30
30
|
private formatTime;
|
|
31
31
|
private defaultTimeInterval;
|
|
32
32
|
defaultPrimaryLabelInterval(pxPerSec: number): number;
|
package/dist/plugins/timeline.js
CHANGED
|
@@ -7,9 +7,9 @@ class TimelinePlugin extends BasePlugin {
|
|
|
7
7
|
var _a;
|
|
8
8
|
super(params, options);
|
|
9
9
|
this.options = Object.assign({}, defaultOptions, options);
|
|
10
|
-
this.container = (_a = this.options.container) !== null && _a !== void 0 ? _a : this.
|
|
11
|
-
this.
|
|
12
|
-
this.container.appendChild(this.
|
|
10
|
+
this.container = (_a = this.options.container) !== null && _a !== void 0 ? _a : this.wrapper;
|
|
11
|
+
this.timelineWrapper = this.initTimelineWrapper();
|
|
12
|
+
this.container.appendChild(this.timelineWrapper);
|
|
13
13
|
if (this.options.duration) {
|
|
14
14
|
this.initTimeline(this.options.duration);
|
|
15
15
|
}
|
|
@@ -21,10 +21,10 @@ class TimelinePlugin extends BasePlugin {
|
|
|
21
21
|
}
|
|
22
22
|
/** Unmount */
|
|
23
23
|
destroy() {
|
|
24
|
-
this.
|
|
24
|
+
this.timelineWrapper.remove();
|
|
25
25
|
super.destroy();
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
initTimelineWrapper() {
|
|
28
28
|
return document.createElement('div');
|
|
29
29
|
}
|
|
30
30
|
formatTime(seconds) {
|
|
@@ -79,7 +79,7 @@ class TimelinePlugin extends BasePlugin {
|
|
|
79
79
|
}
|
|
80
80
|
initTimeline(duration) {
|
|
81
81
|
var _a, _b, _c;
|
|
82
|
-
const width = Math.round(this.
|
|
82
|
+
const width = Math.round(this.timelineWrapper.scrollWidth * devicePixelRatio);
|
|
83
83
|
const pxPerSec = width / duration;
|
|
84
84
|
const timeInterval = (_a = this.options.timeInterval) !== null && _a !== void 0 ? _a : this.defaultTimeInterval(pxPerSec);
|
|
85
85
|
const primaryLabelInterval = (_b = this.options.primaryLabelInterval) !== null && _b !== void 0 ? _b : this.defaultPrimaryLabelInterval(pxPerSec);
|
|
@@ -118,7 +118,7 @@ class TimelinePlugin extends BasePlugin {
|
|
|
118
118
|
}
|
|
119
119
|
timeline.appendChild(notch);
|
|
120
120
|
}
|
|
121
|
-
this.
|
|
121
|
+
this.timelineWrapper.appendChild(timeline);
|
|
122
122
|
this.emit('ready');
|
|
123
123
|
}
|
|
124
124
|
}
|
package/dist/renderer.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ type RendererOptions = {
|
|
|
5
5
|
waveColor: string;
|
|
6
6
|
progressColor: string;
|
|
7
7
|
cursorColor?: string;
|
|
8
|
+
cursorWidth: number;
|
|
8
9
|
minPxPerSec: number;
|
|
9
10
|
fillParent: boolean;
|
|
10
11
|
barWidth?: number;
|
|
@@ -20,13 +21,15 @@ declare class Renderer extends EventEmitter<RendererEvents> {
|
|
|
20
21
|
private options;
|
|
21
22
|
private container;
|
|
22
23
|
private scrollContainer;
|
|
24
|
+
private wrapper;
|
|
23
25
|
private mainCanvas;
|
|
24
26
|
private progressCanvas;
|
|
25
27
|
private cursor;
|
|
26
28
|
private ctx;
|
|
27
29
|
private timeout;
|
|
28
30
|
constructor(options: RendererOptions);
|
|
29
|
-
getContainer():
|
|
31
|
+
getContainer(): ShadowRoot;
|
|
32
|
+
getWrapper(): HTMLElement;
|
|
30
33
|
destroy(): void;
|
|
31
34
|
private delay;
|
|
32
35
|
private renderPeaks;
|
package/dist/renderer.js
CHANGED
|
@@ -63,8 +63,8 @@ class Renderer extends EventEmitter {
|
|
|
63
63
|
top: 0;
|
|
64
64
|
left: 0;
|
|
65
65
|
height: 100%;
|
|
66
|
-
|
|
67
|
-
|
|
66
|
+
width: ${this.options.cursorWidth}px;
|
|
67
|
+
background: ${this.options.cursorColor || this.options.progressColor};
|
|
68
68
|
}
|
|
69
69
|
</style>
|
|
70
70
|
|
|
@@ -78,6 +78,7 @@ class Renderer extends EventEmitter {
|
|
|
78
78
|
`;
|
|
79
79
|
this.container = div;
|
|
80
80
|
this.scrollContainer = shadow.querySelector('.scroll');
|
|
81
|
+
this.wrapper = shadow.querySelector('.wrapper');
|
|
81
82
|
this.mainCanvas = shadow.querySelector('canvas');
|
|
82
83
|
this.ctx = this.mainCanvas.getContext('2d', { desynchronized: true });
|
|
83
84
|
this.progressCanvas = shadow.querySelector('.progress');
|
|
@@ -91,7 +92,10 @@ class Renderer extends EventEmitter {
|
|
|
91
92
|
});
|
|
92
93
|
}
|
|
93
94
|
getContainer() {
|
|
94
|
-
return this.
|
|
95
|
+
return this.container.shadowRoot;
|
|
96
|
+
}
|
|
97
|
+
getWrapper() {
|
|
98
|
+
return this.wrapper;
|
|
95
99
|
}
|
|
96
100
|
destroy() {
|
|
97
101
|
this.container.remove();
|
|
@@ -226,6 +230,13 @@ class Renderer extends EventEmitter {
|
|
|
226
230
|
renderProgress(progress, autoCenter = false) {
|
|
227
231
|
this.progressCanvas.style.clipPath = `inset(0 ${100 - progress * 100}% 0 0)`;
|
|
228
232
|
this.cursor.style.left = `${progress * 100}%`;
|
|
233
|
+
this.cursor.style.marginLeft =
|
|
234
|
+
progress > 0
|
|
235
|
+
? Math.round(progress * 100) === 100
|
|
236
|
+
? `-${this.cursor.offsetWidth}px`
|
|
237
|
+
: `-${this.cursor.offsetWidth / 2}px`
|
|
238
|
+
: '';
|
|
239
|
+
this.cursor.scrollIntoView();
|
|
229
240
|
if (autoCenter) {
|
|
230
241
|
const center = this.container.clientWidth / 2;
|
|
231
242
|
const fullWidth = this.mainCanvas.clientWidth;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Minimap=e():t.Minimap=e()}(WaveSurfer,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>l});const i=class{constructor(){this.eventTarget=new EventTarget}emit(t,e){const i=new CustomEvent(String(t),{detail:e});this.eventTarget.dispatchEvent(i)}on(t,e,i){const s=t=>{t instanceof CustomEvent&&e(t.detail)},n=String(t);return this.eventTarget.addEventListener(n,s,{once:i}),()=>this.eventTarget.removeEventListener(n,s)}once(t,e){return this.on(t,e,!0)}},s=class extends i{constructor(t,e){super(),this.subscriptions=[],this.wavesurfer=t.wavesurfer,this.container=t.container,this.wrapper=t.wrapper,this.options=e}destroy(){this.subscriptions.forEach((t=>t()))}};const n=class extends i{constructor(t){super(),this.timeout=null,this.options=t;let e=null;if("string"==typeof this.options.container?e=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(e=this.options.container),!e)throw new Error("Container not found");const i=document.createElement("div"),s=i.attachShadow({mode:"open"});s.innerHTML=`\n <style>\n :host {\n user-select: none;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .wrapper {\n position: relative;\n width: fit-content;\n min-width: 100%;\n z-index: 2;\n }\n :host canvas {\n display: block;\n height: ${this.options.height}px;\n min-width: 100%;\n image-rendering: pixelated;\n }\n :host .progress {\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 100%;\n pointer-events: none;\n clip-path: inset(0px 100% 0px 0px);\n }\n :host .cursor {\n position: absolute;\n z-index: 3;\n top: 0;\n left: 0;\n height: 100%;\n width: ${this.options.cursorWidth}px;\n background: ${this.options.cursorColor||this.options.progressColor};\n }\n </style>\n\n <div class="scroll">\n <div class="wrapper">\n <canvas></canvas>\n <canvas class="progress"></canvas>\n <div class="cursor"></div>\n </div>\n </div>\n `,this.container=i,this.scrollContainer=s.querySelector(".scroll"),this.wrapper=s.querySelector(".wrapper"),this.mainCanvas=s.querySelector("canvas"),this.ctx=this.mainCanvas.getContext("2d",{desynchronized:!0}),this.progressCanvas=s.querySelector(".progress"),this.cursor=s.querySelector(".cursor"),e.appendChild(i),this.mainCanvas.addEventListener("click",(t=>{const e=this.mainCanvas.getBoundingClientRect(),i=(t.clientX-e.left)/e.width;this.emit("click",{relativeX:i})}))}getContainer(){return this.container.shadowRoot}getWrapper(){return this.wrapper}destroy(){this.container.remove()}delay(t,e=100){return this.timeout&&clearTimeout(this.timeout),new Promise((i=>{this.timeout=setTimeout((()=>{i(t())}),e)}))}renderPeaks(t,e,i){var s,n,r,o,a;return n=this,r=void 0,a=function*(){const{devicePixelRatio:n}=window,{ctx:r}=this,o=null!=this.options.barWidth?this.options.barWidth*n:1,a=null!=this.options.barGap?this.options.barGap*n:this.options.barWidth?o/2:0,h=null!==(s=this.options.barRadius)&&void 0!==s?s:0,l=t[0],c=l.length,d=Math.floor(e/(o+a))/c,u=i/2,p=1===t.length,m=p?l:t[1],y=p&&m.some((t=>t<0)),v=(t,e)=>{let i=0,s=0,n=0;r.beginPath(),r.fillStyle=this.options.waveColor,r.roundRect||(r.roundRect=r.fillRect);for(let c=t;c<e;c++){const t=Math.round(c*d);if(t>i){const e=Math.round(s*u),l=Math.round(n*u);r.roundRect(i*(o+a),u-e,o,e+l||1,h),i=t,s=0,n=0}const e=y?l[c]:Math.abs(l[c]),p=y?m[c]:Math.abs(m[c]);e>s&&(s=e),(y?p<-n:p>n)&&(n=p<0?-p:p)}r.fill(),r.closePath()};r.clearRect(0,0,e,i);const{scrollLeft:g,scrollWidth:f,clientWidth:C}=this.scrollContainer,w=c/f,b=Math.floor(g*w),x=Math.ceil((g+C)*w);v(b,x),this.createProgressMask(),b>0&&(yield this.delay((()=>{v(0,b)}))),x<c&&(yield this.delay((()=>{v(x,c)}))),this.delay((()=>{this.createProgressMask()}))},new((o=void 0)||(o=Promise))((function(t,e){function i(t){try{h(a.next(t))}catch(t){e(t)}}function s(t){try{h(a.throw(t))}catch(t){e(t)}}function h(e){var n;e.done?t(e.value):(n=e.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}h((a=a.apply(n,r||[])).next())}))}createProgressMask(){const t=this.progressCanvas.getContext("2d",{desynchronized:!0});this.progressCanvas.width=this.mainCanvas.width,this.progressCanvas.height=this.mainCanvas.height,t.drawImage(this.mainCanvas,0,0),t.globalCompositeOperation="source-in",t.fillStyle=this.options.progressColor,t.fillRect(0,0,this.progressCanvas.width,this.progressCanvas.height)}render(t){const{devicePixelRatio:e}=window,i=this.options.fillParent?this.scrollContainer.clientWidth*e:0,s=t.duration*this.options.minPxPerSec,n=s>i,r=Math.max(1,n?s:i),{height:o}=this.options;this.mainCanvas.width=r,this.mainCanvas.height=o,this.mainCanvas.style.width=Math.floor(s/e)+"px";const a=[t.getChannelData(0)];t.numberOfChannels>1&&a.push(t.getChannelData(1)),this.renderPeaks(a,r,o)}zoom(t,e){const i=this.cursor.getBoundingClientRect().left;this.options.minPxPerSec=e,this.render(t);const s=this.cursor.getBoundingClientRect().left;this.scrollContainer.scrollLeft+=s-i}renderProgress(t,e=!1){if(this.progressCanvas.style.clipPath=`inset(0 ${100-100*t}% 0 0)`,this.cursor.style.left=100*t+"%",this.cursor.style.marginLeft=t>0?100===Math.round(100*t)?`-${this.cursor.offsetWidth}px`:`-${this.cursor.offsetWidth/2}px`:"",this.cursor.scrollIntoView(),e){const e=this.container.clientWidth/2,i=this.mainCanvas.clientWidth;i*t>=e&&(this.scrollContainer.scrollLeft=i*t-e)}}},r=class extends i{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}};const o={height:128,waveColor:"#999",progressColor:"#555",cursorWidth:1,minPxPerSec:0,fillParent:!0,interactive:!0};class a extends i{static create(t){return new a(t)}constructor(t){var e;super(),this.plugins=[],this.subscriptions=[],this.decodedData=null,this.options=Object.assign({},o,t),this.fetcher=new class{load(t){return e=this,i=void 0,n=function*(){return fetch(t).then((t=>t.arrayBuffer()))},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}},this.decoder=new class{initAudioContext(t){this.audioCtx=new AudioContext({latencyHint:"playback",sampleRate:t})}constructor(){this.audioCtx=null;try{this.initAudioContext(3e3)}catch(t){this.initAudioContext(8e3)}}decode(t){return e=this,i=void 0,n=function*(){if(!this.audioCtx)throw new Error("AudioContext is not initialized");return this.audioCtx.decodeAudioData(t)},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}destroy(){var t;null===(t=this.audioCtx)||void 0===t||t.close(),this.audioCtx=null}},this.timer=new r,this.player=new class{constructor({media:t,autoplay:e}){this.isExternalMedia=!1,this.hasPlayedOnce=!1,t?(this.media=t,this.isExternalMedia=!0):this.media=document.createElement("audio");const i=this.on("play",(()=>{this.hasPlayedOnce=!0,i()}));e&&(this.media.autoplay=!0)}on(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e)}destroy(){this.media.pause(),this.isExternalMedia||this.media.remove()}loadUrl(t){this.media.src=t}getCurrentTime(){return this.media.currentTime}play(){this.media.play()}pause(){this.media.pause()}isPlaying(){return this.media.currentTime>0&&!this.media.paused&&!this.media.ended}seekTo(t){if(this.media){const{hasPlayedOnce:e}=this;e||this.media.play(),this.media.currentTime=t,e||this.media.pause()}}getDuration(){return this.media.duration}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}getMediaElement(){return this.media}}({media:this.options.media,autoplay:this.options.autoplay}),this.renderer=new n({container:this.options.container,height:this.options.height,waveColor:this.options.waveColor,progressColor:this.options.progressColor,cursorColor:this.options.cursorColor,cursorWidth:this.options.cursorWidth,minPxPerSec:this.options.minPxPerSec,fillParent:this.options.fillParent,barWidth:this.options.barWidth,barGap:this.options.barGap,barRadius:this.options.barRadius}),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReadyEvent();const i=this.options.url||(null===(e=this.options.media)||void 0===e?void 0:e.src);i&&this.load(i,this.options.peaks,this.options.duration)}initPlayerEvents(){this.subscriptions.push(this.player.on("timeupdate",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),this.emit("timeupdate",{currentTime:t})})),this.player.on("play",(()=>{this.emit("play"),this.timer.start()})),this.player.on("pause",(()=>{this.emit("pause"),this.timer.stop()})),this.player.on("canplay",(()=>{this.emit("canplay",{duration:this.getDuration()})})),this.player.on("seeking",(()=>{this.emit("seeking",{currentTime:this.getCurrentTime()})})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",(({relativeX:t})=>{if(this.options.interactive){const e=this.getDuration()*t;this.seekTo(e),this.emit("seekClick",{currentTime:this.getCurrentTime()})}})))}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),!0),this.emit("timeupdate",{currentTime:t})})))}initReadyEvent(){let t=!1,e=!1;const i=()=>{t&&e&&this.emit("ready",{duration:this.getDuration()})};this.subscriptions.push(this.on("decode",(()=>{t=!0,i()})),this.on("canplay",(()=>{e=!0,i()})),this.player.on("waiting",(()=>{e=!1,t=!1})))}load(t,e,i){return s=this,n=void 0,o=function*(){if(this.player.loadUrl(t),null==e){const e=yield this.fetcher.load(t),i=yield this.decoder.decode(e);this.decodedData=i}else i||(i=(yield new Promise((t=>{this.player.on("canplay",(()=>t(this.getDuration())),{once:!0})})))||0),this.decodedData={duration:i,numberOfChannels:e.length,sampleRate:e[0].length/i,getChannelData:t=>e[t]};this.renderer.render(this.decodedData),this.emit("decode",{duration:this.decodedData.duration})},new((r=void 0)||(r=Promise))((function(t,e){function i(t){try{h(o.next(t))}catch(t){e(t)}}function a(t){try{h(o.throw(t))}catch(t){e(t)}}function h(e){var s;e.done?t(e.value):(s=e.value,s instanceof r?s:new r((function(t){t(s)}))).then(i,a)}h((o=o.apply(s,n||[])).next())}));var s,n,r,o}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(this.decodedData,t)}play(){this.player.play()}pause(){this.player.pause()}seekTo(t){this.player.seekTo(t)}isPlaying(){return this.player.isPlaying()}getDuration(){var t;return this.player.getDuration()||(null===(t=this.decodedData)||void 0===t?void 0:t.duration)||0}getCurrentTime(){return this.player.getCurrentTime()}getVolume(){return this.player.getVolume()}setVolume(t){this.player.setVolume(t)}getMuted(){return this.player.getMuted()}setMuted(t){this.player.setMuted(t)}getPlaybackRate(){return this.player.getPlaybackRate()}setPlaybackRate(t,e){this.player.setPlaybackRate(t,e)}registerPlugin(t,e){const i=new t({wavesurfer:this,container:this.renderer.getContainer(),wrapper:this.renderer.getWrapper()},e);return this.plugins.push(i),i}getDecodedData(){return this.decodedData}getMediaElement(){return this.player.getMediaElement()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.plugins.forEach((t=>t.destroy())),this.timer.destroy(),this.player.destroy(),this.decoder.destroy(),this.renderer.destroy()}}const h={height:50,overlayColor:"rgba(100, 100, 100, 0.1)"},l=class extends s{constructor(t,e){super(t,e),this.miniWavesurfer=null,this.options=Object.assign({},h,e),this.minimapWrapper=this.initMinimapWrapper(),this.overlay=this.initOverlay(),this.subscriptions.push(this.wavesurfer.on("decode",(()=>{this.initMinimap()})))}initMinimapWrapper(){const t=document.createElement("div");return t.style.position="relative",this.container.appendChild(t),t}initOverlay(){const t=document.createElement("div");return t.setAttribute("style","position: absolute; z-index: 2; left: 0; top: 0; bottom: 0;"),t.style.backgroundColor=this.options.overlayColor,this.minimapWrapper.appendChild(t),t}initMinimap(){const t=this.wavesurfer.getDecodedData(),e=this.wavesurfer.getMediaElement();if(!t||!e)return;this.miniWavesurfer=a.create(Object.assign(Object.assign({},this.options),{container:this.minimapWrapper,minPxPerSec:1,fillParent:!0,media:e,url:e.src,peaks:[t.getChannelData(0)],duration:t.duration}));const i=Math.round(this.minimapWrapper.clientWidth/this.wrapper.clientWidth*100);this.overlay.style.width=`${i}%`,this.wavesurfer.on("timeupdate",(({currentTime:t})=>{const e=Math.max(0,Math.min(t/this.wavesurfer.getDuration()*100-i/2,100-i)).toFixed(2);this.overlay.style.left=`${e}%`}))}destroy(){var t;null===(t=this.miniWavesurfer)||void 0===t||t.destroy(),this.minimapWrapper.remove(),super.destroy()}};return e.default})()));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Multitrack=e():t.Multitrack=e()}(WaveSurfer,(()=>(()=>{"use strict";var t={284:(t,e,i)=>{i.d(e,{Z:()=>r});var s=i(139);class n extends s.Z{constructor(t,e){super(),this.subscriptions=[],this.wavesurfer=t.wavesurfer,this.container=t.container,this.options=e}destroy(){this.subscriptions.forEach((t=>t()))}}const r=n},139:(t,e,i)=>{i.d(e,{Z:()=>s});const s=class{constructor(){this.eventTarget=new EventTarget}emit(t,e){const i=new CustomEvent(String(t),{detail:e});this.eventTarget.dispatchEvent(i)}on(t,e,i){const s=t=>{t instanceof CustomEvent&&e(t.detail)},n=String(t);return this.eventTarget.addEventListener(n,s,{once:i}),()=>this.eventTarget.removeEventListener(n,s)}once(t,e){return this.on(t,e,!0)}}},76:(t,e,i)=>{i.d(e,{default:()=>h});var s=i(284);const n={dragSelection:!0,draggable:!0,resizable:!0},r=(t,e)=>{for(const i in e)t.style[i]=e[i]||""},o=(t,e)=>{const i=document.createElement(t);return r(i,e),i};class a extends s.Z{constructor(t,e){super(t,e),this.dragStart=NaN,this.regions=[],this.createdRegion=null,this.modifiedRegion=null,this.isResizingLeft=!1,this.isMoving=!1,this.handleMouseDown=t=>{(this.options.draggable||this.options.resizable||this.options.dragSelection)&&(this.dragStart=t.clientX-this.container.getBoundingClientRect().left)},this.handleMouseMove=t=>{const e=t.clientX-this.container.getBoundingClientRect().left;if(this.options.draggable&&this.modifiedRegion&&this.isMoving)return this.moveRegion(this.modifiedRegion,e-this.dragStart),void(this.dragStart=e);if(this.options.resizable&&this.modifiedRegion)this.updateRegion(this.modifiedRegion,this.isResizingLeft?e:void 0,this.isResizingLeft?void 0:e);else if(this.options.dragSelection&&!isNaN(this.dragStart)){const e=t.clientX-this.wrapper.getBoundingClientRect().left;e-this.dragStart>=10&&(this.createdRegion?this.updateRegion(this.createdRegion,this.dragStart,e):(this.container.style.pointerEvents="none",this.createdRegion=this.createRegion(this.dragStart,e)))}},this.handleMouseUp=()=>{this.createdRegion&&(this.addRegion(this.createdRegion),this.createdRegion=null),this.modifiedRegion=null,this.isMoving=!1,this.dragStart=NaN,this.container.style.pointerEvents=""},this.options=Object.assign({},n,e),this.wrapper=this.initWrapper();const i=this.wavesurfer.once("decode",(()=>{this.container.appendChild(this.wrapper)}));this.subscriptions.push(i),this.container.addEventListener("mousedown",this.handleMouseDown),document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp)}destroy(){this.container.removeEventListener("mousedown",this.handleMouseDown),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp,!0),this.wrapper.remove(),super.destroy()}initWrapper(){return o("div",{position:"absolute",top:"0",left:"0",width:"100%",height:"100%",zIndex:"3",pointerEvents:"none"})}createRegionElement(t,e,i=""){const s=t===e,n=o("div",{position:"absolute",left:`${t}px`,width:e-t+"px",height:"100%",backgroundColor:s?"":"rgba(0, 0, 0, 0.1)",borderRadius:"2px",boxSizing:"border-box",borderLeft:"2px solid rgba(0, 0, 0, 0.5)",borderRight:s?"":"2px solid rgba(0, 0, 0, 0.5)",transition:"background-color 0.2s ease",cursor:this.options.draggable?"move":"",pointerEvents:"all",whiteSpace:s?"nowrap":"",padding:"0.2em"});n.textContent=i;const a=o("div",{position:"absolute",left:"0",width:"6px",height:"100%",cursor:this.options.resizable?"ew-resize":"",pointerEvents:"all"});n.appendChild(a);const h=a.cloneNode();return r(h,{left:"",right:"0",borderLeft:""}),n.appendChild(h),a.addEventListener("mousedown",(t=>{this.options.resizable&&(t.stopPropagation(),this.modifiedRegion=this.regions.find((t=>t.element===n))||null,this.isResizingLeft=!0,this.isMoving=!1)})),h.addEventListener("mousedown",(t=>{this.options.resizable&&(t.stopPropagation(),this.modifiedRegion=this.regions.find((t=>t.element===n))||null,this.isResizingLeft=!1,this.isMoving=!1)})),n.addEventListener("mousedown",(()=>{this.options.draggable&&(this.modifiedRegion=this.regions.find((t=>t.element===n))||null,this.isMoving=!0)})),n.addEventListener("click",(()=>{const t=this.regions.find((t=>t.element===n));t&&this.emit("region-clicked",{region:t})})),this.wrapper.appendChild(n),n}createRegion(t,e,i=""){const s=this.wavesurfer.getDuration(),n=this.wrapper.clientWidth;return t=Math.max(0,Math.min(t,n-1)),e=Math.max(0,Math.min(e,n-1)),{element:this.createRegionElement(t,e,i),start:t,end:e,startTime:t/n*s,endTime:e/n*s,title:i}}addRegion(t){this.regions.push(t),this.emit("region-created",{region:t})}updateRegion(t,e,i){const s=this.wrapper.clientWidth;null!=e&&(e=Math.max(0,Math.min(e,s)),t.start=e,t.element.style.left=`${t.start}px`,t.startTime=e/this.wrapper.clientWidth*this.wavesurfer.getDuration()),null!=i&&(i=Math.max(0,Math.min(i,s)),t.end=i,t.element.style.width=t.end-t.start+"px",t.endTime=i/this.wrapper.clientWidth*this.wavesurfer.getDuration()),this.emit("region-updated",{region:t})}moveRegion(t,e){this.updateRegion(t,t.start+e,t.end+e)}add(t,e,i="",s=""){const n=this.wavesurfer.getDuration(),r=this.wrapper.clientWidth,o=t/n*r,a=e/n*r,h=this.createRegion(o,a,i);return this.addRegion(h),s&&this.setRegionColor(h,s),h}setRegionColor(t,e){t.element.style[t.startTime===t.endTime?"borderColor":"backgroundColor"]=e}}const h=a},954:(t,e,i)=>{i.d(e,{default:()=>o});var s=i(284);const n={height:20};class r extends s.Z{constructor(t,e){var i;super(t,e),this.options=Object.assign({},n,e),this.container=null!==(i=this.options.container)&&void 0!==i?i:this.container,this.wrapper=this.initWrapper(),this.container.appendChild(this.wrapper),this.options.duration?this.initTimeline(this.options.duration):this.subscriptions.push(this.wavesurfer.on("decode",(({duration:t})=>{this.initTimeline(t)})))}destroy(){this.wrapper.remove(),super.destroy()}initWrapper(){return document.createElement("div")}formatTime(t){return t/60>1?`${Math.round(t/60)}:${(t=Math.round(t%60))<10?"0":""}${t}`:""+Math.round(1e3*t)/1e3}defaultTimeInterval(t){return t>=25?1:5*t>=25?5:15*t>=25?15:60*Math.ceil(.5/t)}defaultPrimaryLabelInterval(t){return t>=25?10:5*t>=25?6:4}defaultSecondaryLabelInterval(t){return t>=25?5:2}initTimeline(t){var e,i,s;const n=Math.round(this.wrapper.scrollWidth*devicePixelRatio)/t,r=null!==(e=this.options.timeInterval)&&void 0!==e?e:this.defaultTimeInterval(n),o=null!==(i=this.options.primaryLabelInterval)&&void 0!==i?i:this.defaultPrimaryLabelInterval(n),a=null!==(s=this.options.secondaryLabelInterval)&&void 0!==s?s:this.defaultSecondaryLabelInterval(n),h=document.createElement("div");h.setAttribute("style",`\n height: ${this.options.height}px;\n overflow: hidden;\n display: flex;\n justify-content: space-between;\n align-items: flex-end;\n font-size: ${this.options.height/2}px;\n white-space: nowrap;\n `);const d=document.createElement("div");d.setAttribute("style","\n width: 1px;\n height: 50%;\n display: flex;\n flex-direction: column;\n justify-content: flex-end;\n overflow: visible;\n border-left: 1px solid currentColor;\n opacity: 0.25;\n ");for(let e=0;e<t;e+=r){const t=d.cloneNode(),i=e%o==0;(i||e%a==0)&&(t.style.height="100%",t.style.textIndent="3px",t.textContent=this.formatTime(e),i&&(t.style.opacity="1")),h.appendChild(t)}this.wrapper.appendChild(h),this.emit("ready")}}const o=r}},e={};function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s](r,r.exports,i),r.exports}i.d=(t,e)=>{for(var s in e)i.o(e,s)&&!i.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var s={};return(()=>{i.d(s,{default:()=>p});var t=i(139);class e extends t.Z{constructor(t){super(),this.timeout=null,this.options=t;let e=null;if("string"==typeof this.options.container?e=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(e=this.options.container),!e)throw new Error("Container not found");const i=document.createElement("div"),s=i.attachShadow({mode:"open"});s.innerHTML=`\n <style>\n :host {\n user-select: none;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .wrapper {\n position: relative;\n width: fit-content;\n min-width: 100%;\n z-index: 2;\n }\n :host canvas {\n display: block;\n height: ${this.options.height}px;\n min-width: 100%;\n image-rendering: pixelated;\n }\n :host .progress {\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 100%;\n pointer-events: none;\n clip-path: inset(0px 100% 0px 0px);\n }\n :host .cursor {\n position: absolute;\n z-index: 3;\n top: 0;\n left: 0;\n height: 100%;\n border-right: 1px solid ${this.options.cursorColor||this.options.progressColor};\n margin-left: -1px;\n }\n </style>\n\n <div class="scroll">\n <div class="wrapper">\n <canvas></canvas>\n <canvas class="progress"></canvas>\n <div class="cursor"></div>\n </div>\n </div>\n `,this.container=i,this.scrollContainer=s.querySelector(".scroll"),this.mainCanvas=s.querySelector("canvas"),this.ctx=this.mainCanvas.getContext("2d",{desynchronized:!0}),this.progressCanvas=s.querySelector(".progress"),this.cursor=s.querySelector(".cursor"),e.appendChild(i),this.mainCanvas.addEventListener("click",(t=>{const e=this.mainCanvas.getBoundingClientRect(),i=(t.clientX-e.left)/e.width;this.emit("click",{relativeX:i})}))}getContainer(){return this.scrollContainer.querySelector(".wrapper")}destroy(){this.container.remove()}delay(t,e=100){return this.timeout&&clearTimeout(this.timeout),new Promise((i=>{this.timeout=setTimeout((()=>{i(t())}),e)}))}renderPeaks(t,e,i){var s,n,r,o,a;return n=this,r=void 0,a=function*(){const{devicePixelRatio:n}=window,{ctx:r}=this,o=null!=this.options.barWidth?this.options.barWidth*n:1,a=null!=this.options.barGap?this.options.barGap*n:this.options.barWidth?o/2:0,h=null!==(s=this.options.barRadius)&&void 0!==s?s:0,d=t[0],l=d.length,c=Math.floor(e/(o+a))/l,u=i/2,p=1===t.length,m=p?d:t[1],g=p&&m.some((t=>t<0)),v=(t,e)=>{let i=0,s=0,n=0;r.beginPath(),r.fillStyle=this.options.waveColor,r.roundRect||(r.roundRect=r.fillRect);for(let l=t;l<e;l++){const t=Math.round(l*c);if(t>i){const e=Math.round(s*u),d=Math.round(n*u);r.roundRect(i*(o+a),u-e,o,e+d||1,h),i=t,s=0,n=0}const e=g?d[l]:Math.abs(d[l]),p=g?m[l]:Math.abs(m[l]);e>s&&(s=e),(g?p<-n:p>n)&&(n=p<0?-p:p)}r.fill(),r.closePath()};r.clearRect(0,0,e,i);const{scrollLeft:f,scrollWidth:y,clientWidth:w}=this.scrollContainer,C=l/y,b=Math.floor(f*C),x=Math.ceil((f+w)*C);v(b,x),this.createProgressMask(),b>0&&(yield this.delay((()=>{v(0,b)}))),x<l&&(yield this.delay((()=>{v(x,l)}))),this.delay((()=>{this.createProgressMask()}))},new((o=void 0)||(o=Promise))((function(t,e){function i(t){try{h(a.next(t))}catch(t){e(t)}}function s(t){try{h(a.throw(t))}catch(t){e(t)}}function h(e){var n;e.done?t(e.value):(n=e.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}h((a=a.apply(n,r||[])).next())}))}createProgressMask(){const t=this.progressCanvas.getContext("2d",{desynchronized:!0});this.progressCanvas.width=this.mainCanvas.width,this.progressCanvas.height=this.mainCanvas.height,t.drawImage(this.mainCanvas,0,0),t.globalCompositeOperation="source-in",t.fillStyle=this.options.progressColor,t.fillRect(0,0,this.progressCanvas.width,this.progressCanvas.height)}render(t){const{devicePixelRatio:e}=window,i=this.options.fillParent?this.scrollContainer.clientWidth*e:0,s=t.duration*this.options.minPxPerSec,n=s>i,r=Math.max(1,n?s:i),{height:o}=this.options;this.mainCanvas.width=r,this.mainCanvas.height=o,this.mainCanvas.style.width=Math.floor(s/e)+"px";const a=[t.getChannelData(0)];t.numberOfChannels>1&&a.push(t.getChannelData(1)),this.renderPeaks(a,r,o)}zoom(t,e){const i=this.cursor.getBoundingClientRect().left;this.options.minPxPerSec=e,this.render(t);const s=this.cursor.getBoundingClientRect().left;this.scrollContainer.scrollLeft+=s-i}renderProgress(t,e=!1){if(this.progressCanvas.style.clipPath=`inset(0 ${100-100*t}% 0 0)`,this.cursor.style.left=100*t+"%",e){const e=this.container.clientWidth/2,i=this.mainCanvas.clientWidth;i*t>=e&&(this.scrollContainer.scrollLeft=i*t-e)}}}const n=e;class r extends t.Z{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=r;const a={height:128,waveColor:"#999",progressColor:"#555",minPxPerSec:0,fillParent:!0,interactive:!0};class h extends t.Z{static create(t){return new h(t)}constructor(t){var e;super(),this.plugins=[],this.subscriptions=[],this.decodedData=null,this.options=Object.assign({},a,t),this.fetcher=new class{load(t){return e=this,i=void 0,n=function*(){return fetch(t).then((t=>t.arrayBuffer()))},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}},this.decoder=new class{initAudioContext(t){this.audioCtx=new AudioContext({latencyHint:"playback",sampleRate:t})}constructor(){this.audioCtx=null;try{this.initAudioContext(3e3)}catch(t){this.initAudioContext(8e3)}}decode(t){return e=this,i=void 0,n=function*(){if(!this.audioCtx)throw new Error("AudioContext is not initialized");return this.audioCtx.decodeAudioData(t)},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}destroy(){var t;null===(t=this.audioCtx)||void 0===t||t.close(),this.audioCtx=null}},this.timer=new o,this.player=new class{constructor({media:t,autoplay:e}){this.isExternalMedia=!1,this.hasPlayedOnce=!1,t?(this.media=t,this.isExternalMedia=!0):this.media=document.createElement("audio");const i=this.on("play",(()=>{this.hasPlayedOnce=!0,i()}));e&&(this.media.autoplay=!0)}on(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e)}destroy(){this.media.pause(),this.isExternalMedia||this.media.remove()}loadUrl(t){this.media.src=t}getCurrentTime(){return this.media.currentTime}play(){this.media.play()}pause(){this.media.pause()}isPlaying(){return this.media.currentTime>0&&!this.media.paused&&!this.media.ended}seekTo(t){if(this.media){const{hasPlayedOnce:e}=this;e||this.media.play(),this.media.currentTime=t,e||this.media.pause()}}getDuration(){return this.media.duration}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}}({media:this.options.media,autoplay:this.options.autoplay}),this.renderer=new n({container:this.options.container,height:this.options.height,waveColor:this.options.waveColor,progressColor:this.options.progressColor,cursorColor:this.options.cursorColor,minPxPerSec:this.options.minPxPerSec,fillParent:this.options.fillParent,barWidth:this.options.barWidth,barGap:this.options.barGap,barRadius:this.options.barRadius}),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReadyEvent();const i=this.options.url||(null===(e=this.options.media)||void 0===e?void 0:e.src);i&&this.load(i,this.options.peaks,this.options.duration)}initPlayerEvents(){this.subscriptions.push(this.player.on("timeupdate",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),this.emit("timeupdate",{currentTime:t})})),this.player.on("play",(()=>{this.emit("play"),this.timer.start()})),this.player.on("pause",(()=>{this.emit("pause"),this.timer.stop()})),this.player.on("canplay",(()=>{this.emit("canplay",{duration:this.getDuration()})})),this.player.on("seeking",(()=>{this.emit("seeking",{currentTime:this.getCurrentTime()})})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",(({relativeX:t})=>{if(this.options.interactive){const e=this.getDuration()*t;this.seekTo(e),this.emit("seekClick",{currentTime:this.getCurrentTime()})}})))}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),!0),this.emit("timeupdate",{currentTime:t})})))}initReadyEvent(){let t=!1,e=!1;const i=()=>{t&&e&&this.emit("ready",{duration:this.getDuration()})};this.subscriptions.push(this.on("decode",(()=>{t=!0,i()})),this.on("canplay",(()=>{e=!0,i()})),this.player.on("waiting",(()=>{e=!1,t=!1})))}load(t,e,i){return s=this,n=void 0,o=function*(){if(this.player.loadUrl(t),null==e){const e=yield this.fetcher.load(t),i=yield this.decoder.decode(e);this.decodedData=i}else i||(i=(yield new Promise((t=>{this.player.on("canplay",(()=>t(this.getDuration())),{once:!0})})))||0),this.decodedData={duration:i,numberOfChannels:e.length,sampleRate:e[0].length/i,getChannelData:t=>e[t]};this.renderer.render(this.decodedData),this.emit("decode",{duration:this.decodedData.duration})},new((r=void 0)||(r=Promise))((function(t,e){function i(t){try{h(o.next(t))}catch(t){e(t)}}function a(t){try{h(o.throw(t))}catch(t){e(t)}}function h(e){var s;e.done?t(e.value):(s=e.value,s instanceof r?s:new r((function(t){t(s)}))).then(i,a)}h((o=o.apply(s,n||[])).next())}));var s,n,r,o}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(this.decodedData,t)}play(){this.player.play()}pause(){this.player.pause()}seekTo(t){this.player.seekTo(t)}isPlaying(){return this.player.isPlaying()}getDuration(){var t;return this.player.getDuration()||(null===(t=this.decodedData)||void 0===t?void 0:t.duration)||0}getCurrentTime(){return this.player.getCurrentTime()}getVolume(){return this.player.getVolume()}setVolume(t){this.player.setVolume(t)}getMuted(){return this.player.getMuted()}setMuted(t){this.player.setMuted(t)}getPlaybackRate(){return this.player.getPlaybackRate()}setPlaybackRate(t,e){this.player.setPlaybackRate(t,e)}registerPlugin(t,e){const i=new t({wavesurfer:this,container:this.renderer.getContainer()},e);return this.plugins.push(i),i}getDecodedData(){return this.decodedData}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.plugins.forEach((t=>t.destroy())),this.timer.destroy(),this.player.destroy(),this.decoder.destroy(),this.renderer.destroy()}}const d=h;var l=i(76),c=i(954);class u{static create(t,e){return new u(t,e)}constructor(t,e){this.audios=[],this.wavesurfers=[],this.durations=[],this.currentTime=0,this.maxDuration=0,this.isDragging=!1,this.frameRequest=null,this.tracks=t.map((t=>Object.assign(Object.assign({},t),{startPosition:t.startPosition||0,peaks:t.peaks||(t.url?void 0:[new Float32Array])}))),this.options=e,this.rendering=function(t,e){let i=0,s=[];const n=document.createElement("div");n.setAttribute("style","width: 100%; overflow-x: scroll; overflow-y: hidden; user-select: none;");const r=document.createElement("div");r.style.position="relative",n.appendChild(r),e.container.appendChild(n);const o=document.createElement("div");o.setAttribute("style","width: 1px; height: 100%; position: absolute; z-index: 10; top: 0; left: 0"),o.style.backgroundColor=e.cursorColor||"#000",r.appendChild(o);const a=t.map(((t,i)=>{const s=document.createElement("div");if(e.trackBorderColor&&i>0){const t=document.createElement("div");t.setAttribute("style",`width: 100%; height: 2px; background-color: ${e.trackBorderColor}`),r.appendChild(t)}return e.trackBackground&&(s.style.background=e.trackBackground),r.appendChild(s),s})),h=()=>{a.forEach(((e,n)=>{const r=t[n].startPosition*i;e.style.width=s[n]*i+"px",e.style.transform=`translateX(${r}px)`,t[n].draggable&&(e.style.cursor="ew-resize")}))};return{containers:a,setContainerOffsets:h,setMainWidth:(t,n)=>{s=t;const{clientWidth:o}=r;i=Math.max(e.minPxPerSec||0,o/n);const a=i*n;a>o&&(r.style.width=`${a}px`),h()},updateCursor:t=>{o.style.left=`${Math.min(100,100*t)}%`},addClickHandler:t=>{r.addEventListener("click",(e=>{const i=r.getBoundingClientRect(),s=(e.clientX-i.left)/r.offsetWidth;t(s)}))},destroy:()=>{n.remove()}}}(this.tracks,this.options),this.initAudios().then((t=>{this.durations=t;const e=this.tracks.reduce(((e,i,s)=>Math.max(e,i.startPosition+t[s])),0);this.rendering.setMainWidth(t,e),this.rendering.addClickHandler((t=>{this.onSeek(t*e)})),this.maxDuration=e,this.initWavesurfers(),this.initTimeline(),this.rendering.containers.forEach(((t,e)=>{const i=function(t,e){const i=t.parentElement;if(!i)return;let s=null;t.addEventListener("mousedown",(t=>{const e=i.getBoundingClientRect();s=t.clientX-e.left}));const n=t=>{null!=s&&(t.stopPropagation(),s=null)},r=t=>{if(null==s)return;const n=i.getBoundingClientRect(),r=t.clientX-n.left,o=r-s;(o>1||o<-1)&&(s=r,e(o/i.offsetWidth))};return document.body.addEventListener("mouseup",n),document.body.addEventListener("mousemove",r),{destroy:()=>{document.body.removeEventListener("mouseup",n),document.body.removeEventListener("mousemove",r)}}}(t,(t=>this.onDrag(e,t)));this.wavesurfers[e].once("destroy",(()=>{null==i||i.destroy()}))}))}))}initAudios(){return this.audios=this.tracks.map((t=>new Audio(t.url))),Promise.all(this.audios.map((t=>new Promise((e=>{t.src?t.addEventListener("canplay",(()=>{e(t.duration)}),{once:!0}):e(0)})))))}initWavesurfers(){const t=this.tracks.map(((t,e)=>{const i=d.create(Object.assign(Object.assign({},this.options),{container:this.rendering.containers[e],minPxPerSec:0,media:this.audios[e],peaks:t.peaks,cursorColor:"transparent",interactive:!1})),s=i.registerPlugin(l.default,{draggable:!1,resizable:!1,dragSelection:!1});return i.once("decode",(()=>{t.startCue&&s.add(t.startCue,t.startCue,"start",this.options.cueColor),t.endCue&&s.add(t.endCue,t.endCue,"end",this.options.cueColor),(t.markers||[]).forEach((t=>{s.add(t.time,t.time,t.label,t.color)}))})),i}));this.wavesurfers=t}initTimeline(){this.wavesurfers[0].registerPlugin(c.default,{duration:this.maxDuration,container:this.rendering.containers[0].parentElement})}updatePosition(t){this.currentTime=t,this.rendering.updateCursor(t/this.maxDuration);const e=!this.isPlaying();this.tracks.forEach(((i,s)=>{const n=this.audios[s],r=this.durations[s],o=t-i.startPosition;Math.abs(n.currentTime-o)>.3&&(n.currentTime=o),e||o<0||o>r?!n.paused&&n.pause():e||n.paused&&n.play();const a=o>=(i.startCue||0)&&o<(i.endCue||1/0)?1:0;a!==n.volume&&(n.volume=a)}))}onSeek(t){if(this.isDragging)return;const e=this.isPlaying();this.updatePosition(t),e&&this.play()}onDrag(t,e){const i=this.tracks[t];if(!i.draggable)return;this.isDragging=!0,setTimeout((()=>this.isDragging=!1),600);const s=i.startPosition+e*this.maxDuration,n=this.tracks.reduce(((e,i,s)=>s!==t&&i.url?Math.min(e,i.startPosition):e),1/0);s+this.durations[t]>=n&&(i.startPosition=s,this.rendering.setContainerOffsets(),this.updatePosition(this.currentTime))}findCurrentTracks(){const t=[];if(this.tracks.forEach(((e,i)=>{e.url&&this.currentTime>=e.startPosition&&this.currentTime<e.startPosition+this.durations[i]&&t.push(i)})),0===t.length){const e=Math.min(...this.tracks.filter((t=>t.url)).map((t=>t.startPosition)));t.push(this.tracks.findIndex((t=>t.startPosition===e)))}return t}startSync(){const t=()=>{const e=this.audios.reduce(((t,e,i)=>(e.paused||(t=Math.max(t,e.currentTime+this.tracks[i].startPosition)),t)),this.currentTime);e>this.currentTime&&this.updatePosition(e),this.frameRequest=requestAnimationFrame(t)};t()}play(){this.startSync(),this.findCurrentTracks().forEach((t=>{var e;null===(e=this.audios[t])||void 0===e||e.play()}))}pause(){this.audios.forEach((t=>t.pause()))}isPlaying(){return this.audios.some((t=>!t.paused))}destroy(){this.frameRequest&&cancelAnimationFrame(this.frameRequest),this.rendering.destroy(),this.audios.forEach((t=>{t.pause(),t.src=""})),this.wavesurfers.forEach((t=>{t.destroy()}))}}const p=u})(),s.default})()));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Multitrack=e():t.Multitrack=e()}(WaveSurfer,(()=>(()=>{"use strict";var t={284:(t,e,i)=>{i.d(e,{Z:()=>r});var s=i(139);class n extends s.Z{constructor(t,e){super(),this.subscriptions=[],this.wavesurfer=t.wavesurfer,this.container=t.container,this.wrapper=t.wrapper,this.options=e}destroy(){this.subscriptions.forEach((t=>t()))}}const r=n},139:(t,e,i)=>{i.d(e,{Z:()=>s});const s=class{constructor(){this.eventTarget=new EventTarget}emit(t,e){const i=new CustomEvent(String(t),{detail:e});this.eventTarget.dispatchEvent(i)}on(t,e,i){const s=t=>{t instanceof CustomEvent&&e(t.detail)},n=String(t);return this.eventTarget.addEventListener(n,s,{once:i}),()=>this.eventTarget.removeEventListener(n,s)}once(t,e){return this.on(t,e,!0)}}},76:(t,e,i)=>{i.d(e,{default:()=>h});var s=i(284);const n={dragSelection:!0,draggable:!0,resizable:!0},r=(t,e)=>{for(const i in e)t.style[i]=e[i]||""},o=(t,e)=>{const i=document.createElement(t);return r(i,e),i};class a extends s.Z{constructor(t,e){super(t,e),this.dragStart=NaN,this.regions=[],this.createdRegion=null,this.modifiedRegion=null,this.isResizingLeft=!1,this.isMoving=!1,this.handleMouseDown=t=>{(this.options.draggable||this.options.resizable||this.options.dragSelection)&&(this.dragStart=t.clientX-this.wrapper.getBoundingClientRect().left)},this.handleMouseMove=t=>{const e=t.clientX-this.wrapper.getBoundingClientRect().left;if(this.options.draggable&&this.modifiedRegion&&this.isMoving)return this.moveRegion(this.modifiedRegion,e-this.dragStart),void(this.dragStart=e);if(this.options.resizable&&this.modifiedRegion)this.updateRegion(this.modifiedRegion,this.isResizingLeft?e:void 0,this.isResizingLeft?void 0:e);else if(this.options.dragSelection&&!isNaN(this.dragStart)){const e=t.clientX-this.regionsContainer.getBoundingClientRect().left;e-this.dragStart>=10&&(this.createdRegion?this.updateRegion(this.createdRegion,this.dragStart,e):(this.wrapper.style.pointerEvents="none",this.createdRegion=this.createRegion(this.dragStart,e)))}},this.handleMouseUp=()=>{this.createdRegion&&(this.addRegion(this.createdRegion),this.createdRegion=null),this.modifiedRegion=null,this.isMoving=!1,this.dragStart=NaN,this.wrapper.style.pointerEvents=""},this.options=Object.assign({},n,e),this.regionsContainer=this.initRegionsContainer();const i=this.wavesurfer.once("decode",(()=>{this.wrapper.appendChild(this.regionsContainer)}));this.subscriptions.push(i),this.wrapper.addEventListener("mousedown",this.handleMouseDown),document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp)}destroy(){this.wrapper.removeEventListener("mousedown",this.handleMouseDown),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp,!0),this.regionsContainer.remove(),super.destroy()}initRegionsContainer(){return o("div",{position:"absolute",top:"0",left:"0",width:"100%",height:"100%",zIndex:"3",pointerEvents:"none"})}createRegionElement(t,e,i=""){const s=t===e,n=o("div",{position:"absolute",left:`${t}px`,width:e-t+"px",height:"100%",backgroundColor:s?"":"rgba(0, 0, 0, 0.1)",borderRadius:"2px",boxSizing:"border-box",borderLeft:s?"2px solid rgba(0, 0, 0, 0.5)":"",transition:"background-color 0.2s ease",cursor:this.options.draggable?"move":"",pointerEvents:"all",whiteSpace:s?"nowrap":"",padding:"0.2em"});n.textContent=i;const a=o("div",{position:"absolute",left:"0",top:"0",width:"6px",height:"100%",cursor:this.options.resizable?"ew-resize":"",pointerEvents:"all",borderLeft:s?"":"2px solid rgba(0, 0, 0, 0.5)",borderRadius:"2px 0 0 2px"});n.appendChild(a);const h=a.cloneNode();return r(h,{left:"",right:"0",borderLeft:"",borderRight:s?"":"2px solid rgba(0, 0, 0, 0.5)",borderRadius:"0 2px 2px 0"}),n.appendChild(h),a.addEventListener("mousedown",(t=>{this.options.resizable&&(t.stopPropagation(),this.modifiedRegion=this.regions.find((t=>t.element===n))||null,this.isResizingLeft=!0,this.isMoving=!1)})),h.addEventListener("mousedown",(t=>{this.options.resizable&&(t.stopPropagation(),this.modifiedRegion=this.regions.find((t=>t.element===n))||null,this.isResizingLeft=!1,this.isMoving=!1)})),n.addEventListener("mousedown",(()=>{this.options.draggable&&(this.modifiedRegion=this.regions.find((t=>t.element===n))||null,this.isMoving=!0)})),n.addEventListener("click",(()=>{const t=this.regions.find((t=>t.element===n));t&&this.emit("region-clicked",{region:t})})),this.regionsContainer.appendChild(n),n}createRegion(t,e,i=""){const s=this.wavesurfer.getDuration(),n=this.regionsContainer.clientWidth;return t=Math.max(0,Math.min(t,n-1)),e=Math.max(0,Math.min(e,n-1)),{element:this.createRegionElement(t,e,i),start:t,end:e,startTime:t/n*s,endTime:e/n*s,title:i}}addRegion(t){this.regions.push(t),this.emit("region-created",{region:t})}updateRegion(t,e,i){const s=this.regionsContainer.clientWidth;null!=e&&(e=Math.max(0,Math.min(e,s)),t.start=e,t.element.style.left=`${t.start}px`,t.element.style.width=t.end-t.start+"px",t.startTime=e/this.regionsContainer.clientWidth*this.wavesurfer.getDuration()),null!=i&&(i=Math.max(0,Math.min(i,s)),t.end=i,t.element.style.width=t.end-t.start+"px",t.endTime=i/this.regionsContainer.clientWidth*this.wavesurfer.getDuration()),this.emit("region-updated",{region:t})}moveRegion(t,e){this.updateRegion(t,t.start+e,t.end+e)}add(t,e,i="",s=""){const n=this.wavesurfer.getDuration(),r=this.regionsContainer.clientWidth,o=t/n*r,a=e/n*r,h=this.createRegion(o,a,i);return this.addRegion(h),s&&this.setRegionColor(h,s),h}setRegionColor(t,e){t.element.style[t.startTime===t.endTime?"borderColor":"backgroundColor"]=e}}const h=a},954:(t,e,i)=>{i.d(e,{default:()=>o});var s=i(284);const n={height:20};class r extends s.Z{constructor(t,e){var i;super(t,e),this.options=Object.assign({},n,e),this.container=null!==(i=this.options.container)&&void 0!==i?i:this.wrapper,this.timelineWrapper=this.initTimelineWrapper(),this.container.appendChild(this.timelineWrapper),this.options.duration?this.initTimeline(this.options.duration):this.subscriptions.push(this.wavesurfer.on("decode",(({duration:t})=>{this.initTimeline(t)})))}destroy(){this.timelineWrapper.remove(),super.destroy()}initTimelineWrapper(){return document.createElement("div")}formatTime(t){return t/60>1?`${Math.round(t/60)}:${(t=Math.round(t%60))<10?"0":""}${t}`:""+Math.round(1e3*t)/1e3}defaultTimeInterval(t){return t>=25?1:5*t>=25?5:15*t>=25?15:60*Math.ceil(.5/t)}defaultPrimaryLabelInterval(t){return t>=25?10:5*t>=25?6:4}defaultSecondaryLabelInterval(t){return t>=25?5:2}initTimeline(t){var e,i,s;const n=Math.round(this.timelineWrapper.scrollWidth*devicePixelRatio)/t,r=null!==(e=this.options.timeInterval)&&void 0!==e?e:this.defaultTimeInterval(n),o=null!==(i=this.options.primaryLabelInterval)&&void 0!==i?i:this.defaultPrimaryLabelInterval(n),a=null!==(s=this.options.secondaryLabelInterval)&&void 0!==s?s:this.defaultSecondaryLabelInterval(n),h=document.createElement("div");h.setAttribute("style",`\n height: ${this.options.height}px;\n overflow: hidden;\n display: flex;\n justify-content: space-between;\n align-items: flex-end;\n font-size: ${this.options.height/2}px;\n white-space: nowrap;\n `);const d=document.createElement("div");d.setAttribute("style","\n width: 1px;\n height: 50%;\n display: flex;\n flex-direction: column;\n justify-content: flex-end;\n overflow: visible;\n border-left: 1px solid currentColor;\n opacity: 0.25;\n ");for(let e=0;e<t;e+=r){const t=d.cloneNode(),i=e%o==0;(i||e%a==0)&&(t.style.height="100%",t.style.textIndent="3px",t.textContent=this.formatTime(e),i&&(t.style.opacity="1")),h.appendChild(t)}this.timelineWrapper.appendChild(h),this.emit("ready")}}const o=r}},e={};function i(s){var n=e[s];if(void 0!==n)return n.exports;var r=e[s]={exports:{}};return t[s](r,r.exports,i),r.exports}i.d=(t,e)=>{for(var s in e)i.o(e,s)&&!i.o(t,s)&&Object.defineProperty(t,s,{enumerable:!0,get:e[s]})},i.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var s={};return(()=>{i.d(s,{default:()=>p});var t=i(139);class e extends t.Z{constructor(t){super(),this.timeout=null,this.options=t;let e=null;if("string"==typeof this.options.container?e=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(e=this.options.container),!e)throw new Error("Container not found");const i=document.createElement("div"),s=i.attachShadow({mode:"open"});s.innerHTML=`\n <style>\n :host {\n user-select: none;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .wrapper {\n position: relative;\n width: fit-content;\n min-width: 100%;\n z-index: 2;\n }\n :host canvas {\n display: block;\n height: ${this.options.height}px;\n min-width: 100%;\n image-rendering: pixelated;\n }\n :host .progress {\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 100%;\n pointer-events: none;\n clip-path: inset(0px 100% 0px 0px);\n }\n :host .cursor {\n position: absolute;\n z-index: 3;\n top: 0;\n left: 0;\n height: 100%;\n width: ${this.options.cursorWidth}px;\n background: ${this.options.cursorColor||this.options.progressColor};\n }\n </style>\n\n <div class="scroll">\n <div class="wrapper">\n <canvas></canvas>\n <canvas class="progress"></canvas>\n <div class="cursor"></div>\n </div>\n </div>\n `,this.container=i,this.scrollContainer=s.querySelector(".scroll"),this.wrapper=s.querySelector(".wrapper"),this.mainCanvas=s.querySelector("canvas"),this.ctx=this.mainCanvas.getContext("2d",{desynchronized:!0}),this.progressCanvas=s.querySelector(".progress"),this.cursor=s.querySelector(".cursor"),e.appendChild(i),this.mainCanvas.addEventListener("click",(t=>{const e=this.mainCanvas.getBoundingClientRect(),i=(t.clientX-e.left)/e.width;this.emit("click",{relativeX:i})}))}getContainer(){return this.container.shadowRoot}getWrapper(){return this.wrapper}destroy(){this.container.remove()}delay(t,e=100){return this.timeout&&clearTimeout(this.timeout),new Promise((i=>{this.timeout=setTimeout((()=>{i(t())}),e)}))}renderPeaks(t,e,i){var s,n,r,o,a;return n=this,r=void 0,a=function*(){const{devicePixelRatio:n}=window,{ctx:r}=this,o=null!=this.options.barWidth?this.options.barWidth*n:1,a=null!=this.options.barGap?this.options.barGap*n:this.options.barWidth?o/2:0,h=null!==(s=this.options.barRadius)&&void 0!==s?s:0,d=t[0],l=d.length,c=Math.floor(e/(o+a))/l,u=i/2,p=1===t.length,m=p?d:t[1],g=p&&m.some((t=>t<0)),v=(t,e)=>{let i=0,s=0,n=0;r.beginPath(),r.fillStyle=this.options.waveColor,r.roundRect||(r.roundRect=r.fillRect);for(let l=t;l<e;l++){const t=Math.round(l*c);if(t>i){const e=Math.round(s*u),d=Math.round(n*u);r.roundRect(i*(o+a),u-e,o,e+d||1,h),i=t,s=0,n=0}const e=g?d[l]:Math.abs(d[l]),p=g?m[l]:Math.abs(m[l]);e>s&&(s=e),(g?p<-n:p>n)&&(n=p<0?-p:p)}r.fill(),r.closePath()};r.clearRect(0,0,e,i);const{scrollLeft:f,scrollWidth:y,clientWidth:C}=this.scrollContainer,w=l/y,b=Math.floor(f*w),x=Math.ceil((f+C)*w);v(b,x),this.createProgressMask(),b>0&&(yield this.delay((()=>{v(0,b)}))),x<l&&(yield this.delay((()=>{v(x,l)}))),this.delay((()=>{this.createProgressMask()}))},new((o=void 0)||(o=Promise))((function(t,e){function i(t){try{h(a.next(t))}catch(t){e(t)}}function s(t){try{h(a.throw(t))}catch(t){e(t)}}function h(e){var n;e.done?t(e.value):(n=e.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}h((a=a.apply(n,r||[])).next())}))}createProgressMask(){const t=this.progressCanvas.getContext("2d",{desynchronized:!0});this.progressCanvas.width=this.mainCanvas.width,this.progressCanvas.height=this.mainCanvas.height,t.drawImage(this.mainCanvas,0,0),t.globalCompositeOperation="source-in",t.fillStyle=this.options.progressColor,t.fillRect(0,0,this.progressCanvas.width,this.progressCanvas.height)}render(t){const{devicePixelRatio:e}=window,i=this.options.fillParent?this.scrollContainer.clientWidth*e:0,s=t.duration*this.options.minPxPerSec,n=s>i,r=Math.max(1,n?s:i),{height:o}=this.options;this.mainCanvas.width=r,this.mainCanvas.height=o,this.mainCanvas.style.width=Math.floor(s/e)+"px";const a=[t.getChannelData(0)];t.numberOfChannels>1&&a.push(t.getChannelData(1)),this.renderPeaks(a,r,o)}zoom(t,e){const i=this.cursor.getBoundingClientRect().left;this.options.minPxPerSec=e,this.render(t);const s=this.cursor.getBoundingClientRect().left;this.scrollContainer.scrollLeft+=s-i}renderProgress(t,e=!1){if(this.progressCanvas.style.clipPath=`inset(0 ${100-100*t}% 0 0)`,this.cursor.style.left=100*t+"%",this.cursor.style.marginLeft=t>0?100===Math.round(100*t)?`-${this.cursor.offsetWidth}px`:`-${this.cursor.offsetWidth/2}px`:"",this.cursor.scrollIntoView(),e){const e=this.container.clientWidth/2,i=this.mainCanvas.clientWidth;i*t>=e&&(this.scrollContainer.scrollLeft=i*t-e)}}}const n=e;class r extends t.Z{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const o=r;const a={height:128,waveColor:"#999",progressColor:"#555",cursorWidth:1,minPxPerSec:0,fillParent:!0,interactive:!0};class h extends t.Z{static create(t){return new h(t)}constructor(t){var e;super(),this.plugins=[],this.subscriptions=[],this.decodedData=null,this.options=Object.assign({},a,t),this.fetcher=new class{load(t){return e=this,i=void 0,n=function*(){return fetch(t).then((t=>t.arrayBuffer()))},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}},this.decoder=new class{initAudioContext(t){this.audioCtx=new AudioContext({latencyHint:"playback",sampleRate:t})}constructor(){this.audioCtx=null;try{this.initAudioContext(3e3)}catch(t){this.initAudioContext(8e3)}}decode(t){return e=this,i=void 0,n=function*(){if(!this.audioCtx)throw new Error("AudioContext is not initialized");return this.audioCtx.decodeAudioData(t)},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}destroy(){var t;null===(t=this.audioCtx)||void 0===t||t.close(),this.audioCtx=null}},this.timer=new o,this.player=new class{constructor({media:t,autoplay:e}){this.isExternalMedia=!1,this.hasPlayedOnce=!1,t?(this.media=t,this.isExternalMedia=!0):this.media=document.createElement("audio");const i=this.on("play",(()=>{this.hasPlayedOnce=!0,i()}));e&&(this.media.autoplay=!0)}on(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e)}destroy(){this.media.pause(),this.isExternalMedia||this.media.remove()}loadUrl(t){this.media.src=t}getCurrentTime(){return this.media.currentTime}play(){this.media.play()}pause(){this.media.pause()}isPlaying(){return this.media.currentTime>0&&!this.media.paused&&!this.media.ended}seekTo(t){if(this.media){const{hasPlayedOnce:e}=this;e||this.media.play(),this.media.currentTime=t,e||this.media.pause()}}getDuration(){return this.media.duration}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}getMediaElement(){return this.media}}({media:this.options.media,autoplay:this.options.autoplay}),this.renderer=new n({container:this.options.container,height:this.options.height,waveColor:this.options.waveColor,progressColor:this.options.progressColor,cursorColor:this.options.cursorColor,cursorWidth:this.options.cursorWidth,minPxPerSec:this.options.minPxPerSec,fillParent:this.options.fillParent,barWidth:this.options.barWidth,barGap:this.options.barGap,barRadius:this.options.barRadius}),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReadyEvent();const i=this.options.url||(null===(e=this.options.media)||void 0===e?void 0:e.src);i&&this.load(i,this.options.peaks,this.options.duration)}initPlayerEvents(){this.subscriptions.push(this.player.on("timeupdate",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),this.emit("timeupdate",{currentTime:t})})),this.player.on("play",(()=>{this.emit("play"),this.timer.start()})),this.player.on("pause",(()=>{this.emit("pause"),this.timer.stop()})),this.player.on("canplay",(()=>{this.emit("canplay",{duration:this.getDuration()})})),this.player.on("seeking",(()=>{this.emit("seeking",{currentTime:this.getCurrentTime()})})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",(({relativeX:t})=>{if(this.options.interactive){const e=this.getDuration()*t;this.seekTo(e),this.emit("seekClick",{currentTime:this.getCurrentTime()})}})))}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),!0),this.emit("timeupdate",{currentTime:t})})))}initReadyEvent(){let t=!1,e=!1;const i=()=>{t&&e&&this.emit("ready",{duration:this.getDuration()})};this.subscriptions.push(this.on("decode",(()=>{t=!0,i()})),this.on("canplay",(()=>{e=!0,i()})),this.player.on("waiting",(()=>{e=!1,t=!1})))}load(t,e,i){return s=this,n=void 0,o=function*(){if(this.player.loadUrl(t),null==e){const e=yield this.fetcher.load(t),i=yield this.decoder.decode(e);this.decodedData=i}else i||(i=(yield new Promise((t=>{this.player.on("canplay",(()=>t(this.getDuration())),{once:!0})})))||0),this.decodedData={duration:i,numberOfChannels:e.length,sampleRate:e[0].length/i,getChannelData:t=>e[t]};this.renderer.render(this.decodedData),this.emit("decode",{duration:this.decodedData.duration})},new((r=void 0)||(r=Promise))((function(t,e){function i(t){try{h(o.next(t))}catch(t){e(t)}}function a(t){try{h(o.throw(t))}catch(t){e(t)}}function h(e){var s;e.done?t(e.value):(s=e.value,s instanceof r?s:new r((function(t){t(s)}))).then(i,a)}h((o=o.apply(s,n||[])).next())}));var s,n,r,o}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(this.decodedData,t)}play(){this.player.play()}pause(){this.player.pause()}seekTo(t){this.player.seekTo(t)}isPlaying(){return this.player.isPlaying()}getDuration(){var t;return this.player.getDuration()||(null===(t=this.decodedData)||void 0===t?void 0:t.duration)||0}getCurrentTime(){return this.player.getCurrentTime()}getVolume(){return this.player.getVolume()}setVolume(t){this.player.setVolume(t)}getMuted(){return this.player.getMuted()}setMuted(t){this.player.setMuted(t)}getPlaybackRate(){return this.player.getPlaybackRate()}setPlaybackRate(t,e){this.player.setPlaybackRate(t,e)}registerPlugin(t,e){const i=new t({wavesurfer:this,container:this.renderer.getContainer(),wrapper:this.renderer.getWrapper()},e);return this.plugins.push(i),i}getDecodedData(){return this.decodedData}getMediaElement(){return this.player.getMediaElement()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.plugins.forEach((t=>t.destroy())),this.timer.destroy(),this.player.destroy(),this.decoder.destroy(),this.renderer.destroy()}}const d=h;var l=i(76),c=i(954);class u{static create(t,e){return new u(t,e)}constructor(t,e){this.audios=[],this.wavesurfers=[],this.durations=[],this.currentTime=0,this.maxDuration=0,this.isDragging=!1,this.frameRequest=null,this.tracks=t.map((t=>Object.assign(Object.assign({},t),{startPosition:t.startPosition||0,peaks:t.peaks||(t.url?void 0:[new Float32Array])}))),this.options=e,this.rendering=function(t,e){let i=0,s=[];const n=document.createElement("div");n.setAttribute("style","width: 100%; overflow-x: scroll; overflow-y: hidden; user-select: none;");const r=document.createElement("div");r.style.position="relative",n.appendChild(r),e.container.appendChild(n);const o=document.createElement("div");o.setAttribute("style","width: 1px; height: 100%; position: absolute; z-index: 10; top: 0; left: 0"),o.style.backgroundColor=e.cursorColor||"#000",r.appendChild(o);const{clientWidth:a}=r,h=t.map(((t,i)=>{const s=document.createElement("div");if(e.trackBorderColor&&i>0){const t=document.createElement("div");t.setAttribute("style",`width: 100%; height: 2px; background-color: ${e.trackBorderColor}`),r.appendChild(t)}return e.trackBackground&&(s.style.background=e.trackBackground),r.appendChild(s),s})),d=()=>{h.forEach(((e,n)=>{const r=t[n].startPosition*i;e.style.width=s[n]*i+"px",e.style.transform=`translateX(${r}px)`,t[n].draggable&&(e.style.cursor="ew-resize")}))};return{containers:h,setContainerOffsets:d,setMainWidth:(t,n)=>{s=t,i=Math.max(e.minPxPerSec||0,a/n);const o=i*n;r.style.width=`${o}px`,d()},updateCursor:t=>{o.style.left=`${Math.min(100,100*t)}%`},addClickHandler:t=>{r.addEventListener("click",(e=>{const i=r.getBoundingClientRect(),s=(e.clientX-i.left)/r.offsetWidth;t(s)}))},destroy:()=>{n.remove()}}}(this.tracks,this.options),this.initAudios().then((t=>{this.durations=t;const e=this.tracks.reduce(((e,i,s)=>Math.max(e,i.startPosition+t[s])),0);this.rendering.setMainWidth(t,e),this.rendering.addClickHandler((t=>{this.isDragging||this.seekTo(t*e)})),this.maxDuration=e,this.initWavesurfers(),this.initTimeline(),this.rendering.containers.forEach(((t,e)=>{const i=function(t,e){const i=t.parentElement;if(!i)return;let s=null;t.addEventListener("mousedown",(t=>{const e=i.getBoundingClientRect();s=t.clientX-e.left}));const n=t=>{null!=s&&(t.stopPropagation(),s=null)},r=t=>{if(null==s)return;const n=i.getBoundingClientRect(),r=t.clientX-n.left,o=r-s;(o>1||o<-1)&&(s=r,e(o/i.offsetWidth))};return document.body.addEventListener("mouseup",n),document.body.addEventListener("mousemove",r),{destroy:()=>{document.body.removeEventListener("mouseup",n),document.body.removeEventListener("mousemove",r)}}}(t,(t=>this.onDrag(e,t)));this.wavesurfers[e].once("destroy",(()=>{null==i||i.destroy()}))}))}))}initAudios(){return this.audios=this.tracks.map((t=>new Audio(t.url))),Promise.all(this.audios.map((t=>new Promise((e=>{if(!t.src)return e(0);t.addEventListener("canplay",(()=>{e(t.duration)}),{once:!0})})))))}initWavesurfers(){const t=this.tracks.map(((t,e)=>{const i=d.create(Object.assign(Object.assign({},this.options),{container:this.rendering.containers[e],minPxPerSec:0,media:this.audios[e],peaks:t.peaks,cursorColor:"transparent",interactive:!1})),s=i.registerPlugin(l.default,{draggable:!1,resizable:!1,dragSelection:!1});return i.once("decode",(()=>{t.startCue&&s.add(t.startCue,t.startCue,"start",this.options.cueColor),t.endCue&&s.add(t.endCue,t.endCue,"end",this.options.cueColor),(t.markers||[]).forEach((t=>{s.add(t.time,t.time,t.label,t.color)}))})),i}));this.wavesurfers=t}initTimeline(){this.wavesurfers[0].registerPlugin(c.default,{duration:this.maxDuration,container:this.rendering.containers[0].parentElement})}updatePosition(t){this.currentTime=t,this.rendering.updateCursor(t/this.maxDuration);const e=!this.isPlaying();this.tracks.forEach(((i,s)=>{const n=this.audios[s],r=this.durations[s],o=t-i.startPosition;Math.abs(n.currentTime-o)>.3&&(n.currentTime=o),e||o<0||o>r?!n.paused&&n.pause():e||n.paused&&n.play();const a=o>=(i.startCue||0)&&o<(i.endCue||1/0)?1:0;a!==n.volume&&(n.volume=a)}))}onDrag(t,e){var i,s;const n=this.tracks[t];if(!n.draggable)return;this.isDragging=!0,setTimeout((()=>this.isDragging=!1),600);const r=n.startPosition+e*this.maxDuration,o=this.tracks.reduce(((e,i,s)=>s!==t&&i.url?Math.min(e,i.startPosition):e),1/0);r+this.durations[t]>=o&&(n.startPosition=r,this.rendering.setContainerOffsets(),this.updatePosition(this.currentTime),null===(s=(i=this.options).onTrackPositionUpdate)||void 0===s||s.call(i,n.id,n.startPosition))}findCurrentTracks(){const t=[];if(this.tracks.forEach(((e,i)=>{e.url&&this.currentTime>=e.startPosition&&this.currentTime<e.startPosition+this.durations[i]&&t.push(i)})),0===t.length){const e=Math.min(...this.tracks.filter((t=>t.url)).map((t=>t.startPosition)));t.push(this.tracks.findIndex((t=>t.startPosition===e)))}return t}startSync(){const t=()=>{const e=this.audios.reduce(((t,e,i)=>(e.paused||(t=Math.max(t,e.currentTime+this.tracks[i].startPosition)),t)),this.currentTime);e>this.currentTime&&this.updatePosition(e),this.frameRequest=requestAnimationFrame(t)};t()}play(){this.startSync(),this.findCurrentTracks().forEach((t=>{var e;null===(e=this.audios[t])||void 0===e||e.play()}))}pause(){this.audios.forEach((t=>t.pause()))}isPlaying(){return this.audios.some((t=>!t.paused))}seekTo(t){const e=this.isPlaying();this.updatePosition(t),e&&this.play()}zoom(t){this.options.minPxPerSec=t,this.wavesurfers.forEach(((e,i)=>this.tracks[i].url&&e.zoom(t))),this.rendering.setMainWidth(this.durations,this.maxDuration),this.rendering.setContainerOffsets()}destroy(){this.frameRequest&&cancelAnimationFrame(this.frameRequest),this.rendering.destroy(),this.audios.forEach((t=>{t.pause(),t.src=""})),this.wavesurfers.forEach((t=>{t.destroy()}))}}const p=u})(),s.default})()));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Regions=t():e.Regions=t()}(WaveSurfer,(()=>(()=>{"use strict";var e={284:(e,t,i)=>{i.d(t,{Z:()=>o});var n=i(139);class s extends n.Z{constructor(e,t){super(),this.subscriptions=[],this.wavesurfer=e.wavesurfer,this.container=e.container,this.options=t}destroy(){this.subscriptions.forEach((e=>e()))}}const o=s},139:(e,t,i)=>{i.d(t,{Z:()=>n});const n=class{constructor(){this.eventTarget=new EventTarget}emit(e,t){const i=new CustomEvent(String(e),{detail:t});this.eventTarget.dispatchEvent(i)}on(e,t,i){const n=e=>{e instanceof CustomEvent&&t(e.detail)},s=String(e);return this.eventTarget.addEventListener(s,n,{once:i}),()=>this.eventTarget.removeEventListener(s,n)}once(e,t){return this.on(e,t,!0)}}}},t={};function i(n){var s=t[n];if(void 0!==s)return s.exports;var o=t[n]={exports:{}};return e[n](o,o.exports,i),o.exports}i.d=(e,t)=>{for(var n in t)i.o(t,n)&&!i.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},i.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n={};return(()=>{i.d(n,{default:()=>a});var e=i(284);const t={dragSelection:!0,draggable:!0,resizable:!0},s=(e,t)=>{for(const i in t)e.style[i]=t[i]||""},o=(e,t)=>{const i=document.createElement(e);return s(i,t),i};class r extends e.Z{constructor(e,i){super(e,i),this.dragStart=NaN,this.regions=[],this.createdRegion=null,this.modifiedRegion=null,this.isResizingLeft=!1,this.isMoving=!1,this.handleMouseDown=e=>{(this.options.draggable||this.options.resizable||this.options.dragSelection)&&(this.dragStart=e.clientX-this.
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Regions=t():e.Regions=t()}(WaveSurfer,(()=>(()=>{"use strict";var e={284:(e,t,i)=>{i.d(t,{Z:()=>o});var n=i(139);class s extends n.Z{constructor(e,t){super(),this.subscriptions=[],this.wavesurfer=e.wavesurfer,this.container=e.container,this.wrapper=e.wrapper,this.options=t}destroy(){this.subscriptions.forEach((e=>e()))}}const o=s},139:(e,t,i)=>{i.d(t,{Z:()=>n});const n=class{constructor(){this.eventTarget=new EventTarget}emit(e,t){const i=new CustomEvent(String(e),{detail:t});this.eventTarget.dispatchEvent(i)}on(e,t,i){const n=e=>{e instanceof CustomEvent&&t(e.detail)},s=String(e);return this.eventTarget.addEventListener(s,n,{once:i}),()=>this.eventTarget.removeEventListener(s,n)}once(e,t){return this.on(e,t,!0)}}}},t={};function i(n){var s=t[n];if(void 0!==s)return s.exports;var o=t[n]={exports:{}};return e[n](o,o.exports,i),o.exports}i.d=(e,t)=>{for(var n in t)i.o(t,n)&&!i.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},i.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n={};return(()=>{i.d(n,{default:()=>a});var e=i(284);const t={dragSelection:!0,draggable:!0,resizable:!0},s=(e,t)=>{for(const i in t)e.style[i]=t[i]||""},o=(e,t)=>{const i=document.createElement(e);return s(i,t),i};class r extends e.Z{constructor(e,i){super(e,i),this.dragStart=NaN,this.regions=[],this.createdRegion=null,this.modifiedRegion=null,this.isResizingLeft=!1,this.isMoving=!1,this.handleMouseDown=e=>{(this.options.draggable||this.options.resizable||this.options.dragSelection)&&(this.dragStart=e.clientX-this.wrapper.getBoundingClientRect().left)},this.handleMouseMove=e=>{const t=e.clientX-this.wrapper.getBoundingClientRect().left;if(this.options.draggable&&this.modifiedRegion&&this.isMoving)return this.moveRegion(this.modifiedRegion,t-this.dragStart),void(this.dragStart=t);if(this.options.resizable&&this.modifiedRegion)this.updateRegion(this.modifiedRegion,this.isResizingLeft?t:void 0,this.isResizingLeft?void 0:t);else if(this.options.dragSelection&&!isNaN(this.dragStart)){const t=e.clientX-this.regionsContainer.getBoundingClientRect().left;t-this.dragStart>=10&&(this.createdRegion?this.updateRegion(this.createdRegion,this.dragStart,t):(this.wrapper.style.pointerEvents="none",this.createdRegion=this.createRegion(this.dragStart,t)))}},this.handleMouseUp=()=>{this.createdRegion&&(this.addRegion(this.createdRegion),this.createdRegion=null),this.modifiedRegion=null,this.isMoving=!1,this.dragStart=NaN,this.wrapper.style.pointerEvents=""},this.options=Object.assign({},t,i),this.regionsContainer=this.initRegionsContainer();const n=this.wavesurfer.once("decode",(()=>{this.wrapper.appendChild(this.regionsContainer)}));this.subscriptions.push(n),this.wrapper.addEventListener("mousedown",this.handleMouseDown),document.addEventListener("mousemove",this.handleMouseMove),document.addEventListener("mouseup",this.handleMouseUp)}destroy(){this.wrapper.removeEventListener("mousedown",this.handleMouseDown),document.removeEventListener("mousemove",this.handleMouseMove),document.removeEventListener("mouseup",this.handleMouseUp,!0),this.regionsContainer.remove(),super.destroy()}initRegionsContainer(){return o("div",{position:"absolute",top:"0",left:"0",width:"100%",height:"100%",zIndex:"3",pointerEvents:"none"})}createRegionElement(e,t,i=""){const n=e===t,r=o("div",{position:"absolute",left:`${e}px`,width:t-e+"px",height:"100%",backgroundColor:n?"":"rgba(0, 0, 0, 0.1)",borderRadius:"2px",boxSizing:"border-box",borderLeft:n?"2px solid rgba(0, 0, 0, 0.5)":"",transition:"background-color 0.2s ease",cursor:this.options.draggable?"move":"",pointerEvents:"all",whiteSpace:n?"nowrap":"",padding:"0.2em"});r.textContent=i;const a=o("div",{position:"absolute",left:"0",top:"0",width:"6px",height:"100%",cursor:this.options.resizable?"ew-resize":"",pointerEvents:"all",borderLeft:n?"":"2px solid rgba(0, 0, 0, 0.5)",borderRadius:"2px 0 0 2px"});r.appendChild(a);const d=a.cloneNode();return s(d,{left:"",right:"0",borderLeft:"",borderRight:n?"":"2px solid rgba(0, 0, 0, 0.5)",borderRadius:"0 2px 2px 0"}),r.appendChild(d),a.addEventListener("mousedown",(e=>{this.options.resizable&&(e.stopPropagation(),this.modifiedRegion=this.regions.find((e=>e.element===r))||null,this.isResizingLeft=!0,this.isMoving=!1)})),d.addEventListener("mousedown",(e=>{this.options.resizable&&(e.stopPropagation(),this.modifiedRegion=this.regions.find((e=>e.element===r))||null,this.isResizingLeft=!1,this.isMoving=!1)})),r.addEventListener("mousedown",(()=>{this.options.draggable&&(this.modifiedRegion=this.regions.find((e=>e.element===r))||null,this.isMoving=!0)})),r.addEventListener("click",(()=>{const e=this.regions.find((e=>e.element===r));e&&this.emit("region-clicked",{region:e})})),this.regionsContainer.appendChild(r),r}createRegion(e,t,i=""){const n=this.wavesurfer.getDuration(),s=this.regionsContainer.clientWidth;return e=Math.max(0,Math.min(e,s-1)),t=Math.max(0,Math.min(t,s-1)),{element:this.createRegionElement(e,t,i),start:e,end:t,startTime:e/s*n,endTime:t/s*n,title:i}}addRegion(e){this.regions.push(e),this.emit("region-created",{region:e})}updateRegion(e,t,i){const n=this.regionsContainer.clientWidth;null!=t&&(t=Math.max(0,Math.min(t,n)),e.start=t,e.element.style.left=`${e.start}px`,e.element.style.width=e.end-e.start+"px",e.startTime=t/this.regionsContainer.clientWidth*this.wavesurfer.getDuration()),null!=i&&(i=Math.max(0,Math.min(i,n)),e.end=i,e.element.style.width=e.end-e.start+"px",e.endTime=i/this.regionsContainer.clientWidth*this.wavesurfer.getDuration()),this.emit("region-updated",{region:e})}moveRegion(e,t){this.updateRegion(e,e.start+t,e.end+t)}add(e,t,i="",n=""){const s=this.wavesurfer.getDuration(),o=this.regionsContainer.clientWidth,r=e/s*o,a=t/s*o,d=this.createRegion(r,a,i);return this.addRegion(d),n&&this.setRegionColor(d,n),d}setRegionColor(e,t){e.element.style[e.startTime===e.endTime?"borderColor":"backgroundColor"]=t}}const a=r})(),n.default})()));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Timeline=t():e.Timeline=t()}(WaveSurfer,(()=>(()=>{"use strict";var e={284:(e,t,n)=>{n.d(t,{Z:()=>o});var i=n(139);class r extends i.Z{constructor(e,t){super(),this.subscriptions=[],this.wavesurfer=e.wavesurfer,this.container=e.container,this.options=t}destroy(){this.subscriptions.forEach((e=>e()))}}const o=r},139:(e,t,n)=>{n.d(t,{Z:()=>i});const i=class{constructor(){this.eventTarget=new EventTarget}emit(e,t){const n=new CustomEvent(String(e),{detail:t});this.eventTarget.dispatchEvent(n)}on(e,t,n){const i=e=>{e instanceof CustomEvent&&t(e.detail)},r=String(e);return this.eventTarget.addEventListener(r,i,{once:n}),()=>this.eventTarget.removeEventListener(r,i)}once(e,t){return this.on(e,t,!0)}}}},t={};function n(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i](o,o.exports,n),o.exports}n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var i={};return(()=>{n.d(i,{default:()=>o});var e=n(284);const t={height:20};class r extends e.Z{constructor(e,n){var i;super(e,n),this.options=Object.assign({},t,n),this.container=null!==(i=this.options.container)&&void 0!==i?i:this.
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Timeline=t():e.Timeline=t()}(WaveSurfer,(()=>(()=>{"use strict";var e={284:(e,t,n)=>{n.d(t,{Z:()=>o});var i=n(139);class r extends i.Z{constructor(e,t){super(),this.subscriptions=[],this.wavesurfer=e.wavesurfer,this.container=e.container,this.wrapper=e.wrapper,this.options=t}destroy(){this.subscriptions.forEach((e=>e()))}}const o=r},139:(e,t,n)=>{n.d(t,{Z:()=>i});const i=class{constructor(){this.eventTarget=new EventTarget}emit(e,t){const n=new CustomEvent(String(e),{detail:t});this.eventTarget.dispatchEvent(n)}on(e,t,n){const i=e=>{e instanceof CustomEvent&&t(e.detail)},r=String(e);return this.eventTarget.addEventListener(r,i,{once:n}),()=>this.eventTarget.removeEventListener(r,i)}once(e,t){return this.on(e,t,!0)}}}},t={};function n(i){var r=t[i];if(void 0!==r)return r.exports;var o=t[i]={exports:{}};return e[i](o,o.exports,n),o.exports}n.d=(e,t)=>{for(var i in t)n.o(t,i)&&!n.o(e,i)&&Object.defineProperty(e,i,{enumerable:!0,get:t[i]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var i={};return(()=>{n.d(i,{default:()=>o});var e=n(284);const t={height:20};class r extends e.Z{constructor(e,n){var i;super(e,n),this.options=Object.assign({},t,n),this.container=null!==(i=this.options.container)&&void 0!==i?i:this.wrapper,this.timelineWrapper=this.initTimelineWrapper(),this.container.appendChild(this.timelineWrapper),this.options.duration?this.initTimeline(this.options.duration):this.subscriptions.push(this.wavesurfer.on("decode",(({duration:e})=>{this.initTimeline(e)})))}destroy(){this.timelineWrapper.remove(),super.destroy()}initTimelineWrapper(){return document.createElement("div")}formatTime(e){return e/60>1?`${Math.round(e/60)}:${(e=Math.round(e%60))<10?"0":""}${e}`:""+Math.round(1e3*e)/1e3}defaultTimeInterval(e){return e>=25?1:5*e>=25?5:15*e>=25?15:60*Math.ceil(.5/e)}defaultPrimaryLabelInterval(e){return e>=25?10:5*e>=25?6:4}defaultSecondaryLabelInterval(e){return e>=25?5:2}initTimeline(e){var t,n,i;const r=Math.round(this.timelineWrapper.scrollWidth*devicePixelRatio)/e,o=null!==(t=this.options.timeInterval)&&void 0!==t?t:this.defaultTimeInterval(r),s=null!==(n=this.options.primaryLabelInterval)&&void 0!==n?n:this.defaultPrimaryLabelInterval(r),a=null!==(i=this.options.secondaryLabelInterval)&&void 0!==i?i:this.defaultSecondaryLabelInterval(r),l=document.createElement("div");l.setAttribute("style",`\n height: ${this.options.height}px;\n overflow: hidden;\n display: flex;\n justify-content: space-between;\n align-items: flex-end;\n font-size: ${this.options.height/2}px;\n white-space: nowrap;\n `);const d=document.createElement("div");d.setAttribute("style","\n width: 1px;\n height: 50%;\n display: flex;\n flex-direction: column;\n justify-content: flex-end;\n overflow: visible;\n border-left: 1px solid currentColor;\n opacity: 0.25;\n ");for(let t=0;t<e;t+=o){const e=d.cloneNode(),n=t%s==0;(n||t%a==0)&&(e.style.height="100%",e.style.textIndent="3px",e.textContent=this.formatTime(t),n&&(e.style.opacity="1")),l.appendChild(e)}this.timelineWrapper.appendChild(l),this.emit("ready")}}const o=r})(),i.default})()));
|
package/dist/wavesurfer.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.WaveSurfer=e():t.WaveSurfer=e()}(this,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>a});const i=class{constructor(){this.eventTarget=new EventTarget}emit(t,e){const i=new CustomEvent(String(t),{detail:e});this.eventTarget.dispatchEvent(i)}on(t,e,i){const s=t=>{t instanceof CustomEvent&&e(t.detail)},n=String(t);return this.eventTarget.addEventListener(n,s,{once:i}),()=>this.eventTarget.removeEventListener(n,s)}once(t,e){return this.on(t,e,!0)}};const s=class extends i{constructor(t){super(),this.timeout=null,this.options=t;let e=null;if("string"==typeof this.options.container?e=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(e=this.options.container),!e)throw new Error("Container not found");const i=document.createElement("div"),s=i.attachShadow({mode:"open"});s.innerHTML=`\n <style>\n :host {\n user-select: none;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .wrapper {\n position: relative;\n width: fit-content;\n min-width: 100%;\n z-index: 2;\n }\n :host canvas {\n display: block;\n height: ${this.options.height}px;\n min-width: 100%;\n image-rendering: pixelated;\n }\n :host .progress {\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 100%;\n pointer-events: none;\n clip-path: inset(0px 100% 0px 0px);\n }\n :host .cursor {\n position: absolute;\n z-index: 3;\n top: 0;\n left: 0;\n height: 100%;\n border-right: 1px solid ${this.options.cursorColor||this.options.progressColor};\n margin-left: -1px;\n }\n </style>\n\n <div class="scroll">\n <div class="wrapper">\n <canvas></canvas>\n <canvas class="progress"></canvas>\n <div class="cursor"></div>\n </div>\n </div>\n `,this.container=i,this.scrollContainer=s.querySelector(".scroll"),this.mainCanvas=s.querySelector("canvas"),this.ctx=this.mainCanvas.getContext("2d",{desynchronized:!0}),this.progressCanvas=s.querySelector(".progress"),this.cursor=s.querySelector(".cursor"),e.appendChild(i),this.mainCanvas.addEventListener("click",(t=>{const e=this.mainCanvas.getBoundingClientRect(),i=(t.clientX-e.left)/e.width;this.emit("click",{relativeX:i})}))}getContainer(){return this.scrollContainer.querySelector(".wrapper")}destroy(){this.container.remove()}delay(t,e=100){return this.timeout&&clearTimeout(this.timeout),new Promise((i=>{this.timeout=setTimeout((()=>{i(t())}),e)}))}renderPeaks(t,e,i){var s,n,o,r,a;return n=this,o=void 0,a=function*(){const{devicePixelRatio:n}=window,{ctx:o}=this,r=null!=this.options.barWidth?this.options.barWidth*n:1,a=null!=this.options.barGap?this.options.barGap*n:this.options.barWidth?r/2:0,h=null!==(s=this.options.barRadius)&&void 0!==s?s:0,l=t[0],c=l.length,d=Math.floor(e/(r+a))/c,u=i/2,p=1===t.length,m=p?l:t[1],y=p&&m.some((t=>t<0)),v=(t,e)=>{let i=0,s=0,n=0;o.beginPath(),o.fillStyle=this.options.waveColor,o.roundRect||(o.roundRect=o.fillRect);for(let c=t;c<e;c++){const t=Math.round(c*d);if(t>i){const e=Math.round(s*u),l=Math.round(n*u);o.roundRect(i*(r+a),u-e,r,e+l||1,h),i=t,s=0,n=0}const e=y?l[c]:Math.abs(l[c]),p=y?m[c]:Math.abs(m[c]);e>s&&(s=e),(y?p<-n:p>n)&&(n=p<0?-p:p)}o.fill(),o.closePath()};o.clearRect(0,0,e,i);const{scrollLeft:g,scrollWidth:f,clientWidth:C}=this.scrollContainer,w=c/f,x=Math.floor(g*w),b=Math.ceil((g+C)*w);v(x,b),this.createProgressMask(),x>0&&(yield this.delay((()=>{v(0,x)}))),b<c&&(yield this.delay((()=>{v(b,c)}))),this.delay((()=>{this.createProgressMask()}))},new((r=void 0)||(r=Promise))((function(t,e){function i(t){try{h(a.next(t))}catch(t){e(t)}}function s(t){try{h(a.throw(t))}catch(t){e(t)}}function h(e){var n;e.done?t(e.value):(n=e.value,n instanceof r?n:new r((function(t){t(n)}))).then(i,s)}h((a=a.apply(n,o||[])).next())}))}createProgressMask(){const t=this.progressCanvas.getContext("2d",{desynchronized:!0});this.progressCanvas.width=this.mainCanvas.width,this.progressCanvas.height=this.mainCanvas.height,t.drawImage(this.mainCanvas,0,0),t.globalCompositeOperation="source-in",t.fillStyle=this.options.progressColor,t.fillRect(0,0,this.progressCanvas.width,this.progressCanvas.height)}render(t){const{devicePixelRatio:e}=window,i=this.options.fillParent?this.scrollContainer.clientWidth*e:0,s=t.duration*this.options.minPxPerSec,n=s>i,o=Math.max(1,n?s:i),{height:r}=this.options;this.mainCanvas.width=o,this.mainCanvas.height=r,this.mainCanvas.style.width=Math.floor(s/e)+"px";const a=[t.getChannelData(0)];t.numberOfChannels>1&&a.push(t.getChannelData(1)),this.renderPeaks(a,o,r)}zoom(t,e){const i=this.cursor.getBoundingClientRect().left;this.options.minPxPerSec=e,this.render(t);const s=this.cursor.getBoundingClientRect().left;this.scrollContainer.scrollLeft+=s-i}renderProgress(t,e=!1){if(this.progressCanvas.style.clipPath=`inset(0 ${100-100*t}% 0 0)`,this.cursor.style.left=100*t+"%",e){const e=this.container.clientWidth/2,i=this.mainCanvas.clientWidth;i*t>=e&&(this.scrollContainer.scrollLeft=i*t-e)}}},n=class extends i{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}};const o={height:128,waveColor:"#999",progressColor:"#555",minPxPerSec:0,fillParent:!0,interactive:!0};class r extends i{static create(t){return new r(t)}constructor(t){var e;super(),this.plugins=[],this.subscriptions=[],this.decodedData=null,this.options=Object.assign({},o,t),this.fetcher=new class{load(t){return e=this,i=void 0,n=function*(){return fetch(t).then((t=>t.arrayBuffer()))},new((s=void 0)||(s=Promise))((function(t,o){function r(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(r,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}},this.decoder=new class{initAudioContext(t){this.audioCtx=new AudioContext({latencyHint:"playback",sampleRate:t})}constructor(){this.audioCtx=null;try{this.initAudioContext(3e3)}catch(t){this.initAudioContext(8e3)}}decode(t){return e=this,i=void 0,n=function*(){if(!this.audioCtx)throw new Error("AudioContext is not initialized");return this.audioCtx.decodeAudioData(t)},new((s=void 0)||(s=Promise))((function(t,o){function r(t){try{h(n.next(t))}catch(t){o(t)}}function a(t){try{h(n.throw(t))}catch(t){o(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(r,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}destroy(){var t;null===(t=this.audioCtx)||void 0===t||t.close(),this.audioCtx=null}},this.timer=new n,this.player=new class{constructor({media:t,autoplay:e}){this.isExternalMedia=!1,this.hasPlayedOnce=!1,t?(this.media=t,this.isExternalMedia=!0):this.media=document.createElement("audio");const i=this.on("play",(()=>{this.hasPlayedOnce=!0,i()}));e&&(this.media.autoplay=!0)}on(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e)}destroy(){this.media.pause(),this.isExternalMedia||this.media.remove()}loadUrl(t){this.media.src=t}getCurrentTime(){return this.media.currentTime}play(){this.media.play()}pause(){this.media.pause()}isPlaying(){return this.media.currentTime>0&&!this.media.paused&&!this.media.ended}seekTo(t){if(this.media){const{hasPlayedOnce:e}=this;e||this.media.play(),this.media.currentTime=t,e||this.media.pause()}}getDuration(){return this.media.duration}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}}({media:this.options.media,autoplay:this.options.autoplay}),this.renderer=new s({container:this.options.container,height:this.options.height,waveColor:this.options.waveColor,progressColor:this.options.progressColor,cursorColor:this.options.cursorColor,minPxPerSec:this.options.minPxPerSec,fillParent:this.options.fillParent,barWidth:this.options.barWidth,barGap:this.options.barGap,barRadius:this.options.barRadius}),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReadyEvent();const i=this.options.url||(null===(e=this.options.media)||void 0===e?void 0:e.src);i&&this.load(i,this.options.peaks,this.options.duration)}initPlayerEvents(){this.subscriptions.push(this.player.on("timeupdate",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),this.emit("timeupdate",{currentTime:t})})),this.player.on("play",(()=>{this.emit("play"),this.timer.start()})),this.player.on("pause",(()=>{this.emit("pause"),this.timer.stop()})),this.player.on("canplay",(()=>{this.emit("canplay",{duration:this.getDuration()})})),this.player.on("seeking",(()=>{this.emit("seeking",{currentTime:this.getCurrentTime()})})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",(({relativeX:t})=>{if(this.options.interactive){const e=this.getDuration()*t;this.seekTo(e),this.emit("seekClick",{currentTime:this.getCurrentTime()})}})))}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),!0),this.emit("timeupdate",{currentTime:t})})))}initReadyEvent(){let t=!1,e=!1;const i=()=>{t&&e&&this.emit("ready",{duration:this.getDuration()})};this.subscriptions.push(this.on("decode",(()=>{t=!0,i()})),this.on("canplay",(()=>{e=!0,i()})),this.player.on("waiting",(()=>{e=!1,t=!1})))}load(t,e,i){return s=this,n=void 0,r=function*(){if(this.player.loadUrl(t),null==e){const e=yield this.fetcher.load(t),i=yield this.decoder.decode(e);this.decodedData=i}else i||(i=(yield new Promise((t=>{this.player.on("canplay",(()=>t(this.getDuration())),{once:!0})})))||0),this.decodedData={duration:i,numberOfChannels:e.length,sampleRate:e[0].length/i,getChannelData:t=>e[t]};this.renderer.render(this.decodedData),this.emit("decode",{duration:this.decodedData.duration})},new((o=void 0)||(o=Promise))((function(t,e){function i(t){try{h(r.next(t))}catch(t){e(t)}}function a(t){try{h(r.throw(t))}catch(t){e(t)}}function h(e){var s;e.done?t(e.value):(s=e.value,s instanceof o?s:new o((function(t){t(s)}))).then(i,a)}h((r=r.apply(s,n||[])).next())}));var s,n,o,r}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(this.decodedData,t)}play(){this.player.play()}pause(){this.player.pause()}seekTo(t){this.player.seekTo(t)}isPlaying(){return this.player.isPlaying()}getDuration(){var t;return this.player.getDuration()||(null===(t=this.decodedData)||void 0===t?void 0:t.duration)||0}getCurrentTime(){return this.player.getCurrentTime()}getVolume(){return this.player.getVolume()}setVolume(t){this.player.setVolume(t)}getMuted(){return this.player.getMuted()}setMuted(t){this.player.setMuted(t)}getPlaybackRate(){return this.player.getPlaybackRate()}setPlaybackRate(t,e){this.player.setPlaybackRate(t,e)}registerPlugin(t,e){const i=new t({wavesurfer:this,container:this.renderer.getContainer()},e);return this.plugins.push(i),i}getDecodedData(){return this.decodedData}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.plugins.forEach((t=>t.destroy())),this.timer.destroy(),this.player.destroy(),this.decoder.destroy(),this.renderer.destroy()}}const a=r;return e.default})()));
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.WaveSurfer=e():t.WaveSurfer=e()}(this,(()=>(()=>{"use strict";var t={d:(e,i)=>{for(var s in i)t.o(i,s)&&!t.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:i[s]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e)},e={};t.d(e,{default:()=>a});const i=class{constructor(){this.eventTarget=new EventTarget}emit(t,e){const i=new CustomEvent(String(t),{detail:e});this.eventTarget.dispatchEvent(i)}on(t,e,i){const s=t=>{t instanceof CustomEvent&&e(t.detail)},n=String(t);return this.eventTarget.addEventListener(n,s,{once:i}),()=>this.eventTarget.removeEventListener(n,s)}once(t,e){return this.on(t,e,!0)}};const s=class extends i{constructor(t){super(),this.timeout=null,this.options=t;let e=null;if("string"==typeof this.options.container?e=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(e=this.options.container),!e)throw new Error("Container not found");const i=document.createElement("div"),s=i.attachShadow({mode:"open"});s.innerHTML=`\n <style>\n :host {\n user-select: none;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .wrapper {\n position: relative;\n width: fit-content;\n min-width: 100%;\n z-index: 2;\n }\n :host canvas {\n display: block;\n height: ${this.options.height}px;\n min-width: 100%;\n image-rendering: pixelated;\n }\n :host .progress {\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 100%;\n pointer-events: none;\n clip-path: inset(0px 100% 0px 0px);\n }\n :host .cursor {\n position: absolute;\n z-index: 3;\n top: 0;\n left: 0;\n height: 100%;\n width: ${this.options.cursorWidth}px;\n background: ${this.options.cursorColor||this.options.progressColor};\n }\n </style>\n\n <div class="scroll">\n <div class="wrapper">\n <canvas></canvas>\n <canvas class="progress"></canvas>\n <div class="cursor"></div>\n </div>\n </div>\n `,this.container=i,this.scrollContainer=s.querySelector(".scroll"),this.wrapper=s.querySelector(".wrapper"),this.mainCanvas=s.querySelector("canvas"),this.ctx=this.mainCanvas.getContext("2d",{desynchronized:!0}),this.progressCanvas=s.querySelector(".progress"),this.cursor=s.querySelector(".cursor"),e.appendChild(i),this.mainCanvas.addEventListener("click",(t=>{const e=this.mainCanvas.getBoundingClientRect(),i=(t.clientX-e.left)/e.width;this.emit("click",{relativeX:i})}))}getContainer(){return this.container.shadowRoot}getWrapper(){return this.wrapper}destroy(){this.container.remove()}delay(t,e=100){return this.timeout&&clearTimeout(this.timeout),new Promise((i=>{this.timeout=setTimeout((()=>{i(t())}),e)}))}renderPeaks(t,e,i){var s,n,r,o,a;return n=this,r=void 0,a=function*(){const{devicePixelRatio:n}=window,{ctx:r}=this,o=null!=this.options.barWidth?this.options.barWidth*n:1,a=null!=this.options.barGap?this.options.barGap*n:this.options.barWidth?o/2:0,h=null!==(s=this.options.barRadius)&&void 0!==s?s:0,d=t[0],c=d.length,l=Math.floor(e/(o+a))/c,u=i/2,p=1===t.length,m=p?d:t[1],y=p&&m.some((t=>t<0)),v=(t,e)=>{let i=0,s=0,n=0;r.beginPath(),r.fillStyle=this.options.waveColor,r.roundRect||(r.roundRect=r.fillRect);for(let c=t;c<e;c++){const t=Math.round(c*l);if(t>i){const e=Math.round(s*u),d=Math.round(n*u);r.roundRect(i*(o+a),u-e,o,e+d||1,h),i=t,s=0,n=0}const e=y?d[c]:Math.abs(d[c]),p=y?m[c]:Math.abs(m[c]);e>s&&(s=e),(y?p<-n:p>n)&&(n=p<0?-p:p)}r.fill(),r.closePath()};r.clearRect(0,0,e,i);const{scrollLeft:g,scrollWidth:f,clientWidth:C}=this.scrollContainer,w=c/f,x=Math.floor(g*w),b=Math.ceil((g+C)*w);v(x,b),this.createProgressMask(),x>0&&(yield this.delay((()=>{v(0,x)}))),b<c&&(yield this.delay((()=>{v(b,c)}))),this.delay((()=>{this.createProgressMask()}))},new((o=void 0)||(o=Promise))((function(t,e){function i(t){try{h(a.next(t))}catch(t){e(t)}}function s(t){try{h(a.throw(t))}catch(t){e(t)}}function h(e){var n;e.done?t(e.value):(n=e.value,n instanceof o?n:new o((function(t){t(n)}))).then(i,s)}h((a=a.apply(n,r||[])).next())}))}createProgressMask(){const t=this.progressCanvas.getContext("2d",{desynchronized:!0});this.progressCanvas.width=this.mainCanvas.width,this.progressCanvas.height=this.mainCanvas.height,t.drawImage(this.mainCanvas,0,0),t.globalCompositeOperation="source-in",t.fillStyle=this.options.progressColor,t.fillRect(0,0,this.progressCanvas.width,this.progressCanvas.height)}render(t){const{devicePixelRatio:e}=window,i=this.options.fillParent?this.scrollContainer.clientWidth*e:0,s=t.duration*this.options.minPxPerSec,n=s>i,r=Math.max(1,n?s:i),{height:o}=this.options;this.mainCanvas.width=r,this.mainCanvas.height=o,this.mainCanvas.style.width=Math.floor(s/e)+"px";const a=[t.getChannelData(0)];t.numberOfChannels>1&&a.push(t.getChannelData(1)),this.renderPeaks(a,r,o)}zoom(t,e){const i=this.cursor.getBoundingClientRect().left;this.options.minPxPerSec=e,this.render(t);const s=this.cursor.getBoundingClientRect().left;this.scrollContainer.scrollLeft+=s-i}renderProgress(t,e=!1){if(this.progressCanvas.style.clipPath=`inset(0 ${100-100*t}% 0 0)`,this.cursor.style.left=100*t+"%",this.cursor.style.marginLeft=t>0?100===Math.round(100*t)?`-${this.cursor.offsetWidth}px`:`-${this.cursor.offsetWidth/2}px`:"",this.cursor.scrollIntoView(),e){const e=this.container.clientWidth/2,i=this.mainCanvas.clientWidth;i*t>=e&&(this.scrollContainer.scrollLeft=i*t-e)}}},n=class extends i{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",(()=>{requestAnimationFrame((()=>{this.emit("tick")}))})),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}};const r={height:128,waveColor:"#999",progressColor:"#555",cursorWidth:1,minPxPerSec:0,fillParent:!0,interactive:!0};class o extends i{static create(t){return new o(t)}constructor(t){var e;super(),this.plugins=[],this.subscriptions=[],this.decodedData=null,this.options=Object.assign({},r,t),this.fetcher=new class{load(t){return e=this,i=void 0,n=function*(){return fetch(t).then((t=>t.arrayBuffer()))},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}},this.decoder=new class{initAudioContext(t){this.audioCtx=new AudioContext({latencyHint:"playback",sampleRate:t})}constructor(){this.audioCtx=null;try{this.initAudioContext(3e3)}catch(t){this.initAudioContext(8e3)}}decode(t){return e=this,i=void 0,n=function*(){if(!this.audioCtx)throw new Error("AudioContext is not initialized");return this.audioCtx.decodeAudioData(t)},new((s=void 0)||(s=Promise))((function(t,r){function o(t){try{h(n.next(t))}catch(t){r(t)}}function a(t){try{h(n.throw(t))}catch(t){r(t)}}function h(e){var i;e.done?t(e.value):(i=e.value,i instanceof s?i:new s((function(t){t(i)}))).then(o,a)}h((n=n.apply(e,i||[])).next())}));var e,i,s,n}destroy(){var t;null===(t=this.audioCtx)||void 0===t||t.close(),this.audioCtx=null}},this.timer=new n,this.player=new class{constructor({media:t,autoplay:e}){this.isExternalMedia=!1,this.hasPlayedOnce=!1,t?(this.media=t,this.isExternalMedia=!0):this.media=document.createElement("audio");const i=this.on("play",(()=>{this.hasPlayedOnce=!0,i()}));e&&(this.media.autoplay=!0)}on(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e)}destroy(){this.media.pause(),this.isExternalMedia||this.media.remove()}loadUrl(t){this.media.src=t}getCurrentTime(){return this.media.currentTime}play(){this.media.play()}pause(){this.media.pause()}isPlaying(){return this.media.currentTime>0&&!this.media.paused&&!this.media.ended}seekTo(t){if(this.media){const{hasPlayedOnce:e}=this;e||this.media.play(),this.media.currentTime=t,e||this.media.pause()}}getDuration(){return this.media.duration}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}getMediaElement(){return this.media}}({media:this.options.media,autoplay:this.options.autoplay}),this.renderer=new s({container:this.options.container,height:this.options.height,waveColor:this.options.waveColor,progressColor:this.options.progressColor,cursorColor:this.options.cursorColor,cursorWidth:this.options.cursorWidth,minPxPerSec:this.options.minPxPerSec,fillParent:this.options.fillParent,barWidth:this.options.barWidth,barGap:this.options.barGap,barRadius:this.options.barRadius}),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReadyEvent();const i=this.options.url||(null===(e=this.options.media)||void 0===e?void 0:e.src);i&&this.load(i,this.options.peaks,this.options.duration)}initPlayerEvents(){this.subscriptions.push(this.player.on("timeupdate",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),this.emit("timeupdate",{currentTime:t})})),this.player.on("play",(()=>{this.emit("play"),this.timer.start()})),this.player.on("pause",(()=>{this.emit("pause"),this.timer.stop()})),this.player.on("canplay",(()=>{this.emit("canplay",{duration:this.getDuration()})})),this.player.on("seeking",(()=>{this.emit("seeking",{currentTime:this.getCurrentTime()})})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",(({relativeX:t})=>{if(this.options.interactive){const e=this.getDuration()*t;this.seekTo(e),this.emit("seekClick",{currentTime:this.getCurrentTime()})}})))}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{const t=this.getCurrentTime();this.renderer.renderProgress(t/this.getDuration(),!0),this.emit("timeupdate",{currentTime:t})})))}initReadyEvent(){let t=!1,e=!1;const i=()=>{t&&e&&this.emit("ready",{duration:this.getDuration()})};this.subscriptions.push(this.on("decode",(()=>{t=!0,i()})),this.on("canplay",(()=>{e=!0,i()})),this.player.on("waiting",(()=>{e=!1,t=!1})))}load(t,e,i){return s=this,n=void 0,o=function*(){if(this.player.loadUrl(t),null==e){const e=yield this.fetcher.load(t),i=yield this.decoder.decode(e);this.decodedData=i}else i||(i=(yield new Promise((t=>{this.player.on("canplay",(()=>t(this.getDuration())),{once:!0})})))||0),this.decodedData={duration:i,numberOfChannels:e.length,sampleRate:e[0].length/i,getChannelData:t=>e[t]};this.renderer.render(this.decodedData),this.emit("decode",{duration:this.decodedData.duration})},new((r=void 0)||(r=Promise))((function(t,e){function i(t){try{h(o.next(t))}catch(t){e(t)}}function a(t){try{h(o.throw(t))}catch(t){e(t)}}function h(e){var s;e.done?t(e.value):(s=e.value,s instanceof r?s:new r((function(t){t(s)}))).then(i,a)}h((o=o.apply(s,n||[])).next())}));var s,n,r,o}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(this.decodedData,t)}play(){this.player.play()}pause(){this.player.pause()}seekTo(t){this.player.seekTo(t)}isPlaying(){return this.player.isPlaying()}getDuration(){var t;return this.player.getDuration()||(null===(t=this.decodedData)||void 0===t?void 0:t.duration)||0}getCurrentTime(){return this.player.getCurrentTime()}getVolume(){return this.player.getVolume()}setVolume(t){this.player.setVolume(t)}getMuted(){return this.player.getMuted()}setMuted(t){this.player.setMuted(t)}getPlaybackRate(){return this.player.getPlaybackRate()}setPlaybackRate(t,e){this.player.setPlaybackRate(t,e)}registerPlugin(t,e){const i=new t({wavesurfer:this,container:this.renderer.getContainer(),wrapper:this.renderer.getWrapper()},e);return this.plugins.push(i),i}getDecodedData(){return this.decodedData}getMediaElement(){return this.player.getMediaElement()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.plugins.forEach((t=>t.destroy())),this.timer.destroy(),this.player.destroy(),this.decoder.destroy(),this.renderer.destroy()}}const a=o;return e.default})()));
|
package/package.json
CHANGED