wavesurfer.js 7.12.3 → 7.12.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__tests__/minimap.test.js +38 -2
- package/dist/__tests__/renderer.test.js +33 -0
- package/dist/plugins/envelope.cjs +1 -0
- package/dist/plugins/envelope.esm.js +1 -0
- package/dist/plugins/envelope.js +1 -426
- package/dist/plugins/envelope.min.js +1 -0
- package/dist/plugins/hover.cjs +1 -0
- package/dist/plugins/hover.esm.js +1 -0
- package/dist/plugins/hover.js +1 -131
- package/dist/plugins/hover.min.js +1 -0
- package/dist/plugins/minimap.cjs +1 -0
- package/dist/plugins/minimap.d.ts +1 -1
- package/dist/plugins/minimap.esm.js +1 -0
- package/dist/plugins/minimap.js +1 -229
- package/dist/plugins/minimap.min.js +1 -0
- package/dist/plugins/record.cjs +1 -0
- package/dist/plugins/record.esm.js +1 -0
- package/dist/plugins/record.js +1 -319
- package/dist/plugins/record.min.js +1 -0
- package/dist/plugins/regions.cjs +1 -0
- package/dist/plugins/regions.esm.js +1 -0
- package/dist/plugins/regions.js +1 -699
- package/dist/plugins/regions.min.js +1 -0
- package/dist/plugins/spectrogram-windowed.cjs +1 -0
- package/dist/plugins/spectrogram-windowed.esm.js +1 -0
- package/dist/plugins/spectrogram-windowed.js +1 -967
- package/dist/plugins/spectrogram-windowed.min.js +1 -0
- package/dist/plugins/spectrogram.cjs +1 -0
- package/dist/plugins/spectrogram.esm.js +1 -0
- package/dist/plugins/spectrogram.min.js +1 -0
- package/dist/renderer.js +16 -1
- package/dist/wavesurfer.cjs +1 -1
- package/dist/wavesurfer.esm.js +1 -1
- package/dist/wavesurfer.min.js +1 -1
- package/package.json +4 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class t{constructor(){this.listeners={}}on(t,e,s){if(this.listeners[t]||(this.listeners[t]=new Set),null==s?void 0:s.once){const s=(...i)=>{this.un(t,s),e(...i)};return this.listeners[t].add(s),()=>this.un(t,s)}return this.listeners[t].add(e),()=>this.un(t,e)}un(t,e){var s;null===(s=this.listeners[t])||void 0===s||s.delete(e)}once(t,e){return this.on(t,e,{once:!0})}unAll(){this.listeners={}}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach((t=>t(...e)))}}class e extends t{constructor(t){super(),this.subscriptions=[],this.isDestroyed=!1,this.options=t}onInit(){}_init(t){this.isDestroyed&&(this.subscriptions=[],this.isDestroyed=!1),this.wavesurfer=t,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.subscriptions=[],this.isDestroyed=!0,this.wavesurfer=void 0}}function s(t,e){const i=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,n]of Object.entries(e))if("children"===t&&n)for(const[t,e]of Object.entries(n))e instanceof Node?i.appendChild(e):"string"==typeof e?i.appendChild(document.createTextNode(e)):i.appendChild(s(t,e));else"style"===t?Object.assign(i.style,n):"textContent"===t?i.textContent=n:i.setAttribute(t,n.toString());return i}function i(t,e,i){const n=s(t,e||{});return null==i||i.appendChild(n),n}function n(t,e){let s;const i=()=>{s&&(s(),s=void 0),s=t()},n=e.map((t=>t.subscribe(i)));return i(),()=>{s&&(s(),s=void 0),n.forEach((t=>t()))}}function o(t,e){const s=function(t){let e=t;const s=new Set;return{get value(){return e},set(t){Object.is(e,t)||(e=t,s.forEach((t=>t(e))))},update(t){this.set(t(e))},subscribe:t=>(s.add(t),()=>s.delete(t))}}(null),i=t=>{s.set(t)};return t.addEventListener(e,i),s._cleanup=()=>{t.removeEventListener(e,i)},s}const r={lineWidth:1,labelSize:11,labelPreferLeft:!1,formatTimeCallback:t=>`${Math.floor(t/60)}:${`0${Math.floor(t)%60}`.slice(-2)}`};class a extends e{constructor(t){super(t||{}),this.lastPointerPosition=null,this.options=Object.assign({},r,t),this.wrapper=i("div",{part:"hover"}),this.label=i("span",{part:"hover-label"},this.wrapper)}static create(t){return new a(t)}addUnits(t){return`${t}${"number"==typeof t?"px":""}`}onInit(){if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const t=this.wavesurfer.options,e=this.options.lineColor||t.cursorColor||t.progressColor;Object.assign(this.wrapper.style,{position:"absolute",zIndex:10,left:0,top:0,height:"100%",pointerEvents:"none",borderLeft:`${this.addUnits(this.options.lineWidth)} solid ${e}`,opacity:"0",transition:"opacity .1s ease-in"}),Object.assign(this.label.style,{display:"block",backgroundColor:this.options.labelBackground,color:this.options.labelColor,fontSize:`${this.addUnits(this.options.labelSize)}`,transition:"transform .1s ease-in",padding:"2px 3px"});const s=this.wavesurfer.getWrapper();s.appendChild(this.wrapper);const i=this.wavesurfer.getState(),r=o(s,"pointermove"),a=o(s,"pointerleave");this.subscriptions.push(n((()=>{const t=r.value;if(!t||!this.wavesurfer)return;this.lastPointerPosition={clientX:t.clientX,clientY:t.clientY};const e=this.wavesurfer.getWrapper().getBoundingClientRect(),{width:s}=e,n=t.clientX-e.left,o=Math.min(1,Math.max(0,n/s)),a=Math.min(s-this.options.lineWidth-1,n);this.wrapper.style.transform=`translateX(${a}px)`,this.wrapper.style.opacity="1";const l=i.duration.value;this.label.textContent=this.options.formatTimeCallback(l*o);const h=this.label.offsetWidth,p=this.options.labelPreferLeft?a-h>0:a+h>s;this.label.style.transform=p?`translateX(-${h+this.options.lineWidth}px)`:"",this.emit("hover",o)}),[r,i.duration])),this.subscriptions.push(n((()=>{a.value&&(this.wrapper.style.opacity="0",this.lastPointerPosition=null)}),[a]));const l=()=>{if(this.lastPointerPosition&&this.wavesurfer){const t=this.wavesurfer.getWrapper().getBoundingClientRect(),{width:e}=t,s=this.lastPointerPosition.clientX-t.left,n=Math.min(1,Math.max(0,s/e)),o=Math.min(e-this.options.lineWidth-1,s);this.wrapper.style.transform=`translateX(${o}px)`;const r=i.duration.value;this.label.textContent=this.options.formatTimeCallback(r*n);const a=this.label.offsetWidth,l=this.options.labelPreferLeft?o-a>0:o+a>e;this.label.style.transform=l?`translateX(-${a+this.options.lineWidth}px)`:""}};this.subscriptions.push(this.wavesurfer.on("zoom",l)),this.subscriptions.push(this.wavesurfer.on("scroll",l))}destroy(){super.destroy(),this.wrapper.remove()}}export{a as default};
|
package/dist/plugins/hover.js
CHANGED
|
@@ -1,131 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* The Hover plugin follows the mouse and shows a timestamp
|
|
3
|
-
*/
|
|
4
|
-
import BasePlugin from '../base-plugin.js';
|
|
5
|
-
import createElement from '../dom.js';
|
|
6
|
-
import { fromEvent } from '../reactive/event-streams.js';
|
|
7
|
-
import { effect } from '../reactive/store.js';
|
|
8
|
-
const defaultOptions = {
|
|
9
|
-
lineWidth: 1,
|
|
10
|
-
labelSize: 11,
|
|
11
|
-
labelPreferLeft: false,
|
|
12
|
-
formatTimeCallback(seconds) {
|
|
13
|
-
const minutes = Math.floor(seconds / 60);
|
|
14
|
-
const secondsRemainder = Math.floor(seconds) % 60;
|
|
15
|
-
const paddedSeconds = `0${secondsRemainder}`.slice(-2);
|
|
16
|
-
return `${minutes}:${paddedSeconds}`;
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
class HoverPlugin extends BasePlugin {
|
|
20
|
-
constructor(options) {
|
|
21
|
-
super(options || {});
|
|
22
|
-
this.lastPointerPosition = null;
|
|
23
|
-
this.options = Object.assign({}, defaultOptions, options);
|
|
24
|
-
// Create the plugin elements
|
|
25
|
-
this.wrapper = createElement('div', { part: 'hover' });
|
|
26
|
-
this.label = createElement('span', { part: 'hover-label' }, this.wrapper);
|
|
27
|
-
}
|
|
28
|
-
static create(options) {
|
|
29
|
-
return new HoverPlugin(options);
|
|
30
|
-
}
|
|
31
|
-
addUnits(value) {
|
|
32
|
-
const units = typeof value === 'number' ? 'px' : '';
|
|
33
|
-
return `${value}${units}`;
|
|
34
|
-
}
|
|
35
|
-
/** Called by wavesurfer, don't call manually */
|
|
36
|
-
onInit() {
|
|
37
|
-
if (!this.wavesurfer) {
|
|
38
|
-
throw Error('WaveSurfer is not initialized');
|
|
39
|
-
}
|
|
40
|
-
const wsOptions = this.wavesurfer.options;
|
|
41
|
-
const lineColor = this.options.lineColor || wsOptions.cursorColor || wsOptions.progressColor;
|
|
42
|
-
// Vertical line
|
|
43
|
-
Object.assign(this.wrapper.style, {
|
|
44
|
-
position: 'absolute',
|
|
45
|
-
zIndex: 10,
|
|
46
|
-
left: 0,
|
|
47
|
-
top: 0,
|
|
48
|
-
height: '100%',
|
|
49
|
-
pointerEvents: 'none',
|
|
50
|
-
borderLeft: `${this.addUnits(this.options.lineWidth)} solid ${lineColor}`,
|
|
51
|
-
opacity: '0',
|
|
52
|
-
transition: 'opacity .1s ease-in',
|
|
53
|
-
});
|
|
54
|
-
// Timestamp label
|
|
55
|
-
Object.assign(this.label.style, {
|
|
56
|
-
display: 'block',
|
|
57
|
-
backgroundColor: this.options.labelBackground,
|
|
58
|
-
color: this.options.labelColor,
|
|
59
|
-
fontSize: `${this.addUnits(this.options.labelSize)}`,
|
|
60
|
-
transition: 'transform .1s ease-in',
|
|
61
|
-
padding: '2px 3px',
|
|
62
|
-
});
|
|
63
|
-
// Append the wrapper
|
|
64
|
-
const container = this.wavesurfer.getWrapper();
|
|
65
|
-
container.appendChild(this.wrapper);
|
|
66
|
-
// Get reactive state
|
|
67
|
-
const state = this.wavesurfer.getState();
|
|
68
|
-
// Create event streams for pointer events
|
|
69
|
-
const pointerMove = fromEvent(container, 'pointermove');
|
|
70
|
-
const pointerLeave = fromEvent(container, 'pointerleave');
|
|
71
|
-
// React to pointer movement
|
|
72
|
-
this.subscriptions.push(effect(() => {
|
|
73
|
-
const e = pointerMove.value;
|
|
74
|
-
if (!e || !this.wavesurfer)
|
|
75
|
-
return;
|
|
76
|
-
// Store only the position data needed for zoom/scroll updates
|
|
77
|
-
this.lastPointerPosition = { clientX: e.clientX, clientY: e.clientY };
|
|
78
|
-
// Position
|
|
79
|
-
const bbox = this.wavesurfer.getWrapper().getBoundingClientRect();
|
|
80
|
-
const { width } = bbox;
|
|
81
|
-
const offsetX = e.clientX - bbox.left;
|
|
82
|
-
const relX = Math.min(1, Math.max(0, offsetX / width));
|
|
83
|
-
const posX = Math.min(width - this.options.lineWidth - 1, offsetX);
|
|
84
|
-
this.wrapper.style.transform = `translateX(${posX}px)`;
|
|
85
|
-
this.wrapper.style.opacity = '1';
|
|
86
|
-
// Timestamp
|
|
87
|
-
const duration = state.duration.value;
|
|
88
|
-
this.label.textContent = this.options.formatTimeCallback(duration * relX);
|
|
89
|
-
const labelWidth = this.label.offsetWidth;
|
|
90
|
-
const transformCondition = this.options.labelPreferLeft ? posX - labelWidth > 0 : posX + labelWidth > width;
|
|
91
|
-
this.label.style.transform = transformCondition ? `translateX(-${labelWidth + this.options.lineWidth}px)` : '';
|
|
92
|
-
// Emit a hover event with the relative X position
|
|
93
|
-
this.emit('hover', relX);
|
|
94
|
-
}, [pointerMove, state.duration]));
|
|
95
|
-
// React to pointer leave
|
|
96
|
-
this.subscriptions.push(effect(() => {
|
|
97
|
-
const e = pointerLeave.value;
|
|
98
|
-
if (!e)
|
|
99
|
-
return;
|
|
100
|
-
this.wrapper.style.opacity = '0';
|
|
101
|
-
this.lastPointerPosition = null;
|
|
102
|
-
}, [pointerLeave]));
|
|
103
|
-
// When zoom or scroll happens, re-run the pointer move logic with the last known mouse position
|
|
104
|
-
const onUpdate = () => {
|
|
105
|
-
if (this.lastPointerPosition && this.wavesurfer) {
|
|
106
|
-
// Position
|
|
107
|
-
const bbox = this.wavesurfer.getWrapper().getBoundingClientRect();
|
|
108
|
-
const { width } = bbox;
|
|
109
|
-
const offsetX = this.lastPointerPosition.clientX - bbox.left;
|
|
110
|
-
const relX = Math.min(1, Math.max(0, offsetX / width));
|
|
111
|
-
const posX = Math.min(width - this.options.lineWidth - 1, offsetX);
|
|
112
|
-
this.wrapper.style.transform = `translateX(${posX}px)`;
|
|
113
|
-
// Timestamp
|
|
114
|
-
const duration = state.duration.value;
|
|
115
|
-
this.label.textContent = this.options.formatTimeCallback(duration * relX);
|
|
116
|
-
const labelWidth = this.label.offsetWidth;
|
|
117
|
-
const transformCondition = this.options.labelPreferLeft ? posX - labelWidth > 0 : posX + labelWidth > width;
|
|
118
|
-
this.label.style.transform = transformCondition ? `translateX(-${labelWidth + this.options.lineWidth}px)` : '';
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
// Subscribe to zoom and scroll events
|
|
122
|
-
this.subscriptions.push(this.wavesurfer.on('zoom', onUpdate));
|
|
123
|
-
this.subscriptions.push(this.wavesurfer.on('scroll', onUpdate));
|
|
124
|
-
}
|
|
125
|
-
/** Unmount */
|
|
126
|
-
destroy() {
|
|
127
|
-
super.destroy();
|
|
128
|
-
this.wrapper.remove();
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
export default HoverPlugin;
|
|
1
|
+
class t{constructor(){this.listeners={}}on(t,e,s){if(this.listeners[t]||(this.listeners[t]=new Set),null==s?void 0:s.once){const s=(...i)=>{this.un(t,s),e(...i)};return this.listeners[t].add(s),()=>this.un(t,s)}return this.listeners[t].add(e),()=>this.un(t,e)}un(t,e){var s;null===(s=this.listeners[t])||void 0===s||s.delete(e)}once(t,e){return this.on(t,e,{once:!0})}unAll(){this.listeners={}}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach((t=>t(...e)))}}class e extends t{constructor(t){super(),this.subscriptions=[],this.isDestroyed=!1,this.options=t}onInit(){}_init(t){this.isDestroyed&&(this.subscriptions=[],this.isDestroyed=!1),this.wavesurfer=t,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.subscriptions=[],this.isDestroyed=!0,this.wavesurfer=void 0}}function s(t,e){const i=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,n]of Object.entries(e))if("children"===t&&n)for(const[t,e]of Object.entries(n))e instanceof Node?i.appendChild(e):"string"==typeof e?i.appendChild(document.createTextNode(e)):i.appendChild(s(t,e));else"style"===t?Object.assign(i.style,n):"textContent"===t?i.textContent=n:i.setAttribute(t,n.toString());return i}function i(t,e,i){const n=s(t,e||{});return null==i||i.appendChild(n),n}function n(t,e){let s;const i=()=>{s&&(s(),s=void 0),s=t()},n=e.map((t=>t.subscribe(i)));return i(),()=>{s&&(s(),s=void 0),n.forEach((t=>t()))}}function o(t,e){const s=function(t){let e=t;const s=new Set;return{get value(){return e},set(t){Object.is(e,t)||(e=t,s.forEach((t=>t(e))))},update(t){this.set(t(e))},subscribe:t=>(s.add(t),()=>s.delete(t))}}(null),i=t=>{s.set(t)};return t.addEventListener(e,i),s._cleanup=()=>{t.removeEventListener(e,i)},s}const r={lineWidth:1,labelSize:11,labelPreferLeft:!1,formatTimeCallback:t=>`${Math.floor(t/60)}:${`0${Math.floor(t)%60}`.slice(-2)}`};class a extends e{constructor(t){super(t||{}),this.lastPointerPosition=null,this.options=Object.assign({},r,t),this.wrapper=i("div",{part:"hover"}),this.label=i("span",{part:"hover-label"},this.wrapper)}static create(t){return new a(t)}addUnits(t){return`${t}${"number"==typeof t?"px":""}`}onInit(){if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const t=this.wavesurfer.options,e=this.options.lineColor||t.cursorColor||t.progressColor;Object.assign(this.wrapper.style,{position:"absolute",zIndex:10,left:0,top:0,height:"100%",pointerEvents:"none",borderLeft:`${this.addUnits(this.options.lineWidth)} solid ${e}`,opacity:"0",transition:"opacity .1s ease-in"}),Object.assign(this.label.style,{display:"block",backgroundColor:this.options.labelBackground,color:this.options.labelColor,fontSize:`${this.addUnits(this.options.labelSize)}`,transition:"transform .1s ease-in",padding:"2px 3px"});const s=this.wavesurfer.getWrapper();s.appendChild(this.wrapper);const i=this.wavesurfer.getState(),r=o(s,"pointermove"),a=o(s,"pointerleave");this.subscriptions.push(n((()=>{const t=r.value;if(!t||!this.wavesurfer)return;this.lastPointerPosition={clientX:t.clientX,clientY:t.clientY};const e=this.wavesurfer.getWrapper().getBoundingClientRect(),{width:s}=e,n=t.clientX-e.left,o=Math.min(1,Math.max(0,n/s)),a=Math.min(s-this.options.lineWidth-1,n);this.wrapper.style.transform=`translateX(${a}px)`,this.wrapper.style.opacity="1";const l=i.duration.value;this.label.textContent=this.options.formatTimeCallback(l*o);const h=this.label.offsetWidth,p=this.options.labelPreferLeft?a-h>0:a+h>s;this.label.style.transform=p?`translateX(-${h+this.options.lineWidth}px)`:"",this.emit("hover",o)}),[r,i.duration])),this.subscriptions.push(n((()=>{a.value&&(this.wrapper.style.opacity="0",this.lastPointerPosition=null)}),[a]));const l=()=>{if(this.lastPointerPosition&&this.wavesurfer){const t=this.wavesurfer.getWrapper().getBoundingClientRect(),{width:e}=t,s=this.lastPointerPosition.clientX-t.left,n=Math.min(1,Math.max(0,s/e)),o=Math.min(e-this.options.lineWidth-1,s);this.wrapper.style.transform=`translateX(${o}px)`;const r=i.duration.value;this.label.textContent=this.options.formatTimeCallback(r*n);const a=this.label.offsetWidth,l=this.options.labelPreferLeft?o-a>0:o+a>e;this.label.style.transform=l?`translateX(-${a+this.options.lineWidth}px)`:""}};this.subscriptions.push(this.wavesurfer.on("zoom",l)),this.subscriptions.push(this.wavesurfer.on("scroll",l))}destroy(){super.destroy(),this.wrapper.remove()}}export{a as default};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):((t="undefined"!=typeof globalThis?globalThis:t||self).WaveSurfer=t.WaveSurfer||{},t.WaveSurfer.Hover=e())}(this,(function(){"use strict";class t{constructor(){this.listeners={}}on(t,e,s){if(this.listeners[t]||(this.listeners[t]=new Set),null==s?void 0:s.once){const s=(...i)=>{this.un(t,s),e(...i)};return this.listeners[t].add(s),()=>this.un(t,s)}return this.listeners[t].add(e),()=>this.un(t,e)}un(t,e){var s;null===(s=this.listeners[t])||void 0===s||s.delete(e)}once(t,e){return this.on(t,e,{once:!0})}unAll(){this.listeners={}}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach((t=>t(...e)))}}class e extends t{constructor(t){super(),this.subscriptions=[],this.isDestroyed=!1,this.options=t}onInit(){}_init(t){this.isDestroyed&&(this.subscriptions=[],this.isDestroyed=!1),this.wavesurfer=t,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.subscriptions=[],this.isDestroyed=!0,this.wavesurfer=void 0}}function s(t,e){const i=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,n]of Object.entries(e))if("children"===t&&n)for(const[t,e]of Object.entries(n))e instanceof Node?i.appendChild(e):"string"==typeof e?i.appendChild(document.createTextNode(e)):i.appendChild(s(t,e));else"style"===t?Object.assign(i.style,n):"textContent"===t?i.textContent=n:i.setAttribute(t,n.toString());return i}function i(t,e,i){const n=s(t,e||{});return null==i||i.appendChild(n),n}function n(t,e){let s;const i=()=>{s&&(s(),s=void 0),s=t()},n=e.map((t=>t.subscribe(i)));return i(),()=>{s&&(s(),s=void 0),n.forEach((t=>t()))}}function o(t,e){const s=function(t){let e=t;const s=new Set;return{get value(){return e},set(t){Object.is(e,t)||(e=t,s.forEach((t=>t(e))))},update(t){this.set(t(e))},subscribe:t=>(s.add(t),()=>s.delete(t))}}(null),i=t=>{s.set(t)};return t.addEventListener(e,i),s._cleanup=()=>{t.removeEventListener(e,i)},s}const r={lineWidth:1,labelSize:11,labelPreferLeft:!1,formatTimeCallback:t=>`${Math.floor(t/60)}:${`0${Math.floor(t)%60}`.slice(-2)}`};class l extends e{constructor(t){super(t||{}),this.lastPointerPosition=null,this.options=Object.assign({},r,t),this.wrapper=i("div",{part:"hover"}),this.label=i("span",{part:"hover-label"},this.wrapper)}static create(t){return new l(t)}addUnits(t){return`${t}${"number"==typeof t?"px":""}`}onInit(){if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const t=this.wavesurfer.options,e=this.options.lineColor||t.cursorColor||t.progressColor;Object.assign(this.wrapper.style,{position:"absolute",zIndex:10,left:0,top:0,height:"100%",pointerEvents:"none",borderLeft:`${this.addUnits(this.options.lineWidth)} solid ${e}`,opacity:"0",transition:"opacity .1s ease-in"}),Object.assign(this.label.style,{display:"block",backgroundColor:this.options.labelBackground,color:this.options.labelColor,fontSize:`${this.addUnits(this.options.labelSize)}`,transition:"transform .1s ease-in",padding:"2px 3px"});const s=this.wavesurfer.getWrapper();s.appendChild(this.wrapper);const i=this.wavesurfer.getState(),r=o(s,"pointermove"),l=o(s,"pointerleave");this.subscriptions.push(n((()=>{const t=r.value;if(!t||!this.wavesurfer)return;this.lastPointerPosition={clientX:t.clientX,clientY:t.clientY};const e=this.wavesurfer.getWrapper().getBoundingClientRect(),{width:s}=e,n=t.clientX-e.left,o=Math.min(1,Math.max(0,n/s)),l=Math.min(s-this.options.lineWidth-1,n);this.wrapper.style.transform=`translateX(${l}px)`,this.wrapper.style.opacity="1";const a=i.duration.value;this.label.textContent=this.options.formatTimeCallback(a*o);const h=this.label.offsetWidth,p=this.options.labelPreferLeft?l-h>0:l+h>s;this.label.style.transform=p?`translateX(-${h+this.options.lineWidth}px)`:"",this.emit("hover",o)}),[r,i.duration])),this.subscriptions.push(n((()=>{l.value&&(this.wrapper.style.opacity="0",this.lastPointerPosition=null)}),[l]));const a=()=>{if(this.lastPointerPosition&&this.wavesurfer){const t=this.wavesurfer.getWrapper().getBoundingClientRect(),{width:e}=t,s=this.lastPointerPosition.clientX-t.left,n=Math.min(1,Math.max(0,s/e)),o=Math.min(e-this.options.lineWidth-1,s);this.wrapper.style.transform=`translateX(${o}px)`;const r=i.duration.value;this.label.textContent=this.options.formatTimeCallback(r*n);const l=this.label.offsetWidth,a=this.options.labelPreferLeft?o-l>0:o+l>e;this.label.style.transform=a?`translateX(-${l+this.options.lineWidth}px)`:""}};this.subscriptions.push(this.wavesurfer.on("zoom",a)),this.subscriptions.push(this.wavesurfer.on("scroll",a))}destroy(){super.destroy(),this.wrapper.remove()}}return l}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";class t{constructor(){this.listeners={}}on(t,e,i){if(this.listeners[t]||(this.listeners[t]=new Set),null==i?void 0:i.once){const i=(...s)=>{this.un(t,i),e(...s)};return this.listeners[t].add(i),()=>this.un(t,i)}return this.listeners[t].add(e),()=>this.un(t,e)}un(t,e){var i;null===(i=this.listeners[t])||void 0===i||i.delete(e)}once(t,e){return this.on(t,e,{once:!0})}unAll(){this.listeners={}}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach((t=>t(...e)))}}class e extends t{constructor(t){super(),this.subscriptions=[],this.isDestroyed=!1,this.options=t}onInit(){}_init(t){this.isDestroyed&&(this.subscriptions=[],this.isDestroyed=!1),this.wavesurfer=t,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.subscriptions=[],this.isDestroyed=!0,this.wavesurfer=void 0}}function i(t,e,i,s){return new(i||(i=Promise))((function(n,r){function o(t){try{h(s.next(t))}catch(t){r(t)}}function a(t){try{h(s.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}h((s=s.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const s={decode:function(t,e){return i(this,void 0,void 0,(function*(){const i=new AudioContext({sampleRate:e});try{return yield i.decodeAudioData(t)}finally{i.close()}}))},createBuffer:function(t,e){if(!t||0===t.length)throw new Error("channelData must be a non-empty array");if(e<=0)throw new Error("duration must be greater than 0");if("number"==typeof t[0]&&(t=[t]),!t[0]||0===t[0].length)throw new Error("channelData must contain non-empty channel arrays");!function(t){const e=t[0];if(e.some((t=>t>1||t<-1))){const i=e.length;let s=0;for(let t=0;t<i;t++){const i=Math.abs(e[t]);i>s&&(s=i)}for(const e of t)for(let t=0;t<i;t++)e[t]/=s}}(t);const i=t.map((t=>t instanceof Float32Array?t:Float32Array.from(t)));return{duration:e,length:i[0].length,sampleRate:i[0].length/e,numberOfChannels:i.length,getChannelData:t=>{const e=i[t];if(!e)throw new Error(`Channel ${t} not found`);return e},copyFromChannel:AudioBuffer.prototype.copyFromChannel,copyToChannel:AudioBuffer.prototype.copyToChannel}}};function n(t,e){const i=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,s]of Object.entries(e))if("children"===t&&s)for(const[t,e]of Object.entries(s))e instanceof Node?i.appendChild(e):"string"==typeof e?i.appendChild(document.createTextNode(e)):i.appendChild(n(t,e));else"style"===t?Object.assign(i.style,s):"textContent"===t?i.textContent=s:i.setAttribute(t,s.toString());return i}function r(t,e,i){const s=n(t,e||{});return null==i||i.appendChild(s),s}var o=Object.freeze({__proto__:null,createElement:r,default:r});const a={fetchBlob:function(t,e,s){return i(this,void 0,void 0,(function*(){const n=yield fetch(t,s);if(n.status>=400)throw new Error(`Failed to fetch ${t}: ${n.status} (${n.statusText})`);return function(t,e){i(this,void 0,void 0,(function*(){if(!t.body||!t.headers)return;const i=t.body.getReader(),s=Number(t.headers.get("Content-Length"))||0;let n=0;const r=t=>{n+=(null==t?void 0:t.length)||0;const i=Math.round(n/s*100);e(i)};try{for(;;){const t=yield i.read();if(t.done)break;r(t.value)}}catch(t){console.warn("Progress tracking error:",t)}}))}(n.clone(),e),n.blob()}))}};function h(t){let e=t;const i=new Set;return{get value(){return e},set(t){Object.is(e,t)||(e=t,i.forEach((t=>t(e))))},update(t){this.set(t(e))},subscribe:t=>(i.add(t),()=>i.delete(t))}}function l(t,e){const i=h(t());return e.forEach((e=>e.subscribe((()=>{const e=t();Object.is(i.value,e)||i.set(e)})))),{get value(){return i.value},subscribe:t=>i.subscribe(t)}}function u(t,e){let i;const s=()=>{i&&(i(),i=void 0),i=t()},n=e.map((t=>t.subscribe(s)));return s(),()=>{i&&(i(),i=void 0),n.forEach((t=>t()))}}class c 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(t){super(),this.isExternalMedia=!1,this.reactiveMediaEventCleanups=[],t.media?(this.media=t.media,this.isExternalMedia=!0):this.media=document.createElement("audio"),this._isPlaying=h(!1),this._currentTime=h(0),this._duration=h(0),this._volume=h(this.media.volume),this._muted=h(this.media.muted),this._playbackRate=h(this.media.playbackRate||1),this._seeking=h(!1),this.setupReactiveMediaEvents(),t.mediaControls&&(this.media.controls=!0),t.autoplay&&(this.media.autoplay=!0),null!=t.playbackRate&&this.onMediaEvent("canplay",(()=>{null!=t.playbackRate&&(this.media.playbackRate=t.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(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e,i)}getSrc(){return this.media.currentSrc||this.media.src||""}revokeSrc(){const t=this.getSrc();t.startsWith("blob:")&&URL.revokeObjectURL(t)}canPlayType(t){return""!==this.media.canPlayType(t)}setSrc(t,e){const i=this.getSrc();if(t&&i===t)return;this.revokeSrc();const s=e instanceof Blob&&(this.canPlayType(e.type)||!t)?URL.createObjectURL(e):t;if(i&&this.media.removeAttribute("src"),s||t)try{this.media.src=s}catch(e){this.media.src=t}}destroy(){this.reactiveMediaEventCleanups.forEach((t=>t())),this.reactiveMediaEventCleanups=[],this.isExternalMedia||(this.media.pause(),this.revokeSrc(),this.media.removeAttribute("src"),this.media.load(),this.media.remove())}setMediaElement(t){this.reactiveMediaEventCleanups.forEach((t=>t())),this.reactiveMediaEventCleanups=[],this.media=t,this.setupReactiveMediaEvents()}play(){return i(this,void 0,void 0,(function*(){try{return yield this.media.play()}catch(t){if(t instanceof DOMException&&"AbortError"===t.name)return;throw t}}))}pause(){this.media.pause()}isPlaying(){return!this.media.paused&&!this.media.ended}setTime(t){this.media.currentTime=Math.max(0,Math.min(t,this.getDuration()))}getDuration(){return this.media.duration}getCurrentTime(){return this.media.currentTime}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}isSeeking(){return this.media.seeking}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}getMediaElement(){return this.media}setSinkId(t){return this.media.setSinkId(t)}}function d({maxTop:t,maxBottom:e,halfHeight:i,vScale:s,barMinHeight:n=0,barAlign:r}){let o=Math.round(t*i*s);let a=o+Math.round(e*i*s)||1;return a<n&&(a=n,r||(o=a/2)),{topHeight:o,totalHeight:a}}function p({barAlign:t,halfHeight:e,topHeight:i,totalHeight:s,canvasHeight:n}){return"top"===t?0:"bottom"===t?n-s:e-i}function m(t,e,i){const s=e-t.left,n=i-t.top;return[s/t.width,n/t.height]}function v(t){return Boolean(t.barWidth||t.barGap||t.barAlign)}function g(t,e){if(!v(e))return t;const i=e.barWidth||.5,s=i+(e.barGap||i/2);return 0===s?t:Math.floor(t/s)*s}function f({scrollLeft:t,totalWidth:e,numCanvases:i}){if(0===e)return[0];const s=t/e,n=Math.floor(s*i);return[n-1,n,n+1]}function b(t){const e=t._cleanup;"function"==typeof e&&e()}function y(t){const e=h({scrollLeft:t.scrollLeft,scrollWidth:t.scrollWidth,clientWidth:t.clientWidth}),i=l((()=>function(t){const{scrollLeft:e,scrollWidth:i,clientWidth:s}=t;if(0===i)return{startX:0,endX:1};const n=e/i,r=(e+s)/i;return{startX:Math.max(0,Math.min(1,n)),endX:Math.max(0,Math.min(1,r))}}(e.value)),[e]),s=l((()=>function(t){return{left:t.scrollLeft,right:t.scrollLeft+t.clientWidth}}(e.value)),[e]),n=()=>{e.set({scrollLeft:t.scrollLeft,scrollWidth:t.scrollWidth,clientWidth:t.clientWidth})};t.addEventListener("scroll",n,{passive:!0});return{scrollData:e,percentages:i,bounds:s,cleanup:()=>{t.removeEventListener("scroll",n),b(e)}}}class S extends t{constructor(t,e){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.subscriptions=[],this.options=t;const i=this.parentFromOptionsContainer(t.container);this.parent=i;const[s,n]=this.initHtml();i.appendChild(s),this.container=s,this.scrollContainer=n.querySelector(".scroll"),this.wrapper=n.querySelector(".wrapper"),this.canvasWrapper=n.querySelector(".canvases"),this.progressWrapper=n.querySelector(".progress"),this.cursor=n.querySelector(".cursor"),e&&n.appendChild(e),this.initEvents()}parentFromOptionsContainer(t){let e;if("string"==typeof t?e=document.querySelector(t):t instanceof HTMLElement&&(e=t),!e)throw new Error("Container not found");return e}initEvents(){this.wrapper.addEventListener("click",(t=>{const e=this.wrapper.getBoundingClientRect(),[i,s]=m(e,t.clientX,t.clientY);this.emit("click",i,s)})),this.wrapper.addEventListener("dblclick",(t=>{const e=this.wrapper.getBoundingClientRect(),[i,s]=m(e,t.clientX,t.clientY);this.emit("dblclick",i,s)})),!0!==this.options.dragToSeek&&"object"!=typeof this.options.dragToSeek||this.initDrag(),this.scrollStream=y(this.scrollContainer);const t=u((()=>{const{startX:t,endX:e}=this.scrollStream.percentages.value,{left:i,right:s}=this.scrollStream.bounds.value;this.emit("scroll",t,e,i,s)}),[this.scrollStream.percentages,this.scrollStream.bounds]);if(this.subscriptions.push(t),"function"==typeof ResizeObserver){const t=this.createDelay(100);this.resizeObserver=new ResizeObserver((()=>{t().then((()=>this.onContainerResize())).catch((()=>{}))})),this.resizeObserver.observe(this.scrollContainer)}}onContainerResize(){const t=this.parent.clientWidth;t===this.lastContainerWidth&&"auto"!==this.options.height||(this.lastContainerWidth=t,this.reRender(),this.emit("resize"))}initDrag(){if(this.dragStream)return;this.dragStream=function(t,e={}){const{threshold:i=3,mouseButton:s=0,touchDelay:n=100}=e,r=h(null),o=new Map,a=matchMedia("(pointer: coarse)").matches;let l=()=>{};const u=e=>{if(e.button!==s)return;if(o.set(e.pointerId,e),o.size>1)return;let h=e.clientX,u=e.clientY,c=!1;const d=Date.now(),p=t.getBoundingClientRect(),{left:m,top:v}=p,g=t=>{if(t.defaultPrevented||o.size>1)return;if(a&&Date.now()-d<n)return;const e=t.clientX,s=t.clientY,l=e-h,p=s-u;(c||Math.abs(l)>i||Math.abs(p)>i)&&(t.preventDefault(),t.stopPropagation(),c||(r.set({type:"start",x:h-m,y:u-v}),c=!0),r.set({type:"move",x:e-m,y:s-v,deltaX:l,deltaY:p}),h=e,u=s)},f=t=>{if(o.delete(t.pointerId),c){const e=t.clientX,i=t.clientY;r.set({type:"end",x:e-m,y:i-v})}l()},b=t=>{o.delete(t.pointerId),t.relatedTarget&&t.relatedTarget!==document.documentElement||f(t)},y=t=>{c&&(t.stopPropagation(),t.preventDefault())},S=t=>{t.defaultPrevented||o.size>1||c&&t.preventDefault()};document.addEventListener("pointermove",g),document.addEventListener("pointerup",f),document.addEventListener("pointerout",b),document.addEventListener("pointercancel",b),document.addEventListener("touchmove",S,{passive:!1}),document.addEventListener("click",y,{capture:!0}),l=()=>{document.removeEventListener("pointermove",g),document.removeEventListener("pointerup",f),document.removeEventListener("pointerout",b),document.removeEventListener("pointercancel",b),document.removeEventListener("touchmove",S),setTimeout((()=>{document.removeEventListener("click",y,{capture:!0})}),10)}};return t.addEventListener("pointerdown",u),{signal:r,cleanup:()=>{l(),t.removeEventListener("pointerdown",u),o.clear(),b(r)}}}(this.wrapper);const t=u((()=>{const t=this.dragStream.signal.value;if(!t)return;const e=this.wrapper.getBoundingClientRect().width,i=(s=t.x/e)<0?0:s>1?1:s;var s;"start"===t.type?(this.isDragging=!0,this.emit("dragstart",i)):"move"===t.type?this.emit("drag",i):"end"===t.type&&(this.isDragging=!1,this.emit("dragend",i))}),[this.dragStream.signal]);this.subscriptions.push(t)}initHtml(){const t=document.createElement("div"),e=t.attachShadow({mode:"open"}),i=this.options.cspNonce&&"string"==typeof this.options.cspNonce?this.options.cspNonce.replace(/"/g,""):"";return e.innerHTML=`\n <style${i?` nonce="${i}"`:""}>\n :host {\n user-select: none;\n min-width: 1px;\n }\n :host audio {\n display: block;\n width: 100%;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .noScrollbar {\n scrollbar-color: transparent;\n scrollbar-width: none;\n }\n :host .noScrollbar::-webkit-scrollbar {\n display: none;\n -webkit-appearance: none;\n }\n :host .wrapper {\n position: relative;\n overflow: visible;\n z-index: 2;\n }\n :host .canvases {\n min-height: ${this.getHeight(this.options.height,this.options.splitChannels)}px;\n pointer-events: none;\n }\n :host .canvases > div {\n position: relative;\n }\n :host canvas {\n display: block;\n position: absolute;\n top: 0;\n image-rendering: pixelated;\n }\n :host .progress {\n pointer-events: none;\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 0;\n height: 100%;\n overflow: hidden;\n }\n :host .progress > div {\n position: relative;\n }\n :host .cursor {\n pointer-events: none;\n position: absolute;\n z-index: 5;\n top: 0;\n left: 0;\n height: 100%;\n border-radius: 2px;\n }\n </style>\n\n <div class="scroll" part="scroll">\n <div class="wrapper" part="wrapper">\n <div class="canvases" part="canvases"></div>\n <div class="progress" part="progress"></div>\n <div class="cursor" part="cursor"></div>\n </div>\n </div>\n `,[t,e]}setOptions(t){var e;if(this.options.container!==t.container){const e=this.parentFromOptionsContainer(t.container);e.appendChild(this.container),this.parent=e}!0===t.dragToSeek||"object"==typeof this.options.dragToSeek?this.initDrag():(null===(e=this.dragStream)||void 0===e||e.cleanup(),this.dragStream=null),this.options=t,this.reRender()}getWrapper(){return this.wrapper}getWidth(){return this.scrollContainer.clientWidth}getScroll(){return this.scrollContainer.scrollLeft}setScroll(t){this.scrollContainer.scrollLeft=t}setScrollPercentage(t){const{scrollWidth:e}=this.scrollContainer,i=e*t;this.setScroll(i)}destroy(){var t;this.subscriptions.forEach((t=>t())),this.container.remove(),this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=null),null===(t=this.unsubscribeOnScroll)||void 0===t||t.forEach((t=>t())),this.unsubscribeOnScroll=[],this.dragStream&&(this.dragStream.cleanup(),this.dragStream=null),this.scrollStream&&(this.scrollStream.cleanup(),this.scrollStream=null)}createDelay(t=10){let e,i;const s=()=>{e&&(clearTimeout(e),e=void 0),i&&(i(),i=void 0)};return this.timeouts.push(s),()=>new Promise(((n,r)=>{s(),i=r,e=setTimeout((()=>{e=void 0,i=void 0,n()}),t)}))}getHeight(t,e){var i;const s=(null===(i=this.audioData)||void 0===i?void 0:i.numberOfChannels)||1;return function({optionsHeight:t,optionsSplitChannels:e,parentHeight:i,numberOfChannels:s,defaultHeight:n=128}){if(null==t)return n;const r=Number(t);if(!isNaN(r))return r;if("auto"===t){const t=i||n;return(null==e?void 0:e.every((t=>!t.overlay)))?t/s:t}return n}({optionsHeight:t,optionsSplitChannels:e,parentHeight:this.parent.clientHeight,numberOfChannels:s,defaultHeight:128})}convertColorValues(t,e){return function(t,e,i){if(!Array.isArray(t))return t||"";if(0===t.length)return"#999";if(t.length<2)return t[0]||"";const s=document.createElement("canvas"),n=s.getContext("2d"),r=null!=i?i:s.height*e,o=n.createLinearGradient(0,0,0,r||e),a=1/(t.length-1);return t.forEach(((t,e)=>{o.addColorStop(e*a,t)})),o}(t,this.getPixelRatio(),null==e?void 0:e.canvas.height)}getPixelRatio(){return t=window.devicePixelRatio,Math.max(1,t||1);var t}renderBarWaveform(t,e,i,s){const{width:n,height:r}=i.canvas,{halfHeight:o,barWidth:a,barRadius:h,barIndexScale:l,barSpacing:u,barMinHeight:c}=function({width:t,height:e,length:i,options:s,pixelRatio:n}){const r=e/2,o=s.barWidth?s.barWidth*n:1,a=s.barGap?s.barGap*n:s.barWidth?o/2:0,h=o+a||1;return{halfHeight:r,barWidth:o,barGap:a,barRadius:s.barRadius||0,barMinHeight:s.barMinHeight?s.barMinHeight*n:0,barIndexScale:i>0?t/h/i:0,barSpacing:h}}({width:n,height:r,length:(t[0]||[]).length,options:e,pixelRatio:this.getPixelRatio()}),m=function({channelData:t,barIndexScale:e,barSpacing:i,barWidth:s,halfHeight:n,vScale:r,canvasHeight:o,barAlign:a,barMinHeight:h}){const l=t[0]||[],u=t[1]||l,c=l.length,m=[];let v=0,g=0,f=0;for(let t=0;t<=c;t++){const c=Math.round(t*e);if(c>v){const{topHeight:t,totalHeight:e}=d({maxTop:g,maxBottom:f,halfHeight:n,vScale:r,barMinHeight:h,barAlign:a}),l=p({barAlign:a,halfHeight:n,topHeight:t,totalHeight:e,canvasHeight:o});m.push({x:v*i,y:l,width:s,height:e}),v=c,g=0,f=0}const b=Math.abs(l[t]||0),y=Math.abs(u[t]||0);b>g&&(g=b),y>f&&(f=y)}return m}({channelData:t,barIndexScale:l,barSpacing:u,barWidth:a,halfHeight:o,vScale:s,canvasHeight:r,barAlign:e.barAlign,barMinHeight:c});i.beginPath();for(const t of m)h&&"roundRect"in i?i.roundRect(t.x,t.y,t.width,t.height,h):i.rect(t.x,t.y,t.width,t.height);i.fill(),i.closePath()}renderLineWaveform(t,e,i,s){const{width:n,height:r}=i.canvas,o=function({channelData:t,width:e,height:i,vScale:s}){const n=i/2,r=t[0]||[];return[r,t[1]||r].map(((t,i)=>{const r=t.length,o=r?e/r:0,a=n,h=0===i?-1:1,l=[{x:0,y:a}];let u=0,c=0;for(let e=0;e<=r;e++){const i=Math.round(e*o);if(i>u){const t=a+(Math.round(c*n*s)||1)*h;l.push({x:u,y:t}),u=i,c=0}const r=Math.abs(t[e]||0);r>c&&(c=r)}return l.push({x:u,y:a}),l}))}({channelData:t,width:n,height:r,vScale:s});i.beginPath();for(const t of o)if(t.length){i.moveTo(t[0].x,t[0].y);for(let e=1;e<t.length;e++){const s=t[e];i.lineTo(s.x,s.y)}}i.fill(),i.closePath()}renderWaveform(t,e,i){if(i.fillStyle=this.convertColorValues(e.waveColor,i),e.renderFunction)return void e.renderFunction(t,i);const s=function({channelData:t,barHeight:e,normalize:i,maxPeak:s}){var n;const r=e||1;if(!i)return r;const o=t[0];if(!o||0===o.length)return r;let a=null!=s?s:0;if(!s)for(let t=0;t<o.length;t++){const e=null!==(n=o[t])&&void 0!==n?n:0,i=Math.abs(e);i>a&&(a=i)}return a?r/a:r}({channelData:t,barHeight:e.barHeight,normalize:e.normalize,maxPeak:e.maxPeak});v(e)?this.renderBarWaveform(t,e,i,s):this.renderLineWaveform(t,e,i,s)}renderSingleCanvas(t,e,i,s,n,r,o){const a=this.getPixelRatio(),h=document.createElement("canvas");h.width=Math.round(i*a),h.height=Math.round(s*a),h.style.width=`${i}px`,h.style.height=`${s}px`,h.style.left=`${Math.round(n)}px`,r.appendChild(h);const l=h.getContext("2d");if(e.renderFunction?(l.fillStyle=this.convertColorValues(e.waveColor,l),e.renderFunction(t,l)):this.renderWaveform(t,e,l),h.width>0&&h.height>0){const t=h.cloneNode(),i=t.getContext("2d");i.drawImage(h,0,0),i.globalCompositeOperation="source-in",i.fillStyle=this.convertColorValues(e.progressColor,i),i.fillRect(0,0,h.width,h.height),o.appendChild(t)}}renderMultiCanvas(t,e,i,s,n,r){const o=this.getPixelRatio(),{clientWidth:a}=this.scrollContainer,h=i/o,l=function({clientWidth:t,totalWidth:e,options:i}){return g(Math.min(8e3,t,e),i)}({clientWidth:a,totalWidth:h,options:e});let u={};if(0===l)return;const c=i=>{if(i<0||i>=d)return;if(u[i])return;u[i]=!0;const o=i*l;let a=Math.min(h-o,l);if(a=g(a,e),a<=0)return;const c=function({channelData:t,offset:e,clampedWidth:i,totalWidth:s}){return t.map((t=>{const n=Math.floor(e/s*t.length),r=Math.floor((e+i)/s*t.length);return t.slice(n,r)}))}({channelData:t,offset:o,clampedWidth:a,totalWidth:h});this.renderSingleCanvas(c,e,a,s,o,n,r)},d=Math.ceil(h/l);if(!this.isScrollable){for(let t=0;t<d;t++)c(t);return}if(f({scrollLeft:this.scrollContainer.scrollLeft,totalWidth:h,numCanvases:d}).forEach((t=>c(t))),d>1){const t=this.on("scroll",(()=>{const{scrollLeft:t}=this.scrollContainer;Object.keys(u).length>10&&(n.innerHTML="",r.innerHTML="",u={}),f({scrollLeft:t,totalWidth:h,numCanvases:d}).forEach((t=>c(t)))}));this.unsubscribeOnScroll.push(t)}}renderChannel(t,e,i,s){var{overlay:n}=e,r=function(t,e){var i={};for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&e.indexOf(s)<0&&(i[s]=t[s]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(s=Object.getOwnPropertySymbols(t);n<s.length;n++)e.indexOf(s[n])<0&&Object.prototype.propertyIsEnumerable.call(t,s[n])&&(i[s[n]]=t[s[n]])}return i}(e,["overlay"]);const o=document.createElement("div"),a=this.getHeight(r.height,r.splitChannels);o.style.height=`${a}px`,n&&s>0&&(o.style.marginTop=`-${a}px`),this.canvasWrapper.style.minHeight=`${a}px`,this.canvasWrapper.appendChild(o);const h=o.cloneNode();this.progressWrapper.appendChild(h),this.renderMultiCanvas(t,r,i,a,o,h)}render(t){return i(this,void 0,void 0,(function*(){var e;this.timeouts.forEach((t=>t())),this.timeouts=[],this.canvasWrapper.innerHTML="",this.progressWrapper.innerHTML="",null!=this.options.width&&(this.scrollContainer.style.width="number"==typeof this.options.width?`${this.options.width}px`:this.options.width);const i=this.getPixelRatio(),s=this.scrollContainer.clientWidth,{scrollWidth:n,isScrollable:r,useParentWidth:o,width:a}=function({duration:t,minPxPerSec:e=0,parentWidth:i,fillParent:s,pixelRatio:n}){const r=Math.ceil(t*e),o=r>i,a=Boolean(s&&!o);return{scrollWidth:r,isScrollable:o,useParentWidth:a,width:(a?i:r)*n}}({duration:t.duration,minPxPerSec:this.options.minPxPerSec||0,parentWidth:s,fillParent:this.options.fillParent,pixelRatio:i});if(this.isScrollable=r,this.wrapper.style.width=o?"100%":`${n}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 i=0;i<t.numberOfChannels;i++){const s=Object.assign(Object.assign({},this.options),null===(e=this.options.splitChannels)||void 0===e?void 0:e[i]);this.renderChannel([t.getChannelData(i)],s,a,i)}else{const e=[t.getChannelData(0)];t.numberOfChannels>1&&e.push(t.getChannelData(1)),this.renderChannel(e,this.options,a,0)}Promise.resolve().then((()=>this.emit("rendered")))}))}reRender(){if(this.unsubscribeOnScroll.forEach((t=>t())),this.unsubscribeOnScroll=[],!this.audioData)return;const{scrollWidth:t}=this.scrollContainer,{right:e}=this.progressWrapper.getBoundingClientRect();if(this.render(this.audioData),this.isScrollable&&t!==this.scrollContainer.scrollWidth){const{right:t}=this.progressWrapper.getBoundingClientRect(),i=function(t){const e=2*t;return(e<0?Math.floor(e):Math.ceil(e))/2}(t-e);this.scrollContainer.scrollLeft+=i}}zoom(t){this.options.minPxPerSec=t,this.reRender()}scrollIntoView(t,e=!1){var i;const{scrollLeft:s,scrollWidth:n,clientWidth:r}=this.scrollContainer,o=t*n,a=s,h=s+r,l=r/2;if(this.isDragging){const t=30;o+t>h?this.scrollContainer.scrollLeft+=t:o-t<a&&(this.scrollContainer.scrollLeft-=t)}else{(o<a||o>h)&&(this.scrollContainer.scrollLeft=o-(this.options.autoCenter?l:0));const t=o-s-l;if(e&&this.options.autoCenter&&t>0){const e=null===(i=this.audioData)||void 0===i?void 0:i.duration;if(void 0===e||e<=0)return void(this.scrollContainer.scrollLeft+=t);const s=n/e;this.scrollContainer.scrollLeft+=s<=600?Math.min(t,10):t}}}renderProgress(t,e){if(isNaN(t))return;const i=100*t;this.canvasWrapper.style.clipPath=`polygon(${i}% 0%, 100% 0%, 100% 100%, ${i}% 100%)`,this.progressWrapper.style.width=`${i}%`,this.cursor.style.left=`${i}%`,this.cursor.style.transform=this.options.cursorWidth?`translateX(-${t*this.options.cursorWidth}px)`:"",this.isScrollable&&this.options.autoScroll&&this.audioData&&this.audioData.duration>0&&this.scrollIntoView(t,e)}exportImage(t,e,s){return i(this,void 0,void 0,(function*(){const i=this.canvasWrapper.querySelectorAll("canvas");if(!i.length)throw new Error("No waveform data");if("dataURL"===s){const s=Array.from(i).map((i=>i.toDataURL(t,e)));return Promise.resolve(s)}return Promise.all(Array.from(i).map((i=>new Promise(((s,n)=>{i.toBlob((t=>{t?s(t):n(new Error("Could not export image"))}),t,e)})))))}))}}class C extends t{constructor(){super(...arguments),this.animationFrameId=null,this.isRunning=!1}start(){if(this.isRunning)return;this.isRunning=!0;const t=()=>{this.isRunning&&(this.emit("tick"),this.animationFrameId=requestAnimationFrame(t))};t()}stop(){this.isRunning=!1,null!==this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}destroy(){this.stop()}}class w extends t{constructor(t=new AudioContext){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,this.audioContext=t,this.gainNode=this.audioContext.createGain(),this.gainNode.connect(this.audioContext.destination)}load(){return i(this,void 0,void 0,(function*(){}))}get src(){return this.currentSrc}set src(t){if(this.currentSrc=t,this._duration=void 0,!t)return this.buffer=null,void this.emit("emptied");fetch(t).then((e=>{if(e.status>=400)throw new Error(`Failed to fetch ${t}: ${e.status} (${e.statusText})`);return e.arrayBuffer()})).then((e=>this.currentSrc!==t?null:this.audioContext.decodeAudioData(e))).then((e=>{this.currentSrc===t&&(this.buffer=e,this.emit("loadedmetadata"),this.emit("canplay"),this.autoplay&&this.play())})).catch((t=>{console.error("WebAudioPlayer load error:",t)}))}_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 t=this.playbackPosition;(t>=this.duration||t<0)&&(t=0,this.playbackPosition=0),this.bufferNode.start(this.audioContext.currentTime,t),this.playStartTime=this.audioContext.currentTime,this.bufferNode.onended=()=>{this.currentTime>=this.duration&&(this.pause(),this.emit("ended"))}}_pause(){var t;this.paused=!0,null===(t=this.bufferNode)||void 0===t||t.stop(),this.playbackPosition+=(this.audioContext.currentTime-this.playStartTime)*this._playbackRate}play(){return i(this,void 0,void 0,(function*(){this.paused&&(this._play(),this.emit("play"))}))}pause(){this.paused||(this._pause(),this.emit("pause"))}stopAt(t){const e=t-this.currentTime,i=this.bufferNode;null==i||i.stop(this.audioContext.currentTime+e),null==i||i.addEventListener("ended",(()=>{i===this.bufferNode&&(this.bufferNode=null,this.pause())}),{once:!0})}setSinkId(t){return i(this,void 0,void 0,(function*(){return this.audioContext.setSinkId(t)}))}get playbackRate(){return this._playbackRate}set playbackRate(t){const e=!this.paused;e&&this._pause(),this._playbackRate=t,e&&this._play(),this.bufferNode&&(this.bufferNode.playbackRate.value=t)}get currentTime(){return this.paused?this.playbackPosition:this.playbackPosition+(this.audioContext.currentTime-this.playStartTime)*this._playbackRate}set currentTime(t){const e=!this.paused;e&&this._pause(),this.playbackPosition=t,e&&this._play(),this.emit("seeking"),this.emit("timeupdate")}get duration(){var t,e;return null!==(t=this._duration)&&void 0!==t?t:(null===(e=this.buffer)||void 0===e?void 0:e.duration)||0}set duration(t){this._duration=t}get volume(){return this.gainNode.gain.value}set volume(t){this.gainNode.gain.value=t,this.emit("volumechange")}get muted(){return this._muted}set muted(t){this._muted!==t&&(this._muted=t,this._muted?this.gainNode.disconnect():this.gainNode.connect(this.audioContext.destination))}canPlayType(t){return/^(audio|video)\//.test(t)}getGainNode(){return this.gainNode}getChannelData(){const t=[];if(!this.buffer)return t;const e=this.buffer.numberOfChannels;for(let i=0;i<e;i++)t.push(this.buffer.getChannelData(i));return t}removeAttribute(t){switch(t){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}}}const P={waveColor:"#999",progressColor:"#555",cursorWidth:1,minPxPerSec:0,fillParent:!0,interact:!0,dragToSeek:!1,autoScroll:!0,autoCenter:!0,sampleRate:8e3};class M extends c{static create(t){return new M(t)}getState(){return this.wavesurferState}getRenderer(){return this.renderer}constructor(t){const e=t.media||("WebAudio"===t.backend?new w:void 0);super({media:e,mediaControls:t.mediaControls,autoplay:t.autoplay,playbackRate:t.audioRate}),this.plugins=[],this.decodedData=null,this.stopAtPosition=null,this.subscriptions=[],this.mediaSubscriptions=[],this.abortController=null,this.reactiveCleanups=[],this.options=Object.assign({},P,t);const{state:i,actions:s}=function(t){var e,i,s,n,r,o;const a=null!==(e=null==t?void 0:t.currentTime)&&void 0!==e?e:h(0),u=null!==(i=null==t?void 0:t.duration)&&void 0!==i?i:h(0),c=null!==(s=null==t?void 0:t.isPlaying)&&void 0!==s?s:h(!1),d=null!==(n=null==t?void 0:t.isSeeking)&&void 0!==n?n:h(!1),p=null!==(r=null==t?void 0:t.volume)&&void 0!==r?r:h(1),m=null!==(o=null==t?void 0:t.playbackRate)&&void 0!==o?o:h(1),v=h(null),g=h(null),f=h(""),b=h(0),y=h(0),S=l((()=>!c.value),[c]),C=l((()=>null!==v.value),[v]),w=l((()=>C.value&&u.value>0),[C,u]),P=l((()=>a.value),[a]),M=l((()=>u.value>0?a.value/u.value:0),[a,u]);return{state:{currentTime:a,duration:u,isPlaying:c,isPaused:S,isSeeking:d,volume:p,playbackRate:m,audioBuffer:v,peaks:g,url:f,zoom:b,scrollPosition:y,canPlay:C,isReady:w,progress:P,progressPercent:M},actions:{setCurrentTime:t=>{const e=Math.max(0,Math.min(u.value||1/0,t));a.set(e)},setDuration:t=>{u.set(Math.max(0,t))},setPlaying:t=>{c.set(t)},setSeeking:t=>{d.set(t)},setVolume:t=>{const e=Math.max(0,Math.min(1,t));p.set(e)},setPlaybackRate:t=>{const e=Math.max(.1,Math.min(16,t));m.set(e)},setAudioBuffer:t=>{v.set(t),t&&u.set(t.duration)},setPeaks:t=>{g.set(t)},setUrl:t=>{f.set(t)},setZoom:t=>{b.set(Math.max(0,t))},setScrollPosition:t=>{y.set(Math.max(0,t))}}}}({isPlaying:this.isPlayingSignal,currentTime:this.currentTimeSignal,duration:this.durationSignal,volume:this.volumeSignal,playbackRate:this.playbackRateSignal,isSeeking:this.seekingSignal});this.wavesurferState=i,this.wavesurferActions=s,this.timer=new C;const n=e?void 0:this.getMediaElement();this.renderer=new S(this.options,n),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReactiveState(),this.initPlugins();const r=this.options.url||this.getSrc()||"";Promise.resolve().then((()=>{this.emit("init");const{peaks:t,duration:e}=this.options;(r||t&&e)&&this.load(r,t,e).catch((t=>{this.emit("error",t instanceof Error?t:new Error(String(t)))}))}))}updateProgress(t=this.getCurrentTime()){return this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),t}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{if(!this.isSeeking()){const t=this.updateProgress();this.emit("timeupdate",t),this.emit("audioprocess",t),null!=this.stopAtPosition&&this.isPlaying()&&t>=this.stopAtPosition&&this.pause()}})))}initReactiveState(){this.reactiveCleanups.push(function(t,e){const i=[];i.push(u((()=>{const i=t.isPlaying.value;e.emit(i?"play":"pause")}),[t.isPlaying])),i.push(u((()=>{const i=t.currentTime.value;e.emit("timeupdate",i),t.isPlaying.value&&e.emit("audioprocess",i)}),[t.currentTime,t.isPlaying])),i.push(u((()=>{t.isSeeking.value&&e.emit("seeking",t.currentTime.value)}),[t.isSeeking,t.currentTime]));let s=!1;i.push(u((()=>{t.isReady.value&&!s&&(s=!0,e.emit("ready",t.duration.value))}),[t.isReady,t.duration]));let n=!1;return i.push(u((()=>{const i=t.isPlaying.value,s=t.currentTime.value,r=t.duration.value,o=r>0&&s>=r;n&&!i&&o&&e.emit("finish"),n=i&&o}),[t.isPlaying,t.currentTime,t.duration])),i.push(u((()=>{const i=t.zoom.value;i>0&&e.emit("zoom",i)}),[t.zoom])),()=>{i.forEach((t=>t()))}}(this.wavesurferState,{emit:this.emit.bind(this)}))}initPlayerEvents(){this.isPlaying()&&(this.emit("play"),this.timer.start()),this.mediaSubscriptions.push(this.onMediaEvent("timeupdate",(()=>{const t=this.updateProgress();this.emit("timeupdate",t)})),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",(()=>{var t;this.emit("error",null!==(t=this.getMediaElement().error)&&void 0!==t?t:new Error("Media error")),this.stopAtPosition=null})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",((t,e)=>{this.options.interact&&(this.seekTo(t),this.emit("interaction",t*this.getDuration()),this.emit("click",t,e))})),this.renderer.on("dblclick",((t,e)=>{this.emit("dblclick",t,e)})),this.renderer.on("scroll",((t,e,i,s)=>{const n=this.getDuration();this.emit("scroll",t*n,e*n,i,s)})),this.renderer.on("render",(()=>{this.emit("redraw")})),this.renderer.on("rendered",(()=>{this.emit("redrawcomplete")})),this.renderer.on("dragstart",(t=>{this.emit("dragstart",t)})),this.renderer.on("dragend",(t=>{this.emit("dragend",t)})),this.renderer.on("resize",(()=>{this.emit("resize")})));{let t;const e=this.renderer.on("drag",(e=>{var i;if(!this.options.interact)return;this.renderer.renderProgress(e),clearTimeout(t);let s=0;const n=this.options.dragToSeek;this.isPlaying()?s=0:!0===n?s=200:n&&"object"==typeof n&&(s=null!==(i=n.debounceTime)&&void 0!==i?i:200),t=setTimeout((()=>{this.seekTo(e)}),s),this.emit("interaction",e*this.getDuration()),this.emit("drag",e)}));this.subscriptions.push((()=>{clearTimeout(t),e()}))}}initPlugins(){var t;(null===(t=this.options.plugins)||void 0===t?void 0:t.length)&&this.options.plugins.forEach((t=>{this.registerPlugin(t)}))}unsubscribePlayerEvents(){this.mediaSubscriptions.forEach((t=>t())),this.mediaSubscriptions=[]}setOptions(t){this.options=Object.assign({},this.options,t),t.duration&&!t.peaks&&(this.decodedData=s.createBuffer(this.exportPeaks(),t.duration)),t.peaks&&t.duration&&(this.decodedData=s.createBuffer(t.peaks,t.duration)),this.renderer.setOptions(this.options),t.audioRate&&this.setPlaybackRate(t.audioRate),null!=t.mediaControls&&(this.getMediaElement().controls=t.mediaControls)}registerPlugin(t){if(this.plugins.includes(t))return t;t._init(this),this.plugins.push(t);const e=t.once("destroy",(()=>{this.plugins=this.plugins.filter((e=>e!==t)),this.subscriptions=this.subscriptions.filter((t=>t!==e))}));return this.subscriptions.push(e),t}unregisterPlugin(t){this.plugins=this.plugins.filter((e=>e!==t)),t.destroy()}getWrapper(){return this.renderer.getWrapper()}getWidth(){return this.renderer.getWidth()}getScroll(){return this.renderer.getScroll()}setScroll(t){return this.renderer.setScroll(t)}setScrollTime(t){const e=t/this.getDuration();this.renderer.setScrollPercentage(e)}getActivePlugins(){return this.plugins}loadAudio(t,e,n,r){return i(this,void 0,void 0,(function*(){var i;if(this.emit("load",t),!this.options.media&&this.isPlaying()&&this.pause(),this.decodedData=null,this.stopAtPosition=null,null===(i=this.abortController)||void 0===i||i.abort(),this.abortController=null,!e&&!n){const i=this.options.fetchParams||{};window.AbortController&&!i.signal&&(this.abortController=new AbortController,i.signal=this.abortController.signal);const s=t=>this.emit("loading",t);e=yield a.fetchBlob(t,s,i);const n=this.options.blobMimeType;n&&(e=new Blob([e],{type:n}))}this.setSrc(t,e);const o=yield new Promise((t=>{const e=r||this.getDuration();e?t(e):this.mediaSubscriptions.push(this.onMediaEvent("loadedmetadata",(()=>t(this.getDuration())),{once:!0}))}));if(!t&&!e){const t=this.getMediaElement();t instanceof w&&(t.duration=o)}if(n)this.decodedData=s.createBuffer(n,o||0);else if(e){const t=yield e.arrayBuffer();this.decodedData=yield s.decode(t,this.options.sampleRate)}this.decodedData&&(this.emit("decode",this.getDuration()),this.renderer.render(this.decodedData)),this.emit("ready",this.getDuration())}))}load(t,e,s){return i(this,void 0,void 0,(function*(){try{return yield this.loadAudio(t,void 0,e,s)}catch(t){throw this.emit("error",t),t}}))}loadBlob(t,e,s){return i(this,void 0,void 0,(function*(){try{return yield this.loadAudio("",t,e,s)}catch(t){throw this.emit("error",t),t}}))}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(t),this.emit("zoom",t)}getDecodedData(){return this.decodedData}exportPeaks({channels:t=2,maxLength:e=8e3,precision:i=1e4}={}){if(!this.decodedData)throw new Error("The audio has not been decoded yet");const s=Math.min(t,this.decodedData.numberOfChannels),n=[];for(let t=0;t<s;t++){const s=this.decodedData.getChannelData(t),r=[],o=s.length/e;for(let t=0;t<e;t++){const e=s.slice(Math.floor(t*o),Math.ceil((t+1)*o));let n=0;for(let t=0;t<e.length;t++){const i=e[t];Math.abs(i)>Math.abs(n)&&(n=i)}r.push(Math.round(n*i)/i)}n.push(r)}return n}getDuration(){let t=super.getDuration()||0;return 0!==t&&t!==1/0||!this.decodedData||(t=this.decodedData.duration),t}toggleInteraction(t){this.options.interact=t}setTime(t){this.stopAtPosition=null,super.setTime(t),this.updateProgress(t),this.emit("timeupdate",t)}seekTo(t){const e=this.getDuration()*t;this.setTime(e)}play(t,e){const s=Object.create(null,{play:{get:()=>super.play}});return i(this,void 0,void 0,(function*(){null!=t&&this.setTime(t);const i=yield s.play.call(this);return null!=e&&(this.media instanceof w?this.media.stopAt(e):this.stopAtPosition=e),i}))}playPause(){return i(this,void 0,void 0,(function*(){return this.isPlaying()?this.pause():this.play()}))}stop(){this.pause(),this.setTime(0)}skip(t){this.setTime(this.getCurrentTime()+t)}empty(){this.load("",[[0]],.001)}setMediaElement(t){this.unsubscribePlayerEvents(),super.setMediaElement(t),this.initPlayerEvents()}exportImage(){return i(this,arguments,void 0,(function*(t="image/png",e=1,i="dataURL"){return this.renderer.exportImage(t,e,i)}))}destroy(){var t;this.emit("destroy"),null===(t=this.abortController)||void 0===t||t.abort(),this.plugins.forEach((t=>t.destroy())),this.subscriptions.forEach((t=>t())),this.unsubscribePlayerEvents(),this.reactiveCleanups.forEach((t=>t())),this.reactiveCleanups=[],this.timer.destroy(),this.renderer.destroy(),super.destroy()}}M.BasePlugin=e,M.dom=o;const E={height:50,overlayColor:"rgba(100, 100, 100, 0.1)",insertPosition:"afterend"};class k extends e{constructor(t){super(t),this.miniWavesurfer=null,this.miniSubscriptions=[],this.container=null,this.isInitializing=!1,this.dragTimeout=null,this.options=Object.assign({},E,t),this.minimapWrapper=this.initMinimapWrapper(),this.overlay=this.initOverlay()}static create(t){return new k(t)}onInit(){var t,e;if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");this.options.container?("string"==typeof this.options.container?this.container=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(this.container=this.options.container),null===(t=this.container)||void 0===t||t.appendChild(this.minimapWrapper)):(this.container=this.wavesurfer.getWrapper().parentElement,null===(e=this.container)||void 0===e||e.insertAdjacentElement(this.options.insertPosition,this.minimapWrapper)),this.initWaveSurferEvents(),Promise.resolve().then((()=>{this.initMinimap()}))}initMinimapWrapper(){return r("div",{part:"minimap",style:{position:"relative"}})}initOverlay(){return r("div",{part:"minimap-overlay",style:{position:"absolute",zIndex:"2",left:"0",top:"0",bottom:"0",transition:"left 100ms ease-out, width 100ms ease-out",pointerEvents:"none",backgroundColor:this.options.overlayColor}},this.minimapWrapper)}initMinimap(){if(this.isInitializing)return;if(this.isInitializing=!0,this.destroyMinimap(),!this.wavesurfer)return void(this.isInitializing=!1);const t=this.wavesurfer.getDecodedData();if(!t)return void(this.isInitializing=!1);const e=[];for(let i=0;i<t.numberOfChannels;i++)e.push(t.getChannelData(i));this.miniWavesurfer=M.create(Object.assign(Object.assign({},this.options),{container:this.minimapWrapper,minPxPerSec:0,fillParent:!0,url:void 0,media:void 0,peaks:e,duration:t.duration})),this.syncMinimapPosition(this.wavesurfer.getCurrentTime()),this.miniSubscriptions.push(this.miniWavesurfer.on("audioprocess",(t=>{this.emit("audioprocess",t)})),this.miniWavesurfer.on("click",((t,e)=>{var i;null===(i=this.wavesurfer)||void 0===i||i.seekTo(t),this.emit("click",t,e)})),this.miniWavesurfer.on("dblclick",((t,e)=>{this.emit("dblclick",t,e)})),this.miniWavesurfer.on("decode",(t=>{this.emit("decode",t)})),this.miniWavesurfer.on("destroy",(()=>{this.emit("destroy")})),this.miniWavesurfer.on("drag",(t=>{this.onMinimapDrag(t),this.emit("drag",t)})),this.miniWavesurfer.on("dragend",(t=>{this.emit("dragend",t)})),this.miniWavesurfer.on("dragstart",(t=>{this.emit("dragstart",t)})),this.miniWavesurfer.on("interaction",(()=>{this.emit("interaction")})),this.miniWavesurfer.on("init",(()=>{this.emit("init")})),this.miniWavesurfer.on("ready",(()=>{var t;this.syncMinimapPosition((null===(t=this.wavesurfer)||void 0===t?void 0:t.getCurrentTime())||0),this.emit("ready")})),this.miniWavesurfer.on("redraw",(()=>{this.emit("redraw")})),this.miniWavesurfer.on("redrawcomplete",(()=>{this.emit("redrawcomplete")})),this.miniWavesurfer.on("seeking",(t=>{this.emit("seeking",t)})),this.miniWavesurfer.on("timeupdate",(t=>{this.emit("timeupdate",t)}))),this.isInitializing=!1}updateOverlay(t,e){if(!this.wavesurfer)return;const i=this.wavesurfer.getDuration();if(!i)return;if(void 0===t||void 0===e){const s=this.wavesurfer.getWrapper().clientWidth||1,n=this.wavesurfer.getWidth(),r=this.wavesurfer.getScroll();t=r/s*i,e=(r+n)/s*i}const s=Math.min(Math.max(t,0),i),n=s/i*100,r=(Math.min(Math.max(e,s),i)-s)/i*100;this.overlay.style.left=`${n}%`,this.overlay.style.width=`${Math.min(r,100-n)}%`}destroyMinimap(){const t=this.miniWavesurfer;this.miniWavesurfer=null,null==t||t.destroy(),this.miniSubscriptions.forEach((t=>t())),this.miniSubscriptions=[],this.dragTimeout&&(clearTimeout(this.dragTimeout),this.dragTimeout=null)}renderMainProgress(t){this.wavesurfer&&this.wavesurfer.getRenderer().renderProgress(t,this.wavesurfer.isPlaying())}renderMinimapProgress(t){this.wavesurfer&&this.miniWavesurfer&&this.miniWavesurfer.getRenderer().renderProgress(t,this.wavesurfer.isPlaying())}syncMinimapPosition(t){if(!this.wavesurfer||!this.miniWavesurfer)return;const e=this.wavesurfer.getDuration();e&&(this.miniWavesurfer.getDuration()?this.miniWavesurfer.setTime(t):this.renderMinimapProgress(t/e))}onMinimapDrag(t){var e;if(!this.wavesurfer)return;this.renderMainProgress(t),this.dragTimeout&&clearTimeout(this.dragTimeout);let i=0;const s=this.options.dragToSeek;this.wavesurfer.isPlaying()||!0!==s?!this.wavesurfer.isPlaying()&&s&&"object"==typeof s&&(i=null!==(e=s.debounceTime)&&void 0!==e?e:200):i=200,this.dragTimeout=setTimeout((()=>{var e;null===(e=this.wavesurfer)||void 0===e||e.seekTo(t),this.dragTimeout=null}),i)}onRedraw(){this.updateOverlay()}onScroll(t,e){this.updateOverlay(t,e)}initWaveSurferEvents(){this.wavesurfer&&this.subscriptions.push(this.wavesurfer.on("decode",(()=>{this.initMinimap()})),this.wavesurfer.on("timeupdate",(t=>{this.syncMinimapPosition(t)})),this.wavesurfer.on("drag",(t=>{this.renderMinimapProgress(t)})),this.wavesurfer.on("scroll",((t,e)=>{this.onScroll(t,e)})),this.wavesurfer.on("redraw",(()=>{this.onRedraw()})))}destroy(){this.destroyMinimap(),this.minimapWrapper.remove(),this.container=null,super.destroy()}}module.exports=k;
|
|
@@ -58,7 +58,7 @@ declare class MinimapPlugin extends BasePlugin<MinimapPluginEvents, MinimapPlugi
|
|
|
58
58
|
private initMinimapWrapper;
|
|
59
59
|
private initOverlay;
|
|
60
60
|
private initMinimap;
|
|
61
|
-
private
|
|
61
|
+
private updateOverlay;
|
|
62
62
|
private destroyMinimap;
|
|
63
63
|
private renderMainProgress;
|
|
64
64
|
private renderMinimapProgress;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class t{constructor(){this.listeners={}}on(t,e,i){if(this.listeners[t]||(this.listeners[t]=new Set),null==i?void 0:i.once){const i=(...s)=>{this.un(t,i),e(...s)};return this.listeners[t].add(i),()=>this.un(t,i)}return this.listeners[t].add(e),()=>this.un(t,e)}un(t,e){var i;null===(i=this.listeners[t])||void 0===i||i.delete(e)}once(t,e){return this.on(t,e,{once:!0})}unAll(){this.listeners={}}emit(t,...e){this.listeners[t]&&this.listeners[t].forEach((t=>t(...e)))}}class e extends t{constructor(t){super(),this.subscriptions=[],this.isDestroyed=!1,this.options=t}onInit(){}_init(t){this.isDestroyed&&(this.subscriptions=[],this.isDestroyed=!1),this.wavesurfer=t,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach((t=>t())),this.subscriptions=[],this.isDestroyed=!0,this.wavesurfer=void 0}}function i(t,e,i,s){return new(i||(i=Promise))((function(n,r){function o(t){try{h(s.next(t))}catch(t){r(t)}}function a(t){try{h(s.throw(t))}catch(t){r(t)}}function h(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i((function(t){t(e)}))).then(o,a)}h((s=s.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;const s={decode:function(t,e){return i(this,void 0,void 0,(function*(){const i=new AudioContext({sampleRate:e});try{return yield i.decodeAudioData(t)}finally{i.close()}}))},createBuffer:function(t,e){if(!t||0===t.length)throw new Error("channelData must be a non-empty array");if(e<=0)throw new Error("duration must be greater than 0");if("number"==typeof t[0]&&(t=[t]),!t[0]||0===t[0].length)throw new Error("channelData must contain non-empty channel arrays");!function(t){const e=t[0];if(e.some((t=>t>1||t<-1))){const i=e.length;let s=0;for(let t=0;t<i;t++){const i=Math.abs(e[t]);i>s&&(s=i)}for(const e of t)for(let t=0;t<i;t++)e[t]/=s}}(t);const i=t.map((t=>t instanceof Float32Array?t:Float32Array.from(t)));return{duration:e,length:i[0].length,sampleRate:i[0].length/e,numberOfChannels:i.length,getChannelData:t=>{const e=i[t];if(!e)throw new Error(`Channel ${t} not found`);return e},copyFromChannel:AudioBuffer.prototype.copyFromChannel,copyToChannel:AudioBuffer.prototype.copyToChannel}}};function n(t,e){const i=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,s]of Object.entries(e))if("children"===t&&s)for(const[t,e]of Object.entries(s))e instanceof Node?i.appendChild(e):"string"==typeof e?i.appendChild(document.createTextNode(e)):i.appendChild(n(t,e));else"style"===t?Object.assign(i.style,s):"textContent"===t?i.textContent=s:i.setAttribute(t,s.toString());return i}function r(t,e,i){const s=n(t,e||{});return null==i||i.appendChild(s),s}var o=Object.freeze({__proto__:null,createElement:r,default:r});const a={fetchBlob:function(t,e,s){return i(this,void 0,void 0,(function*(){const n=yield fetch(t,s);if(n.status>=400)throw new Error(`Failed to fetch ${t}: ${n.status} (${n.statusText})`);return function(t,e){i(this,void 0,void 0,(function*(){if(!t.body||!t.headers)return;const i=t.body.getReader(),s=Number(t.headers.get("Content-Length"))||0;let n=0;const r=t=>{n+=(null==t?void 0:t.length)||0;const i=Math.round(n/s*100);e(i)};try{for(;;){const t=yield i.read();if(t.done)break;r(t.value)}}catch(t){console.warn("Progress tracking error:",t)}}))}(n.clone(),e),n.blob()}))}};function h(t){let e=t;const i=new Set;return{get value(){return e},set(t){Object.is(e,t)||(e=t,i.forEach((t=>t(e))))},update(t){this.set(t(e))},subscribe:t=>(i.add(t),()=>i.delete(t))}}function l(t,e){const i=h(t());return e.forEach((e=>e.subscribe((()=>{const e=t();Object.is(i.value,e)||i.set(e)})))),{get value(){return i.value},subscribe:t=>i.subscribe(t)}}function u(t,e){let i;const s=()=>{i&&(i(),i=void 0),i=t()},n=e.map((t=>t.subscribe(s)));return s(),()=>{i&&(i(),i=void 0),n.forEach((t=>t()))}}class c 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(t){super(),this.isExternalMedia=!1,this.reactiveMediaEventCleanups=[],t.media?(this.media=t.media,this.isExternalMedia=!0):this.media=document.createElement("audio"),this._isPlaying=h(!1),this._currentTime=h(0),this._duration=h(0),this._volume=h(this.media.volume),this._muted=h(this.media.muted),this._playbackRate=h(this.media.playbackRate||1),this._seeking=h(!1),this.setupReactiveMediaEvents(),t.mediaControls&&(this.media.controls=!0),t.autoplay&&(this.media.autoplay=!0),null!=t.playbackRate&&this.onMediaEvent("canplay",(()=>{null!=t.playbackRate&&(this.media.playbackRate=t.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(t,e,i){return this.media.addEventListener(t,e,i),()=>this.media.removeEventListener(t,e,i)}getSrc(){return this.media.currentSrc||this.media.src||""}revokeSrc(){const t=this.getSrc();t.startsWith("blob:")&&URL.revokeObjectURL(t)}canPlayType(t){return""!==this.media.canPlayType(t)}setSrc(t,e){const i=this.getSrc();if(t&&i===t)return;this.revokeSrc();const s=e instanceof Blob&&(this.canPlayType(e.type)||!t)?URL.createObjectURL(e):t;if(i&&this.media.removeAttribute("src"),s||t)try{this.media.src=s}catch(e){this.media.src=t}}destroy(){this.reactiveMediaEventCleanups.forEach((t=>t())),this.reactiveMediaEventCleanups=[],this.isExternalMedia||(this.media.pause(),this.revokeSrc(),this.media.removeAttribute("src"),this.media.load(),this.media.remove())}setMediaElement(t){this.reactiveMediaEventCleanups.forEach((t=>t())),this.reactiveMediaEventCleanups=[],this.media=t,this.setupReactiveMediaEvents()}play(){return i(this,void 0,void 0,(function*(){try{return yield this.media.play()}catch(t){if(t instanceof DOMException&&"AbortError"===t.name)return;throw t}}))}pause(){this.media.pause()}isPlaying(){return!this.media.paused&&!this.media.ended}setTime(t){this.media.currentTime=Math.max(0,Math.min(t,this.getDuration()))}getDuration(){return this.media.duration}getCurrentTime(){return this.media.currentTime}getVolume(){return this.media.volume}setVolume(t){this.media.volume=t}getMuted(){return this.media.muted}setMuted(t){this.media.muted=t}getPlaybackRate(){return this.media.playbackRate}isSeeking(){return this.media.seeking}setPlaybackRate(t,e){null!=e&&(this.media.preservesPitch=e),this.media.playbackRate=t}getMediaElement(){return this.media}setSinkId(t){return this.media.setSinkId(t)}}function d({maxTop:t,maxBottom:e,halfHeight:i,vScale:s,barMinHeight:n=0,barAlign:r}){let o=Math.round(t*i*s);let a=o+Math.round(e*i*s)||1;return a<n&&(a=n,r||(o=a/2)),{topHeight:o,totalHeight:a}}function p({barAlign:t,halfHeight:e,topHeight:i,totalHeight:s,canvasHeight:n}){return"top"===t?0:"bottom"===t?n-s:e-i}function m(t,e,i){const s=e-t.left,n=i-t.top;return[s/t.width,n/t.height]}function v(t){return Boolean(t.barWidth||t.barGap||t.barAlign)}function g(t,e){if(!v(e))return t;const i=e.barWidth||.5,s=i+(e.barGap||i/2);return 0===s?t:Math.floor(t/s)*s}function f({scrollLeft:t,totalWidth:e,numCanvases:i}){if(0===e)return[0];const s=t/e,n=Math.floor(s*i);return[n-1,n,n+1]}function b(t){const e=t._cleanup;"function"==typeof e&&e()}function y(t){const e=h({scrollLeft:t.scrollLeft,scrollWidth:t.scrollWidth,clientWidth:t.clientWidth}),i=l((()=>function(t){const{scrollLeft:e,scrollWidth:i,clientWidth:s}=t;if(0===i)return{startX:0,endX:1};const n=e/i,r=(e+s)/i;return{startX:Math.max(0,Math.min(1,n)),endX:Math.max(0,Math.min(1,r))}}(e.value)),[e]),s=l((()=>function(t){return{left:t.scrollLeft,right:t.scrollLeft+t.clientWidth}}(e.value)),[e]),n=()=>{e.set({scrollLeft:t.scrollLeft,scrollWidth:t.scrollWidth,clientWidth:t.clientWidth})};t.addEventListener("scroll",n,{passive:!0});return{scrollData:e,percentages:i,bounds:s,cleanup:()=>{t.removeEventListener("scroll",n),b(e)}}}class S extends t{constructor(t,e){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.subscriptions=[],this.options=t;const i=this.parentFromOptionsContainer(t.container);this.parent=i;const[s,n]=this.initHtml();i.appendChild(s),this.container=s,this.scrollContainer=n.querySelector(".scroll"),this.wrapper=n.querySelector(".wrapper"),this.canvasWrapper=n.querySelector(".canvases"),this.progressWrapper=n.querySelector(".progress"),this.cursor=n.querySelector(".cursor"),e&&n.appendChild(e),this.initEvents()}parentFromOptionsContainer(t){let e;if("string"==typeof t?e=document.querySelector(t):t instanceof HTMLElement&&(e=t),!e)throw new Error("Container not found");return e}initEvents(){this.wrapper.addEventListener("click",(t=>{const e=this.wrapper.getBoundingClientRect(),[i,s]=m(e,t.clientX,t.clientY);this.emit("click",i,s)})),this.wrapper.addEventListener("dblclick",(t=>{const e=this.wrapper.getBoundingClientRect(),[i,s]=m(e,t.clientX,t.clientY);this.emit("dblclick",i,s)})),!0!==this.options.dragToSeek&&"object"!=typeof this.options.dragToSeek||this.initDrag(),this.scrollStream=y(this.scrollContainer);const t=u((()=>{const{startX:t,endX:e}=this.scrollStream.percentages.value,{left:i,right:s}=this.scrollStream.bounds.value;this.emit("scroll",t,e,i,s)}),[this.scrollStream.percentages,this.scrollStream.bounds]);if(this.subscriptions.push(t),"function"==typeof ResizeObserver){const t=this.createDelay(100);this.resizeObserver=new ResizeObserver((()=>{t().then((()=>this.onContainerResize())).catch((()=>{}))})),this.resizeObserver.observe(this.scrollContainer)}}onContainerResize(){const t=this.parent.clientWidth;t===this.lastContainerWidth&&"auto"!==this.options.height||(this.lastContainerWidth=t,this.reRender(),this.emit("resize"))}initDrag(){if(this.dragStream)return;this.dragStream=function(t,e={}){const{threshold:i=3,mouseButton:s=0,touchDelay:n=100}=e,r=h(null),o=new Map,a=matchMedia("(pointer: coarse)").matches;let l=()=>{};const u=e=>{if(e.button!==s)return;if(o.set(e.pointerId,e),o.size>1)return;let h=e.clientX,u=e.clientY,c=!1;const d=Date.now(),p=t.getBoundingClientRect(),{left:m,top:v}=p,g=t=>{if(t.defaultPrevented||o.size>1)return;if(a&&Date.now()-d<n)return;const e=t.clientX,s=t.clientY,l=e-h,p=s-u;(c||Math.abs(l)>i||Math.abs(p)>i)&&(t.preventDefault(),t.stopPropagation(),c||(r.set({type:"start",x:h-m,y:u-v}),c=!0),r.set({type:"move",x:e-m,y:s-v,deltaX:l,deltaY:p}),h=e,u=s)},f=t=>{if(o.delete(t.pointerId),c){const e=t.clientX,i=t.clientY;r.set({type:"end",x:e-m,y:i-v})}l()},b=t=>{o.delete(t.pointerId),t.relatedTarget&&t.relatedTarget!==document.documentElement||f(t)},y=t=>{c&&(t.stopPropagation(),t.preventDefault())},S=t=>{t.defaultPrevented||o.size>1||c&&t.preventDefault()};document.addEventListener("pointermove",g),document.addEventListener("pointerup",f),document.addEventListener("pointerout",b),document.addEventListener("pointercancel",b),document.addEventListener("touchmove",S,{passive:!1}),document.addEventListener("click",y,{capture:!0}),l=()=>{document.removeEventListener("pointermove",g),document.removeEventListener("pointerup",f),document.removeEventListener("pointerout",b),document.removeEventListener("pointercancel",b),document.removeEventListener("touchmove",S),setTimeout((()=>{document.removeEventListener("click",y,{capture:!0})}),10)}};return t.addEventListener("pointerdown",u),{signal:r,cleanup:()=>{l(),t.removeEventListener("pointerdown",u),o.clear(),b(r)}}}(this.wrapper);const t=u((()=>{const t=this.dragStream.signal.value;if(!t)return;const e=this.wrapper.getBoundingClientRect().width,i=(s=t.x/e)<0?0:s>1?1:s;var s;"start"===t.type?(this.isDragging=!0,this.emit("dragstart",i)):"move"===t.type?this.emit("drag",i):"end"===t.type&&(this.isDragging=!1,this.emit("dragend",i))}),[this.dragStream.signal]);this.subscriptions.push(t)}initHtml(){const t=document.createElement("div"),e=t.attachShadow({mode:"open"}),i=this.options.cspNonce&&"string"==typeof this.options.cspNonce?this.options.cspNonce.replace(/"/g,""):"";return e.innerHTML=`\n <style${i?` nonce="${i}"`:""}>\n :host {\n user-select: none;\n min-width: 1px;\n }\n :host audio {\n display: block;\n width: 100%;\n }\n :host .scroll {\n overflow-x: auto;\n overflow-y: hidden;\n width: 100%;\n position: relative;\n }\n :host .noScrollbar {\n scrollbar-color: transparent;\n scrollbar-width: none;\n }\n :host .noScrollbar::-webkit-scrollbar {\n display: none;\n -webkit-appearance: none;\n }\n :host .wrapper {\n position: relative;\n overflow: visible;\n z-index: 2;\n }\n :host .canvases {\n min-height: ${this.getHeight(this.options.height,this.options.splitChannels)}px;\n pointer-events: none;\n }\n :host .canvases > div {\n position: relative;\n }\n :host canvas {\n display: block;\n position: absolute;\n top: 0;\n image-rendering: pixelated;\n }\n :host .progress {\n pointer-events: none;\n position: absolute;\n z-index: 2;\n top: 0;\n left: 0;\n width: 0;\n height: 100%;\n overflow: hidden;\n }\n :host .progress > div {\n position: relative;\n }\n :host .cursor {\n pointer-events: none;\n position: absolute;\n z-index: 5;\n top: 0;\n left: 0;\n height: 100%;\n border-radius: 2px;\n }\n </style>\n\n <div class="scroll" part="scroll">\n <div class="wrapper" part="wrapper">\n <div class="canvases" part="canvases"></div>\n <div class="progress" part="progress"></div>\n <div class="cursor" part="cursor"></div>\n </div>\n </div>\n `,[t,e]}setOptions(t){var e;if(this.options.container!==t.container){const e=this.parentFromOptionsContainer(t.container);e.appendChild(this.container),this.parent=e}!0===t.dragToSeek||"object"==typeof this.options.dragToSeek?this.initDrag():(null===(e=this.dragStream)||void 0===e||e.cleanup(),this.dragStream=null),this.options=t,this.reRender()}getWrapper(){return this.wrapper}getWidth(){return this.scrollContainer.clientWidth}getScroll(){return this.scrollContainer.scrollLeft}setScroll(t){this.scrollContainer.scrollLeft=t}setScrollPercentage(t){const{scrollWidth:e}=this.scrollContainer,i=e*t;this.setScroll(i)}destroy(){var t;this.subscriptions.forEach((t=>t())),this.container.remove(),this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=null),null===(t=this.unsubscribeOnScroll)||void 0===t||t.forEach((t=>t())),this.unsubscribeOnScroll=[],this.dragStream&&(this.dragStream.cleanup(),this.dragStream=null),this.scrollStream&&(this.scrollStream.cleanup(),this.scrollStream=null)}createDelay(t=10){let e,i;const s=()=>{e&&(clearTimeout(e),e=void 0),i&&(i(),i=void 0)};return this.timeouts.push(s),()=>new Promise(((n,r)=>{s(),i=r,e=setTimeout((()=>{e=void 0,i=void 0,n()}),t)}))}getHeight(t,e){var i;const s=(null===(i=this.audioData)||void 0===i?void 0:i.numberOfChannels)||1;return function({optionsHeight:t,optionsSplitChannels:e,parentHeight:i,numberOfChannels:s,defaultHeight:n=128}){if(null==t)return n;const r=Number(t);if(!isNaN(r))return r;if("auto"===t){const t=i||n;return(null==e?void 0:e.every((t=>!t.overlay)))?t/s:t}return n}({optionsHeight:t,optionsSplitChannels:e,parentHeight:this.parent.clientHeight,numberOfChannels:s,defaultHeight:128})}convertColorValues(t,e){return function(t,e,i){if(!Array.isArray(t))return t||"";if(0===t.length)return"#999";if(t.length<2)return t[0]||"";const s=document.createElement("canvas"),n=s.getContext("2d"),r=null!=i?i:s.height*e,o=n.createLinearGradient(0,0,0,r||e),a=1/(t.length-1);return t.forEach(((t,e)=>{o.addColorStop(e*a,t)})),o}(t,this.getPixelRatio(),null==e?void 0:e.canvas.height)}getPixelRatio(){return t=window.devicePixelRatio,Math.max(1,t||1);var t}renderBarWaveform(t,e,i,s){const{width:n,height:r}=i.canvas,{halfHeight:o,barWidth:a,barRadius:h,barIndexScale:l,barSpacing:u,barMinHeight:c}=function({width:t,height:e,length:i,options:s,pixelRatio:n}){const r=e/2,o=s.barWidth?s.barWidth*n:1,a=s.barGap?s.barGap*n:s.barWidth?o/2:0,h=o+a||1;return{halfHeight:r,barWidth:o,barGap:a,barRadius:s.barRadius||0,barMinHeight:s.barMinHeight?s.barMinHeight*n:0,barIndexScale:i>0?t/h/i:0,barSpacing:h}}({width:n,height:r,length:(t[0]||[]).length,options:e,pixelRatio:this.getPixelRatio()}),m=function({channelData:t,barIndexScale:e,barSpacing:i,barWidth:s,halfHeight:n,vScale:r,canvasHeight:o,barAlign:a,barMinHeight:h}){const l=t[0]||[],u=t[1]||l,c=l.length,m=[];let v=0,g=0,f=0;for(let t=0;t<=c;t++){const c=Math.round(t*e);if(c>v){const{topHeight:t,totalHeight:e}=d({maxTop:g,maxBottom:f,halfHeight:n,vScale:r,barMinHeight:h,barAlign:a}),l=p({barAlign:a,halfHeight:n,topHeight:t,totalHeight:e,canvasHeight:o});m.push({x:v*i,y:l,width:s,height:e}),v=c,g=0,f=0}const b=Math.abs(l[t]||0),y=Math.abs(u[t]||0);b>g&&(g=b),y>f&&(f=y)}return m}({channelData:t,barIndexScale:l,barSpacing:u,barWidth:a,halfHeight:o,vScale:s,canvasHeight:r,barAlign:e.barAlign,barMinHeight:c});i.beginPath();for(const t of m)h&&"roundRect"in i?i.roundRect(t.x,t.y,t.width,t.height,h):i.rect(t.x,t.y,t.width,t.height);i.fill(),i.closePath()}renderLineWaveform(t,e,i,s){const{width:n,height:r}=i.canvas,o=function({channelData:t,width:e,height:i,vScale:s}){const n=i/2,r=t[0]||[];return[r,t[1]||r].map(((t,i)=>{const r=t.length,o=r?e/r:0,a=n,h=0===i?-1:1,l=[{x:0,y:a}];let u=0,c=0;for(let e=0;e<=r;e++){const i=Math.round(e*o);if(i>u){const t=a+(Math.round(c*n*s)||1)*h;l.push({x:u,y:t}),u=i,c=0}const r=Math.abs(t[e]||0);r>c&&(c=r)}return l.push({x:u,y:a}),l}))}({channelData:t,width:n,height:r,vScale:s});i.beginPath();for(const t of o)if(t.length){i.moveTo(t[0].x,t[0].y);for(let e=1;e<t.length;e++){const s=t[e];i.lineTo(s.x,s.y)}}i.fill(),i.closePath()}renderWaveform(t,e,i){if(i.fillStyle=this.convertColorValues(e.waveColor,i),e.renderFunction)return void e.renderFunction(t,i);const s=function({channelData:t,barHeight:e,normalize:i,maxPeak:s}){var n;const r=e||1;if(!i)return r;const o=t[0];if(!o||0===o.length)return r;let a=null!=s?s:0;if(!s)for(let t=0;t<o.length;t++){const e=null!==(n=o[t])&&void 0!==n?n:0,i=Math.abs(e);i>a&&(a=i)}return a?r/a:r}({channelData:t,barHeight:e.barHeight,normalize:e.normalize,maxPeak:e.maxPeak});v(e)?this.renderBarWaveform(t,e,i,s):this.renderLineWaveform(t,e,i,s)}renderSingleCanvas(t,e,i,s,n,r,o){const a=this.getPixelRatio(),h=document.createElement("canvas");h.width=Math.round(i*a),h.height=Math.round(s*a),h.style.width=`${i}px`,h.style.height=`${s}px`,h.style.left=`${Math.round(n)}px`,r.appendChild(h);const l=h.getContext("2d");if(e.renderFunction?(l.fillStyle=this.convertColorValues(e.waveColor,l),e.renderFunction(t,l)):this.renderWaveform(t,e,l),h.width>0&&h.height>0){const t=h.cloneNode(),i=t.getContext("2d");i.drawImage(h,0,0),i.globalCompositeOperation="source-in",i.fillStyle=this.convertColorValues(e.progressColor,i),i.fillRect(0,0,h.width,h.height),o.appendChild(t)}}renderMultiCanvas(t,e,i,s,n,r){const o=this.getPixelRatio(),{clientWidth:a}=this.scrollContainer,h=i/o,l=function({clientWidth:t,totalWidth:e,options:i}){return g(Math.min(8e3,t,e),i)}({clientWidth:a,totalWidth:h,options:e});let u={};if(0===l)return;const c=i=>{if(i<0||i>=d)return;if(u[i])return;u[i]=!0;const o=i*l;let a=Math.min(h-o,l);if(a=g(a,e),a<=0)return;const c=function({channelData:t,offset:e,clampedWidth:i,totalWidth:s}){return t.map((t=>{const n=Math.floor(e/s*t.length),r=Math.floor((e+i)/s*t.length);return t.slice(n,r)}))}({channelData:t,offset:o,clampedWidth:a,totalWidth:h});this.renderSingleCanvas(c,e,a,s,o,n,r)},d=Math.ceil(h/l);if(!this.isScrollable){for(let t=0;t<d;t++)c(t);return}if(f({scrollLeft:this.scrollContainer.scrollLeft,totalWidth:h,numCanvases:d}).forEach((t=>c(t))),d>1){const t=this.on("scroll",(()=>{const{scrollLeft:t}=this.scrollContainer;Object.keys(u).length>10&&(n.innerHTML="",r.innerHTML="",u={}),f({scrollLeft:t,totalWidth:h,numCanvases:d}).forEach((t=>c(t)))}));this.unsubscribeOnScroll.push(t)}}renderChannel(t,e,i,s){var{overlay:n}=e,r=function(t,e){var i={};for(var s in t)Object.prototype.hasOwnProperty.call(t,s)&&e.indexOf(s)<0&&(i[s]=t[s]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var n=0;for(s=Object.getOwnPropertySymbols(t);n<s.length;n++)e.indexOf(s[n])<0&&Object.prototype.propertyIsEnumerable.call(t,s[n])&&(i[s[n]]=t[s[n]])}return i}(e,["overlay"]);const o=document.createElement("div"),a=this.getHeight(r.height,r.splitChannels);o.style.height=`${a}px`,n&&s>0&&(o.style.marginTop=`-${a}px`),this.canvasWrapper.style.minHeight=`${a}px`,this.canvasWrapper.appendChild(o);const h=o.cloneNode();this.progressWrapper.appendChild(h),this.renderMultiCanvas(t,r,i,a,o,h)}render(t){return i(this,void 0,void 0,(function*(){var e;this.timeouts.forEach((t=>t())),this.timeouts=[],this.canvasWrapper.innerHTML="",this.progressWrapper.innerHTML="",null!=this.options.width&&(this.scrollContainer.style.width="number"==typeof this.options.width?`${this.options.width}px`:this.options.width);const i=this.getPixelRatio(),s=this.scrollContainer.clientWidth,{scrollWidth:n,isScrollable:r,useParentWidth:o,width:a}=function({duration:t,minPxPerSec:e=0,parentWidth:i,fillParent:s,pixelRatio:n}){const r=Math.ceil(t*e),o=r>i,a=Boolean(s&&!o);return{scrollWidth:r,isScrollable:o,useParentWidth:a,width:(a?i:r)*n}}({duration:t.duration,minPxPerSec:this.options.minPxPerSec||0,parentWidth:s,fillParent:this.options.fillParent,pixelRatio:i});if(this.isScrollable=r,this.wrapper.style.width=o?"100%":`${n}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 i=0;i<t.numberOfChannels;i++){const s=Object.assign(Object.assign({},this.options),null===(e=this.options.splitChannels)||void 0===e?void 0:e[i]);this.renderChannel([t.getChannelData(i)],s,a,i)}else{const e=[t.getChannelData(0)];t.numberOfChannels>1&&e.push(t.getChannelData(1)),this.renderChannel(e,this.options,a,0)}Promise.resolve().then((()=>this.emit("rendered")))}))}reRender(){if(this.unsubscribeOnScroll.forEach((t=>t())),this.unsubscribeOnScroll=[],!this.audioData)return;const{scrollWidth:t}=this.scrollContainer,{right:e}=this.progressWrapper.getBoundingClientRect();if(this.render(this.audioData),this.isScrollable&&t!==this.scrollContainer.scrollWidth){const{right:t}=this.progressWrapper.getBoundingClientRect(),i=function(t){const e=2*t;return(e<0?Math.floor(e):Math.ceil(e))/2}(t-e);this.scrollContainer.scrollLeft+=i}}zoom(t){this.options.minPxPerSec=t,this.reRender()}scrollIntoView(t,e=!1){var i;const{scrollLeft:s,scrollWidth:n,clientWidth:r}=this.scrollContainer,o=t*n,a=s,h=s+r,l=r/2;if(this.isDragging){const t=30;o+t>h?this.scrollContainer.scrollLeft+=t:o-t<a&&(this.scrollContainer.scrollLeft-=t)}else{(o<a||o>h)&&(this.scrollContainer.scrollLeft=o-(this.options.autoCenter?l:0));const t=o-s-l;if(e&&this.options.autoCenter&&t>0){const e=null===(i=this.audioData)||void 0===i?void 0:i.duration;if(void 0===e||e<=0)return void(this.scrollContainer.scrollLeft+=t);const s=n/e;this.scrollContainer.scrollLeft+=s<=600?Math.min(t,10):t}}}renderProgress(t,e){if(isNaN(t))return;const i=100*t;this.canvasWrapper.style.clipPath=`polygon(${i}% 0%, 100% 0%, 100% 100%, ${i}% 100%)`,this.progressWrapper.style.width=`${i}%`,this.cursor.style.left=`${i}%`,this.cursor.style.transform=this.options.cursorWidth?`translateX(-${t*this.options.cursorWidth}px)`:"",this.isScrollable&&this.options.autoScroll&&this.audioData&&this.audioData.duration>0&&this.scrollIntoView(t,e)}exportImage(t,e,s){return i(this,void 0,void 0,(function*(){const i=this.canvasWrapper.querySelectorAll("canvas");if(!i.length)throw new Error("No waveform data");if("dataURL"===s){const s=Array.from(i).map((i=>i.toDataURL(t,e)));return Promise.resolve(s)}return Promise.all(Array.from(i).map((i=>new Promise(((s,n)=>{i.toBlob((t=>{t?s(t):n(new Error("Could not export image"))}),t,e)})))))}))}}class C extends t{constructor(){super(...arguments),this.animationFrameId=null,this.isRunning=!1}start(){if(this.isRunning)return;this.isRunning=!0;const t=()=>{this.isRunning&&(this.emit("tick"),this.animationFrameId=requestAnimationFrame(t))};t()}stop(){this.isRunning=!1,null!==this.animationFrameId&&(cancelAnimationFrame(this.animationFrameId),this.animationFrameId=null)}destroy(){this.stop()}}class w extends t{constructor(t=new AudioContext){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,this.audioContext=t,this.gainNode=this.audioContext.createGain(),this.gainNode.connect(this.audioContext.destination)}load(){return i(this,void 0,void 0,(function*(){}))}get src(){return this.currentSrc}set src(t){if(this.currentSrc=t,this._duration=void 0,!t)return this.buffer=null,void this.emit("emptied");fetch(t).then((e=>{if(e.status>=400)throw new Error(`Failed to fetch ${t}: ${e.status} (${e.statusText})`);return e.arrayBuffer()})).then((e=>this.currentSrc!==t?null:this.audioContext.decodeAudioData(e))).then((e=>{this.currentSrc===t&&(this.buffer=e,this.emit("loadedmetadata"),this.emit("canplay"),this.autoplay&&this.play())})).catch((t=>{console.error("WebAudioPlayer load error:",t)}))}_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 t=this.playbackPosition;(t>=this.duration||t<0)&&(t=0,this.playbackPosition=0),this.bufferNode.start(this.audioContext.currentTime,t),this.playStartTime=this.audioContext.currentTime,this.bufferNode.onended=()=>{this.currentTime>=this.duration&&(this.pause(),this.emit("ended"))}}_pause(){var t;this.paused=!0,null===(t=this.bufferNode)||void 0===t||t.stop(),this.playbackPosition+=(this.audioContext.currentTime-this.playStartTime)*this._playbackRate}play(){return i(this,void 0,void 0,(function*(){this.paused&&(this._play(),this.emit("play"))}))}pause(){this.paused||(this._pause(),this.emit("pause"))}stopAt(t){const e=t-this.currentTime,i=this.bufferNode;null==i||i.stop(this.audioContext.currentTime+e),null==i||i.addEventListener("ended",(()=>{i===this.bufferNode&&(this.bufferNode=null,this.pause())}),{once:!0})}setSinkId(t){return i(this,void 0,void 0,(function*(){return this.audioContext.setSinkId(t)}))}get playbackRate(){return this._playbackRate}set playbackRate(t){const e=!this.paused;e&&this._pause(),this._playbackRate=t,e&&this._play(),this.bufferNode&&(this.bufferNode.playbackRate.value=t)}get currentTime(){return this.paused?this.playbackPosition:this.playbackPosition+(this.audioContext.currentTime-this.playStartTime)*this._playbackRate}set currentTime(t){const e=!this.paused;e&&this._pause(),this.playbackPosition=t,e&&this._play(),this.emit("seeking"),this.emit("timeupdate")}get duration(){var t,e;return null!==(t=this._duration)&&void 0!==t?t:(null===(e=this.buffer)||void 0===e?void 0:e.duration)||0}set duration(t){this._duration=t}get volume(){return this.gainNode.gain.value}set volume(t){this.gainNode.gain.value=t,this.emit("volumechange")}get muted(){return this._muted}set muted(t){this._muted!==t&&(this._muted=t,this._muted?this.gainNode.disconnect():this.gainNode.connect(this.audioContext.destination))}canPlayType(t){return/^(audio|video)\//.test(t)}getGainNode(){return this.gainNode}getChannelData(){const t=[];if(!this.buffer)return t;const e=this.buffer.numberOfChannels;for(let i=0;i<e;i++)t.push(this.buffer.getChannelData(i));return t}removeAttribute(t){switch(t){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}}}const P={waveColor:"#999",progressColor:"#555",cursorWidth:1,minPxPerSec:0,fillParent:!0,interact:!0,dragToSeek:!1,autoScroll:!0,autoCenter:!0,sampleRate:8e3};class M extends c{static create(t){return new M(t)}getState(){return this.wavesurferState}getRenderer(){return this.renderer}constructor(t){const e=t.media||("WebAudio"===t.backend?new w:void 0);super({media:e,mediaControls:t.mediaControls,autoplay:t.autoplay,playbackRate:t.audioRate}),this.plugins=[],this.decodedData=null,this.stopAtPosition=null,this.subscriptions=[],this.mediaSubscriptions=[],this.abortController=null,this.reactiveCleanups=[],this.options=Object.assign({},P,t);const{state:i,actions:s}=function(t){var e,i,s,n,r,o;const a=null!==(e=null==t?void 0:t.currentTime)&&void 0!==e?e:h(0),u=null!==(i=null==t?void 0:t.duration)&&void 0!==i?i:h(0),c=null!==(s=null==t?void 0:t.isPlaying)&&void 0!==s?s:h(!1),d=null!==(n=null==t?void 0:t.isSeeking)&&void 0!==n?n:h(!1),p=null!==(r=null==t?void 0:t.volume)&&void 0!==r?r:h(1),m=null!==(o=null==t?void 0:t.playbackRate)&&void 0!==o?o:h(1),v=h(null),g=h(null),f=h(""),b=h(0),y=h(0),S=l((()=>!c.value),[c]),C=l((()=>null!==v.value),[v]),w=l((()=>C.value&&u.value>0),[C,u]),P=l((()=>a.value),[a]),M=l((()=>u.value>0?a.value/u.value:0),[a,u]);return{state:{currentTime:a,duration:u,isPlaying:c,isPaused:S,isSeeking:d,volume:p,playbackRate:m,audioBuffer:v,peaks:g,url:f,zoom:b,scrollPosition:y,canPlay:C,isReady:w,progress:P,progressPercent:M},actions:{setCurrentTime:t=>{const e=Math.max(0,Math.min(u.value||1/0,t));a.set(e)},setDuration:t=>{u.set(Math.max(0,t))},setPlaying:t=>{c.set(t)},setSeeking:t=>{d.set(t)},setVolume:t=>{const e=Math.max(0,Math.min(1,t));p.set(e)},setPlaybackRate:t=>{const e=Math.max(.1,Math.min(16,t));m.set(e)},setAudioBuffer:t=>{v.set(t),t&&u.set(t.duration)},setPeaks:t=>{g.set(t)},setUrl:t=>{f.set(t)},setZoom:t=>{b.set(Math.max(0,t))},setScrollPosition:t=>{y.set(Math.max(0,t))}}}}({isPlaying:this.isPlayingSignal,currentTime:this.currentTimeSignal,duration:this.durationSignal,volume:this.volumeSignal,playbackRate:this.playbackRateSignal,isSeeking:this.seekingSignal});this.wavesurferState=i,this.wavesurferActions=s,this.timer=new C;const n=e?void 0:this.getMediaElement();this.renderer=new S(this.options,n),this.initPlayerEvents(),this.initRendererEvents(),this.initTimerEvents(),this.initReactiveState(),this.initPlugins();const r=this.options.url||this.getSrc()||"";Promise.resolve().then((()=>{this.emit("init");const{peaks:t,duration:e}=this.options;(r||t&&e)&&this.load(r,t,e).catch((t=>{this.emit("error",t instanceof Error?t:new Error(String(t)))}))}))}updateProgress(t=this.getCurrentTime()){return this.renderer.renderProgress(t/this.getDuration(),this.isPlaying()),t}initTimerEvents(){this.subscriptions.push(this.timer.on("tick",(()=>{if(!this.isSeeking()){const t=this.updateProgress();this.emit("timeupdate",t),this.emit("audioprocess",t),null!=this.stopAtPosition&&this.isPlaying()&&t>=this.stopAtPosition&&this.pause()}})))}initReactiveState(){this.reactiveCleanups.push(function(t,e){const i=[];i.push(u((()=>{const i=t.isPlaying.value;e.emit(i?"play":"pause")}),[t.isPlaying])),i.push(u((()=>{const i=t.currentTime.value;e.emit("timeupdate",i),t.isPlaying.value&&e.emit("audioprocess",i)}),[t.currentTime,t.isPlaying])),i.push(u((()=>{t.isSeeking.value&&e.emit("seeking",t.currentTime.value)}),[t.isSeeking,t.currentTime]));let s=!1;i.push(u((()=>{t.isReady.value&&!s&&(s=!0,e.emit("ready",t.duration.value))}),[t.isReady,t.duration]));let n=!1;return i.push(u((()=>{const i=t.isPlaying.value,s=t.currentTime.value,r=t.duration.value,o=r>0&&s>=r;n&&!i&&o&&e.emit("finish"),n=i&&o}),[t.isPlaying,t.currentTime,t.duration])),i.push(u((()=>{const i=t.zoom.value;i>0&&e.emit("zoom",i)}),[t.zoom])),()=>{i.forEach((t=>t()))}}(this.wavesurferState,{emit:this.emit.bind(this)}))}initPlayerEvents(){this.isPlaying()&&(this.emit("play"),this.timer.start()),this.mediaSubscriptions.push(this.onMediaEvent("timeupdate",(()=>{const t=this.updateProgress();this.emit("timeupdate",t)})),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",(()=>{var t;this.emit("error",null!==(t=this.getMediaElement().error)&&void 0!==t?t:new Error("Media error")),this.stopAtPosition=null})))}initRendererEvents(){this.subscriptions.push(this.renderer.on("click",((t,e)=>{this.options.interact&&(this.seekTo(t),this.emit("interaction",t*this.getDuration()),this.emit("click",t,e))})),this.renderer.on("dblclick",((t,e)=>{this.emit("dblclick",t,e)})),this.renderer.on("scroll",((t,e,i,s)=>{const n=this.getDuration();this.emit("scroll",t*n,e*n,i,s)})),this.renderer.on("render",(()=>{this.emit("redraw")})),this.renderer.on("rendered",(()=>{this.emit("redrawcomplete")})),this.renderer.on("dragstart",(t=>{this.emit("dragstart",t)})),this.renderer.on("dragend",(t=>{this.emit("dragend",t)})),this.renderer.on("resize",(()=>{this.emit("resize")})));{let t;const e=this.renderer.on("drag",(e=>{var i;if(!this.options.interact)return;this.renderer.renderProgress(e),clearTimeout(t);let s=0;const n=this.options.dragToSeek;this.isPlaying()?s=0:!0===n?s=200:n&&"object"==typeof n&&(s=null!==(i=n.debounceTime)&&void 0!==i?i:200),t=setTimeout((()=>{this.seekTo(e)}),s),this.emit("interaction",e*this.getDuration()),this.emit("drag",e)}));this.subscriptions.push((()=>{clearTimeout(t),e()}))}}initPlugins(){var t;(null===(t=this.options.plugins)||void 0===t?void 0:t.length)&&this.options.plugins.forEach((t=>{this.registerPlugin(t)}))}unsubscribePlayerEvents(){this.mediaSubscriptions.forEach((t=>t())),this.mediaSubscriptions=[]}setOptions(t){this.options=Object.assign({},this.options,t),t.duration&&!t.peaks&&(this.decodedData=s.createBuffer(this.exportPeaks(),t.duration)),t.peaks&&t.duration&&(this.decodedData=s.createBuffer(t.peaks,t.duration)),this.renderer.setOptions(this.options),t.audioRate&&this.setPlaybackRate(t.audioRate),null!=t.mediaControls&&(this.getMediaElement().controls=t.mediaControls)}registerPlugin(t){if(this.plugins.includes(t))return t;t._init(this),this.plugins.push(t);const e=t.once("destroy",(()=>{this.plugins=this.plugins.filter((e=>e!==t)),this.subscriptions=this.subscriptions.filter((t=>t!==e))}));return this.subscriptions.push(e),t}unregisterPlugin(t){this.plugins=this.plugins.filter((e=>e!==t)),t.destroy()}getWrapper(){return this.renderer.getWrapper()}getWidth(){return this.renderer.getWidth()}getScroll(){return this.renderer.getScroll()}setScroll(t){return this.renderer.setScroll(t)}setScrollTime(t){const e=t/this.getDuration();this.renderer.setScrollPercentage(e)}getActivePlugins(){return this.plugins}loadAudio(t,e,n,r){return i(this,void 0,void 0,(function*(){var i;if(this.emit("load",t),!this.options.media&&this.isPlaying()&&this.pause(),this.decodedData=null,this.stopAtPosition=null,null===(i=this.abortController)||void 0===i||i.abort(),this.abortController=null,!e&&!n){const i=this.options.fetchParams||{};window.AbortController&&!i.signal&&(this.abortController=new AbortController,i.signal=this.abortController.signal);const s=t=>this.emit("loading",t);e=yield a.fetchBlob(t,s,i);const n=this.options.blobMimeType;n&&(e=new Blob([e],{type:n}))}this.setSrc(t,e);const o=yield new Promise((t=>{const e=r||this.getDuration();e?t(e):this.mediaSubscriptions.push(this.onMediaEvent("loadedmetadata",(()=>t(this.getDuration())),{once:!0}))}));if(!t&&!e){const t=this.getMediaElement();t instanceof w&&(t.duration=o)}if(n)this.decodedData=s.createBuffer(n,o||0);else if(e){const t=yield e.arrayBuffer();this.decodedData=yield s.decode(t,this.options.sampleRate)}this.decodedData&&(this.emit("decode",this.getDuration()),this.renderer.render(this.decodedData)),this.emit("ready",this.getDuration())}))}load(t,e,s){return i(this,void 0,void 0,(function*(){try{return yield this.loadAudio(t,void 0,e,s)}catch(t){throw this.emit("error",t),t}}))}loadBlob(t,e,s){return i(this,void 0,void 0,(function*(){try{return yield this.loadAudio("",t,e,s)}catch(t){throw this.emit("error",t),t}}))}zoom(t){if(!this.decodedData)throw new Error("No audio loaded");this.renderer.zoom(t),this.emit("zoom",t)}getDecodedData(){return this.decodedData}exportPeaks({channels:t=2,maxLength:e=8e3,precision:i=1e4}={}){if(!this.decodedData)throw new Error("The audio has not been decoded yet");const s=Math.min(t,this.decodedData.numberOfChannels),n=[];for(let t=0;t<s;t++){const s=this.decodedData.getChannelData(t),r=[],o=s.length/e;for(let t=0;t<e;t++){const e=s.slice(Math.floor(t*o),Math.ceil((t+1)*o));let n=0;for(let t=0;t<e.length;t++){const i=e[t];Math.abs(i)>Math.abs(n)&&(n=i)}r.push(Math.round(n*i)/i)}n.push(r)}return n}getDuration(){let t=super.getDuration()||0;return 0!==t&&t!==1/0||!this.decodedData||(t=this.decodedData.duration),t}toggleInteraction(t){this.options.interact=t}setTime(t){this.stopAtPosition=null,super.setTime(t),this.updateProgress(t),this.emit("timeupdate",t)}seekTo(t){const e=this.getDuration()*t;this.setTime(e)}play(t,e){const s=Object.create(null,{play:{get:()=>super.play}});return i(this,void 0,void 0,(function*(){null!=t&&this.setTime(t);const i=yield s.play.call(this);return null!=e&&(this.media instanceof w?this.media.stopAt(e):this.stopAtPosition=e),i}))}playPause(){return i(this,void 0,void 0,(function*(){return this.isPlaying()?this.pause():this.play()}))}stop(){this.pause(),this.setTime(0)}skip(t){this.setTime(this.getCurrentTime()+t)}empty(){this.load("",[[0]],.001)}setMediaElement(t){this.unsubscribePlayerEvents(),super.setMediaElement(t),this.initPlayerEvents()}exportImage(){return i(this,arguments,void 0,(function*(t="image/png",e=1,i="dataURL"){return this.renderer.exportImage(t,e,i)}))}destroy(){var t;this.emit("destroy"),null===(t=this.abortController)||void 0===t||t.abort(),this.plugins.forEach((t=>t.destroy())),this.subscriptions.forEach((t=>t())),this.unsubscribePlayerEvents(),this.reactiveCleanups.forEach((t=>t())),this.reactiveCleanups=[],this.timer.destroy(),this.renderer.destroy(),super.destroy()}}M.BasePlugin=e,M.dom=o;const E={height:50,overlayColor:"rgba(100, 100, 100, 0.1)",insertPosition:"afterend"};class k extends e{constructor(t){super(t),this.miniWavesurfer=null,this.miniSubscriptions=[],this.container=null,this.isInitializing=!1,this.dragTimeout=null,this.options=Object.assign({},E,t),this.minimapWrapper=this.initMinimapWrapper(),this.overlay=this.initOverlay()}static create(t){return new k(t)}onInit(){var t,e;if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");this.options.container?("string"==typeof this.options.container?this.container=document.querySelector(this.options.container):this.options.container instanceof HTMLElement&&(this.container=this.options.container),null===(t=this.container)||void 0===t||t.appendChild(this.minimapWrapper)):(this.container=this.wavesurfer.getWrapper().parentElement,null===(e=this.container)||void 0===e||e.insertAdjacentElement(this.options.insertPosition,this.minimapWrapper)),this.initWaveSurferEvents(),Promise.resolve().then((()=>{this.initMinimap()}))}initMinimapWrapper(){return r("div",{part:"minimap",style:{position:"relative"}})}initOverlay(){return r("div",{part:"minimap-overlay",style:{position:"absolute",zIndex:"2",left:"0",top:"0",bottom:"0",transition:"left 100ms ease-out, width 100ms ease-out",pointerEvents:"none",backgroundColor:this.options.overlayColor}},this.minimapWrapper)}initMinimap(){if(this.isInitializing)return;if(this.isInitializing=!0,this.destroyMinimap(),!this.wavesurfer)return void(this.isInitializing=!1);const t=this.wavesurfer.getDecodedData();if(!t)return void(this.isInitializing=!1);const e=[];for(let i=0;i<t.numberOfChannels;i++)e.push(t.getChannelData(i));this.miniWavesurfer=M.create(Object.assign(Object.assign({},this.options),{container:this.minimapWrapper,minPxPerSec:0,fillParent:!0,url:void 0,media:void 0,peaks:e,duration:t.duration})),this.syncMinimapPosition(this.wavesurfer.getCurrentTime()),this.miniSubscriptions.push(this.miniWavesurfer.on("audioprocess",(t=>{this.emit("audioprocess",t)})),this.miniWavesurfer.on("click",((t,e)=>{var i;null===(i=this.wavesurfer)||void 0===i||i.seekTo(t),this.emit("click",t,e)})),this.miniWavesurfer.on("dblclick",((t,e)=>{this.emit("dblclick",t,e)})),this.miniWavesurfer.on("decode",(t=>{this.emit("decode",t)})),this.miniWavesurfer.on("destroy",(()=>{this.emit("destroy")})),this.miniWavesurfer.on("drag",(t=>{this.onMinimapDrag(t),this.emit("drag",t)})),this.miniWavesurfer.on("dragend",(t=>{this.emit("dragend",t)})),this.miniWavesurfer.on("dragstart",(t=>{this.emit("dragstart",t)})),this.miniWavesurfer.on("interaction",(()=>{this.emit("interaction")})),this.miniWavesurfer.on("init",(()=>{this.emit("init")})),this.miniWavesurfer.on("ready",(()=>{var t;this.syncMinimapPosition((null===(t=this.wavesurfer)||void 0===t?void 0:t.getCurrentTime())||0),this.emit("ready")})),this.miniWavesurfer.on("redraw",(()=>{this.emit("redraw")})),this.miniWavesurfer.on("redrawcomplete",(()=>{this.emit("redrawcomplete")})),this.miniWavesurfer.on("seeking",(t=>{this.emit("seeking",t)})),this.miniWavesurfer.on("timeupdate",(t=>{this.emit("timeupdate",t)}))),this.isInitializing=!1}updateOverlay(t,e){if(!this.wavesurfer)return;const i=this.wavesurfer.getDuration();if(!i)return;if(void 0===t||void 0===e){const s=this.wavesurfer.getWrapper().clientWidth||1,n=this.wavesurfer.getWidth(),r=this.wavesurfer.getScroll();t=r/s*i,e=(r+n)/s*i}const s=Math.min(Math.max(t,0),i),n=s/i*100,r=(Math.min(Math.max(e,s),i)-s)/i*100;this.overlay.style.left=`${n}%`,this.overlay.style.width=`${Math.min(r,100-n)}%`}destroyMinimap(){const t=this.miniWavesurfer;this.miniWavesurfer=null,null==t||t.destroy(),this.miniSubscriptions.forEach((t=>t())),this.miniSubscriptions=[],this.dragTimeout&&(clearTimeout(this.dragTimeout),this.dragTimeout=null)}renderMainProgress(t){this.wavesurfer&&this.wavesurfer.getRenderer().renderProgress(t,this.wavesurfer.isPlaying())}renderMinimapProgress(t){this.wavesurfer&&this.miniWavesurfer&&this.miniWavesurfer.getRenderer().renderProgress(t,this.wavesurfer.isPlaying())}syncMinimapPosition(t){if(!this.wavesurfer||!this.miniWavesurfer)return;const e=this.wavesurfer.getDuration();e&&(this.miniWavesurfer.getDuration()?this.miniWavesurfer.setTime(t):this.renderMinimapProgress(t/e))}onMinimapDrag(t){var e;if(!this.wavesurfer)return;this.renderMainProgress(t),this.dragTimeout&&clearTimeout(this.dragTimeout);let i=0;const s=this.options.dragToSeek;this.wavesurfer.isPlaying()||!0!==s?!this.wavesurfer.isPlaying()&&s&&"object"==typeof s&&(i=null!==(e=s.debounceTime)&&void 0!==e?e:200):i=200,this.dragTimeout=setTimeout((()=>{var e;null===(e=this.wavesurfer)||void 0===e||e.seekTo(t),this.dragTimeout=null}),i)}onRedraw(){this.updateOverlay()}onScroll(t,e){this.updateOverlay(t,e)}initWaveSurferEvents(){this.wavesurfer&&this.subscriptions.push(this.wavesurfer.on("decode",(()=>{this.initMinimap()})),this.wavesurfer.on("timeupdate",(t=>{this.syncMinimapPosition(t)})),this.wavesurfer.on("drag",(t=>{this.renderMinimapProgress(t)})),this.wavesurfer.on("scroll",((t,e)=>{this.onScroll(t,e)})),this.wavesurfer.on("redraw",(()=>{this.onRedraw()})))}destroy(){this.destroyMinimap(),this.minimapWrapper.remove(),this.container=null,super.destroy()}}export{k as default};
|