列出所有实际麦克风Python

问题描述 投票:0回答:1

如何列出 python 中当前可用的所有实际麦克风?

目前我正在这样做:

import pyaudio

p = pyaudio.PyAudio()

print("Available audio input devices:")
for i in range(p.get_device_count()):
    dev_info = p.get_device_info_by_index(i)
    print(f"{i}: {dev_info['name']}")

device_index = int(input("Enter the index of the audio input device you want to use: "))

我知道这也会打印输出设备,但结果因重复项和实际上不是设备的东西而变得臃肿。

仅供参考,我有 3 个输入设备和 2 个输出设备,但脚本打印了 29 个项目。

“过滤”此内容以仅显示实际设备(即“设置”>“系统”>“声音”中可用的设备)的正确方法是什么?

python audio filter pyaudio microphone
1个回答
0
投票

我不确定这是否是最有效的解决方案,但它确实有效。

import pyaudio

p = pyaudio.PyAudio()

devices = p.get_device_count()

#list of device objects(dicts)
device_list = []

#list of device names. Used for detecting duplicates
temp_name_list = []

for i in range(devices):
   # Get the device info
   device_info = p.get_device_info_by_index(i)
   # Check if this device is a microphone (an input device)
   if device_info.get('maxInputChannels') > 0:
        #get device name
        device_name = device_info.get("name")
        #check if device is duplicate
        if(device_name not in temp_name_list):
            #add device to device list
            device_list.append(device_info)
            #add device to name list
            temp_name_list.append(device_name)

#print device names
for pos, device in enumerate(device_list):
   print(f"{pos}: {device["name"]}")

list_len = len(device_list)

#handle user input
while True:
    device_user_selected = int(input("Enter the number of the audio input device you want to use: "))
    
    if(device_user_selected < 0 or device_user_selected >= list_len):
        print("Please select a device that is in the list.")
    else:
        break

#do device select logic
#retrieve device info by doing device_list[device_user_selected]

print(device_list[device_user_selected])

我使用了this教程中的代码,并添加了我自己的逻辑来删除重复项并使用户输入更加稳健。

这是我得到的输出:

0: Microsoft Sound Mapper - Input
1: Microphone (Razer Seiren X)
2: Microphone (High Definition Aud
3: Primary Sound Capture Driver
4: Microphone (High Definition Audio Device)
5: Microphone (HD Audio Microphone)
Enter the number of the audio input device you want to use: 1
{'index': 1, 'structVersion': 2, 'name': 'Microphone (Razer Seiren X)', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.09, 'defaultLowOutputLatency': 0.09, 'defaultHighInputLatency': 0.18, 'defaultHighOutputLatency': 0.18, 'defaultSampleRate': 44100.0}

如果您想进一步过滤响应,请告诉我,我可以向您展示如何做到这一点。

© www.soinside.com 2019 - 2024. All rights reserved.