简体   繁体   中英

pyaudio OSError: [Errno -9998] Invalid number of channels (On mac with External sound card)

I am writing a python program to stream data from a sound card. It works perfectly when I select the internal mic or the external mic, but won't work with an external stereo sound card (TI PCM2900C).

I have tried both 1 and 2 channels as well as different sample rates, but the error persists.

My input devices are:

0: USB AUDIO CODEC

1: External Microphone

2: iMac Microphone

Devices 1 & 2 work fine, but device 0 gives the error.

p = pyaudio.PyAudio()

# Open the selected audio input device
stream = p.open(
    format=pyaudio.paInt16,
    channels=2,
    rate=48000,
    input=True,
    input_device_index= 0,
    frames_per_buffer=1024)

Changing the channel numbers to 1 or 2 does not make any difference nor does changing the sample rate.

I tried...

# Get the number of channels that the selected device supports
device_channels = p.get_device_info_by_host_api_device_index(0, device_index).get('maxInputChannels')
print('Input device channels is ', device_channels)

It returns "0" for device "0" even though it is a stereo CODEC.

I solved this problem, it turns out that some audio devices have separate indexes for input and output channels. In my instance the USB AUDIO CODEC shows up with indexes 0 and 1.

{'index': 0, 'structVersion': 2, 'name': 'USB AUDIO  CODEC', 'hostApi': 0, 'maxInputChannels': 0, 'maxOutputChannels': 2, 'defaultLowInputLatency': 0.01, 'defaultLowOutputLatency': 0.004354166666666667, 'defaultHighInputLatency': 0.1, 'defaultHighOutputLatency': 0.0136875, 'defaultSampleRate': 48000.0}
 
{'index': 1, 'structVersion': 2, 'name': 'USB AUDIO  CODEC', 'hostApi': 0, 'maxInputChannels': 2, 'maxOutputChannels': 0, 'defaultLowInputLatency': 0.0057083333333333335, 'defaultLowOutputLatency': 0.01, 'defaultHighInputLatency': 0.015041666666666667, 'defaultHighOutputLatency': 0.1, 'defaultSampleRate': 48000.0}

As I was selecting device index 0 (Output) I was getting an error because the number of input channels was zero.

Just make sure you list all devices before selecting the input.

Hope that makes sense.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM