Skip to content

Commit 257c140

Browse files
committed
Update GitHub issue template, add threaded workers example, update documentation for listen_in_background
1 parent 301f889 commit 257c140

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

.github/ISSUE_TEMPLATE.md

+2
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@ My **SpeechRecognition library version** is <INSERT VERSION HERE>. (You can chec
3434

3535
My **PyAudio library version** is <INSERT VERSION HERE> / I don't have PyAudio installed. (You can check this by running `python -c "import pyaudio as p;print(p.__version__)"`.)
3636

37+
My **microphones** are: (You can check this by running `python -c "import speech_recognition as sr;print(sr.Microphone.list_microphone_names())"`.)
38+
3739
I **installed PocketSphinx from** <INSERT SOURCE HERE>. (For example, from the Debian repositories, from Homebrew, or from the source code.)

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ fr-FR.zip
99
zh-CN.zip
1010
it-IT.zip
1111
pocketsphinx-python/
12-
venv/
12+
examples/TEST.py

examples/threaded_workers.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python3
2+
3+
# NOTE: this example requires PyAudio because it uses the Microphone class
4+
5+
from queue import Queue
6+
7+
import speech_recognition as sr
8+
9+
10+
r = sr.Recognizer()
11+
audio_queue = Queue()
12+
13+
def recognize_worker():
14+
# this runs in a background thread
15+
while True:
16+
audio = audio_queue.get() # retrieve the next audio processing job from the main thread
17+
if audio is None: break # stop processing if the main thread is done
18+
19+
# received audio data, now we'll recognize it using Google Speech Recognition
20+
try:
21+
# for testing purposes, we're just using the default API key
22+
# to use another API key, use `r.recognize_google(audio, key="GOOGLE_SPEECH_RECOGNITION_API_KEY")`
23+
# instead of `r.recognize_google(audio)`
24+
print("Google Speech Recognition thinks you said " + recognizer.recognize_google(audio))
25+
except sr.UnknownValueError:
26+
print("Google Speech Recognition could not understand audio")
27+
except sr.RequestError as e:
28+
print("Could not request results from Google Speech Recognition service; {0}".format(e))
29+
30+
audio_queue.task_done() # mark the audio processing job as completed in the queue
31+
32+
# start a new thread to recognize audio, while this thread focuses on listening
33+
recognize_thread = threading.Thread(target=recognize_worker)
34+
recognize_thread.daemon = True
35+
recognize_thread.start()
36+
with sr.Microphone() as source:
37+
try:
38+
while True: # repeatedly listen for phrases and put the resulting audio on the audio processing job queue
39+
audio_queue.put(r.listen(source))
40+
except KeyboardInterrupt: # allow Ctrl + C to shut down the program
41+
pass
42+
43+
audio_queue.join() # block until all current audio processing jobs are done
44+
audio_queue.put(None) # tell the recognize_thread to stop
45+
recognize_thread.join() # wait for the recognize_thread to actually stop

reference/library-reference.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ This operation will always complete within ``timeout + phrase_timeout`` seconds
170170

171171
Spawns a thread to repeatedly record phrases from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance and call ``callback`` with that ``AudioData`` instance as soon as each phrase are detected.
172172

173-
Returns a function object that, when called from the current thread, requests that the background listener thread stop. The background thread is a daemon and will not stop the program from exiting if there are no other non-daemon threads. The function accepts one parameter, ``wait_for_stop``: if truthy, the function will wait for the background listener to stop before returning, otherwise it will return immediately and the background listener thread might still be running for a second or two afterwards.
173+
Returns a function object that, when called, requests that the background listener thread stop. The background thread is a daemon and will not stop the program from exiting if there are no other non-daemon threads. The function accepts one parameter, ``wait_for_stop``: if truthy, the function will wait for the background listener to stop before returning, otherwise it will return immediately and the background listener thread might still be running for a second or two afterwards. Additionally, if you are using a truthy value for ``wait_for_stop``, you must call the function from the same thread you originally called ``listen_in_background`` from.
174174

175175
Phrase recognition uses the exact same mechanism as ``recognizer_instance.listen(source)``. The ``phrase_time_limit`` parameter works in the same way as the ``phrase_time_limit`` parameter for ``recognizer_instance.listen(source)``, as well.
176176

speech_recognition/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def listen_in_background(self, source, callback, phrase_time_limit=None):
723723
"""
724724
Spawns a thread to repeatedly record phrases from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance and call ``callback`` with that ``AudioData`` instance as soon as each phrase are detected.
725725
726-
Returns a function object that, when called from the current thread, requests that the background listener thread stop. The background thread is a daemon and will not stop the program from exiting if there are no other non-daemon threads. The function accepts one parameter, ``wait_for_stop``: if truthy, the function will wait for the background listener to stop before returning, otherwise it will return immediately and the background listener thread might still be running for a second or two afterwards.
726+
Returns a function object that, when called, requests that the background listener thread stop. The background thread is a daemon and will not stop the program from exiting if there are no other non-daemon threads. The function accepts one parameter, ``wait_for_stop``: if truthy, the function will wait for the background listener to stop before returning, otherwise it will return immediately and the background listener thread might still be running for a second or two afterwards. Additionally, if you are using a truthy value for ``wait_for_stop``, you must call the function from the same thread you originally called ``listen_in_background`` from.
727727
728728
Phrase recognition uses the exact same mechanism as ``recognizer_instance.listen(source)``. The ``phrase_time_limit`` parameter works in the same way as the ``phrase_time_limit`` parameter for ``recognizer_instance.listen(source)``, as well.
729729

0 commit comments

Comments
 (0)