fix audio
This commit is contained in:
parent
e4bcce4946
commit
bc1dd7f03f
@ -80,6 +80,25 @@ class AudioDeviceManager:
|
||||
except Exception as e:
|
||||
print(f"音频播放失败: {e}")
|
||||
|
||||
def read_audio_data(self, frames: int) -> bytes:
|
||||
"""读取音频数据"""
|
||||
try:
|
||||
if self.input_stream is None:
|
||||
return b'\x00' * (frames * 2) # 返回静音数据
|
||||
|
||||
# sounddevice返回numpy数组
|
||||
audio_data = self.input_stream.read(frames)
|
||||
|
||||
# 转换为字节数据
|
||||
if isinstance(audio_data, np.ndarray):
|
||||
return audio_data.tobytes()
|
||||
else:
|
||||
return audio_data
|
||||
|
||||
except Exception as e:
|
||||
print(f"读取音频数据失败: {e}")
|
||||
return b'\x00' * (frames * 2) # 返回静音数据
|
||||
|
||||
def cleanup(self) -> None:
|
||||
"""清理音频设备资源"""
|
||||
try:
|
||||
@ -649,8 +668,8 @@ class DialogSession:
|
||||
# 非播放期间:正常录音
|
||||
last_silence_time = current_time
|
||||
|
||||
# 添加exception_on_overflow=False参数来忽略溢出错误
|
||||
audio_data = stream.read(config.input_audio_config["chunk"], exception_on_overflow=False)
|
||||
# 使用AudioDeviceManager的专用读取方法
|
||||
audio_data = self.audio_device.read_audio_data(config.input_audio_config["chunk"])
|
||||
|
||||
# 在发送前再次检查是否应该发送静音数据(最后一道防线)
|
||||
with self.audio_queue_lock:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user