Update services/streaming_voice_service.py
Browse files
services/streaming_voice_service.py
CHANGED
|
@@ -966,28 +966,35 @@ class StreamingVoiceService:
|
|
| 966 |
time.sleep(0.5)
|
| 967 |
|
| 968 |
def process_streaming_audio(self, audio_data: tuple) -> Dict[str, Any]:
|
| 969 |
-
"""Xử lý audio streaming manual mode với VOSK - FIXED VERSION"""
|
| 970 |
if not audio_data:
|
| 971 |
return self._create_error_response("❌ Không có dữ liệu âm thanh")
|
| 972 |
|
| 973 |
try:
|
| 974 |
sample_rate, audio_array = audio_data
|
| 975 |
|
| 976 |
-
print(f"🎤 Manual audio: {len(audio_array)} samples, {sample_rate}Hz")
|
| 977 |
|
| 978 |
-
#
|
|
|
|
|
|
|
|
|
|
| 979 |
if isinstance(audio_array, np.ndarray):
|
| 980 |
if audio_array.dtype in [np.float32, np.float64]:
|
| 981 |
audio_rms = np.sqrt(np.mean(audio_array**2))
|
| 982 |
-
|
|
|
|
| 983 |
|
| 984 |
-
|
| 985 |
-
|
| 986 |
-
|
| 987 |
-
|
| 988 |
-
|
| 989 |
-
|
| 990 |
-
|
|
|
|
|
|
|
|
|
|
| 991 |
|
| 992 |
# Khởi động VOSK stream tạm thời
|
| 993 |
if not self.vosk_asr.start_stream():
|
|
@@ -1019,7 +1026,7 @@ class StreamingVoiceService:
|
|
| 1019 |
}
|
| 1020 |
else:
|
| 1021 |
return {
|
| 1022 |
-
'transcription': "Đang nghe... Hãy nói rõ hơn",
|
| 1023 |
'response': "",
|
| 1024 |
'tts_audio': None,
|
| 1025 |
'status': 'listening'
|
|
@@ -1029,6 +1036,26 @@ class StreamingVoiceService:
|
|
| 1029 |
print(f"❌ Lỗi xử lý streaming audio: {e}")
|
| 1030 |
traceback.print_exc()
|
| 1031 |
return self._create_error_response(f"❌ Lỗi: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1032 |
|
| 1033 |
def _generate_ai_response_optimized(self, transcription: str) -> str:
|
| 1034 |
"""Tạo phản hồi AI tối ưu hóa"""
|
|
|
|
| 966 |
time.sleep(0.5)
|
| 967 |
|
| 968 |
def process_streaming_audio(self, audio_data: tuple) -> Dict[str, Any]:
|
| 969 |
+
"""Xử lý audio streaming manual mode với VOSK - FIXED VOLUME VERSION"""
|
| 970 |
if not audio_data:
|
| 971 |
return self._create_error_response("❌ Không có dữ liệu âm thanh")
|
| 972 |
|
| 973 |
try:
|
| 974 |
sample_rate, audio_array = audio_data
|
| 975 |
|
| 976 |
+
print(f"🎤 Manual audio: {len(audio_array)} samples, {sample_rate}Hz, Max: {np.max(audio_array)}")
|
| 977 |
|
| 978 |
+
# FIXED: Tăng cường âm lượng trước khi xử lý
|
| 979 |
+
audio_array = self._boost_input_volume(audio_array)
|
| 980 |
+
|
| 981 |
+
# Kiểm tra âm lượng với ngưỡng thấp hơn
|
| 982 |
if isinstance(audio_array, np.ndarray):
|
| 983 |
if audio_array.dtype in [np.float32, np.float64]:
|
| 984 |
audio_rms = np.sqrt(np.mean(audio_array**2))
|
| 985 |
+
else:
|
| 986 |
+
audio_rms = np.sqrt(np.mean(audio_array.astype(np.float32)**2)) / 32768.0
|
| 987 |
|
| 988 |
+
print(f"📊 Manual audio RMS: {audio_rms:.6f}, Max: {np.max(audio_array)}")
|
| 989 |
+
|
| 990 |
+
# FIXED: Giảm ngưỡng âm lượng
|
| 991 |
+
if audio_rms < 0.001: # Giảm từ 0.01 xuống 0.001
|
| 992 |
+
return {
|
| 993 |
+
'transcription': f"Âm thanh quá nhỏ (RMS: {audio_rms:.6f}), hãy nói to hơn hoặc điều chỉnh microphone",
|
| 994 |
+
'response': "",
|
| 995 |
+
'tts_audio': None,
|
| 996 |
+
'status': 'listening'
|
| 997 |
+
}
|
| 998 |
|
| 999 |
# Khởi động VOSK stream tạm thời
|
| 1000 |
if not self.vosk_asr.start_stream():
|
|
|
|
| 1026 |
}
|
| 1027 |
else:
|
| 1028 |
return {
|
| 1029 |
+
'transcription': "Đang nghe... Hãy nói rõ hơn và gần microphone",
|
| 1030 |
'response': "",
|
| 1031 |
'tts_audio': None,
|
| 1032 |
'status': 'listening'
|
|
|
|
| 1036 |
print(f"❌ Lỗi xử lý streaming audio: {e}")
|
| 1037 |
traceback.print_exc()
|
| 1038 |
return self._create_error_response(f"❌ Lỗi: {str(e)}")
|
| 1039 |
+
|
| 1040 |
+
def _boost_input_volume(self, audio_array: np.ndarray, boost_factor: float = 10.0) -> np.ndarray:
|
| 1041 |
+
"""Tăng cường âm lượng input audio"""
|
| 1042 |
+
try:
|
| 1043 |
+
if audio_array.dtype in [np.float32, np.float64]:
|
| 1044 |
+
# Audio đã ở dạng float
|
| 1045 |
+
boosted = audio_array * boost_factor
|
| 1046 |
+
boosted = np.clip(boosted, -1.0, 1.0)
|
| 1047 |
+
else:
|
| 1048 |
+
# Audio ở dạng int
|
| 1049 |
+
boosted = audio_array.astype(np.float32) * boost_factor
|
| 1050 |
+
max_val = np.iinfo(audio_array.dtype).max
|
| 1051 |
+
boosted = np.clip(boosted, -max_val, max_val).astype(audio_array.dtype)
|
| 1052 |
+
|
| 1053 |
+
print(f"🔊 Input volume boosted: {boost_factor}x")
|
| 1054 |
+
return boosted
|
| 1055 |
+
|
| 1056 |
+
except Exception as e:
|
| 1057 |
+
print(f"⚠️ Lỗi boost input volume: {e}")
|
| 1058 |
+
return audio_array
|
| 1059 |
|
| 1060 |
def _generate_ai_response_optimized(self, transcription: str) -> str:
|
| 1061 |
"""Tạo phản hồi AI tối ưu hóa"""
|