Skip to content
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

refactor: Extract PyAudioWrapper.list_microphone_names() #788

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 0 additions & 17 deletions speech_recognition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,6 @@ def get_pyaudio():
raise AttributeError("Could not find PyAudio; check installation")
return pyaudio

@staticmethod
def list_microphone_names():
"""
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 = Microphone.get_pyaudio().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

@staticmethod
def list_working_microphones():
"""
Expand Down
39 changes: 39 additions & 0 deletions speech_recognition/microphone.py
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():
"""
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
14 changes: 14 additions & 0 deletions tests/test_microphone.py
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()