-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeech.py
More file actions
138 lines (120 loc) · 5.4 KB
/
Copy pathspeech.py
File metadata and controls
138 lines (120 loc) · 5.4 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
138
import speech_recognition as sr
import logging
import time
from threading import Event
import queue
# Event flags for thread synchronization
should_listen = Event()
should_listen.set() # Start in listening mode
processing_event = Event()
class SpeechRecognizer:
def __init__(self):
self.recognizer = sr.Recognizer()
self.setup_recognizer()
self.setup_logging()
self.input_file = "input.txt"
self.audio_queue = queue.Queue()
self.last_sample = time.time()
def setup_logging(self):
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('speech_recognition.log')
]
)
def setup_recognizer(self):
# Optimized settings for better recognition
self.recognizer.energy_threshold = 3000 # Lowered for better sensitivity
self.recognizer.dynamic_energy_threshold = True
self.recognizer.dynamic_energy_adjustment_damping = 0.15
self.recognizer.dynamic_energy_ratio = 1.5
self.recognizer.pause_threshold = 0.6 # Reduced for faster response
self.recognizer.phrase_threshold = 0.3
self.recognizer.non_speaking_duration = 0.4 # Reduced for faster detection
def continuous_listen(self):
microphone = sr.Microphone()
with microphone as source:
print("Adjusting for ambient noise...")
self.recognizer.adjust_for_ambient_noise(source, duration=1)
print("Ready to listen!")
while True:
if not should_listen.is_set():
time.sleep(0.1)
continue
try:
with microphone as source:
print("Listening...") if time.time() - self.last_sample > 1 else None
self.last_sample = time.time()
audio = self.recognizer.listen(
source,
timeout=None, # Remove timeout for continuous listening
phrase_time_limit=5 # Limit phrase length
)
self.audio_queue.put(audio)
self.process_audio()
except sr.WaitTimeoutError:
continue
except Exception as e:
logging.error(f"Error in continuous listening: {e}")
time.sleep(0.5)
continue
def process_audio(self):
while not self.audio_queue.empty():
audio = self.audio_queue.get()
try:
if should_listen.is_set(): # Check again before processing
text = self.recognizer.recognize_google(audio)
if text:
text = text.lower().strip()
print(f"Recognized: {text}")
# Set processing flag before saving
processing_event.set()
should_listen.clear() # Stop listening while processing
self.save_to_file(text)
# Reset listening after processing
should_listen.set() # Allow listening again
except sr.UnknownValueError:
pass
except sr.RequestError as e:
logging.error(f"Could not request results; {e}")
time.sleep(1)
except Exception as e:
logging.error(f"Error processing audio: {e}")
def save_to_file(self, text):
try:
with open(self.input_file, "w", encoding='utf-8') as file:
file.write(text)
logging.info(f"Saved to file: {text}")
except Exception as e:
logging.error(f"Error saving to file: {e}")
def listen_for_file_name(self):
"""Listen for the file name input from the user."""
should_listen.set() # Ensure listening is enabled
while True:
if self.audio_queue.qsize() > 0:
audio = self.audio_queue.get()
try:
file_name = self.recognizer.recognize_google(audio)
print(f"File name recognized: {file_name}") # Debugging statement
should_listen.set() # Reset listening state
return file_name # Return the recognized file name
except sr.UnknownValueError:
print("Could not understand audio, please try again.")
except Exception as e:
print(f"Error recognizing file name: {e}")
return None # Return None if recognition fails
def listen():
"""Main listening loop."""
recognizer = SpeechRecognizer()
recognizer.continuous_listen()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
try:
listen() # Start listening for commands
except KeyboardInterrupt:
logging.info("Speech recognition stopped by user")
except Exception as e:
logging.error(f"Critical error in speech recognition: {e}")
time.sleep(2) # Wait before restarting