From b37b9eb77987c46f6238026b7f887e8fe772a428 Mon Sep 17 00:00:00 2001 From: Wang Zichong Date: Mon, 12 Oct 2026 16:10:59 +0800 Subject: [PATCH] fix(asr): do not call InputFinished for a never-ending stream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A live caption stream never ends: the boundary of an utterance is silence, not end-of-input, so InputFinished (which means "no more audio ever") does not apply. Drop it and rely on the trailing audio alone; keep a short silence padding as a safety net for end-of-file cases. Verified that the file path still emits the final character without InputFinished. 实时字幕的音频流永不结束,一句话的边界是"静音"而不是"输入结束",所以 不该调用语义为"再也不会有音频"的 InputFinished。改为只补一小段静音兜底, 实测文件模式的结果依然完整。文档也一并更正。 --- docs/asr-backends.md | 6 +++++- src/core/sherpaasr.cpp | 7 ++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/asr-backends.md b/docs/asr-backends.md index 670a28b..0ee2b69 100644 --- a/docs/asr-backends.md +++ b/docs/asr-backends.md @@ -67,7 +67,11 @@ IAsr(src/core/iasr.h) 1. **sherpa-onnx 的预编译库是旧 C++ ABI**(`_GLIBCXX_USE_CXX11_ABI=0`),和 Qt 的新 ABI 冲突, C++ API 链接不上。**必须用它的 C API**(`sherpa-onnx/c-api/c-api.h`,`extern "C"`,无名字改写)。 -2. **结束时要补一小段静音**(`streamFinish()` 里补 0.3s)再 `InputFinished`,否则最后一个字会被截掉。 +2. **末尾要补一小段静音**(`streamFinish()` 里补 0.3s),否则最后一个字会被截掉。 + 但**不需要 `InputFinished`**:实时字幕的音频流永不结束,一句话的边界是「静音」而不是「输入结束」, + `InputFinished` 的语义只对文件成立。实测只补静音(不调 `InputFinished`)结果就是完整的。 + 真实场景里那段尾部静音本来就会被喂进去(VAD 判定静音要 0.8s,这 0.8s 已经送给模型了), + 所以补静音只是个兜底。 ## 自动语言识别 diff --git a/src/core/sherpaasr.cpp b/src/core/sherpaasr.cpp index b65c69e..3185193 100644 --- a/src/core/sherpaasr.cpp +++ b/src/core/sherpaasr.cpp @@ -92,9 +92,10 @@ std::string SherpaAsr::streamFinish() const std::vector silence(pad, 0.0f); SherpaOnnxOnlineStreamAcceptWaveform(d->stream, 16000, silence.data(), (int32_t) pad); d->drain(); - // 通知不再有输入,把尾部一并解出来 - SherpaOnnxOnlineStreamInputFinished(d->stream); - d->drain(); + // 注意:这里**不调用 InputFinished**。 + // 实时字幕的音频流是永不结束的,一句话的边界是"静音"而不是"输入结束"; + // InputFinished 的语义(再也不会有音频了)对文件才成立,对我们不适用。 + // 只需要补一小段静音,让模型把最后一个字吐出来即可。 return d->resultText(); }