xiaozhi-esp32-server/main/xiaozhi-server/VAD_UPGRADE_SUMMARY.md
2025-08-19 15:27:31 +05:30

93 lines
No EOL
3.4 KiB
Markdown

# VAD Upgrade Summary
## Overview
Successfully upgraded the Voice Activity Detection (VAD) system from a basic PyTorch implementation to a production-ready ONNX-based system with advanced features.
## Changes Made
### 1. New Files Created
- **`core/providers/vad/vad_analyzer.py`**: Base classes and utilities for VAD analysis
- `VADState` enum with 4 states (QUIET, STARTING, SPEAKING, STOPPING)
- `VADParams` configuration class for parameter management
- `VADAnalyzer` abstract base class with state machine implementation
- Audio volume calculation and exponential smoothing utilities
- **`core/providers/vad/silero_onnx.py`**: Production-ready ONNX implementation
- `SileroOnnxModel`: ONNX runtime wrapper for the Silero model
- `SileroVADAnalyzer`: Advanced VAD analyzer with state management
- `VADProvider`: Compatible interface with existing system
### 2. Modified Files
- **`core/providers/vad/silero.py`**: Updated to intelligent provider selection
- Automatically uses ONNX if available (preferred)
- Falls back to PyTorch if ONNX runtime not installed
- Configurable via `use_torch` parameter
### 3. Backup Created
- **`core/providers/vad/silero_torch_backup.py`**: Original PyTorch implementation preserved
## Key Improvements
### Performance & Architecture
- **ONNX Runtime**: Better performance, lower latency, reduced memory usage
- **State Machine**: 4-state system for more accurate voice detection
- **Automatic Memory Management**: Model states reset every 5 seconds
### Features
- **Multi-Sample Rate Support**: 8kHz and 16kHz
- **Volume Detection**: Combined confidence + volume thresholds
- **Exponential Smoothing**: Reduces false positives from noise
- **Configurable Parameters**: Clean API via `VADParams` class
### Production Readiness
- **Better Error Handling**: Comprehensive validation and recovery
- **Type Safety**: Proper type hints and Pydantic models
- **Logging**: Detailed debug information for troubleshooting
- **Backward Compatibility**: Works with existing connection interface
## Configuration
The VAD now supports these parameters:
```yaml
vad:
type: "silero"
model_path: "models/snakers4_silero-vad/src/silero_vad/data/silero_vad.onnx" # Optional
threshold: 0.5 # Voice confidence threshold (0.0-1.0)
start_secs: 0.2 # Seconds before confirming voice start
stop_secs: 0.8 # Seconds before confirming voice stop
min_volume: 0.6 # Minimum volume threshold
min_silence_duration_ms: 1000 # Milliseconds of silence before sentence end
use_torch: false # Force PyTorch implementation (default: false)
```
## Testing
Run the test script to verify the implementation:
```bash
python test_vad_upgrade.py
```
Expected output shows:
- ONNX-based VAD loaded successfully
- Correct state transitions
- Proper audio processing
## Migration Notes
1. **Install ONNX Runtime** (recommended):
```bash
pip install onnxruntime
```
2. **No Code Changes Required**: The upgrade is backward compatible
3. **Performance Tuning**: Adjust parameters based on your use case:
- Lower `threshold` for more sensitive detection
- Increase `stop_secs` for longer pauses between sentences
- Adjust `min_volume` based on microphone sensitivity
## Troubleshooting
- If ONNX model not found, check `model_path` configuration
- If falling back to PyTorch, ensure `onnxruntime` is installed
- For debugging, check logs with tag `core.providers.vad.silero_onnx`