Skip to content

Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.

fix(runtime): close online 2pass sentence state

dio724a08585523
diff --git a/runtime/websocket/bin/websocket-server-2pass.cpp b/runtime/websocket/bin/websocket-server-2pass.cpp
index 45c016c19..ddfe91ce0 100644
--- a/runtime/websocket/bin/websocket-server-2pass.cpp
+++ b/runtime/websocket/bin/websocket-server-2pass.cpp
@@ -65,7 +65,7 @@ context_ptr WebSocketServer::on_tls_init(tls_mode mode,
return ctx;
}
-nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl& hdl, std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,std::owner_less<websocketpp::connection_hdl>>& data_map) {
+nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl& hdl, std::map<websocketpp::connection_hdl, std::shared_ptr<FUNASR_MESSAGE>,std::owner_less<websocketpp::connection_hdl>>& data_map, const std::string& modetype, bool is_final) {
std::shared_ptr<FUNASR_MESSAGE> data_msg = nullptr;
auto it = data_map.find(hdl);
if (it != data_map.end()) {
@@ -76,8 +76,9 @@ nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl&
nlohmann::json jsonresult;
jsonresult["text"] = "";
- std::string tmp_online_msg = FunASRGetResult(result, 0);
- if (tmp_online_msg != "") {
+ const char* online_result = FunASRGetResult(result, 0);
+ if (online_result != nullptr && online_result[0] != '\0') {
+ std::string tmp_online_msg = online_result;
LOG(INFO) << "wav_name: " << data_msg->msg["wav_name"].get<std::string>() << " | online_res :" << tmp_online_msg;
jsonresult["text"] = tmp_online_msg;
jsonresult["mode"] = "2pass-online";
@@ -95,18 +96,26 @@ nlohmann::json handle_result(FUNASR_RESULT result, websocketpp::connection_hdl&
data_msg->end_time = FunASRGetTpassEnd(result); // 记录句子的结束时间
jsonresult["timestamp"] = data_msg->timestamp;
- std::string tmp_tpass_msg = FunASRGetTpassResult(result, 0);
- if (tmp_tpass_msg != "") {
- LOG(INFO) << "wav_name: " << data_msg->msg["wav_name"].get<std::string>() << " | offline results : " << tmp_tpass_msg;
- jsonresult["text"] = tmp_tpass_msg;
- jsonresult["mode"] = "2pass-offline";
-
- // 句子结束,记录结束时间
- jsonresult["start_time"] = data_msg->start_time;
- jsonresult["end_time"] = data_msg->end_time;
- jsonresult["slice_type"] = 2;
- jsonresult["index"] = data_msg->index;
-
+ if (modetype != "online") {
+ const char* tpass_result = FunASRGetTpassResult(result, 0);
+ if (tpass_result != nullptr && tpass_result[0] != '\0') {
+ std::string tmp_tpass_msg = tpass_result;
+ LOG(INFO) << "wav_name: " << data_msg->msg["wav_name"].get<std::string>() << " | offline results : " << tmp_tpass_msg;
+ jsonresult["text"] = tmp_tpass_msg;
+ jsonresult["mode"] = "2pass-offline";
+
+ // 句子结束,记录结束时间
+ jsonresult["start_time"] = data_msg->start_time;
+ jsonresult["end_time"] = data_msg->end_time;
+ jsonresult["slice_type"] = 2;
+ jsonresult["index"] = data_msg->index;
+
+ data_msg->index++; //句子序号
+ data_msg->is_sentence_started = false; // 重置句子状态
+ }
+ } else if (is_final && data_msg->is_sentence_started) {
+ // online mode must not send 2pass-offline text, but final chunks still close
+ // the sentence state so the next utterance starts with slice_type=0.
data_msg->index++; //句子序号
data_msg->is_sentence_started = false; // 重置句子状态
}
@@ -195,7 +204,7 @@ void WebSocketServer::do_decoder(
}
if (Result) {
websocketpp::lib::error_code ec;
- nlohmann::json jsonresult = handle_result(Result, hdl, data_map);
+ nlohmann::json jsonresult = handle_result(Result, hdl, data_map, modetype, false);
jsonresult["wav_name"] = wav_name;
jsonresult["is_final"] = false;
if (jsonresult["text"] != "") {
@@ -238,7 +247,7 @@ void WebSocketServer::do_decoder(
}
if (Result) {
websocketpp::lib::error_code ec;
- nlohmann::json jsonresult = handle_result(Result, hdl, data_map);
+ nlohmann::json jsonresult = handle_result(Result, hdl, data_map, modetype, true);
jsonresult["wav_name"] = wav_name;
jsonresult["is_final"] = true;
//LOG(INFO) << "jsonresult: " << jsonresult.dump(4);