@meersagor/wavesurfer-vue 0.0.5 → 0.1.1

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.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("vue"),b=require("wavesurfer.js"),R=({containerRef:n,options:e})=>{const i=a.ref(null),t=()=>{n.value&&(i.value=b.create({container:n.value,...e}))},r=()=>{i.value&&(i.value.destroy(),i.value=null)};return a.onMounted(()=>{t()}),a.onUnmounted(()=>{r()}),{waveSurfer:i}},M=n=>{const e=a.ref(!1),i=a.ref(!1),t=a.ref(0),r=a.ref(0),o=()=>{e.value=!1,i.value=!1,t.value=0},s=m=>{e.value=!0,i.value=!1,t.value=0,r.value=m},u=()=>{i.value=!0},d=()=>{i.value=!1},c=()=>{n.value&&(t.value=n.value.getCurrentTime())},l=()=>{e.value=!1,i.value=!1};return a.onMounted(()=>{n.value&&(n.value.on("load",o),n.value.on("ready",s),n.value.on("play",u),n.value.on("pause",d),n.value.on("timeupdate",c),n.value.on("destroy",l))}),a.onUnmounted(()=>{n.value&&n.value.unAll()}),{isReady:e,isPlaying:i,currentTime:t,totalDuration:r}},y=({containerRef:n,options:e})=>{const{waveSurfer:i}=R({containerRef:n,options:e}),{isReady:t,totalDuration:r,isPlaying:o,currentTime:s}=M(i);return{waveSurfer:i,isReady:t,totalDuration:r,isPlaying:o,currentTime:s}},D=["audioprocess","click","dblclick","decode","drag","finish","init","interaction","load","loading","pause","play","ready","redraw","redrawcomplete","scroll","seeking","timeupdate","zoom"],T=a.defineComponent({__name:"WaveSurferPlayer",props:{options:{}},setup(n){const e=n,i=a.ref(null),{waveSurfer:t}=y({containerRef:i,options:e.options}),r=a.getCurrentInstance();return a.onMounted(()=>{D.forEach(o=>{t.value?.on(o,(...s)=>{r?.emit(o,...s)})}),r?.emit("waveSurfer",t.value)}),(o,s)=>(a.openBlock(),a.createElementBlock("div",{ref_key:"containerRef",ref:i},null,512))}});function w(n,e,i,t){return new(i||(i=Promise))(function(r,o){function s(c){try{d(t.next(c))}catch(l){o(l)}}function u(c){try{d(t.throw(c))}catch(l){o(l)}}function d(c){var l;c.done?r(c.value):(l=c.value,l instanceof i?l:new i(function(m){m(l)})).then(s,u)}d((t=t.apply(n,[])).next())})}class S{constructor(){this.listeners={}}on(e,i,t){if(this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(i),t?.once){const r=()=>{this.un(e,r),this.un(e,i)};return this.on(e,r),r}return()=>this.un(e,i)}un(e,i){var t;(t=this.listeners[e])===null||t===void 0||t.delete(i)}once(e,i){return this.on(e,i,{once:!0})}unAll(){this.listeners={}}emit(e,...i){this.listeners[e]&&this.listeners[e].forEach(t=>t(...i))}}class O extends S{constructor(e){super(),this.subscriptions=[],this.options=e}onInit(){}_init(e){this.wavesurfer=e,this.onInit()}destroy(){this.emit("destroy"),this.subscriptions.forEach(e=>e())}}class A extends S{constructor(){super(...arguments),this.unsubscribe=()=>{}}start(){this.unsubscribe=this.on("tick",()=>{requestAnimationFrame(()=>{this.emit("tick")})}),this.emit("tick")}stop(){this.unsubscribe()}destroy(){this.unsubscribe()}}const E=["audio/webm","audio/wav","audio/mpeg","audio/mp4","audio/mp3"];class W extends O{constructor(e){var i,t,r,o,s,u;super(Object.assign(Object.assign({},e),{audioBitsPerSecond:(i=e.audioBitsPerSecond)!==null&&i!==void 0?i:128e3,scrollingWaveform:(t=e.scrollingWaveform)!==null&&t!==void 0&&t,scrollingWaveformWindow:(r=e.scrollingWaveformWindow)!==null&&r!==void 0?r:5,continuousWaveform:(o=e.continuousWaveform)!==null&&o!==void 0&&o,renderRecordedAudio:(s=e.renderRecordedAudio)===null||s===void 0||s,mediaRecorderTimeslice:(u=e.mediaRecorderTimeslice)!==null&&u!==void 0?u:void 0})),this.stream=null,this.mediaRecorder=null,this.dataWindow=null,this.isWaveformPaused=!1,this.lastStartTime=0,this.lastDuration=0,this.duration=0,this.timer=new A,this.subscriptions.push(this.timer.on("tick",()=>{const d=performance.now()-this.lastStartTime;this.duration=this.isPaused()?this.duration:this.lastDuration+d,this.emit("record-progress",this.duration)}))}static create(e){return new W(e||{})}renderMicStream(e){var i;const t=new AudioContext,r=t.createMediaStreamSource(e),o=t.createAnalyser();r.connect(o),this.options.continuousWaveform&&(o.fftSize=32);const s=o.frequencyBinCount,u=new Float32Array(s);let d=0;this.wavesurfer&&((i=this.originalOptions)!==null&&i!==void 0||(this.originalOptions=Object.assign({},this.wavesurfer.options)),this.wavesurfer.options.interact=!1,this.options.scrollingWaveform&&(this.wavesurfer.options.cursorWidth=0));const c=setInterval(()=>{var l,m,g,h;if(!this.isWaveformPaused){if(o.getFloatTimeDomainData(u),this.options.scrollingWaveform){const f=Math.floor((this.options.scrollingWaveformWindow||0)*t.sampleRate),v=Math.min(f,this.dataWindow?this.dataWindow.length+s:s),p=new Float32Array(f);if(this.dataWindow){const P=Math.max(0,f-this.dataWindow.length);p.set(this.dataWindow.slice(-v+s),P)}p.set(u,f-s),this.dataWindow=p}else if(this.options.continuousWaveform){if(!this.dataWindow){const v=this.options.continuousWaveformDuration?Math.round(100*this.options.continuousWaveformDuration):((m=(l=this.wavesurfer)===null||l===void 0?void 0:l.getWidth())!==null&&m!==void 0?m:0)*window.devicePixelRatio;this.dataWindow=new Float32Array(v)}let f=0;for(let v=0;v<s;v++){const p=Math.abs(u[v]);p>f&&(f=p)}if(d+1>this.dataWindow.length){const v=new Float32Array(2*this.dataWindow.length);v.set(this.dataWindow,0),this.dataWindow=v}this.dataWindow[d]=f,d++}else this.dataWindow=u;if(this.wavesurfer){const f=((h=(g=this.dataWindow)===null||g===void 0?void 0:g.length)!==null&&h!==void 0?h:0)/100;this.wavesurfer.load("",[this.dataWindow],this.options.scrollingWaveform?this.options.scrollingWaveformWindow:f).then(()=>{this.wavesurfer&&this.options.continuousWaveform&&(this.wavesurfer.setTime(this.getDuration()/1e3),this.wavesurfer.options.minPxPerSec||this.wavesurfer.setOptions({minPxPerSec:this.wavesurfer.getWidth()/this.wavesurfer.getDuration()}))}).catch(v=>{console.error("Error rendering real-time recording data:",v)})}}},10);return{onDestroy:()=>{clearInterval(c),r?.disconnect(),t?.close()},onEnd:()=>{this.isWaveformPaused=!0,clearInterval(c),this.stopMic()}}}startMic(e){return w(this,void 0,void 0,function*(){let i;try{i=yield navigator.mediaDevices.getUserMedia({audio:!e?.deviceId||{deviceId:e.deviceId}})}catch(o){throw new Error("Error accessing the microphone: "+o.message)}const{onDestroy:t,onEnd:r}=this.renderMicStream(i);return this.subscriptions.push(this.once("destroy",t)),this.subscriptions.push(this.once("record-end",r)),this.stream=i,i})}stopMic(){this.stream&&(this.stream.getTracks().forEach(e=>e.stop()),this.stream=null,this.mediaRecorder=null)}startRecording(e){return w(this,void 0,void 0,function*(){const i=this.stream||(yield this.startMic(e));this.dataWindow=null;const t=this.mediaRecorder||new MediaRecorder(i,{mimeType:this.options.mimeType||E.find(s=>MediaRecorder.isTypeSupported(s)),audioBitsPerSecond:this.options.audioBitsPerSecond});this.mediaRecorder=t,this.stopRecording();const r=[];t.ondataavailable=s=>{s.data.size>0&&r.push(s.data),this.emit("record-data-available",s.data)};const o=s=>{var u;const d=new Blob(r,{type:t.mimeType});this.emit(s,d),this.options.renderRecordedAudio&&(this.applyOriginalOptionsIfNeeded(),(u=this.wavesurfer)===null||u===void 0||u.load(URL.createObjectURL(d)))};t.onpause=()=>o("record-pause"),t.onstop=()=>o("record-end"),t.start(this.options.mediaRecorderTimeslice),this.lastStartTime=performance.now(),this.lastDuration=0,this.duration=0,this.isWaveformPaused=!1,this.timer.start(),this.emit("record-start")})}getDuration(){return this.duration}isRecording(){var e;return((e=this.mediaRecorder)===null||e===void 0?void 0:e.state)==="recording"}isPaused(){var e;return((e=this.mediaRecorder)===null||e===void 0?void 0:e.state)==="paused"}isActive(){var e;return((e=this.mediaRecorder)===null||e===void 0?void 0:e.state)!=="inactive"}stopRecording(){var e;this.isActive()&&((e=this.mediaRecorder)===null||e===void 0||e.stop(),this.timer.stop())}pauseRecording(){var e,i;this.isRecording()&&(this.isWaveformPaused=!0,(e=this.mediaRecorder)===null||e===void 0||e.requestData(),(i=this.mediaRecorder)===null||i===void 0||i.pause(),this.timer.stop(),this.lastDuration=this.duration)}resumeRecording(){var e;this.isPaused()&&(this.isWaveformPaused=!1,(e=this.mediaRecorder)===null||e===void 0||e.resume(),this.timer.start(),this.lastStartTime=performance.now(),this.emit("record-resume"))}static getAvailableAudioDevices(){return w(this,void 0,void 0,function*(){return navigator.mediaDevices.enumerateDevices().then(e=>e.filter(i=>i.kind==="audioinput"))})}destroy(){this.applyOriginalOptionsIfNeeded(),super.destroy(),this.stopRecording(),this.stopMic()}applyOriginalOptionsIfNeeded(){this.wavesurfer&&this.originalOptions&&(this.wavesurfer.setOptions(this.originalOptions),delete this.originalOptions)}}const I=({containerRef:n,options:e})=>{const{waveSurfer:i}=R({containerRef:n,options:e}),t=a.ref(null),r=a.ref(0),o=a.ref(!1),s=a.ref(!1),u=a.computed(()=>[Math.floor(r.value%36e5/6e4),Math.floor(r.value%6e4/1e3)].map(h=>h<10?"0"+h:h).join(":")),d=a.computed(()=>o.value||!s.value),c=()=>{t.value&&t.value?.on("record-progress",h=>{r.value=h})},l=()=>{if(t.value?.isRecording()||t.value?.isPaused()){t.value?.stopRecording(),o.value=!1,s.value=!0;return}t.value?.startRecording(),o.value=!0,s.value=!1,c()},m=()=>new Promise(h=>{let f;(t.value?.isRecording()||t.value?.isPaused())&&(t.value?.stopRecording(),o.value=!1,s.value=!1),t.value?.on("record-end",v=>{f=v,h(f)})}),g=()=>{if(t.value?.isPaused()){t.value?.resumeRecording(),o.value=!0,s.value=!1;return}o.value=!1,s.value=!0,t.value?.pauseRecording()};return a.onMounted(()=>{const h=i.value?.registerPlugin(W.create({renderRecordedAudio:!1}));h&&(t.value=h)}),{waveSurfer:i,waveSurferRecorder:t,currentTime:u,startRecording:l,stopRecording:m,pauseRecording:g,isRecording:o,isPaused:s,isPauseResume:d}};exports.WaveSurferPlayer=T;exports.useWaveSurfer=y;exports.useWaveSurferRecorder=I;
@@ -1,139 +1,124 @@
1
- import { ref as v, onMounted as g, onUnmounted as b, defineComponent as T, getCurrentInstance as O, openBlock as k, createElementBlock as E, computed as P } from "vue";
2
- import I from "wavesurfer.js";
3
- const A = ({ containerRef: n, options: e }) => {
4
- const t = v(null), i = () => {
5
- n.value && (t.value = I.create({
1
+ import { ref as m, onMounted as w, onUnmounted as S, defineComponent as T, getCurrentInstance as M, openBlock as A, createElementBlock as O, computed as y } from "vue";
2
+ import E from "wavesurfer.js";
3
+ const P = ({ containerRef: n, options: e }) => {
4
+ const i = m(null), t = () => {
5
+ n.value && (i.value = E.create({
6
6
  container: n.value,
7
7
  ...e
8
8
  }));
9
- }, s = () => {
10
- t.value && (t.value.destroy(), t.value = null);
9
+ }, o = () => {
10
+ i.value && (i.value.destroy(), i.value = null);
11
11
  };
12
- return g(() => {
13
- i();
14
- }), b(() => {
15
- s();
16
- }), { waveSurfer: t };
17
- }, x = (n) => {
18
- const e = v(!1), t = v(!1), i = v(0), s = v(0), r = () => {
19
- e.value = !1, t.value = !1, i.value = 0;
20
- }, o = (p) => {
21
- e.value = !0, t.value = !1, i.value = 0, s.value = p;
12
+ return w(() => {
13
+ t();
14
+ }), S(() => {
15
+ o();
16
+ }), { waveSurfer: i };
17
+ }, I = (n) => {
18
+ const e = m(!1), i = m(!1), t = m(0), o = m(0), r = () => {
19
+ e.value = !1, i.value = !1, t.value = 0;
20
+ }, s = (f) => {
21
+ e.value = !0, i.value = !1, t.value = 0, o.value = f;
22
+ }, a = () => {
23
+ i.value = !0;
22
24
  }, u = () => {
23
- t.value = !0;
24
- }, d = () => {
25
- t.value = !1;
25
+ i.value = !1;
26
26
  }, l = () => {
27
- n.value && (i.value = n.value.getCurrentTime());
28
- }, c = () => {
29
- e.value = !1, t.value = !1;
27
+ n.value && (t.value = n.value.getCurrentTime());
28
+ }, d = () => {
29
+ e.value = !1, i.value = !1;
30
30
  };
31
- return g(() => {
32
- n.value && (n.value.on("load", r), n.value.on("ready", o), n.value.on("play", u), n.value.on("pause", d), n.value.on("timeupdate", l), n.value.on("destroy", c));
33
- }), b(() => {
31
+ return w(() => {
32
+ n.value && (n.value.on("load", r), n.value.on("ready", s), n.value.on("play", a), n.value.on("pause", u), n.value.on("timeupdate", l), n.value.on("destroy", d));
33
+ }), S(() => {
34
34
  n.value && n.value.unAll();
35
35
  }), {
36
36
  isReady: e,
37
- isPlaying: t,
38
- currentTime: i,
39
- totalDuration: s
37
+ isPlaying: i,
38
+ currentTime: t,
39
+ totalDuration: o
40
40
  };
41
- }, B = ({ containerRef: n, options: e }) => {
42
- const { waveSurfer: t } = A({ containerRef: n, options: e }), { isReady: i, totalDuration: s, isPlaying: r, currentTime: o } = x(t);
41
+ }, k = ({ containerRef: n, options: e }) => {
42
+ const { waveSurfer: i } = P({ containerRef: n, options: e }), { isReady: t, totalDuration: o, isPlaying: r, currentTime: s } = I(i);
43
43
  return {
44
- waveSurfer: t,
45
- isReady: i,
46
- totalDuration: s,
44
+ waveSurfer: i,
45
+ isReady: t,
46
+ totalDuration: o,
47
47
  isPlaying: r,
48
- currentTime: o
48
+ currentTime: s
49
49
  };
50
- }, F = ["audioprocess", "click", "dblclick", "decode", "drag", "finish", "init", "interaction", "load", "loading", "pause", "play", "ready", "redraw", "redrawcomplete", "scroll", "seeking", "timeupdate", "zoom"], L = /* @__PURE__ */ T({
50
+ }, x = ["audioprocess", "click", "dblclick", "decode", "drag", "finish", "init", "interaction", "load", "loading", "pause", "play", "ready", "redraw", "redrawcomplete", "scroll", "seeking", "timeupdate", "zoom"], U = /* @__PURE__ */ T({
51
51
  __name: "WaveSurferPlayer",
52
52
  props: {
53
53
  options: {}
54
54
  },
55
55
  setup(n) {
56
- const e = n, t = v(null), { waveSurfer: i } = B({ containerRef: t, options: e.options }), s = O();
57
- return g(() => {
58
- F.forEach((r) => {
59
- var o;
60
- (o = i.value) == null || o.on(r, (...u) => {
61
- s == null || s.emit(r, ...u);
56
+ const e = n, i = m(null), { waveSurfer: t } = k({ containerRef: i, options: e.options }), o = M();
57
+ return w(() => {
58
+ x.forEach((r) => {
59
+ t.value?.on(r, (...s) => {
60
+ o?.emit(r, ...s);
62
61
  });
63
- }), s == null || s.emit("waveSurfer", i.value);
64
- }), (r, o) => (k(), E("div", {
62
+ }), o?.emit("waveSurfer", t.value);
63
+ }), (r, s) => (A(), O("div", {
65
64
  ref_key: "containerRef",
66
- ref: t
65
+ ref: i
67
66
  }, null, 512));
68
67
  }
69
68
  });
70
- /*! *****************************************************************************
71
- Copyright (c) Microsoft Corporation.
72
-
73
- Permission to use, copy, modify, and/or distribute this software for any
74
- purpose with or without fee is hereby granted.
75
-
76
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
77
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
78
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
79
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
80
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
81
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
82
- PERFORMANCE OF THIS SOFTWARE.
83
- ***************************************************************************** */
84
- function w(n, e, t, i) {
85
- return new (t || (t = Promise))(function(s, r) {
86
- function o(l) {
69
+ function W(n, e, i, t) {
70
+ return new (i || (i = Promise))(function(o, r) {
71
+ function s(l) {
87
72
  try {
88
- d(i.next(l));
89
- } catch (c) {
90
- r(c);
73
+ u(t.next(l));
74
+ } catch (d) {
75
+ r(d);
91
76
  }
92
77
  }
93
- function u(l) {
78
+ function a(l) {
94
79
  try {
95
- d(i.throw(l));
96
- } catch (c) {
97
- r(c);
80
+ u(t.throw(l));
81
+ } catch (d) {
82
+ r(d);
98
83
  }
99
84
  }
100
- function d(l) {
101
- var c;
102
- l.done ? s(l.value) : (c = l.value, c instanceof t ? c : new t(function(p) {
103
- p(c);
104
- })).then(o, u);
85
+ function u(l) {
86
+ var d;
87
+ l.done ? o(l.value) : (d = l.value, d instanceof i ? d : new i(function(f) {
88
+ f(d);
89
+ })).then(s, a);
105
90
  }
106
- d((i = i.apply(n, e || [])).next());
91
+ u((t = t.apply(n, [])).next());
107
92
  });
108
93
  }
109
- class D {
94
+ class b {
110
95
  constructor() {
111
96
  this.listeners = {};
112
97
  }
113
- on(e, t, i) {
114
- if (this.listeners[e] || (this.listeners[e] = /* @__PURE__ */ new Set()), this.listeners[e].add(t), i == null ? void 0 : i.once) {
115
- const s = () => {
116
- this.un(e, s), this.un(e, t);
98
+ on(e, i, t) {
99
+ if (this.listeners[e] || (this.listeners[e] = /* @__PURE__ */ new Set()), this.listeners[e].add(i), t?.once) {
100
+ const o = () => {
101
+ this.un(e, o), this.un(e, i);
117
102
  };
118
- return this.on(e, s), s;
103
+ return this.on(e, o), o;
119
104
  }
120
- return () => this.un(e, t);
105
+ return () => this.un(e, i);
121
106
  }
122
- un(e, t) {
123
- var i;
124
- (i = this.listeners[e]) === null || i === void 0 || i.delete(t);
107
+ un(e, i) {
108
+ var t;
109
+ (t = this.listeners[e]) === null || t === void 0 || t.delete(i);
125
110
  }
126
- once(e, t) {
127
- return this.on(e, t, { once: !0 });
111
+ once(e, i) {
112
+ return this.on(e, i, { once: !0 });
128
113
  }
129
114
  unAll() {
130
115
  this.listeners = {};
131
116
  }
132
- emit(e, ...t) {
133
- this.listeners[e] && this.listeners[e].forEach((i) => i(...t));
117
+ emit(e, ...i) {
118
+ this.listeners[e] && this.listeners[e].forEach((t) => t(...i));
134
119
  }
135
120
  }
136
- class _ extends D {
121
+ class B extends b {
137
122
  constructor(e) {
138
123
  super(), this.subscriptions = [], this.options = e;
139
124
  }
@@ -146,7 +131,7 @@ class _ extends D {
146
131
  this.emit("destroy"), this.subscriptions.forEach((e) => e());
147
132
  }
148
133
  }
149
- class q extends D {
134
+ class _ extends b {
150
135
  constructor() {
151
136
  super(...arguments), this.unsubscribe = () => {
152
137
  };
@@ -165,76 +150,98 @@ class q extends D {
165
150
  this.unsubscribe();
166
151
  }
167
152
  }
168
- const C = ["audio/webm", "audio/wav", "audio/mpeg", "audio/mp4", "audio/mp3"];
169
- class y extends _ {
153
+ const F = ["audio/webm", "audio/wav", "audio/mpeg", "audio/mp4", "audio/mp3"];
154
+ class R extends B {
170
155
  constructor(e) {
171
- var t, i, s, r;
172
- super(Object.assign(Object.assign({}, e), { audioBitsPerSecond: (t = e.audioBitsPerSecond) !== null && t !== void 0 ? t : 128e3, scrollingWaveform: (i = e.scrollingWaveform) !== null && i !== void 0 && i, scrollingWaveformWindow: (s = e.scrollingWaveformWindow) !== null && s !== void 0 ? s : 5, renderRecordedAudio: (r = e.renderRecordedAudio) === null || r === void 0 || r })), this.stream = null, this.mediaRecorder = null, this.dataWindow = null, this.isWaveformPaused = !1, this.lastStartTime = 0, this.lastDuration = 0, this.duration = 0, this.timer = new q(), this.subscriptions.push(this.timer.on("tick", () => {
173
- const o = performance.now() - this.lastStartTime;
174
- this.duration = this.isPaused() ? this.duration : this.lastDuration + o, this.emit("record-progress", this.duration);
156
+ var i, t, o, r, s, a;
157
+ super(Object.assign(Object.assign({}, e), { audioBitsPerSecond: (i = e.audioBitsPerSecond) !== null && i !== void 0 ? i : 128e3, scrollingWaveform: (t = e.scrollingWaveform) !== null && t !== void 0 && t, scrollingWaveformWindow: (o = e.scrollingWaveformWindow) !== null && o !== void 0 ? o : 5, continuousWaveform: (r = e.continuousWaveform) !== null && r !== void 0 && r, renderRecordedAudio: (s = e.renderRecordedAudio) === null || s === void 0 || s, mediaRecorderTimeslice: (a = e.mediaRecorderTimeslice) !== null && a !== void 0 ? a : void 0 })), this.stream = null, this.mediaRecorder = null, this.dataWindow = null, this.isWaveformPaused = !1, this.lastStartTime = 0, this.lastDuration = 0, this.duration = 0, this.timer = new _(), this.subscriptions.push(this.timer.on("tick", () => {
158
+ const u = performance.now() - this.lastStartTime;
159
+ this.duration = this.isPaused() ? this.duration : this.lastDuration + u, this.emit("record-progress", this.duration);
175
160
  }));
176
161
  }
177
162
  static create(e) {
178
- return new y(e || {});
163
+ return new R(e || {});
179
164
  }
180
165
  renderMicStream(e) {
181
- const t = new AudioContext(), i = t.createMediaStreamSource(e), s = t.createAnalyser();
182
- i.connect(s);
183
- const r = s.frequencyBinCount, o = new Float32Array(r);
184
- let u;
185
- const d = Math.floor((this.options.scrollingWaveformWindow || 0) * t.sampleRate), l = () => {
186
- var c;
187
- if (this.isWaveformPaused)
188
- return void (u = requestAnimationFrame(l));
189
- if (s.getFloatTimeDomainData(o), this.options.scrollingWaveform) {
190
- const R = Math.min(d, this.dataWindow ? this.dataWindow.length + r : r), a = new Float32Array(d);
191
- if (this.dataWindow) {
192
- const h = Math.max(0, d - this.dataWindow.length);
193
- a.set(this.dataWindow.slice(-R + r), h);
166
+ var i;
167
+ const t = new AudioContext(), o = t.createMediaStreamSource(e), r = t.createAnalyser();
168
+ o.connect(r), this.options.continuousWaveform && (r.fftSize = 32);
169
+ const s = r.frequencyBinCount, a = new Float32Array(s);
170
+ let u = 0;
171
+ this.wavesurfer && ((i = this.originalOptions) !== null && i !== void 0 || (this.originalOptions = Object.assign({}, this.wavesurfer.options)), this.wavesurfer.options.interact = !1, this.options.scrollingWaveform && (this.wavesurfer.options.cursorWidth = 0));
172
+ const l = setInterval(() => {
173
+ var d, f, g, c;
174
+ if (!this.isWaveformPaused) {
175
+ if (r.getFloatTimeDomainData(a), this.options.scrollingWaveform) {
176
+ const v = Math.floor((this.options.scrollingWaveformWindow || 0) * t.sampleRate), h = Math.min(v, this.dataWindow ? this.dataWindow.length + s : s), p = new Float32Array(v);
177
+ if (this.dataWindow) {
178
+ const D = Math.max(0, v - this.dataWindow.length);
179
+ p.set(this.dataWindow.slice(-h + s), D);
180
+ }
181
+ p.set(a, v - s), this.dataWindow = p;
182
+ } else if (this.options.continuousWaveform) {
183
+ if (!this.dataWindow) {
184
+ const h = this.options.continuousWaveformDuration ? Math.round(100 * this.options.continuousWaveformDuration) : ((f = (d = this.wavesurfer) === null || d === void 0 ? void 0 : d.getWidth()) !== null && f !== void 0 ? f : 0) * window.devicePixelRatio;
185
+ this.dataWindow = new Float32Array(h);
186
+ }
187
+ let v = 0;
188
+ for (let h = 0; h < s; h++) {
189
+ const p = Math.abs(a[h]);
190
+ p > v && (v = p);
191
+ }
192
+ if (u + 1 > this.dataWindow.length) {
193
+ const h = new Float32Array(2 * this.dataWindow.length);
194
+ h.set(this.dataWindow, 0), this.dataWindow = h;
195
+ }
196
+ this.dataWindow[u] = v, u++;
197
+ } else this.dataWindow = a;
198
+ if (this.wavesurfer) {
199
+ const v = ((c = (g = this.dataWindow) === null || g === void 0 ? void 0 : g.length) !== null && c !== void 0 ? c : 0) / 100;
200
+ this.wavesurfer.load("", [this.dataWindow], this.options.scrollingWaveform ? this.options.scrollingWaveformWindow : v).then(() => {
201
+ this.wavesurfer && this.options.continuousWaveform && (this.wavesurfer.setTime(this.getDuration() / 1e3), this.wavesurfer.options.minPxPerSec || this.wavesurfer.setOptions({ minPxPerSec: this.wavesurfer.getWidth() / this.wavesurfer.getDuration() }));
202
+ }).catch((h) => {
203
+ console.error("Error rendering real-time recording data:", h);
204
+ });
194
205
  }
195
- a.set(o, d - r), this.dataWindow = a;
196
- } else
197
- this.dataWindow = o;
198
- const p = this.options.scrollingWaveformWindow;
199
- this.wavesurfer && ((c = this.originalOptions) !== null && c !== void 0 || (this.originalOptions = { cursorWidth: this.wavesurfer.options.cursorWidth, interact: this.wavesurfer.options.interact }), this.wavesurfer.options.cursorWidth = 0, this.wavesurfer.options.interact = !1, this.wavesurfer.load("", [this.dataWindow], p)), u = requestAnimationFrame(l);
200
- };
201
- return l(), { onDestroy: () => {
202
- cancelAnimationFrame(u), i == null || i.disconnect(), t == null || t.close();
206
+ }
207
+ }, 10);
208
+ return { onDestroy: () => {
209
+ clearInterval(l), o?.disconnect(), t?.close();
203
210
  }, onEnd: () => {
204
- this.isWaveformPaused = !0, cancelAnimationFrame(u), this.stopMic();
211
+ this.isWaveformPaused = !0, clearInterval(l), this.stopMic();
205
212
  } };
206
213
  }
207
214
  startMic(e) {
208
- return w(this, void 0, void 0, function* () {
209
- let t;
215
+ return W(this, void 0, void 0, function* () {
216
+ let i;
210
217
  try {
211
- t = yield navigator.mediaDevices.getUserMedia({ audio: !(e != null && e.deviceId) || { deviceId: e.deviceId } });
218
+ i = yield navigator.mediaDevices.getUserMedia({ audio: !e?.deviceId || { deviceId: e.deviceId } });
212
219
  } catch (r) {
213
220
  throw new Error("Error accessing the microphone: " + r.message);
214
221
  }
215
- const { onDestroy: i, onEnd: s } = this.renderMicStream(t);
216
- return this.subscriptions.push(this.once("destroy", i)), this.subscriptions.push(this.once("record-end", s)), this.stream = t, t;
222
+ const { onDestroy: t, onEnd: o } = this.renderMicStream(i);
223
+ return this.subscriptions.push(this.once("destroy", t)), this.subscriptions.push(this.once("record-end", o)), this.stream = i, i;
217
224
  });
218
225
  }
219
226
  stopMic() {
220
227
  this.stream && (this.stream.getTracks().forEach((e) => e.stop()), this.stream = null, this.mediaRecorder = null);
221
228
  }
222
229
  startRecording(e) {
223
- return w(this, void 0, void 0, function* () {
224
- const t = this.stream || (yield this.startMic(e));
230
+ return W(this, void 0, void 0, function* () {
231
+ const i = this.stream || (yield this.startMic(e));
225
232
  this.dataWindow = null;
226
- const i = this.mediaRecorder || new MediaRecorder(t, { mimeType: this.options.mimeType || C.find((o) => MediaRecorder.isTypeSupported(o)), audioBitsPerSecond: this.options.audioBitsPerSecond });
227
- this.mediaRecorder = i, this.stopRecording();
228
- const s = [];
229
- i.ondataavailable = (o) => {
230
- o.data.size > 0 && s.push(o.data);
233
+ const t = this.mediaRecorder || new MediaRecorder(i, { mimeType: this.options.mimeType || F.find((s) => MediaRecorder.isTypeSupported(s)), audioBitsPerSecond: this.options.audioBitsPerSecond });
234
+ this.mediaRecorder = t, this.stopRecording();
235
+ const o = [];
236
+ t.ondataavailable = (s) => {
237
+ s.data.size > 0 && o.push(s.data), this.emit("record-data-available", s.data);
231
238
  };
232
- const r = (o) => {
233
- var u;
234
- const d = new Blob(s, { type: i.mimeType });
235
- this.emit(o, d), this.options.renderRecordedAudio && (this.applyOriginalOptionsIfNeeded(), (u = this.wavesurfer) === null || u === void 0 || u.load(URL.createObjectURL(d)));
239
+ const r = (s) => {
240
+ var a;
241
+ const u = new Blob(o, { type: t.mimeType });
242
+ this.emit(s, u), this.options.renderRecordedAudio && (this.applyOriginalOptionsIfNeeded(), (a = this.wavesurfer) === null || a === void 0 || a.load(URL.createObjectURL(u)));
236
243
  };
237
- i.onpause = () => r("record-pause"), i.onstop = () => r("record-end"), i.start(), this.lastStartTime = performance.now(), this.lastDuration = 0, this.duration = 0, this.isWaveformPaused = !1, this.timer.start(), this.emit("record-start");
244
+ t.onpause = () => r("record-pause"), t.onstop = () => r("record-end"), t.start(this.options.mediaRecorderTimeslice), this.lastStartTime = performance.now(), this.lastDuration = 0, this.duration = 0, this.isWaveformPaused = !1, this.timer.start(), this.emit("record-start");
238
245
  });
239
246
  }
240
247
  getDuration() {
@@ -257,75 +264,70 @@ class y extends _ {
257
264
  this.isActive() && ((e = this.mediaRecorder) === null || e === void 0 || e.stop(), this.timer.stop());
258
265
  }
259
266
  pauseRecording() {
260
- var e, t;
261
- this.isRecording() && (this.isWaveformPaused = !0, (e = this.mediaRecorder) === null || e === void 0 || e.requestData(), (t = this.mediaRecorder) === null || t === void 0 || t.pause(), this.timer.stop(), this.lastDuration = this.duration);
267
+ var e, i;
268
+ this.isRecording() && (this.isWaveformPaused = !0, (e = this.mediaRecorder) === null || e === void 0 || e.requestData(), (i = this.mediaRecorder) === null || i === void 0 || i.pause(), this.timer.stop(), this.lastDuration = this.duration);
262
269
  }
263
270
  resumeRecording() {
264
271
  var e;
265
272
  this.isPaused() && (this.isWaveformPaused = !1, (e = this.mediaRecorder) === null || e === void 0 || e.resume(), this.timer.start(), this.lastStartTime = performance.now(), this.emit("record-resume"));
266
273
  }
267
274
  static getAvailableAudioDevices() {
268
- return w(this, void 0, void 0, function* () {
269
- return navigator.mediaDevices.enumerateDevices().then((e) => e.filter((t) => t.kind === "audioinput"));
275
+ return W(this, void 0, void 0, function* () {
276
+ return navigator.mediaDevices.enumerateDevices().then((e) => e.filter((i) => i.kind === "audioinput"));
270
277
  });
271
278
  }
272
279
  destroy() {
273
280
  this.applyOriginalOptionsIfNeeded(), super.destroy(), this.stopRecording(), this.stopMic();
274
281
  }
275
282
  applyOriginalOptionsIfNeeded() {
276
- this.wavesurfer && this.originalOptions && (this.wavesurfer.options.cursorWidth = this.originalOptions.cursorWidth, this.wavesurfer.options.interact = this.originalOptions.interact, delete this.originalOptions);
283
+ this.wavesurfer && this.originalOptions && (this.wavesurfer.setOptions(this.originalOptions), delete this.originalOptions);
277
284
  }
278
285
  }
279
- const z = ({ containerRef: n, options: e }) => {
280
- const { waveSurfer: t } = A({ containerRef: n, options: e }), i = v(null), s = v(0), r = v(!1), o = v(!1), u = P(() => [
281
- Math.floor(s.value % 36e5 / 6e4),
286
+ const q = ({ containerRef: n, options: e }) => {
287
+ const { waveSurfer: i } = P({ containerRef: n, options: e }), t = m(null), o = m(0), r = m(!1), s = m(!1), a = y(() => [
288
+ Math.floor(o.value % 36e5 / 6e4),
282
289
  // minutes
283
- Math.floor(s.value % 6e4 / 1e3)
290
+ Math.floor(o.value % 6e4 / 1e3)
284
291
  // seconds
285
- ].map((a) => a < 10 ? "0" + a : a).join(":")), d = P(() => r.value || !o.value), l = () => {
286
- var a;
287
- i.value && ((a = i.value) == null || a.on("record-progress", (h) => {
288
- s.value = h;
289
- }));
290
- }, c = () => {
291
- var a, h, m, f;
292
- if ((a = i.value) != null && a.isRecording() || (h = i.value) != null && h.isPaused()) {
293
- (m = i.value) == null || m.stopRecording(), r.value = !1, o.value = !0;
292
+ ].map((c) => c < 10 ? "0" + c : c).join(":")), u = y(() => r.value || !s.value), l = () => {
293
+ t.value && t.value?.on("record-progress", (c) => {
294
+ o.value = c;
295
+ });
296
+ }, d = () => {
297
+ if (t.value?.isRecording() || t.value?.isPaused()) {
298
+ t.value?.stopRecording(), r.value = !1, s.value = !0;
294
299
  return;
295
300
  }
296
- (f = i.value) == null || f.startRecording(), r.value = !0, o.value = !1, l();
297
- }, p = () => new Promise((a) => {
298
- var m, f, W, S;
299
- let h;
300
- ((m = i.value) != null && m.isRecording() || (f = i.value) != null && f.isPaused()) && ((W = i.value) == null || W.stopRecording(), r.value = !1, o.value = !1), (S = i.value) == null || S.on("record-end", (M) => {
301
- h = M, a(h);
301
+ t.value?.startRecording(), r.value = !0, s.value = !1, l();
302
+ }, f = () => new Promise((c) => {
303
+ let v;
304
+ (t.value?.isRecording() || t.value?.isPaused()) && (t.value?.stopRecording(), r.value = !1, s.value = !1), t.value?.on("record-end", (h) => {
305
+ v = h, c(v);
302
306
  });
303
- }), R = () => {
304
- var a, h, m;
305
- if ((a = i.value) != null && a.isPaused()) {
306
- (h = i.value) == null || h.resumeRecording(), r.value = !0, o.value = !1;
307
+ }), g = () => {
308
+ if (t.value?.isPaused()) {
309
+ t.value?.resumeRecording(), r.value = !0, s.value = !1;
307
310
  return;
308
311
  }
309
- r.value = !1, o.value = !0, (m = i.value) == null || m.pauseRecording();
312
+ r.value = !1, s.value = !0, t.value?.pauseRecording();
310
313
  };
311
- return g(() => {
312
- var h;
313
- const a = (h = t.value) == null ? void 0 : h.registerPlugin(y.create({ renderRecordedAudio: !1 }));
314
- a && (i.value = a);
314
+ return w(() => {
315
+ const c = i.value?.registerPlugin(R.create({ renderRecordedAudio: !1 }));
316
+ c && (t.value = c);
315
317
  }), {
316
- waveSurfer: t,
317
- waveSurferRecorder: i,
318
- currentTime: u,
319
- startRecording: c,
320
- stopRecording: p,
321
- pauseRecording: R,
318
+ waveSurfer: i,
319
+ waveSurferRecorder: t,
320
+ currentTime: a,
321
+ startRecording: d,
322
+ stopRecording: f,
323
+ pauseRecording: g,
322
324
  isRecording: r,
323
- isPaused: o,
324
- isPauseResume: d
325
+ isPaused: s,
326
+ isPauseResume: u
325
327
  };
326
328
  };
327
329
  export {
328
- L as WaveSurferPlayer,
329
- B as useWaveSurfer,
330
- z as useWaveSurferRecorder
330
+ U as WaveSurferPlayer,
331
+ k as useWaveSurfer,
332
+ q as useWaveSurferRecorder
331
333
  };
@@ -0,0 +1,7 @@
1
+ import { PartialWaveSurferOptions } from '../types';
2
+ declare const _default: import('vue').DefineComponent<{
3
+ options: PartialWaveSurferOptions;
4
+ }, {}, {}, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').PublicProps, Readonly<{
5
+ options: PartialWaveSurferOptions;
6
+ }> & Readonly<{}>, {}, {}, {}, {}, string, import('vue').ComponentProvideOptions, false, {}, any>;
7
+ export default _default;
@@ -1 +1,2 @@
1
+ import { UseWaveSurfer, WaveSurferIns } from '../types';
1
2
  export declare const useWaveSurfer: ({ containerRef, options }: WaveSurferIns) => UseWaveSurfer;