perf(voice): 砍兜底等待 1.2s→0.5s + 语音提示词强制简短 + 时延测量

- voice.go: ClientEnd 兜底等待 1200ms→500ms(白吃的时延),首字出声 6.6s→5.3s
- voice_task.go: JARVIS 系统提示词改"必须简短/最多三句/先给结论"(语音场景长答案既拖慢
  首字又难听;注:deepseek-v4-pro 仍可能不理会长度指令,可靠收短需 max_tokens 硬顶)
- voicesim: 时延拆解(提交/首字出声/朗读完毕,以"说完"为0点)

现状:首字出声 ~5.3s,大头是 deepseek 生成第一句(大模型 TTFT);管线开销(兜底+TTS握手)已压到~1s。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-22 11:17:28 +08:00
parent 9ae06229b8
commit 6b31856551
6 changed files with 31 additions and 7 deletions
+23 -2
View File
@@ -69,7 +69,8 @@ func main() {
answer := make([]byte, 0, 1<<20)
done := make(chan struct{})
go func() { // 读循环:文本帧=事件,二进制帧=回答 TTS 音频
var endAt, tTask, tFirstAudio, tDone time.Time // 时延测量:以"说完(ClientEnd)"为起点
go func() { // 读循环:文本帧=事件,二进制帧=回答 TTS 音频
defer close(done)
for {
mt, data, err := conn.ReadMessage()
@@ -77,6 +78,9 @@ func main() {
return
}
if mt == websocket.BinaryMessage {
if tFirstAudio.IsZero() {
tFirstAudio = time.Now() // 首个音频帧=听到第一声
}
answer = append(answer, data...)
continue
}
@@ -94,10 +98,12 @@ func main() {
}
fmt.Printf(" ← 转写[%s]%q\n", tag, m.Text)
case voice.ServerTask:
tTask = time.Now()
fmt.Printf(" ← 任务已提交 task_id=%sAgent 正在思考…)\n", m.TaskID)
case voice.ServerSpeaking:
fmt.Println(" ← Agent 开始朗读回答…")
case voice.ServerTTSEnd:
tDone = time.Now()
fmt.Println(" ← 回答朗读完毕")
return
case voice.ServerError:
@@ -119,6 +125,7 @@ func main() {
time.Sleep(90 * time.Millisecond)
}
send(conn, voice.ClientMsg{Type: voice.ClientEnd})
endAt = time.Now() // 时延起点:用户"说完"这一刻
fmt.Println("④ 已说完,等 Agent 回答 + 朗读(大模型 + TTS,稍候)…")
// 5) 等回答朗读完(或超时)。
@@ -129,9 +136,23 @@ func main() {
}
if len(answer) > 0 {
dur := float64(len(answer)/2) / float64(voice.TTSSampleRate)
_ = writeWAV("sim_answer.wav", answer, voice.TTSSampleRate)
fmt.Printf("\n🔊 回答音频 %d 字节(%.1f 秒)→ 存 sim_answer.wav\n", len(answer), float64(len(answer)/2)/float64(voice.TTSSampleRate))
fmt.Printf("\n🔊 回答音频 %d 字节(时长 %.1f 秒)→ 存 sim_answer.wav\n", len(answer), dur)
fmt.Println(" afplay sim_answer.wav # 听 JARVIS 的语音回答")
// 时延拆解(以"说完"为 0 点)。首字延迟 = 听到第一声的时间,是体感关键。
fmt.Println("\n⏱️ 时延拆解(从「说完」起算):")
if !tTask.IsZero() {
fmt.Printf(" · 提交任务 %.2fs(含 ClientEnd 后 0.5s 兜底等待)\n", tTask.Sub(endAt).Seconds())
}
if !tFirstAudio.IsZero() {
fmt.Printf(" · 👂 首字出声 %.2fs ← 体感响应速度就看这个\n", tFirstAudio.Sub(endAt).Seconds())
}
if !tDone.IsZero() {
fmt.Printf(" · 朗读完毕 %.2fs= 首字 %.2fs + 念完 %.1fs 那段话)\n",
tDone.Sub(endAt).Seconds(), tFirstAudio.Sub(endAt).Seconds(), dur)
}
} else {
fmt.Println("\n⚠️ 没收到回答音频(看上面事件流定位:转写?任务?朗读?)")
}