fix: 优化 VAD 语音片段收集
This commit is contained in:
@@ -27,7 +27,6 @@ class __EventManager:
|
|||||||
self.session_id = 0
|
self.session_id = 0
|
||||||
self.current_step = Step.idle
|
self.current_step = Step.idle
|
||||||
self.next_step_future = None
|
self.next_step_future = None
|
||||||
self.tts_session_id = ""
|
|
||||||
|
|
||||||
def update_step(self, step: Step, step_data=None):
|
def update_step(self, step: Step, step_data=None):
|
||||||
if get_xiaoai().mode == "xiaozhi":
|
if get_xiaoai().mode == "xiaozhi":
|
||||||
@@ -80,12 +79,9 @@ class __EventManager:
|
|||||||
|
|
||||||
def on_tts_end(self, session_id):
|
def on_tts_end(self, session_id):
|
||||||
"""TTS结束"""
|
"""TTS结束"""
|
||||||
if self.current_step == Step.on_interrupt:
|
if self.current_step in [Step.on_interrupt, Step.on_tts_end]:
|
||||||
# 当前 session 已经被打断了,不再处理
|
# 当前 session 已经被打断了,不再处理
|
||||||
return
|
return
|
||||||
if self.tts_session_id == session_id:
|
|
||||||
return
|
|
||||||
self.tts_session_id = session_id
|
|
||||||
self.session_id = self.session_id + 1
|
self.session_id = self.session_id + 1
|
||||||
self.update_step(Step.on_tts_end)
|
self.update_step(Step.on_tts_end)
|
||||||
self.start_session()
|
self.start_session()
|
||||||
|
|||||||
@@ -33,16 +33,16 @@ class _VAD:
|
|||||||
self.stream = None
|
self.stream = None
|
||||||
|
|
||||||
# 暂存的语音片段
|
# 暂存的语音片段
|
||||||
self.temp_frames = []
|
self.silence_frames = [] # 静音片段
|
||||||
self.speech_buffer = []
|
self.speech_frames = [] # 语音片段
|
||||||
self.target = None # 检测目标 speech/silence
|
self.target = None # 检测目标 speech/silence
|
||||||
|
|
||||||
def _reset_state(self):
|
def _reset_state(self):
|
||||||
"""重置状态"""
|
"""重置状态"""
|
||||||
self.speech_count = 0
|
self.speech_count = 0
|
||||||
self.silence_count = 0
|
self.silence_count = 0
|
||||||
self.speech_buffer = []
|
self.speech_frames = []
|
||||||
self.temp_frames = []
|
self.silence_frames = []
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""启动VAD检测器"""
|
"""启动VAD检测器"""
|
||||||
@@ -78,22 +78,37 @@ class _VAD:
|
|||||||
"""处理语音帧"""
|
"""处理语音帧"""
|
||||||
self.speech_count += len(frames)
|
self.speech_count += len(frames)
|
||||||
self.silence_count = 0
|
self.silence_count = 0
|
||||||
self.speech_buffer.extend(frames)
|
|
||||||
|
|
||||||
speech_bytes = bytes(self.speech_buffer)
|
if not self.speech_frames:
|
||||||
|
# 加入静音片段(潜在的语音片段)
|
||||||
|
self.speech_frames.extend(self.silence_frames)
|
||||||
|
|
||||||
|
# 加入语音片段
|
||||||
|
self.speech_frames.extend(frames)
|
||||||
|
|
||||||
|
speech_bytes = bytes(self.speech_frames)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self.target == "speech"
|
self.target == "speech"
|
||||||
and self.speech_count > self.min_speech_duration * self.sample_rate / 1000
|
and self.speech_count > self.min_speech_duration * self.sample_rate / 1000
|
||||||
):
|
):
|
||||||
self.pause()
|
self.pause()
|
||||||
|
# !FIXME: 需要保证音频流的连续性(在发消息期间)
|
||||||
EventManager.on_speech(speech_bytes)
|
EventManager.on_speech(speech_bytes)
|
||||||
|
|
||||||
def _handle_silence_frame(self, frames):
|
def _handle_silence_frame(self, frames):
|
||||||
"""处理静音帧"""
|
"""处理静音帧"""
|
||||||
self.silence_count += len(frames)
|
self.silence_count += len(frames)
|
||||||
self.speech_count = 0
|
self.speech_count = 0
|
||||||
self.speech_buffer = []
|
|
||||||
|
if not self.speech_frames:
|
||||||
|
# 如果之前没有语音片段,则将当前帧加入静音片段
|
||||||
|
self.silence_frames.extend(frames)
|
||||||
|
# 确保静音片段长度不超过 3s
|
||||||
|
self.silence_frames = self.silence_frames[: 3 * self.sample_rate]
|
||||||
|
else:
|
||||||
|
# 如果之前有语音片段,则将当前帧加入语音片段
|
||||||
|
self.speech_frames.extend(frames)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
self.target == "silence"
|
self.target == "silence"
|
||||||
|
|||||||
Reference in New Issue
Block a user