UNPKG

@stlite/browser

Version:
238 lines (237 loc) 9.12 kB
//#region ../react/build/record-CvRL89xC.js function e(e, t, n, r) { return new (n ||= Promise)(function(t, i) { function a(e) { try { s(r.next(e)); } catch (e) { i(e); } } function o(e) { try { s(r.throw(e)); } catch (e) { i(e); } } function s(e) { var r; e.done ? t(e.value) : (r = e.value, r instanceof n ? r : new n(function(e) { e(r); })).then(a, o); } s((r = r.apply(e, [])).next()); }); } var t = class { constructor() { this.listeners = {}; } on(e, t, n) { if (this.listeners[e] || (this.listeners[e] = /* @__PURE__ */ new Set()), n?.once) { let n = (...r) => { this.un(e, n), t(...r); }; return this.listeners[e].add(n), () => this.un(e, n); } return this.listeners[e].add(t), () => this.un(e, t); } un(e, t) { var n; (n = this.listeners[e]) == null || n.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((e) => e(...t)); } }, n = class extends t { constructor(e) { super(), this.subscriptions = [], this.isDestroyed = !1, this.options = e; } onInit() {} _init(e) { this.isDestroyed &&= (this.subscriptions = [], !1), this.wavesurfer = e, this.onInit(); } destroy() { this.emit("destroy"), this.subscriptions.forEach((e) => e()), this.subscriptions = [], this.isDestroyed = !0, this.wavesurfer = void 0; } }, r = class extends t { constructor() { super(...arguments), this.animationFrameId = null, this.isRunning = !1; } start() { if (this.isRunning) return; this.isRunning = !0; let e = () => { this.isRunning && (this.emit("tick"), this.animationFrameId = requestAnimationFrame(e)); }; e(); } stop() { this.isRunning = !1, this.animationFrameId !== null && (cancelAnimationFrame(this.animationFrameId), this.animationFrameId = null); } destroy() { this.stop(); } }, i = 100, a = [ "audio/webm", "audio/wav", "audio/mpeg", "audio/mp4", "audio/mp3" ], o = class t extends n { constructor(e) { var t, n, i; super(Object.assign(Object.assign({}, e), { audioBitsPerSecond: e.audioBitsPerSecond ?? 128e3, scrollingWaveform: (t = e.scrollingWaveform) != null && t, scrollingWaveformWindow: e.scrollingWaveformWindow ?? 5, continuousWaveform: (n = e.continuousWaveform) != null && n, renderRecordedAudio: (i = e.renderRecordedAudio) == null || i, mediaRecorderTimeslice: e.mediaRecorderTimeslice ?? void 0 })), this.stream = null, this.mediaRecorder = null, this.dataWindow = null, this.isWaveformPaused = !1, this.lastStartTime = 0, this.lastDuration = 0, this.duration = 0, this.micStream = null, this.recordedBlobUrl = null, this.timer = new r(), this.subscriptions.push(this.timer.on("tick", () => { let e = performance.now() - this.lastStartTime; this.duration = this.isPaused() ? this.duration : this.lastDuration + e, this.emit("record-progress", this.duration); })); } static create(e) { return new t(e || {}); } renderMicStream(e) { let t = new AudioContext(), n = t.createMediaStreamSource(e), r = t.createAnalyser(); n.connect(r), (this.options.continuousWaveform || this.options.scrollingWaveform) && (r.fftSize = 32); let a = r.frequencyBinCount, o = new Float32Array(a), s = 0; this.wavesurfer && (this.originalOptions ??= Object.assign({}, this.wavesurfer.options), this.wavesurfer.options.interact = !1, this.options.scrollingWaveform && (this.wavesurfer.options.cursorWidth = 0, this.wavesurfer.options.normalize = !0, this.wavesurfer.options.maxPeak = 1)); let c = setInterval(() => { if (!this.isWaveformPaused) { if (r.getFloatTimeDomainData(o), this.options.scrollingWaveform) { let e = Math.floor((this.options.scrollingWaveformWindow || 0) * i), t = 0; for (let e = 0; e < a; e++) { let n = Math.abs(o[e]); n > t && (t = n); } this.dataWindow ||= new Float32Array(e); let n = new Float32Array(e); if (this.dataWindow && this.dataWindow.length > 0) { let t = e - 1, r = this.dataWindow.slice(-t); n.set(r, 0); } n[e - 1] = t, this.dataWindow = n; } else if (this.options.continuousWaveform) { if (!this.dataWindow) { let e = this.options.continuousWaveformDuration ? Math.round(this.options.continuousWaveformDuration * i) : (this.wavesurfer?.getWidth() ?? 0) * window.devicePixelRatio; this.dataWindow = new Float32Array(e); } let e = 0; for (let t = 0; t < a; t++) { let n = Math.abs(o[t]); n > e && (e = n); } if (s + 1 > this.dataWindow.length) { let e = new Float32Array(2 * this.dataWindow.length); e.set(this.dataWindow, 0), this.dataWindow = e; } this.dataWindow[s] = e, s++; } else this.dataWindow = o; if (this.wavesurfer) { let e = (this.dataWindow?.length ?? 0) / i; this.wavesurfer.load("", [this.dataWindow], this.options.scrollingWaveform ? this.options.scrollingWaveformWindow : e).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((e) => { console.error("Error rendering real-time recording data:", e); }); } } }, 10); return { onDestroy: () => { clearInterval(c), n?.disconnect(), t?.close(); }, onEnd: () => { this.isWaveformPaused = !0, this.stopMic(); } }; } startMic(t) { return e(this, void 0, void 0, function* () { let e; this.micStream && this.stopMic(); try { e = yield navigator.mediaDevices.getUserMedia({ audio: t == null || t }); } catch (e) { throw Error("Error accessing the microphone: " + e.message); } let n = this.renderMicStream(e); return this.micStream = n, this.unsubscribeDestroy = this.once("destroy", n.onDestroy), this.unsubscribeRecordEnd = this.once("record-end", n.onEnd), this.stream = e, e; }); } stopMic() { var e, t, n; (e = this.micStream) == null || e.onDestroy(), (t = this.unsubscribeDestroy) == null || t.call(this), (n = this.unsubscribeRecordEnd) == null || n.call(this), this.micStream = null, this.unsubscribeDestroy = void 0, this.unsubscribeRecordEnd = void 0, this.stream && (this.stream.getTracks().forEach((e) => e.stop()), this.stream = null, this.mediaRecorder = null); } startRecording(t) { return e(this, void 0, void 0, function* () { let e = this.stream || (yield this.startMic(t)); this.dataWindow = null; let n = this.mediaRecorder || new MediaRecorder(e, { mimeType: this.options.mimeType || a.find((e) => MediaRecorder.isTypeSupported(e)), audioBitsPerSecond: this.options.audioBitsPerSecond }); this.mediaRecorder = n, this.stopRecording(); let r = []; n.ondataavailable = (e) => { e.data.size > 0 && r.push(e.data), this.emit("record-data-available", e.data); }; let i = (e) => { var t; let i = new Blob(r, { type: n.mimeType }); this.emit(e, i), this.options.renderRecordedAudio && (this.applyOriginalOptionsIfNeeded(), this.recordedBlobUrl && URL.revokeObjectURL(this.recordedBlobUrl), this.recordedBlobUrl = URL.createObjectURL(i), (t = this.wavesurfer) == null || t.load(this.recordedBlobUrl)); }; n.onpause = () => i("record-pause"), n.onstop = () => i("record-end"), n.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() { return this.mediaRecorder?.state === "recording"; } isPaused() { return this.mediaRecorder?.state === "paused"; } isActive() { return this.mediaRecorder?.state !== "inactive"; } stopRecording() { var e; this.isActive() && ((e = this.mediaRecorder) == null || e.stop(), this.timer.stop()); } pauseRecording() { var e, t; this.isRecording() && (this.isWaveformPaused = !0, (e = this.mediaRecorder) == null || e.requestData(), (t = this.mediaRecorder) == null || t.pause(), this.timer.stop(), this.lastDuration = this.duration); } resumeRecording() { var e; this.isPaused() && (this.isWaveformPaused = !1, (e = this.mediaRecorder) == null || e.resume(), this.timer.start(), this.lastStartTime = performance.now(), this.emit("record-resume")); } static getAvailableAudioDevices() { return e(this, void 0, void 0, function* () { return navigator.mediaDevices.enumerateDevices().then((e) => e.filter((e) => e.kind === "audioinput")); }); } destroy() { this.applyOriginalOptionsIfNeeded(), super.destroy(), this.stopRecording(), this.stopMic(), this.recordedBlobUrl &&= (URL.revokeObjectURL(this.recordedBlobUrl), null); } applyOriginalOptionsIfNeeded() { this.wavesurfer && this.originalOptions && (this.wavesurfer.setOptions(this.originalOptions), delete this.originalOptions); } }; //#endregion export { o as default }; //# sourceMappingURL=record-CvRL89xC-Djmz1rvI.js.map