This commit is contained in:
yumoqing 2024-09-11 15:59:58 +08:00
parent 5a6d421b3d
commit ec070ff771

View File

@ -6,6 +6,23 @@ from av import AudioFrame
from aiortc import VideoStreamTrack, AudioStreamTrack
def bytes_to_audio_frame(bytes_data, format='s16', layout='mono', sample_rate=16000):
"""
从字节数据构造av.AudioFrame对象
参数:
- bytes_data: 字节数据代表音频内容
- format: 音频样本格式字符串's16'表示16位有符号整数
- layout: 通道布局'mono''stereo'
- sample_rate: 采样率
"""
# 根据给定的参数创建AudioFrame
frame = av.AudioFrame(format=format, layout=layout, samples=len(bytes_data) // av.AudioFrame.format_size(format), rate=sample_rate)
# 将字节数据复制到AudioFrame中
frame.planes[0].update(bytes_data)
return frame
class LLMAudioStreamTrack(AudioStreamTrack):
def __init__(self, omni_infer):
super().__init__()
@ -19,7 +36,7 @@ class LLMAudioStreamTrack(AudioStreamTrack):
b = self.get_audio_bytes()
if b is None:
return await super().recv()
frame = AudioFrame.from_ndarray(io.BytesIO(b), format='s16', layout='mono')
frame = bytes_to_audio_frame(b)
print('LLMAudioStreamTrack return frame ...')
return frame
except Exception as e: