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