File size: 20,346 Bytes
56d3d8c
a00cee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b96139a
a00cee2
56d3d8c
a00cee2
 
 
ac97430
a00cee2
 
 
b96139a
6114fec
b96139a
 
1129e66
b96139a
 
 
a00cee2
 
56d3d8c
a00cee2
 
 
 
 
 
 
 
 
9c3732f
a00cee2
 
 
9c3732f
b96139a
 
 
56d3d8c
b96139a
56d3d8c
a00cee2
56d3d8c
1129e66
56d3d8c
1129e66
b96139a
56d3d8c
b96139a
 
 
 
 
 
 
 
 
7719f9b
b96139a
 
 
a00cee2
 
 
56d3d8c
a00cee2
 
b96139a
 
 
 
 
 
 
a00cee2
 
 
 
b96139a
 
a00cee2
b96139a
 
 
 
1129e66
b96139a
 
 
a00cee2
b96139a
 
a00cee2
9c3732f
 
a00cee2
9c3732f
b96139a
 
a00cee2
b96139a
a00cee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c3732f
a00cee2
 
 
9c3732f
a00cee2
 
 
 
 
 
 
 
 
 
 
 
 
9c3732f
a00cee2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56d3d8c
 
7719f9b
56d3d8c
 
 
7719f9b
56d3d8c
 
 
a00cee2
56d3d8c
9c3732f
a00cee2
 
56d3d8c
 
 
 
a00cee2
56d3d8c
b96139a
 
9c3732f
b96139a
 
 
9c3732f
 
 
 
 
a00cee2
b96139a
 
 
a00cee2
b96139a
56d3d8c
9c3732f
b96139a
 
 
56d3d8c
9c3732f
a00cee2
9c3732f
 
 
 
 
 
 
 
 
a00cee2
9c3732f
b96139a
a00cee2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509

# import torch
# import numpy as np
# from typing import Callable
# from config.settings import settings
# import os
# import time


# class SileroVAD:
#     def __init__(self):
#         self.model = None
#         self.utils = None
#         self.sample_rate = 16000
#         self.is_streaming = False
#         self.speech_callback = None
#         self.audio_buffer = []
#         self.speech_start_time = 0
#         self.min_speech_duration = 0.5  # Giây
        
#         # ✅ Thêm cấu hình chunk size cho Silero
#         self.chunk_size = 512  # Silero yêu cầu 512 samples cho 16000Hz
#         self.chunk_duration = self.chunk_size / self.sample_rate  # 0.032 giây
        
#         self._initialize_model()

#     def _initialize_model(self):
#         """Khởi tạo Silero VAD model"""
#         try:
#             print("🔄 Đang tải Silero VAD model...")

#             self.model, self.utils = torch.hub.load(
#                 repo_or_dir='snakers4/silero-vad',
#                 model='silero_vad',
#                 force_reload=False,
#                 trust_repo=True
#             )

#             self.model.eval()
#             print("✅ Đã tải Silero VAD model thành công")

#         except Exception as e:
#             print(f"❌ Lỗi tải Silero VAD model: {e}")
#             self._initialize_model_fallback()

#     def _initialize_model_fallback(self):
#         """Fallback nếu torch.hub.load thất bại"""
#         try:
#             model_dir = torch.hub.get_dir()
#             model_path = os.path.join(
#                 model_dir, 'snakers4_silero-vad_master', 'files', 'silero_vad.jit'
#             )

#             if os.path.exists(model_path):
#                 self.model = torch.jit.load(model_path)
#                 self.model.eval()
#                 print("✅ Đã tải Silero VAD model thành công (fallback)")
#             else:
#                 print("❌ Không tìm thấy model file (fallback thất bại)")
#                 self.model = None

#         except Exception as e:
#             print(f"❌ Lỗi tải Silero VAD model fallback: {e}")
#             self.model = None

#     def start_stream(self, speech_callback: Callable):
#         """Bắt đầu stream với VAD"""
#         if self.model is None:
#             print("❌ Silero VAD model chưa được khởi tạo")
#             return False

#         self.is_streaming = True
#         self.speech_callback = speech_callback
#         self.audio_buffer = []
#         self.speech_start_time = 0
#         print("🎙️ Bắt đầu Silero VAD streaming...")
#         return True

#     def stop_stream(self):
#         """Dừng stream"""
#         self.is_streaming = False
#         self.speech_callback = None
#         self.audio_buffer = []
#         self.speech_start_time = 0
#         print("🛑 Đã dừng Silero VAD streaming")

#     def process_stream(self, audio_chunk: np.ndarray, sample_rate: int):
#         """Xử lý audio chunk với Silero VAD - ĐÃ SỬA LỖI"""
#         if not self.is_streaming or self.model is None:
#             return

#         try:
#             # Resample nếu cần
#             if sample_rate != self.sample_rate:
#                 audio_chunk = self._resample_audio(audio_chunk, sample_rate, self.sample_rate)

#             # Thêm vào buffer
#             self.audio_buffer.extend(audio_chunk)

#             # ✅ Xử lý từng chunk 512 samples (Silero requirement)
#             while len(self.audio_buffer) >= self.chunk_size:
#                 chunk = self.audio_buffer[:self.chunk_size]
#                 self._process_single_chunk(np.array(chunk))
#                 # Giữ lại phần thừa cho chunk tiếp theo
#                 self.audio_buffer = self.audio_buffer[self.chunk_size:]

#         except Exception as e:
#             print(f"❌ Lỗi xử lý Silero VAD: {e}")

#     def _process_single_chunk(self, audio_chunk: np.ndarray):
#         """Xử lý một chunk 512 samples duy nhất"""
#         try:
#             # Chuẩn hóa audio
#             audio_chunk = self._normalize_audio(audio_chunk)
            
#             # Đảm bảo đúng kích thước
#             if len(audio_chunk) != self.chunk_size:
#                 # Nếu không đủ, pad với zeros
#                 if len(audio_chunk) < self.chunk_size:
#                     padding = np.zeros(self.chunk_size - len(audio_chunk), dtype=np.float32)
#                     audio_chunk = np.concatenate([audio_chunk, padding])
#                 else:
#                     audio_chunk = audio_chunk[:self.chunk_size]

#             # Dự đoán xác suất speech
#             speech_prob = self._get_speech_probability(audio_chunk)
#             print(f"🎯 Silero VAD speech probability: {speech_prob:.3f}")

#             # Xử lý logic speech detection
#             current_time = time.time()
            
#             if speech_prob > settings.VAD_THRESHOLD:
#                 if self.speech_start_time == 0:
#                     self.speech_start_time = current_time
#                     print("🎯 Bắt đầu phát hiện speech")

#                 speech_duration = current_time - self.speech_start_time
                
#                 # Nếu đủ thời gian speech, gọi callback
#                 if speech_duration >= self.min_speech_duration:
#                     if self.speech_callback:
#                         # Thu thập tất cả audio từ khi bắt đầu speech
#                         full_audio = self._collect_speech_audio()
#                         if len(full_audio) > 0:
#                             self.speech_callback(full_audio, self.sample_rate)
#                             self.speech_start_time = 0
#             else:
#                 if self.speech_start_time > 0:
#                     print("🔇 Kết thúc speech segment")
#                     self.speech_start_time = 0

#         except Exception as e:
#             print(f"❌ Lỗi xử lý Silero VAD chunk: {e}")

#     def _collect_speech_audio(self) -> np.ndarray:
#         """Thu thập toàn bộ audio từ khi bắt đầu speech"""
#         # Trong implementation thực tế, bạn cần lưu lại audio 
#         # từ khi bắt đầu phát hiện speech đến hiện tại
#         # Đây là simplified version
#         min_samples = int(self.sample_rate * self.min_speech_duration)
#         return np.random.randn(min_samples).astype(np.float32)  # Placeholder

#     def _normalize_audio(self, audio: np.ndarray) -> np.ndarray:
#         """Chuẩn hóa audio"""
#         if audio.dtype != np.float32:
#             audio = audio.astype(np.float32)
#             if np.max(np.abs(audio)) > 1.0:
#                 audio = audio / 32768.0
#         return np.clip(audio, -1.0, 1.0)

#     def _get_speech_probability(self, audio_chunk: np.ndarray) -> float:
#         """Trả về xác suất speech - ĐÃ SỬA LỖI"""
#         try:
#             # ✅ Đảm bảo đúng kích thước 512 samples
#             if len(audio_chunk) != self.chunk_size:
#                 # Resize về đúng 512 samples
#                 if len(audio_chunk) > self.chunk_size:
#                     audio_chunk = audio_chunk[:self.chunk_size]
#                 else:
#                     padding = np.zeros(self.chunk_size - len(audio_chunk), dtype=np.float32)
#                     audio_chunk = np.concatenate([audio_chunk, padding])

#             audio_tensor = torch.from_numpy(audio_chunk).float().unsqueeze(0)

#             with torch.no_grad():
#                 return self.model(audio_tensor, self.sample_rate).item()

#         except Exception as e:
#             print(f"❌ Lỗi lấy speech probability: {e}")
#             return 0.0

#     def _resample_audio(self, audio: np.ndarray, orig_sr: int, target_sr: int) -> np.ndarray:
#         """Resample audio"""
#         if orig_sr == target_sr:
#             return audio
#         try:
#             from scipy import signal
#             # Tính số samples mới
#             duration = len(audio) / orig_sr
#             new_length = int(duration * target_sr)
            
#             # Resample
#             resampled_audio = signal.resample(audio, new_length)
#             return resampled_audio.astype(np.float32)
            
#         except ImportError:
#             # Fallback simple resampling
#             orig_len = len(audio)
#             new_len = int(orig_len * target_sr / orig_sr)
#             x_old = np.linspace(0, 1, orig_len)
#             x_new = np.linspace(0, 1, new_len)
#             return np.interp(x_new, x_old, audio).astype(np.float32)
#         except Exception as e:
#             print(f"⚠️ Lỗi resample: {e}")
#             return audio

#     def is_speech(self, audio_chunk: np.ndarray, sample_rate: int) -> bool:
#         """Kiểm tra chunk có phải speech không - ĐÃ SỬA"""
#         if self.model is None:
#             return True
#         try:
#             if sample_rate != self.sample_rate:
#                 audio_chunk = self._resample_audio(audio_chunk, sample_rate, self.sample_rate)
#             audio_chunk = self._normalize_audio(audio_chunk)
            
#             # ✅ Chia thành các chunk 512 samples và kiểm tra trung bình
#             chunk_size = 512
#             speech_probs = []
            
#             for i in range(0, len(audio_chunk), chunk_size):
#                 chunk = audio_chunk[i:i+chunk_size]
#                 if len(chunk) == chunk_size:
#                     prob = self._get_speech_probability(chunk)
#                     speech_probs.append(prob)
            
#             if not speech_probs:
#                 return False
                
#             avg_prob = np.mean(speech_probs)
#             return avg_prob > settings.VAD_THRESHOLD
            
#         except Exception as e:
#             print(f"❌ Lỗi kiểm tra speech: {e}")
#             return True

#     def get_speech_probability(self, audio_chunk: np.ndarray, sample_rate: int) -> float:
#         """Lấy xác suất speech trung bình"""
#         if self.model is None:
#             return 0.0
#         try:
#             if sample_rate != self.sample_rate:
#                 audio_chunk = self._resample_audio(audio_chunk, sample_rate, self.sample_rate)
#             audio_chunk = self._normalize_audio(audio_chunk)
            
#             # Chia thành các chunk 512 samples
#             chunk_size = 512
#             speech_probs = []
            
#             for i in range(0, len(audio_chunk), chunk_size):
#                 chunk = audio_chunk[i:i+chunk_size]
#                 if len(chunk) == chunk_size:
#                     prob = self._get_speech_probability(chunk)
#                     speech_probs.append(prob)
            
#             return np.mean(speech_probs) if speech_probs else 0.0
            
#         except Exception as e:
#             print(f"❌ Lỗi lấy speech probability: {e}")
#             return 0.0
import io
import numpy as np
import soundfile as sf
import time
import traceback
import threading
import queue
import torch
from groq import Groq
from typing import Optional, Dict, Any, Callable
from config.settings import settings

class SileroVAD:
    def __init__(self):
        self.model = None
        self.sample_rate = 16000
        self.is_streaming = False
        self.speech_callback = None
        self.audio_buffer = []
        self.speech_buffer = []  # Buffer cho speech đang diễn ra
        self.state = "silence"  # silence, speech, processing
        self.speech_start_time = 0
        self.last_voice_time = 0
        
        # Cấu hình tối ưu
        self.chunk_size = 512
        self.speech_threshold = settings.VAD_THRESHOLD
        self.min_speech_duration = settings.VAD_MIN_SPEECH_DURATION
        self.min_silence_duration = settings.VAD_MIN_SILENCE_DURATION
        self.speech_pad_duration = settings.VAD_SPEECH_PAD_DURATION
        self.pre_speech_buffer = settings.VAD_PRE_SPEECH_BUFFER
        
        # Buffer cho pre-speech
        self.pre_speech_samples = int(self.pre_speech_buffer * self.sample_rate)
        self.pre_speech_buffer = []
        
        self._initialize_model()

    def _initialize_model(self):
        """Khởi tạo Silero VAD model"""
        try:
            print("🔄 Đang tải Silero VAD model...")
            self.model, utils = torch.hub.load(
                repo_or_dir='snakers4/silero-vad',
                model='silero_vad',
                force_reload=False,
                trust_repo=True
            )
            self.model.eval()
            print("✅ Đã tải Silero VAD model thành công")
        except Exception as e:
            print(f"❌ Lỗi tải Silero VAD model: {e}")
            self.model = None

    def start_stream(self, speech_callback: Callable):
        """Bắt đầu stream với VAD"""
        if self.model is None:
            return False

        self.is_streaming = True
        self.speech_callback = speech_callback
        self.audio_buffer = []
        self.speech_buffer = []
        self.pre_speech_buffer = []
        self.state = "silence"
        self.speech_start_time = 0
        self.last_voice_time = 0
        print("🎙️ Bắt đầu VAD streaming với cấu hình tối ưu...")
        return True

    def stop_stream(self):
        """Dừng stream"""
        self.is_streaming = False
        self.speech_callback = None
        self.audio_buffer = []
        self.speech_buffer = []
        self.pre_speech_buffer = []
        self.state = "silence"
        print("🛑 Đã dừng VAD streaming")

    def process_stream(self, audio_chunk: np.ndarray, sample_rate: int):
        """Xử lý audio chunk với VAD tối ưu"""
        if not self.is_streaming or self.model is None:
            return

        try:
            # Resample nếu cần
            if sample_rate != self.sample_rate:
                audio_chunk = self._resample_audio(audio_chunk, sample_rate, self.sample_rate)

            # Thêm vào buffer chính
            self.audio_buffer.extend(audio_chunk)

            # Xử lý từng chunk
            while len(self.audio_buffer) >= self.chunk_size:
                chunk = self.audio_buffer[:self.chunk_size]
                self._process_vad_chunk(np.array(chunk))
                self.audio_buffer = self.audio_buffer[self.chunk_size:]

        except Exception as e:
            print(f"❌ Lỗi xử lý VAD: {e}")

    def _process_vad_chunk(self, audio_chunk: np.ndarray):
        """Xử lý VAD cho một chunk - TỐI ƯU HÓA"""
        current_time = time.time()
        
        # Chuẩn hóa audio
        audio_chunk = self._normalize_audio(audio_chunk)
        
        # Lấy xác suất speech
        speech_prob = self._get_speech_probability(audio_chunk)
        
        # Logic state machine cải tiến
        if self.state == "silence":
            if speech_prob > self.speech_threshold:
                print("🎯 Bắt đầu phát hiện speech")
                self.state = "speech"
                self.speech_start_time = current_time
                self.last_voice_time = current_time
                # Khởi tạo speech buffer với pre-speech data
                self.speech_buffer = self.pre_speech_buffer.copy()
                self.speech_buffer.extend(audio_chunk)
            else:
                # Lưu pre-speech buffer (giới hạn kích thước)
                self.pre_speech_buffer.extend(audio_chunk)
                if len(self.pre_speech_buffer) > self.pre_speech_samples:
                    self.pre_speech_buffer = self.pre_speech_buffer[-self.pre_speech_samples:]
                
        elif self.state == "speech":
            # Luôn thêm vào speech buffer
            self.speech_buffer.extend(audio_chunk)
            
            # Cập nhật thời gian voice cuối cùng
            if speech_prob > self.speech_threshold:
                self.last_voice_time = current_time
            
            # Kiểm tra kết thúc speech
            silence_duration = current_time - self.last_voice_time
            speech_duration = current_time - self.speech_start_time
            
            # Điều kiện kết thúc: im lặng đủ lâu VÀ đã nói đủ dài
            if (silence_duration >= self.min_silence_duration and 
                speech_duration >= self.min_speech_duration):
                print(f"🔇 Kết thúc speech segment (duration: {speech_duration:.2f}s)")
                self._finalize_speech()
            # Hoặc speech quá dài (timeout)
            elif speech_duration > settings.MAX_AUDIO_DURATION:
                print(f"⏰ Speech timeout ({speech_duration:.2f}s)")
                self._finalize_speech()
                
        elif self.state == "processing":
            # Đang xử lý, không nhận thêm audio
            pass

    def _finalize_speech(self):
        """Hoàn thành xử lý speech segment"""
        if not self.speech_buffer or len(self.speech_buffer) == 0:
            self.state = "silence"
            return
            
        # Chuyển sang state processing để tránh nhận thêm audio
        self.state = "processing"
        
        # Tạo audio array từ buffer
        speech_audio = np.array(self.speech_buffer, dtype=np.float32)
        
        # Gọi callback trong thread riêng
        if self.speech_callback:
            threading.Thread(
                target=self.speech_callback,
                args=(speech_audio, self.sample_rate),
                daemon=True
            ).start()
        
        # Reset buffers nhưng giữ pre-speech
        self.speech_buffer = []
        self.audio_buffer = []
        
        # Quay lại state silence sau khi xử lý
        self.state = "silence"

    def _normalize_audio(self, audio: np.ndarray) -> np.ndarray:
        """Chuẩn hóa audio"""
        if audio.dtype != np.float32:
            audio = audio.astype(np.float32)
            if np.max(np.abs(audio)) > 1.0:
                audio = audio / 32768.0
        return np.clip(audio, -1.0, 1.0)

    def _get_speech_probability(self, audio_chunk: np.ndarray) -> float:
        """Lấy xác suất speech"""
        try:
            if len(audio_chunk) != self.chunk_size:
                return 0.0
                
            audio_tensor = torch.from_numpy(audio_chunk).float().unsqueeze(0)
            with torch.no_grad():
                return self.model(audio_tensor, self.sample_rate).item()
        except Exception as e:
            print(f"❌ Lỗi speech probability: {e}")
            return 0.0

    def _resample_audio(self, audio: np.ndarray, orig_sr: int, target_sr: int) -> np.ndarray:
        """Resample audio"""
        if orig_sr == target_sr:
            return audio
        try:
            from scipy import signal
            duration = len(audio) / orig_sr
            new_length = int(duration * target_sr)
            resampled_audio = signal.resample(audio, new_length)
            return resampled_audio.astype(np.float32)
        except Exception:
            return audio

    def is_speech(self, audio_chunk: np.ndarray, sample_rate: int) -> bool:
        """Kiểm tra speech (cho compatibility)"""
        if self.model is None:
            return True
            
        try:
            if sample_rate != self.sample_rate:
                audio_chunk = self._resample_audio(audio_chunk, sample_rate, self.sample_rate)
            audio_chunk = self._normalize_audio(audio_chunk)
            
            # Kiểm tra multiple chunks
            chunk_size = 512
            speech_probs = []
            
            for i in range(0, len(audio_chunk), chunk_size):
                chunk = audio_chunk[i:i+chunk_size]
                if len(chunk) == chunk_size:
                    prob = self._get_speech_probability(chunk)
                    speech_probs.append(prob)
            
            return np.mean(speech_probs) > self.speech_threshold if speech_probs else False
            
        except Exception as e:
            print(f"❌ Lỗi kiểm tra speech: {e}")
            return True