Update services/streaming_voice_service.py
Browse files
services/streaming_voice_service.py
CHANGED
|
@@ -596,7 +596,7 @@ class VoskStreamingASR:
|
|
| 596 |
return False
|
| 597 |
|
| 598 |
def process_audio_chunk(self, audio_chunk: np.ndarray, sample_rate: int = None) -> Dict[str, Any]:
|
| 599 |
-
"""Xử lý audio chunk và trả về kết quả - FIXED VERSION"""
|
| 600 |
if self.recognizer is None or not self.is_streaming:
|
| 601 |
return {"text": "", "partial": "", "is_final": False}
|
| 602 |
|
|
@@ -616,12 +616,16 @@ class VoskStreamingASR:
|
|
| 616 |
else:
|
| 617 |
audio_chunk = audio_chunk.astype(np.int16)
|
| 618 |
|
| 619 |
-
#
|
|
|
|
|
|
|
|
|
|
| 620 |
audio_rms = np.sqrt(np.mean(audio_chunk.astype(np.float32)**2)) / 32767.0
|
| 621 |
-
print(f"📊 Audio RMS: {audio_rms:.4f}")
|
| 622 |
|
| 623 |
-
|
| 624 |
-
|
|
|
|
| 625 |
return {"text": "", "partial": "", "is_final": False}
|
| 626 |
|
| 627 |
# Chuyển đổi sang bytes
|
|
@@ -651,6 +655,27 @@ class VoskStreamingASR:
|
|
| 651 |
|
| 652 |
return {"text": "", "partial": "", "is_final": False}
|
| 653 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
def stop_stream(self) -> str:
|
| 655 |
"""Kết thúc stream và lấy kết quả cuối"""
|
| 656 |
if self.recognizer:
|
|
|
|
| 596 |
return False
|
| 597 |
|
| 598 |
def process_audio_chunk(self, audio_chunk: np.ndarray, sample_rate: int = None) -> Dict[str, Any]:
|
| 599 |
+
"""Xử lý audio chunk và trả về kết quả - FIXED VOLUME VERSION"""
|
| 600 |
if self.recognizer is None or not self.is_streaming:
|
| 601 |
return {"text": "", "partial": "", "is_final": False}
|
| 602 |
|
|
|
|
| 616 |
else:
|
| 617 |
audio_chunk = audio_chunk.astype(np.int16)
|
| 618 |
|
| 619 |
+
# FIXED: Tăng cường âm lượng trước khi kiểm tra
|
| 620 |
+
audio_chunk = self._boost_audio_volume(audio_chunk)
|
| 621 |
+
|
| 622 |
+
# Kiểm tra âm lượng - GIẢM ngưỡng xuống
|
| 623 |
audio_rms = np.sqrt(np.mean(audio_chunk.astype(np.float32)**2)) / 32767.0
|
| 624 |
+
print(f"📊 Audio RMS: {audio_rms:.4f}, Max: {np.max(audio_chunk)}")
|
| 625 |
|
| 626 |
+
# FIXED: Giảm ngưỡng âm lượng từ 0.01 xuống 0.001
|
| 627 |
+
if audio_rms < 0.001: # Giảm ngưỡng 10 lần
|
| 628 |
+
print(f"⚠️ Âm lượng quá thấp (RMS: {audio_rms:.6f}), bỏ qua")
|
| 629 |
return {"text": "", "partial": "", "is_final": False}
|
| 630 |
|
| 631 |
# Chuyển đổi sang bytes
|
|
|
|
| 655 |
|
| 656 |
return {"text": "", "partial": "", "is_final": False}
|
| 657 |
|
| 658 |
+
def _boost_audio_volume(self, audio_chunk: np.ndarray, boost_factor: float = 5.0) -> np.ndarray:
|
| 659 |
+
"""Tăng cường âm lượng audio"""
|
| 660 |
+
try:
|
| 661 |
+
# Chuyển sang float để xử lý
|
| 662 |
+
audio_float = audio_chunk.astype(np.float32) / 32768.0
|
| 663 |
+
|
| 664 |
+
# Tăng âm lượng
|
| 665 |
+
boosted_audio = audio_float * boost_factor
|
| 666 |
+
|
| 667 |
+
# Ngăn chặn clipping
|
| 668 |
+
boosted_audio = np.clip(boosted_audio, -1.0, 1.0)
|
| 669 |
+
|
| 670 |
+
# Chuyển lại sang int16
|
| 671 |
+
boosted_audio_int16 = (boosted_audio * 32767).astype(np.int16)
|
| 672 |
+
|
| 673 |
+
print(f"🔊 Volume boosted: {boost_factor}x, New max: {np.max(boosted_audio_int16)}")
|
| 674 |
+
return boosted_audio_int16
|
| 675 |
+
|
| 676 |
+
except Exception as e:
|
| 677 |
+
print(f"⚠️ Lỗi boost volume: {e}")
|
| 678 |
+
return audio_chunk
|
| 679 |
def stop_stream(self) -> str:
|
| 680 |
"""Kết thúc stream và lấy kết quả cuối"""
|
| 681 |
if self.recognizer:
|