Skip to content
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():
Copy link

Copilot AI Nov 19, 2025

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 -> list or more specifically -> list[str | None] to match the docstring which states it returns "a list of the names" where entries can contain None.

Suggested change
def list_microphone_names():
def list_microphone_names() -> list[str | None]:

Copilot uses AI. Check for mistakes.
Comment on lines +24 to +25
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The refactoring breaks the public API. The Microphone.list_microphone_names() method is documented and used in examples (see README.rst line 221 and reference/library-reference.rst). After this change, calling sr.Microphone.list_microphone_names() will fail with an AttributeError. Consider either:

  1. Adding list_microphone_names = PyAudioWrapper.list_microphone_names as a class attribute in the Microphone class, or
  2. Importing and re-exporting the method from the microphone module in __init__.py

Copilot uses AI. Check for mistakes.
"""
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()
Comment on lines +11 to +14
Copy link

Copilot AI Nov 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch target is incorrect. Since get_pyaudio() imports pyaudio locally within the function (line 17 in microphone.py), patching pyaudio.PyAudio at the module level won't work. The patch should target speech_recognition.microphone.pyaudio.PyAudio or the import within the function itself. Consider using @patch("speech_recognition.microphone.pyaudio") and mocking the entire module.

Suggested change
@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()

Copilot uses AI. Check for mistakes.
Loading