python-sounddevice: PortAudioError: Error opening Stream: Invalid number of channels [PaErrorCode -9998]

i set Headphones as the input device and received that error

How can i set my headphones as device to record and then play the data from it to my mic?

I ran that on Windows 10, using PortAudio V19.6.0-devel

code:

with sd.Stream(device = (3,1),
                   samplerate=args.samplerate, blocksize=args.blocksize,
                   dtype=args.dtype, latency=args.latency, channels=2,
                   callback=callback)

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 19 (9 by maintainers)

Most upvoted comments

I am on ubuntu 18.04 and encountered the same exception.

I encountered this issue trying to record audio from a usb microphone: https://www.amazon.com/Newest-YOUMI-Microphone-Laptop-desktop/dp/B01MQ2AA0X/ref=sr_1_2?dchild=1&keywords=raspberry+pi+usb+microphone&qid=1603452930&sr=8-2.

Recording with alsa works fine, but using sounddevice raises

Expression 'paInvalidSampleRate' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2048
Expression 'PaAlsaStreamComponent_InitialConfigure( &self->capture, inParams, self->primeBuffers, hwParamsCapture, &realSr )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2719
Expression 'PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, &inputLatency, &outputLatency, &hostBufferSizeMode )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 2843
Traceback (most recent call last):
  File "/home/jonas/.cache/bazel/_bazel_jonas/f882173bb04895bcb83c04830ed51f60/execroot/__main__/bazel-out/k8-fastbuild/bin/friday/audio/keyword_detection/goldfish/preprocessing/record_personal_examples.runfiles/__main__/friday/audio/keyword_detection/goldfish/preprocessing/record_personal_examples.py", line 127, in <module>
    text=args.text)
  File "/home/jonas/.cache/bazel/_bazel_jonas/f882173bb04895bcb83c04830ed51f60/execroot/__main__/bazel-out/k8-fastbuild/bin/friday/audio/keyword_detection/goldfish/preprocessing/record_personal_examples.runfiles/__main__/friday/audio/keyword_detection/goldfish/preprocessing/record_personal_examples.py", line 32, in record_audio
    device=device, dtype="int16")
  File "/home/jonas/.local/lib/python3.6/site-packages/sounddevice.py", line 280, in rec
    ctx.input_dtype, callback, blocking, **kwargs)
  File "/home/jonas/.local/lib/python3.6/site-packages/sounddevice.py", line 2498, in start_stream
    **kwargs)
  File "/home/jonas/.local/lib/python3.6/site-packages/sounddevice.py", line 1382, in __init__
    **_remove_self(locals()))
  File "/home/jonas/.local/lib/python3.6/site-packages/sounddevice.py", line 861, in __init__
    'Error opening {0}'.format(self.__class__.__name__))
  File "/home/jonas/.local/lib/python3.6/site-packages/sounddevice.py", line 2653, in _check
    raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening InputStream: Invalid sample rate [PaErrorCode -9997]

sounddevice works fine with my default speakers, but not the usb microphone, any clues on how to remedy this?