@meersagor/wavesurfer-vue 0.1.0 → 0.1.2
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/README.md +5 -2
- package/dist/@meersagor-wavesurfer-vue.cjs +1 -0
- package/dist/@meersagor-wavesurfer-vue.js +154 -143
- package/dist/src/components/WaveSurferPlayer.vue.d.ts +7 -0
- package/dist/{types → src}/composables/useWaveSurfer.d.ts +1 -1
- package/dist/src/composables/useWaveSurferInstance.d.ts +999 -0
- package/dist/src/composables/useWaveSurferRecorder.d.ts +1053 -0
- package/dist/src/composables/useWaveSurferState.d.ts +8 -0
- package/dist/{types → src}/eventsEmitter/index.d.ts +1 -1
- package/dist/{types → src}/index.d.ts +1 -1
- package/dist/src/types/index.d.ts +29 -0
- package/dist/vite.config.d.ts +1 -1
- package/package.json +25 -23
- package/dist/@meersagor-wavesurfer-vue.umd.cjs +0 -14
- package/dist/types/components/WaveSurferPlayer.vue.d.ts +0 -16
- package/dist/types/composables/useWaveSurferInstance.d.ts +0 -495
- package/dist/types/composables/useWaveSurferRecorder.d.ts +0 -525
- package/dist/types/composables/useWaveSurferState.d.ts +0 -8
- package/dist/types/types/index.d.ts +0 -10
package/README.md
CHANGED
|
@@ -122,7 +122,7 @@ const formatTime = (seconds: number):string => [seconds / 60, seconds % 60].map(
|
|
|
122
122
|
|
|
123
123
|
|
|
124
124
|
|
|
125
|
-
## useWaveSurferRecorder
|
|
125
|
+
## use useWaveSurferRecorder composable method
|
|
126
126
|
|
|
127
127
|
```vue
|
|
128
128
|
<script lang="ts" setup>
|
|
@@ -145,6 +145,9 @@ const options = computed(() => ({
|
|
|
145
145
|
const { pauseRecording, startRecording, stopRecording, currentTime, isPauseResume } = useWaveSurferRecorder({
|
|
146
146
|
containerRef,
|
|
147
147
|
options: options.value,
|
|
148
|
+
recordPluginOptions:{
|
|
149
|
+
continuousWaveform: true
|
|
150
|
+
}
|
|
148
151
|
})
|
|
149
152
|
|
|
150
153
|
const startAudioRecordHandler = () => {
|
|
@@ -173,7 +176,7 @@ const stopHandler = async () => {
|
|
|
173
176
|
</div>
|
|
174
177
|
</template>
|
|
175
178
|
```
|
|
176
|
-
|
|
179
|
+
## useWaveSurferRecorder: method Return Types
|
|
177
180
|
### `waveSurfer`
|
|
178
181
|
|
|
179
182
|
- Type: `Ref<WaveSurfer | null>`
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("vue"),b=require("wavesurfer.js"),R=({containerRef:n,options:e})=>{const t=u.ref(null),i=()=>{n.value&&(t.value=b.create({container:n.value,...e}))},s=()=>{t.value&&(t.value.destroy(),t.value=null)};return u.onMounted(()=>{i()}),u.onUnmounted(()=>{s()}),{waveSurfer:t}},M=n=>{const e=u.ref(!1),t=u.ref(!1),i=u.ref(0),s=u.ref(0),o=()=>{e.value=!1,t.value=!1,i.value=0},r=f=>{e.value=!0,t.value=!1,i.value=0,s.value=f},a=()=>{t.value=!0},l=()=>{t.value=!1},v=()=>{n.value&&(i.value=n.value.getCurrentTime())},c=()=>{e.value=!1,t.value=!1};return u.onMounted(()=>{n.value&&(n.value.on("load",o),n.value.on("ready",r),n.value.on("play",a),n.value.on("pause",l),n.value.on("timeupdate",v),n.value.on("destroy",c))}),u.onUnmounted(()=>{n.value&&n.value.unAll()}),{isReady:e,isPlaying:t,currentTime:i,totalDuration:s}},y=({containerRef:n,options:e})=>{const{waveSurfer:t}=R({containerRef:n,options:e}),{isReady:i,totalDuration:s,isPlaying:o,currentTime:r}=M(t);return{waveSurfer:t,isReady:i,totalDuration:s,isPlaying:o,currentTime:r}},D=["audioprocess","click","dblclick","decode","drag","finish","init","interaction","load","loading","pause","play","ready","redraw","redrawcomplete","scroll","seeking","timeupdate","zoom"],T=u.defineComponent({__name:"WaveSurferPlayer",props:{options:{}},setup(n){const e=n,t=u.ref(null),{waveSurfer:i}=y({containerRef:t,options:e.options}),s=u.getCurrentInstance();return u.onMounted(()=>{D.forEach(o=>{i.value?.on(o,(...r)=>{s?.emit(o,...r)})}),s?.emit("waveSurfer",i.value)}),(o,r)=>(u.openBlock(),u.createElementBlock("div",{ref_key:"containerRef",ref:t},null,512))}});function w(n,e,t,i){return new(t||(t=Promise))(function(s,o){function r(v){try{l(i.next(v))}catch(c){o(c)}}function a(v){try{l(i.throw(v))}catch(c){o(c)}}function l(v){var c;v.done?s(v.value):(c=v.value,c instanceof t?c:new t(function(f){f(c)})).then(r,a)}l((i=i.apply(n,[])).next())})}class S{constructor(){this.listeners={}}on(e,t,i){if(this.listeners[e]||(this.listeners[e]=new Set),this.listeners[e].add(t),i?.once){const s=()=>{this.un(e,s),this.un(e,t)};return this.on(e,s),s}return()=>this.un(e,t)}un(e,t){var i;(i=this.listeners[e])===null||i===void 0||i.delete(t)}once(e,t){return this.on(e,t,{once:!0})}unAll(){this.listeners={}}emit(e,...t){this.listeners[e]&&this.listeners[e].forEach(i=>i(...t))}}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 t,i,s,o,r,a;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,continuousWaveform:(o=e.continuousWaveform)!==null&&o!==void 0&&o,renderRecordedAudio:(r=e.renderRecordedAudio)===null||r===void 0||r,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 A,this.subscriptions.push(this.timer.on("tick",()=>{const l=performance.now()-this.lastStartTime;this.duration=this.isPaused()?this.duration:this.lastDuration+l,this.emit("record-progress",this.duration)}))}static create(e){return new W(e||{})}renderMicStream(e){var t;const i=new AudioContext,s=i.createMediaStreamSource(e),o=i.createAnalyser();s.connect(o),this.options.continuousWaveform&&(o.fftSize=32);const r=o.frequencyBinCount,a=new Float32Array(r);let l=0;this.wavesurfer&&((t=this.originalOptions)!==null&&t!==void 0||(this.originalOptions=Object.assign({},this.wavesurfer.options)),this.wavesurfer.options.interact=!1,this.options.scrollingWaveform&&(this.wavesurfer.options.cursorWidth=0));const v=setInterval(()=>{var c,f,p,g;if(!this.isWaveformPaused){if(o.getFloatTimeDomainData(a),this.options.scrollingWaveform){const d=Math.floor((this.options.scrollingWaveformWindow||0)*i.sampleRate),h=Math.min(d,this.dataWindow?this.dataWindow.length+r:r),m=new Float32Array(d);if(this.dataWindow){const P=Math.max(0,d-this.dataWindow.length);m.set(this.dataWindow.slice(-h+r),P)}m.set(a,d-r),this.dataWindow=m}else if(this.options.continuousWaveform){if(!this.dataWindow){const h=this.options.continuousWaveformDuration?Math.round(100*this.options.continuousWaveformDuration):((f=(c=this.wavesurfer)===null||c===void 0?void 0:c.getWidth())!==null&&f!==void 0?f:0)*window.devicePixelRatio;this.dataWindow=new Float32Array(h)}let d=0;for(let h=0;h<r;h++){const m=Math.abs(a[h]);m>d&&(d=m)}if(l+1>this.dataWindow.length){const h=new Float32Array(2*this.dataWindow.length);h.set(this.dataWindow,0),this.dataWindow=h}this.dataWindow[l]=d,l++}else this.dataWindow=a;if(this.wavesurfer){const d=((g=(p=this.dataWindow)===null||p===void 0?void 0:p.length)!==null&&g!==void 0?g:0)/100;this.wavesurfer.load("",[this.dataWindow],this.options.scrollingWaveform?this.options.scrollingWaveformWindow:d).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(h=>{console.error("Error rendering real-time recording data:",h)})}}},10);return{onDestroy:()=>{clearInterval(v),s?.disconnect(),i?.close()},onEnd:()=>{this.isWaveformPaused=!0,clearInterval(v),this.stopMic()}}}startMic(e){return w(this,void 0,void 0,function*(){let t;try{t=yield navigator.mediaDevices.getUserMedia({audio:!e?.deviceId||{deviceId:e.deviceId}})}catch(o){throw new Error("Error accessing the microphone: "+o.message)}const{onDestroy:i,onEnd:s}=this.renderMicStream(t);return this.subscriptions.push(this.once("destroy",i)),this.subscriptions.push(this.once("record-end",s)),this.stream=t,t})}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 t=this.stream||(yield this.startMic(e));this.dataWindow=null;const i=this.mediaRecorder||new MediaRecorder(t,{mimeType:this.options.mimeType||E.find(r=>MediaRecorder.isTypeSupported(r)),audioBitsPerSecond:this.options.audioBitsPerSecond});this.mediaRecorder=i,this.stopRecording();const s=[];i.ondataavailable=r=>{r.data.size>0&&s.push(r.data),this.emit("record-data-available",r.data)};const o=r=>{var a;const l=new Blob(s,{type:i.mimeType});this.emit(r,l),this.options.renderRecordedAudio&&(this.applyOriginalOptionsIfNeeded(),(a=this.wavesurfer)===null||a===void 0||a.load(URL.createObjectURL(l)))};i.onpause=()=>o("record-pause"),i.onstop=()=>o("record-end"),i.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,t;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)}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(t=>t.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,recordPluginOptions:t})=>{const{waveSurfer:i}=R({containerRef:n,options:e}),s=u.ref(null),o=u.ref(0),r=u.ref(!1),a=u.ref(!1),l=u.computed(()=>[Math.floor(o.value%36e5/6e4),Math.floor(o.value%6e4/1e3)].map(d=>d<10?"0"+d:d).join(":")),v=u.computed(()=>r.value||!a.value),c=()=>{s.value&&s.value?.on("record-progress",d=>{o.value=d})},f=()=>{if(s.value?.isRecording()||s.value?.isPaused()){s.value?.stopRecording(),r.value=!1,a.value=!0;return}s.value?.startRecording(),r.value=!0,a.value=!1,c()},p=()=>new Promise(d=>{let h;(s.value?.isRecording()||s.value?.isPaused())&&(s.value?.stopRecording(),r.value=!1,a.value=!1),s.value?.on("record-end",m=>{h=m,d(h)})}),g=()=>{if(s.value?.isPaused()){s.value?.resumeRecording(),r.value=!0,a.value=!1;return}r.value=!1,a.value=!0,s.value?.pauseRecording()};return u.onMounted(()=>{const d=i.value?.registerPlugin(W.create({renderRecordedAudio:!1,...t}));d&&(s.value=d)}),{waveSurfer:i,waveSurferRecorder:s,currentTime:l,startRecording:f,stopRecording:p,pauseRecording:g,isRecording:r,isPaused:a,isPauseResume:v}};exports.WaveSurferPlayer=T;exports.useWaveSurfer=y;exports.useWaveSurferRecorder=I;
|
|
@@ -1,120 +1,106 @@
|
|
|
1
|
-
import { ref as
|
|
2
|
-
import
|
|
3
|
-
const
|
|
4
|
-
const t =
|
|
5
|
-
n.value && (t.value =
|
|
1
|
+
import { ref as v, 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 t = v(null), i = () => {
|
|
5
|
+
n.value && (t.value = E.create({
|
|
6
6
|
container: n.value,
|
|
7
7
|
...e
|
|
8
8
|
}));
|
|
9
|
-
},
|
|
9
|
+
}, s = () => {
|
|
10
10
|
t.value && (t.value.destroy(), t.value = null);
|
|
11
11
|
};
|
|
12
|
-
return
|
|
12
|
+
return w(() => {
|
|
13
13
|
i();
|
|
14
|
-
}),
|
|
15
|
-
|
|
14
|
+
}), S(() => {
|
|
15
|
+
s();
|
|
16
16
|
}), { waveSurfer: t };
|
|
17
|
-
},
|
|
18
|
-
const e =
|
|
17
|
+
}, I = (n) => {
|
|
18
|
+
const e = v(!1), t = v(!1), i = v(0), s = v(0), r = () => {
|
|
19
19
|
e.value = !1, t.value = !1, i.value = 0;
|
|
20
|
-
}, o = (
|
|
21
|
-
e.value = !0, t.value = !1, i.value = 0,
|
|
22
|
-
}, l = () => {
|
|
23
|
-
t.value = !0;
|
|
20
|
+
}, o = (m) => {
|
|
21
|
+
e.value = !0, t.value = !1, i.value = 0, s.value = m;
|
|
24
22
|
}, a = () => {
|
|
23
|
+
t.value = !0;
|
|
24
|
+
}, d = () => {
|
|
25
25
|
t.value = !1;
|
|
26
|
-
},
|
|
26
|
+
}, h = () => {
|
|
27
27
|
n.value && (i.value = n.value.getCurrentTime());
|
|
28
|
-
},
|
|
28
|
+
}, l = () => {
|
|
29
29
|
e.value = !1, t.value = !1;
|
|
30
30
|
};
|
|
31
|
-
return
|
|
32
|
-
n.value && (n.value.on("load",
|
|
33
|
-
}),
|
|
31
|
+
return w(() => {
|
|
32
|
+
n.value && (n.value.on("load", r), n.value.on("ready", o), n.value.on("play", a), n.value.on("pause", d), n.value.on("timeupdate", h), n.value.on("destroy", l));
|
|
33
|
+
}), S(() => {
|
|
34
34
|
n.value && n.value.unAll();
|
|
35
35
|
}), {
|
|
36
36
|
isReady: e,
|
|
37
37
|
isPlaying: t,
|
|
38
38
|
currentTime: i,
|
|
39
|
-
totalDuration:
|
|
39
|
+
totalDuration: s
|
|
40
40
|
};
|
|
41
41
|
}, k = ({ containerRef: n, options: e }) => {
|
|
42
|
-
const { waveSurfer: t } =
|
|
42
|
+
const { waveSurfer: t } = P({ containerRef: n, options: e }), { isReady: i, totalDuration: s, isPlaying: r, currentTime: o } = I(t);
|
|
43
43
|
return {
|
|
44
44
|
waveSurfer: t,
|
|
45
45
|
isReady: i,
|
|
46
|
-
totalDuration:
|
|
47
|
-
isPlaying:
|
|
46
|
+
totalDuration: s,
|
|
47
|
+
isPlaying: r,
|
|
48
48
|
currentTime: o
|
|
49
49
|
};
|
|
50
|
-
},
|
|
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 =
|
|
57
|
-
return
|
|
58
|
-
|
|
59
|
-
i.value?.on(
|
|
60
|
-
|
|
56
|
+
const e = n, t = v(null), { waveSurfer: i } = k({ containerRef: t, options: e.options }), s = M();
|
|
57
|
+
return w(() => {
|
|
58
|
+
x.forEach((r) => {
|
|
59
|
+
i.value?.on(r, (...o) => {
|
|
60
|
+
s?.emit(r, ...o);
|
|
61
61
|
});
|
|
62
|
-
}),
|
|
63
|
-
}), (
|
|
62
|
+
}), s?.emit("waveSurfer", i.value);
|
|
63
|
+
}), (r, o) => (A(), O("div", {
|
|
64
64
|
ref_key: "containerRef",
|
|
65
65
|
ref: t
|
|
66
66
|
}, null, 512));
|
|
67
67
|
}
|
|
68
68
|
});
|
|
69
|
-
|
|
70
|
-
|
|
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, t, i) {
|
|
70
|
+
return new (t || (t = Promise))(function(s, r) {
|
|
71
|
+
function o(h) {
|
|
86
72
|
try {
|
|
87
|
-
|
|
88
|
-
} catch (
|
|
89
|
-
|
|
73
|
+
d(i.next(h));
|
|
74
|
+
} catch (l) {
|
|
75
|
+
r(l);
|
|
90
76
|
}
|
|
91
77
|
}
|
|
92
|
-
function
|
|
78
|
+
function a(h) {
|
|
93
79
|
try {
|
|
94
|
-
|
|
95
|
-
} catch (
|
|
96
|
-
|
|
80
|
+
d(i.throw(h));
|
|
81
|
+
} catch (l) {
|
|
82
|
+
r(l);
|
|
97
83
|
}
|
|
98
84
|
}
|
|
99
|
-
function
|
|
100
|
-
var
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
})).then(o,
|
|
85
|
+
function d(h) {
|
|
86
|
+
var l;
|
|
87
|
+
h.done ? s(h.value) : (l = h.value, l instanceof t ? l : new t(function(m) {
|
|
88
|
+
m(l);
|
|
89
|
+
})).then(o, a);
|
|
104
90
|
}
|
|
105
|
-
|
|
91
|
+
d((i = i.apply(n, [])).next());
|
|
106
92
|
});
|
|
107
93
|
}
|
|
108
|
-
class
|
|
94
|
+
class b {
|
|
109
95
|
constructor() {
|
|
110
96
|
this.listeners = {};
|
|
111
97
|
}
|
|
112
98
|
on(e, t, i) {
|
|
113
99
|
if (this.listeners[e] || (this.listeners[e] = /* @__PURE__ */ new Set()), this.listeners[e].add(t), i?.once) {
|
|
114
|
-
const
|
|
115
|
-
this.un(e,
|
|
100
|
+
const s = () => {
|
|
101
|
+
this.un(e, s), this.un(e, t);
|
|
116
102
|
};
|
|
117
|
-
return this.on(e,
|
|
103
|
+
return this.on(e, s), s;
|
|
118
104
|
}
|
|
119
105
|
return () => this.un(e, t);
|
|
120
106
|
}
|
|
@@ -132,7 +118,7 @@ class S {
|
|
|
132
118
|
this.listeners[e] && this.listeners[e].forEach((i) => i(...t));
|
|
133
119
|
}
|
|
134
120
|
}
|
|
135
|
-
class
|
|
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
|
|
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
|
|
168
|
-
class R extends
|
|
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,
|
|
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: (
|
|
172
|
-
const
|
|
173
|
-
this.duration = this.isPaused() ? this.duration : this.lastDuration +
|
|
156
|
+
var t, i, s, r, o, a;
|
|
157
|
+
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, continuousWaveform: (r = e.continuousWaveform) !== null && r !== void 0 && r, renderRecordedAudio: (o = e.renderRecordedAudio) === null || o === void 0 || o, 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 d = performance.now() - this.lastStartTime;
|
|
159
|
+
this.duration = this.isPaused() ? this.duration : this.lastDuration + d, 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
|
-
|
|
181
|
-
i.
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
166
|
+
var t;
|
|
167
|
+
const i = new AudioContext(), s = i.createMediaStreamSource(e), r = i.createAnalyser();
|
|
168
|
+
s.connect(r), this.options.continuousWaveform && (r.fftSize = 32);
|
|
169
|
+
const o = r.frequencyBinCount, a = new Float32Array(o);
|
|
170
|
+
let d = 0;
|
|
171
|
+
this.wavesurfer && ((t = this.originalOptions) !== null && t !== void 0 || (this.originalOptions = Object.assign({}, this.wavesurfer.options)), this.wavesurfer.options.interact = !1, this.options.scrollingWaveform && (this.wavesurfer.options.cursorWidth = 0));
|
|
172
|
+
const h = setInterval(() => {
|
|
173
|
+
var l, m, p, g;
|
|
174
|
+
if (!this.isWaveformPaused) {
|
|
175
|
+
if (r.getFloatTimeDomainData(a), this.options.scrollingWaveform) {
|
|
176
|
+
const u = Math.floor((this.options.scrollingWaveformWindow || 0) * i.sampleRate), c = Math.min(u, this.dataWindow ? this.dataWindow.length + o : o), f = new Float32Array(u);
|
|
177
|
+
if (this.dataWindow) {
|
|
178
|
+
const D = Math.max(0, u - this.dataWindow.length);
|
|
179
|
+
f.set(this.dataWindow.slice(-c + o), D);
|
|
180
|
+
}
|
|
181
|
+
f.set(a, u - o), this.dataWindow = f;
|
|
182
|
+
} else if (this.options.continuousWaveform) {
|
|
183
|
+
if (!this.dataWindow) {
|
|
184
|
+
const c = 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;
|
|
185
|
+
this.dataWindow = new Float32Array(c);
|
|
186
|
+
}
|
|
187
|
+
let u = 0;
|
|
188
|
+
for (let c = 0; c < o; c++) {
|
|
189
|
+
const f = Math.abs(a[c]);
|
|
190
|
+
f > u && (u = f);
|
|
191
|
+
}
|
|
192
|
+
if (d + 1 > this.dataWindow.length) {
|
|
193
|
+
const c = new Float32Array(2 * this.dataWindow.length);
|
|
194
|
+
c.set(this.dataWindow, 0), this.dataWindow = c;
|
|
195
|
+
}
|
|
196
|
+
this.dataWindow[d] = u, d++;
|
|
197
|
+
} else this.dataWindow = a;
|
|
198
|
+
if (this.wavesurfer) {
|
|
199
|
+
const u = ((g = (p = this.dataWindow) === null || p === void 0 ? void 0 : p.length) !== null && g !== void 0 ? g : 0) / 100;
|
|
200
|
+
this.wavesurfer.load("", [this.dataWindow], this.options.scrollingWaveform ? this.options.scrollingWaveformWindow : u).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((c) => {
|
|
203
|
+
console.error("Error rendering real-time recording data:", c);
|
|
204
|
+
});
|
|
193
205
|
}
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
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(h), s?.disconnect(), i?.close();
|
|
202
210
|
}, onEnd: () => {
|
|
203
|
-
this.isWaveformPaused = !0,
|
|
211
|
+
this.isWaveformPaused = !0, clearInterval(h), this.stopMic();
|
|
204
212
|
} };
|
|
205
213
|
}
|
|
206
214
|
startMic(e) {
|
|
207
|
-
return
|
|
215
|
+
return W(this, void 0, void 0, function* () {
|
|
208
216
|
let t;
|
|
209
217
|
try {
|
|
210
218
|
t = yield navigator.mediaDevices.getUserMedia({ audio: !e?.deviceId || { deviceId: e.deviceId } });
|
|
211
|
-
} catch (
|
|
212
|
-
throw new Error("Error accessing the microphone: " +
|
|
219
|
+
} catch (r) {
|
|
220
|
+
throw new Error("Error accessing the microphone: " + r.message);
|
|
213
221
|
}
|
|
214
|
-
const { onDestroy: i, onEnd:
|
|
215
|
-
return this.subscriptions.push(this.once("destroy", i)), this.subscriptions.push(this.once("record-end",
|
|
222
|
+
const { onDestroy: i, onEnd: s } = this.renderMicStream(t);
|
|
223
|
+
return this.subscriptions.push(this.once("destroy", i)), this.subscriptions.push(this.once("record-end", s)), this.stream = t, t;
|
|
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
|
|
230
|
+
return W(this, void 0, void 0, function* () {
|
|
223
231
|
const t = this.stream || (yield this.startMic(e));
|
|
224
232
|
this.dataWindow = null;
|
|
225
|
-
const i = this.mediaRecorder || new MediaRecorder(t, { mimeType: this.options.mimeType ||
|
|
233
|
+
const i = this.mediaRecorder || new MediaRecorder(t, { mimeType: this.options.mimeType || F.find((o) => MediaRecorder.isTypeSupported(o)), audioBitsPerSecond: this.options.audioBitsPerSecond });
|
|
226
234
|
this.mediaRecorder = i, this.stopRecording();
|
|
227
|
-
const
|
|
235
|
+
const s = [];
|
|
228
236
|
i.ondataavailable = (o) => {
|
|
229
|
-
o.data.size > 0 &&
|
|
237
|
+
o.data.size > 0 && s.push(o.data), this.emit("record-data-available", o.data);
|
|
230
238
|
};
|
|
231
|
-
const
|
|
232
|
-
var
|
|
233
|
-
const
|
|
234
|
-
this.emit(o,
|
|
239
|
+
const r = (o) => {
|
|
240
|
+
var a;
|
|
241
|
+
const d = new Blob(s, { type: i.mimeType });
|
|
242
|
+
this.emit(o, d), this.options.renderRecordedAudio && (this.applyOriginalOptionsIfNeeded(), (a = this.wavesurfer) === null || a === void 0 || a.load(URL.createObjectURL(d)));
|
|
235
243
|
};
|
|
236
|
-
i.onpause = () =>
|
|
244
|
+
i.onpause = () => r("record-pause"), i.onstop = () => r("record-end"), i.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() {
|
|
@@ -264,7 +272,7 @@ class R extends I {
|
|
|
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
|
|
275
|
+
return W(this, void 0, void 0, function* () {
|
|
268
276
|
return navigator.mediaDevices.enumerateDevices().then((e) => e.filter((t) => t.kind === "audioinput"));
|
|
269
277
|
});
|
|
270
278
|
}
|
|
@@ -272,54 +280,57 @@ class R extends I {
|
|
|
272
280
|
this.applyOriginalOptionsIfNeeded(), super.destroy(), this.stopRecording(), this.stopMic();
|
|
273
281
|
}
|
|
274
282
|
applyOriginalOptionsIfNeeded() {
|
|
275
|
-
this.wavesurfer && this.originalOptions && (this.wavesurfer.
|
|
283
|
+
this.wavesurfer && this.originalOptions && (this.wavesurfer.setOptions(this.originalOptions), delete this.originalOptions);
|
|
276
284
|
}
|
|
277
285
|
}
|
|
278
|
-
const
|
|
279
|
-
const { waveSurfer:
|
|
286
|
+
const q = ({ containerRef: n, options: e, recordPluginOptions: t }) => {
|
|
287
|
+
const { waveSurfer: i } = P({ containerRef: n, options: e }), s = v(null), r = v(0), o = v(!1), a = v(!1), d = y(() => [
|
|
280
288
|
Math.floor(r.value % 36e5 / 6e4),
|
|
281
289
|
// minutes
|
|
282
290
|
Math.floor(r.value % 6e4 / 1e3)
|
|
283
291
|
// seconds
|
|
284
|
-
].map((
|
|
285
|
-
|
|
286
|
-
r.value =
|
|
292
|
+
].map((u) => u < 10 ? "0" + u : u).join(":")), h = y(() => o.value || !a.value), l = () => {
|
|
293
|
+
s.value && s.value?.on("record-progress", (u) => {
|
|
294
|
+
r.value = u;
|
|
287
295
|
});
|
|
288
|
-
},
|
|
289
|
-
if (
|
|
290
|
-
|
|
296
|
+
}, m = () => {
|
|
297
|
+
if (s.value?.isRecording() || s.value?.isPaused()) {
|
|
298
|
+
s.value?.stopRecording(), o.value = !1, a.value = !0;
|
|
291
299
|
return;
|
|
292
300
|
}
|
|
293
|
-
|
|
294
|
-
},
|
|
295
|
-
let
|
|
296
|
-
(
|
|
297
|
-
|
|
301
|
+
s.value?.startRecording(), o.value = !0, a.value = !1, l();
|
|
302
|
+
}, p = () => new Promise((u) => {
|
|
303
|
+
let c;
|
|
304
|
+
(s.value?.isRecording() || s.value?.isPaused()) && (s.value?.stopRecording(), o.value = !1, a.value = !1), s.value?.on("record-end", (f) => {
|
|
305
|
+
c = f, u(c);
|
|
298
306
|
});
|
|
299
|
-
}),
|
|
300
|
-
if (
|
|
301
|
-
|
|
307
|
+
}), g = () => {
|
|
308
|
+
if (s.value?.isPaused()) {
|
|
309
|
+
s.value?.resumeRecording(), o.value = !0, a.value = !1;
|
|
302
310
|
return;
|
|
303
311
|
}
|
|
304
|
-
|
|
312
|
+
o.value = !1, a.value = !0, s.value?.pauseRecording();
|
|
305
313
|
};
|
|
306
|
-
return
|
|
307
|
-
const
|
|
308
|
-
|
|
314
|
+
return w(() => {
|
|
315
|
+
const u = i.value?.registerPlugin(R.create({
|
|
316
|
+
renderRecordedAudio: !1,
|
|
317
|
+
...t
|
|
318
|
+
}));
|
|
319
|
+
u && (s.value = u);
|
|
309
320
|
}), {
|
|
310
|
-
waveSurfer:
|
|
311
|
-
waveSurferRecorder:
|
|
312
|
-
currentTime:
|
|
313
|
-
startRecording:
|
|
314
|
-
stopRecording:
|
|
315
|
-
pauseRecording:
|
|
316
|
-
isRecording:
|
|
317
|
-
isPaused:
|
|
318
|
-
isPauseResume:
|
|
321
|
+
waveSurfer: i,
|
|
322
|
+
waveSurferRecorder: s,
|
|
323
|
+
currentTime: d,
|
|
324
|
+
startRecording: m,
|
|
325
|
+
stopRecording: p,
|
|
326
|
+
pauseRecording: g,
|
|
327
|
+
isRecording: o,
|
|
328
|
+
isPaused: a,
|
|
329
|
+
isPauseResume: h
|
|
319
330
|
};
|
|
320
331
|
};
|
|
321
332
|
export {
|
|
322
|
-
|
|
333
|
+
U as WaveSurferPlayer,
|
|
323
334
|
k as useWaveSurfer,
|
|
324
|
-
|
|
335
|
+
q as useWaveSurferRecorder
|
|
325
336
|
};
|
|
@@ -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
|
|
1
|
+
import { UseWaveSurfer, WaveSurferIns } from '../types';
|
|
2
2
|
export declare const useWaveSurfer: ({ containerRef, options }: WaveSurferIns) => UseWaveSurfer;
|