Skip to content

Commit a8af61f

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

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

core/providers/tts/base.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,31 @@ def get_opus_data(self, file_path):
4949
with open(file_path, 'rb') as f:
5050
opus_data = f.read()
5151

52-
# 获取音频时长(从API响应中获取)
52+
# 获取音频时长
5353
duration = self.get_audio_duration(file_path)
5454

55-
# 按照每帧大小分割数据
56-
frame_size = 960 # opus标准帧大小
57-
opus_datas = []
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
5861

59-
# 分帧处理
62+
opus_datas = []
6063
current_pos = 0
64+
6165
while current_pos < len(opus_data):
62-
# 读取帧长度(前2个字节)
63-
frame_length = int.from_bytes(opus_data[current_pos:current_pos + 2], 'little')
64-
current_pos += 2
65-
6666
# 读取帧数据
67-
frame_data = opus_data[current_pos:current_pos + frame_length]
68-
opus_datas.append(frame_data)
69-
current_pos += frame_length
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))
71+
72+
# 使用与原方法相同的编码方式
73+
encoded_data = encoder.encode(frame_data, frame_size)
74+
opus_datas.append(encoded_data)
75+
76+
current_pos += frame_size
7077

7178
return opus_datas, duration
7279

0 commit comments

Comments
 (0)