From 7132d4bee729f688316aebfef7d53c55c838024f Mon Sep 17 00:00:00 2001 From: Ankit Aditya <43180246+AnksIndustries@users.noreply.github.com> Date: Wed, 20 May 2020 00:08:48 +0530 Subject: [PATCH] update thread workers works in windows and not tested in other environment added sample rate and chunk size added adjust_for_ambient_noise to remove noise and it works --- examples/threaded_workers.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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