@stlite/browser
Version:
Browser Stlite
1,412 lines (1,410 loc) • 48.9 kB
JavaScript
//#region ../react/build/wavesurfer.esm-SpUmkfwo.js
function e(e, t, n, r) {
return new (n ||= Promise)(function(i, a) {
function o(e) {
try {
c(r.next(e));
} catch (e) {
a(e);
}
}
function s(e) {
try {
c(r.throw(e));
} catch (e) {
a(e);
}
}
function c(e) {
var t;
e.done ? i(e.value) : (t = e.value, t instanceof n ? t : new n(function(e) {
e(t);
})).then(o, s);
}
c((r = r.apply(e, t || [])).next());
});
}
var t = class {
constructor() {
this.listeners = {};
}
on(e, t, n) {
if (this.listeners[e] || (this.listeners[e] = /* @__PURE__ */ new Set()), n?.once) {
let n = (...r) => {
this.un(e, n), t(...r);
};
return this.listeners[e].add(n), () => this.un(e, n);
}
return this.listeners[e].add(t), () => this.un(e, t);
}
un(e, t) {
var n;
(n = this.listeners[e]) == null || n.delete(t);
}
once(e, t) {
return this.on(e, t, { once: !0 });
}
unAll() {
this.listeners = {};
}
emit(e, ...t) {
this.listeners[e] && this.listeners[e].forEach((e) => e(...t));
}
}, n = {
decode: function(t, n) {
return e(this, void 0, void 0, function* () {
let e = new AudioContext({ sampleRate: n });
try {
return yield e.decodeAudioData(t);
} finally {
e.close();
}
});
},
createBuffer: function(e, t) {
if (!e || e.length === 0) throw Error("channelData must be a non-empty array");
if (t <= 0) throw Error("duration must be greater than 0");
if (typeof e[0] == "number" && (e = [e]), !e[0] || e[0].length === 0) throw Error("channelData must contain non-empty channel arrays");
(function(e) {
let t = e[0];
if (t.some((e) => e > 1 || e < -1)) {
let n = t.length, r = 0;
for (let e = 0; e < n; e++) {
let n = Math.abs(t[e]);
n > r && (r = n);
}
for (let t of e) for (let e = 0; e < n; e++) t[e] /= r;
}
})(e);
let n = e.map((e) => e instanceof Float32Array ? e : Float32Array.from(e));
return {
duration: t,
length: n[0].length,
sampleRate: n[0].length / t,
numberOfChannels: n.length,
getChannelData: (e) => {
let t = n[e];
if (!t) throw Error(`Channel ${e} not found`);
return t;
},
copyFromChannel: AudioBuffer.prototype.copyFromChannel,
copyToChannel: AudioBuffer.prototype.copyToChannel
};
}
};
function r(e, t) {
let n = t.xmlns ? document.createElementNS(t.xmlns, e) : document.createElement(e);
for (let [e, i] of Object.entries(t)) if (e === "children" && i) for (let [e, t] of Object.entries(i)) t instanceof Node ? n.appendChild(t) : typeof t == "string" ? n.appendChild(document.createTextNode(t)) : n.appendChild(r(e, t));
else e === "style" ? Object.assign(n.style, i) : e === "textContent" ? n.textContent = i : n.setAttribute(e, i.toString());
return n;
}
function i(e, t, n) {
let i = r(e, t || {});
return n?.appendChild(i), i;
}
var a = Object.freeze({
__proto__: null,
createElement: i,
default: i
}), o = { fetchBlob: function(t, n, r) {
return e(this, void 0, void 0, function* () {
let i = yield fetch(t, r);
if (i.status >= 400) throw Error(`Failed to fetch ${t}: ${i.status} (${i.statusText})`);
return function(t, n) {
e(this, void 0, void 0, function* () {
if (!t.body || !t.headers) return;
let e = t.body.getReader(), r = Number(t.headers.get("Content-Length")) || 0, i = 0, a = (e) => {
i += e?.length || 0, n(Math.round(i / r * 100));
};
try {
for (;;) {
let t = yield e.read();
if (t.done) break;
a(t.value);
}
} catch (e) {
console.warn("Progress tracking error:", e);
}
});
}(i.clone(), n), i.blob();
});
} };
function s(e) {
let t = e, n = /* @__PURE__ */ new Set();
return {
get value() {
return t;
},
set(e) {
Object.is(t, e) || (t = e, n.forEach((e) => e(t)));
},
update(e) {
this.set(e(t));
},
subscribe: (e) => (n.add(e), () => n.delete(e))
};
}
function c(e, t) {
let n = s(e());
return t.forEach((t) => t.subscribe(() => {
let t = e();
Object.is(n.value, t) || n.set(t);
})), {
get value() {
return n.value;
},
subscribe: (e) => n.subscribe(e)
};
}
function l(e, t) {
let n, r = () => {
n &&= (n(), void 0), n = e();
}, i = t.map((e) => e.subscribe(r));
return r(), () => {
n &&= (n(), void 0), i.forEach((e) => e());
};
}
var u = class extends t {
get isPlayingSignal() {
return this._isPlaying;
}
get currentTimeSignal() {
return this._currentTime;
}
get durationSignal() {
return this._duration;
}
get volumeSignal() {
return this._volume;
}
get mutedSignal() {
return this._muted;
}
get playbackRateSignal() {
return this._playbackRate;
}
get seekingSignal() {
return this._seeking;
}
constructor(e) {
super(), this.isExternalMedia = !1, this.reactiveMediaEventCleanups = [], e.media ? (this.media = e.media, this.isExternalMedia = !0) : this.media = document.createElement("audio"), this._isPlaying = s(!1), this._currentTime = s(0), this._duration = s(0), this._volume = s(this.media.volume), this._muted = s(this.media.muted), this._playbackRate = s(this.media.playbackRate || 1), this._seeking = s(!1), this.setupReactiveMediaEvents(), e.mediaControls && (this.media.controls = !0), e.autoplay && (this.media.autoplay = !0), e.playbackRate != null && this.onMediaEvent("canplay", () => {
e.playbackRate != null && (this.media.playbackRate = e.playbackRate);
}, { once: !0 });
}
setupReactiveMediaEvents() {
this.reactiveMediaEventCleanups.push(this.onMediaEvent("play", () => {
this._isPlaying.set(!0);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("pause", () => {
this._isPlaying.set(!1);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("ended", () => {
this._isPlaying.set(!1);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("timeupdate", () => {
this._currentTime.set(this.media.currentTime);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("durationchange", () => {
this._duration.set(this.media.duration || 0);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("loadedmetadata", () => {
this._duration.set(this.media.duration || 0);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("seeking", () => {
this._seeking.set(!0);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("seeked", () => {
this._seeking.set(!1);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("volumechange", () => {
this._volume.set(this.media.volume), this._muted.set(this.media.muted);
})), this.reactiveMediaEventCleanups.push(this.onMediaEvent("ratechange", () => {
this._playbackRate.set(this.media.playbackRate);
}));
}
onMediaEvent(e, t, n) {
return this.media.addEventListener(e, t, n), () => this.media.removeEventListener(e, t, n);
}
getSrc() {
return this.media.currentSrc || this.media.src || "";
}
revokeSrc() {
let e = this.getSrc();
e.startsWith("blob:") && URL.revokeObjectURL(e);
}
canPlayType(e) {
return this.media.canPlayType(e) !== "";
}
setSrc(e, t) {
let n = this.getSrc();
if (e && n === e) return;
this.revokeSrc();
let r = t instanceof Blob && (this.canPlayType(t.type) || !e) ? URL.createObjectURL(t) : e;
if (n && this.media.removeAttribute("src"), r || e) try {
this.media.src = r;
} catch {
this.media.src = e;
}
}
destroy() {
this.reactiveMediaEventCleanups.forEach((e) => e()), this.reactiveMediaEventCleanups = [], this.isExternalMedia || (this.media.pause(), this.revokeSrc(), this.media.removeAttribute("src"), this.media.load(), this.media.remove());
}
setMediaElement(e) {
this.reactiveMediaEventCleanups.forEach((e) => e()), this.reactiveMediaEventCleanups = [], this.media = e, this.setupReactiveMediaEvents();
}
play() {
return e(this, void 0, void 0, function* () {
try {
return yield this.media.play();
} catch (e) {
if (e instanceof DOMException && e.name === "AbortError") return;
throw e;
}
});
}
pause() {
this.media.pause();
}
isPlaying() {
return !this.media.paused && !this.media.ended;
}
setTime(e) {
this.media.currentTime = Math.max(0, Math.min(e, this.getDuration()));
}
getDuration() {
return this.media.duration;
}
getCurrentTime() {
return this.media.currentTime;
}
getVolume() {
return this.media.volume;
}
setVolume(e) {
this.media.volume = e;
}
getMuted() {
return this.media.muted;
}
setMuted(e) {
this.media.muted = e;
}
getPlaybackRate() {
return this.media.playbackRate;
}
isSeeking() {
return this.media.seeking;
}
setPlaybackRate(e, t) {
t != null && (this.media.preservesPitch = t), this.media.playbackRate = e;
}
getMediaElement() {
return this.media;
}
setSinkId(e) {
return this.media.setSinkId(e);
}
};
function d({ maxTop: e, maxBottom: t, halfHeight: n, vScale: r, barMinHeight: i = 0, barAlign: a }) {
let o = Math.round(e * n * r), s = o + Math.round(t * n * r) || 1;
return s < i && (s = i, a || (o = s / 2)), {
topHeight: o,
totalHeight: s
};
}
function f({ barAlign: e, halfHeight: t, topHeight: n, totalHeight: r, canvasHeight: i }) {
return e === "top" ? 0 : e === "bottom" ? i - r : t - n;
}
function p(e, t, n) {
let r = t - e.left, i = n - e.top;
return [r / e.width, i / e.height];
}
function m(e) {
return !!(e.barWidth || e.barGap || e.barAlign);
}
function h(e, t) {
if (!m(t)) return e;
let n = t.barWidth || .5, r = n + (t.barGap || n / 2);
return r === 0 ? e : Math.floor(e / r) * r;
}
function g({ scrollLeft: e, totalWidth: t, numCanvases: n }) {
if (t === 0) return [0];
let r = e / t, i = Math.floor(r * n);
return [
i - 1,
i,
i + 1
];
}
function _(e) {
let t = e._cleanup;
typeof t == "function" && t();
}
function v(e) {
let t = s({
scrollLeft: e.scrollLeft,
scrollWidth: e.scrollWidth,
clientWidth: e.clientWidth
}), n = c(() => function(e) {
let { scrollLeft: t, scrollWidth: n, clientWidth: r } = e;
if (n === 0) return {
startX: 0,
endX: 1
};
let i = t / n, a = (t + r) / n;
return {
startX: Math.max(0, Math.min(1, i)),
endX: Math.max(0, Math.min(1, a))
};
}(t.value), [t]), r = c(() => function(e) {
return {
left: e.scrollLeft,
right: e.scrollLeft + e.clientWidth
};
}(t.value), [t]), i = () => {
t.set({
scrollLeft: e.scrollLeft,
scrollWidth: e.scrollWidth,
clientWidth: e.clientWidth
});
};
return e.addEventListener("scroll", i, { passive: !0 }), {
scrollData: t,
percentages: n,
bounds: r,
cleanup: () => {
e.removeEventListener("scroll", i), _(t);
}
};
}
var y = class extends t {
constructor(e, t) {
super(), this.timeouts = [], this.isScrollable = !1, this.audioData = null, this.resizeObserver = null, this.lastContainerWidth = 0, this.isDragging = !1, this.subscriptions = [], this.unsubscribeOnScroll = [], this.dragStream = null, this.scrollStream = null, this.containerInlinePadding = 0, this.subscriptions = [], this.options = e;
let n = this.parentFromOptionsContainer(e.container);
this.parent = n;
let [r, i] = this.initHtml();
n.appendChild(r), this.container = r, this.scrollContainer = i.querySelector(".scroll"), this.wrapper = i.querySelector(".wrapper"), this.canvasWrapper = i.querySelector(".canvases"), this.progressWrapper = i.querySelector(".progress"), this.cursor = i.querySelector(".cursor"), this.calculateInlinePadding(), t && i.appendChild(t), this.initEvents();
}
parentFromOptionsContainer(e) {
let t;
if (typeof e == "string" ? t = document.querySelector(e) : e instanceof HTMLElement && (t = e), !t) throw Error("Container not found");
return t;
}
initEvents() {
this.wrapper.addEventListener("click", (e) => {
let [t, n] = p(this.wrapper.getBoundingClientRect(), e.clientX, e.clientY);
this.emit("click", t, n);
}), this.wrapper.addEventListener("dblclick", (e) => {
let [t, n] = p(this.wrapper.getBoundingClientRect(), e.clientX, e.clientY);
this.emit("dblclick", t, n);
}), this.options.dragToSeek !== !0 && typeof this.options.dragToSeek != "object" || this.initDrag(), this.scrollStream = v(this.scrollContainer);
let e = l(() => {
let { startX: e, endX: t } = this.scrollStream.percentages.value, { left: n, right: r } = this.scrollStream.bounds.value;
this.emit("scroll", e, t, n, r);
}, [this.scrollStream.percentages, this.scrollStream.bounds]);
if (this.subscriptions.push(e), typeof ResizeObserver == "function") {
let e = this.createDelay(100);
this.resizeObserver = new ResizeObserver(() => {
e().then(() => this.onContainerResize()).catch(() => {});
}), this.resizeObserver.observe(this.scrollContainer);
}
}
onContainerResize() {
let e = this.parent.clientWidth;
this.calculateInlinePadding(), e === this.lastContainerWidth && this.options.height !== "auto" || (this.lastContainerWidth = e, this.reRender(), this.emit("resize"));
}
initDrag() {
if (this.dragStream) return;
this.dragStream = function(e, t = {}) {
let { threshold: n = 3, mouseButton: r = 0, touchDelay: i = 100 } = t, a = s(null), o = /* @__PURE__ */ new Map(), c = matchMedia("(pointer: coarse)").matches, l = () => {}, u = (t) => {
if (t.button !== r || (o.set(t.pointerId, t), o.size > 1)) return;
let s = t.clientX, u = t.clientY, d = !1, f = Date.now(), { left: p, top: m } = e.getBoundingClientRect(), h = (e) => {
if (e.defaultPrevented || o.size > 1 || c && Date.now() - f < i) return;
let t = e.clientX, r = e.clientY, l = t - s, h = r - u;
(d || Math.abs(l) > n || Math.abs(h) > n) && (e.preventDefault(), e.stopPropagation(), d ||= (a.set({
type: "start",
x: s - p,
y: u - m
}), !0), a.set({
type: "move",
x: t - p,
y: r - m,
deltaX: l,
deltaY: h
}), s = t, u = r);
}, g = (e) => {
if (o.delete(e.pointerId), d) {
let t = e.clientX, n = e.clientY;
a.set({
type: "end",
x: t - p,
y: n - m
});
}
l();
}, _ = (e) => {
o.delete(e.pointerId), e.relatedTarget && e.relatedTarget !== document.documentElement || g(e);
}, v = (e) => {
d && (e.stopPropagation(), e.preventDefault());
}, y = (e) => {
e.defaultPrevented || o.size > 1 || d && e.preventDefault();
};
document.addEventListener("pointermove", h), document.addEventListener("pointerup", g), document.addEventListener("pointerout", _), document.addEventListener("pointercancel", _), document.addEventListener("touchmove", y, { passive: !1 }), document.addEventListener("click", v, { capture: !0 }), l = () => {
document.removeEventListener("pointermove", h), document.removeEventListener("pointerup", g), document.removeEventListener("pointerout", _), document.removeEventListener("pointercancel", _), document.removeEventListener("touchmove", y), setTimeout(() => {
document.removeEventListener("click", v, { capture: !0 });
}, 10);
};
};
return e.addEventListener("pointerdown", u), {
signal: a,
cleanup: () => {
l(), e.removeEventListener("pointerdown", u), o.clear(), _(a);
}
};
}(this.wrapper);
let e = l(() => {
let e = this.dragStream.signal.value;
if (!e) return;
let t = this.wrapper.getBoundingClientRect().width, n = (r = e.x / t) < 0 ? 0 : r > 1 ? 1 : r;
var r;
e.type === "start" ? (this.isDragging = !0, this.emit("dragstart", n)) : e.type === "move" ? this.emit("drag", n) : e.type === "end" && (this.isDragging = !1, this.emit("dragend", n));
}, [this.dragStream.signal]);
this.subscriptions.push(e);
}
calculateInlinePadding() {
let { paddingLeft: e, paddingRight: t } = getComputedStyle(this.scrollContainer), n = parseFloat(e) + parseFloat(t);
this.containerInlinePadding = Number.isNaN(n) ? 0 : n;
}
initHtml() {
let e = document.createElement("div"), t = e.attachShadow({ mode: "open" }), n = this.options.cspNonce && typeof this.options.cspNonce == "string" ? this.options.cspNonce.replace(/"/g, "") : "";
return t.innerHTML = `
<style${n ? ` nonce="${n}"` : ""}>
:host {
user-select: none;
min-width: 1px;
}
:host audio {
display: block;
width: 100%;
}
:host .scroll {
overflow-x: auto;
overflow-y: hidden;
width: 100%;
position: relative;
}
:host .noScrollbar {
scrollbar-color: transparent;
scrollbar-width: none;
}
:host .noScrollbar::-webkit-scrollbar {
display: none;
-webkit-appearance: none;
}
:host .wrapper {
position: relative;
overflow: visible;
z-index: 2;
}
:host .canvases {
min-height: ${this.getHeight(this.options.height, this.options.splitChannels)}px;
pointer-events: none;
}
:host .canvases > div {
position: relative;
}
:host canvas {
display: block;
position: absolute;
top: 0;
image-rendering: pixelated;
}
:host .progress {
pointer-events: none;
position: absolute;
z-index: 2;
top: 0;
left: 0;
width: 0;
height: 100%;
overflow: hidden;
}
:host .progress > div {
position: relative;
}
:host .cursor {
pointer-events: none;
position: absolute;
z-index: 5;
top: 0;
left: 0;
height: 100%;
border-radius: 2px;
}
</style>
<div class="scroll" part="scroll">
<div class="wrapper" part="wrapper">
<div class="canvases" part="canvases"></div>
<div class="progress" part="progress"></div>
<div class="cursor" part="cursor"></div>
</div>
</div>
`, [e, t];
}
setOptions(e) {
var t;
if (this.options.container !== e.container) {
let t = this.parentFromOptionsContainer(e.container);
t.appendChild(this.container), this.parent = t;
}
e.dragToSeek === !0 || typeof this.options.dragToSeek == "object" ? this.initDrag() : ((t = this.dragStream) == null || t.cleanup(), this.dragStream = null), this.options = e, this.reRender();
}
getWrapper() {
return this.wrapper;
}
getWidth() {
return this.scrollContainer.clientWidth - this.containerInlinePadding;
}
getScroll() {
return this.scrollContainer.scrollLeft;
}
setScroll(e) {
this.scrollContainer.scrollLeft = e;
}
setScrollPercentage(e) {
let { scrollWidth: t } = this.scrollContainer, n = t * e;
this.setScroll(n);
}
destroy() {
var e;
this.subscriptions.forEach((e) => e()), this.container.remove(), this.resizeObserver &&= (this.resizeObserver.disconnect(), null), (e = this.unsubscribeOnScroll) == null || e.forEach((e) => e()), this.unsubscribeOnScroll = [], this.dragStream &&= (this.dragStream.cleanup(), null), this.scrollStream &&= (this.scrollStream.cleanup(), null);
}
createDelay(e = 10) {
let t, n, r = () => {
t &&= (clearTimeout(t), void 0), n &&= (n(), void 0);
};
return this.timeouts.push(r), () => new Promise((i, a) => {
r(), n = a, t = setTimeout(() => {
t = void 0, n = void 0, i();
}, e);
});
}
getHeight(e, t) {
let n = this.audioData?.numberOfChannels || 1;
return function({ optionsHeight: e, optionsSplitChannels: t, parentHeight: n, numberOfChannels: r, defaultHeight: i = 128 }) {
if (e == null) return i;
let a = Number(e);
if (!isNaN(a)) return a;
if (e === "auto") {
let e = n || i;
return t?.every((e) => !e.overlay) ? e / r : e;
}
return i;
}({
optionsHeight: e,
optionsSplitChannels: t,
parentHeight: this.parent.clientHeight,
numberOfChannels: n,
defaultHeight: 128
});
}
convertColorValues(e, t) {
return function(e, t, n) {
if (!Array.isArray(e)) return e || "";
if (e.length === 0) return "#999";
if (e.length < 2) return e[0] || "";
let r = document.createElement("canvas"), i = r.getContext("2d"), a = n ?? r.height * t, o = i.createLinearGradient(0, 0, 0, a || t), s = 1 / (e.length - 1);
return e.forEach((e, t) => {
o.addColorStop(t * s, e);
}), o;
}(e, this.getPixelRatio(), t?.canvas.height);
}
getPixelRatio() {
return e = window.devicePixelRatio, Math.max(1, e || 1);
var e;
}
renderBarWaveform(e, t, n, r) {
let { width: i, height: a } = n.canvas, { halfHeight: o, barWidth: s, barRadius: c, barIndexScale: l, barSpacing: u, barMinHeight: p } = function({ width: e, height: t, length: n, options: r, pixelRatio: i }) {
let a = t / 2, o = r.barWidth ? r.barWidth * i : 1, s = r.barGap ? r.barGap * i : r.barWidth ? o / 2 : 0, c = o + s || 1;
return {
halfHeight: a,
barWidth: o,
barGap: s,
barRadius: r.barRadius || 0,
barMinHeight: r.barMinHeight ? r.barMinHeight * i : 0,
barIndexScale: n > 0 ? e / c / n : 0,
barSpacing: c
};
}({
width: i,
height: a,
length: (e[0] || []).length,
options: t,
pixelRatio: this.getPixelRatio()
}), m = function({ channelData: e, barIndexScale: t, barSpacing: n, barWidth: r, halfHeight: i, vScale: a, canvasHeight: o, barAlign: s, barMinHeight: c }) {
let l = e[0] || [], u = e[1] || l, p = l.length, m = [], h = 0, g = 0, _ = 0;
for (let e = 0; e <= p; e++) {
let p = Math.round(e * t);
if (p > h) {
let { topHeight: e, totalHeight: t } = d({
maxTop: g,
maxBottom: _,
halfHeight: i,
vScale: a,
barMinHeight: c,
barAlign: s
}), l = f({
barAlign: s,
halfHeight: i,
topHeight: e,
totalHeight: t,
canvasHeight: o
});
m.push({
x: h * n,
y: l,
width: r,
height: t
}), h = p, g = 0, _ = 0;
}
let v = Math.abs(l[e] || 0), y = Math.abs(u[e] || 0);
v > g && (g = v), y > _ && (_ = y);
}
return m;
}({
channelData: e,
barIndexScale: l,
barSpacing: u,
barWidth: s,
halfHeight: o,
vScale: r,
canvasHeight: a,
barAlign: t.barAlign,
barMinHeight: p
});
n.beginPath();
for (let e of m) c && "roundRect" in n ? n.roundRect(e.x, e.y, e.width, e.height, c) : n.rect(e.x, e.y, e.width, e.height);
n.fill(), n.closePath();
}
renderLineWaveform(e, t, n, r) {
let { width: i, height: a } = n.canvas, o = function({ channelData: e, width: t, height: n, vScale: r }) {
let i = n / 2, a = e[0] || [];
return [a, e[1] || a].map((e, n) => {
let a = e.length, o = a ? t / a : 0, s = i, c = n === 0 ? -1 : 1, l = [{
x: 0,
y: s
}], u = 0, d = 0;
for (let t = 0; t <= a; t++) {
let n = Math.round(t * o);
if (n > u) {
let e = s + (Math.round(d * i * r) || 1) * c;
l.push({
x: u,
y: e
}), u = n, d = 0;
}
let a = Math.abs(e[t] || 0);
a > d && (d = a);
}
return l.push({
x: u,
y: s
}), l;
});
}({
channelData: e,
width: i,
height: a,
vScale: r
});
n.beginPath();
for (let e of o) if (e.length) {
n.moveTo(e[0].x, e[0].y);
for (let t = 1; t < e.length; t++) {
let r = e[t];
n.lineTo(r.x, r.y);
}
}
n.fill(), n.closePath();
}
renderWaveform(e, t, n) {
if (n.fillStyle = this.convertColorValues(t.waveColor, n), t.renderFunction) return void t.renderFunction(e, n);
let r = function({ channelData: e, barHeight: t, normalize: n, maxPeak: r }) {
let i = t || 1;
if (!n) return i;
let a = e[0];
if (!a || a.length === 0) return i;
let o = r ?? 0;
if (!r) for (let e = 0; e < a.length; e++) {
let t = a[e] ?? 0, n = Math.abs(t);
n > o && (o = n);
}
return o ? i / o : i;
}({
channelData: e,
barHeight: t.barHeight,
normalize: t.normalize,
maxPeak: t.maxPeak
});
m(t) ? this.renderBarWaveform(e, t, n, r) : this.renderLineWaveform(e, t, n, r);
}
renderSingleCanvas(e, t, n, r, i, a, o) {
let s = this.getPixelRatio(), c = document.createElement("canvas");
c.width = Math.round(n * s), c.height = Math.round(r * s), c.style.width = `${n}px`, c.style.height = `${r}px`, c.style.left = `${Math.round(i)}px`, a.appendChild(c);
let l = c.getContext("2d");
if (t.renderFunction ? (l.fillStyle = this.convertColorValues(t.waveColor, l), t.renderFunction(e, l)) : this.renderWaveform(e, t, l), c.width > 0 && c.height > 0) {
let e = c.cloneNode(), n = e.getContext("2d");
n.drawImage(c, 0, 0), n.globalCompositeOperation = "source-in", n.fillStyle = this.convertColorValues(t.progressColor, n), n.fillRect(0, 0, c.width, c.height), o.appendChild(e);
}
}
renderMultiCanvas(e, t, n, r, i, a) {
let o = this.getPixelRatio(), { clientWidth: s } = this.scrollContainer, c = n / o, l = function({ clientWidth: e, totalWidth: t, options: n }) {
return h(Math.min(8e3, e, t), n);
}({
clientWidth: s,
totalWidth: c,
options: t
}), u = {};
if (l === 0) return;
let d = (n) => {
if (n < 0 || n >= f || u[n]) return;
u[n] = !0;
let o = n * l, s = Math.min(c - o, l);
if (s = h(s, t), s <= 0) return;
let d = function({ channelData: e, offset: t, clampedWidth: n, totalWidth: r }) {
return e.map((e) => {
let i = Math.floor(t / r * e.length), a = Math.floor((t + n) / r * e.length);
return e.slice(i, a);
});
}({
channelData: e,
offset: o,
clampedWidth: s,
totalWidth: c
});
this.renderSingleCanvas(d, t, s, r, o, i, a);
}, f = Math.ceil(c / l);
if (!this.isScrollable) {
for (let e = 0; e < f; e++) d(e);
return;
}
if (g({
scrollLeft: this.scrollContainer.scrollLeft,
totalWidth: c,
numCanvases: f
}).forEach((e) => d(e)), f > 1) {
let e = this.on("scroll", () => {
let { scrollLeft: e } = this.scrollContainer;
Object.keys(u).length > 10 && (i.innerHTML = "", a.innerHTML = "", u = {}), g({
scrollLeft: e,
totalWidth: c,
numCanvases: f
}).forEach((e) => d(e));
});
this.unsubscribeOnScroll.push(e);
}
}
renderChannel(e, t, n, r) {
var { overlay: i } = t, a = function(e, t) {
var n = {};
for (var r in e) Object.prototype.hasOwnProperty.call(e, r) && t.indexOf(r) < 0 && (n[r] = e[r]);
if (e != null && typeof Object.getOwnPropertySymbols == "function") {
var i = 0;
for (r = Object.getOwnPropertySymbols(e); i < r.length; i++) t.indexOf(r[i]) < 0 && Object.prototype.propertyIsEnumerable.call(e, r[i]) && (n[r[i]] = e[r[i]]);
}
return n;
}(t, ["overlay"]);
let o = document.createElement("div"), s = this.getHeight(a.height, a.splitChannels);
o.style.height = `${s}px`, i && r > 0 && (o.style.marginTop = `-${s}px`), this.canvasWrapper.style.minHeight = `${s}px`, this.canvasWrapper.appendChild(o);
let c = o.cloneNode();
this.progressWrapper.appendChild(c), this.renderMultiCanvas(e, a, n, s, o, c);
}
render(t) {
return e(this, void 0, void 0, function* () {
this.timeouts.forEach((e) => e()), this.timeouts = [], this.canvasWrapper.innerHTML = "", this.progressWrapper.innerHTML = "", this.options.width != null && (this.scrollContainer.style.width = typeof this.options.width == "number" ? `${this.options.width}px` : this.options.width);
let e = this.getPixelRatio(), n = this.scrollContainer.clientWidth - this.containerInlinePadding, { scrollWidth: r, isScrollable: i, useParentWidth: a, width: o } = function({ duration: e, minPxPerSec: t = 0, parentWidth: n, fillParent: r, pixelRatio: i }) {
let a = Math.ceil(e * t), o = a > n, s = !!(r && !o);
return {
scrollWidth: a,
isScrollable: o,
useParentWidth: s,
width: (s ? n : a) * i
};
}({
duration: t.duration,
minPxPerSec: this.options.minPxPerSec || 0,
parentWidth: n,
fillParent: this.options.fillParent,
pixelRatio: e
});
if (this.isScrollable = i, this.wrapper.style.width = a ? "100%" : `${r}px`, this.scrollContainer.style.overflowX = this.isScrollable ? "auto" : "hidden", this.scrollContainer.classList.toggle("noScrollbar", !!this.options.hideScrollbar), this.cursor.style.backgroundColor = `${this.options.cursorColor || this.options.progressColor}`, this.cursor.style.width = `${this.options.cursorWidth}px`, this.audioData = t, this.emit("render"), this.options.splitChannels) for (let e = 0; e < t.numberOfChannels; e++) {
let n = Object.assign(Object.assign({}, this.options), this.options.splitChannels?.[e]);
this.renderChannel([t.getChannelData(e)], n, o, e);
}
else {
let e = [t.getChannelData(0)];
t.numberOfChannels > 1 && e.push(t.getChannelData(1)), this.renderChannel(e, this.options, o, 0);
}
Promise.resolve().then(() => this.emit("rendered"));
});
}
reRender() {
if (this.unsubscribeOnScroll.forEach((e) => e()), this.unsubscribeOnScroll = [], !this.audioData) return;
let { scrollWidth: e } = this.scrollContainer, { right: t } = this.progressWrapper.getBoundingClientRect();
if (this.render(this.audioData), !this.isScrollable && this.scrollContainer.scrollLeft) this.scrollContainer.scrollLeft = 0;
else if (this.isScrollable && e !== this.scrollContainer.scrollWidth) {
let { right: e } = this.progressWrapper.getBoundingClientRect(), n = function(e) {
let t = 2 * e;
return (t < 0 ? Math.floor(t) : Math.ceil(t)) / 2;
}(e - t);
this.scrollContainer.scrollLeft += n;
}
}
zoom(e) {
this.options.minPxPerSec = e, this.reRender();
}
scrollIntoView(e, t = !1) {
let { scrollLeft: n, scrollWidth: r, clientWidth: i } = this.scrollContainer, a = e * r, o = n, s = n + i, c = i / 2;
if (this.isDragging) a + 30 > s ? this.scrollContainer.scrollLeft += 30 : a - 30 < o && (this.scrollContainer.scrollLeft -= 30);
else {
(a < o || a > s) && (this.scrollContainer.scrollLeft = a - (this.options.autoCenter ? c : 0));
let e = a - n - c;
if (t && this.options.autoCenter && e > 0) {
let t = this.audioData?.duration;
if (t === void 0 || t <= 0) return void (this.scrollContainer.scrollLeft += e);
let n = r / t;
this.scrollContainer.scrollLeft += n <= 600 ? Math.min(e, 10) : e;
}
}
}
renderProgress(e, t) {
if (isNaN(e)) return;
let n = 100 * e;
this.canvasWrapper.style.clipPath = `polygon(${n}% 0%, 100% 0%, 100% 100%, ${n}% 100%)`, this.progressWrapper.style.width = `${n}%`, this.cursor.style.left = `${n}%`, this.cursor.style.transform = this.options.cursorWidth ? `translateX(-${e * this.options.cursorWidth}px)` : "", this.isScrollable && this.options.autoScroll && this.audioData && this.audioData.duration > 0 && this.scrollIntoView(e, t);
}
exportImage(t, n, r) {
return e(this, void 0, void 0, function* () {
let e = this.canvasWrapper.querySelectorAll("canvas");
if (!e.length) throw Error("No waveform data");
if (r === "dataURL") {
let r = Array.from(e).map((e) => e.toDataURL(t, n));
return Promise.resolve(r);
}
return Promise.all(Array.from(e).map((e) => new Promise((r, i) => {
e.toBlob((e) => {
e ? r(e) : i(/* @__PURE__ */ Error("Could not export image"));
}, t, n);
})));
});
}
}, b = class extends t {
constructor() {
super(...arguments), this.animationFrameId = null, this.isRunning = !1;
}
start() {
if (this.isRunning) return;
this.isRunning = !0;
let e = () => {
this.isRunning && (this.emit("tick"), this.animationFrameId = requestAnimationFrame(e));
};
e();
}
stop() {
this.isRunning = !1, this.animationFrameId !== null && (cancelAnimationFrame(this.animationFrameId), this.animationFrameId = null);
}
destroy() {
this.stop();
}
}, x = class extends t {
constructor(e) {
super(), this.bufferNode = null, this.playStartTime = 0, this.playbackPosition = 0, this._muted = !1, this._playbackRate = 1, this._duration = void 0, this.buffer = null, this.currentSrc = "", this.paused = !0, this.crossOrigin = null, this.seeking = !1, this.autoplay = !1, this.addEventListener = this.on, this.removeEventListener = this.un, function() {
let e = globalThis.navigator;
if (e?.audioSession) try {
e.audioSession.type = "playback";
} catch (e) {
console.warn("Setting navigator.audioSession.type failed:", e);
}
}(), this.audioContext = e || new AudioContext(), this.gainNode = this.audioContext.createGain(), this.gainNode.connect(this.audioContext.destination);
}
load() {
return e(this, void 0, void 0, function* () {});
}
get src() {
return this.currentSrc;
}
set src(e) {
if (this.currentSrc = e, this._duration = void 0, !e) return this.buffer = null, void this.emit("emptied");
fetch(e).then((t) => {
if (t.status >= 400) throw Error(`Failed to fetch ${e}: ${t.status} (${t.statusText})`);
return t.arrayBuffer();
}).then((t) => this.currentSrc === e ? this.audioContext.decodeAudioData(t) : null).then((t) => {
this.currentSrc === e && (this.buffer = t, this.emit("loadedmetadata"), this.emit("canplay"), this.autoplay && this.play());
}).catch((e) => {
console.error("WebAudioPlayer load error:", e);
});
}
_play() {
if (!this.paused) return;
this.paused = !1, this.bufferNode && (this.bufferNode.onended = null, this.bufferNode.disconnect()), this.bufferNode = this.audioContext.createBufferSource(), this.buffer && (this.bufferNode.buffer = this.buffer), this.bufferNode.playbackRate.value = this._playbackRate, this.bufferNode.connect(this.gainNode);
let e = this.playbackPosition;
(e >= this.duration || e < 0) && (e = 0, this.playbackPosition = 0), this.bufferNode.start(this.audioContext.currentTime, e), this.playStartTime = this.audioContext.currentTime, this.bufferNode.onended = () => {
!this.paused && this.duration - this.currentTime < .01 && (this.pause(), this.emit("ended"));
};
}
_pause() {
var e;
this.paused = !0, (e = this.bufferNode) == null || e.stop(), this.playbackPosition += (this.audioContext.currentTime - this.playStartTime) * this._playbackRate;
}
play() {
return e(this, void 0, void 0, function* () {
this.paused && (this._play(), this.emit("play"));
});
}
pause() {
this.paused || (this._pause(), this.emit("pause"));
}
stopAt(e) {
let t = e - this.currentTime, n = this.bufferNode;
n?.stop(this.audioContext.currentTime + t), n?.addEventListener("ended", () => {
n === this.bufferNode && (this.bufferNode = null, this.pause());
}, { once: !0 });
}
setSinkId(t) {
return e(this, void 0, void 0, function* () {
return this.audioContext.setSinkId(t);
});
}
get playbackRate() {
return this._playbackRate;
}
set playbackRate(e) {
let t = !this.paused;
t && this._pause(), this._playbackRate = e, t && this._play(), this.bufferNode && (this.bufferNode.playbackRate.value = e);
}
get currentTime() {
return this.paused ? this.playbackPosition : this.playbackPosition + (this.audioContext.currentTime - this.playStartTime) * this._playbackRate;
}
set currentTime(e) {
let t = !this.paused;
t && this._pause(), this.playbackPosition = e, t && this._play(), this.emit("seeking"), this.emit("timeupdate");
}
get duration() {
return this._duration ?? (this.buffer?.duration || 0);
}
set duration(e) {
this._duration = e;
}
get volume() {
return this.gainNode.gain.value;
}
set volume(e) {
this.gainNode.gain.value = e, this.emit("volumechange");
}
get muted() {
return this._muted;
}
set muted(e) {
this._muted !== e && (this._muted = e, this._muted ? this.gainNode.disconnect() : this.gainNode.connect(this.audioContext.destination));
}
canPlayType(e) {
return /^(audio|video)\//.test(e);
}
getGainNode() {
return this.gainNode;
}
getChannelData() {
let e = [];
if (!this.buffer) return e;
let t = this.buffer.numberOfChannels;
for (let n = 0; n < t; n++) e.push(this.buffer.getChannelData(n));
return e;
}
removeAttribute(e) {
switch (e) {
case "src":
this.src = "";
break;
case "playbackRate":
this.playbackRate = 0;
break;
case "currentTime":
this.currentTime = 0;
break;
case "duration":
this.duration = 0;
break;
case "volume":
this.volume = 0;
break;
case "muted": this.muted = !1;
}
}
}, S = {
waveColor: "#999",
progressColor: "#555",
cursorWidth: 1,
minPxPerSec: 0,
fillParent: !0,
interact: !0,
dragToSeek: !1,
autoScroll: !0,
autoCenter: !0,
sampleRate: 8e3
}, C = class t extends u {
static create(e) {
return new t(e);
}
getState() {
return this.wavesurferState;
}
getRenderer() {
return this.renderer;
}
constructor(e) {
let t = e.media || (e.backend === "WebAudio" ? new x() : void 0);
super({
media: t,
mediaControls: e.mediaControls,
autoplay: e.autoplay,
playbackRate: e.audioRate
}), this.plugins = [], this.decodedData = null, this.stopAtPosition = null, this.subscriptions = [], this.mediaSubscriptions = [], this.abortController = null, this.reactiveCleanups = [], this.options = Object.assign({}, S, e);
let { state: n, actions: r } = function(e) {
let t = e?.currentTime ?? s(0), n = e?.duration ?? s(0), r = e?.isPlaying ?? s(!1), i = e?.isSeeking ?? s(!1), a = e?.volume ?? s(1), o = e?.playbackRate ?? s(1), l = s(null), u = s(null), d = s(""), f = s(0), p = s(0), m = c(() => !r.value, [r]), h = c(() => l.value !== null, [l]);
return {
state: {
currentTime: t,
duration: n,
isPlaying: r,
isPaused: m,
isSeeking: i,
volume: a,
playbackRate: o,
audioBuffer: l,
peaks: u,
url: d,
zoom: f,
scrollPosition: p,
canPlay: h,
isReady: c(() => h.value && n.value > 0, [h, n]),
progress: c(() => t.value, [t]),
progressPercent: c(() => n.value > 0 ? t.value / n.value : 0, [t, n])
},
actions: {
setCurrentTime: (e) => {
let r = Math.max(0, Math.min(n.value || Infinity, e));
t.set(r);
},
setDuration: (e) => {
n.set(Math.max(0, e));
},
setPlaying: (e) => {
r.set(e);
},
setSeeking: (e) => {
i.set(e);
},
setVolume: (e) => {
let t = Math.max(0, Math.min(1, e));
a.set(t);
},
setPlaybackRate: (e) => {
let t = Math.max(.1, Math.min(16, e));
o.set(t);
},
setAudioBuffer: (e) => {
l.set(e), e && n.set(e.duration);
},
setPeaks: (e) => {
u.set(e);
},
setUrl: (e) => {
d.set(e);
},
setZoom: (e) => {
f.set(Math.max(0, e));
},
setScrollPosition: (e) => {
p.set(Math.max(0, e));
}
}
};
}({
isPlaying: this.isPlayingSignal,
currentTime: this.currentTimeSignal,
duration: this.durationSignal,
volume: this.volumeSignal,
playbackRate: this.playbackRateSignal,
isSeeking: this.seekingSignal
});
this.wavesurferState = n, this.wavesurferActions = r, this.timer = new b();
let i = t ? void 0 : this.getMediaElement();
this.renderer = new y(this.options, i), this.initPlayerEvents(), this.initRendererEvents(), this.initTimerEvents(), this.initReactiveState(), this.initPlugins();
let a = this.options.url || this.getSrc() || "";
Promise.resolve().then(() => {
this.emit("init");
let { peaks: e, duration: t } = this.options;
(a || e && t) && this.load(a, e, t).catch((e) => {
this.emit("error", e instanceof Error ? e : Error(String(e)));
});
});
}
updateProgress(e = this.getCurrentTime()) {
return this.renderer.renderProgress(e / this.getDuration(), this.isPlaying()), e;
}
initTimerEvents() {
this.subscriptions.push(this.timer.on("tick", () => {
if (!this.isSeeking()) {
let e = this.updateProgress();
this.emit("timeupdate", e), this.emit("audioprocess", e), this.stopAtPosition != null && this.isPlaying() && e >= this.stopAtPosition && this.pause();
}
}));
}
initReactiveState() {
this.reactiveCleanups.push(function(e, t) {
let n = [];
n.push(l(() => {
let n = e.isPlaying.value;
t.emit(n ? "play" : "pause");
}, [e.isPlaying])), n.push(l(() => {
let n = e.currentTime.value;
t.emit("timeupdate", n), e.isPlaying.value && t.emit("audioprocess", n);
}, [e.currentTime, e.isPlaying])), n.push(l(() => {
e.isSeeking.value && t.emit("seeking", e.currentTime.value);
}, [e.isSeeking, e.currentTime]));
let r = !1;
n.push(l(() => {
e.isReady.value && !r && (r = !0, t.emit("ready", e.duration.value));
}, [e.isReady, e.duration]));
let i = !1;
return n.push(l(() => {
let n = e.isPlaying.value, r = e.currentTime.value, a = e.duration.value, o = a > 0 && r >= a;
i && !n && o && t.emit("finish"), i = n && o;
}, [
e.isPlaying,
e.currentTime,
e.duration
])), n.push(l(() => {
let n = e.zoom.value;
n > 0 && t.emit("zoom", n);
}, [e.zoom])), () => {
n.forEach((e) => e());
};
}(this.wavesurferState, { emit: this.emit.bind(this) }));
}
initPlayerEvents() {
this.isPlaying() && (this.emit("play"), this.timer.start()), this.mediaSubscriptions.push(this.onMediaEvent("timeupdate", () => {
let e = this.updateProgress();
this.emit("timeupdate", e);
}), this.onMediaEvent("play", () => {
this.emit("play"), this.timer.start();
}), this.onMediaEvent("pause", () => {
this.emit("pause"), this.timer.stop(), this.stopAtPosition = null;
}), this.onMediaEvent("emptied", () => {
this.timer.stop(), this.stopAtPosition = null;
}), this.onMediaEvent("ended", () => {
this.emit("timeupdate", this.getDuration()), this.emit("finish"), this.stopAtPosition = null;
}), this.onMediaEvent("seeking", () => {
this.emit("seeking", this.getCurrentTime());
}), this.onMediaEvent("error", () => {
this.emit("error", this.getMediaElement().error ?? /* @__PURE__ */ Error("Media error")), this.stopAtPosition = null;
}));
}
initRendererEvents() {
this.subscriptions.push(this.renderer.on("click", (e, t) => {
this.options.interact && (this.seekTo(e), this.emit("interaction", e * this.getDuration()), this.emit("click", e, t));
}), this.renderer.on("dblclick", (e, t) => {
this.emit("dblclick", e, t);
}), this.renderer.on("scroll", (e, t, n, r) => {
let i = this.getDuration();
this.emit("scroll", e * i, t * i, n, r);
}), this.renderer.on("render", () => {
this.emit("redraw");
}), this.renderer.on("rendered", () => {
this.emit("redrawcomplete");
}), this.renderer.on("dragstart", (e) => {
this.emit("dragstart", e);
}), this.renderer.on("dragend", (e) => {
this.emit("dragend", e);
}), this.renderer.on("resize", () => {
this.emit("resize");
}));
{
let e, t = this.renderer.on("drag", (t) => {
if (!this.options.interact) return;
this.renderer.renderProgress(t), clearTimeout(e);
let n = 0, r = this.options.dragToSeek;
this.isPlaying() ? n = 0 : r === !0 ? n = 200 : r && typeof r == "object" && (n = r.debounceTime ?? 200), e = setTimeout(() => {
this.seekTo(t);
}, n), this.emit("interaction", t * this.getDuration()), this.emit("drag", t);
});
this.subscriptions.push(() => {
clearTimeout(e), t();
});
}
}
initPlugins() {
var e;
(e = this.options.plugins) != null && e.length && this.options.plugins.forEach((e) => {
this.registerPlugin(e);
});
}
unsubscribePlayerEvents() {
this.mediaSubscriptions.forEach((e) => e()), this.mediaSubscriptions = [];
}
setOptions(e) {
this.options = Object.assign({}, this.options, e), e.duration && !e.peaks && (this.decodedData = n.createBuffer(this.exportPeaks(), e.duration)), e.peaks && e.duration && (this.decodedData = n.createBuffer(e.peaks, e.duration)), this.renderer.setOptions(this.options), e.audioRate && this.setPlaybackRate(e.audioRate), e.mediaControls != null && (this.getMediaElement().controls = e.mediaControls);
}
registerPlugin(e) {
if (this.plugins.includes(e)) return e;
e._init(this), this.plugins.push(e);
let t = e.once("destroy", () => {
this.plugins = this.plugins.filter((t) => t !== e), this.subscriptions = this.subscriptions.filter((e) => e !== t);
});
return this.subscriptions.push(t), e;
}
unregisterPlugin(e) {
this.plugins = this.plugins.filter((t) => t !== e), e.destroy();
}
getWrapper() {
return this.renderer.getWrapper();
}
getWidth() {
return this.renderer.getWidth();
}
getScroll() {
return this.renderer.getScroll();
}
setScroll(e) {
return this.renderer.setScroll(e);
}
setScrollTime(e) {
let t = e / this.getDuration();
this.renderer.setScrollPercentage(t);
}
getActivePlugins() {
return this.plugins;
}
loadAudio(t, r, i, a) {
return e(this, void 0, void 0, function* () {
var e;
if (this.emit("load", t), !this.options.media && this.isPlaying() && this.pause(), this.decodedData = null, this.stopAtPosition = null, (e = this.abortController) == null || e.abort(), this.abortController = null, !r && !i) {
let e = this.options.fetchParams || {};
window.AbortController && !e.signal && (this.abortController = new AbortController(), e.signal = this.abortController.signal), r = yield o.fetchBlob(t, (e) => this.emit("loading", e), e);
let n = this.options.blobMimeType;
n && (r = new Blob([r], { type: n }));
}
this.setSrc(t, r);
let s = yield new Promise((e) => {
let t = a || this.getDuration();
t ? e(t) : this.mediaSubscriptions.push(this.onMediaEvent("loadedmetadata", () => e(this.getDuration()), { once: !0 }));
});
if (!t && !r) {
let e = this.getMediaElement();
e instanceof x && (e.duration = s);
}
if (i) this.decodedData = n.createBuffer(i, s || 0);
else if (r) {
let e = yield r.arrayBuffer();
this.decodedData = yield n.decode(e, this.options.sampleRate);
}
this.decodedData && (this.emit("decode", this.getDuration()), this.renderer.render(this.decodedData)), this.emit("ready", this.getDuration());
});
}
load(t, n, r) {
return e(this, void 0, void 0, function* () {
try {
return yield this.loadAudio(t, void 0, n, r);
} catch (e) {
throw this.emit("error", e), e;
}
});
}
loadBlob(t, n, r) {
return e(this, void 0, void 0, function* () {
try {
return yield this.loadAudio("", t, n, r);
} catch (e) {
throw this.emit("error", e), e;
}
});
}
zoom(e) {
if (!this.decodedData) throw Error("No audio loaded");
this.renderer.zoom(e), this.emit("zoom", e);
}
getDecodedData() {
return this.decodedData;
}
exportPeaks({ channels: e = 2, maxLength: t = 8e3, precision: n = 1e4 } = {}) {
if (!this.decodedData) throw Error("The audio has not been decoded yet");
let r = Math.min(e, this.decodedData.numberOfChannels), i = [];
for (let e = 0; e < r; e++) {
let r = this.decodedData.getChannelData(e), a = [], o = r.length / t;
for (let e = 0; e < t; e++) {
let t = r.slice(Math.floor(e * o), Math.ceil((e + 1) * o)), i = 0;
for (let e = 0; e < t.length; e++) {
let n = t[e];
Math.abs(n) > Math.abs(i) && (i = n);
}
a.push(Math.round(i * n) / n);
}
i.push(a);
}
return i;
}
getDuration() {
let e = super.getDuration() || 0;
return e !== 0 && e !== Infinity || !this.decodedData || (e = this.decodedData.duration), e;
}
toggleInteraction(e) {
this.options.interact = e;
}
setTime(e) {
this.stopAtPosition = null, super.setTime(e), this.updateProgress(e), this.emit("timeupdate", e);
}
seekTo(e) {
let t = this.getDuration() * e;
this.setTime(t);
}
play(t, n) {
let r = Object.create(null, { play: { get: () => super.play } });
return e(this, void 0, void 0, function* () {
t != null && this.setTime(t);
let e = yield r.play.call(this);
return n != null && (this.media instanceof x ? this.media.stopAt(n) : this.stopAtPosition = n), e;
});
}
playPause() {
return e(this, void 0, void 0, function* () {
return this.isPlaying() ? this.pause() : this.play();
});
}
stop() {
this.pause(), this.setTime(0);
}
skip(e) {
this.setTime(this.getCurrentTime() + e);
}
empty() {
this.load("", [[0]], .001);
}
setMediaElement(e) {
this.unsubscribePlayerEvents(), super.setMediaElement(e), this.initPlayerEvents();
}
exportImage() {
return e(this, arguments, void 0, function* (e = "image/png", t = 1, n = "dataURL") {
return this.renderer.exportImage(e, t, n);
});
}
destroy() {
var e;
this.emit("destroy"), (e = this.abortController) == null || e.abort(), this.plugins.forEach((e) => e.destroy()), this.subscriptions.forEach((e) => e()), this.unsubscribePlayerEvents(), this.reactiveCleanups.forEach((e) => e()), this.reactiveCleanups = [], this.timer.destroy(), this.renderer.destroy(), super.destroy();
}
};
C.BasePlugin = class extends t {
constructor(e) {
super(), this.subscriptions = [], this.isDestroyed = !1, this.options = e;
}
onInit() {}
_init(e) {
this.isDestroyed &&= (this.subscriptions = [], !1), this.wavesurfer = e, this.onInit();
}
destroy() {
this.emit("destroy"), this.subscriptions.forEach((e) => e()), this.subscriptions = [], this.isDestroyed = !0, this.wavesurfer = void 0;
}
}, C.dom = a;
//#endregion
export { C as default };
//# sourceMappingURL=wavesurfer.esm-SpUmkfwo-BUVhZlGD.js.map