-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_vad.py
More file actions
137 lines (108 loc) · 4.01 KB
/
Copy pathtest_vad.py
File metadata and controls
137 lines (108 loc) · 4.01 KB
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
#!/usr/bin/env python3
"""
Test script for Voice Activity Detection and Microphone Testing features
"""
import numpy as np
import sounddevice as sd
import time
import sys
import os
# Add current directory to path to import hotmic
sys.path.insert(0, os.path.dirname(__file__))
from hotmic import Config, calculate_audio_energy, test_microphone
def test_energy_calculation():
"""Test audio energy calculation"""
print("=" * 60)
print("Testing audio energy calculation...")
print("=" * 60)
# Test with silent audio
silent = np.zeros(1000, dtype=np.int16).tobytes()
energy_silent = calculate_audio_energy(silent)
print(f"Silent audio energy: {energy_silent:.2f} (should be ~0)")
# Test with noise
noise = (np.random.randint(-1000, 1000, 1000, dtype=np.int16)).tobytes()
energy_noise = calculate_audio_energy(noise)
print(f"Noise audio energy: {energy_noise:.2f}")
# Test with loud audio
loud = (np.random.randint(-10000, 10000, 1000, dtype=np.int16)).tobytes()
energy_loud = calculate_audio_energy(loud)
print(f"Loud audio energy: {energy_loud:.2f}")
print("\n[PASS] Audio energy calculation test passed!\n")
def test_vad_settings():
"""Test VAD configuration settings"""
print("=" * 60)
print("Testing VAD configuration...")
print("=" * 60)
# Create a test config
test_config = {
"endpoint": "wss://f.gpty.ai/api/v1/ws",
"hotkey": "<ctrl>+u",
"autopaste": True,
"samplerate": 48000,
"channels": 1,
"block_samples": 24000,
"input_device": None,
"connect_timeout": 8.0,
"stop_flush_wait": 0.5,
"enable_vad": True,
"vad_energy_threshold": 500.0,
"vad_min_speech_duration": 0.3,
"silence_timeout": 2.0,
"test_mic_on_startup": False, # Disable for this test
"enable_hold_mode": False,
"enable_mouse_button": False,
"show_visual_indicator": False
}
# Write test config
import json
with open("test_config.json", "w") as f:
json.dump(test_config, f, indent=2)
# Load config
from hotmic import load_config
cfg = load_config("test_config.json")
print(f"VAD enabled: {cfg.enable_vad}")
print(f"VAD energy threshold: {cfg.vad_energy_threshold}")
print(f"VAD min speech duration: {cfg.vad_min_speech_duration}")
print(f"Silence timeout: {cfg.silence_timeout}")
print(f"Test mic on startup: {cfg.test_mic_on_startup}")
# Clean up
os.remove("test_config.json")
print("\n[PASS] VAD configuration test passed!\n")
def test_microphone_availability():
"""Test if microphone is available"""
print("=" * 60)
print("Testing microphone availability...")
print("=" * 60)
devices = sd.query_devices()
print(f"\nAvailable audio devices:")
for i, device in enumerate(devices):
if device['max_input_channels'] > 0:
print(f" [{i}] {device['name']} (inputs: {device['max_input_channels']})")
default_input = sd.query_devices(kind='input')
print(f"\nDefault input device: {default_input['name']}")
print(f"Sample rate: {default_input['default_samplerate']}")
print(f"Channels: {default_input['max_input_channels']}")
print("\n[PASS] Microphone availability test passed!\n")
def main():
print("\n" + "=" * 60)
print("HotMic VAD and Microphone Testing Suite")
print("=" * 60 + "\n")
try:
# Run tests
test_energy_calculation()
test_vad_settings()
test_microphone_availability()
print("=" * 60)
print("All tests passed!")
print("=" * 60)
print("\nYou can now run the main program with: python hotmic.py")
print("\nNote: The microphone test will run automatically on startup")
print(" if 'test_mic_on_startup' is enabled in config.json\n")
except Exception as e:
print(f"\n[ERROR] Error during testing: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
sys.exit(main())