1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
# 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 |
|---|---|
| `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.
## 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) |
| `-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 |
| `-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, sticky English, windowing
```
## Layout
```
main.go mic → ASR → slide splitting → SSE, recorder, browser launch
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
```
|