|
| 1 | +// Audio recording and transcription |
| 2 | +(function () { |
| 3 | + let mediaRecorder = null; |
| 4 | + let audioChunks = []; |
| 5 | + |
| 6 | + const MIC_SVG = |
| 7 | + '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="23"/><line x1="8" y1="23" x2="16" y2="23"/></svg>'; |
| 8 | + const STOP_SVG = |
| 9 | + '<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" fill="currentColor" stroke="none"><rect x="4" y="4" width="16" height="16" rx="2"/></svg>'; |
| 10 | + |
| 11 | + function getMicButton() { |
| 12 | + return document.getElementById("micButton"); |
| 13 | + } |
| 14 | + |
| 15 | + function getTextarea() { |
| 16 | + return document.getElementById("userInput"); |
| 17 | + } |
| 18 | + |
| 19 | + async function startRecording() { |
| 20 | + const btn = getMicButton(); |
| 21 | + |
| 22 | + if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) { |
| 23 | + alert( |
| 24 | + "Microphone access requires a secure context.\n" + |
| 25 | + "Please access this app via http://localhost:8000 instead of http://0.0.0.0:8000." |
| 26 | + ); |
| 27 | + return; |
| 28 | + } |
| 29 | + |
| 30 | + try { |
| 31 | + const stream = await navigator.mediaDevices.getUserMedia({ audio: true }); |
| 32 | + mediaRecorder = new MediaRecorder(stream); |
| 33 | + audioChunks = []; |
| 34 | + |
| 35 | + mediaRecorder.addEventListener("dataavailable", (e) => { |
| 36 | + if (e.data.size > 0) audioChunks.push(e.data); |
| 37 | + }); |
| 38 | + |
| 39 | + mediaRecorder.addEventListener("stop", async () => { |
| 40 | + // Stop all tracks so the browser releases the mic |
| 41 | + stream.getTracks().forEach((t) => t.stop()); |
| 42 | + |
| 43 | + const blob = new Blob(audioChunks, { type: mediaRecorder.mimeType }); |
| 44 | + await transcribe(blob); |
| 45 | + }); |
| 46 | + |
| 47 | + mediaRecorder.start(); |
| 48 | + btn.classList.add("recording"); |
| 49 | + btn.innerHTML = STOP_SVG; |
| 50 | + btn.title = "Stop recording"; |
| 51 | + } catch (err) { |
| 52 | + console.error("Microphone access denied:", err); |
| 53 | + alert("Could not access your microphone. Please check permissions."); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + function stopRecording() { |
| 58 | + if (mediaRecorder && mediaRecorder.state !== "inactive") { |
| 59 | + mediaRecorder.stop(); |
| 60 | + } |
| 61 | + const btn = getMicButton(); |
| 62 | + btn.classList.remove("recording"); |
| 63 | + btn.innerHTML = MIC_SVG; |
| 64 | + btn.title = "Record audio"; |
| 65 | + } |
| 66 | + |
| 67 | + async function transcribe(blob) { |
| 68 | + const formData = new FormData(); |
| 69 | + // Use webm extension; Whisper accepts it |
| 70 | + formData.append("audio", blob, "recording.webm"); |
| 71 | + |
| 72 | + const textarea = getTextarea(); |
| 73 | + const btn = getMicButton(); |
| 74 | + const savedPlaceholder = textarea ? textarea.placeholder : ""; |
| 75 | + |
| 76 | + // Show transcribing state |
| 77 | + if (textarea) { |
| 78 | + textarea.placeholder = "Transcribing..."; |
| 79 | + textarea.disabled = true; |
| 80 | + } |
| 81 | + if (btn) btn.disabled = true; |
| 82 | + |
| 83 | + try { |
| 84 | + const res = await fetch("/audio/transcribe", { |
| 85 | + method: "POST", |
| 86 | + body: formData, |
| 87 | + }); |
| 88 | + |
| 89 | + if (!res.ok) { |
| 90 | + console.error("Transcription failed:", res.status); |
| 91 | + alert("Transcription failed. Please try again."); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const text = await res.text(); |
| 96 | + if (textarea) { |
| 97 | + // Append with a space if there's already text |
| 98 | + const current = textarea.value.trim(); |
| 99 | + textarea.value = current ? current + " " + text : text; |
| 100 | + // Trigger resize |
| 101 | + textarea.style.height = "auto"; |
| 102 | + textarea.style.height = textarea.scrollHeight + "px"; |
| 103 | + textarea.focus(); |
| 104 | + } |
| 105 | + } catch (err) { |
| 106 | + console.error("Transcription request error:", err); |
| 107 | + alert("Transcription failed. Please try again."); |
| 108 | + } finally { |
| 109 | + // Restore input state |
| 110 | + if (textarea) { |
| 111 | + textarea.placeholder = savedPlaceholder; |
| 112 | + textarea.disabled = false; |
| 113 | + } |
| 114 | + if (btn) btn.disabled = false; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Toggle recording on mic button click |
| 119 | + document.addEventListener("click", (e) => { |
| 120 | + const btn = e.target.closest("#micButton"); |
| 121 | + if (!btn) return; |
| 122 | + |
| 123 | + if (mediaRecorder && mediaRecorder.state === "recording") { |
| 124 | + stopRecording(); |
| 125 | + } else { |
| 126 | + startRecording(); |
| 127 | + } |
| 128 | + }); |
| 129 | +})(); |
0 commit comments