# lilchinese — live 高橋メソッド subtitles for spoken Chinese Speak Chinese into your mic → huge black-on-white Chinese phrases fill the screen, one short phrase at a time. 100% local: no Google, no cloud, no network at runtime. ``` parec (mic) ──► sherpa-onnx streaming zipformer (zh, int8) ──► SSE ──► Firefox kiosk 20 ms reads offline, on CPU, decode chunk 320 ms local Noto Sans CJK SC Black │ └─► (optional) llama-server + Hy-MT2-1.8B ──► English line ``` ## Why this shape - **ASR in Go, pixels in the browser.** The Go side owns the audio path (a long-lived stream that must not hiccup); the browser owns text layout, CJK font shaping, screen fitting and fullscreen — things a Go GUI toolkit makes you fight for. - **Server-sent events, not WebSocket.** The stream is one-way, so SSE via `net/http` needs zero dependencies and reconnects by itself. - **Firefox is fine.** Because recognition happens in the local binary, Firefox never touches a speech service; it is only a renderer. (Firefox has no Web Speech API in any case — which is exactly why the ASR lives here.) ## Setup ```sh ./scripts/fetch-model.sh # 133 MB, int8 streaming zipformer, Chinese go build -o lilchinese . # needs gcc (cgo) + prebuilt sherpa-onnx libs ./scripts/fetch-mt-model.sh # optional: 1.1 GB Hy-MT2-1.8B for English subtitles # (needs llama-server from llama.cpp on PATH) ``` ## Run ```sh ./lilchinese # mic → Firefox kiosk, fullscreen ./lilchinese -record talk.mp4 # same, but also record screen + mic ./lilchinese -wav speech.wav # play a file through the pipeline instead ./lilchinese -debug # overlay "N字 340ms" in the corner ``` Exit kiosk with `Alt+F4` (there is no chrome to click). `Ctrl+C` in the terminal stops everything and finalizes a recording. Then just talk. Silence holds the last phrase on screen — that is the Takahashi rhythm: one short thought per screen, replaced on the next one. ### During the talk | key | what it does | |---|---| | `Enter` | **start** while the intro is up; afterwards **keep** — the sentence turns green and is appended to the session log | | `Backspace` / `Delete` / `c` | **cut** — the screen goes red and the half-heard sentence is dropped so you can say it again | | `f` | toggle fullscreen | The red stays up until the retake is heard, then it is back to normal colours. It is not just a mood light: the ASR state is reset at the cut, so the flubbed sentence cannot leak into the next one, and because the recording is a screen capture the red frame also marks where each retake starts when you edit the video. Colours alternate — black on white, then white on black — at every sentence boundary (a pause long enough for the recogniser's endpoint detector), so consecutive thoughts read as clearly separate slides. **While you speak**, a long sentence flips through short phrases. **When you stop**, the sentence settles: if it is at most `-settle` (20) characters, the whole thing replaces the last fragment. So *我觉得今年我的中文会说得好一些* no longer ends up on screen as `会说得好一些` — the frame that stays up, gets recorded, gets translated and is what `Enter` logs is the sentence itself. Longer sentences keep the fragment behaviour; `-settle 0` restores pure Takahashi. ## Intro page The talk opens on a red title slide — photo, 批斗大会, STRUGGLE SESSION, and a `⏎ Press enter to start` hint at the bottom. `Enter` starts the display *and* the recognition: while the intro is up nothing is published, so the noise of setting up (moving chairs, testing the mic) never turns into a slide. | flag | default | meaning | |---|---|---| | `-intro` | `true` | show the intro and wait for Enter | | `-intro-zh` | `批斗大会` | headline | | `-intro-en` | `STRUGGLE SESSION` | subtitle | | `-intro-image` | `/intro.jpg` | picture; overwrite `web/intro.jpg` (or pass a URL/local path) to change it | > `web/intro.jpg` is third-party press photography from secretchina.com, bundled here for > convenience. It is not covered by this project's licence — swap it for something you > have the rights to if the repo ever stops being private. ## Session log (Enter) `Enter` turns the sentence on screen green and appends it to a Markdown file, so the talk can be cut down into a script afterwards: ```markdown # lilchinese session 2026-11-19 18:34 - 18:34:06 重点呢想谈三个问题首先呢就是这一轮全球金融动荡的表现 — I would like to focus on three issues. First, it is about the manifestations of this round of global financial turmoil - 18:35:41 我自学中文已经五年了 — I have been studying Chinese for five years ``` - Default target is `sessions/session-.md` (gitignored); pass `-log talk.md` for a fixed file. Nothing is created until you keep something. - The **whole sentence** is logged, not the 9 characters on screen — including the last syllables: the snapshot waits 450 ms for the model's lookahead to finish decoding. - The English is a fresh translation of that exact sentence, produced off the audio path, so a slow translator never delays recognition. Entries are written in order by one worker. - A double `Enter` within 2 s does not duplicate the line. ## English subtitles (optional) Pass a local translation model and every phrase gets an English line underneath, translated on your own machine: ```sh ./lilchinese -translate-model models/mt/Hy-MT2-1.8B-Q4_K_M.gguf ./lilchinese -translate-model models/mt/Hy-MT2-1.8B-Q4_K_M.gguf -record talk.mp4 ``` `-translate-model` spawns `llama-server` itself (free port, waits for `/health`, killed on exit, log in `~/.cache/lilchinese/llama-server.log`). If you already run an OpenAI-compatible endpoint — llama-server, ollama, LM Studio — point at it instead and skip the spawn: ```sh ./lilchinese -translate http://127.0.0.1:11434 # ollama also speaks /v1/chat/completions ./lilchinese -translate http://127.0.0.1:8080 -prompt '将以下文本翻译成英语:{text}' ``` How it behaves, and why: - **Hy-MT2-1.8B** is a dedicated zh→en translation model, which matters here: slides are 9-character fragments, and general chat models (Qwen, GPT-class) hallucinate context around fragments while claiming to translate. On this CPU it does **29–32 tok/s**, so a phrase lands in **0.5–0.8 s**. - The English line is **sticky**: it is not tied to the 9 characters currently on screen. A slide lives ~0.3 s while speaking continuously — no LLM can keep up with that, so the translation lags the speaker by roughly one model call and updates as the sentence grows, like an interpreter. It never blanks between phrases. - `-translate-window` (default 40) is how many characters of speech go to the model. Larger = smoother/more context but slower; smaller = snappier, choppier fragments. - Translations are cached per phrase, so repeated words cost nothing. - Without `-translate`/`-translate-model` the page is laid out exactly as before — the English line is only reserved when `?en=1` is passed to the page. ## Flags | flag | default | meaning | |---|---|---| | `-chars` | `9` | max characters per slide (the whole point: keep it short) | | `-settle` | `20` | on a pause, sentences up to this length are shown whole (`0` = off) | | `-preview` | `3` | don't replace a full slide with a shorter in-progress phrase than this | | `-source` | system default | PulseAudio source name (`pactl list short sources`) | | `-model` | auto-detect `models/` | model directory | | `-browser` | `firefox --kiosk {url}` | how to open the display; `{url}` substituted; empty = don't launch | | `-record` | off | ffmpeg: `x11grab` + `pulse` → h264/aac mp4 | | `-log` | `sessions/` | session log target: a file, or a directory for a timestamped file | | `-intro`, `-intro-zh`, `-intro-en`, `-intro-image` | on, 批斗大会, STRUGGLE SESSION, /intro.jpg | title slide | | `-threads` | `2` | onnxruntime threads | | `-addr` | `127.0.0.1:8787` | HTTP/SSE listen address | | `-wav` | off | feed an audio file (paced realtime) instead of the mic | | `-translate-model` | off | spawn llama-server with this `.gguf` for English subtitles | | `-translate` | off | use an already running OpenAI-compatible endpoint | | `-translate-window` | `40` | characters of speech sent to the translator | | `-prompt` | Hy-MT template | translation prompt, `{text}` substituted | | `-llama-server` / `-llama-args` / `-llama-threads` | `llama-server` / — / half the cores | spawned backend tuning | ## Latency, honestly - Model lookahead: **320 ms** (`decode_chunk_len=32` frames in this model). Recognition cannot show a syllable before its chunk is complete — that is the floor for any streaming ASR. - Plumbing (audio read → SSE → paint): **~35 ms** measured (`ts - cap` in the SSE payload). - So: text appears roughly a third of a second behind your voice, then grows in 320 ms steps. There is no zero-latency speech recognizer; this is about as close as it gets while staying offline. - English is a second, slower lane: +0.5–0.8 s per phrase with Hy-MT2-1.8B on CPU (`prompt_ms` ~300 ms warm, 30 tok/s decode). It rides along with each slide update. To trade accuracy for latency, point `-model` at a CTC streaming model instead (`sherpa-onnx-streaming-zipformer-small-ctc-zh-int8-2025-04-01`, 21 MB) — non-autoregressive, smaller chunks, noticeably faster to emit, somewhat less accurate. ## Look and feel - Font stack starts at `Noto Sans CJK SC Black` (already installed here); size is auto-fitted so the phrase fills 90% × 86% of the screen, whatever its length. - Phrase changes slam in with a 90 ms scale/fade; refinements of the same phrase are updated in place so the text doesn't flicker. - Design preview without a mic or model: open `web/index.html?text=现在我开始` in any browser, and `web/index.html?en=1&text=现在我开始&t=Now I begin` to preview both lines. ## Notes - Slides break on `,。、;:?!…` and at `-chars`, whichever comes first — but a length break only happens when at least `-preview` characters remain for the next phrase. Without that rule the last frame of *我自学中文已经五年了* was just `了`, with the rest of the sentence thrown away. Short sentences therefore stay whole. - Slides are *not* re-split for the translator: it gets up to `-translate-window` (40) characters of speech, starting at a clause boundary where possible. - The binary links the shared libs from the Go module cache (`rpath` → `.../pkg/mod/github.com/k2-fsa/sherpa-onnx-go-linux@v1.13.8/lib/...`). If you wipe the module cache, rebuild. - Screen blanking will ruin a long talk: `xset s off -dpms`. - Recording captures the whole screen, so start it after the kiosk window is up (it auto-delays 1.5 s). ## Tests ```sh go test ./... # slide cutting, colour alternation, cut, keep, log format, windowing ``` ## Layout ``` main.go mic → ASR → slide splitting → SSE, recorder, browser launch log.go session log written by Enter translate.go optional local MT: llama-server supervision, debounce, sticky EN line web/index.html the whole UI (autofit, pop transition, EventSource client) scripts/ fetch-model.sh (ASR), fetch-mt-model.sh (translation) models/ gitignored ```