From ec070ff77123e108041ffec1cc0a8ba3acc91907 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Wed, 11 Sep 2024 15:59:58 +0800 Subject: [PATCH] bugfix --- rtcllm/a2a.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/rtcllm/a2a.py b/rtcllm/a2a.py index 3d7957d..e9e88ed 100644 --- a/rtcllm/a2a.py +++ b/rtcllm/a2a.py @@ -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: