Record review audio in the browser
The review panel can now record from the microphone as well as take a file. The take is made with MediaRecorder at 32 kbps, which is plenty for speech and puts the 25 MB limit at over an hour; it stops itself just short of that. It is named .webm (Chrome, Firefox) or .m4a (Safari) to match its container, since the server picks the format by extension, so the backend is unchanged. Whichever recording is chosen, recorded or picked, gets a player to check it before sending. Closing the panel mid-take discards it and releases the microphone. Recording needs HTTPS or localhost; elsewhere the button is disabled with a note saying why. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -157,6 +157,7 @@ The Explain panel on a problem page (admin only) asks Claude for an intuitive ex
|
||||
|
||||
The "Review my explanation" panel (admin only) takes a recording of the admin explaining a solution out loud, as to an interviewer, and commits feedback on it as `reviews/<id>.md`. The id is the UTC time it started (`2026-10-20-153000`), so each attempt is its own file and the page lists them newest first.
|
||||
|
||||
- **Recording in the browser.** Besides a file picker, the panel records from the microphone (`AudioRecorder.vue`, `MediaRecorder` at 32 kbps): webm/Opus in Chrome and Firefox, m4a in Safari, named with that extension because the server picks the format by it. A take stops itself just short of 25 MB (over an hour), and either kind of recording gets a player to check before sending. The microphone needs a secure context, so it works on the deployed HTTPS site and on `localhost`, not over plain HTTP on a LAN address.
|
||||
- **Transcription.** Claude cannot take audio, so the recording goes first to an OpenAI-compatible `/v1/audio/transcriptions` endpoint: `TRANSCRIBE_URL` (default the OpenAI API) with `TRANSCRIBE_MODEL` (default `whisper-1`, about $0.006 a minute). It asks for word and segment timestamps, and for a verbatim transcript, because Whisper otherwise drops the fillers. A self-hosted faster-whisper server works by changing the URL.
|
||||
- **Metrics.** Pace (words per minute), pauses of 2 seconds or more, filler-word counts and when complexity was first mentioned are measured from the timestamps (`transcribe.Metrics`), since Claude never hears the recording. It is told so, and judges delivery from the words and these numbers only, not tone.
|
||||
- **Review.** The statement, examples, optionally the main solution file, the admin's note, the metrics and the timestamped transcript go to Claude with a JSON schema (`internal/review` `Schema`): a verdict (correct, partly correct, incorrect, incomplete), a restatement, an assessment of the approach and complexities, missed edge cases, delivery feedback quoting `[mm:ss]`, 1 to 5 scores in five areas, strengths and things to work on. The markdown ends with the metrics and the full transcript. Its first line is a `<!-- review verdict=… audio=… -->` comment the index reads.
|
||||
|
||||
@@ -0,0 +1,152 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Records from the microphone, for a review without leaving the page. The
|
||||
* take is handed up as a File named for its container, because the server
|
||||
* picks the format by extension: Chrome and Firefox record webm/Opus, Safari
|
||||
* mp4/AAC. A take stops itself before it outgrows maxBytes.
|
||||
*/
|
||||
import { computed, onUnmounted, ref } from "vue";
|
||||
|
||||
const props = defineProps({ maxBytes: { type: Number, required: true } });
|
||||
const emit = defineEmits(["start", "recorded", "error"]);
|
||||
|
||||
// Speech needs nothing like the browser's default 128 kbps; at 32 kbps
|
||||
// 25 MB is well over an hour.
|
||||
const BITRATE = 32000;
|
||||
const TYPES = [
|
||||
["audio/webm;codecs=opus", ".webm"],
|
||||
["audio/mp4", ".m4a"],
|
||||
["audio/ogg;codecs=opus", ".ogg"],
|
||||
];
|
||||
// Room for the last chunk and the container's trailer.
|
||||
const SLACK = 256 << 10;
|
||||
|
||||
const supported = !!(navigator.mediaDevices?.getUserMedia && window.MediaRecorder);
|
||||
const state = ref("idle"); // idle | asking | recording
|
||||
const elapsed = ref(0);
|
||||
let recorder = null;
|
||||
let stream = null;
|
||||
let timer = null;
|
||||
let discard = false;
|
||||
|
||||
const clock = computed(() => {
|
||||
const s = Math.floor(elapsed.value / 1000);
|
||||
return `${Math.floor(s / 60)}:${String(s % 60).padStart(2, "0")}`;
|
||||
});
|
||||
|
||||
function release() {
|
||||
clearInterval(timer);
|
||||
timer = null;
|
||||
stream?.getTracks().forEach((t) => t.stop());
|
||||
stream = null;
|
||||
recorder = null;
|
||||
}
|
||||
|
||||
async function start() {
|
||||
state.value = "asking";
|
||||
try {
|
||||
stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
||||
} catch (e) {
|
||||
state.value = "idle";
|
||||
emit("error", e.name === "NotAllowedError" ? "Microphone access was blocked; allow it for this site and try again." : `No microphone: ${e.message}`);
|
||||
return;
|
||||
}
|
||||
// The component may have gone while the permission prompt was up.
|
||||
if (discard) {
|
||||
release();
|
||||
return;
|
||||
}
|
||||
|
||||
const [mimeType, ext] = TYPES.find(([t]) => MediaRecorder.isTypeSupported(t)) || ["", ".webm"];
|
||||
recorder = new MediaRecorder(stream, { mimeType: mimeType || undefined, audioBitsPerSecond: BITRATE });
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
let full = false;
|
||||
recorder.ondataavailable = (e) => {
|
||||
chunks.push(e.data);
|
||||
size += e.data.size;
|
||||
if (size > props.maxBytes - SLACK && recorder?.state === "recording") {
|
||||
full = true;
|
||||
recorder.stop();
|
||||
}
|
||||
};
|
||||
recorder.onstop = () => {
|
||||
const type = recorder.mimeType || mimeType || "audio/webm";
|
||||
release();
|
||||
state.value = "idle";
|
||||
if (discard) return;
|
||||
const stamp = new Date().toISOString().slice(0, 19).replace(/[-:]/g, "").replace("T", "-");
|
||||
emit("recorded", new File(chunks, `recording-${stamp}${ext}`, { type }));
|
||||
if (full) emit("error", "The recording reached the 25 MB limit and was stopped there.");
|
||||
};
|
||||
|
||||
emit("start");
|
||||
// Chunks every second, so the size check above sees the take grow.
|
||||
recorder.start(1000);
|
||||
const began = Date.now();
|
||||
elapsed.value = 0;
|
||||
timer = setInterval(() => (elapsed.value = Date.now() - began), 250);
|
||||
state.value = "recording";
|
||||
}
|
||||
|
||||
function stop() {
|
||||
if (recorder?.state === "recording") recorder.stop();
|
||||
}
|
||||
|
||||
// Closing the panel mid-take throws the take away and frees the microphone.
|
||||
onUnmounted(() => {
|
||||
discard = true;
|
||||
if (recorder?.state === "recording") recorder.stop();
|
||||
else release();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="recorder">
|
||||
<button v-if="state !== 'recording'" class="btn" type="button" :disabled="!supported || state === 'asking'" @click="start">
|
||||
<span class="dot" aria-hidden="true"></span>{{ state === "asking" ? "Waiting for the microphone…" : "Record" }}
|
||||
</button>
|
||||
<template v-else>
|
||||
<button class="btn primary" type="button" @click="stop">Stop</button>
|
||||
<span class="live" role="status"><span class="dot on" aria-hidden="true"></span>Recording {{ clock }}</span>
|
||||
</template>
|
||||
<span v-if="!supported" class="muted">This browser can't record here (it needs HTTPS or localhost).</span>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.recorder {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
.dot {
|
||||
display: inline-block;
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
margin-right: 8px;
|
||||
border-radius: 50%;
|
||||
background: var(--color-ink-danger);
|
||||
vertical-align: middle;
|
||||
}
|
||||
.live {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
.dot.on {
|
||||
animation: pulse 1.2s ease-in-out infinite;
|
||||
}
|
||||
@keyframes pulse {
|
||||
50% {
|
||||
opacity: 0.25;
|
||||
}
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.dot.on {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -10,6 +10,7 @@
|
||||
*/
|
||||
import { computed, onUnmounted, ref, watch } from "vue";
|
||||
import { RouterLink } from "vue-router";
|
||||
import AudioRecorder from "@/components/AudioRecorder.vue";
|
||||
import CodeBlock from "@/components/CodeBlock.vue";
|
||||
import Explanation from "@/components/Explanation.vue";
|
||||
import ReviewList from "@/components/ReviewList.vue";
|
||||
@@ -76,7 +77,8 @@ const tools = [
|
||||
];
|
||||
|
||||
function toggle(id) {
|
||||
// The file input goes with the panel, so the picked file must too.
|
||||
// The file input and recorder go with the panel, so the chosen recording
|
||||
// must too.
|
||||
if (panel.value === "review") recording.value = null;
|
||||
if (panel.value === id) {
|
||||
panel.value = "";
|
||||
@@ -205,15 +207,39 @@ watch(() => props.name, stopReviewPolling);
|
||||
// 25 MB is the transcriber's limit; checking here saves uploading it first.
|
||||
const MAX_RECORDING = 25 << 20;
|
||||
|
||||
function pickRecording(e) {
|
||||
recording.value = e.target.files[0] || null;
|
||||
if (recording.value && recording.value.size > MAX_RECORDING) {
|
||||
function useRecording(f) {
|
||||
recording.value = f;
|
||||
if (f && f.size > MAX_RECORDING) {
|
||||
reviewJob.value = { state: "error", error: "That recording is over 25 MB; export it at a lower bitrate." };
|
||||
} else if (reviewJob.value.state === "error") {
|
||||
reviewJob.value = { state: "idle" };
|
||||
}
|
||||
}
|
||||
|
||||
function pickRecording(e) {
|
||||
useRecording(e.target.files[0] || null);
|
||||
}
|
||||
|
||||
// A take recorded here replaces a picked file, so the file input must not
|
||||
// go on naming one.
|
||||
const fileInput = ref(null);
|
||||
function tookRecording(f) {
|
||||
if (fileInput.value) fileInput.value.value = "";
|
||||
useRecording(f);
|
||||
}
|
||||
|
||||
function recorderError(message) {
|
||||
reviewJob.value = { state: "error", error: message };
|
||||
}
|
||||
|
||||
// Whichever recording is chosen gets a player, to check it before sending.
|
||||
const recordingUrl = ref("");
|
||||
watch(recording, (f) => {
|
||||
URL.revokeObjectURL(recordingUrl.value);
|
||||
recordingUrl.value = f ? URL.createObjectURL(f) : "";
|
||||
});
|
||||
onUnmounted(() => URL.revokeObjectURL(recordingUrl.value));
|
||||
|
||||
async function checkReview() {
|
||||
stopReviewPolling();
|
||||
const name = props.name;
|
||||
@@ -355,11 +381,21 @@ const moves = computed(() => {
|
||||
<div v-if="panel === 'review'" class="panel">
|
||||
<h2>Review my explanation</h2>
|
||||
<p class="muted">
|
||||
Record yourself explaining your solution as you would to an interviewer, then upload it (mp3, m4a, wav,
|
||||
webm, ogg or flac, up to 25 MB). It is transcribed, and Claude gives feedback on the approach and on your
|
||||
delivery. Each review is committed under <code>reviews/</code>.
|
||||
Record yourself explaining your solution as you would to an interviewer, here or elsewhere and upload it
|
||||
(mp3, m4a, wav, webm, ogg or flac, up to 25 MB). It is transcribed, and Claude gives feedback on the approach
|
||||
and on your delivery. Each review is committed under <code>reviews/</code>.
|
||||
</p>
|
||||
<input type="file" accept="audio/*,.mp3,.m4a,.wav,.webm,.ogg,.flac" @change="pickRecording" />
|
||||
<div class="source">
|
||||
<AudioRecorder
|
||||
:max-bytes="MAX_RECORDING"
|
||||
@start="useRecording(null)"
|
||||
@recorded="tookRecording"
|
||||
@error="recorderError"
|
||||
/>
|
||||
<span class="muted">or</span>
|
||||
<input ref="fileInput" type="file" accept="audio/*,.mp3,.m4a,.wav,.webm,.ogg,.flac" @change="pickRecording" />
|
||||
</div>
|
||||
<audio v-if="recordingUrl" :src="recordingUrl" controls preload="metadata"></audio>
|
||||
<textarea v-model="reviewNote" rows="2" placeholder="Optional context: a mock interview, a time limit, what to focus on"></textarea>
|
||||
<div class="admin-row">
|
||||
<label class="check"><input v-model="reviewCode" type="checkbox" /> Include my code</label>
|
||||
@@ -598,6 +634,16 @@ h2 {
|
||||
color: var(--color-ink);
|
||||
background: var(--color-paper);
|
||||
}
|
||||
.source {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
.panel audio {
|
||||
width: 100%;
|
||||
max-width: 480px;
|
||||
}
|
||||
.panel input[type="file"] {
|
||||
font-size: 0.9rem;
|
||||
max-width: 100%;
|
||||
|
||||
Reference in New Issue
Block a user