PocketSphinx 音频设备列表

PocketSphinx audio device list

找了好几天,一无所获,特来求助

有什么方法可以找到 PocketSphinx 用来输入正确设备的音频设备列表,而不必根据 python 中的性能来猜测?

不知道有没有这个功能但是PocketSphinxSpeechRecognition which has list_microphone_names() and even list_working_microphones()

使用了

它使用模块 pyaudio 来获取此列表。和模块 audioop 来测试它。

如果您复制此代码

(我在list_working_microphones里面加了import audioop

class Microphone():
    
    @staticmethod
    def get_pyaudio():
        """
        Imports the pyaudio module and checks its version. Throws exceptions if pyaudio can't be found or a wrong version is installed
        """
        try:
            import pyaudio
        except ImportError:
            raise AttributeError("Could not find PyAudio; check installation")
        from distutils.version import LooseVersion
        if LooseVersion(pyaudio.__version__) < LooseVersion("0.2.11"):
            raise AttributeError("PyAudio 0.2.11 or later is required (found version {})".format(pyaudio.__version__))
        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():
        """
        Returns a dictionary mapping device indices to microphone names, for microphones that are currently hearing sounds. When using this function, ensure that your microphone is unmuted and make some noise at it to ensure it will be detected as working.
        Each key in the returned dictionary can be passed to the ``Microphone`` constructor to use that microphone. For example, if the return value is ``{3: "HDA Intel PCH: ALC3232 Analog (hw:1,0)"}``, you can do ``Microphone(device_index=3)`` to use that microphone.
        """

        import audioop   # <-- added 

        pyaudio_module = Microphone.get_pyaudio()
        audio = pyaudio_module.PyAudio()
        try:
            result = {}
            for device_index in range(audio.get_device_count()):
                device_info = audio.get_device_info_by_index(device_index)
                device_name = device_info.get("name")
                assert isinstance(device_info.get("defaultSampleRate"), (float, int)) and device_info["defaultSampleRate"] > 0, "Invalid device info returned from PyAudio: {}".format(device_info)
                try:
                    # read audio
                    pyaudio_stream = audio.open(
                        input_device_index=device_index, channels=1, format=pyaudio_module.paInt16,
                        rate=int(device_info["defaultSampleRate"]), input=True
                    )
                    try:
                        buffer = pyaudio_stream.read(1024)
                        if not pyaudio_stream.is_stopped(): pyaudio_stream.stop_stream()
                    finally:
                        pyaudio_stream.close()
                except Exception:
                    continue

                # compute RMS of debiased audio
                energy = -audioop.rms(buffer, 2)
                energy_bytes = chr(energy & 0xFF) + chr((energy >> 8) & 0xFF) if bytes is str else bytes([energy & 0xFF, (energy >> 8) & 0xFF])  # Python 2 compatibility
                debiased_energy = audioop.rms(audioop.add(buffer, energy_bytes * (len(buffer) // 2), 2), 2)

                if debiased_energy > 30:  # probably actually audio
                    result[device_index] = device_name
        finally:
            audio.terminate()
        return result

那你就可以用了

 Microphone.list_microphone_names()

我的 Linux Mint

结果
['HDA Intel MID: ALC272X Analog (hw:0,0)',
 'HDA Intel MID: ALC272X Digital (hw:0,1)',
 'HDA Intel MID: ALC272X Alt Analog (hw:0,2)',
 'HDA ATI HDMI: 0 (hw:1,3)',
 'sysdefault',
 'front',
 'surround40',
 'surround51',
 'surround71',
 'iec958',
 'spdif',
 'samplerate',
 'speexrate',
 'pulse',
 'upmix',
 'vdownmix',
 'dmix',
 'default']

如果在 list_microphone_names 中将 device_info.get("name") 替换为 device_info 那么您可以获得更多信息,但我不知道它们是否对您有用

[{'defaultHighInputLatency': 0.034829931972789115,
  'defaultHighOutputLatency': 0.034829931972789115,
  'defaultLowInputLatency': 0.008707482993197279,
  'defaultLowOutputLatency': 0.008707482993197279,
  'defaultSampleRate': 44100.0,
  'hostApi': 0,
  'index': 0,
  'maxInputChannels': 2,
  'maxOutputChannels': 2,
  'name': 'HDA Intel MID: ALC272X Analog (hw:0,0)',
  'structVersion': 2},
 {'defaultHighInputLatency': -1.0,
  'defaultHighOutputLatency': 0.034829931972789115,
  'defaultLowInputLatency': -1.0,
  'defaultLowOutputLatency': 0.008707482993197279,
  'defaultSampleRate': 44100.0,
  'hostApi': 0,
  'index': 1,
  'maxInputChannels': 0,
  'maxOutputChannels': 2,
  'name': 'HDA Intel MID: ALC272X Digital (hw:0,1)',
  'structVersion': 2},

  # ...etc...

当然你可以减少这段代码直接使用pyaudio


我也测试了 list_working_microphones() 但在某些时候它在某些 C/C++ 模块上崩溃了,我不知道它是如何工作的。