python - PocketSphinx 音频设备列表
问题描述
由于我已经搜索了几天,但找不到任何东西,所以我来这里寻求帮助。
有什么方法可以找到 PocketSphinx 用来输入正确设备的音频设备列表,而不必通过 python 中的性能来猜测?
解决方案
我不知道它是否有这个功能,但PocketSphinx
被具有list_microphone_names ()甚至list_working_microphones()的 SpeechRecognition 使用
它使用模块pyaudio
来获取此列表。和模块audioop
来测试它。
如果您复制此代码
(我import audioop
在里面加了list_working_microphones
)
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
then 您可以获得更多信息,但我不知道它们是否对您有用
[{'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++ 模块上崩溃了,我不知道它是如何工作的。
推荐阅读
- java - 没有数据源的 Java Smart GWT listgrid - 过滤器不起作用
- javascript - Vuejs - Vue 应用程序构建为其他项目的 Vue 路由器的组件
- javascript - 使用 RegExp 概念,我如何编写一个函数来接受一个字符串,该字符串返回另一个字符串,所有大写字母都连接在一起?
- rstudio - RStudio 呈现非常错误
- php - 如何在php中反转while循环输出的顺序
- python - 如何使用 pep8speaks 检查我的项目的代码样式/结构
- java - 实现具有多个父母和孩子的图
- git - 在不让服务器上的每个人都访问我的 github 的情况下,通过 SSH 将 repo 拉到服务器的正确方法?
- python - 布尔值作为 if-else 条件
- javascript - 如何在不破坏它们的情况下删除“上一个”和“下一个”innerHTML