-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_microphone.py
More file actions
98 lines (81 loc) · 3.09 KB
/
test_microphone.py
File metadata and controls
98 lines (81 loc) · 3.09 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
import speech_recognition as sr
import time
print("=" * 60)
print("MICROPHONE TEST & TRANSCRIPTION")
print("=" * 60)
# Initialize recognizer
r = sr.Recognizer()
# List all microphones
print("\n1. Available Microphones:")
print("-" * 60)
try:
mics = sr.Microphone.list_microphone_names()
for i, mic in enumerate(mics):
print(f" [{i}] {mic}")
except Exception as e:
print(f" ERROR: {e}")
# Test microphone
print("\n2. Testing Microphone...")
print("-" * 60)
try:
with sr.Microphone() as source:
print(" ✓ Microphone opened successfully")
print(" Adjusting for ambient noise... (please be quiet)")
r.adjust_for_ambient_noise(source, duration=2)
print(f" ✓ Energy threshold: {r.energy_threshold}")
print(f" ✓ Dynamic threshold: {r.dynamic_energy_threshold}")
print("\n3. Recording Test (3 seconds)...")
print("-" * 60)
print(" SAY SOMETHING NOW!")
audio = r.listen(source, timeout=5, phrase_time_limit=3)
print(f" ✓ Recorded {len(audio.frame_data)} bytes of audio")
print("\n4. Transcription Test...")
print("-" * 60)
# Try Google
try:
print(" Trying Google Speech Recognition...")
text = r.recognize_google(audio)
print(f" ✓ SUCCESS: '{text}'")
except sr.UnknownValueError:
print(" ✗ Google could not understand audio")
except sr.RequestError as e:
print(f" ✗ Google API error: {e}")
# Try Sphinx (offline)
try:
print("\n Trying Sphinx (offline)...")
text = r.recognize_sphinx(audio)
print(f" ✓ SUCCESS: '{text}'")
except sr.UnknownValueError:
print(" ✗ Sphinx could not understand audio")
except Exception as e:
print(f" ✗ Sphinx not available: {e}")
except Exception as e:
print(f" ✗ ERROR: {e}")
print("\n" + "=" * 60)
print("CONTINUOUS LISTENING TEST")
print("=" * 60)
print("Speak continuously. Press Ctrl+C to stop.\n")
try:
with sr.Microphone() as source:
r.adjust_for_ambient_noise(source, duration=1)
print(f"Energy threshold: {r.energy_threshold}")
print("Ready! Start speaking...\n")
while True:
try:
print("[Listening...]", end=" ", flush=True)
audio = r.listen(source, timeout=None, phrase_time_limit=10)
print("[Processing...]", end=" ", flush=True)
text = r.recognize_google(audio)
timestamp = time.strftime("%H:%M:%S")
print(f"\n[{timestamp}] {text}\n")
except sr.UnknownValueError:
print("[?]")
except sr.RequestError as e:
print(f"\n[API Error: {e}]")
break
except KeyboardInterrupt:
print("\n\nStopped by user.")
break
except Exception as e:
print(f"\nError: {e}")
print("\nTest complete!")