使用 python 读取声卡输出

Reading soundcard output with python

我已经成功地利用 pyaudio 读取和记录麦克风数据。我想对声卡输出做同样的事情。 (即播放 wavfile 并使用 pyaudio 打开流并逐帧读取声卡输出)。

为了阅读麦克风,我打开了一个 pyaudio 流,如下所示

stream = pyaud.open(
    format = pyaudio.paInt16,
    channels = 1,
    rate = 22050,
    input_device_index = 0,
    input = True)

当我将 input_device_index 更改为 1 时,出现以下错误

IOError:[Errno 通道数无效]-9998

下面是 system_info macbook 声卡

Default Devices:
================
Default Input Device : 0
    defaultSampleRate: 44100.0
    defaultLowOutputLatency: 0.01
    defaultLowInputLatency: 0.00199546485261
    maxInputChannels: 2
    structVersion: 2
    hostApi: 0 (Core Audio)
    index: 0
    defaultHighOutputLatency: 0.1
    maxOutputChannels: 0
    name: Built-in Microph
    defaultHighInputLatency: 0.0121541950113
    --------------------------------
Default Output Device: 1
    defaultSampleRate: 44100.0
    defaultLowOutputLatency: 0.00655328798186
    defaultLowInputLatency: 0.01
    maxInputChannels: 0
    structVersion: 2
    hostApi: 0 (Core Audio)
    index: 1
    defaultHighOutputLatency: 0.0167120181406
    maxOutputChannels: 2
    name: Built-in Output
    defaultHighInputLatency: 0.1
    --------------------------------

有些声卡有'Capture'个声道,这是声卡的输出。它们可能会或可能不会正常工作(音乐行业似乎施加了消除这些端口的压力)。

以下是Linux...例如。在我的系统上,我只有一个标准的板载 ALC887 音频端口。在 /proc/asound/card0 中,我找到了一个端口 pcm2c,它有以下信息:

card: 0
device: 2
subdevice: 0
stream: CAPTURE
id: ALC887-VD Alt Analog
name: ALC887-VD Alt Analog
subname: subdevice #0
class: 0
subclass: 0
subdevices_count: 1
subdevices_avail: 1

这些捕获设备应该会出现在您的混音器中。