python-sounddevice-音频设备

安装:pip install sounddevice -i https://pypi.douban.com/simple      

import sounddevice as sd
dev=sd.query_devices()  #返回系统所有的声音设备

下面是我计算机上的声音设备,>标示为默认输入设备,<表示默认的输出设备,声音设备名称之前是序号

0 Microsoft 声音映射器 - Input, MME (2 in, 0 out)
> 1 Microphone (Realtek High Defini, MME (2 in, 0 out)
2 立体声混音 (Realtek High Defini, MME (2 in, 0 out)
3 Microsoft 声音映射器 - Output, MME (0 in, 2 out)
< 4 喇叭/耳机 (Realtek High Definit, MME (0 in, 2 out)
5 主声音捕获驱动程序, Windows DirectSound (2 in, 0 out)
6 Microphone (Realtek High Definition Audio), Windows DirectSound (2 in, 0 out)
7 立体声混音 (Realtek High Definition Audio), Windows DirectSound (2 in, 0 out)
8 主声音驱动程序, Windows DirectSound (0 in, 2 out)
9 喇叭/耳机 (Realtek High Definition Audio), Windows DirectSound (0 in, 2 out)
10 喇叭/耳机 (Realtek High Definition Audio), Windows WASAPI (0 in, 2 out)
11 立体声混音 (Realtek High Definition Audio), Windows WASAPI (2 in, 0 out)
12 Microphone (Realtek High Definition Audio), Windows WASAPI (2 in, 0 out)
13 Speakers (Realtek HD Audio output), Windows WDM-KS (0 in, 2 out)
14 麦克风 (Realtek HD Audio Mic input), Windows WDM-KS (2 in, 0 out)
15 立体声混音 (Realtek HD Audio Stereo input), Windows WDM-KS (2 in, 0 out)

dev1=sd.default.device  #返回默认声音设备
#[1, 4]
#返回的是一个列表,有两个元素。第一个元素为默认的输入设备,第二个元素为默认的输出设备,设备是用序号来表示的
sd.default.device[0] = 2  #修改默认的输入设备为系统输出声音(捕获系统声音)
#系统默认的声音输入设备是麦克风,如果需要录制系统声音则需要将声音设备切换成立体声混音
#通过上面运行sd.query_devices()可以看到,我的计算机立体声混音设备序号为2

 注意:有的电脑还需要修改

 把相应的设备设为默认值

遍历:

import sounddevice as sd
devs=sd.query_devices()  #返回系统所有的声音设备
i=0
for dev in devs:
    print('第{0}个'.format(i),dev)
    i+=1

说明:dev是个字典(每个设备的信息),如下:

第0个 {'name': 'Microsoft 声音映射器 - Input', 'hostapi': 0, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_latency':
0.09, 'default_low_output_latency': 0.09, 'default_high_input_latency': 0.18, 'default_high_output_latency': 0.18, 'default_samplerate': 4410
0.0}
第1个 {'name': '麦克风 (Realtek High Definition', 'hostapi': 0, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_latency
': 0.09, 'default_low_output_latency': 0.09, 'default_high_input_latency': 0.18, 'default_high_output_latency': 0.18, 'default_samplerate': 4
4100.0}
第2个 {'name': 'Microsoft 声音映射器 - Output', 'hostapi': 0, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_latency':
0.09, 'default_low_output_latency': 0.09, 'default_high_input_latency': 0.18, 'default_high_output_latency': 0.18, 'default_samplerate': 441
00.0}
第3个 {'name': '扬声器 (Realtek High Definition', 'hostapi': 0, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_latency
': 0.09, 'default_low_output_latency': 0.09, 'default_high_input_latency': 0.18, 'default_high_output_latency': 0.18, 'default_samplerate': 4
4100.0}
第4个 {'name': '主声音捕获驱动程序', 'hostapi': 1, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_latency': 0.12, 'def
ault_low_output_latency': 0.0, 'default_high_input_latency': 0.24, 'default_high_output_latency': 0.0, 'default_samplerate': 44100.0}
第5个 {'name': '麦克风 (Realtek High Definition Audio)', 'hostapi': 1, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_
latency': 0.12, 'default_low_output_latency': 0.0, 'default_high_input_latency': 0.24, 'default_high_output_latency': 0.0, 'default_samplerat
e': 44100.0}
第6个 {'name': '主声音驱动程序', 'hostapi': 1, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_latency': 0.0, 'default_
low_output_latency': 0.12, 'default_high_input_latency': 0.0, 'default_high_output_latency': 0.24, 'default_samplerate': 44100.0}
第7个 {'name': '扬声器 (Realtek High Definition Audio)', 'hostapi': 1, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_
latency': 0.0, 'default_low_output_latency': 0.12, 'default_high_input_latency': 0.0, 'default_high_output_latency': 0.24, 'default_samplerat
e': 44100.0}
第8个 {'name': '扬声器 (Realtek High Definition Audio)', 'hostapi': 3, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_
latency': 0.0, 'default_low_output_latency': 0.003, 'default_high_input_latency': 0.0, 'default_high_output_latency': 0.01, 'default_samplera
te': 48000.0}
第9个 {'name': '麦克风 (Realtek High Definition Audio)', 'hostapi': 3, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_
latency': 0.0013333, 'default_low_output_latency': 0.0, 'default_high_input_latency': 0.01, 'default_high_output_latency': 0.0, 'default_samp
lerate': 48000.0}
第10个 {'name': '麦克风 (Realtek HD Audio Mic input)', 'hostapi': 4, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_input_la
tency': 0.01, 'default_low_output_latency': 0.01, 'default_high_input_latency': 0.04, 'default_high_output_latency': 0.04, 'default_samplerat
e': 44100.0}
第11个 {'name': 'Speakers (Realtek HD Audio output)', 'hostapi': 4, 'max_input_channels': 0, 'max_output_channels': 2, 'default_low_input_lat
ency': 0.01, 'default_low_output_latency': 0.01, 'default_high_input_latency': 0.04, 'default_high_output_latency': 0.04, 'default_samplerate
': 44100.0}
第12个 {'name': '立体声混音 (Realtek HD Audio Stereo input)', 'hostapi': 4, 'max_input_channels': 2, 'max_output_channels': 0, 'default_low_i
nput_latency': 0.01, 'default_low_output_latency': 0.01, 'default_high_input_latency': 0.04, 'default_high_output_latency': 0.04, 'default_sa
mplerate': 48000.0}

default_samplerate  默认采样率

max_input_channels  最多输入通道

 

 

原文地址:https://www.cnblogs.com/liming19680104/p/12106449.html