wavesurfer.js 7.0.0-alpha.4 → 7.0.0-alpha.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +64 -17
  2. package/dist/base-plugin.d.ts +14 -5
  3. package/dist/base-plugin.js +5 -1
  4. package/dist/decoder.d.ts +8 -10
  5. package/dist/decoder.js +37 -44
  6. package/dist/event-emitter.d.ts +16 -10
  7. package/dist/event-emitter.js +38 -16
  8. package/dist/fetcher.js +2 -13
  9. package/dist/legacy-adapter.d.ts +7 -0
  10. package/dist/legacy-adapter.js +15 -0
  11. package/dist/player.d.ts +32 -11
  12. package/dist/player.js +55 -39
  13. package/dist/plugins/envelope.d.ts +57 -0
  14. package/dist/plugins/envelope.js +305 -0
  15. package/dist/plugins/envelope.min.js +1 -0
  16. package/dist/plugins/minimap.d.ts +34 -0
  17. package/dist/plugins/minimap.js +99 -0
  18. package/dist/plugins/minimap.min.js +1 -0
  19. package/dist/plugins/multitrack.d.ts +117 -0
  20. package/dist/plugins/multitrack.js +506 -0
  21. package/dist/plugins/multitrack.min.js +1 -0
  22. package/dist/plugins/record.d.ts +26 -0
  23. package/dist/plugins/record.js +125 -0
  24. package/dist/plugins/record.min.js +1 -0
  25. package/dist/plugins/regions.d.ts +88 -41
  26. package/dist/plugins/regions.js +365 -170
  27. package/dist/plugins/regions.min.js +1 -0
  28. package/dist/plugins/spectrogram.d.ts +24 -0
  29. package/dist/plugins/spectrogram.js +165 -0
  30. package/dist/plugins/timeline.d.ts +41 -0
  31. package/dist/plugins/timeline.js +135 -0
  32. package/dist/plugins/timeline.min.js +1 -0
  33. package/dist/renderer.d.ts +25 -12
  34. package/dist/renderer.js +211 -157
  35. package/dist/timer.d.ts +1 -1
  36. package/dist/wavesurfer.d.ts +134 -0
  37. package/dist/wavesurfer.js +213 -0
  38. package/dist/wavesurfer.min.js +1 -1
  39. package/package.json +49 -24
  40. package/dist/index.d.ts +0 -104
  41. package/dist/index.js +0 -188
  42. package/dist/player-webaudio.d.ts +0 -8
  43. package/dist/player-webaudio.js +0 -32
  44. package/dist/react/useWavesurfer.d.ts +0 -5
  45. package/dist/react/useWavesurfer.js +0 -20
  46. package/dist/wavesurfer.Regions.min.js +0 -1
package/dist/renderer.js CHANGED
@@ -1,28 +1,47 @@
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
1
  import EventEmitter from './event-emitter.js';
11
2
  class Renderer extends EventEmitter {
12
- constructor(options) {
3
+ constructor(params, options) {
13
4
  super();
5
+ this.options = {
6
+ height: 0,
7
+ };
14
8
  this.timeout = null;
15
- this.options = options;
9
+ this.isScrolling = false;
10
+ this.channelData = null;
11
+ this.duration = null;
12
+ this.resizeObserver = null;
13
+ this.options = { ...options };
16
14
  let container = null;
17
- if (typeof this.options.container === 'string') {
18
- container = document.querySelector(this.options.container);
15
+ if (typeof params.container === 'string') {
16
+ container = document.querySelector(params.container);
19
17
  }
20
- else if (this.options.container instanceof HTMLElement) {
21
- container = this.options.container;
18
+ else if (params.container instanceof HTMLElement) {
19
+ container = params.container;
22
20
  }
23
21
  if (!container) {
24
22
  throw new Error('Container not found');
25
23
  }
24
+ const [div, shadow] = this.initHtml();
25
+ container.appendChild(div);
26
+ this.container = div;
27
+ this.scrollContainer = shadow.querySelector('.scroll');
28
+ this.wrapper = shadow.querySelector('.wrapper');
29
+ this.canvasWrapper = shadow.querySelector('.canvases');
30
+ this.progressWrapper = shadow.querySelector('.progress');
31
+ // Set a click handler
32
+ this.wrapper.addEventListener('click', (e) => {
33
+ const rect = this.wrapper.getBoundingClientRect();
34
+ const x = e.clientX - rect.left;
35
+ const relativeX = x / rect.width;
36
+ this.emit('click', relativeX);
37
+ });
38
+ // Re-render the waveform on container resize
39
+ this.resizeObserver = new ResizeObserver(() => {
40
+ this.delay(() => this.reRender(), 100);
41
+ });
42
+ this.resizeObserver.observe(this.scrollContainer);
43
+ }
44
+ initHtml() {
26
45
  const div = document.createElement('div');
27
46
  const shadow = div.attachShadow({ mode: 'open' });
28
47
  shadow.innerHTML = `
@@ -32,73 +51,72 @@ class Renderer extends EventEmitter {
32
51
  }
33
52
  :host .scroll {
34
53
  overflow-x: auto;
35
- overflow-y: visible;
54
+ overflow-y: hidden;
36
55
  width: 100%;
37
- height: ${this.options.height}px;
38
56
  position: relative;
39
57
  }
58
+ :host .noScrollbar {
59
+ scrollbar-color: transparent;
60
+ scrollbar-width: none;
61
+ }
62
+ :host .noScrollbar::-webkit-scrollbar {
63
+ display: none;
64
+ -webkit-appearance: none;
65
+ }
40
66
  :host .wrapper {
41
67
  position: relative;
42
- width: fit-content;
43
- min-width: 100%;
44
- height: 100%;
68
+ overflow: visible;
45
69
  z-index: 2;
46
70
  }
71
+ :host .canvases {
72
+ position: relative;
73
+ height: ${this.options.height}px;
74
+ }
47
75
  :host canvas {
48
76
  display: block;
49
- height: 100%;
50
- min-width: 100%;
77
+ position: absolute;
78
+ top: 0;
51
79
  image-rendering: pixelated;
80
+ height: ${this.options.height}px;
52
81
  }
53
82
  :host .progress {
54
- position: absolute;
55
- z-index: 2;
56
- top: 0;
57
- left: 0;
58
- height: 100%;
59
83
  pointer-events: none;
60
- clip-path: inset(100%);
61
- }
62
- :host .cursor {
63
84
  position: absolute;
64
- z-index: 3;
85
+ z-index: 2;
65
86
  top: 0;
66
87
  left: 0;
88
+ width: 0;
67
89
  height: 100%;
68
- border-right: 1px solid ${this.options.progressColor};
69
- margin-left: -1px;
90
+ overflow: hidden;
91
+ box-sizing: border-box;
70
92
  }
71
93
  </style>
72
94
 
73
95
  <div class="scroll">
74
96
  <div class="wrapper">
75
- <canvas></canvas>
76
- <canvas class="progress"></canvas>
77
- <div class="cursor"></div>
97
+ <div class="canvases"></div>
98
+ <div class="progress"></div>
78
99
  </div>
79
100
  </div>
80
101
  `;
81
- this.container = div;
82
- this.scrollContainer = shadow.querySelector('.scroll');
83
- this.mainCanvas = shadow.querySelector('canvas');
84
- this.ctx = this.mainCanvas.getContext('2d', { desynchronized: true });
85
- this.progressCanvas = shadow.querySelector('.progress');
86
- this.cursor = shadow.querySelector('.cursor');
87
- container.appendChild(div);
88
- this.mainCanvas.addEventListener('click', (e) => {
89
- const rect = this.mainCanvas.getBoundingClientRect();
90
- const x = e.clientX - rect.left;
91
- const relativeX = x / rect.width;
92
- this.emit('click', { relativeX });
93
- });
102
+ return [div, shadow];
103
+ }
104
+ setOptions(options) {
105
+ this.options = options;
106
+ // Re-render the waveform
107
+ this.reRender();
94
108
  }
95
109
  getContainer() {
96
- return this.scrollContainer.querySelector('.wrapper');
110
+ return this.scrollContainer;
111
+ }
112
+ getWrapper() {
113
+ return this.wrapper;
97
114
  }
98
115
  destroy() {
99
116
  this.container.remove();
117
+ this.resizeObserver?.disconnect();
100
118
  }
101
- delay(fn, delayMs = 100) {
119
+ delay(fn, delayMs = 10) {
102
120
  if (this.timeout) {
103
121
  clearTimeout(this.timeout);
104
122
  }
@@ -108,127 +126,163 @@ class Renderer extends EventEmitter {
108
126
  }, delayMs);
109
127
  });
110
128
  }
111
- renderPeaks(channelData, width, height) {
112
- var _a;
113
- return __awaiter(this, void 0, void 0, function* () {
114
- const { devicePixelRatio } = window;
115
- const { ctx } = this;
116
- const barWidth = this.options.barWidth != null ? this.options.barWidth * devicePixelRatio : 1;
117
- const barGap = this.options.barGap != null ? this.options.barGap * devicePixelRatio : this.options.barWidth ? barWidth / 2 : 0;
118
- const barRadius = (_a = this.options.barRadius) !== null && _a !== void 0 ? _a : 0;
119
- const leftChannel = channelData[0];
120
- const len = leftChannel.length;
121
- const barCount = Math.floor(width / (barWidth + barGap));
122
- const barIndexScale = barCount / len;
123
- const halfHeight = height / 2;
124
- const isMono = channelData.length === 1;
125
- const rightChannel = isMono ? leftChannel : channelData[1];
126
- const useNegative = isMono && rightChannel.some((v) => v < 0);
127
- const draw = (start, end) => {
128
- let prevX = 0;
129
- let prevLeft = 0;
130
- let prevRight = 0;
131
- ctx.beginPath();
132
- for (let i = start; i < end; i++) {
133
- const barIndex = Math.round(i * barIndexScale);
134
- if (barIndex > prevX) {
135
- const leftBarHeight = Math.round(prevLeft * halfHeight);
136
- const rightBarHeight = Math.round(prevRight * halfHeight);
137
- ctx.roundRect(prevX * (barWidth + barGap), halfHeight - leftBarHeight, barWidth, leftBarHeight + rightBarHeight || 1, barRadius);
138
- prevX = barIndex;
139
- prevLeft = 0;
140
- prevRight = 0;
141
- }
142
- const leftValue = useNegative ? leftChannel[i] : Math.abs(leftChannel[i]);
143
- const rightValue = useNegative ? rightChannel[i] : Math.abs(rightChannel[i]);
144
- if (leftValue > prevLeft) {
145
- prevLeft = leftValue;
146
- }
147
- // If stereo, both channels are drawn as max values
148
- // If mono with negative values, the bottom channel will be the min negative values
149
- if (useNegative ? rightValue < -prevRight : rightValue > prevRight) {
150
- prevRight = rightValue < 0 ? -rightValue : rightValue;
151
- }
129
+ async renderPeaks(channelData, width, height, pixelRatio) {
130
+ const barWidth = this.options.barWidth != null ? this.options.barWidth * pixelRatio : 1;
131
+ const barGap = this.options.barGap != null ? this.options.barGap * pixelRatio : this.options.barWidth ? barWidth / 2 : 0;
132
+ const barRadius = this.options.barRadius ?? 0;
133
+ const leftChannel = channelData[0];
134
+ const len = leftChannel.length;
135
+ const barCount = Math.floor(width / (barWidth + barGap));
136
+ const barIndexScale = barCount / len;
137
+ const halfHeight = height / 2;
138
+ const isMono = channelData.length === 1;
139
+ const rightChannel = isMono ? leftChannel : channelData[1];
140
+ const useNegative = isMono && rightChannel.some((v) => v < 0);
141
+ const draw = (start, end) => {
142
+ let prevX = 0;
143
+ let prevLeft = 0;
144
+ let prevRight = 0;
145
+ const canvas = document.createElement('canvas');
146
+ canvas.width = Math.round((width * (end - start)) / len);
147
+ canvas.height = this.options.height;
148
+ canvas.style.width = `${Math.floor(canvas.width / pixelRatio)}px`;
149
+ canvas.style.height = `${this.options.height}px`;
150
+ canvas.style.left = `${Math.floor((start * width) / pixelRatio / len)}px`;
151
+ this.canvasWrapper.appendChild(canvas);
152
+ const ctx = canvas.getContext('2d', {
153
+ desynchronized: true,
154
+ });
155
+ ctx.beginPath();
156
+ ctx.fillStyle = this.options.waveColor ?? '';
157
+ // Firefox shim until 2023.04.11
158
+ if (!ctx.roundRect)
159
+ ctx.roundRect = ctx.fillRect;
160
+ for (let i = start; i < end; i++) {
161
+ const barIndex = Math.round((i - start) * barIndexScale);
162
+ if (barIndex > prevX) {
163
+ const leftBarHeight = Math.round(prevLeft * halfHeight);
164
+ const rightBarHeight = Math.round(prevRight * halfHeight);
165
+ ctx.roundRect(prevX * (barWidth + barGap), halfHeight - leftBarHeight, barWidth, leftBarHeight + rightBarHeight || 1, barRadius);
166
+ prevX = barIndex;
167
+ prevLeft = 0;
168
+ prevRight = 0;
169
+ }
170
+ const leftValue = useNegative ? leftChannel[i] : Math.abs(leftChannel[i]);
171
+ const rightValue = useNegative ? rightChannel[i] : Math.abs(rightChannel[i]);
172
+ if (leftValue > prevLeft) {
173
+ prevLeft = leftValue;
174
+ }
175
+ // If stereo, both channels are drawn as max values
176
+ // If mono with negative values, the bottom channel will be the min negative values
177
+ if (useNegative ? rightValue < -prevRight : rightValue > prevRight) {
178
+ prevRight = rightValue < 0 ? -rightValue : rightValue;
152
179
  }
153
- ctx.fillStyle = this.options.waveColor;
154
- ctx.fill();
155
- ctx.closePath();
156
- };
157
- // Clear the canvas
158
- ctx.clearRect(0, 0, width, height);
159
- // Draw the currently visible part of the waveform
160
- const { scrollLeft, scrollWidth, clientWidth } = this.scrollContainer;
161
- const scale = len / scrollWidth;
162
- const start = Math.floor(scrollLeft * scale);
163
- const end = Math.ceil((scrollLeft + clientWidth) * scale);
164
- draw(start, end);
165
- // Draw the progress mask
166
- this.createProgressMask();
167
- // Draw the rest of the waveform with a timeout for better performance
168
- if (start > 0) {
169
- yield this.delay(() => {
170
- draw(0, start);
171
- });
172
180
  }
173
- if (end < len) {
174
- yield this.delay(() => {
175
- draw(end, len);
176
- });
181
+ ctx.fill();
182
+ ctx.closePath();
183
+ // Draw a progress canvas
184
+ const progressCanvas = canvas.cloneNode();
185
+ this.progressWrapper.appendChild(progressCanvas);
186
+ const progressCtx = progressCanvas.getContext('2d', {
187
+ desynchronized: true,
188
+ });
189
+ if (canvas.width > 0 && canvas.height > 0) {
190
+ progressCtx.drawImage(canvas, 0, 0);
177
191
  }
178
- // Redraw the progress mask
179
- this.delay(() => {
180
- this.createProgressMask();
192
+ // Set the composition method to draw only where the waveform is drawn
193
+ progressCtx.globalCompositeOperation = 'source-in';
194
+ progressCtx.fillStyle = this.options.progressColor ?? '';
195
+ // This rectangle acts as a mask thanks to the composition method
196
+ progressCtx.fillRect(0, 0, canvas.width, canvas.height);
197
+ };
198
+ // Clear the canvas
199
+ this.canvasWrapper.innerHTML = '';
200
+ this.progressWrapper.innerHTML = '';
201
+ // Determine the currently visible part of the waveform
202
+ const { scrollLeft, scrollWidth, clientWidth } = this.scrollContainer;
203
+ const scale = len / scrollWidth;
204
+ let viewportWidth = Math.min(Renderer.MAX_CANVAS_WIDTH, clientWidth);
205
+ viewportWidth -= viewportWidth % ((barWidth + barGap) / pixelRatio);
206
+ const start = Math.floor(Math.abs(scrollLeft) * scale);
207
+ const end = Math.ceil(start + viewportWidth * scale);
208
+ // Draw the visible portion of the waveform
209
+ draw(start, end);
210
+ // Draw the rest of the waveform with a timeout for better performance
211
+ const step = end - start;
212
+ for (let i = end; i < len; i += step) {
213
+ await this.delay(() => {
214
+ draw(i, Math.min(len, i + step));
181
215
  });
182
- });
183
- }
184
- createProgressMask() {
185
- const progressCtx = this.progressCanvas.getContext('2d', { desynchronized: true });
186
- // Set the canvas to the same size as the main canvas
187
- this.progressCanvas.width = this.mainCanvas.width;
188
- this.progressCanvas.height = this.mainCanvas.height;
189
- this.progressCanvas.style.width = this.mainCanvas.style.width;
190
- this.progressCanvas.style.height = this.mainCanvas.style.height;
191
- // Copy the waveform image to the progress canvas
192
- // The main canvas itself is used as the source image
193
- progressCtx.drawImage(this.mainCanvas, 0, 0);
194
- // Set the composition method to draw only where the waveform is drawn
195
- progressCtx.globalCompositeOperation = 'source-in';
196
- progressCtx.fillStyle = this.options.progressColor;
197
- // This rectangle acts as a mask thanks to the composition method
198
- progressCtx.fillRect(0, 0, this.progressCanvas.width, this.progressCanvas.height);
216
+ }
217
+ for (let i = start - 1; i >= 0; i -= step) {
218
+ await this.delay(() => {
219
+ draw(Math.max(0, i - step), i);
220
+ });
221
+ }
199
222
  }
200
223
  render(channelData, duration) {
201
- // Determine the width of the canvas
202
- const { devicePixelRatio } = window;
203
- const parentWidth = this.scrollContainer.clientWidth * devicePixelRatio;
204
- const scrollWidth = duration * this.options.minPxPerSec;
205
- const scrollParent = scrollWidth > parentWidth;
206
- const width = scrollParent ? scrollWidth : parentWidth;
224
+ // Determine the width of the waveform
225
+ const pixelRatio = window.devicePixelRatio || 1;
226
+ const parentWidth = this.scrollContainer.clientWidth;
227
+ const scrollWidth = Math.ceil(duration * (this.options.minPxPerSec || 0));
228
+ // Whether the container should scroll
229
+ this.isScrolling = scrollWidth > parentWidth;
230
+ const useParentWidth = this.options.fillParent && !this.isScrolling;
231
+ // Width and height of the waveform in pixels
232
+ const width = (useParentWidth ? parentWidth : scrollWidth) * pixelRatio;
207
233
  const { height } = this.options;
208
- this.mainCanvas.width = width;
209
- this.mainCanvas.height = height;
210
- this.mainCanvas.style.width = scrollParent ? Math.round(scrollWidth / devicePixelRatio) + 'px' : '100%';
211
- this.renderPeaks(channelData, width, height);
234
+ // Set the width of the wrapper
235
+ this.wrapper.style.width = useParentWidth ? '100%' : `${scrollWidth}px`;
236
+ // Set additional styles
237
+ this.scrollContainer.style.overflowX = this.isScrolling ? 'auto' : 'hidden';
238
+ this.scrollContainer.classList.toggle('noScrollbar', !!this.options.hideScrollbar);
239
+ this.progressWrapper.style.borderRightStyle = 'solid';
240
+ this.progressWrapper.style.borderRightColor = `${this.options.cursorColor || this.options.progressColor}`;
241
+ this.progressWrapper.style.borderRightWidth = `${this.options.cursorWidth}px`;
242
+ this.canvasWrapper.style.height = `${this.options.height}px`;
243
+ // Render the waveform
244
+ this.renderPeaks(channelData, width, height, pixelRatio);
245
+ this.channelData = channelData;
246
+ this.duration = duration;
212
247
  }
213
- zoom(channelData, duration, minPxPerSec) {
248
+ reRender() {
249
+ // Return if the waveform has not been rendered yet
250
+ if (!this.channelData || !this.duration)
251
+ return;
214
252
  // Remember the current cursor position
215
- const oldCursorPosition = this.cursor.getBoundingClientRect().left;
216
- this.options.minPxPerSec = minPxPerSec;
217
- this.render(channelData, duration);
253
+ const oldCursorPosition = this.progressWrapper.clientWidth;
254
+ // Set the new zoom level and re-render the waveform
255
+ this.render(this.channelData, this.duration);
218
256
  // Adjust the scroll position so that the cursor stays in the same place
219
- const newCursortPosition = this.cursor.getBoundingClientRect().left;
257
+ const newCursortPosition = this.progressWrapper.clientWidth;
220
258
  this.scrollContainer.scrollLeft += newCursortPosition - oldCursorPosition;
221
259
  }
260
+ zoom(minPxPerSec) {
261
+ this.options.minPxPerSec = minPxPerSec;
262
+ this.reRender();
263
+ }
222
264
  renderProgress(progress, autoCenter = false) {
223
- this.progressCanvas.style.clipPath = `inset(0 ${100 - progress * 100}% 0 0)`;
224
- this.cursor.style.left = `${progress * 100}%`;
225
- if (autoCenter) {
226
- const center = this.container.clientWidth / 2;
227
- const fullWidth = this.mainCanvas.clientWidth;
228
- if (fullWidth * progress >= center) {
229
- this.scrollContainer.scrollLeft = fullWidth * progress - center;
265
+ if (isNaN(progress))
266
+ return;
267
+ this.progressWrapper.style.width = `${progress * 100}%`;
268
+ if (this.isScrolling && this.options.autoCenter) {
269
+ const { clientWidth, scrollLeft, scrollWidth } = this.scrollContainer;
270
+ const progressWidth = scrollWidth * progress;
271
+ const center = clientWidth / 2;
272
+ const minScroll = autoCenter ? center : clientWidth;
273
+ const minDiff = center / 20;
274
+ if (progressWidth > scrollLeft + minScroll || progressWidth < scrollLeft) {
275
+ // If the cursor is in viewport but not centered, scroll to the center slowly
276
+ if (progressWidth - (scrollLeft + center) >= minDiff && progressWidth < scrollLeft + clientWidth) {
277
+ this.scrollContainer.scrollLeft += minDiff;
278
+ }
279
+ else {
280
+ // Otherwise, scroll to the center immediately
281
+ this.scrollContainer.scrollLeft = progressWidth - center;
282
+ }
230
283
  }
231
284
  }
232
285
  }
233
286
  }
287
+ Renderer.MAX_CANVAS_WIDTH = 4000;
234
288
  export default Renderer;
package/dist/timer.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import EventEmitter from './event-emitter.js';
2
2
  type TimerEvents = {
3
- tick: void;
3
+ tick: [];
4
4
  };
5
5
  declare class Timer extends EventEmitter<TimerEvents> {
6
6
  private unsubscribe;
@@ -0,0 +1,134 @@
1
+ import { type RendererStyleOptions } from './renderer.js';
2
+ import Player from './player.js';
3
+ import type { GenericPlugin } from './base-plugin.js';
4
+ export type WaveSurferOptions = {
5
+ /** HTML element or CSS selector */
6
+ container: HTMLElement | string;
7
+ /** The height of the waveform in pixels */
8
+ height?: number;
9
+ /** The color of the waveform */
10
+ waveColor?: string;
11
+ /** The color of the progress mask */
12
+ progressColor?: string;
13
+ /** The color of the playpack cursor */
14
+ cursorColor?: string;
15
+ /** The cursor width */
16
+ cursorWidth?: number;
17
+ /** If set, the waveform will be rendered with bars like this: ▁ ▂ ▇ ▃ ▅ ▂ */
18
+ barWidth?: number;
19
+ /** Spacing between bars in pixels */
20
+ barGap?: number;
21
+ /** Rounded borders for bars */
22
+ barRadius?: number;
23
+ /** Minimum pixels per second of audio (i.e. zoom level) */
24
+ minPxPerSec?: number;
25
+ /** Stretch the waveform to fill the container, true by default */
26
+ fillParent?: boolean;
27
+ /** Audio URL */
28
+ url?: string;
29
+ /** Pre-computed audio data */
30
+ peaks?: Float32Array[] | Array<number[]>;
31
+ /** Pre-computed duration */
32
+ duration?: number;
33
+ /** Use an existing media element instead of creating one */
34
+ media?: HTMLMediaElement;
35
+ /** Play the audio on load */
36
+ autoplay?: boolean;
37
+ /** Pass false to disable clicks on the waveform */
38
+ interact?: boolean;
39
+ /** Hide the scrollbar */
40
+ hideScrollbar?: boolean;
41
+ /** Audio rate */
42
+ audioRate?: number;
43
+ /** Keep scroll in the center of the waveform during playback */
44
+ autoCenter?: boolean;
45
+ /** The list of plugins to initialize on start */
46
+ plugins?: GenericPlugin[];
47
+ };
48
+ declare const defaultOptions: {
49
+ height: number;
50
+ waveColor: string;
51
+ progressColor: string;
52
+ cursorWidth: number;
53
+ minPxPerSec: number;
54
+ fillParent: boolean;
55
+ interact: boolean;
56
+ autoCenter: boolean;
57
+ };
58
+ export type WaveSurferEvents = {
59
+ /** When audio starts loading */
60
+ load: [url: string];
61
+ /** When the audio has been decoded */
62
+ decode: [duration: number];
63
+ /** When the media element has loaded enough to play */
64
+ canplay: [duration: number];
65
+ /** When the audio is both decoded and can play */
66
+ ready: [duration: number];
67
+ /** When a waveform is drawn */
68
+ redraw: [];
69
+ /** When the audio starts playing */
70
+ play: [];
71
+ /** When the audio pauses */
72
+ pause: [];
73
+ /** When the audio finishes playing */
74
+ finish: [];
75
+ /** On audio position change, fires continuously while the audio is playing */
76
+ timeupdate: [currentTime: number];
77
+ /** An alias of timeupdate but only when the audio is playing */
78
+ audioprocess: [currentTime: number];
79
+ /** When the user seeks to a new position */
80
+ seeking: [currentTime: number];
81
+ /** When a user interaction (i.e. a click on the waveform) happens */
82
+ interaction: [];
83
+ /** When the zoom level changes */
84
+ zoom: [minPxPerSec: number];
85
+ /** Just before the waveform is destroyed so you can clean up your events */
86
+ destroy: [];
87
+ };
88
+ declare class WaveSurfer extends Player<WaveSurferEvents> {
89
+ options: WaveSurferOptions & typeof defaultOptions;
90
+ private fetcher;
91
+ private renderer;
92
+ private timer;
93
+ private plugins;
94
+ private decodedData;
95
+ private canPlay;
96
+ /** Create a new WaveSurfer instance */
97
+ static create(options: WaveSurferOptions): WaveSurfer;
98
+ /** Create a new WaveSurfer instance */
99
+ constructor(options: WaveSurferOptions);
100
+ setOptions(options: Partial<RendererStyleOptions>): void;
101
+ private initPlayerEvents;
102
+ private initRendererEvents;
103
+ private initTimerEvents;
104
+ private initReadyEvent;
105
+ private initPlugins;
106
+ /** Register a wavesurfer.js plugin */
107
+ registerPlugin<T extends GenericPlugin>(plugin: T): T;
108
+ /** Get all registered plugins */
109
+ getActivePlugins(): GenericPlugin[];
110
+ /** Load an audio file by URL, with optional pre-decoded audio data */
111
+ load(url: string, channelData?: WaveSurferOptions['peaks'], duration?: number): Promise<void>;
112
+ private renderAudio;
113
+ /** Zoom in or out */
114
+ zoom(minPxPerSec: number): void;
115
+ /** Get the decoded audio data */
116
+ getDecodedData(): AudioBuffer | null;
117
+ /** Get the duration of the audio in seconds */
118
+ getDuration(): number;
119
+ /** Toggle if the waveform should react to clicks */
120
+ toggleInteraction(isInteractive: boolean): void;
121
+ /** Seeks to a percentage of audio as [0..1] (0 = beginning, 1 = end) */
122
+ seekTo(progress: number): void;
123
+ /** Play or pause the audio */
124
+ playPause(): Promise<void>;
125
+ /** Stop the audio and go to the beginning */
126
+ stop(): void;
127
+ /** Skip N or -N seconds from the current positions */
128
+ skip(seconds: number): void;
129
+ /** Empty the waveform by loading a tiny silent audio */
130
+ empty(): void;
131
+ /** Unmount wavesurfer */
132
+ destroy(): void;
133
+ }
134
+ export default WaveSurfer;