Skip to content

Commit 9387a5a

Browse files
author
jiangtao
committed
语音发送新增同步机制
1 parent a8af61f commit 9387a5a

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

core/providers/tts/base.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,25 @@ def get_opus_data(self, file_path):
5252
# 获取音频时长
5353
duration = self.get_audio_duration(file_path)
5454

55-
# 初始化Opus编码器(用于验证帧大小)
56-
encoder = opuslib_next.Encoder(16000, 1, opuslib_next.APPLICATION_AUDIO)
57-
58-
# 编码参数(与原wav转换保持一致)
59-
frame_duration = 60 # 60ms per frame
60-
frame_size = int(16000 * frame_duration / 1000) # 960 samples/frame
61-
6255
opus_datas = []
6356
current_pos = 0
6457

6558
while current_pos < len(opus_data):
66-
# 读取帧数据
67-
frame_data = opus_data[current_pos:current_pos + frame_size]
68-
if len(frame_data) < frame_size:
69-
# 如果最后一帧不足,补零
70-
frame_data += b'\x00' * (frame_size - len(frame_data))
59+
# 读取帧长度(前2个字节)
60+
if current_pos + 2 > len(opus_data):
61+
break
62+
63+
frame_length = int.from_bytes(opus_data[current_pos:current_pos + 2], 'little')
64+
current_pos += 2
7165

72-
# 使用与原方法相同的编码方式
73-
encoded_data = encoder.encode(frame_data, frame_size)
74-
opus_datas.append(encoded_data)
75-
76-
current_pos += frame_size
66+
# 确保有足够的数据读取
67+
if current_pos + frame_length > len(opus_data):
68+
break
69+
70+
# 读取帧数据
71+
frame_data = opus_data[current_pos:current_pos + frame_length]
72+
opus_datas.append(frame_data)
73+
current_pos += frame_length
7774

7875
return opus_datas, duration
7976

core/providers/tts/doubao.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def text_to_speak(self, text, output_file):
3939
"speed_ratio": 1.0,
4040
"volume_ratio": 1.0,
4141
"pitch_ratio": 1.0,
42-
"rate": 16000,
42+
"rate": 16000
4343
},
4444
"request": {
4545
"reqid": str(uuid.uuid4()),

0 commit comments

Comments
 (0)