wavesurfer.js 7.0.0-alpha.9 → 7.0.0-beta.2
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/README.md +90 -18
- package/dist/base-plugin.d.ts +6 -4
- package/dist/base-plugin.js +9 -3
- package/dist/decoder.d.ts +8 -7
- package/dist/decoder.js +43 -38
- package/dist/event-emitter.d.ts +16 -10
- package/dist/event-emitter.js +38 -16
- package/dist/fetcher.d.ts +4 -3
- package/dist/fetcher.js +5 -15
- package/dist/player.d.ts +31 -11
- package/dist/player.js +58 -31
- package/dist/plugins/envelope.d.ts +71 -0
- package/dist/plugins/envelope.js +347 -0
- package/dist/plugins/envelope.min.cjs +1 -0
- package/dist/plugins/envelope.min.js +1 -0
- package/dist/plugins/minimap.d.ts +39 -0
- package/dist/plugins/minimap.js +114 -0
- package/dist/plugins/minimap.min.cjs +1 -0
- package/dist/plugins/minimap.min.js +1 -0
- package/dist/plugins/multitrack.d.ts +85 -12
- package/dist/plugins/multitrack.js +331 -84
- package/dist/plugins/multitrack.min.cjs +1 -0
- package/dist/plugins/multitrack.min.js +1 -0
- package/dist/plugins/record.d.ts +26 -0
- package/dist/plugins/record.js +126 -0
- package/dist/plugins/record.min.cjs +1 -0
- package/dist/plugins/record.min.js +1 -0
- package/dist/plugins/regions.d.ts +83 -43
- package/dist/plugins/regions.js +362 -192
- package/dist/plugins/regions.min.cjs +1 -0
- package/dist/plugins/regions.min.js +1 -0
- package/dist/plugins/spectrogram.d.ts +69 -0
- package/dist/plugins/spectrogram.js +340 -0
- package/dist/plugins/spectrogram.min.cjs +1 -0
- package/dist/plugins/spectrogram.min.js +1 -0
- package/dist/plugins/timeline.d.ts +25 -9
- package/dist/plugins/timeline.js +65 -24
- package/dist/plugins/timeline.min.cjs +1 -0
- package/dist/plugins/timeline.min.js +1 -0
- package/dist/renderer.d.ts +29 -13
- package/dist/renderer.js +280 -161
- package/dist/timer.d.ts +1 -1
- package/dist/wavesurfer.d.ts +151 -0
- package/dist/wavesurfer.js +241 -0
- package/dist/wavesurfer.min.cjs +1 -0
- package/dist/wavesurfer.min.js +1 -1
- package/package.json +55 -28
- package/dist/index.d.ts +0 -117
- package/dist/index.js +0 -227
- package/dist/player-webaudio.d.ts +0 -8
- package/dist/player-webaudio.js +0 -32
- package/dist/plugins/xmultitrack.d.ts +0 -44
- package/dist/plugins/xmultitrack.js +0 -260
- package/dist/react/useWavesurfer.d.ts +0 -5
- package/dist/react/useWavesurfer.js +0 -20
- package/dist/wavesurfer.Multitrack.min.js +0 -1
- package/dist/wavesurfer.Regions.min.js +0 -1
- package/dist/wavesurfer.Timeline.min.js +0 -1
package/dist/index.d.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import EventEmitter, { type GeneralEventTypes } from './event-emitter.js';
|
|
2
|
-
import BasePlugin from './base-plugin.js';
|
|
3
|
-
export type WaveSurferOptions = {
|
|
4
|
-
/** HTML element or CSS selector */
|
|
5
|
-
container: HTMLElement | string | null;
|
|
6
|
-
/** Height of the waveform in pixels */
|
|
7
|
-
height?: number;
|
|
8
|
-
/** The color of the waveform */
|
|
9
|
-
waveColor?: string;
|
|
10
|
-
/** The color of the progress mask */
|
|
11
|
-
progressColor?: string;
|
|
12
|
-
/** The color of the playpack cursort */
|
|
13
|
-
cursorColor?: string;
|
|
14
|
-
/** If set, the waveform will be rendered in bars like so: ▁ ▂ ▇ ▃ ▅ ▂ */
|
|
15
|
-
barWidth?: number;
|
|
16
|
-
/** Spacing between bars in pixels */
|
|
17
|
-
barGap?: number;
|
|
18
|
-
/** Rounded borders for bars */
|
|
19
|
-
barRadius?: number;
|
|
20
|
-
/** Minimum pixels per second of audio (zoom) */
|
|
21
|
-
minPxPerSec?: number;
|
|
22
|
-
/** Stretch the waveform to fill the container, true by default */
|
|
23
|
-
fillParent?: boolean;
|
|
24
|
-
/** Audio URL */
|
|
25
|
-
url?: string;
|
|
26
|
-
/** Pre-computed audio data */
|
|
27
|
-
peaks?: Float32Array[];
|
|
28
|
-
/** Pre-computed duration */
|
|
29
|
-
duration?: number;
|
|
30
|
-
/** Use an existing media element instead of creating one */
|
|
31
|
-
media?: HTMLMediaElement;
|
|
32
|
-
/** Play the audio on load */
|
|
33
|
-
autoplay?: boolean;
|
|
34
|
-
/** Is the waveform clickable? */
|
|
35
|
-
interactive?: boolean;
|
|
36
|
-
};
|
|
37
|
-
export type WaveSurferEvents = {
|
|
38
|
-
decode: {
|
|
39
|
-
duration: number;
|
|
40
|
-
};
|
|
41
|
-
canplay: {
|
|
42
|
-
duration: number;
|
|
43
|
-
};
|
|
44
|
-
ready: {
|
|
45
|
-
duration: number;
|
|
46
|
-
};
|
|
47
|
-
play: void;
|
|
48
|
-
pause: void;
|
|
49
|
-
timeupdate: {
|
|
50
|
-
currentTime: number;
|
|
51
|
-
};
|
|
52
|
-
seeking: {
|
|
53
|
-
currentTime: number;
|
|
54
|
-
};
|
|
55
|
-
seekClick: {
|
|
56
|
-
currentTime: number;
|
|
57
|
-
};
|
|
58
|
-
destroy: void;
|
|
59
|
-
};
|
|
60
|
-
export type WaveSurferPluginParams = {
|
|
61
|
-
wavesurfer: WaveSurfer;
|
|
62
|
-
container: HTMLElement;
|
|
63
|
-
};
|
|
64
|
-
export declare class WaveSurfer extends EventEmitter<WaveSurferEvents> {
|
|
65
|
-
private options;
|
|
66
|
-
private fetcher;
|
|
67
|
-
private decoder;
|
|
68
|
-
private renderer;
|
|
69
|
-
private player;
|
|
70
|
-
private timer;
|
|
71
|
-
private plugins;
|
|
72
|
-
private subscriptions;
|
|
73
|
-
private decodedData;
|
|
74
|
-
/** Create a new WaveSurfer instance */
|
|
75
|
-
static create(options: WaveSurferOptions): WaveSurfer;
|
|
76
|
-
/** Create a new WaveSurfer instance */
|
|
77
|
-
constructor(options: WaveSurferOptions);
|
|
78
|
-
private initPlayerEvents;
|
|
79
|
-
private initRendererEvents;
|
|
80
|
-
private initTimerEvents;
|
|
81
|
-
private initReadyEvent;
|
|
82
|
-
/** Load an audio file by URL, with optional pre-decoded audio data */
|
|
83
|
-
load(url: string, channelData?: Float32Array[], duration?: number): Promise<void>;
|
|
84
|
-
/** Zoom in or out */
|
|
85
|
-
zoom(minPxPerSec: number): void;
|
|
86
|
-
/** Start playing the audio */
|
|
87
|
-
play(): void;
|
|
88
|
-
/** Pause the audio */
|
|
89
|
-
pause(): void;
|
|
90
|
-
/** Skip to a time position in seconds */
|
|
91
|
-
seekTo(time: number): void;
|
|
92
|
-
/** Check if the audio is playing */
|
|
93
|
-
isPlaying(): boolean;
|
|
94
|
-
/** Get the duration of the audio in seconds */
|
|
95
|
-
getDuration(): number;
|
|
96
|
-
/** Get the current audio position in seconds */
|
|
97
|
-
getCurrentTime(): number;
|
|
98
|
-
/** Get the audio volume */
|
|
99
|
-
getVolume(): number;
|
|
100
|
-
/** Set the audio volume */
|
|
101
|
-
setVolume(volume: number): void;
|
|
102
|
-
/** Get the audio muted state */
|
|
103
|
-
getMuted(): boolean;
|
|
104
|
-
/** Mute or unmute the audio */
|
|
105
|
-
setMuted(muted: boolean): void;
|
|
106
|
-
/** Get playback rate */
|
|
107
|
-
getPlaybackRate(): number;
|
|
108
|
-
/** Set playback rate, with an optional parameter to NOT preserve the pitch if false */
|
|
109
|
-
setPlaybackRate(rate: number, preservePitch?: boolean): void;
|
|
110
|
-
/** Register and initialize a plugin */
|
|
111
|
-
registerPlugin<T extends BasePlugin<GeneralEventTypes, any>>(CustomPlugin: new (params: WaveSurferPluginParams, options: any) => T, options?: any): T;
|
|
112
|
-
/** Get the decoded audio data */
|
|
113
|
-
getDecodedData(): AudioBuffer | null;
|
|
114
|
-
/** Unmount wavesurfer */
|
|
115
|
-
destroy(): void;
|
|
116
|
-
}
|
|
117
|
-
export default WaveSurfer;
|
package/dist/index.js
DELETED
|
@@ -1,227 +0,0 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import Fetcher from './fetcher.js';
|
|
11
|
-
import Decoder from './decoder.js';
|
|
12
|
-
import Renderer from './renderer.js';
|
|
13
|
-
import Player from './player.js';
|
|
14
|
-
import EventEmitter from './event-emitter.js';
|
|
15
|
-
import Timer from './timer.js';
|
|
16
|
-
const defaultOptions = {
|
|
17
|
-
height: 128,
|
|
18
|
-
waveColor: '#999',
|
|
19
|
-
progressColor: '#555',
|
|
20
|
-
minPxPerSec: 0,
|
|
21
|
-
fillParent: true,
|
|
22
|
-
interactive: true,
|
|
23
|
-
};
|
|
24
|
-
export class WaveSurfer extends EventEmitter {
|
|
25
|
-
/** Create a new WaveSurfer instance */
|
|
26
|
-
static create(options) {
|
|
27
|
-
return new WaveSurfer(options);
|
|
28
|
-
}
|
|
29
|
-
/** Create a new WaveSurfer instance */
|
|
30
|
-
constructor(options) {
|
|
31
|
-
var _a;
|
|
32
|
-
super();
|
|
33
|
-
this.plugins = [];
|
|
34
|
-
this.subscriptions = [];
|
|
35
|
-
this.decodedData = null;
|
|
36
|
-
this.options = Object.assign({}, defaultOptions, options);
|
|
37
|
-
this.fetcher = new Fetcher();
|
|
38
|
-
this.decoder = new Decoder();
|
|
39
|
-
this.timer = new Timer();
|
|
40
|
-
this.player = new Player({
|
|
41
|
-
media: this.options.media,
|
|
42
|
-
autoplay: this.options.autoplay,
|
|
43
|
-
});
|
|
44
|
-
this.renderer = new Renderer({
|
|
45
|
-
container: this.options.container,
|
|
46
|
-
height: this.options.height,
|
|
47
|
-
waveColor: this.options.waveColor,
|
|
48
|
-
progressColor: this.options.progressColor,
|
|
49
|
-
cursorColor: this.options.cursorColor,
|
|
50
|
-
minPxPerSec: this.options.minPxPerSec,
|
|
51
|
-
fillParent: this.options.fillParent,
|
|
52
|
-
barWidth: this.options.barWidth,
|
|
53
|
-
barGap: this.options.barGap,
|
|
54
|
-
barRadius: this.options.barRadius,
|
|
55
|
-
});
|
|
56
|
-
this.initPlayerEvents();
|
|
57
|
-
this.initRendererEvents();
|
|
58
|
-
this.initTimerEvents();
|
|
59
|
-
this.initReadyEvent();
|
|
60
|
-
const url = this.options.url || ((_a = this.options.media) === null || _a === void 0 ? void 0 : _a.src);
|
|
61
|
-
if (url) {
|
|
62
|
-
this.load(url, this.options.peaks, this.options.duration);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
initPlayerEvents() {
|
|
66
|
-
this.subscriptions.push(this.player.on('timeupdate', () => {
|
|
67
|
-
const currentTime = this.getCurrentTime();
|
|
68
|
-
this.renderer.renderProgress(currentTime / this.getDuration(), this.isPlaying());
|
|
69
|
-
this.emit('timeupdate', { currentTime });
|
|
70
|
-
}), this.player.on('play', () => {
|
|
71
|
-
this.emit('play');
|
|
72
|
-
this.timer.start();
|
|
73
|
-
}), this.player.on('pause', () => {
|
|
74
|
-
this.emit('pause');
|
|
75
|
-
this.timer.stop();
|
|
76
|
-
}), this.player.on('canplay', () => {
|
|
77
|
-
this.emit('canplay', { duration: this.getDuration() });
|
|
78
|
-
}), this.player.on('seeking', () => {
|
|
79
|
-
this.emit('seeking', { currentTime: this.getCurrentTime() });
|
|
80
|
-
}));
|
|
81
|
-
}
|
|
82
|
-
initRendererEvents() {
|
|
83
|
-
// Seek on click
|
|
84
|
-
this.subscriptions.push(this.renderer.on('click', ({ relativeX }) => {
|
|
85
|
-
if (this.options.interactive) {
|
|
86
|
-
const time = this.getDuration() * relativeX;
|
|
87
|
-
this.seekTo(time);
|
|
88
|
-
this.emit('seekClick', { currentTime: this.getCurrentTime() });
|
|
89
|
-
}
|
|
90
|
-
}));
|
|
91
|
-
}
|
|
92
|
-
initTimerEvents() {
|
|
93
|
-
// The timer fires every 16ms for a smooth progress animation
|
|
94
|
-
this.subscriptions.push(this.timer.on('tick', () => {
|
|
95
|
-
const currentTime = this.getCurrentTime();
|
|
96
|
-
this.renderer.renderProgress(currentTime / this.getDuration(), true);
|
|
97
|
-
this.emit('timeupdate', { currentTime });
|
|
98
|
-
}));
|
|
99
|
-
}
|
|
100
|
-
initReadyEvent() {
|
|
101
|
-
let isDecoded = false;
|
|
102
|
-
let isPlayable = false;
|
|
103
|
-
const emitReady = () => {
|
|
104
|
-
if (isDecoded && isPlayable) {
|
|
105
|
-
this.emit('ready', { duration: this.getDuration() });
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
this.subscriptions.push(this.on('decode', () => {
|
|
109
|
-
isDecoded = true;
|
|
110
|
-
emitReady();
|
|
111
|
-
}), this.on('canplay', () => {
|
|
112
|
-
isPlayable = true;
|
|
113
|
-
emitReady();
|
|
114
|
-
}), this.player.on('waiting', () => {
|
|
115
|
-
isPlayable = false;
|
|
116
|
-
isDecoded = false;
|
|
117
|
-
}));
|
|
118
|
-
}
|
|
119
|
-
/** Load an audio file by URL, with optional pre-decoded audio data */
|
|
120
|
-
load(url, channelData, duration) {
|
|
121
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
-
this.player.loadUrl(url);
|
|
123
|
-
// Fetch and decode the audio of no pre-computed audio data is provided
|
|
124
|
-
if (channelData == null) {
|
|
125
|
-
const audio = yield this.fetcher.load(url);
|
|
126
|
-
const data = yield this.decoder.decode(audio);
|
|
127
|
-
this.decodedData = data;
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
if (!duration) {
|
|
131
|
-
duration =
|
|
132
|
-
(yield new Promise((resolve) => {
|
|
133
|
-
this.player.on('canplay', () => resolve(this.getDuration()), { once: true });
|
|
134
|
-
})) || 0;
|
|
135
|
-
}
|
|
136
|
-
this.decodedData = {
|
|
137
|
-
duration,
|
|
138
|
-
numberOfChannels: channelData.length,
|
|
139
|
-
sampleRate: channelData[0].length / duration,
|
|
140
|
-
getChannelData: (i) => channelData[i],
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
this.renderer.render(this.decodedData);
|
|
144
|
-
this.emit('decode', { duration: this.decodedData.duration });
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/** Zoom in or out */
|
|
148
|
-
zoom(minPxPerSec) {
|
|
149
|
-
if (!this.decodedData) {
|
|
150
|
-
throw new Error('No audio loaded');
|
|
151
|
-
}
|
|
152
|
-
this.renderer.zoom(this.decodedData, minPxPerSec);
|
|
153
|
-
}
|
|
154
|
-
/** Start playing the audio */
|
|
155
|
-
play() {
|
|
156
|
-
this.player.play();
|
|
157
|
-
}
|
|
158
|
-
/** Pause the audio */
|
|
159
|
-
pause() {
|
|
160
|
-
this.player.pause();
|
|
161
|
-
}
|
|
162
|
-
/** Skip to a time position in seconds */
|
|
163
|
-
seekTo(time) {
|
|
164
|
-
this.player.seekTo(time);
|
|
165
|
-
}
|
|
166
|
-
/** Check if the audio is playing */
|
|
167
|
-
isPlaying() {
|
|
168
|
-
return this.player.isPlaying();
|
|
169
|
-
}
|
|
170
|
-
/** Get the duration of the audio in seconds */
|
|
171
|
-
getDuration() {
|
|
172
|
-
var _a;
|
|
173
|
-
return this.player.getDuration() || ((_a = this.decodedData) === null || _a === void 0 ? void 0 : _a.duration) || 0;
|
|
174
|
-
}
|
|
175
|
-
/** Get the current audio position in seconds */
|
|
176
|
-
getCurrentTime() {
|
|
177
|
-
return this.player.getCurrentTime();
|
|
178
|
-
}
|
|
179
|
-
/** Get the audio volume */
|
|
180
|
-
getVolume() {
|
|
181
|
-
return this.player.getVolume();
|
|
182
|
-
}
|
|
183
|
-
/** Set the audio volume */
|
|
184
|
-
setVolume(volume) {
|
|
185
|
-
this.player.setVolume(volume);
|
|
186
|
-
}
|
|
187
|
-
/** Get the audio muted state */
|
|
188
|
-
getMuted() {
|
|
189
|
-
return this.player.getMuted();
|
|
190
|
-
}
|
|
191
|
-
/** Mute or unmute the audio */
|
|
192
|
-
setMuted(muted) {
|
|
193
|
-
this.player.setMuted(muted);
|
|
194
|
-
}
|
|
195
|
-
/** Get playback rate */
|
|
196
|
-
getPlaybackRate() {
|
|
197
|
-
return this.player.getPlaybackRate();
|
|
198
|
-
}
|
|
199
|
-
/** Set playback rate, with an optional parameter to NOT preserve the pitch if false */
|
|
200
|
-
setPlaybackRate(rate, preservePitch) {
|
|
201
|
-
this.player.setPlaybackRate(rate, preservePitch);
|
|
202
|
-
}
|
|
203
|
-
/** Register and initialize a plugin */
|
|
204
|
-
registerPlugin(CustomPlugin, options) {
|
|
205
|
-
const plugin = new CustomPlugin({
|
|
206
|
-
wavesurfer: this,
|
|
207
|
-
container: this.renderer.getContainer(),
|
|
208
|
-
}, options);
|
|
209
|
-
this.plugins.push(plugin);
|
|
210
|
-
return plugin;
|
|
211
|
-
}
|
|
212
|
-
/** Get the decoded audio data */
|
|
213
|
-
getDecodedData() {
|
|
214
|
-
return this.decodedData;
|
|
215
|
-
}
|
|
216
|
-
/** Unmount wavesurfer */
|
|
217
|
-
destroy() {
|
|
218
|
-
this.emit('destroy');
|
|
219
|
-
this.subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
220
|
-
this.plugins.forEach((plugin) => plugin.destroy());
|
|
221
|
-
this.timer.destroy();
|
|
222
|
-
this.player.destroy();
|
|
223
|
-
this.decoder.destroy();
|
|
224
|
-
this.renderer.destroy();
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
export default WaveSurfer;
|
package/dist/player-webaudio.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import Player from './player.js';
|
|
2
|
-
class WebAudioPlayer extends Player {
|
|
3
|
-
constructor() {
|
|
4
|
-
super(...arguments);
|
|
5
|
-
this.audioCtx = null;
|
|
6
|
-
this.sourceNode = null;
|
|
7
|
-
}
|
|
8
|
-
destroy() {
|
|
9
|
-
var _a, _b;
|
|
10
|
-
(_a = this.sourceNode) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
11
|
-
this.sourceNode = null;
|
|
12
|
-
(_b = this.audioCtx) === null || _b === void 0 ? void 0 : _b.close();
|
|
13
|
-
this.audioCtx = null;
|
|
14
|
-
super.destroy();
|
|
15
|
-
}
|
|
16
|
-
loadUrl(url) {
|
|
17
|
-
super.loadUrl(url);
|
|
18
|
-
if (!this.audioCtx) {
|
|
19
|
-
this.audioCtx = new AudioContext({
|
|
20
|
-
latencyHint: 'playback',
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
if (this.sourceNode) {
|
|
24
|
-
this.sourceNode.disconnect();
|
|
25
|
-
}
|
|
26
|
-
if (this.media) {
|
|
27
|
-
this.sourceNode = this.audioCtx.createMediaElementSource(this.media);
|
|
28
|
-
this.sourceNode.connect(this.audioCtx.destination);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
export default WebAudioPlayer;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { type WaveSurferOptions } from '../index.js';
|
|
2
|
-
type MultitrackTracks = Array<{
|
|
3
|
-
id: string | number;
|
|
4
|
-
url?: string;
|
|
5
|
-
peaks?: WaveSurferOptions['peaks'];
|
|
6
|
-
draggable?: boolean;
|
|
7
|
-
startPosition: number;
|
|
8
|
-
startCue?: number;
|
|
9
|
-
endCue?: number;
|
|
10
|
-
markers?: Array<{
|
|
11
|
-
id: string | number;
|
|
12
|
-
time: number;
|
|
13
|
-
label?: string;
|
|
14
|
-
color?: string;
|
|
15
|
-
}>;
|
|
16
|
-
}>;
|
|
17
|
-
type MultitrackOptions = {
|
|
18
|
-
container: HTMLElement;
|
|
19
|
-
minPxPerSec: number;
|
|
20
|
-
} & WaveSurferOptions;
|
|
21
|
-
declare class MultiTrack {
|
|
22
|
-
private tracks;
|
|
23
|
-
private options;
|
|
24
|
-
private audios;
|
|
25
|
-
private wavesurfers;
|
|
26
|
-
private durations;
|
|
27
|
-
private currentTime;
|
|
28
|
-
private maxDuration;
|
|
29
|
-
private rendering;
|
|
30
|
-
private isDragging;
|
|
31
|
-
static create(tracks: MultitrackTracks, options: MultitrackOptions): MultiTrack;
|
|
32
|
-
constructor(tracks: MultitrackTracks, options: MultitrackOptions);
|
|
33
|
-
private initAudios;
|
|
34
|
-
private initWavesurfers;
|
|
35
|
-
private updatePosition;
|
|
36
|
-
private onSeek;
|
|
37
|
-
private onDrag;
|
|
38
|
-
private findCurrentTracks;
|
|
39
|
-
play(): void;
|
|
40
|
-
pause(): void;
|
|
41
|
-
isPlaying(): boolean;
|
|
42
|
-
destroy(): void;
|
|
43
|
-
}
|
|
44
|
-
export default MultiTrack;
|
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
import WaveSurfer from '../index.js';
|
|
2
|
-
import RegionsPlugin from './regions.js';
|
|
3
|
-
class MultiTrack {
|
|
4
|
-
static create(tracks, options) {
|
|
5
|
-
return new MultiTrack(tracks, options);
|
|
6
|
-
}
|
|
7
|
-
constructor(tracks, options) {
|
|
8
|
-
this.audios = [];
|
|
9
|
-
this.wavesurfers = [];
|
|
10
|
-
this.durations = [];
|
|
11
|
-
this.currentTime = 0;
|
|
12
|
-
this.maxDuration = 0;
|
|
13
|
-
this.isDragging = false;
|
|
14
|
-
this.tracks = tracks.map((track) => (Object.assign(Object.assign({}, track), { startPosition: track.startPosition || 0 })));
|
|
15
|
-
this.options = options;
|
|
16
|
-
this.rendering = initRendering(this.tracks, this.options);
|
|
17
|
-
this.initAudios().then((durations) => {
|
|
18
|
-
this.durations = durations;
|
|
19
|
-
const maxDuration = this.tracks.reduce((max, track, index) => {
|
|
20
|
-
return Math.max(max, track.startPosition + durations[index]);
|
|
21
|
-
}, 0);
|
|
22
|
-
this.rendering.setMainWidth(maxDuration * this.options.minPxPerSec);
|
|
23
|
-
this.rendering.addClickHandler((position) => {
|
|
24
|
-
this.onSeek(position * maxDuration);
|
|
25
|
-
});
|
|
26
|
-
this.maxDuration = maxDuration;
|
|
27
|
-
});
|
|
28
|
-
this.initWavesurfers();
|
|
29
|
-
this.rendering.containers.forEach((container, index) => {
|
|
30
|
-
const drag = initDragging(container, (delta) => this.onDrag(index, delta));
|
|
31
|
-
this.wavesurfers[index].once('destroy', () => {
|
|
32
|
-
drag === null || drag === void 0 ? void 0 : drag.destroy();
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
initAudios() {
|
|
37
|
-
const emptyTrackUrl = 'data:audio/wav;base64,UklGRigAAABXQVZFZm10IBIAAAABAAEARKwAAIhYAQACABAAAABkYXRhAgAAAAEA';
|
|
38
|
-
this.audios = this.tracks.map((track) => new Audio(track.url || emptyTrackUrl));
|
|
39
|
-
return Promise.all(this.audios.map((audio) => {
|
|
40
|
-
return new Promise((resolve) => {
|
|
41
|
-
audio.addEventListener('canplay', () => {
|
|
42
|
-
resolve(audio.duration);
|
|
43
|
-
}, { once: true });
|
|
44
|
-
});
|
|
45
|
-
}));
|
|
46
|
-
}
|
|
47
|
-
initWavesurfers() {
|
|
48
|
-
const wavesurfers = this.tracks.map((track, index) => {
|
|
49
|
-
// Create a wavesurfer instance
|
|
50
|
-
const ws = WaveSurfer.create(Object.assign(Object.assign({}, this.options), { container: this.rendering.containers[index], media: this.audios[index], peaks: track.peaks, cursorColor: 'transparent', fillParent: false, interactive: false }));
|
|
51
|
-
ws.on('timeupdate', ({ currentTime }) => {
|
|
52
|
-
if (ws.isPlaying() && currentTime + track.startPosition > this.currentTime) {
|
|
53
|
-
this.updatePosition(track.startPosition + currentTime);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
const wsRegions = ws.registerPlugin(RegionsPlugin, {
|
|
57
|
-
draggable: false,
|
|
58
|
-
resizable: false,
|
|
59
|
-
dragSelection: false,
|
|
60
|
-
});
|
|
61
|
-
ws.once('decode', () => {
|
|
62
|
-
// Render start and end cues
|
|
63
|
-
if (track.startCue) {
|
|
64
|
-
wsRegions.add(track.startCue, track.startCue, 'start');
|
|
65
|
-
}
|
|
66
|
-
if (track.endCue) {
|
|
67
|
-
wsRegions.add(track.endCue, track.endCue, 'end');
|
|
68
|
-
}
|
|
69
|
-
// Render markers
|
|
70
|
-
;
|
|
71
|
-
(track.markers || []).forEach((marker) => {
|
|
72
|
-
wsRegions.add(marker.time, marker.time, marker.label, marker.color);
|
|
73
|
-
});
|
|
74
|
-
});
|
|
75
|
-
return ws;
|
|
76
|
-
});
|
|
77
|
-
this.wavesurfers = wavesurfers;
|
|
78
|
-
}
|
|
79
|
-
updatePosition(time) {
|
|
80
|
-
const precisionSeconds = 0.3;
|
|
81
|
-
this.currentTime = time;
|
|
82
|
-
this.rendering.updateCursor(time / this.maxDuration);
|
|
83
|
-
const isPaused = !this.isPlaying();
|
|
84
|
-
// Update the current time of each audio
|
|
85
|
-
this.tracks.forEach((track, index) => {
|
|
86
|
-
const audio = this.audios[index];
|
|
87
|
-
const duration = this.durations[index];
|
|
88
|
-
const newTime = time - track.startPosition;
|
|
89
|
-
if (Math.abs(audio.currentTime - newTime) > precisionSeconds) {
|
|
90
|
-
audio.currentTime = newTime;
|
|
91
|
-
}
|
|
92
|
-
// If the position is out of the track bounds, pause it
|
|
93
|
-
if (isPaused || newTime < 0 || newTime > duration) {
|
|
94
|
-
!audio.paused && audio.pause();
|
|
95
|
-
}
|
|
96
|
-
else if (!isPaused) {
|
|
97
|
-
// If the position is in the track bounds, play it
|
|
98
|
-
audio.paused && audio.play();
|
|
99
|
-
}
|
|
100
|
-
// Unmute if cue is reached
|
|
101
|
-
const newVolume = newTime >= (track.startCue || 0) && newTime < (track.endCue || Infinity) ? 1 : 0;
|
|
102
|
-
if (newVolume !== audio.volume)
|
|
103
|
-
audio.volume = newVolume;
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
onSeek(time) {
|
|
107
|
-
if (this.isDragging)
|
|
108
|
-
return;
|
|
109
|
-
const wasPlaying = this.isPlaying();
|
|
110
|
-
this.updatePosition(time);
|
|
111
|
-
if (wasPlaying)
|
|
112
|
-
this.play();
|
|
113
|
-
}
|
|
114
|
-
onDrag(index, delta) {
|
|
115
|
-
const track = this.tracks[index];
|
|
116
|
-
if (!track.draggable)
|
|
117
|
-
return;
|
|
118
|
-
this.isDragging = true;
|
|
119
|
-
setTimeout(() => (this.isDragging = false), 300);
|
|
120
|
-
const newTime = track.startPosition + delta * this.maxDuration;
|
|
121
|
-
track.startPosition = newTime;
|
|
122
|
-
this.rendering.setContainerOffsets();
|
|
123
|
-
}
|
|
124
|
-
findCurrentTracks() {
|
|
125
|
-
// Find the audios at the current time
|
|
126
|
-
const indexes = [];
|
|
127
|
-
this.tracks.forEach((track, index) => {
|
|
128
|
-
if (track.url &&
|
|
129
|
-
this.currentTime >= track.startPosition &&
|
|
130
|
-
this.currentTime < track.startPosition + this.durations[index]) {
|
|
131
|
-
indexes.push(index);
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
if (indexes.length === 0) {
|
|
135
|
-
const minStartTime = Math.min(...this.tracks.filter((t) => t.url).map((track) => track.startPosition));
|
|
136
|
-
indexes.push(this.tracks.findIndex((track) => track.startPosition === minStartTime));
|
|
137
|
-
}
|
|
138
|
-
return indexes;
|
|
139
|
-
}
|
|
140
|
-
play() {
|
|
141
|
-
const indexes = this.findCurrentTracks();
|
|
142
|
-
indexes.forEach((index) => {
|
|
143
|
-
var _a;
|
|
144
|
-
(_a = this.audios[index]) === null || _a === void 0 ? void 0 : _a.play();
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
pause() {
|
|
148
|
-
this.audios.forEach((audio) => audio.pause());
|
|
149
|
-
}
|
|
150
|
-
isPlaying() {
|
|
151
|
-
return this.audios.some((audio) => !audio.paused);
|
|
152
|
-
}
|
|
153
|
-
destroy() {
|
|
154
|
-
this.rendering.destroy();
|
|
155
|
-
this.audios.forEach((audio) => {
|
|
156
|
-
audio.pause();
|
|
157
|
-
audio.src = '';
|
|
158
|
-
});
|
|
159
|
-
this.wavesurfers.forEach((ws) => {
|
|
160
|
-
ws.destroy();
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
function initRendering(tracks, options) {
|
|
165
|
-
// Create a common container for all tracks
|
|
166
|
-
let scroll = document.createElement('div');
|
|
167
|
-
scroll.setAttribute('style', 'width: 100%; overflow-x: scroll; overflow-y: hidden; user-select: none;');
|
|
168
|
-
const wrapper = document.createElement('div');
|
|
169
|
-
wrapper.style.position = 'relative';
|
|
170
|
-
scroll.appendChild(wrapper);
|
|
171
|
-
options.container.appendChild(scroll);
|
|
172
|
-
// Create a common cursor
|
|
173
|
-
const cursor = document.createElement('div');
|
|
174
|
-
cursor.setAttribute('style', 'width: 1px; height: 100%; position: absolute; z-index: 10; top: 0; left: 0');
|
|
175
|
-
cursor.style.backgroundColor = options.cursorColor || options.progressColor || '#000';
|
|
176
|
-
wrapper.appendChild(cursor);
|
|
177
|
-
// Create containers for each track
|
|
178
|
-
const containers = tracks.map(() => {
|
|
179
|
-
const container = document.createElement('div');
|
|
180
|
-
container.style.width = 'fit-content';
|
|
181
|
-
wrapper.appendChild(container);
|
|
182
|
-
return container;
|
|
183
|
-
});
|
|
184
|
-
// Set the positions of each container
|
|
185
|
-
const setContainerOffsets = () => {
|
|
186
|
-
containers.forEach((container, i) => {
|
|
187
|
-
const offset = (tracks[i].startPosition * options.minPxPerSec) / devicePixelRatio;
|
|
188
|
-
container.style.transform = `translateX(${offset}px)`;
|
|
189
|
-
if (tracks[i].draggable)
|
|
190
|
-
container.style.cursor = 'ew-resize';
|
|
191
|
-
});
|
|
192
|
-
};
|
|
193
|
-
setContainerOffsets();
|
|
194
|
-
return {
|
|
195
|
-
containers,
|
|
196
|
-
// Set the start offset
|
|
197
|
-
setContainerOffsets,
|
|
198
|
-
// Set the container width
|
|
199
|
-
setMainWidth: (width) => {
|
|
200
|
-
wrapper.style.width = `${width / devicePixelRatio}px`;
|
|
201
|
-
},
|
|
202
|
-
// Update cursor position
|
|
203
|
-
updateCursor: (position) => {
|
|
204
|
-
cursor.style.left = `${Math.min(100, position * 100)}%`;
|
|
205
|
-
},
|
|
206
|
-
// Click to seek
|
|
207
|
-
addClickHandler: (onClick) => {
|
|
208
|
-
wrapper.addEventListener('click', (e) => {
|
|
209
|
-
const rect = wrapper.getBoundingClientRect();
|
|
210
|
-
const x = e.clientX - rect.left;
|
|
211
|
-
const position = x / wrapper.offsetWidth;
|
|
212
|
-
onClick(position);
|
|
213
|
-
});
|
|
214
|
-
},
|
|
215
|
-
// Destroy the container
|
|
216
|
-
destroy: () => {
|
|
217
|
-
scroll.remove();
|
|
218
|
-
},
|
|
219
|
-
};
|
|
220
|
-
}
|
|
221
|
-
function initDragging(container, onDrag) {
|
|
222
|
-
const wrapper = container.parentElement;
|
|
223
|
-
if (!wrapper)
|
|
224
|
-
return;
|
|
225
|
-
// Dragging tracks to set position
|
|
226
|
-
let dragStart = null;
|
|
227
|
-
// Drag start
|
|
228
|
-
container.addEventListener('mousedown', (e) => {
|
|
229
|
-
const rect = wrapper.getBoundingClientRect();
|
|
230
|
-
dragStart = e.clientX - rect.left;
|
|
231
|
-
});
|
|
232
|
-
// Drag end
|
|
233
|
-
const onMouseUp = (e) => {
|
|
234
|
-
if (dragStart != null) {
|
|
235
|
-
e.stopPropagation();
|
|
236
|
-
dragStart = null;
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
// Drag move
|
|
240
|
-
const onMouseMove = (e) => {
|
|
241
|
-
if (dragStart == null)
|
|
242
|
-
return;
|
|
243
|
-
const rect = wrapper.getBoundingClientRect();
|
|
244
|
-
const x = e.clientX - rect.left;
|
|
245
|
-
const diff = x - dragStart;
|
|
246
|
-
if (diff > 1 || diff < -1) {
|
|
247
|
-
dragStart = x;
|
|
248
|
-
onDrag(diff / wrapper.offsetWidth);
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
document.body.addEventListener('mouseup', onMouseUp);
|
|
252
|
-
document.body.addEventListener('mousemove', onMouseMove);
|
|
253
|
-
return {
|
|
254
|
-
destroy: () => {
|
|
255
|
-
document.body.removeEventListener('mouseup', onMouseUp);
|
|
256
|
-
document.body.removeEventListener('mousemove', onMouseMove);
|
|
257
|
-
},
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
export default MultiTrack;
|