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 = ",。!?;:))”、"; // 要替换的中文标点
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();
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);
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)
}
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; // 重置静态变量
}
}