From 9cf8c1ff20b10997ea91ebb0657319844ada8121 Mon Sep 17 00:00:00 2001 From: luozheng <1732036298@qq.com> Date: Thu, 14 Aug 2025 17:32:39 +0800 Subject: [PATCH] =?utf8?q?4GAi=20tts=E6=96=87=E6=9C=AC=E5=8F=91=E9=80=81?= =?utf8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../nativeui/aivoice/src/AiVoiceModel.cpp | 162 ++++-------------- .../nativeui/aivoice/src/AiVoicePresenter.cpp | 6 +- 2 files changed, 39 insertions(+), 129 deletions(-) diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp index 792781ad2..e45606de2 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoiceModel.cpp @@ -683,105 +683,6 @@ namespace OHOS str.swap(result); } -#if 0 - void AiVoiceModel::ProcessTextByPunctuation() - { - const std::string punctuation = ",。!?;:))”、"; // 要替换的中文标点 - const char replacement = ','; // 替换为英文逗号 - const size_t MAX_SEGMENT_LENGTH = 120; // 最大分段长度 - - if (accumulatedText.empty()) - return; - - // 安全替换中文标点为英文逗号 - std::string processedText = accumulatedText; - safe_replace_utf8(processedText, punctuation, replacement); - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "processedText:%s", processedText.c_str()); - - accumulatedText = processedText; - size_t commaPos = accumulatedText.find_last_of(','); - - // 处理长度限制,若超过最大长度则强制分段 - if (accumulatedText.length() >= MAX_SEGMENT_LENGTH) - { - commaPos = MAX_SEGMENT_LENGTH - 1; - // 查找最大长度内最近的逗号 - size_t lastCommaInRange = accumulatedText.find_last_of(',', commaPos); - if (lastCommaInRange != std::string::npos) - { - commaPos = lastCommaInRange; - } - } - - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "commaPos:%d", commaPos); - - if (commaPos != std::string::npos) - { - // 提取分段(包含英文逗号) - std::string segment = accumulatedText.substr(0, commaPos + 1); - - // 清理文本 - segment.erase( - std::remove(segment.begin(), segment.end(), '\n'), - segment.end()); - segment.erase( - std::remove(segment.begin(), segment.end(), '\r'), - segment.end()); - - // 发送到TTS播放 - if (!segment.empty()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "播放分段文本: %s (长度: %zu)", - segment.c_str(), segment.length()); - uapi_start_tts_single_txt((char *)segment.c_str()); - LOS_Msleep(50); - } - - // 更新累积文本 - if (commaPos + 1 < accumulatedText.length()) - { - accumulatedText = accumulatedText.substr(commaPos + 1); - } - else - { - accumulatedText.clear(); - } - } - else - { - // 无逗号时继续累积或强制分段 - if (accumulatedText.length() > MAX_SEGMENT_LENGTH) - { - // 强制截断(确保在字符边界) - std::string segment = accumulatedText.substr(0, MAX_SEGMENT_LENGTH); - // 清理文本 - segment.erase( - std::remove(segment.begin(), segment.end(), '\n'), - segment.end()); - segment.erase( - std::remove(segment.begin(), segment.end(), '\r'), - segment.end()); - // 发送分段 - if (!segment.empty()) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "强制分段: %s (长度: %zu)", - segment.c_str(), segment.length()); - uapi_start_tts_single_txt((char *)segment.c_str()); - LOS_Msleep(50); - } - // 更新累积文本 - accumulatedText = accumulatedText.substr(MAX_SEGMENT_LENGTH); - } - else - { - // 无逗号且长度不足时继续累积 - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "累积文本(未触发): %s", accumulatedText.c_str()); - } - } - } -#endif - void AiVoiceModel::ProcessTextByPunctuation() { const std::string punctuation = ",。!?;:))”、"; // 要替换的中文标点 @@ -1029,7 +930,7 @@ namespace OHOS void AiVoiceModel::StopListenAndRecordingUserQuestion(void) { WEARABLE_LOGD(WEARABLE_LOG_MODULE_APP, "停止录音"); - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "停止录音:%d",GetSelect4gorbt()); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "停止录音:%d", GetSelect4gorbt()); if (GetSelect4gorbt() == AI_SELECT_4G) // 选的是4G需要关闭asr { // uapi_stop_asr(); @@ -1626,12 +1527,12 @@ namespace OHOS static uint8_t tts_record_flag = 0; // 添加一个静态变量用于跟踪连续出现0的次数 static int s_tts_tick_zero_count = 0; - + static int continue_zero_count = 0; // 静态变量:记录连续为0的次数(初始化为0) static void AI4G_IS_START_TTS_CallbackProc(int data) { WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "AI4G_IS_START_TTS_CallbackProc:%s", AiVoiceModel::GetInstance().GetAccumulatedText().c_str()); - AiVoiceModel::GetInstance().GetInit_4gai_task();//获取4gai服务状态 - if (get_ai_tts_server_init_flag() == 0) //tts服务如果断开了,就停止定时器 + AiVoiceModel::GetInstance().GetInit_4gai_task(); // 获取4gai服务状态 + if (get_ai_tts_server_init_flag() == 0) // tts服务如果断开了,就停止定时器 { s_tts_tick_zero_count++; // 增加连续0的次数 WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "s_tts_tick_zero_count count: %d", s_tts_tick_zero_count); @@ -1642,30 +1543,48 @@ namespace OHOS return; } } - // 用来判断是否需要停止发送文本 + + // 无文本时停止定时器 if (AiVoiceModel::GetInstance().GetAccumulatedText().empty()) { AiVoiceModel::GetInstance().stop_ai4g_tts_play_timer(); return; } - if (get_bk_websocket_tts_tick() == 0) // 代表可以发录音文本 + + // 当前回调中get_bk_websocket_tts_tick()是否为0 + bool current_tick_zero = (get_bk_websocket_tts_tick() == 0); + + // 更新连续为0的次数 + if (current_tick_zero) { - if (!get_ai_osal_msg_queue_is_full()) - { - AiVoiceModel::GetInstance().ProcessTextByPunctuation(); - } + continue_zero_count++; // 本次为0,计数+1 } else { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get_bk_websocket_tts_tick:%d", get_bk_websocket_tts_tick()); + continue_zero_count = 0; // 本次不为0,计数重置为0 + } + + // 核心逻辑:连续0中,奇数次(1、3、5...)发送,偶数次(2、4、6...)跳过 + if (current_tick_zero) + { + if (continue_zero_count % 2 == 1) // 奇数次(第1、3、5...次):发送 + { + if (!get_ai_osal_msg_queue_is_full()) + { + AiVoiceModel::GetInstance().ProcessTextByPunctuation(); + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "发送TTS文本(连续第%d次为0,奇数次)", continue_zero_count); + } + } + else // 偶数次(第2、4、6...次):跳过 + { + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "连续第%d次为0,偶数次,本次不发送", continue_zero_count); + } } -#if TTS_TEST - if (!tts_record_flag) + else { - tts_record_flag = 1; - startAudioCaptureTask(); // 测试汇声抑制的 + WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "get_bk_websocket_tts_tick:%d(不满足发送条件)", get_bk_websocket_tts_tick()); } -#endif + return; } static void AI4G_IS_START_TTS_Callback(int data) @@ -1725,17 +1644,8 @@ namespace OHOS } g_ai4g_is_start_tts_play_Handle = nullptr; AI4G_IS_START_TTS_PLAY_CNT = NUMBER_TEN; - s_tts_tick_zero_count = 0; // 重置计数器 -#if TTS_TEST - tts_record_flag = 0; - std::string filePath = "/user/aiRecord456.mp3"; - // 删除filePath文件 - if (remove(filePath.c_str()) != 0) - { - WEARABLE_LOGE(WEARABLE_LOG_MODULE_APP, "remove failed, path: %s", filePath.c_str()); - } - StopListenAndRecordingUserQuestion(); -#endif + s_tts_tick_zero_count = 0; // 重置计数器 + continue_zero_count = 0; // 重置静态变量 } } diff --git a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp index 596253b83..d75f6d6f0 100644 --- a/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp +++ b/code/sdk/application/wearable/fd230app/nativeui/aivoice/src/AiVoicePresenter.cpp @@ -333,8 +333,8 @@ namespace OHOS case AIVOICE_LISTENING_VIEWID: model->StopListenAndRecordingUserQuestion(); model->stop_listening_timer(); - LOS_Msleep(100); - set_stop_send_ai_flag(1); // 停止发送AI数据 + // LOS_Msleep(100); + // set_stop_send_ai_flag(1); // 停止发送AI数据 break; case AIVOICE_THINKING_VIEWID: @@ -365,7 +365,7 @@ namespace OHOS set_need_tts_reconnect(1); // 开始tts重连 break; case AIVOICE_LISTENING_VIEWID: - set_stop_send_ai_flag(0); // 开始发送AI数据 + // set_stop_send_ai_flag(0); // 开始发送AI数据 AiVoiceModel::GetInstance().StartListenAndRecordingUserQuestion(); AiVoiceModel::GetInstance().start_listening_timer(); break; -- 2.17.1