File size: 25,880 Bytes
6963d43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
#!/usr/bin/env python
"""ONNX inference script for encoder_only_decoder VAD model - Silero-style implementation.

This implementation follows Silero VAD's architecture for cleaner, more efficient processing:
- Fixed-size chunk processing for consistent behavior
- State management for streaming capability
- Hysteresis-based speech detection (dual threshold)
- Simplified segment extraction with proper padding
"""

import argparse
import json
import os
import time
import warnings
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple

import librosa
import numpy as np
import torch
from transformers import WhisperFeatureExtractor


class WhisperVADOnnxWrapper:
    """ONNX wrapper for Whisper-based VAD model following Silero's architecture."""

    def __init__(
        self,
        model_path: str,
        metadata_path: Optional[str] = None,
        force_cpu: bool = False,
        num_threads: int = 1,
    ):
        """Initialize ONNX model wrapper.

        Args:
            model_path: Path to ONNX model file
            metadata_path: Path to metadata JSON file (optional)
            force_cpu: Force CPU execution even if GPU is available
            num_threads: Number of CPU threads for inference
        """
        try:
            import onnxruntime as ort
        except ImportError:
            raise ImportError(
                "onnxruntime not installed. Install with:\n"
                "  pip install onnxruntime      # For CPU\n"
                "  pip install onnxruntime-gpu  # For GPU"
            )

        self.model_path = model_path

        # Load metadata
        if metadata_path is None:
            metadata_path = model_path.replace('.onnx', '_metadata.json')

        if os.path.exists(metadata_path):
            with open(metadata_path, 'r') as f:
                self.metadata = json.load(f)
        else:
            warnings.warn("No metadata file found. Using default values.")
            self.metadata = {
                'whisper_model_name': 'openai/whisper-base',
                'frame_duration_ms': 20,
                'total_duration_ms': 30000,
            }

        # Initialize feature extractor
        self.feature_extractor = WhisperFeatureExtractor.from_pretrained(
            self.metadata['whisper_model_name']
        )

        # Set up ONNX Runtime session
        opts = ort.SessionOptions()
        opts.inter_op_num_threads = num_threads
        opts.intra_op_num_threads = num_threads

        providers = ['CPUExecutionProvider']
        if not force_cpu and 'CUDAExecutionProvider' in ort.get_available_providers():
            providers.insert(0, 'CUDAExecutionProvider')

        self.session = ort.InferenceSession(model_path, providers=providers, sess_options=opts)

        # Get input/output info
        self.input_name = self.session.get_inputs()[0].name
        self.output_names = [out.name for out in self.session.get_outputs()]

        # Model parameters
        self.sample_rate = 16000  # Whisper uses 16kHz
        self.frame_duration_ms = self.metadata.get('frame_duration_ms', 20)
        self.chunk_duration_ms = self.metadata.get('total_duration_ms', 30000)
        self.chunk_samples = int(self.chunk_duration_ms * self.sample_rate / 1000)
        self.frames_per_chunk = int(self.chunk_duration_ms / self.frame_duration_ms)

        # Initialize state
        self.reset_states()

        print(f"Model loaded: {model_path}")
        print(f"  Providers: {providers}")
        print(f"  Chunk duration: {self.chunk_duration_ms}ms")
        print(f"  Frame duration: {self.frame_duration_ms}ms")

    def reset_states(self):
        """Reset internal states for new audio stream."""
        self._context = None
        self._last_chunk = None

    def _validate_input(self, audio: np.ndarray, sr: int) -> np.ndarray:
        """Validate and preprocess input audio.

        Args:
            audio: Input audio array
            sr: Sample rate

        Returns:
            Preprocessed audio at 16kHz
        """
        if audio.ndim > 1:
            # Convert to mono if multi-channel
            audio = audio.mean(axis=0 if audio.shape[0] > audio.shape[1] else 1)

        # Resample if needed
        if sr != self.sample_rate:
            import librosa
            audio = librosa.resample(audio, orig_sr=sr, target_sr=self.sample_rate)

        return audio

    def __call__(self, audio_chunk: np.ndarray, sr: int = 16000) -> np.ndarray:
        """Process a single audio chunk.

        Args:
            audio_chunk: Audio chunk to process
            sr: Sample rate

        Returns:
            Frame-level speech probabilities
        """
        # Validate input
        audio_chunk = self._validate_input(audio_chunk, sr)

        # Ensure chunk is correct size
        if len(audio_chunk) < self.chunk_samples:
            audio_chunk = np.pad(
                audio_chunk,
                (0, self.chunk_samples - len(audio_chunk)),
                mode='constant'
            )
        elif len(audio_chunk) > self.chunk_samples:
            audio_chunk = audio_chunk[:self.chunk_samples]

        # Extract features
        inputs = self.feature_extractor(
            audio_chunk,
            sampling_rate=self.sample_rate,
            return_tensors="np"
        )

        # Run inference
        outputs = self.session.run(
            self.output_names,
            {self.input_name: inputs.input_features}
        )

        # Apply sigmoid to get probabilities
        frame_logits = outputs[0][0]  # Remove batch dimension
        frame_probs = 1 / (1 + np.exp(-frame_logits))

        return frame_probs

    def audio_forward(self, audio: np.ndarray, sr: int = 16000) -> np.ndarray:
        """Process full audio file in chunks (Silero-style).

        Args:
            audio: Full audio array
            sr: Sample rate

        Returns:
            Concatenated frame probabilities for entire audio
        """
        audio = self._validate_input(audio, sr)
        self.reset_states()

        all_probs = []

        # Process in chunks
        for i in range(0, len(audio), self.chunk_samples):
            chunk = audio[i:i + self.chunk_samples]

            # Pad last chunk if needed
            if len(chunk) < self.chunk_samples:
                chunk = np.pad(chunk, (0, self.chunk_samples - len(chunk)), mode='constant')

            # Get predictions for chunk
            chunk_probs = self.__call__(chunk, self.sample_rate)
            all_probs.append(chunk_probs)

        # Concatenate all probabilities
        if all_probs:
            return np.concatenate(all_probs)
        return np.array([])


def get_speech_timestamps(
    audio: np.ndarray,
    model,
    threshold: float = 0.5,
    sampling_rate: int = 16000,
    min_speech_duration_ms: int = 250,
    max_speech_duration_s: float = float('inf'),
    min_silence_duration_ms: int = 100,
    speech_pad_ms: int = 30,
    return_seconds: bool = False,
    neg_threshold: Optional[float] = None,
    progress_tracking_callback: Optional[Callable[[float], None]] = None,
) -> List[Dict[str, float]]:
    """Extract speech timestamps from audio using Silero-style processing.

    This function implements Silero VAD's approach with:
    - Dual threshold (positive and negative) for hysteresis
    - Proper segment padding
    - Minimum duration filtering
    - Maximum duration handling with intelligent splitting

    Args:
        audio: Input audio array
        model: VAD model (WhisperVADOnnxWrapper instance)
        threshold: Speech threshold (default: 0.5)
        sampling_rate: Audio sample rate
        min_speech_duration_ms: Minimum speech segment duration
        max_speech_duration_s: Maximum speech segment duration
        min_silence_duration_ms: Minimum silence to split segments
        speech_pad_ms: Padding to add to speech segments
        return_seconds: Return times in seconds vs samples
        neg_threshold: Negative threshold for hysteresis (default: threshold - 0.15)
        progress_tracking_callback: Progress callback function

    Returns:
        List of speech segments with start/end times
    """
    # Convert to numpy if torch tensor
    if torch.is_tensor(audio):
        audio = audio.numpy()

    # Validate audio
    if audio.ndim > 1:
        audio = audio.mean(axis=0 if audio.shape[0] > audio.shape[1] else 1)

    # Get frame probabilities for entire audio
    model.reset_states()
    speech_probs = model.audio_forward(audio, sampling_rate)

    # Calculate frame parameters
    frame_duration_ms = model.frame_duration_ms
    frame_samples = int(sampling_rate * frame_duration_ms / 1000)

    # Convert durations to frames
    min_speech_frames = int(min_speech_duration_ms / frame_duration_ms)
    min_silence_frames = int(min_silence_duration_ms / frame_duration_ms)
    speech_pad_frames = int(speech_pad_ms / frame_duration_ms)
    max_speech_frames = int(max_speech_duration_s * 1000 / frame_duration_ms) if max_speech_duration_s != float('inf') else len(speech_probs)

    # Set negative threshold for hysteresis
    if neg_threshold is None:
        neg_threshold = max(threshold - 0.15, 0.01)

    # Track speech segments
    triggered = False
    speeches = []
    current_speech = {}
    current_probs = []  # Track probabilities for current segment
    temp_end = 0

    # Process each frame
    for i, speech_prob in enumerate(speech_probs):
        # Report progress
        if progress_tracking_callback:
            progress = (i + 1) / len(speech_probs) * 100
            progress_tracking_callback(progress)

        # Track probabilities for current segment
        if triggered:
            current_probs.append(float(speech_prob))

        # Speech onset detection
        if speech_prob >= threshold and not triggered:
            triggered = True
            current_speech['start'] = i
            current_probs = [float(speech_prob)]  # Start tracking probabilities
            continue

        # Check for maximum speech duration
        if triggered and 'start' in current_speech:
            duration = i - current_speech['start']
            if duration > max_speech_frames:
                # Force end segment at max duration
                current_speech['end'] = current_speech['start'] + max_speech_frames
                # Calculate probability statistics for segment
                if current_probs:
                    current_speech['avg_prob'] = np.mean(current_probs)
                    current_speech['min_prob'] = np.min(current_probs)
                    current_speech['max_prob'] = np.max(current_probs)
                speeches.append(current_speech)
                current_speech = {}
                current_probs = []
                triggered = False
                temp_end = 0
                continue

        # Speech offset detection with hysteresis
        if speech_prob < neg_threshold and triggered:
            if not temp_end:
                temp_end = i

            # Check if silence is long enough
            if i - temp_end >= min_silence_frames:
                # End current speech segment
                current_speech['end'] = temp_end

                # Check minimum duration
                if current_speech['end'] - current_speech['start'] >= min_speech_frames:
                    # Calculate probability statistics for segment
                    if current_probs:
                        current_speech['avg_prob'] = np.mean(current_probs[:temp_end - current_speech['start']])
                        current_speech['min_prob'] = np.min(current_probs[:temp_end - current_speech['start']])
                        current_speech['max_prob'] = np.max(current_probs[:temp_end - current_speech['start']])
                    speeches.append(current_speech)

                current_speech = {}
                current_probs = []
                triggered = False
                temp_end = 0

        # Reset temp_end if speech resumes
        elif speech_prob >= threshold and temp_end:
            temp_end = 0

    # Handle speech that continues to the end
    if triggered and 'start' in current_speech:
        current_speech['end'] = len(speech_probs)
        if current_speech['end'] - current_speech['start'] >= min_speech_frames:
            # Calculate probability statistics for segment
            if current_probs:
                current_speech['avg_prob'] = np.mean(current_probs)
                current_speech['min_prob'] = np.min(current_probs)
                current_speech['max_prob'] = np.max(current_probs)
            speeches.append(current_speech)

    # Apply padding to segments
    for i, speech in enumerate(speeches):
        # Add padding
        if i == 0:
            speech['start'] = max(0, speech['start'] - speech_pad_frames)
        else:
            speech['start'] = max(speeches[i-1]['end'], speech['start'] - speech_pad_frames)

        if i < len(speeches) - 1:
            speech['end'] = min(speeches[i+1]['start'], speech['end'] + speech_pad_frames)
        else:
            speech['end'] = min(len(speech_probs), speech['end'] + speech_pad_frames)

    # Convert to time units
    if return_seconds:
        for speech in speeches:
            speech['start'] = speech['start'] * frame_duration_ms / 1000
            speech['end'] = speech['end'] * frame_duration_ms / 1000
    else:
        # Convert frames to samples
        for speech in speeches:
            speech['start'] = speech['start'] * frame_samples
            speech['end'] = speech['end'] * frame_samples

    return speeches


class VADIterator:
    """Stream iterator for real-time VAD processing (Silero-style)."""

    def __init__(
        self,
        model,
        threshold: float = 0.5,
        sampling_rate: int = 16000,
        min_silence_duration_ms: int = 100,
        speech_pad_ms: int = 30,
    ):
        """Initialize VAD iterator for streaming.

        Args:
            model: WhisperVADOnnxWrapper instance
            threshold: Speech detection threshold
            sampling_rate: Audio sample rate
            min_silence_duration_ms: Minimum silence duration
            speech_pad_ms: Speech padding in milliseconds
        """
        self.model = model
        self.threshold = threshold
        self.neg_threshold = max(threshold - 0.15, 0.01)
        self.sampling_rate = sampling_rate

        # Calculate frame-based parameters
        self.frame_duration_ms = model.frame_duration_ms
        self.min_silence_frames = min_silence_duration_ms / self.frame_duration_ms
        self.speech_pad_frames = speech_pad_ms / self.frame_duration_ms

        self.reset_states()

    def reset_states(self):
        """Reset iterator state."""
        self.model.reset_states()
        self.triggered = False
        self.temp_end = 0
        self.current_frame = 0
        self.buffer = np.array([])
        self.speech_start = 0

    def __call__(self, audio_chunk: np.ndarray, return_seconds: bool = False) -> Optional[Dict]:
        """Process audio chunk and detect speech boundaries.

        Args:
            audio_chunk: Audio chunk to process
            return_seconds: Return times in seconds vs samples

        Returns:
            Dict with 'start' or 'end' key when speech boundary detected
        """
        # Add to buffer
        self.buffer = np.concatenate([self.buffer, audio_chunk]) if len(self.buffer) > 0 else audio_chunk

        # Check if we have enough samples for a full chunk
        if len(self.buffer) < self.model.chunk_samples:
            return None

        # Process full chunk
        chunk = self.buffer[:self.model.chunk_samples]
        self.buffer = self.buffer[self.model.chunk_samples:]

        # Get frame predictions
        frame_probs = self.model(chunk, self.sampling_rate)

        results = []

        # Process each frame
        for prob in frame_probs:
            self.current_frame += 1

            # Speech onset
            if prob >= self.threshold and not self.triggered:
                self.triggered = True
                self.speech_start = self.current_frame - self.speech_pad_frames
                start_time = max(0, self.speech_start * self.frame_duration_ms / 1000) if return_seconds else \
                            max(0, self.speech_start * self.frame_duration_ms * 16)
                return {'start': start_time}

            # Speech offset
            if prob < self.neg_threshold and self.triggered:
                if not self.temp_end:
                    self.temp_end = self.current_frame
                elif self.current_frame - self.temp_end >= self.min_silence_frames:
                    # End speech
                    end_frame = self.temp_end + self.speech_pad_frames
                    end_time = end_frame * self.frame_duration_ms / 1000 if return_seconds else \
                              end_frame * self.frame_duration_ms * 16
                    self.triggered = False
                    self.temp_end = 0
                    return {'end': end_time}
            elif prob >= self.threshold and self.temp_end:
                self.temp_end = 0

        return None


def load_audio(audio_path: str, sampling_rate: int = 16000) -> np.ndarray:
    """Load audio file and convert to target sample rate.

    Args:
        audio_path: Path to audio file
        sampling_rate: Target sample rate

    Returns:
        Audio array at target sample rate
    """
    audio, sr = librosa.load(audio_path, sr=sampling_rate)
    return audio


def save_segments(segments: List[Dict], output_path: str, format: str = 'json'):
    """Save speech segments to file.

    Args:
        segments: List of speech segments
        output_path: Output file path
        format: Output format (json, txt, csv, srt)
    """
    if format == 'json':
        with open(output_path, 'w') as f:
            json.dump({'segments': segments}, f, indent=2)

    elif format == 'txt':
        with open(output_path, 'w') as f:
            for i, seg in enumerate(segments, 1):
                start = seg['start']
                end = seg['end']
                duration = end - start
                f.write(f"{i:3d}. {start:8.3f}s - {end:8.3f}s (duration: {duration:6.3f}s)\n")

    elif format == 'csv':
        import csv
        with open(output_path, 'w', newline='') as f:
            writer = csv.DictWriter(f, fieldnames=['start', 'end', 'duration'])
            writer.writeheader()
            for seg in segments:
                row = {
                    'start': seg['start'],
                    'end': seg['end'],
                    'duration': seg['end'] - seg['start']
                }
                writer.writerow(row)

    elif format == 'srt':
        with open(output_path, 'w') as f:
            for i, seg in enumerate(segments, 1):
                start_s = seg['start']
                end_s = seg['end']

                # Convert to SRT timestamp format
                def seconds_to_srt(seconds):
                    hours = int(seconds // 3600)
                    minutes = int((seconds % 3600) // 60)
                    secs = int(seconds % 60)
                    millis = int((seconds % 1) * 1000)
                    return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"

                f.write(f"{i}\n")
                f.write(f"{seconds_to_srt(start_s)} --> {seconds_to_srt(end_s)}\n")

                # Write speech probability information if available
                if 'avg_prob' in seg:
                    f.write(f"Speech [Avg: {seg['avg_prob']:.2%}, Min: {seg['min_prob']:.2%}, Max: {seg['max_prob']:.2%}]\n\n")
                else:
                    f.write(f"[Speech]\n\n")


def main():
    parser = argparse.ArgumentParser(
        description='Silero-style ONNX inference for Whisper-based VAD model'
    )
    parser.add_argument('--model', required=True, help='Path to ONNX model file')
    parser.add_argument('--audio', required=True, help='Path to audio file')
    parser.add_argument('--output', help='Output file path (default: audio_path.vad.json)')
    parser.add_argument('--format', choices=['json', 'txt', 'csv', 'srt'],
                      default='json', help='Output format')
    parser.add_argument('--threshold', type=float, default=0.5,
                      help='Speech detection threshold (0.0-1.0)')
    parser.add_argument('--neg-threshold', type=float, default=None,
                      help='Negative threshold for hysteresis (default: threshold - 0.15)')
    parser.add_argument('--min-speech-duration', type=int, default=250,
                      help='Minimum speech duration in ms')
    parser.add_argument('--min-silence-duration', type=int, default=100,
                      help='Minimum silence duration in ms')
    parser.add_argument('--speech-pad', type=int, default=30,
                      help='Speech padding in ms')
    parser.add_argument('--max-speech-duration', type=float, default=float('inf'),
                      help='Maximum speech duration in seconds')
    parser.add_argument('--metadata', help='Path to metadata JSON file')
    parser.add_argument('--force-cpu', action='store_true',
                      help='Force CPU execution even if GPU is available')
    parser.add_argument('--threads', type=int, default=1,
                      help='Number of CPU threads')
    parser.add_argument('--stream', action='store_true',
                      help='Use streaming mode (demonstrate VADIterator)')

    args = parser.parse_args()

    # Check files exist
    if not os.path.exists(args.model):
        print(f"Error: Model file not found: {args.model}")
        return 1

    if not os.path.exists(args.audio):
        print(f"Error: Audio file not found: {args.audio}")
        return 1

    try:
        # Initialize model
        print("Loading model...")
        model = WhisperVADOnnxWrapper(
            model_path=args.model,
            metadata_path=args.metadata,
            force_cpu=args.force_cpu,
            num_threads=args.threads,
        )

        # Load audio
        print(f"Loading audio: {args.audio}")
        audio = load_audio(args.audio)
        duration = len(audio) / 16000
        print(f"Audio duration: {duration:.2f}s")

        if args.stream:
            # Demonstrate streaming mode
            print("\nUsing streaming mode (VADIterator)...")
            vad_iterator = VADIterator(
                model=model,
                threshold=args.threshold,
                min_silence_duration_ms=args.min_silence_duration,
                speech_pad_ms=args.speech_pad,
            )

            # Simulate streaming by processing in small chunks
            chunk_size = 16000  # 1 second chunks
            segments = []
            current_segment = {}

            for i in range(0, len(audio), chunk_size):
                chunk = audio[i:i + chunk_size]
                result = vad_iterator(chunk, return_seconds=True)

                if result:
                    if 'start' in result:
                        current_segment = {'start': result['start'] + i/16000}
                        print(f"  Speech started: {current_segment['start']:.2f}s")
                    elif 'end' in result and current_segment:
                        current_segment['end'] = result['end'] + i/16000
                        segments.append(current_segment)
                        print(f"  Speech ended: {current_segment['end']:.2f}s")
                        current_segment = {}

            # Handle ongoing speech at end
            if current_segment and 'start' in current_segment:
                current_segment['end'] = duration
                segments.append(current_segment)
        else:
            # Use batch mode with Silero-style processing
            print("\nProcessing with Silero-style speech detection...")

            # Progress callback
            def progress_callback(percent):
                print(f"\rProgress: {percent:.1f}%", end='', flush=True)

            # Get speech timestamps
            segments = get_speech_timestamps(
                audio=audio,
                model=model,
                threshold=args.threshold,
                sampling_rate=16000,
                min_speech_duration_ms=args.min_speech_duration,
                min_silence_duration_ms=args.min_silence_duration,
                speech_pad_ms=args.speech_pad,
                max_speech_duration_s=args.max_speech_duration,
                return_seconds=True,
                neg_threshold=args.neg_threshold,
                progress_tracking_callback=progress_callback,
            )
            print()  # New line after progress

        # Display results
        print(f"\nFound {len(segments)} speech segments:")
        total_speech = sum(seg['end'] - seg['start'] for seg in segments)
        print(f"Total speech: {total_speech:.2f}s ({total_speech/duration*100:.1f}%)")

        if segments:
            print("\nSegments:")
            for i, seg in enumerate(segments[:10], 1):  # Show first 10
                duration_seg = seg['end'] - seg['start']
                print(f"  {i:2d}. {seg['start']:7.3f}s - {seg['end']:7.3f}s (duration: {duration_seg:5.3f}s)")
            if len(segments) > 10:
                print(f"  ... and {len(segments) - 10} more segments")

        # Save results
        output_path = args.output
        if not output_path:
            base = os.path.splitext(args.audio)[0]
            output_path = f"{base}.vad.{args.format}"

        save_segments(segments, output_path, format=args.format)
        print(f"\nResults saved to: {output_path}")

    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()
        return 1

    return 0


if __name__ == '__main__':
    exit(main())