diff --git a/examples/threaded_workers.py b/examples/threaded_workers.py index 82f2ef9b..2436e55d 100644 --- a/examples/threaded_workers.py +++ b/examples/threaded_workers.py @@ -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: @@ -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