Skip to content

Commit 98f3432

Browse files
author
jiangtao
committed
add print log
1 parent 4ba7890 commit 98f3432

File tree

3 files changed

+3
-63
lines changed

3 files changed

+3
-63
lines changed

core/connection.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ def speak_and_play(self, text):
325325
if tts_file is None:
326326
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
327327
return None, text
328-
329328
self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}")
330329
return tts_file, text
331330

@@ -362,19 +361,4 @@ def stop_all_tasks(self):
362361
while self.scheduled_tasks:
363362
task = self.scheduled_tasks.popleft()
364363
task.cancel()
365-
self.scheduled_tasks.clear()
366-
367-
def full_to_half(text):
368-
"""
369-
将全角字符转换为半角字符
370-
包括:数字、字母、标点符号
371-
"""
372-
result = ""
373-
for char in text:
374-
code = ord(char)
375-
if code == 0x3000: # 全角空格
376-
char = ' '
377-
elif 0xFF01 <= code <= 0xFF5E: # 全角字符范围
378-
char = chr(code - 0xFEE0)
379-
result += char
380-
return result
364+
self.scheduled_tasks.clear()

core/handle/audioHandle.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ async def isLLMWantToFinish(conn):
5959
first_text = conn.tts_first_text
6060
last_text = conn.tts_last_text
6161
_, last_text_without_punctuation = remove_punctuation_and_length(last_text)
62-
logger.bind(tag=TAG).info(f"last_text_without_punctuation: {last_text_without_punctuation}, last_text: {last_text}, first_text: {first_text}")
6362
if "再见" in last_text_without_punctuation or "拜拜" in last_text_without_punctuation:
6463
return True
6564
_, first_text_without_punctuation = remove_punctuation_and_length(first_text)
@@ -84,7 +83,7 @@ async def sendAudioMessage(conn, audios, duration, text):
8483
if text == conn.tts_first_text:
8584
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
8685
conn.tts_start_speak_time = time.time()
87-
#base_delay = len(text) * 0.45
86+
8887
# 发送 sentence_start(每个音频文件之前发送一次)
8988
sentence_task = asyncio.create_task(
9089
schedule_with_interrupt(base_delay, send_tts_message(conn, "sentence_start", text))
@@ -101,7 +100,7 @@ async def sendAudioMessage(conn, audios, duration, text):
101100
stop_duration = conn.tts_duration - (time.time() - conn.tts_start_speak_time)
102101
logger.bind(tag=TAG).info(f"llm_finish_task: {text}, stop_duration: {stop_duration}")
103102
stop_task = asyncio.create_task(
104-
schedule_with_interrupt(base_delay, send_tts_message(conn, 'stop'))
103+
schedule_with_interrupt(stop_duration, send_tts_message(conn, 'stop'))
105104
)
106105
conn.scheduled_tasks.append(stop_task)
107106
if await isLLMWantToFinish(conn):

core/providers/tts/base.py

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,4 @@ def wav_to_opus_data(self, wav_file_path):
8282
opus_data = encoder.encode(np_frame.tobytes(), frame_size)
8383
opus_datas.append(opus_data)
8484

85-
8685
return opus_datas, duration
87-
88-
def read_opus_data(self, opus_file_path, format='opus'):
89-
"""
90-
直接读取opus文件数据并解码重新编码,确保格式一致
91-
"""
92-
try:
93-
# 使用pydub获取音频时长和PCM数据
94-
audio = AudioSegment.from_file(opus_file_path, format=format)
95-
duration = len(audio) / 1000.0
96-
97-
# 确保音频格式正确
98-
audio = audio.set_channels(1) # 单声道
99-
audio = audio.set_frame_rate(16000) # 16kHz采样率
100-
audio = audio.set_sample_width(2) # 16位采样
101-
102-
# 获取原始PCM数据
103-
raw_data = audio.raw_data
104-
105-
# 初始化Opus编码器
106-
encoder = opuslib_next.Encoder(16000, 1, opuslib_next.APPLICATION_AUDIO)
107-
108-
# 编码参数
109-
frame_duration = 60 # 60ms per frame
110-
frame_size = int(16000 * frame_duration / 1000) # 960 samples/frame
111-
112-
opus_datas = []
113-
# 按帧处理所有音频数据
114-
for i in range(0, len(raw_data), frame_size * 2):
115-
chunk = raw_data[i:i + frame_size * 2]
116-
117-
if len(chunk) < frame_size * 2:
118-
chunk += b'\x00' * (frame_size * 2 - len(chunk))
119-
120-
np_frame = np.frombuffer(chunk, dtype=np.int16)
121-
opus_data = encoder.encode(np_frame.tobytes(), frame_size)
122-
opus_datas.append(opus_data)
123-
124-
return opus_datas, duration
125-
126-
except Exception as e:
127-
logger.bind(tag=TAG).error(f"处理音频文件失败: {e}")
128-
return [], 0

0 commit comments

Comments
 (0)