spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
1.04k stars 149 forks source link

PortAudioError: Error opening Stream: Invalid number of channels [PaErrorCode -9998] #244

Open OnRaptor opened 4 years ago

OnRaptor commented 4 years ago

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)
mgeier commented 4 years ago

Thanks for the report!

Please update your question according to https://python-sounddevice.readthedocs.io/en/latest/CONTRIBUTING.html#reporting-problems.

OnRaptor commented 4 years ago

Thanks for the report!

Please update your question according to https://python-sounddevice.readthedocs.io/en/latest/CONTRIBUTING.html#reporting-problems.

I updated it but can you tell me what information you want?

mgeier commented 4 years ago

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

I guess the short answer is: you can't.

How would you expect to record something with your headphones and play it back with a microphone? Those things are made for the exact opposite.

Of course, the physical principles are similar, so you could try to plug your headphones into the microphone jack and shout into them like mad person, but I doubt you would be able to record a reasonable signal.

Or am I missing something?

OnRaptor commented 4 years ago

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

I guess the short answer is: you can't.

How would you expect to record something with your headphones and play it back with a microphone? Those things are made for the exact opposite.

Of course, the physical principles are similar, so you could try to plug your headphones into the microphone jack and shout into them like mad person, but I doubt you would be able to record a reasonable signal.

Or am I missing something?

I can play wav file to mic using sounddevice.Play(), but i need play from captured stream of Headphones.

OnRaptor commented 4 years ago

@mgeier The Main Question is "How to Record Sound from Headphones"

mgeier commented 4 years ago

And my question is: "How would you expect to record something with your headphones?"

I might be missing something, but headphones are not recording devices, right?

Or do you mean "how to record sound that's coming out of some headphones"?

OnRaptor commented 4 years ago

@mgeier I mean, record sound that i can hear with headphones.

OnRaptor commented 4 years ago

@mgeier Help meeeeeeeeee

mgeier commented 4 years ago

Help me help you by telling me what you actually want to do.

Based on the information you provided up to now (and knowing common user requests), I can only guess:

You have some program running on your computer that generates sound, and you want to record that sound.

Is that true?

OnRaptor commented 4 years ago

I want the sound that is recorded from my microphone and also the sound that I hear from my headphones. In simple words, i want to others hear sound from my headphones(If i use that in voice chat).

mgeier commented 4 years ago

In typical voice chat applications, the sound you hear in your headphones does not contain your own voice.

So it sounds like you want to mix the signal from your microphone with the signal generated by one application (or multiple applications?) and then (at the same time?) provide this mixed signal in some way (probably involving some kind of computer network like e.g. the internet?) to "others".

This is all still very vague, but I fear it won't get any more precise.

So let me try to give some you some information/advice:

The sounddevice module can only record from something that the underlying PortAudio library can use as an input device.

Typically, this is the microphone (or line-in) channel of some sound card.

However, there are platform-specific tools that can provide a "virtual sound card" (which, when activated, will be recognized as input device by the soundfile module) allowing to record the outputs of some program or even the mix of different such outputs.

I know of solutions for Linux (e.g. JACK) and macOS (e.g. SoundFlower), but I don't know any tools for Windows (because I'm not a Windows user). But I assume there are such tools. You can probably find them by searching for "audio loop device" or "virtual sound card" or "virtual audio cable" or something like that, together with the name and version of your operating system.

When you find something, please leave a comment here, because other Windows users might find that useful as well.

See also issue #129, which is a similar question, but regarding Linux.

JonasRSV commented 3 years ago

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?

mgeier commented 3 years ago

What about the sample rate?

JonasRSV commented 3 years ago

Using a sample rate of 8000 -

mgeier commented 3 years ago

Did you try different sample rates? Probably 8000 is not supported by the device?

What does check_input_settings() say?

JonasRSV commented 3 years ago

I figured out the issue, the sample rate was supported by the device, but the period length was not. Had to use a different period length.

mgeier commented 3 years ago

OK, good to know! Just out of curiosity: which value wasn't supported, which was?

JonasRSV commented 3 years ago

Oddly enough it only supported a period rate of 170, I guess this is what you get when you buy the cheap stuff xD

mgeier commented 3 years ago

Odd indeed ...