Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions examples/threaded_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

import speech_recognition as sr

Sample_Rate = 48000
Chunk_Size = 1024

r = sr.Recognizer()
audio_queue = Queue()


def recognize_worker():
# this runs in a background thread
while True:
Expand All @@ -34,15 +35,17 @@ def recognize_worker():

audio_queue.task_done() # mark the audio processing job as completed in the queue


# start a new thread to recognize audio, while this thread focuses on listening
recognize_thread = Thread(target=recognize_worker)
recognize_thread.daemon = True
recognize_thread.start()
with sr.Microphone() as source:
with sr.Microphone(sample_rate=Sample_Rate,chunk_size=Chunk_Size) as source:
r.adjust_for_ambient_noise(source)
try:
while True: # repeatedly listen for phrases and put the resulting audio on the audio processing job queue
print("Listening...")
audio_queue.put(r.listen(source))

except KeyboardInterrupt: # allow Ctrl + C to shut down the program
pass

Expand Down