簡體   English   中英

如何選擇要錄制的設備(Python PyAudio)

[英]How to select which device to record with (Python PyAudio)

當我在 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 的官方文檔中指定的配置。

如果您需要更多腳本方面的幫助,我建議您查看 PyAudio 的非阻塞音頻的簡單示例, 可在他們的文檔中找到。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM