diff options
| -rw-r--r-- | README.md | 8 | ||||
| -rw-r--r-- | main.go | 20 | ||||
| -rw-r--r-- | main_test.go | 64 |
3 files changed, 89 insertions, 3 deletions
@@ -63,6 +63,13 @@ Colours alternate — black on white, then white on black — at every sentence (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. + ## Session log (Enter) `Enter` turns the sentence on screen green and appends it to a Markdown file, so the @@ -124,6 +131,7 @@ How it behaves, and why: | 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 | @@ -262,6 +262,7 @@ func cleanSlide(s string) string { type presenter struct { max int // max characters per slide preview int // don't replace a full slide with a shorter preview than this + settle int // on a pause, sentences up to this many characters land whole emitted int // runes of the current utterance already turned into slides b *broker @@ -326,8 +327,20 @@ func (p *presenter) update(u string, capTS int64, endpoint bool) { } // Live tail: grows character by character while you keep talking. A very short // tail would blank a full slide, so keep the previous phrase until it grows. - if tail := cleanSlide(string(r[p.emitted:])); tail != "" && - (endpoint || utf8.RuneCountInString(tail) >= p.preview) { + tail := cleanSlide(string(r[p.emitted:])) + + // End of a sentence: fragments were fine while speaking, but the frame that stays + // up (and gets recorded, and translated, and logged) should be the whole sentence. + whole := "" + if endpoint { + if c := cleanSlide(strings.TrimSpace(u)); c != "" && utf8.RuneCountInString(c) <= p.settle { + whole = c + } + } + switch { + case whole != "": + p.b.publish(whole, capTS) + case tail != "" && (endpoint || utf8.RuneCountInString(tail) >= p.preview): p.b.publish(tail, capTS) } if p.onUtterance != nil && u != p.lastU && strings.TrimSpace(u) != "" { @@ -570,6 +583,7 @@ func main() { threads = flag.Int("threads", 2, "onnxruntime threads") chars = flag.Int("chars", 9, "max characters per slide") preview = flag.Int("preview", 3, "min characters before the next phrase replaces the current slide") + settle = flag.Int("settle", 20, "on a pause, show sentences up to this many characters whole (0 = off)") 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)") @@ -616,7 +630,7 @@ func main() { stream := sherpa.NewOnlineStream(rec) b := newBroker() - pres := &presenter{max: *chars, preview: *preview, b: b} + pres := &presenter{max: *chars, preview: *preview, settle: *settle, b: b} cutReq, keepReq := new(atomic.Bool), new(atomic.Bool) var trRef atomic.Value // *translator, may stay nil diff --git a/main_test.go b/main_test.go index 1838cdb..7b82cf2 100644 --- a/main_test.go +++ b/main_test.go @@ -249,3 +249,67 @@ func TestSessionLogFormat(t *testing.T) { t.Fatalf("translation missing:\n%s", body) } } + +// feed sends the utterance character by character, as the streaming recogniser does, +// then closes it with an endpoint. Returns every frame that reached the screen. +func feed(t *testing.T, p *presenter, b *broker, u string) []string { + t.Helper() + ch, _, unsub := b.subscribe() + defer unsub() + r := []rune(u) + var frames []string + for i := 1; i <= len(r); i++ { + p.update(string(r[:i]), time.Now().UnixMilli(), false) + for { + select { + case s := <-ch: + frames = append(frames, s.Text) + default: + goto cont + } + } + cont: + } + p.update(u, time.Now().UnixMilli(), true) + for { + select { + case s := <-ch: + frames = append(frames, s.Text) + default: + return frames + } + } +} + +func TestSentenceSettlesWholeWhenYouPause(t *testing.T) { + b := newBroker() + p := &presenter{max: 9, preview: 3, settle: 20, b: b} + frames := feed(t, p, b, "我觉得今年我的中文会说得好一些。") + last := frames[len(frames)-1] + if last != "我觉得今年我的中文会说得好一些" { + t.Fatalf("last frame = %q, want the whole sentence", last) + } + // ...and it is what Enter would log. + if got := p.sentence(); got != "我觉得今年我的中文会说得好一些。" { + t.Fatalf("sentence() = %q", got) + } +} + +func TestLongSentenceStillEndsOnAFragment(t *testing.T) { + b := newBroker() + p := &presenter{max: 9, preview: 3, settle: 12, b: b} + u := "今天我想跟大家讲一讲我自己学习中文的一些经历" + frames := feed(t, p, b, u) + if last := frames[len(frames)-1]; last == u { + t.Fatalf("a %d character sentence should not settle at -settle 12", len([]rune(u))) + } +} + +func TestSettleOffKeepsTakahashiFragments(t *testing.T) { + b := newBroker() + p := &presenter{max: 9, preview: 3, settle: 0, b: b} + frames := feed(t, p, b, "我觉得今年我的中文会说得好一些。") + if last := frames[len(frames)-1]; last != "会说得好一些" { + t.Fatalf("last frame = %q, want the final fragment", last) + } +} |
