summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'main.go')
-rw-r--r--main.go80
1 files changed, 70 insertions, 10 deletions
diff --git a/main.go b/main.go
index d58aaa6..d53674b 100644
--- a/main.go
+++ b/main.go
@@ -20,6 +20,7 @@ import (
"runtime"
"strings"
"sync"
+ "sync/atomic"
"syscall"
"time"
"unicode"
@@ -35,11 +36,13 @@ var indexHTML []byte
// Slide is one phrase to put on screen.
type Slide struct {
- Text string `json:"text"`
- EN string `json:"en,omitempty"`
- Seq int `json:"seq"`
- TS int64 `json:"ts"` // published at (unix ms)
- Cap int64 `json:"cap"` // audio chunk captured at (unix ms)
+ Text string `json:"text"`
+ EN string `json:"en,omitempty"`
+ Invert bool `json:"invert,omitempty"` // white on black for this sentence
+ Cut bool `json:"cut,omitempty"` // red "retake" screen
+ Seq int `json:"seq"`
+ TS int64 `json:"ts"` // published at (unix ms)
+ Cap int64 `json:"cap"` // audio chunk captured at (unix ms)
}
type broker struct {
@@ -53,6 +56,11 @@ type broker struct {
// survives the slides that happen while the next one is being produced, so the
// English line lags the speaker by roughly one model call instead of blinking.
curEN string
+
+ // invert alternates black-on-white / white-on-black per sentence.
+ invert bool
+ // cut is the red "take again" screen; the next phrase clears it.
+ cut bool
}
func newBroker() *broker { return &broker{subs: map[chan Slide]struct{}{}} }
@@ -68,7 +76,8 @@ func (b *broker) publish(text string, capTS int64) {
return
}
b.seq++
- s := Slide{Text: text, EN: b.curEN, Seq: b.seq, TS: time.Now().UnixMilli(), Cap: capTS}
+ b.cut = false
+ s := Slide{Text: text, EN: b.curEN, Invert: b.invert, Seq: b.seq, TS: time.Now().UnixMilli(), Cap: capTS}
b.cur = s
for ch := range b.subs {
select {
@@ -93,6 +102,7 @@ func (b *broker) publishEN(en string) {
b.seq++
s := b.cur
s.EN = en
+ s.Invert = b.invert
s.Seq = b.seq
b.cur = s
for ch := range b.subs {
@@ -103,6 +113,30 @@ func (b *broker) publishEN(en string) {
}
}
+// cutNow blanks the screen and turns it red: the speaker is redoing a sentence.
+func (b *broker) cutNow() {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ b.cut = true
+ b.curEN = ""
+ b.seq++
+ s := Slide{Invert: b.invert, Cut: true, Seq: b.seq, TS: time.Now().UnixMilli()}
+ b.cur = s
+ for ch := range b.subs {
+ select {
+ case ch <- s:
+ default:
+ }
+ }
+}
+
+// flip swaps the colour scheme; called at each sentence boundary.
+func (b *broker) flip() {
+ b.mu.Lock()
+ defer b.mu.Unlock()
+ b.invert = !b.invert
+}
+
func (b *broker) currentText() string {
b.mu.Lock()
defer b.mu.Unlock()
@@ -205,6 +239,12 @@ type presenter struct {
lastU string
}
+// reset drops the in-progress utterance so a retake starts from a clean slate.
+func (p *presenter) reset() {
+ p.emitted = 0
+ p.lastU = ""
+}
+
func (p *presenter) update(u string, capTS int64, endpoint bool) {
r := []rune(u)
if len(r) < p.emitted {
@@ -217,7 +257,14 @@ func (p *presenter) update(u string, capTS int64, endpoint bool) {
}
cut := -1
for i, c := range rest {
- if isBoundary(c) || i+1 >= p.max {
+ if isBoundary(c) {
+ cut = i + 1
+ break
+ }
+ // Break on length only if a full next phrase still fits; otherwise the
+ // last frame of a sentence is a one-character stub ("了") leaving the
+ // rest of what was said on the cutting-room floor.
+ if i+1 >= p.max && len(rest) >= p.max+p.preview {
cut = i + 1
break
}
@@ -244,6 +291,7 @@ func (p *presenter) update(u string, capTS int64, endpoint bool) {
if endpoint {
p.emitted = 0
p.lastU = ""
+ p.b.flip() // next sentence: white on black, and back again
}
}
@@ -270,7 +318,7 @@ func fileCmd(path string) *exec.Cmd {
return c
}
-func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineStream, pres *presenter) error {
+func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineStream, pres *presenter, cutReq *atomic.Bool) error {
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
@@ -305,6 +353,13 @@ func runASR(cmd *exec.Cmd, rec *sherpa.OnlineRecognizer, stream *sherpa.OnlineSt
if res := rec.GetResult(stream); res != nil {
text = res.Text
}
+ // Backspace was pressed: throw away the half-heard sentence and go red.
+ if cutReq.Swap(false) {
+ rec.Reset(stream)
+ pres.reset()
+ pres.b.cutNow()
+ continue
+ }
ep := rec.IsEndpoint(stream)
pres.update(text, capTS, ep)
if ep {
@@ -453,7 +508,7 @@ func main() {
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")
- trWindow = flag.Int("translate-window", 28, "characters of speech sent to the translator (bigger = smoother, slower)")
+ trWindow = flag.Int("translate-window", 40, "characters of speech sent to the translator (bigger = smoother, slower)")
llamaBin = flag.String("llama-server", "llama-server", "llama-server binary")
llamaArgs = flag.String("llama-args", "", "extra arguments for llama-server")
llamaThr = flag.Int("llama-threads", 0, "threads for the translation model (0 = half the cores)")
@@ -494,6 +549,7 @@ func main() {
b := newBroker()
pres := &presenter{max: *chars, preview: *preview, b: b}
+ cutReq := new(atomic.Bool)
// Optional English subtitles from a local model.
var llamaCmd *exec.Cmd
@@ -530,6 +586,10 @@ func main() {
w.Write(indexHTML)
})
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
+ w.WriteHeader(http.StatusNoContent)
+ })
srv := &http.Server{Addr: *addr, Handler: mux}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
@@ -597,7 +657,7 @@ func main() {
if *wav != "" {
in = fileCmd(*wav)
}
- if err := runASR(in, rec, stream, pres); err != nil {
+ if err := runASR(in, rec, stream, pres, cutReq); err != nil {
log.Printf("input ended: %v", err)
}
stop()