Record review audio in the browser
Test and publish the site / test (push) Successful in 44s
Test and publish the site / publish (push) Successful in 35s

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:
2026-10-20 10:32:46 +01:00
co-authored by Claude Opus 5.5
parent 4dc8d1674c
commit 107ecab9da
3 changed files with 207 additions and 8 deletions
+152
View File
@@ -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>
+54 -8
View File
@@ -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%;