diff options
| -rw-r--r-- | README.md | 21 | ||||
| -rw-r--r-- | main.go | 114 | ||||
| -rw-r--r-- | web/index.html | 84 |
3 files changed, 198 insertions, 21 deletions
@@ -50,7 +50,7 @@ one short thought per screen, replaced on the next one. | key | what it does | |---|---| -| `Enter` | **keep** — the sentence turns green and is appended to the session log | +| `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 | @@ -70,6 +70,24 @@ last fragment. So *我觉得今年我的中文会说得好一些* no longer ends `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 @@ -138,6 +156,7 @@ How it behaves, and why: | `-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 | @@ -5,12 +5,13 @@ package main import ( - _ "embed" + "embed" "encoding/binary" "encoding/json" "flag" "fmt" "io" + "io/fs" "log" "net/http" "os" @@ -29,8 +30,8 @@ import ( sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx" ) -//go:embed web/index.html -var indexHTML []byte +//go:embed web +var webAssets embed.FS // ---------------------------------------------------------------- broker (SSE fan-out) @@ -41,6 +42,7 @@ type Slide struct { Invert bool `json:"invert,omitempty"` // white on black for this sentence Cut bool `json:"cut,omitempty"` // red "retake" screen Keep bool `json:"keep,omitempty"` // green: this sentence was kept + Start bool `json:"start,omitempty"` // intro dismissed, the talk has begun Seq int `json:"seq"` TS int64 `json:"ts"` // published at (unix ms) Cap int64 `json:"cap"` // audio chunk captured at (unix ms) @@ -64,6 +66,8 @@ type broker struct { cut bool // keep is the green "this sentence was saved" state, cleared the same way. keep bool + // started is false while the intro page is up: nothing is published until then. + started bool } func newBroker() *broker { return &broker{subs: map[chan Slide]struct{}{}} } @@ -73,6 +77,10 @@ func (b *broker) publish(text string, capTS int64) { return } b.mu.Lock() + if !b.started { + b.mu.Unlock() + return + } if text == b.cur.Text { // Same phrase, but a translation may now be stale-free: keep as is. b.mu.Unlock() @@ -81,7 +89,7 @@ func (b *broker) publish(text string, capTS int64) { b.seq++ b.cut = false b.keep = false - s := Slide{Text: text, EN: b.curEN, Invert: b.invert, Seq: b.seq, TS: time.Now().UnixMilli(), Cap: capTS} + s := Slide{Text: text, EN: b.curEN, Invert: b.invert, Start: true, Seq: b.seq, TS: time.Now().UnixMilli(), Cap: capTS} b.cur = s for ch := range b.subs { select { @@ -99,7 +107,7 @@ func (b *broker) publishEN(en string) { } b.mu.Lock() defer b.mu.Unlock() - if b.curEN == en { + if !b.started || b.curEN == en { return } b.curEN = en @@ -122,6 +130,9 @@ func (b *broker) publishEN(en string) { func (b *broker) cutNow() { b.mu.Lock() defer b.mu.Unlock() + if !b.started { + return + } b.cut = true b.curEN = "" b.seq++ @@ -135,11 +146,39 @@ func (b *broker) cutNow() { } } +// start dismisses the intro page; nothing was published before this. +func (b *broker) start() { + b.mu.Lock() + if b.started { + b.mu.Unlock() + return + } + b.started = true + b.seq++ + s := b.cur + s.Start = true + s.Seq = b.seq + b.cur = s + for ch := range b.subs { + select { + case ch <- s: + default: + } + } + b.mu.Unlock() +} + +func (b *broker) isStarted() bool { + b.mu.Lock() + defer b.mu.Unlock() + return b.started +} + // keepNow turns the sentence on screen green: it has been written to the log. func (b *broker) keepNow() { b.mu.Lock() defer b.mu.Unlock() - if b.cur.Text == "" || b.keep { + if !b.started || b.cur.Text == "" || b.keep { return } b.keep = true @@ -356,6 +395,12 @@ func (p *presenter) update(u string, capTS int64, endpoint bool) { } } +// controls carries the keypress requests from the display to the audio loop, which +// owns the recogniser (so they never race with decoding). +type controls struct { + cut, keep, start atomic.Bool +} + // ---------------------------------------------------------------- audio in func micCmd(source string) *exec.Cmd { @@ -379,7 +424,7 @@ func fileCmd(path string) *exec.Cmd { return c } -func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineStream, pres *presenter, cutReq, keepReq *atomic.Bool, keepQ chan<- string) error { +func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineStream, pres *presenter, ctrl *controls, keepQ chan<- string) error { stdout, err := cmd.StdoutPipe() if err != nil { return err @@ -415,8 +460,17 @@ func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineSt if res := rec.GetResult(stream); res != nil { text = res.Text } + // Enter on the intro page: begin. Whatever was heard while the intro was up is + // thrown away, so the talk starts from a clean slate. + if ctrl.start.Swap(false) { + rec.Reset(stream) + pres.reset() + pres.last = "" + pres.b.start() + } + // Backspace was pressed: throw away the half-heard sentence and go red. - if cutReq.Swap(false) { + if ctrl.cut.Swap(false) { rec.Reset(stream) pres.reset() pres.b.cutNow() @@ -431,7 +485,7 @@ func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineSt // Enter was pressed. Wait out the model's 320ms lookahead first, otherwise // the last syllables of the sentence are not decoded yet. - if keepReq.Swap(false) { + if ctrl.keep.Swap(false) { keepAt = time.Now().Add(450 * time.Millisecond) } if !keepAt.IsZero() && !time.Now().Before(keepAt) { @@ -587,6 +641,11 @@ func main() { debug = flag.Bool("debug", false, "show latency/char-count overlay") logFile = flag.String("log", "sessions/", "session log written by Enter (a file, or a directory for a timestamped file)") + intro = flag.Bool("intro", true, "show the intro page and wait for Enter before displaying anything") + introZH = flag.String("intro-zh", "批斗大会", "intro headline") + introEN = flag.String("intro-en", "STRUGGLE SESSION", "intro subtitle") + introImage = flag.String("intro-image", "/intro.jpg", "intro picture (replace web/intro.jpg to change it)") + trModel = flag.String("translate-model", "", "spawn llama-server with this .gguf and translate every slide into English") trURL = flag.String("translate", "", "use an already running OpenAI-compatible endpoint for English (e.g. http://127.0.0.1:8080)") trPrompt = flag.String("prompt", defaultPrompt, "translation prompt; {text} is substituted") @@ -630,8 +689,9 @@ func main() { stream := sherpa.NewOnlineStream(rec) b := newBroker() + b.started = !*intro pres := &presenter{max: *chars, preview: *preview, settle: *settle, b: b} - cutReq, keepReq := new(atomic.Bool), new(atomic.Bool) + ctrl := new(controls) var trRef atomic.Value // *translator, may stay nil @@ -689,22 +749,36 @@ func main() { go tr.run() } + webFS, err := fs.Sub(webAssets, "web") + if err != nil { + log.Fatal(err) + } + mux := http.NewServeMux() - mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - http.NotFound(w, r) - return - } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - w.Write(indexHTML) + mux.Handle("/", http.FileServer(http.FS(webFS))) + mux.HandleFunc("/config", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(map[string]any{ + "intro": *intro, + "introZH": *introZH, + "introEN": *introEN, + "introImg": *introImage, + "translate": tr != nil, + "debug": *debug, + "started": b.isStarted(), + }) + }) + mux.HandleFunc("/start", func(w http.ResponseWriter, r *http.Request) { + ctrl.start.Store(true) + w.WriteHeader(http.StatusNoContent) }) mux.HandleFunc("/events", b.serveSSE) mux.HandleFunc("/cut", func(w http.ResponseWriter, r *http.Request) { - cutReq.Store(true) // the audio loop applies it within one 20ms chunk + ctrl.cut.Store(true) // the audio loop applies it within one 20ms chunk w.WriteHeader(http.StatusNoContent) }) mux.HandleFunc("/keep", func(w http.ResponseWriter, r *http.Request) { - keepReq.Store(true) + ctrl.keep.Store(true) w.WriteHeader(http.StatusNoContent) }) srv := &http.Server{Addr: *addr, Handler: mux} @@ -778,7 +852,7 @@ func main() { if *wav != "" { in = fileCmd(*wav) } - if err := runASR(in, rec, stream, pres, cutReq, keepReq, keepQ); err != nil { + if err := runASR(in, rec, stream, pres, ctrl, keepQ); err != nil { log.Printf("input ended: %v", err) } stop() diff --git a/web/index.html b/web/index.html index 404b367..e02159c 100644 --- a/web/index.html +++ b/web/index.html @@ -54,6 +54,47 @@ to { opacity: 1; transform: scale(1); } } .pop { animation: pop 90ms ease-out; } + /* intro page: red, the photo, and the title */ + #intro { + position: fixed; inset: 0; z-index: 10; + background: #c8102e; color: #fff; + display: flex; flex-direction: column; + align-items: center; justify-content: center; + gap: 3.2vh; padding: 6vh 6vw; box-sizing: border-box; + text-align: center; + } + #intro[hidden] { display: none; } + #intro img { + max-width: min(74vw, 1150px); + max-height: 44vh; + box-shadow: 0 0 0 5px rgba(0,0,0,.28), 0 18px 50px rgba(0,0,0,.35); + filter: grayscale(1) contrast(1.06); + } + #intro h1 { + margin: 0; + font-size: clamp(44px, 11vh, 190px); + font-weight: 900; + line-height: 1; + letter-spacing: .05em; + text-indent: .05em; + } + #intro h2 { + margin: 0; + font-family: "Noto Sans", "DejaVu Sans", sans-serif; + font-size: clamp(16px, 3.4vh, 56px); + font-weight: 800; + letter-spacing: .34em; + text-indent: .34em; + } + #intro .hint { + position: absolute; bottom: 4.5vh; + font: 600 clamp(12px, 1.7vh, 22px)/1 "Noto Sans", "DejaVu Sans", sans-serif; + letter-spacing: .1em; opacity: .9; + } + #intro .hint .key { + display: inline-block; padding: .12em .45em; margin-right: .5em; + border: 2px solid currentColor; border-radius: .28em; font-weight: 700; + } #d { position: fixed; right: 12px; bottom: 10px; display: none; font: 12px/1.4 ui-monospace, monospace; color: #bbb; letter-spacing: 0; @@ -61,6 +102,12 @@ </style> </head> <body> +<div id="intro"> + <img id="intro-img" src="/intro.jpg" alt=""> + <h1 id="intro-zh">批斗大会</h1> + <h2 id="intro-en">STRUGGLE SESSION</h2> + <div class="hint"><span class="key">⏎</span> Press enter to start</div> +</div> <div id="wrap"><div id="stack"><div id="zh"></div><div id="en"></div></div></div> <div id="d"></div> <script> @@ -162,6 +209,13 @@ addEventListener('resize', function () { measurePad(); fit(); }); addEventListener('keydown', function (e) { + if (introVisible) { + if (e.key === 'Enter' || e.key === ' ') { + e.preventDefault(); + startTalk(); + } + return; + } if (e.key === 'Enter') { e.preventDefault(); keep(); @@ -181,6 +235,29 @@ measurePad(); + const introEl = document.getElementById('intro'); + let introVisible = !introEl.hidden; + + function showIntro(cfg) { + document.getElementById('intro-zh').textContent = cfg.introZH || ''; + document.getElementById('intro-en').textContent = cfg.introEN || ''; + const img = document.getElementById('intro-img'); + if (cfg.introImg) img.src = cfg.introImg; else img.remove(); + introEl.hidden = false; + introVisible = true; + } + + function hideIntro() { + introEl.hidden = true; + introVisible = false; + } + + function startTalk() { + hideIntro(); + fetch('/start', { method: 'POST', keepalive: true }).catch(function () {}); + } + + if (q.has('text') || q.has('cut')) hideIntro(); if (q.has('invert')) body.classList.add('invert'); if (q.has('keep')) body.classList.add('keep'); if (q.has('cut')) { body.classList.add('cut'); return; } @@ -190,6 +267,12 @@ return; } + fetch('/config').then(function (r) { return r.json(); }).then(function (cfg) { + if (cfg.translate) body.classList.add('en'); + if (cfg.debug) d.style.display = 'block'; + if (cfg.intro && !cfg.started) showIntro(cfg); else hideIntro(); + }).catch(function () {}); + const es = new EventSource('/events'); es.onmessage = function (e) { try { @@ -197,6 +280,7 @@ body.classList.toggle('invert', !!s.invert); body.classList.toggle('cut', !!s.cut); body.classList.toggle('keep', !!s.keep); + if (s.start && introVisible) hideIntro(); if (s.cut) { curZh = ''; curEn = ''; zh.textContent = ''; en.textContent = ''; return; } if (s.keep && s.text === curZh) return; // only the colour changed render(s.text, s.en, s.cap); |
