From c547e022535c86de6ca98f626abbb66c4bbd96b2 Mon Sep 17 00:00:00 2001 From: wuhei Date: Thu, 19 Nov 2026 20:27:10 +0100 Subject: =?UTF-8?q?intro=20page:=20red=20title=20slide=20with=20photo,=20?= =?UTF-8?q?=E6=89=B9=E6=96=97=E5=A4=A7=E4=BC=9A=20/=20STRUGGLE=20SESSION?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - served from the embedded web dir (http.FileServer over the go:embed FS), so the binary is still self-contained - nothing is published to the display until Enter: the broker gates every broadcast on a started flag, and /start also resets the recogniser so room noise during setup cannot leak into the first slide - /config hands the page the intro strings, whether translation is on, and whether the intro was already dismissed (so a reload mid-talk skips it) - -intro=false, -intro-zh, -intro-en, -intro-image to tune or skip it - web/intro.jpg is third-party press photography (secretchina.com), bundled for convenience, not covered by this project's licence --- main.go | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 94 insertions(+), 20 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index c8fd0bc..9f76d2b 100644 --- a/main.go +++ b/main.go @@ -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() -- cgit v1.2.3