python - 如何选择要录制的设备(Python PyAudio)

标签 python audio pyaudio recording input-devices

我正在尝试选择要在Python中使用PyAudio库进行录制的设备,但我不知道该怎么做。我在网上找到了这段代码,其中显示了所有可用的输入设备:

import pyaudio
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print("Input Device id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i))

但是,这可行,如何从该列表中选择要使用的设备?我似乎无法在网上找到有关选择要使用的设备的信息,因此,如果有人可以帮助我,那将非常好,谢谢。

最佳答案

列出设备后(按照问题代码中显示的方式打印它们),您可以选择要使用的设备索引。

即它可能会打印出来

('Input Device id ', 2, ' - ', u'USB Sound Device: Audio (hw:1,0)')
('Input Device id ', 3, ' - ', u'sysdefault')
('Input Device id ', 11, ' - ', u'spdif')
('Input Device id ', 12, ' - ', u'default')

然后从该特定设备开始录制,您需要打开一个PyAudio流:
# Open stream with the index of the chosen device you selected from your initial code
stream = p.open(format=p.get_format_from_width(width=2),
                channels=1,
                output=True,
                rate=OUTPUT_SAMPLE_RATE,
                input_device_index=INDEX_OF_CHOSEN_INPUT_DEVICE, # This is where you specify which input device to use
                stream_callback=callback)

# Start processing and do whatever else...
stream.start_stream()


有关流选项的更多信息,请查看PyAudio's official documentation上指定的配置。

如果您需要更多脚本的帮助,我建议您看一下使用PyAudio,available on their documentation.进行非阻塞音频的简单示例

关于python - 如何选择要录制的设备(Python PyAudio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60437858/

相关文章:

python - 如何使用 ElementTree 从 Python 中的 XML 文档中删除节点

python - rpy2 转换矩阵 -> DataFrame

python - PyAudio,录音时如何分辨频率和幅度?

python - 如何在 Pandas 中高效处理时间序列数据

Python - 通用函数的最佳位置

audio - 使用 ffmpeg 将 wav 转换为 amr 时出现错误

r - 声音数据如何?

facebook - 如何在Facebook上分享我网站的MP3音频并在自己的墙上播放?

arrays - 将pyaudio数据 block 组合成一个较长的 block

python - pyaudio 中的破解声音正弦音调