-
Notifications
You must be signed in to change notification settings - Fork 2.4k
refactor: Extract PyAudioWrapper.list_microphone_names() #788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
fff3930
2867316
ff0b8f4
87db628
affab62
1fb4c0c
5cc9cee
ca695b0
2cdbd5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| import pyaudio | ||
|
|
||
|
|
||
| class PyAudioWrapper: | ||
| @staticmethod | ||
| def get_pyaudio() -> pyaudio.PyAudio: | ||
| """Returns pyaudio.PyAudio instance. | ||
|
|
||
| Checks pyaudio's installation, throws exceptions if pyaudio can't be found | ||
| """ | ||
| try: | ||
| import pyaudio | ||
| except ImportError: | ||
| raise AttributeError( | ||
| "Could not find PyAudio; Run `pip install SpeechRecognition[audio]`" | ||
| ) | ||
| return pyaudio.PyAudio() | ||
|
|
||
| @staticmethod | ||
| def list_microphone_names(): | ||
|
Comment on lines
+24
to
+25
|
||
| """ | ||
| Returns a list of the names of all available microphones. For microphones where the name can't be retrieved, the list entry contains ``None`` instead. | ||
|
|
||
| The index of each microphone's name in the returned list is the same as its device index when creating a ``Microphone`` instance - if you want to use the microphone at index 3 in the returned list, use ``Microphone(device_index=3)``. | ||
| """ | ||
| audio = PyAudioWrapper.get_pyaudio() | ||
| try: | ||
| result = [] | ||
| for i in range(audio.get_device_count()): | ||
| device_info = audio.get_device_info_by_index(i) | ||
| result.append(device_info.get("name")) | ||
| finally: | ||
| audio.terminate() | ||
| return result | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,14 @@ | ||||||||||||||||||
| import sys | ||||||||||||||||||
| from unittest.mock import patch | ||||||||||||||||||
|
|
||||||||||||||||||
| import pytest | ||||||||||||||||||
|
|
||||||||||||||||||
| from speech_recognition.microphone import PyAudioWrapper | ||||||||||||||||||
|
|
||||||||||||||||||
|
|
||||||||||||||||||
| @pytest.mark.skipif(sys.platform.startswith("win"), reason="skip on Windows") | ||||||||||||||||||
| class TestPyAudioWrapper: | ||||||||||||||||||
| @patch("pyaudio.PyAudio") | ||||||||||||||||||
| def test_get_pyaudio(self, PyAudio): | ||||||||||||||||||
| assert PyAudioWrapper.get_pyaudio() == PyAudio.return_value | ||||||||||||||||||
| PyAudio.assert_called_once_with() | ||||||||||||||||||
|
Comment on lines
+11
to
+14
|
||||||||||||||||||
| @patch("pyaudio.PyAudio") | |
| def test_get_pyaudio(self, PyAudio): | |
| assert PyAudioWrapper.get_pyaudio() == PyAudio.return_value | |
| PyAudio.assert_called_once_with() | |
| @patch("speech_recognition.microphone.pyaudio") | |
| def test_get_pyaudio(self, mock_pyaudio): | |
| assert PyAudioWrapper.get_pyaudio() == mock_pyaudio.PyAudio.return_value | |
| mock_pyaudio.PyAudio.assert_called_once_with() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing return type annotation. The method should include
-> listor more specifically-> list[str | None]to match the docstring which states it returns "a list of the names" where entries can containNone.