xiaozhi-esp32-server/main/xiaozhi-server/scripts/test_ten_vad.py
rahulpscraftech360 5db5e26119 ten VAD added
2025-08-13 11:52:38 +05:30

158 lines
No EOL
4.8 KiB
Python

#!/usr/bin/env python3
"""
Test script for TEN VAD integration
This script tests if TEN VAD is properly installed and working
"""
import sys
import os
import numpy as np
from pathlib import Path
# Add the project root to Python path
project_root = Path(__file__).parent.parent
sys.path.insert(0, str(project_root))
def test_ten_vad_import():
"""Test if TEN VAD can be imported"""
print("🔍 Testing TEN VAD import...")
try:
from ten_vad import TenVAD
print("✅ TEN VAD import successful")
return True
except ImportError as e:
print(f"❌ Failed to import TEN VAD: {e}")
print(" Please install with: pip install ten-vad")
return False
def test_vad_provider():
"""Test if our VAD provider can be created"""
print("🔍 Testing TEN VAD provider...")
try:
from core.providers.vad.ten_vad import VADProvider
# Test configuration
config = {
"model_path": "models/ten-vad",
"sample_rate": 16000,
"frame_size": 512,
"threshold": 0.5,
"threshold_low": 0.2,
"min_silence_duration_ms": 1000,
"frame_window_threshold": 3
}
print(" Creating VAD provider instance...")
vad_provider = VADProvider(config)
print("✅ TEN VAD provider created successfully")
return True
except Exception as e:
print(f"❌ Failed to create TEN VAD provider: {e}")
return False
def test_audio_processing():
"""Test audio processing with dummy data"""
print("🔍 Testing audio processing...")
try:
from core.providers.vad.ten_vad import VADProvider
config = {
"model_path": "models/ten-vad",
"sample_rate": 16000,
"frame_size": 512,
"threshold": 0.5,
"threshold_low": 0.2,
"min_silence_duration_ms": 1000,
"frame_window_threshold": 3
}
vad_provider = VADProvider(config)
# Create a mock connection object
class MockConnection:
def __init__(self):
self.client_audio_buffer = bytearray()
self.client_voice_window = []
self.last_is_voice = False
self.client_have_voice = False
self.last_activity_time = 0
self.client_voice_stop = False
conn = MockConnection()
# Generate dummy Opus packet (this is just for testing the structure)
# In real usage, this would be actual Opus-encoded audio
dummy_opus_packet = b'\x00' * 100 # Dummy data
print(" Note: This test uses dummy audio data")
print(" Real audio processing will happen during actual usage")
print("✅ Audio processing structure test passed")
return True
except Exception as e:
print(f"❌ Audio processing test failed: {e}")
return False
def test_configuration_loading():
"""Test if configuration can be loaded"""
print("🔍 Testing configuration loading...")
try:
from core.utils.vad import create_instance
config = {
"model_path": "models/ten-vad",
"sample_rate": 16000,
"threshold": 0.5,
"threshold_low": 0.2,
}
# Test the factory method
vad_instance = create_instance("ten_vad", config)
print("✅ Configuration loading test passed")
return True
except Exception as e:
print(f"❌ Configuration loading test failed: {e}")
return False
def main():
"""Run all tests"""
print("🚀 Testing TEN VAD Integration")
print("=" * 50)
tests = [
("TEN VAD Import", test_ten_vad_import),
("VAD Provider Creation", test_vad_provider),
("Audio Processing", test_audio_processing),
("Configuration Loading", test_configuration_loading),
]
passed = 0
total = len(tests)
for test_name, test_func in tests:
print(f"\n📋 {test_name}")
print("-" * 30)
if test_func():
passed += 1
else:
print(f" Test failed: {test_name}")
print("\n" + "=" * 50)
print(f"📊 Test Results: {passed}/{total} tests passed")
if passed == total:
print("✅ All tests passed! TEN VAD is ready to use.")
print("\nTo use TEN VAD, update your .config.yaml:")
print(" selected_module:")
print(" VAD: TenVAD")
else:
print("❌ Some tests failed. Please check the errors above.")
print(" Make sure to install TEN VAD: pip install ten-vad")
return passed == total
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)