安裝: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 最多輸入通道