mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
3.4 KiB
3.4 KiB
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 analysisVADStateenum with 4 states (QUIET, STARTING, SPEAKING, STOPPING)VADParamsconfiguration class for parameter managementVADAnalyzerabstract base class with state machine implementation- Audio volume calculation and exponential smoothing utilities
-
core/providers/vad/silero_onnx.py: Production-ready ONNX implementationSileroOnnxModel: ONNX runtime wrapper for the Silero modelSileroVADAnalyzer: Advanced VAD analyzer with state managementVADProvider: 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_torchparameter
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
VADParamsclass
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:
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:
python test_vad_upgrade.py
Expected output shows:
- ONNX-based VAD loaded successfully
- Correct state transitions
- Proper audio processing
Migration Notes
-
Install ONNX Runtime (recommended):
pip install onnxruntime -
No Code Changes Required: The upgrade is backward compatible
-
Performance Tuning: Adjust parameters based on your use case:
- Lower
thresholdfor more sensitive detection - Increase
stop_secsfor longer pauses between sentences - Adjust
min_volumebased on microphone sensitivity
- Lower
Troubleshooting
- If ONNX model not found, check
model_pathconfiguration - If falling back to PyTorch, ensure
onnxruntimeis installed - For debugging, check logs with tag
core.providers.vad.silero_onnx