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
|
@@ -42,12 +42,25 @@ const createMainWaveSurfer = (dragToSeek = true) => {
|
|
|
42
42
|
const wrapper = document.createElement('div');
|
|
43
43
|
const renderer = { renderProgress: jest.fn() };
|
|
44
44
|
const channelData = new Float32Array([0, 1, -1]);
|
|
45
|
+
let scroll = 0;
|
|
46
|
+
let width = 200;
|
|
47
|
+
let wrapperWidth = 500;
|
|
48
|
+
Object.defineProperty(wrapper, 'clientWidth', {
|
|
49
|
+
configurable: true,
|
|
50
|
+
get: () => wrapperWidth,
|
|
51
|
+
});
|
|
45
52
|
wrapperParent.appendChild(wrapper);
|
|
46
|
-
return Object.assign(Object.assign({}, emitter), {
|
|
53
|
+
return Object.assign(Object.assign({}, emitter), { __setScroll: (value) => {
|
|
54
|
+
scroll = value;
|
|
55
|
+
}, __setWidth: (value) => {
|
|
56
|
+
width = value;
|
|
57
|
+
}, __setWrapperWidth: (value) => {
|
|
58
|
+
wrapperWidth = value;
|
|
59
|
+
}, options: { dragToSeek }, getCurrentTime: jest.fn(() => 12), getDecodedData: jest.fn(() => ({
|
|
47
60
|
duration: 30,
|
|
48
61
|
numberOfChannels: 1,
|
|
49
62
|
getChannelData: jest.fn(() => channelData),
|
|
50
|
-
})), getDuration: jest.fn(() => 30), getRenderer: jest.fn(() => renderer), getWrapper: jest.fn(() => wrapper), isPlaying: jest.fn(() => false), seekTo: jest.fn(), renderer });
|
|
63
|
+
})), getDuration: jest.fn(() => 30), getRenderer: jest.fn(() => renderer), getScroll: jest.fn(() => scroll), getWidth: jest.fn(() => width), getWrapper: jest.fn(() => wrapper), isPlaying: jest.fn(() => false), seekTo: jest.fn(), renderer });
|
|
51
64
|
};
|
|
52
65
|
const createMock = WaveSurfer.create;
|
|
53
66
|
describe('MinimapPlugin', () => {
|
|
@@ -89,4 +102,27 @@ describe('MinimapPlugin', () => {
|
|
|
89
102
|
jest.advanceTimersByTime(200);
|
|
90
103
|
expect(mainWaveSurfer.seekTo).toHaveBeenLastCalledWith(0.75);
|
|
91
104
|
}));
|
|
105
|
+
test('keeps the overlay contained while redraw and scroll updates are catching up during zoom-out', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
106
|
+
const miniWaveSurfer = createMiniWaveSurfer();
|
|
107
|
+
const mainWaveSurfer = createMainWaveSurfer();
|
|
108
|
+
createMock.mockReturnValue(miniWaveSurfer);
|
|
109
|
+
const plugin = MinimapPlugin.create({ height: 20 });
|
|
110
|
+
plugin._init(mainWaveSurfer);
|
|
111
|
+
yield Promise.resolve();
|
|
112
|
+
const minimapContainer = createMock.mock.calls[0][0].container;
|
|
113
|
+
const overlay = minimapContainer.querySelector('[part="minimap-overlay"]');
|
|
114
|
+
expect(overlay.style.transition).toBe('left 100ms ease-out, width 100ms ease-out');
|
|
115
|
+
mainWaveSurfer.__setScroll(180);
|
|
116
|
+
mainWaveSurfer.emit('redraw');
|
|
117
|
+
mainWaveSurfer.emit('scroll', 10.8, 22.8, 180, 380);
|
|
118
|
+
mainWaveSurfer.__setScroll(300);
|
|
119
|
+
mainWaveSurfer.__setWrapperWidth(400);
|
|
120
|
+
mainWaveSurfer.emit('redraw');
|
|
121
|
+
expect(overlay.style.left).toBe('75%');
|
|
122
|
+
expect(overlay.style.width).toBe('25%');
|
|
123
|
+
mainWaveSurfer.__setScroll(200);
|
|
124
|
+
mainWaveSurfer.emit('scroll', 15, 30, 200, 400);
|
|
125
|
+
expect(overlay.style.left).toBe('50%');
|
|
126
|
+
expect(overlay.style.width).toBe('50%');
|
|
127
|
+
}));
|
|
92
128
|
});
|
|
@@ -161,6 +161,39 @@ describe('Renderer', () => {
|
|
|
161
161
|
renderer.scrollIntoView(0.8);
|
|
162
162
|
expect(renderer.getScroll()).toBeGreaterThanOrEqual(0);
|
|
163
163
|
});
|
|
164
|
+
test('renderProgress clamps only at low zoom when auto-centering', () => {
|
|
165
|
+
;
|
|
166
|
+
renderer.options.autoScroll = true;
|
|
167
|
+
renderer.options.autoCenter = true;
|
|
168
|
+
renderer.isScrollable = true;
|
|
169
|
+
const viewportWidth = 100;
|
|
170
|
+
const lowZoomDuration = 2;
|
|
171
|
+
const lowZoomScrollWidth = 200;
|
|
172
|
+
const highZoomDuration = 1;
|
|
173
|
+
const highZoomScrollWidth = 800;
|
|
174
|
+
Object.defineProperty(renderer.scrollContainer, 'clientWidth', {
|
|
175
|
+
configurable: true,
|
|
176
|
+
value: viewportWidth,
|
|
177
|
+
});
|
|
178
|
+
renderer.audioData = { duration: lowZoomDuration };
|
|
179
|
+
Object.defineProperty(renderer.scrollContainer, 'scrollWidth', {
|
|
180
|
+
configurable: true,
|
|
181
|
+
value: lowZoomScrollWidth,
|
|
182
|
+
});
|
|
183
|
+
renderer.setScroll(0);
|
|
184
|
+
renderer.renderProgress(0.35, true);
|
|
185
|
+
// 200 / 2 = 100 px/s, so low zoom keeps Math.min(20, 10) = 10
|
|
186
|
+
expect(renderer.getScroll()).toBe(10);
|
|
187
|
+
renderer.audioData = { duration: highZoomDuration };
|
|
188
|
+
Object.defineProperty(renderer.scrollContainer, 'scrollWidth', {
|
|
189
|
+
configurable: true,
|
|
190
|
+
value: highZoomScrollWidth,
|
|
191
|
+
});
|
|
192
|
+
renderer.setScroll(0);
|
|
193
|
+
renderer.renderProgress(0.0875, true);
|
|
194
|
+
// 800 / 1 = 800 px/s, so high zoom applies no clamp and scrolls by the full center offset
|
|
195
|
+
expect(renderer.getScroll()).toBe(20);
|
|
196
|
+
});
|
|
164
197
|
test('renderProgress updates styles', () => {
|
|
165
198
|
renderer.renderProgress(0.5);
|
|
166
199
|
expect(renderer.progressWrapper.style.width).toBe('50%');
|
|
@@ -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){const s=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,o]of Object.entries(e))if("children"===t&&o)for(const[t,e]of Object.entries(o))e instanceof Node?s.appendChild(e):"string"==typeof e?s.appendChild(document.createTextNode(e)):s.appendChild(i(t,e));else"style"===t?Object.assign(s.style,o):"textContent"===t?s.textContent=o:s.setAttribute(t,o.toString());return s}function s(t,e,s){const o=i(t,e||{});return null==s||s.appendChild(o),o}function o(t,e){let i;const s=()=>{i&&(i(),i=void 0),i=t()},o=e.map((t=>t.subscribe(s)));return s(),()=>{i&&(i(),i=void 0),o.forEach((t=>t()))}}function n(t,e={}){const{threshold:i=3,mouseButton:s=0,touchDelay:o=100}=e,n=function(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))}}(null),r=new Map,l=matchMedia("(pointer: coarse)").matches;let h=()=>{};const a=e=>{if(e.button!==s)return;if(r.set(e.pointerId,e),r.size>1)return;let a=e.clientX,u=e.clientY,c=!1;const d=Date.now(),p=t.getBoundingClientRect(),{left:v,top:m}=p,g=t=>{if(t.defaultPrevented||r.size>1)return;if(l&&Date.now()-d<o)return;const e=t.clientX,s=t.clientY,h=e-a,p=s-u;(c||Math.abs(h)>i||Math.abs(p)>i)&&(t.preventDefault(),t.stopPropagation(),c||(n.set({type:"start",x:a-v,y:u-m}),c=!0),n.set({type:"move",x:e-v,y:s-m,deltaX:h,deltaY:p}),a=e,u=s)},f=t=>{if(r.delete(t.pointerId),c){const e=t.clientX,i=t.clientY;n.set({type:"end",x:e-v,y:i-m})}h()},y=t=>{r.delete(t.pointerId),t.relatedTarget&&t.relatedTarget!==document.documentElement||f(t)},P=t=>{c&&(t.stopPropagation(),t.preventDefault())},w=t=>{t.defaultPrevented||r.size>1||c&&t.preventDefault()};document.addEventListener("pointermove",g),document.addEventListener("pointerup",f),document.addEventListener("pointerout",y),document.addEventListener("pointercancel",y),document.addEventListener("touchmove",w,{passive:!1}),document.addEventListener("click",P,{capture:!0}),h=()=>{document.removeEventListener("pointermove",g),document.removeEventListener("pointerup",f),document.removeEventListener("pointerout",y),document.removeEventListener("pointercancel",y),document.removeEventListener("touchmove",w),setTimeout((()=>{document.removeEventListener("click",P,{capture:!0})}),10)}};t.addEventListener("pointerdown",a);return{signal:n,cleanup:()=>{h(),t.removeEventListener("pointerdown",a),r.clear(),function(t){const e=t._cleanup;"function"==typeof e&&e()}(n)}}}const r={points:[],lineWidth:4,lineColor:"rgba(0, 0, 255, 0.5)",dragPointSize:10,dragPointFill:"rgba(255, 255, 255, 0.8)",dragPointStroke:"rgba(255, 255, 255, 0.8)"};class l extends t{constructor(t,e){super(),this.subscriptions=[],this.subscriptions=[],this.options=t,this.polyPoints=new Map;const i=e.clientWidth,r=e.clientHeight,l=s("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",viewBox:`0 0 ${i} ${r}`,preserveAspectRatio:"none",style:{position:"absolute",left:"0",top:"0",zIndex:"4"},part:"envelope"},e);this.svg=l;const h=s("polyline",{xmlns:"http://www.w3.org/2000/svg",points:`0,${r} ${i},${r}`,stroke:t.lineColor,"stroke-width":t.lineWidth,fill:"none",part:"polyline",style:t.dragLine?{cursor:"row-resize",pointerEvents:"stroke"}:{}},l);if(t.dragLine){const t=n(h),e=o((()=>{const e=t.signal.value;if(!e||"move"!==e.type||void 0===e.deltaY)return;const i=e.deltaY,{height:s}=l.viewBox.baseVal,{points:o}=h;for(let t=1;t<o.numberOfItems-1;t++){const e=o.getItem(t);e.y=Math.min(s,Math.max(0,e.y+i))}const n=l.querySelectorAll("ellipse");Array.from(n).forEach((t=>{const e=Math.min(s,Math.max(0,Number(t.getAttribute("cy"))+i));t.setAttribute("cy",e.toString())})),this.emit("line-move",i/s)}),[t.signal]);this.subscriptions.push((()=>{e(),t.cleanup()}))}this.dblClickListener=t=>{const e=l.getBoundingClientRect(),i=t.clientX-e.left,s=t.clientY-e.top;this.emit("point-create",i/e.width,s/e.height)},l.addEventListener("dblclick",this.dblClickListener);const a=()=>{void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0)};this.touchStartListener=t=>{1===t.touches.length?this.pressTimer=window.setTimeout((()=>{t.preventDefault();const e=l.getBoundingClientRect(),i=t.touches[0].clientX-e.left,s=t.touches[0].clientY-e.top;this.emit("point-create",i/e.width,s/e.height)}),500):a()},this.touchMoveListener=a,this.touchEndListener=a,l.addEventListener("touchstart",this.touchStartListener),l.addEventListener("touchmove",this.touchMoveListener),l.addEventListener("touchend",this.touchEndListener)}makeDraggable(t,e){const i=n(t,{threshold:1}),s=o((()=>{const s=i.signal.value;s&&("start"===s.type?t.style.cursor="grabbing":"move"===s.type&&void 0!==s.deltaX&&void 0!==s.deltaY?e(s.deltaX,s.deltaY):"end"===s.type&&(t.style.cursor="grab"))}),[i.signal]);this.subscriptions.push((()=>{s(),i.cleanup()}))}createCircle(t,e){const i=this.options.dragPointSize/2;return s("ellipse",{xmlns:"http://www.w3.org/2000/svg",cx:t,cy:e,rx:i,ry:i,fill:this.options.dragPointFill,stroke:this.options.dragPointStroke,"stroke-width":"2",style:{cursor:"grab",pointerEvents:"all"},part:"envelope-circle"},this.svg)}removePolyPoint(t){const e=this.polyPoints.get(t);if(!e)return;const{polyPoint:i,circle:s}=e,{points:o}=this.svg.querySelector("polyline"),n=Array.from(o).findIndex((t=>t.x===i.x&&t.y===i.y));o.removeItem(n),s.remove(),this.polyPoints.delete(t)}addPolyPoint(t,e,i){const{svg:s}=this,{width:o,height:n}=s.viewBox.baseVal,r=t*o,l=n-e*n,h=this.options.dragPointSize/2,a=s.createSVGPoint();a.x=t*o,a.y=n-e*n;const u=this.createCircle(r,l),{points:c}=s.querySelector("polyline"),d=Array.from(c).findIndex((t=>t.x>=r));c.insertItemBefore(a,Math.max(d,1)),this.polyPoints.set(i,{polyPoint:a,circle:u}),this.makeDraggable(u,((t,e)=>{const s=a.x+t,r=a.y+e;if(s<-h||r<-h||s>o+h||r>n+h)return void this.emit("point-dragout",i);const l=Array.from(c).find((t=>t.x>a.x)),d=Array.from(c).findLast((t=>t.x<a.x));l&&s>=l.x||d&&s<=d.x||(a.x=s,a.y=r,u.setAttribute("cx",s.toString()),u.setAttribute("cy",r.toString()),this.emit("point-move",i,s/o,r/n))}))}update(){const{svg:t}=this,{clientWidth:e,clientHeight:i}=t;if(!e||!i)return;const s=t.viewBox.baseVal.width/e,o=t.viewBox.baseVal.height/i;t.querySelectorAll("ellipse").forEach((t=>{const e=this.options.dragPointSize/2,i=e*s,n=e*o;t.setAttribute("rx",i.toString()),t.setAttribute("ry",n.toString())}))}destroy(){void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0),this.dblClickListener&&(this.svg.removeEventListener("dblclick",this.dblClickListener),this.dblClickListener=void 0),this.touchStartListener&&(this.svg.removeEventListener("touchstart",this.touchStartListener),this.touchStartListener=void 0),this.touchMoveListener&&(this.svg.removeEventListener("touchmove",this.touchMoveListener),this.touchMoveListener=void 0),this.touchEndListener&&(this.svg.removeEventListener("touchend",this.touchEndListener),this.touchEndListener=void 0),this.subscriptions.forEach((t=>t())),this.polyPoints.clear(),this.svg.remove()}}class h extends e{constructor(t){super(t),this.polyline=null,this.throttleTimeout=null,this.volume=1,this.points=t.points||[],this.options=Object.assign({},r,t),this.options.lineColor=this.options.lineColor||r.lineColor,this.options.dragPointFill=this.options.dragPointFill||r.dragPointFill,this.options.dragPointStroke=this.options.dragPointStroke||r.dragPointStroke,this.options.dragPointSize=this.options.dragPointSize||r.dragPointSize}static create(t){return new h(t)}addPoint(t){var e;t.id||(t.id=Math.random().toString(36).slice(2));const i=this.points.findLastIndex((e=>e.time<t.time));this.points.splice(i+1,0,t),this.emitPoints();const s=null===(e=this.wavesurfer)||void 0===e?void 0:e.getDuration();s&&this.addPolyPoint(t,s)}removePoint(t){var e;const i=this.points.indexOf(t);i>-1&&(this.points.splice(i,1),null===(e=this.polyline)||void 0===e||e.removePolyPoint(t),this.emitPoints())}getPoints(){return this.points}setPoints(t){this.points.slice().forEach((t=>this.removePoint(t))),t.forEach((t=>this.addPoint(t)))}destroy(){var t;this.throttleTimeout&&(clearTimeout(this.throttleTimeout),this.throttleTimeout=null),null===(t=this.polyline)||void 0===t||t.destroy(),super.destroy()}getCurrentVolume(){return this.volume}setVolume(t){var e;this.volume=t,null===(e=this.wavesurfer)||void 0===e||e.setVolume(t)}onInit(){var t;if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const{options:e}=this;e.volume=null!==(t=e.volume)&&void 0!==t?t:this.wavesurfer.getVolume(),this.setVolume(e.volume),this.subscriptions.push(this.wavesurfer.on("decode",(t=>{this.initPolyline(),this.points.forEach((e=>{this.addPolyPoint(e,t)}))})),this.wavesurfer.on("redraw",(()=>{var t;null===(t=this.polyline)||void 0===t||t.update()})),this.wavesurfer.on("timeupdate",(t=>{this.onTimeUpdate(t)})))}emitPoints(){this.throttleTimeout&&clearTimeout(this.throttleTimeout),this.throttleTimeout=setTimeout((()=>{this.emit("points-change",this.points)}),200)}initPolyline(){if(this.polyline&&this.polyline.destroy(),!this.wavesurfer)return;const t=this.wavesurfer.getWrapper();this.polyline=new l(this.options,t),this.subscriptions.push(this.polyline.on("point-move",((t,e,i)=>{var s;const o=(null===(s=this.wavesurfer)||void 0===s?void 0:s.getDuration())||0;t.time=e*o,t.volume=1-i,this.emitPoints()})),this.polyline.on("point-dragout",(t=>{this.removePoint(t)})),this.polyline.on("point-create",((t,e)=>{var i;this.addPoint({time:t*((null===(i=this.wavesurfer)||void 0===i?void 0:i.getDuration())||0),volume:1-e})})),this.polyline.on("line-move",(t=>{var e;this.points.forEach((e=>{e.volume=Math.min(1,Math.max(0,e.volume-t))})),this.emitPoints(),this.onTimeUpdate((null===(e=this.wavesurfer)||void 0===e?void 0:e.getCurrentTime())||0)})))}addPolyPoint(t,e){var i;null===(i=this.polyline)||void 0===i||i.addPolyPoint(t.time/e,t.volume,t)}onTimeUpdate(t){if(!this.wavesurfer)return;let e=this.points.find((e=>e.time>t));e||(e={time:this.wavesurfer.getDuration()||0,volume:0});let i=this.points.findLast((e=>e.time<=t));i||(i={time:0,volume:0});const s=e.time-i.time,o=e.volume-i.volume,n=i.volume+(t-i.time)*(o/s),r=Math.min(1,Math.max(0,n)),l=Math.round(100*r)/100;l!==this.getCurrentVolume()&&(this.setVolume(l),this.emit("volume-change",l))}}module.exports=h;
|
|
@@ -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){const s=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,o]of Object.entries(e))if("children"===t&&o)for(const[t,e]of Object.entries(o))e instanceof Node?s.appendChild(e):"string"==typeof e?s.appendChild(document.createTextNode(e)):s.appendChild(i(t,e));else"style"===t?Object.assign(s.style,o):"textContent"===t?s.textContent=o:s.setAttribute(t,o.toString());return s}function s(t,e,s){const o=i(t,e||{});return null==s||s.appendChild(o),o}function o(t,e){let i;const s=()=>{i&&(i(),i=void 0),i=t()},o=e.map((t=>t.subscribe(s)));return s(),()=>{i&&(i(),i=void 0),o.forEach((t=>t()))}}function n(t,e={}){const{threshold:i=3,mouseButton:s=0,touchDelay:o=100}=e,n=function(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))}}(null),r=new Map,l=matchMedia("(pointer: coarse)").matches;let h=()=>{};const a=e=>{if(e.button!==s)return;if(r.set(e.pointerId,e),r.size>1)return;let a=e.clientX,u=e.clientY,c=!1;const d=Date.now(),p=t.getBoundingClientRect(),{left:v,top:m}=p,g=t=>{if(t.defaultPrevented||r.size>1)return;if(l&&Date.now()-d<o)return;const e=t.clientX,s=t.clientY,h=e-a,p=s-u;(c||Math.abs(h)>i||Math.abs(p)>i)&&(t.preventDefault(),t.stopPropagation(),c||(n.set({type:"start",x:a-v,y:u-m}),c=!0),n.set({type:"move",x:e-v,y:s-m,deltaX:h,deltaY:p}),a=e,u=s)},f=t=>{if(r.delete(t.pointerId),c){const e=t.clientX,i=t.clientY;n.set({type:"end",x:e-v,y:i-m})}h()},y=t=>{r.delete(t.pointerId),t.relatedTarget&&t.relatedTarget!==document.documentElement||f(t)},P=t=>{c&&(t.stopPropagation(),t.preventDefault())},w=t=>{t.defaultPrevented||r.size>1||c&&t.preventDefault()};document.addEventListener("pointermove",g),document.addEventListener("pointerup",f),document.addEventListener("pointerout",y),document.addEventListener("pointercancel",y),document.addEventListener("touchmove",w,{passive:!1}),document.addEventListener("click",P,{capture:!0}),h=()=>{document.removeEventListener("pointermove",g),document.removeEventListener("pointerup",f),document.removeEventListener("pointerout",y),document.removeEventListener("pointercancel",y),document.removeEventListener("touchmove",w),setTimeout((()=>{document.removeEventListener("click",P,{capture:!0})}),10)}};t.addEventListener("pointerdown",a);return{signal:n,cleanup:()=>{h(),t.removeEventListener("pointerdown",a),r.clear(),function(t){const e=t._cleanup;"function"==typeof e&&e()}(n)}}}const r={points:[],lineWidth:4,lineColor:"rgba(0, 0, 255, 0.5)",dragPointSize:10,dragPointFill:"rgba(255, 255, 255, 0.8)",dragPointStroke:"rgba(255, 255, 255, 0.8)"};class l extends t{constructor(t,e){super(),this.subscriptions=[],this.subscriptions=[],this.options=t,this.polyPoints=new Map;const i=e.clientWidth,r=e.clientHeight,l=s("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",viewBox:`0 0 ${i} ${r}`,preserveAspectRatio:"none",style:{position:"absolute",left:"0",top:"0",zIndex:"4"},part:"envelope"},e);this.svg=l;const h=s("polyline",{xmlns:"http://www.w3.org/2000/svg",points:`0,${r} ${i},${r}`,stroke:t.lineColor,"stroke-width":t.lineWidth,fill:"none",part:"polyline",style:t.dragLine?{cursor:"row-resize",pointerEvents:"stroke"}:{}},l);if(t.dragLine){const t=n(h),e=o((()=>{const e=t.signal.value;if(!e||"move"!==e.type||void 0===e.deltaY)return;const i=e.deltaY,{height:s}=l.viewBox.baseVal,{points:o}=h;for(let t=1;t<o.numberOfItems-1;t++){const e=o.getItem(t);e.y=Math.min(s,Math.max(0,e.y+i))}const n=l.querySelectorAll("ellipse");Array.from(n).forEach((t=>{const e=Math.min(s,Math.max(0,Number(t.getAttribute("cy"))+i));t.setAttribute("cy",e.toString())})),this.emit("line-move",i/s)}),[t.signal]);this.subscriptions.push((()=>{e(),t.cleanup()}))}this.dblClickListener=t=>{const e=l.getBoundingClientRect(),i=t.clientX-e.left,s=t.clientY-e.top;this.emit("point-create",i/e.width,s/e.height)},l.addEventListener("dblclick",this.dblClickListener);const a=()=>{void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0)};this.touchStartListener=t=>{1===t.touches.length?this.pressTimer=window.setTimeout((()=>{t.preventDefault();const e=l.getBoundingClientRect(),i=t.touches[0].clientX-e.left,s=t.touches[0].clientY-e.top;this.emit("point-create",i/e.width,s/e.height)}),500):a()},this.touchMoveListener=a,this.touchEndListener=a,l.addEventListener("touchstart",this.touchStartListener),l.addEventListener("touchmove",this.touchMoveListener),l.addEventListener("touchend",this.touchEndListener)}makeDraggable(t,e){const i=n(t,{threshold:1}),s=o((()=>{const s=i.signal.value;s&&("start"===s.type?t.style.cursor="grabbing":"move"===s.type&&void 0!==s.deltaX&&void 0!==s.deltaY?e(s.deltaX,s.deltaY):"end"===s.type&&(t.style.cursor="grab"))}),[i.signal]);this.subscriptions.push((()=>{s(),i.cleanup()}))}createCircle(t,e){const i=this.options.dragPointSize/2;return s("ellipse",{xmlns:"http://www.w3.org/2000/svg",cx:t,cy:e,rx:i,ry:i,fill:this.options.dragPointFill,stroke:this.options.dragPointStroke,"stroke-width":"2",style:{cursor:"grab",pointerEvents:"all"},part:"envelope-circle"},this.svg)}removePolyPoint(t){const e=this.polyPoints.get(t);if(!e)return;const{polyPoint:i,circle:s}=e,{points:o}=this.svg.querySelector("polyline"),n=Array.from(o).findIndex((t=>t.x===i.x&&t.y===i.y));o.removeItem(n),s.remove(),this.polyPoints.delete(t)}addPolyPoint(t,e,i){const{svg:s}=this,{width:o,height:n}=s.viewBox.baseVal,r=t*o,l=n-e*n,h=this.options.dragPointSize/2,a=s.createSVGPoint();a.x=t*o,a.y=n-e*n;const u=this.createCircle(r,l),{points:c}=s.querySelector("polyline"),d=Array.from(c).findIndex((t=>t.x>=r));c.insertItemBefore(a,Math.max(d,1)),this.polyPoints.set(i,{polyPoint:a,circle:u}),this.makeDraggable(u,((t,e)=>{const s=a.x+t,r=a.y+e;if(s<-h||r<-h||s>o+h||r>n+h)return void this.emit("point-dragout",i);const l=Array.from(c).find((t=>t.x>a.x)),d=Array.from(c).findLast((t=>t.x<a.x));l&&s>=l.x||d&&s<=d.x||(a.x=s,a.y=r,u.setAttribute("cx",s.toString()),u.setAttribute("cy",r.toString()),this.emit("point-move",i,s/o,r/n))}))}update(){const{svg:t}=this,{clientWidth:e,clientHeight:i}=t;if(!e||!i)return;const s=t.viewBox.baseVal.width/e,o=t.viewBox.baseVal.height/i;t.querySelectorAll("ellipse").forEach((t=>{const e=this.options.dragPointSize/2,i=e*s,n=e*o;t.setAttribute("rx",i.toString()),t.setAttribute("ry",n.toString())}))}destroy(){void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0),this.dblClickListener&&(this.svg.removeEventListener("dblclick",this.dblClickListener),this.dblClickListener=void 0),this.touchStartListener&&(this.svg.removeEventListener("touchstart",this.touchStartListener),this.touchStartListener=void 0),this.touchMoveListener&&(this.svg.removeEventListener("touchmove",this.touchMoveListener),this.touchMoveListener=void 0),this.touchEndListener&&(this.svg.removeEventListener("touchend",this.touchEndListener),this.touchEndListener=void 0),this.subscriptions.forEach((t=>t())),this.polyPoints.clear(),this.svg.remove()}}class h extends e{constructor(t){super(t),this.polyline=null,this.throttleTimeout=null,this.volume=1,this.points=t.points||[],this.options=Object.assign({},r,t),this.options.lineColor=this.options.lineColor||r.lineColor,this.options.dragPointFill=this.options.dragPointFill||r.dragPointFill,this.options.dragPointStroke=this.options.dragPointStroke||r.dragPointStroke,this.options.dragPointSize=this.options.dragPointSize||r.dragPointSize}static create(t){return new h(t)}addPoint(t){var e;t.id||(t.id=Math.random().toString(36).slice(2));const i=this.points.findLastIndex((e=>e.time<t.time));this.points.splice(i+1,0,t),this.emitPoints();const s=null===(e=this.wavesurfer)||void 0===e?void 0:e.getDuration();s&&this.addPolyPoint(t,s)}removePoint(t){var e;const i=this.points.indexOf(t);i>-1&&(this.points.splice(i,1),null===(e=this.polyline)||void 0===e||e.removePolyPoint(t),this.emitPoints())}getPoints(){return this.points}setPoints(t){this.points.slice().forEach((t=>this.removePoint(t))),t.forEach((t=>this.addPoint(t)))}destroy(){var t;this.throttleTimeout&&(clearTimeout(this.throttleTimeout),this.throttleTimeout=null),null===(t=this.polyline)||void 0===t||t.destroy(),super.destroy()}getCurrentVolume(){return this.volume}setVolume(t){var e;this.volume=t,null===(e=this.wavesurfer)||void 0===e||e.setVolume(t)}onInit(){var t;if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const{options:e}=this;e.volume=null!==(t=e.volume)&&void 0!==t?t:this.wavesurfer.getVolume(),this.setVolume(e.volume),this.subscriptions.push(this.wavesurfer.on("decode",(t=>{this.initPolyline(),this.points.forEach((e=>{this.addPolyPoint(e,t)}))})),this.wavesurfer.on("redraw",(()=>{var t;null===(t=this.polyline)||void 0===t||t.update()})),this.wavesurfer.on("timeupdate",(t=>{this.onTimeUpdate(t)})))}emitPoints(){this.throttleTimeout&&clearTimeout(this.throttleTimeout),this.throttleTimeout=setTimeout((()=>{this.emit("points-change",this.points)}),200)}initPolyline(){if(this.polyline&&this.polyline.destroy(),!this.wavesurfer)return;const t=this.wavesurfer.getWrapper();this.polyline=new l(this.options,t),this.subscriptions.push(this.polyline.on("point-move",((t,e,i)=>{var s;const o=(null===(s=this.wavesurfer)||void 0===s?void 0:s.getDuration())||0;t.time=e*o,t.volume=1-i,this.emitPoints()})),this.polyline.on("point-dragout",(t=>{this.removePoint(t)})),this.polyline.on("point-create",((t,e)=>{var i;this.addPoint({time:t*((null===(i=this.wavesurfer)||void 0===i?void 0:i.getDuration())||0),volume:1-e})})),this.polyline.on("line-move",(t=>{var e;this.points.forEach((e=>{e.volume=Math.min(1,Math.max(0,e.volume-t))})),this.emitPoints(),this.onTimeUpdate((null===(e=this.wavesurfer)||void 0===e?void 0:e.getCurrentTime())||0)})))}addPolyPoint(t,e){var i;null===(i=this.polyline)||void 0===i||i.addPolyPoint(t.time/e,t.volume,t)}onTimeUpdate(t){if(!this.wavesurfer)return;let e=this.points.find((e=>e.time>t));e||(e={time:this.wavesurfer.getDuration()||0,volume:0});let i=this.points.findLast((e=>e.time<=t));i||(i={time:0,volume:0});const s=e.time-i.time,o=e.volume-i.volume,n=i.volume+(t-i.time)*(o/s),r=Math.min(1,Math.max(0,n)),l=Math.round(100*r)/100;l!==this.getCurrentVolume()&&(this.setVolume(l),this.emit("volume-change",l))}}export{h as default};
|
package/dist/plugins/envelope.js
CHANGED
|
@@ -1,426 +1 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Envelope is a visual UI for controlling the audio volume and add fade-in and fade-out effects.
|
|
3
|
-
*/
|
|
4
|
-
import BasePlugin from '../base-plugin.js';
|
|
5
|
-
import EventEmitter from '../event-emitter.js';
|
|
6
|
-
import createElement from '../dom.js';
|
|
7
|
-
import { createDragStream } from '../reactive/drag-stream.js';
|
|
8
|
-
import { effect } from '../reactive/store.js';
|
|
9
|
-
const defaultOptions = {
|
|
10
|
-
points: [],
|
|
11
|
-
lineWidth: 4,
|
|
12
|
-
lineColor: 'rgba(0, 0, 255, 0.5)',
|
|
13
|
-
dragPointSize: 10,
|
|
14
|
-
dragPointFill: 'rgba(255, 255, 255, 0.8)',
|
|
15
|
-
dragPointStroke: 'rgba(255, 255, 255, 0.8)',
|
|
16
|
-
};
|
|
17
|
-
class Polyline extends EventEmitter {
|
|
18
|
-
constructor(options, wrapper) {
|
|
19
|
-
super();
|
|
20
|
-
this.subscriptions = [];
|
|
21
|
-
this.subscriptions = [];
|
|
22
|
-
this.options = options;
|
|
23
|
-
this.polyPoints = new Map();
|
|
24
|
-
const width = wrapper.clientWidth;
|
|
25
|
-
const height = wrapper.clientHeight;
|
|
26
|
-
// SVG element
|
|
27
|
-
const svg = createElement('svg', {
|
|
28
|
-
xmlns: 'http://www.w3.org/2000/svg',
|
|
29
|
-
width: '100%',
|
|
30
|
-
height: '100%',
|
|
31
|
-
viewBox: `0 0 ${width} ${height}`,
|
|
32
|
-
preserveAspectRatio: 'none',
|
|
33
|
-
style: {
|
|
34
|
-
position: 'absolute',
|
|
35
|
-
left: '0',
|
|
36
|
-
top: '0',
|
|
37
|
-
zIndex: '4',
|
|
38
|
-
},
|
|
39
|
-
part: 'envelope',
|
|
40
|
-
}, wrapper);
|
|
41
|
-
this.svg = svg;
|
|
42
|
-
// A polyline representing the envelope
|
|
43
|
-
const polyline = createElement('polyline', {
|
|
44
|
-
xmlns: 'http://www.w3.org/2000/svg',
|
|
45
|
-
points: `0,${height} ${width},${height}`,
|
|
46
|
-
stroke: options.lineColor,
|
|
47
|
-
'stroke-width': options.lineWidth,
|
|
48
|
-
fill: 'none',
|
|
49
|
-
part: 'polyline',
|
|
50
|
-
style: options.dragLine
|
|
51
|
-
? {
|
|
52
|
-
cursor: 'row-resize',
|
|
53
|
-
pointerEvents: 'stroke',
|
|
54
|
-
}
|
|
55
|
-
: {},
|
|
56
|
-
}, svg);
|
|
57
|
-
// Make the polyline draggable along the Y axis
|
|
58
|
-
if (options.dragLine) {
|
|
59
|
-
const dragStream = createDragStream(polyline);
|
|
60
|
-
const unsubscribe = effect(() => {
|
|
61
|
-
const drag = dragStream.signal.value;
|
|
62
|
-
if (!drag || drag.type !== 'move' || drag.deltaY === undefined)
|
|
63
|
-
return;
|
|
64
|
-
const deltaY = drag.deltaY;
|
|
65
|
-
const { height } = svg.viewBox.baseVal;
|
|
66
|
-
const { points } = polyline;
|
|
67
|
-
for (let i = 1; i < points.numberOfItems - 1; i++) {
|
|
68
|
-
const point = points.getItem(i);
|
|
69
|
-
point.y = Math.min(height, Math.max(0, point.y + deltaY));
|
|
70
|
-
}
|
|
71
|
-
const circles = svg.querySelectorAll('ellipse');
|
|
72
|
-
Array.from(circles).forEach((circle) => {
|
|
73
|
-
const newY = Math.min(height, Math.max(0, Number(circle.getAttribute('cy')) + deltaY));
|
|
74
|
-
circle.setAttribute('cy', newY.toString());
|
|
75
|
-
});
|
|
76
|
-
this.emit('line-move', deltaY / height);
|
|
77
|
-
}, [dragStream.signal]);
|
|
78
|
-
this.subscriptions.push(() => {
|
|
79
|
-
unsubscribe();
|
|
80
|
-
dragStream.cleanup();
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
// Listen to double click to add a new point
|
|
84
|
-
this.dblClickListener = (e) => {
|
|
85
|
-
const rect = svg.getBoundingClientRect();
|
|
86
|
-
const x = e.clientX - rect.left;
|
|
87
|
-
const y = e.clientY - rect.top;
|
|
88
|
-
this.emit('point-create', x / rect.width, y / rect.height);
|
|
89
|
-
};
|
|
90
|
-
svg.addEventListener('dblclick', this.dblClickListener);
|
|
91
|
-
// Long press on touch devices
|
|
92
|
-
const clearTimer = () => {
|
|
93
|
-
if (this.pressTimer !== undefined) {
|
|
94
|
-
clearTimeout(this.pressTimer);
|
|
95
|
-
this.pressTimer = undefined;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
|
-
this.touchStartListener = (e) => {
|
|
99
|
-
if (e.touches.length === 1) {
|
|
100
|
-
this.pressTimer = window.setTimeout(() => {
|
|
101
|
-
e.preventDefault();
|
|
102
|
-
const rect = svg.getBoundingClientRect();
|
|
103
|
-
const x = e.touches[0].clientX - rect.left;
|
|
104
|
-
const y = e.touches[0].clientY - rect.top;
|
|
105
|
-
this.emit('point-create', x / rect.width, y / rect.height);
|
|
106
|
-
}, 500);
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
109
|
-
clearTimer();
|
|
110
|
-
}
|
|
111
|
-
};
|
|
112
|
-
this.touchMoveListener = clearTimer;
|
|
113
|
-
this.touchEndListener = clearTimer;
|
|
114
|
-
svg.addEventListener('touchstart', this.touchStartListener);
|
|
115
|
-
svg.addEventListener('touchmove', this.touchMoveListener);
|
|
116
|
-
svg.addEventListener('touchend', this.touchEndListener);
|
|
117
|
-
}
|
|
118
|
-
makeDraggable(draggable, onDrag) {
|
|
119
|
-
const dragStream = createDragStream(draggable, { threshold: 1 });
|
|
120
|
-
const unsubscribe = effect(() => {
|
|
121
|
-
const drag = dragStream.signal.value;
|
|
122
|
-
if (!drag)
|
|
123
|
-
return;
|
|
124
|
-
if (drag.type === 'start') {
|
|
125
|
-
draggable.style.cursor = 'grabbing';
|
|
126
|
-
}
|
|
127
|
-
else if (drag.type === 'move' && drag.deltaX !== undefined && drag.deltaY !== undefined) {
|
|
128
|
-
onDrag(drag.deltaX, drag.deltaY);
|
|
129
|
-
}
|
|
130
|
-
else if (drag.type === 'end') {
|
|
131
|
-
draggable.style.cursor = 'grab';
|
|
132
|
-
}
|
|
133
|
-
}, [dragStream.signal]);
|
|
134
|
-
this.subscriptions.push(() => {
|
|
135
|
-
unsubscribe();
|
|
136
|
-
dragStream.cleanup();
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
createCircle(x, y) {
|
|
140
|
-
const size = this.options.dragPointSize;
|
|
141
|
-
const radius = size / 2;
|
|
142
|
-
return createElement('ellipse', {
|
|
143
|
-
xmlns: 'http://www.w3.org/2000/svg',
|
|
144
|
-
cx: x,
|
|
145
|
-
cy: y,
|
|
146
|
-
rx: radius,
|
|
147
|
-
ry: radius,
|
|
148
|
-
fill: this.options.dragPointFill,
|
|
149
|
-
stroke: this.options.dragPointStroke,
|
|
150
|
-
'stroke-width': '2',
|
|
151
|
-
style: {
|
|
152
|
-
cursor: 'grab',
|
|
153
|
-
pointerEvents: 'all',
|
|
154
|
-
},
|
|
155
|
-
part: 'envelope-circle',
|
|
156
|
-
}, this.svg);
|
|
157
|
-
}
|
|
158
|
-
removePolyPoint(point) {
|
|
159
|
-
const item = this.polyPoints.get(point);
|
|
160
|
-
if (!item)
|
|
161
|
-
return;
|
|
162
|
-
const { polyPoint, circle } = item;
|
|
163
|
-
const { points } = this.svg.querySelector('polyline');
|
|
164
|
-
const index = Array.from(points).findIndex((p) => p.x === polyPoint.x && p.y === polyPoint.y);
|
|
165
|
-
points.removeItem(index);
|
|
166
|
-
circle.remove();
|
|
167
|
-
this.polyPoints.delete(point);
|
|
168
|
-
}
|
|
169
|
-
addPolyPoint(relX, relY, refPoint) {
|
|
170
|
-
const { svg } = this;
|
|
171
|
-
const { width, height } = svg.viewBox.baseVal;
|
|
172
|
-
const x = relX * width;
|
|
173
|
-
const y = height - relY * height;
|
|
174
|
-
const threshold = this.options.dragPointSize / 2;
|
|
175
|
-
const newPoint = svg.createSVGPoint();
|
|
176
|
-
newPoint.x = relX * width;
|
|
177
|
-
newPoint.y = height - relY * height;
|
|
178
|
-
const circle = this.createCircle(x, y);
|
|
179
|
-
const { points } = svg.querySelector('polyline');
|
|
180
|
-
const newIndex = Array.from(points).findIndex((point) => point.x >= x);
|
|
181
|
-
points.insertItemBefore(newPoint, Math.max(newIndex, 1));
|
|
182
|
-
this.polyPoints.set(refPoint, { polyPoint: newPoint, circle });
|
|
183
|
-
this.makeDraggable(circle, (dx, dy) => {
|
|
184
|
-
const newX = newPoint.x + dx;
|
|
185
|
-
const newY = newPoint.y + dy;
|
|
186
|
-
// Remove the point if it's dragged out of the SVG
|
|
187
|
-
if (newX < -threshold || newY < -threshold || newX > width + threshold || newY > height + threshold) {
|
|
188
|
-
this.emit('point-dragout', refPoint);
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
// Don't allow to drag past the next or previous point
|
|
192
|
-
const next = Array.from(points).find((point) => point.x > newPoint.x);
|
|
193
|
-
const prev = Array.from(points).findLast((point) => point.x < newPoint.x);
|
|
194
|
-
if ((next && newX >= next.x) || (prev && newX <= prev.x)) {
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
// Update the point and the circle position
|
|
198
|
-
newPoint.x = newX;
|
|
199
|
-
newPoint.y = newY;
|
|
200
|
-
circle.setAttribute('cx', newX.toString());
|
|
201
|
-
circle.setAttribute('cy', newY.toString());
|
|
202
|
-
// Emit the event passing the point and new relative coordinates
|
|
203
|
-
this.emit('point-move', refPoint, newX / width, newY / height);
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
update() {
|
|
207
|
-
const { svg } = this;
|
|
208
|
-
// Skip the update if the container is hidden
|
|
209
|
-
const { clientWidth, clientHeight } = svg;
|
|
210
|
-
if (!clientWidth || !clientHeight) {
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
const aspectRatioX = svg.viewBox.baseVal.width / clientWidth;
|
|
214
|
-
const aspectRatioY = svg.viewBox.baseVal.height / clientHeight;
|
|
215
|
-
const circles = svg.querySelectorAll('ellipse');
|
|
216
|
-
circles.forEach((circle) => {
|
|
217
|
-
const radius = this.options.dragPointSize / 2;
|
|
218
|
-
const rx = radius * aspectRatioX;
|
|
219
|
-
const ry = radius * aspectRatioY;
|
|
220
|
-
circle.setAttribute('rx', rx.toString());
|
|
221
|
-
circle.setAttribute('ry', ry.toString());
|
|
222
|
-
});
|
|
223
|
-
}
|
|
224
|
-
destroy() {
|
|
225
|
-
// Clear pending press timer
|
|
226
|
-
if (this.pressTimer !== undefined) {
|
|
227
|
-
clearTimeout(this.pressTimer);
|
|
228
|
-
this.pressTimer = undefined;
|
|
229
|
-
}
|
|
230
|
-
// Remove event listeners
|
|
231
|
-
if (this.dblClickListener) {
|
|
232
|
-
this.svg.removeEventListener('dblclick', this.dblClickListener);
|
|
233
|
-
this.dblClickListener = undefined;
|
|
234
|
-
}
|
|
235
|
-
if (this.touchStartListener) {
|
|
236
|
-
this.svg.removeEventListener('touchstart', this.touchStartListener);
|
|
237
|
-
this.touchStartListener = undefined;
|
|
238
|
-
}
|
|
239
|
-
if (this.touchMoveListener) {
|
|
240
|
-
this.svg.removeEventListener('touchmove', this.touchMoveListener);
|
|
241
|
-
this.touchMoveListener = undefined;
|
|
242
|
-
}
|
|
243
|
-
if (this.touchEndListener) {
|
|
244
|
-
this.svg.removeEventListener('touchend', this.touchEndListener);
|
|
245
|
-
this.touchEndListener = undefined;
|
|
246
|
-
}
|
|
247
|
-
this.subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
248
|
-
this.polyPoints.clear();
|
|
249
|
-
this.svg.remove();
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
const randomId = () => Math.random().toString(36).slice(2);
|
|
253
|
-
class EnvelopePlugin extends BasePlugin {
|
|
254
|
-
/**
|
|
255
|
-
* Create a new Envelope plugin.
|
|
256
|
-
*/
|
|
257
|
-
constructor(options) {
|
|
258
|
-
super(options);
|
|
259
|
-
this.polyline = null;
|
|
260
|
-
this.throttleTimeout = null;
|
|
261
|
-
this.volume = 1;
|
|
262
|
-
this.points = options.points || [];
|
|
263
|
-
this.options = Object.assign({}, defaultOptions, options);
|
|
264
|
-
this.options.lineColor = this.options.lineColor || defaultOptions.lineColor;
|
|
265
|
-
this.options.dragPointFill = this.options.dragPointFill || defaultOptions.dragPointFill;
|
|
266
|
-
this.options.dragPointStroke = this.options.dragPointStroke || defaultOptions.dragPointStroke;
|
|
267
|
-
this.options.dragPointSize = this.options.dragPointSize || defaultOptions.dragPointSize;
|
|
268
|
-
}
|
|
269
|
-
static create(options) {
|
|
270
|
-
return new EnvelopePlugin(options);
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Add an envelope point with a given time and volume.
|
|
274
|
-
*/
|
|
275
|
-
addPoint(point) {
|
|
276
|
-
var _a;
|
|
277
|
-
if (!point.id)
|
|
278
|
-
point.id = randomId();
|
|
279
|
-
// Insert the point in the correct position to keep the array sorted
|
|
280
|
-
const index = this.points.findLastIndex((p) => p.time < point.time);
|
|
281
|
-
this.points.splice(index + 1, 0, point);
|
|
282
|
-
this.emitPoints();
|
|
283
|
-
// Add the point to the polyline if the duration is available
|
|
284
|
-
const duration = (_a = this.wavesurfer) === null || _a === void 0 ? void 0 : _a.getDuration();
|
|
285
|
-
if (duration) {
|
|
286
|
-
this.addPolyPoint(point, duration);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
/**
|
|
290
|
-
* Remove an envelope point.
|
|
291
|
-
*/
|
|
292
|
-
removePoint(point) {
|
|
293
|
-
var _a;
|
|
294
|
-
const index = this.points.indexOf(point);
|
|
295
|
-
if (index > -1) {
|
|
296
|
-
this.points.splice(index, 1);
|
|
297
|
-
(_a = this.polyline) === null || _a === void 0 ? void 0 : _a.removePolyPoint(point);
|
|
298
|
-
this.emitPoints();
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Get all envelope points. Should not be modified directly.
|
|
303
|
-
*/
|
|
304
|
-
getPoints() {
|
|
305
|
-
return this.points;
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Set new envelope points.
|
|
309
|
-
*/
|
|
310
|
-
setPoints(newPoints) {
|
|
311
|
-
this.points.slice().forEach((point) => this.removePoint(point));
|
|
312
|
-
newPoints.forEach((point) => this.addPoint(point));
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Destroy the plugin instance.
|
|
316
|
-
*/
|
|
317
|
-
destroy() {
|
|
318
|
-
var _a;
|
|
319
|
-
// Clear pending throttle timeout
|
|
320
|
-
if (this.throttleTimeout) {
|
|
321
|
-
clearTimeout(this.throttleTimeout);
|
|
322
|
-
this.throttleTimeout = null;
|
|
323
|
-
}
|
|
324
|
-
(_a = this.polyline) === null || _a === void 0 ? void 0 : _a.destroy();
|
|
325
|
-
super.destroy();
|
|
326
|
-
}
|
|
327
|
-
/**
|
|
328
|
-
* Get the envelope volume.
|
|
329
|
-
*/
|
|
330
|
-
getCurrentVolume() {
|
|
331
|
-
return this.volume;
|
|
332
|
-
}
|
|
333
|
-
/**
|
|
334
|
-
* Set the envelope volume. 0..1 (more than 1 will boost the volume).
|
|
335
|
-
*/
|
|
336
|
-
setVolume(floatValue) {
|
|
337
|
-
var _a;
|
|
338
|
-
this.volume = floatValue;
|
|
339
|
-
(_a = this.wavesurfer) === null || _a === void 0 ? void 0 : _a.setVolume(floatValue);
|
|
340
|
-
}
|
|
341
|
-
/** Called by wavesurfer, don't call manually */
|
|
342
|
-
onInit() {
|
|
343
|
-
var _a;
|
|
344
|
-
if (!this.wavesurfer) {
|
|
345
|
-
throw Error('WaveSurfer is not initialized');
|
|
346
|
-
}
|
|
347
|
-
const { options } = this;
|
|
348
|
-
options.volume = (_a = options.volume) !== null && _a !== void 0 ? _a : this.wavesurfer.getVolume();
|
|
349
|
-
this.setVolume(options.volume);
|
|
350
|
-
this.subscriptions.push(this.wavesurfer.on('decode', (duration) => {
|
|
351
|
-
this.initPolyline();
|
|
352
|
-
this.points.forEach((point) => {
|
|
353
|
-
this.addPolyPoint(point, duration);
|
|
354
|
-
});
|
|
355
|
-
}), this.wavesurfer.on('redraw', () => {
|
|
356
|
-
var _a;
|
|
357
|
-
(_a = this.polyline) === null || _a === void 0 ? void 0 : _a.update();
|
|
358
|
-
}), this.wavesurfer.on('timeupdate', (time) => {
|
|
359
|
-
this.onTimeUpdate(time);
|
|
360
|
-
}));
|
|
361
|
-
}
|
|
362
|
-
emitPoints() {
|
|
363
|
-
if (this.throttleTimeout) {
|
|
364
|
-
clearTimeout(this.throttleTimeout);
|
|
365
|
-
}
|
|
366
|
-
this.throttleTimeout = setTimeout(() => {
|
|
367
|
-
this.emit('points-change', this.points);
|
|
368
|
-
}, 200);
|
|
369
|
-
}
|
|
370
|
-
initPolyline() {
|
|
371
|
-
if (this.polyline)
|
|
372
|
-
this.polyline.destroy();
|
|
373
|
-
if (!this.wavesurfer)
|
|
374
|
-
return;
|
|
375
|
-
const wrapper = this.wavesurfer.getWrapper();
|
|
376
|
-
this.polyline = new Polyline(this.options, wrapper);
|
|
377
|
-
this.subscriptions.push(this.polyline.on('point-move', (point, relativeX, relativeY) => {
|
|
378
|
-
var _a;
|
|
379
|
-
const duration = ((_a = this.wavesurfer) === null || _a === void 0 ? void 0 : _a.getDuration()) || 0;
|
|
380
|
-
point.time = relativeX * duration;
|
|
381
|
-
point.volume = 1 - relativeY;
|
|
382
|
-
this.emitPoints();
|
|
383
|
-
}), this.polyline.on('point-dragout', (point) => {
|
|
384
|
-
this.removePoint(point);
|
|
385
|
-
}), this.polyline.on('point-create', (relativeX, relativeY) => {
|
|
386
|
-
var _a;
|
|
387
|
-
this.addPoint({
|
|
388
|
-
time: relativeX * (((_a = this.wavesurfer) === null || _a === void 0 ? void 0 : _a.getDuration()) || 0),
|
|
389
|
-
volume: 1 - relativeY,
|
|
390
|
-
});
|
|
391
|
-
}), this.polyline.on('line-move', (relativeY) => {
|
|
392
|
-
var _a;
|
|
393
|
-
this.points.forEach((point) => {
|
|
394
|
-
point.volume = Math.min(1, Math.max(0, point.volume - relativeY));
|
|
395
|
-
});
|
|
396
|
-
this.emitPoints();
|
|
397
|
-
this.onTimeUpdate(((_a = this.wavesurfer) === null || _a === void 0 ? void 0 : _a.getCurrentTime()) || 0);
|
|
398
|
-
}));
|
|
399
|
-
}
|
|
400
|
-
addPolyPoint(point, duration) {
|
|
401
|
-
var _a;
|
|
402
|
-
(_a = this.polyline) === null || _a === void 0 ? void 0 : _a.addPolyPoint(point.time / duration, point.volume, point);
|
|
403
|
-
}
|
|
404
|
-
onTimeUpdate(time) {
|
|
405
|
-
if (!this.wavesurfer)
|
|
406
|
-
return;
|
|
407
|
-
let nextPoint = this.points.find((point) => point.time > time);
|
|
408
|
-
if (!nextPoint) {
|
|
409
|
-
nextPoint = { time: this.wavesurfer.getDuration() || 0, volume: 0 };
|
|
410
|
-
}
|
|
411
|
-
let prevPoint = this.points.findLast((point) => point.time <= time);
|
|
412
|
-
if (!prevPoint) {
|
|
413
|
-
prevPoint = { time: 0, volume: 0 };
|
|
414
|
-
}
|
|
415
|
-
const timeDiff = nextPoint.time - prevPoint.time;
|
|
416
|
-
const volumeDiff = nextPoint.volume - prevPoint.volume;
|
|
417
|
-
const newVolume = prevPoint.volume + (time - prevPoint.time) * (volumeDiff / timeDiff);
|
|
418
|
-
const clampedVolume = Math.min(1, Math.max(0, newVolume));
|
|
419
|
-
const roundedVolume = Math.round(clampedVolume * 100) / 100;
|
|
420
|
-
if (roundedVolume !== this.getCurrentVolume()) {
|
|
421
|
-
this.setVolume(roundedVolume);
|
|
422
|
-
this.emit('volume-change', roundedVolume);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
export default EnvelopePlugin;
|
|
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){const s=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,o]of Object.entries(e))if("children"===t&&o)for(const[t,e]of Object.entries(o))e instanceof Node?s.appendChild(e):"string"==typeof e?s.appendChild(document.createTextNode(e)):s.appendChild(i(t,e));else"style"===t?Object.assign(s.style,o):"textContent"===t?s.textContent=o:s.setAttribute(t,o.toString());return s}function s(t,e,s){const o=i(t,e||{});return null==s||s.appendChild(o),o}function o(t,e){let i;const s=()=>{i&&(i(),i=void 0),i=t()},o=e.map((t=>t.subscribe(s)));return s(),()=>{i&&(i(),i=void 0),o.forEach((t=>t()))}}function n(t,e={}){const{threshold:i=3,mouseButton:s=0,touchDelay:o=100}=e,n=function(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))}}(null),r=new Map,l=matchMedia("(pointer: coarse)").matches;let h=()=>{};const a=e=>{if(e.button!==s)return;if(r.set(e.pointerId,e),r.size>1)return;let a=e.clientX,u=e.clientY,c=!1;const d=Date.now(),p=t.getBoundingClientRect(),{left:v,top:m}=p,g=t=>{if(t.defaultPrevented||r.size>1)return;if(l&&Date.now()-d<o)return;const e=t.clientX,s=t.clientY,h=e-a,p=s-u;(c||Math.abs(h)>i||Math.abs(p)>i)&&(t.preventDefault(),t.stopPropagation(),c||(n.set({type:"start",x:a-v,y:u-m}),c=!0),n.set({type:"move",x:e-v,y:s-m,deltaX:h,deltaY:p}),a=e,u=s)},f=t=>{if(r.delete(t.pointerId),c){const e=t.clientX,i=t.clientY;n.set({type:"end",x:e-v,y:i-m})}h()},y=t=>{r.delete(t.pointerId),t.relatedTarget&&t.relatedTarget!==document.documentElement||f(t)},P=t=>{c&&(t.stopPropagation(),t.preventDefault())},w=t=>{t.defaultPrevented||r.size>1||c&&t.preventDefault()};document.addEventListener("pointermove",g),document.addEventListener("pointerup",f),document.addEventListener("pointerout",y),document.addEventListener("pointercancel",y),document.addEventListener("touchmove",w,{passive:!1}),document.addEventListener("click",P,{capture:!0}),h=()=>{document.removeEventListener("pointermove",g),document.removeEventListener("pointerup",f),document.removeEventListener("pointerout",y),document.removeEventListener("pointercancel",y),document.removeEventListener("touchmove",w),setTimeout((()=>{document.removeEventListener("click",P,{capture:!0})}),10)}};t.addEventListener("pointerdown",a);return{signal:n,cleanup:()=>{h(),t.removeEventListener("pointerdown",a),r.clear(),function(t){const e=t._cleanup;"function"==typeof e&&e()}(n)}}}const r={points:[],lineWidth:4,lineColor:"rgba(0, 0, 255, 0.5)",dragPointSize:10,dragPointFill:"rgba(255, 255, 255, 0.8)",dragPointStroke:"rgba(255, 255, 255, 0.8)"};class l extends t{constructor(t,e){super(),this.subscriptions=[],this.subscriptions=[],this.options=t,this.polyPoints=new Map;const i=e.clientWidth,r=e.clientHeight,l=s("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",viewBox:`0 0 ${i} ${r}`,preserveAspectRatio:"none",style:{position:"absolute",left:"0",top:"0",zIndex:"4"},part:"envelope"},e);this.svg=l;const h=s("polyline",{xmlns:"http://www.w3.org/2000/svg",points:`0,${r} ${i},${r}`,stroke:t.lineColor,"stroke-width":t.lineWidth,fill:"none",part:"polyline",style:t.dragLine?{cursor:"row-resize",pointerEvents:"stroke"}:{}},l);if(t.dragLine){const t=n(h),e=o((()=>{const e=t.signal.value;if(!e||"move"!==e.type||void 0===e.deltaY)return;const i=e.deltaY,{height:s}=l.viewBox.baseVal,{points:o}=h;for(let t=1;t<o.numberOfItems-1;t++){const e=o.getItem(t);e.y=Math.min(s,Math.max(0,e.y+i))}const n=l.querySelectorAll("ellipse");Array.from(n).forEach((t=>{const e=Math.min(s,Math.max(0,Number(t.getAttribute("cy"))+i));t.setAttribute("cy",e.toString())})),this.emit("line-move",i/s)}),[t.signal]);this.subscriptions.push((()=>{e(),t.cleanup()}))}this.dblClickListener=t=>{const e=l.getBoundingClientRect(),i=t.clientX-e.left,s=t.clientY-e.top;this.emit("point-create",i/e.width,s/e.height)},l.addEventListener("dblclick",this.dblClickListener);const a=()=>{void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0)};this.touchStartListener=t=>{1===t.touches.length?this.pressTimer=window.setTimeout((()=>{t.preventDefault();const e=l.getBoundingClientRect(),i=t.touches[0].clientX-e.left,s=t.touches[0].clientY-e.top;this.emit("point-create",i/e.width,s/e.height)}),500):a()},this.touchMoveListener=a,this.touchEndListener=a,l.addEventListener("touchstart",this.touchStartListener),l.addEventListener("touchmove",this.touchMoveListener),l.addEventListener("touchend",this.touchEndListener)}makeDraggable(t,e){const i=n(t,{threshold:1}),s=o((()=>{const s=i.signal.value;s&&("start"===s.type?t.style.cursor="grabbing":"move"===s.type&&void 0!==s.deltaX&&void 0!==s.deltaY?e(s.deltaX,s.deltaY):"end"===s.type&&(t.style.cursor="grab"))}),[i.signal]);this.subscriptions.push((()=>{s(),i.cleanup()}))}createCircle(t,e){const i=this.options.dragPointSize/2;return s("ellipse",{xmlns:"http://www.w3.org/2000/svg",cx:t,cy:e,rx:i,ry:i,fill:this.options.dragPointFill,stroke:this.options.dragPointStroke,"stroke-width":"2",style:{cursor:"grab",pointerEvents:"all"},part:"envelope-circle"},this.svg)}removePolyPoint(t){const e=this.polyPoints.get(t);if(!e)return;const{polyPoint:i,circle:s}=e,{points:o}=this.svg.querySelector("polyline"),n=Array.from(o).findIndex((t=>t.x===i.x&&t.y===i.y));o.removeItem(n),s.remove(),this.polyPoints.delete(t)}addPolyPoint(t,e,i){const{svg:s}=this,{width:o,height:n}=s.viewBox.baseVal,r=t*o,l=n-e*n,h=this.options.dragPointSize/2,a=s.createSVGPoint();a.x=t*o,a.y=n-e*n;const u=this.createCircle(r,l),{points:c}=s.querySelector("polyline"),d=Array.from(c).findIndex((t=>t.x>=r));c.insertItemBefore(a,Math.max(d,1)),this.polyPoints.set(i,{polyPoint:a,circle:u}),this.makeDraggable(u,((t,e)=>{const s=a.x+t,r=a.y+e;if(s<-h||r<-h||s>o+h||r>n+h)return void this.emit("point-dragout",i);const l=Array.from(c).find((t=>t.x>a.x)),d=Array.from(c).findLast((t=>t.x<a.x));l&&s>=l.x||d&&s<=d.x||(a.x=s,a.y=r,u.setAttribute("cx",s.toString()),u.setAttribute("cy",r.toString()),this.emit("point-move",i,s/o,r/n))}))}update(){const{svg:t}=this,{clientWidth:e,clientHeight:i}=t;if(!e||!i)return;const s=t.viewBox.baseVal.width/e,o=t.viewBox.baseVal.height/i;t.querySelectorAll("ellipse").forEach((t=>{const e=this.options.dragPointSize/2,i=e*s,n=e*o;t.setAttribute("rx",i.toString()),t.setAttribute("ry",n.toString())}))}destroy(){void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0),this.dblClickListener&&(this.svg.removeEventListener("dblclick",this.dblClickListener),this.dblClickListener=void 0),this.touchStartListener&&(this.svg.removeEventListener("touchstart",this.touchStartListener),this.touchStartListener=void 0),this.touchMoveListener&&(this.svg.removeEventListener("touchmove",this.touchMoveListener),this.touchMoveListener=void 0),this.touchEndListener&&(this.svg.removeEventListener("touchend",this.touchEndListener),this.touchEndListener=void 0),this.subscriptions.forEach((t=>t())),this.polyPoints.clear(),this.svg.remove()}}class h extends e{constructor(t){super(t),this.polyline=null,this.throttleTimeout=null,this.volume=1,this.points=t.points||[],this.options=Object.assign({},r,t),this.options.lineColor=this.options.lineColor||r.lineColor,this.options.dragPointFill=this.options.dragPointFill||r.dragPointFill,this.options.dragPointStroke=this.options.dragPointStroke||r.dragPointStroke,this.options.dragPointSize=this.options.dragPointSize||r.dragPointSize}static create(t){return new h(t)}addPoint(t){var e;t.id||(t.id=Math.random().toString(36).slice(2));const i=this.points.findLastIndex((e=>e.time<t.time));this.points.splice(i+1,0,t),this.emitPoints();const s=null===(e=this.wavesurfer)||void 0===e?void 0:e.getDuration();s&&this.addPolyPoint(t,s)}removePoint(t){var e;const i=this.points.indexOf(t);i>-1&&(this.points.splice(i,1),null===(e=this.polyline)||void 0===e||e.removePolyPoint(t),this.emitPoints())}getPoints(){return this.points}setPoints(t){this.points.slice().forEach((t=>this.removePoint(t))),t.forEach((t=>this.addPoint(t)))}destroy(){var t;this.throttleTimeout&&(clearTimeout(this.throttleTimeout),this.throttleTimeout=null),null===(t=this.polyline)||void 0===t||t.destroy(),super.destroy()}getCurrentVolume(){return this.volume}setVolume(t){var e;this.volume=t,null===(e=this.wavesurfer)||void 0===e||e.setVolume(t)}onInit(){var t;if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const{options:e}=this;e.volume=null!==(t=e.volume)&&void 0!==t?t:this.wavesurfer.getVolume(),this.setVolume(e.volume),this.subscriptions.push(this.wavesurfer.on("decode",(t=>{this.initPolyline(),this.points.forEach((e=>{this.addPolyPoint(e,t)}))})),this.wavesurfer.on("redraw",(()=>{var t;null===(t=this.polyline)||void 0===t||t.update()})),this.wavesurfer.on("timeupdate",(t=>{this.onTimeUpdate(t)})))}emitPoints(){this.throttleTimeout&&clearTimeout(this.throttleTimeout),this.throttleTimeout=setTimeout((()=>{this.emit("points-change",this.points)}),200)}initPolyline(){if(this.polyline&&this.polyline.destroy(),!this.wavesurfer)return;const t=this.wavesurfer.getWrapper();this.polyline=new l(this.options,t),this.subscriptions.push(this.polyline.on("point-move",((t,e,i)=>{var s;const o=(null===(s=this.wavesurfer)||void 0===s?void 0:s.getDuration())||0;t.time=e*o,t.volume=1-i,this.emitPoints()})),this.polyline.on("point-dragout",(t=>{this.removePoint(t)})),this.polyline.on("point-create",((t,e)=>{var i;this.addPoint({time:t*((null===(i=this.wavesurfer)||void 0===i?void 0:i.getDuration())||0),volume:1-e})})),this.polyline.on("line-move",(t=>{var e;this.points.forEach((e=>{e.volume=Math.min(1,Math.max(0,e.volume-t))})),this.emitPoints(),this.onTimeUpdate((null===(e=this.wavesurfer)||void 0===e?void 0:e.getCurrentTime())||0)})))}addPolyPoint(t,e){var i;null===(i=this.polyline)||void 0===i||i.addPolyPoint(t.time/e,t.volume,t)}onTimeUpdate(t){if(!this.wavesurfer)return;let e=this.points.find((e=>e.time>t));e||(e={time:this.wavesurfer.getDuration()||0,volume:0});let i=this.points.findLast((e=>e.time<=t));i||(i={time:0,volume:0});const s=e.time-i.time,o=e.volume-i.volume,n=i.volume+(t-i.time)*(o/s),r=Math.min(1,Math.max(0,n)),l=Math.round(100*r)/100;l!==this.getCurrentVolume()&&(this.setVolume(l),this.emit("volume-change",l))}}export{h 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.Envelope=e())}(this,(function(){"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){const s=e.xmlns?document.createElementNS(e.xmlns,t):document.createElement(t);for(const[t,o]of Object.entries(e))if("children"===t&&o)for(const[t,e]of Object.entries(o))e instanceof Node?s.appendChild(e):"string"==typeof e?s.appendChild(document.createTextNode(e)):s.appendChild(i(t,e));else"style"===t?Object.assign(s.style,o):"textContent"===t?s.textContent=o:s.setAttribute(t,o.toString());return s}function s(t,e,s){const o=i(t,e||{});return null==s||s.appendChild(o),o}function o(t,e){let i;const s=()=>{i&&(i(),i=void 0),i=t()},o=e.map((t=>t.subscribe(s)));return s(),()=>{i&&(i(),i=void 0),o.forEach((t=>t()))}}function n(t,e={}){const{threshold:i=3,mouseButton:s=0,touchDelay:o=100}=e,n=function(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))}}(null),r=new Map,l=matchMedia("(pointer: coarse)").matches;let h=()=>{};const a=e=>{if(e.button!==s)return;if(r.set(e.pointerId,e),r.size>1)return;let a=e.clientX,u=e.clientY,c=!1;const d=Date.now(),p=t.getBoundingClientRect(),{left:v,top:m}=p,f=t=>{if(t.defaultPrevented||r.size>1)return;if(l&&Date.now()-d<o)return;const e=t.clientX,s=t.clientY,h=e-a,p=s-u;(c||Math.abs(h)>i||Math.abs(p)>i)&&(t.preventDefault(),t.stopPropagation(),c||(n.set({type:"start",x:a-v,y:u-m}),c=!0),n.set({type:"move",x:e-v,y:s-m,deltaX:h,deltaY:p}),a=e,u=s)},g=t=>{if(r.delete(t.pointerId),c){const e=t.clientX,i=t.clientY;n.set({type:"end",x:e-v,y:i-m})}h()},y=t=>{r.delete(t.pointerId),t.relatedTarget&&t.relatedTarget!==document.documentElement||g(t)},P=t=>{c&&(t.stopPropagation(),t.preventDefault())},b=t=>{t.defaultPrevented||r.size>1||c&&t.preventDefault()};document.addEventListener("pointermove",f),document.addEventListener("pointerup",g),document.addEventListener("pointerout",y),document.addEventListener("pointercancel",y),document.addEventListener("touchmove",b,{passive:!1}),document.addEventListener("click",P,{capture:!0}),h=()=>{document.removeEventListener("pointermove",f),document.removeEventListener("pointerup",g),document.removeEventListener("pointerout",y),document.removeEventListener("pointercancel",y),document.removeEventListener("touchmove",b),setTimeout((()=>{document.removeEventListener("click",P,{capture:!0})}),10)}};t.addEventListener("pointerdown",a);return{signal:n,cleanup:()=>{h(),t.removeEventListener("pointerdown",a),r.clear(),function(t){const e=t._cleanup;"function"==typeof e&&e()}(n)}}}const r={points:[],lineWidth:4,lineColor:"rgba(0, 0, 255, 0.5)",dragPointSize:10,dragPointFill:"rgba(255, 255, 255, 0.8)",dragPointStroke:"rgba(255, 255, 255, 0.8)"};class l extends t{constructor(t,e){super(),this.subscriptions=[],this.subscriptions=[],this.options=t,this.polyPoints=new Map;const i=e.clientWidth,r=e.clientHeight,l=s("svg",{xmlns:"http://www.w3.org/2000/svg",width:"100%",height:"100%",viewBox:`0 0 ${i} ${r}`,preserveAspectRatio:"none",style:{position:"absolute",left:"0",top:"0",zIndex:"4"},part:"envelope"},e);this.svg=l;const h=s("polyline",{xmlns:"http://www.w3.org/2000/svg",points:`0,${r} ${i},${r}`,stroke:t.lineColor,"stroke-width":t.lineWidth,fill:"none",part:"polyline",style:t.dragLine?{cursor:"row-resize",pointerEvents:"stroke"}:{}},l);if(t.dragLine){const t=n(h),e=o((()=>{const e=t.signal.value;if(!e||"move"!==e.type||void 0===e.deltaY)return;const i=e.deltaY,{height:s}=l.viewBox.baseVal,{points:o}=h;for(let t=1;t<o.numberOfItems-1;t++){const e=o.getItem(t);e.y=Math.min(s,Math.max(0,e.y+i))}const n=l.querySelectorAll("ellipse");Array.from(n).forEach((t=>{const e=Math.min(s,Math.max(0,Number(t.getAttribute("cy"))+i));t.setAttribute("cy",e.toString())})),this.emit("line-move",i/s)}),[t.signal]);this.subscriptions.push((()=>{e(),t.cleanup()}))}this.dblClickListener=t=>{const e=l.getBoundingClientRect(),i=t.clientX-e.left,s=t.clientY-e.top;this.emit("point-create",i/e.width,s/e.height)},l.addEventListener("dblclick",this.dblClickListener);const a=()=>{void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0)};this.touchStartListener=t=>{1===t.touches.length?this.pressTimer=window.setTimeout((()=>{t.preventDefault();const e=l.getBoundingClientRect(),i=t.touches[0].clientX-e.left,s=t.touches[0].clientY-e.top;this.emit("point-create",i/e.width,s/e.height)}),500):a()},this.touchMoveListener=a,this.touchEndListener=a,l.addEventListener("touchstart",this.touchStartListener),l.addEventListener("touchmove",this.touchMoveListener),l.addEventListener("touchend",this.touchEndListener)}makeDraggable(t,e){const i=n(t,{threshold:1}),s=o((()=>{const s=i.signal.value;s&&("start"===s.type?t.style.cursor="grabbing":"move"===s.type&&void 0!==s.deltaX&&void 0!==s.deltaY?e(s.deltaX,s.deltaY):"end"===s.type&&(t.style.cursor="grab"))}),[i.signal]);this.subscriptions.push((()=>{s(),i.cleanup()}))}createCircle(t,e){const i=this.options.dragPointSize/2;return s("ellipse",{xmlns:"http://www.w3.org/2000/svg",cx:t,cy:e,rx:i,ry:i,fill:this.options.dragPointFill,stroke:this.options.dragPointStroke,"stroke-width":"2",style:{cursor:"grab",pointerEvents:"all"},part:"envelope-circle"},this.svg)}removePolyPoint(t){const e=this.polyPoints.get(t);if(!e)return;const{polyPoint:i,circle:s}=e,{points:o}=this.svg.querySelector("polyline"),n=Array.from(o).findIndex((t=>t.x===i.x&&t.y===i.y));o.removeItem(n),s.remove(),this.polyPoints.delete(t)}addPolyPoint(t,e,i){const{svg:s}=this,{width:o,height:n}=s.viewBox.baseVal,r=t*o,l=n-e*n,h=this.options.dragPointSize/2,a=s.createSVGPoint();a.x=t*o,a.y=n-e*n;const u=this.createCircle(r,l),{points:c}=s.querySelector("polyline"),d=Array.from(c).findIndex((t=>t.x>=r));c.insertItemBefore(a,Math.max(d,1)),this.polyPoints.set(i,{polyPoint:a,circle:u}),this.makeDraggable(u,((t,e)=>{const s=a.x+t,r=a.y+e;if(s<-h||r<-h||s>o+h||r>n+h)return void this.emit("point-dragout",i);const l=Array.from(c).find((t=>t.x>a.x)),d=Array.from(c).findLast((t=>t.x<a.x));l&&s>=l.x||d&&s<=d.x||(a.x=s,a.y=r,u.setAttribute("cx",s.toString()),u.setAttribute("cy",r.toString()),this.emit("point-move",i,s/o,r/n))}))}update(){const{svg:t}=this,{clientWidth:e,clientHeight:i}=t;if(!e||!i)return;const s=t.viewBox.baseVal.width/e,o=t.viewBox.baseVal.height/i;t.querySelectorAll("ellipse").forEach((t=>{const e=this.options.dragPointSize/2,i=e*s,n=e*o;t.setAttribute("rx",i.toString()),t.setAttribute("ry",n.toString())}))}destroy(){void 0!==this.pressTimer&&(clearTimeout(this.pressTimer),this.pressTimer=void 0),this.dblClickListener&&(this.svg.removeEventListener("dblclick",this.dblClickListener),this.dblClickListener=void 0),this.touchStartListener&&(this.svg.removeEventListener("touchstart",this.touchStartListener),this.touchStartListener=void 0),this.touchMoveListener&&(this.svg.removeEventListener("touchmove",this.touchMoveListener),this.touchMoveListener=void 0),this.touchEndListener&&(this.svg.removeEventListener("touchend",this.touchEndListener),this.touchEndListener=void 0),this.subscriptions.forEach((t=>t())),this.polyPoints.clear(),this.svg.remove()}}class h extends e{constructor(t){super(t),this.polyline=null,this.throttleTimeout=null,this.volume=1,this.points=t.points||[],this.options=Object.assign({},r,t),this.options.lineColor=this.options.lineColor||r.lineColor,this.options.dragPointFill=this.options.dragPointFill||r.dragPointFill,this.options.dragPointStroke=this.options.dragPointStroke||r.dragPointStroke,this.options.dragPointSize=this.options.dragPointSize||r.dragPointSize}static create(t){return new h(t)}addPoint(t){var e;t.id||(t.id=Math.random().toString(36).slice(2));const i=this.points.findLastIndex((e=>e.time<t.time));this.points.splice(i+1,0,t),this.emitPoints();const s=null===(e=this.wavesurfer)||void 0===e?void 0:e.getDuration();s&&this.addPolyPoint(t,s)}removePoint(t){var e;const i=this.points.indexOf(t);i>-1&&(this.points.splice(i,1),null===(e=this.polyline)||void 0===e||e.removePolyPoint(t),this.emitPoints())}getPoints(){return this.points}setPoints(t){this.points.slice().forEach((t=>this.removePoint(t))),t.forEach((t=>this.addPoint(t)))}destroy(){var t;this.throttleTimeout&&(clearTimeout(this.throttleTimeout),this.throttleTimeout=null),null===(t=this.polyline)||void 0===t||t.destroy(),super.destroy()}getCurrentVolume(){return this.volume}setVolume(t){var e;this.volume=t,null===(e=this.wavesurfer)||void 0===e||e.setVolume(t)}onInit(){var t;if(!this.wavesurfer)throw Error("WaveSurfer is not initialized");const{options:e}=this;e.volume=null!==(t=e.volume)&&void 0!==t?t:this.wavesurfer.getVolume(),this.setVolume(e.volume),this.subscriptions.push(this.wavesurfer.on("decode",(t=>{this.initPolyline(),this.points.forEach((e=>{this.addPolyPoint(e,t)}))})),this.wavesurfer.on("redraw",(()=>{var t;null===(t=this.polyline)||void 0===t||t.update()})),this.wavesurfer.on("timeupdate",(t=>{this.onTimeUpdate(t)})))}emitPoints(){this.throttleTimeout&&clearTimeout(this.throttleTimeout),this.throttleTimeout=setTimeout((()=>{this.emit("points-change",this.points)}),200)}initPolyline(){if(this.polyline&&this.polyline.destroy(),!this.wavesurfer)return;const t=this.wavesurfer.getWrapper();this.polyline=new l(this.options,t),this.subscriptions.push(this.polyline.on("point-move",((t,e,i)=>{var s;const o=(null===(s=this.wavesurfer)||void 0===s?void 0:s.getDuration())||0;t.time=e*o,t.volume=1-i,this.emitPoints()})),this.polyline.on("point-dragout",(t=>{this.removePoint(t)})),this.polyline.on("point-create",((t,e)=>{var i;this.addPoint({time:t*((null===(i=this.wavesurfer)||void 0===i?void 0:i.getDuration())||0),volume:1-e})})),this.polyline.on("line-move",(t=>{var e;this.points.forEach((e=>{e.volume=Math.min(1,Math.max(0,e.volume-t))})),this.emitPoints(),this.onTimeUpdate((null===(e=this.wavesurfer)||void 0===e?void 0:e.getCurrentTime())||0)})))}addPolyPoint(t,e){var i;null===(i=this.polyline)||void 0===i||i.addPolyPoint(t.time/e,t.volume,t)}onTimeUpdate(t){if(!this.wavesurfer)return;let e=this.points.find((e=>e.time>t));e||(e={time:this.wavesurfer.getDuration()||0,volume:0});let i=this.points.findLast((e=>e.time<=t));i||(i={time:0,volume:0});const s=e.time-i.time,o=e.volume-i.volume,n=i.volume+(t-i.time)*(o/s),r=Math.min(1,Math.max(0,n)),l=Math.round(100*r)/100;l!==this.getCurrentVolume()&&(this.setVolume(l),this.emit("volume-change",l))}}return h}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"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()}}module.exports=l;
|