Update core/silero_vad.py
Browse files- core/silero_vad.py +19 -11
core/silero_vad.py
CHANGED
|
@@ -1,5 +1,3 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
import io
|
| 4 |
import numpy as np
|
| 5 |
import soundfile as sf
|
|
@@ -114,7 +112,7 @@ class SileroVAD:
|
|
| 114 |
# Logic state machine cải tiến
|
| 115 |
if self.state == "silence":
|
| 116 |
if speech_prob > self.speech_threshold:
|
| 117 |
-
print(" Bắt đầu phát hiện speech")
|
| 118 |
self.state = "speech"
|
| 119 |
self.speech_start_time = current_time
|
| 120 |
self.last_voice_time = current_time
|
|
@@ -135,24 +133,34 @@ class SileroVAD:
|
|
| 135 |
if speech_prob > self.speech_threshold:
|
| 136 |
self.last_voice_time = current_time
|
| 137 |
|
| 138 |
-
#
|
| 139 |
silence_duration = current_time - self.last_voice_time
|
| 140 |
speech_duration = current_time - self.speech_start_time
|
| 141 |
|
| 142 |
-
#
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
self._finalize_speech()
|
| 147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
elif speech_duration > settings.MAX_AUDIO_DURATION:
|
| 149 |
-
print(f"Speech timeout ({speech_duration:.2f}s)")
|
| 150 |
self._finalize_speech()
|
| 151 |
|
| 152 |
elif self.state == "processing":
|
| 153 |
# Đang xử lý, không nhận thêm audio
|
| 154 |
pass
|
| 155 |
-
|
| 156 |
def _finalize_speech(self):
|
| 157 |
"""Hoàn thành xử lý speech segment"""
|
| 158 |
if not self.speech_buffer or len(self.speech_buffer) == 0:
|
|
|
|
|
|
|
|
|
|
| 1 |
import io
|
| 2 |
import numpy as np
|
| 3 |
import soundfile as sf
|
|
|
|
| 112 |
# Logic state machine cải tiến
|
| 113 |
if self.state == "silence":
|
| 114 |
if speech_prob > self.speech_threshold:
|
| 115 |
+
print("🎤 Bắt đầu phát hiện speech")
|
| 116 |
self.state = "speech"
|
| 117 |
self.speech_start_time = current_time
|
| 118 |
self.last_voice_time = current_time
|
|
|
|
| 133 |
if speech_prob > self.speech_threshold:
|
| 134 |
self.last_voice_time = current_time
|
| 135 |
|
| 136 |
+
# Tính toán thời gian
|
| 137 |
silence_duration = current_time - self.last_voice_time
|
| 138 |
speech_duration = current_time - self.speech_start_time
|
| 139 |
|
| 140 |
+
# 🎯 LOGIC KẾT THÚC THÔNG MINH - 3 TRƯỜNG HỢP:
|
| 141 |
+
|
| 142 |
+
# 1. User nói ngắn (dưới min_speech) nhưng đã im lặng đủ lâu -> XỬ LÝ NGAY
|
| 143 |
+
is_short_response = speech_duration < self.min_speech_duration
|
| 144 |
+
is_long_silence_after_short = silence_duration >= self.min_silence_duration
|
| 145 |
+
|
| 146 |
+
if is_short_response and is_long_silence_after_short:
|
| 147 |
+
print(f"🎯 Phát hiện phản hồi ngắn: {speech_duration:.2f}s, im lặng: {silence_duration:.2f}s")
|
| 148 |
self._finalize_speech()
|
| 149 |
+
|
| 150 |
+
# 2. User nói đủ dài VÀ im lặng đủ lâu -> XỬ LÝ BÌNH THƯỜNG
|
| 151 |
+
elif (speech_duration >= self.min_speech_duration and
|
| 152 |
+
silence_duration >= self.min_silence_duration):
|
| 153 |
+
print(f"🎯 Kết thúc speech dài: {speech_duration:.2f}s")
|
| 154 |
+
self._finalize_speech()
|
| 155 |
+
|
| 156 |
+
# 3. Speech quá dài (timeout) -> XỬ LÝ DÙ ĐANG NÓI
|
| 157 |
elif speech_duration > settings.MAX_AUDIO_DURATION:
|
| 158 |
+
print(f"⏰ Speech timeout ({speech_duration:.2f}s) - xử lý dù đang nói")
|
| 159 |
self._finalize_speech()
|
| 160 |
|
| 161 |
elif self.state == "processing":
|
| 162 |
# Đang xử lý, không nhận thêm audio
|
| 163 |
pass
|
|
|
|
| 164 |
def _finalize_speech(self):
|
| 165 |
"""Hoàn thành xử lý speech segment"""
|
| 166 |
if not self.speech_buffer or len(self.speech_buffer) == 0:
|