forked from Blendman974/faceclaw
Add Transcribe screen
This commit is contained in:
1 parent
811477811e
commit
bcc30545ad
16 files changed
+904
-20
No files matched your search
@@ -8,6 +8,7 @@ import { nightscoutBridge } from "../native/nightscout";
|
||||
import { readActiveNotificationIcons } from "../native/notification-icons";
|
||||
import { readPhoneBatteryState } from "../native/phone-battery";
|
||||
import { readSystemStatusIcons } from "../native/system-status-icons";
|
||||
import { voiceControlBridge, type VoiceTranscriptEvent } from "../native/voice-control";
|
||||
import { EventSourceType, OsEventTypeList } from "../g2/events";
|
||||
import {
|
||||
getDashboardPlugin,
|
||||
@@ -85,6 +86,9 @@ const dashboardActions: LayerActions = {
|
||||
endTextSettingEdit: () => {},
|
||||
setVoiceControlEnabled: () => {},
|
||||
setStopwatchRenderActive: () => {},
|
||||
setTranscribeRenderActive: () => {},
|
||||
startDedicatedVoiceInput: () => {},
|
||||
stopDedicatedVoiceInput: () => {},
|
||||
};
|
||||
const dashboardFont = loadEmbeddedTerminus12();
|
||||
const dashboardSystemFont = loadEmbeddedTerminus16();
|
||||
@@ -487,6 +491,14 @@ function createAppsMenuLayer(): MenuLayer {
|
||||
void ctx.actions.setStopwatchRenderActive(true);
|
||||
},
|
||||
},
|
||||
{
|
||||
label: "Transcribe",
|
||||
onSelect: (ctx) => {
|
||||
const layer = new TranscribeLayer();
|
||||
ctx.stack.push(layer);
|
||||
layer.start(ctx);
|
||||
},
|
||||
},
|
||||
],
|
||||
TOP_LEFT_MENU_LAYOUT,
|
||||
);
|
||||
@@ -539,6 +551,122 @@ class StopwatchLayer implements Layer {
|
||||
}
|
||||
}
|
||||
|
||||
class TranscribeLayer implements Layer {
|
||||
private mode: "off" | "wakeword" | "full" = "full";
|
||||
private status = "Starting transcription...";
|
||||
private transcript = "";
|
||||
private unsubscribeTranscript: (() => void) | null = null;
|
||||
private unsubscribeStatus: (() => void) | null = null;
|
||||
|
||||
start(ctx: LayerContext): void {
|
||||
this.unsubscribeTranscript = voiceControlBridge.onTranscript((event) => this.onTranscript(event));
|
||||
this.unsubscribeStatus = voiceControlBridge.onStatus((state) => {
|
||||
this.status = state.status;
|
||||
});
|
||||
void ctx.actions.setTranscribeRenderActive(true);
|
||||
void Promise.resolve(ctx.actions.startDedicatedVoiceInput("full")).catch((error) => {
|
||||
this.status = `Could not start transcription: ${error instanceof Error ? error.message : String(error)}`;
|
||||
});
|
||||
}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const image = new GrayImage(G2_LENS_WIDTH, G2_LENS_HEIGHT, 0);
|
||||
const modeLabel = this.mode === "full" ? "Full" : this.mode === "wakeword" ? "Wakeword" : "Off";
|
||||
const text = this.transcript || "Listening...";
|
||||
const wrapped = wrapTranscribeText(ctx.font, text, G2_LENS_WIDTH - 64);
|
||||
|
||||
image.drawRect(12, 12, G2_LENS_WIDTH - 24, G2_LENS_HEIGHT - 24, 52);
|
||||
image.drawText(ctx.font, 24, 24, "Transcribe", 200);
|
||||
image.drawText(ctx.font, G2_LENS_WIDTH - 108, 24, modeLabel, this.mode === "full" ? 220 : 130);
|
||||
//image.drawText(ctx.font, 24, 46, truncateTranscribeLine(this.status, 74), 110);
|
||||
image.drawText(ctx.font, 24, 46, this.status, 110);
|
||||
|
||||
const firstLine = Math.max(0, wrapped.length - 10);
|
||||
for (let index = firstLine; index < wrapped.length; index++) {
|
||||
const y = 72 + (index - firstLine) * 16;
|
||||
image.drawText(ctx.font, 32, y, wrapped[index]!, 230);
|
||||
}
|
||||
|
||||
image.drawText(ctx.font, 24, 252, "Click: mode / Double-click: back", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
handleInput(event: DashboardInputEvent, ctx: LayerContext): void {
|
||||
if (event.type === "double-click") {
|
||||
this.stop(ctx);
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
}
|
||||
if (event.type === "click") {
|
||||
this.cycleMode(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
private cycleMode(ctx: LayerContext): void {
|
||||
if (this.mode === "full") {
|
||||
this.mode = "wakeword";
|
||||
void ctx.actions.startDedicatedVoiceInput("wakeword");
|
||||
} else if (this.mode === "wakeword") {
|
||||
this.mode = "off";
|
||||
void ctx.actions.stopDedicatedVoiceInput();
|
||||
} else {
|
||||
this.mode = "full";
|
||||
void ctx.actions.startDedicatedVoiceInput("full");
|
||||
}
|
||||
}
|
||||
|
||||
private stop(ctx: LayerContext): void {
|
||||
this.unsubscribeTranscript?.();
|
||||
this.unsubscribeTranscript = null;
|
||||
this.unsubscribeStatus?.();
|
||||
this.unsubscribeStatus = null;
|
||||
void ctx.actions.setTranscribeRenderActive(false);
|
||||
void ctx.actions.stopDedicatedVoiceInput();
|
||||
}
|
||||
|
||||
private onTranscript(event: VoiceTranscriptEvent): void {
|
||||
if (event.isFinal) {
|
||||
if (this.transcript && !this.transcript.endsWith("\n")) {
|
||||
this.transcript = `${this.transcript}\n`;
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.transcript = appendTranscriptChunk(this.transcript, event.text);
|
||||
}
|
||||
}
|
||||
|
||||
function appendTranscriptChunk(transcript: string, chunk: string): string {
|
||||
const trimmed = chunk.replace(/\s+/g, " ").trim();
|
||||
if (!trimmed) return transcript;
|
||||
if (!transcript || transcript.endsWith("\n") || /^[.,!?;:]/.test(trimmed)) {
|
||||
return `${transcript}${trimmed}`;
|
||||
}
|
||||
return `${transcript} ${trimmed}`;
|
||||
}
|
||||
|
||||
function wrapTranscribeText(font: typeof dashboardFont, text: string, maxWidth: number): string[] {
|
||||
const words = text.split(/\s+/).filter(Boolean);
|
||||
const lines: string[] = [];
|
||||
let line = "";
|
||||
for (const word of words) {
|
||||
const candidate = line ? `${line} ${word}` : word;
|
||||
if (line && font.measureText(candidate) > maxWidth) {
|
||||
lines.push(line);
|
||||
line = word;
|
||||
} else {
|
||||
line = candidate;
|
||||
}
|
||||
}
|
||||
if (line) {
|
||||
lines.push(line);
|
||||
}
|
||||
return lines.length ? lines : [""];
|
||||
}
|
||||
|
||||
function truncateTranscribeLine(text: string, maxChars: number): string {
|
||||
return text.length <= maxChars ? text : `${text.slice(0, maxChars - 3)}...`;
|
||||
}
|
||||
|
||||
function formatStopwatchElapsed(elapsedMs: number): string {
|
||||
const totalTenths = Math.floor(elapsedMs / 100);
|
||||
const tenths = totalTenths % 10;
|
||||
|
||||
@@ -17,6 +17,9 @@ export type LayerActions = {
|
||||
endTextSettingEdit: () => Promise<void> | void;
|
||||
setVoiceControlEnabled: (enabled: boolean) => Promise<void> | void;
|
||||
setStopwatchRenderActive: (active: boolean) => Promise<void> | void;
|
||||
setTranscribeRenderActive: (active: boolean) => Promise<void> | void;
|
||||
startDedicatedVoiceInput: (mode: "wakeword" | "full") => Promise<void> | void;
|
||||
stopDedicatedVoiceInput: () => Promise<void> | void;
|
||||
};
|
||||
|
||||
export type PaintBelow = () => GrayImage;
|
||||
|
||||
Reference in new issue
Block a user