Skip to content

Commit 1b3ccff

Browse files
authored
Merge pull request #852 from ftnext/followup-additional-type-inference-840
docs: add additional type hints (follow up #840)
2 parents c87f5da + b377d31 commit 1b3ccff

File tree

2 files changed

+7
-20
lines changed

2 files changed

+7
-20
lines changed

reference/library-reference.rst

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -163,26 +163,12 @@ If ``duration`` is not specified, then it will record until there is no more aud
163163
``recognizer_instance.adjust_for_ambient_noise(source: AudioSource, duration: float = 1) -> None``
164164
--------------------------------------------------------------------------------------------------
165165

166-
Adjusts the energy threshold dynamically using audio from ``source`` (an ``AudioSource`` instance) to account for ambient noise.
166+
.. autofunction:: speech_recognition.Recognizer.adjust_for_ambient_noise
167167

168-
Intended to calibrate the energy threshold with the ambient energy level. Should be used on periods of audio without speech - will stop early if any speech is detected.
168+
``recognizer_instance.listen(source: AudioSource, timeout: Union[float, None] = None, phrase_time_limit: Union[float, None] = None, snowboy_configuration: Union[Tuple[str, Iterable[str]], None] = None, stream: bool = False) -> AudioData``
169+
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
169170

170-
The ``duration`` parameter is the maximum number of seconds that it will dynamically adjust the threshold for before returning. This value should be at least 0.5 in order to get a representative sample of the ambient noise.
171-
172-
``recognizer_instance.listen(source: AudioSource, timeout: Union[float, None] = None, phrase_time_limit: Union[float, None] = None, snowboy_configuration: Union[Tuple[str, Iterable[str]], None] = None) -> AudioData``
173-
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
174-
175-
Records a single phrase from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance, which it returns.
176-
177-
This is done by waiting until the audio has an energy above ``recognizer_instance.energy_threshold`` (the user has started speaking), and then recording until it encounters ``recognizer_instance.pause_threshold`` seconds of non-speaking or there is no more audio input. The ending silence is not included.
178-
179-
The ``timeout`` parameter is the maximum number of seconds that this will wait for a phrase to start before giving up and throwing an ``speech_recognition.WaitTimeoutError`` exception. If ``timeout`` is ``None``, there will be no wait timeout.
180-
181-
The ``phrase_time_limit`` parameter is the maximum number of seconds that this will allow a phrase to continue before stopping and returning the part of the phrase processed before the time limit was reached. The resulting audio will be the phrase cut off at the time limit. If ``phrase_timeout`` is ``None``, there will be no phrase time limit.
182-
183-
The ``snowboy_configuration`` parameter allows integration with `Snowboy <https://snowboy.kitt.ai/>`__, an offline, high-accuracy, power-efficient hotword recognition engine. When used, this function will pause until Snowboy detects a hotword, after which it will unpause. This parameter should either be ``None`` to turn off Snowboy support, or a tuple of the form ``(SNOWBOY_LOCATION, LIST_OF_HOT_WORD_FILES)``, where ``SNOWBOY_LOCATION`` is the path to the Snowboy root directory, and ``LIST_OF_HOT_WORD_FILES`` is a list of paths to Snowboy hotword configuration files (`*.pmdl` or `*.umdl` format).
184-
185-
This operation will always complete within ``timeout + phrase_timeout`` seconds if both are numbers, either by returning the audio data, or by raising a ``speech_recognition.WaitTimeoutError`` exception.
171+
.. autofunction:: speech_recognition.Recognizer.listen
186172

187173
``recognizer_instance.listen_in_background(source: AudioSource, callback: Callable[[Recognizer, AudioData], Any]) -> Callable[bool, None]``
188174
-------------------------------------------------------------------------------------------------------------------------------------------

speech_recognition/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import time
2222
import uuid
2323
import wave
24+
from collections.abc import Iterable
2425
from urllib.error import HTTPError, URLError
2526
from urllib.parse import urlencode
2627
from urllib.request import Request, urlopen
@@ -363,7 +364,7 @@ def record(self, source, duration=None, offset=None):
363364
frames.close()
364365
return AudioData(frame_data, source.SAMPLE_RATE, source.SAMPLE_WIDTH)
365366

366-
def adjust_for_ambient_noise(self, source: AudioSource, duration=1):
367+
def adjust_for_ambient_noise(self, source: AudioSource, duration: float = 1) -> None:
367368
"""
368369
Adjusts the energy threshold dynamically using audio from ``source`` (an ``AudioSource`` instance) to account for ambient noise.
369370
@@ -439,7 +440,7 @@ def snowboy_wait_for_hot_word(self, snowboy_location, snowboy_hot_word_files, so
439440

440441
return b"".join(frames), elapsed_time
441442

442-
def listen(self, source: AudioSource, timeout=None, phrase_time_limit=None, snowboy_configuration=None, stream=False):
443+
def listen(self, source: AudioSource, timeout: float | None = None, phrase_time_limit: float | None = None, snowboy_configuration: tuple[str, Iterable[str]] | None = None, stream: bool = False) -> AudioData:
443444
"""
444445
Records a single phrase from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance, which it returns.
445446

0 commit comments

Comments
 (0)