Multi-mic support and replay mode for Transcribe app

This commit is contained in:
James Babcock committed 2026-11-12 21:54:57 -08:00
1 parent c162c8e633
commit 9372b2742c
45 files changed
+3926 -288

No files matched your search

+72 -13
View File
@@ -3,15 +3,20 @@ import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { lineStep } from "../../ui/metrics";
import { MicLevelMeter, paintListeningHeader, RECORDING_FAILED_NOTICE } from "../../ui/listening-header";
import { writeTextToDownloads } from "../../native/file-access";
import { voiceControlBridge } from "../../native/voice-control";
import { voiceControlBridge, type VoiceEventSource, type VoiceReplay } from "../../native/voice-control";
import { Layer, type LayerContext } from "../../ui/layers";
import { type InputEvent } from "../../ui/gestures";
import { formatReplayTime } from "./replay-time";
import { TranscriptModel, TranscriptScroll, transcriptLayout, wrapTranscribeText } from "./transcript-model";
/**
* Live transcription view. Continuous mic capture is owned by the app wrapper
* (createTranscribeAppWindow); this layer accumulates the transcript, shows
* it, and saves it to Downloads from the app menu.
*
* Given a replay, it transcribes that recording instead of the microphone:
* the header shows the position in the recording, a tap pauses and resumes,
* and each jump leaves its time as a line in the transcript.
*/
export class TranscribeLayer implements Layer {
private status = "Starting microphone...";
@@ -24,25 +29,67 @@ export class TranscribeLayer implements Layer {
private unsubscribeTranscript: (() => void) | null = null;
private unsubscribePause: (() => void) | null = null;
private unsubscribeStatus: (() => void) | null = null;
private unsubscribeReplay: (() => void) | null = null;
private readonly source: VoiceEventSource;
/** `stamp` names the saved transcript, matching the session's audio recording. */
constructor(private readonly stamp: string = transcriptTimestamp()) {}
/**
* `stamp` names the saved transcript, matching the session's audio
* recording. `replay` is the recording to transcribe in place of the
* microphone; `replayName` is what to call it.
*/
constructor(
private readonly stamp: string = transcriptTimestamp(),
private readonly replay: VoiceReplay | null = null,
private readonly replayName = "",
) {
this.source = replay ? replay.events : voiceControlBridge;
if (replay) this.status = "Opening the recording...";
}
/** `isVisible` says whether the window is on screen (the level meter repaints only then). */
/** `isVisible` says whether the window is on screen (the level meter and replay clock repaint only then). */
start(requestRender: () => void, isVisible: () => boolean): void {
this.meter.start(requestRender, isVisible);
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => {
this.meter.start(requestRender, isVisible, this.source);
this.unsubscribeTranscript = this.source.onTranscript((event) => {
this.transcript.accept(event);
requestRender();
});
this.unsubscribePause = voiceControlBridge.onSpeechPause(() => this.transcript.pause());
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
this.unsubscribePause = this.source.onSpeechPause(() => this.transcript.pause());
// The current status arrives while subscribing, before the window has a
// surface to render to; the window paints it when it is first shown.
let subscribing = true;
this.unsubscribeStatus = this.source.onStatus((state) => {
this.status = state.status;
this.listening = state.listening;
this.detail = state.detail;
this.saveNotice = "";
requestRender();
if (!subscribing) requestRender();
});
subscribing = false;
const replay = this.replay;
if (replay) {
let shownSecond = -1;
let shownState = replay.state;
this.unsubscribeReplay = replay.onChange((jumpedToMs) => {
if (jumpedToMs !== undefined) this.transcript.mark(`[${formatReplayTime(jumpedToMs)}]`);
// The clock ticks in whole seconds; repaint when what it shows changed.
const second = Math.floor(replay.positionMs / 1000);
if (jumpedToMs === undefined && second === shownSecond && replay.state === shownState) return;
shownSecond = second;
shownState = replay.state;
if (isVisible()) requestRender();
});
}
}
/** The header's status for a replay: where it is in the recording, and anything worth saying beside that. */
private replayStatus(replay: VoiceReplay): string {
if (replay.state === "failed" || (replay.state === "opening" && replay.durationMs === 0)) return this.status;
const withHours = replay.durationMs >= 3_600_000;
const clock = `${formatReplayTime(replay.positionMs, withHours)} / ${formatReplayTime(replay.durationMs, withHours)}`;
if (replay.state === "paused") return `${clock} · Paused`;
if (replay.state === "ended") return `${clock} · Finished`;
// Anything but plain listening is worth showing: the model loading, a provider reconnecting.
return this.listening ? clock : `${clock} · ${this.status}`;
}
paint(ctx: LayerContext): GrayImage {
@@ -50,14 +97,19 @@ export class TranscribeLayer implements Layer {
const viewport = ctx.stack.getBaseSize();
const image = new GrayImage(viewport.width, viewport.height, 0);
const status = voiceControlBridge.recordingFailed ? RECORDING_FAILED_NOTICE : this.status;
const replay = this.replay;
const status = replay
? this.replayStatus(replay)
: this.source.recordingFailed ? RECORDING_FAILED_NOTICE : this.status;
const playing = !replay || replay.state === "playing";
const bodyTop = paintListeningHeader(
image, font, "Transcribe", this.saveNotice || status, this.listening ? this.meter : null, viewport.padding,
voiceControlBridge.recording,
image, font, "Transcribe", this.saveNotice || status, this.listening && playing ? this.meter : null, viewport.padding,
this.source.recording,
);
const step = lineStep(font) + 2;
const layout = transcriptLayout(viewport, bodyTop, font.lineHeight, step);
const text = this.transcript.text || (this.listening ? "Listening..." : this.detail);
const placeholder = replay ? `Replaying ${this.replayName}` : "Listening...";
const text = this.transcript.text || (this.listening ? placeholder : this.detail);
const wrapped = wrapTranscribeText((line) => font.measureText(line), text, layout.textWidth);
const firstLine = this.scroll.layout(wrapped.length, layout.pageLines);
@@ -82,6 +134,11 @@ export class TranscribeLayer implements Layer {
ctx.actions.requestRender();
return;
}
if (event.type === "click" && this.replay) {
this.replay.setPaused(this.replay.state === "playing");
ctx.actions.requestRender();
return;
}
if (event.type === "double-click") {
ctx.stack.pop();
}
@@ -89,6 +146,8 @@ export class TranscribeLayer implements Layer {
onRemoved(): void {
this.meter.stop();
this.unsubscribeReplay?.();
this.unsubscribeReplay = null;
this.unsubscribePause?.();
this.unsubscribePause = null;
this.unsubscribeTranscript?.();