sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
I get the follow errer when I run sounddevice. Could anybody show me how to make it work?
My OS is Mac OS X 10.15.6.
$ cat main.py
#!/usr/bin/env python3
# vim: set noexpandtab tabstop=2 shiftwidth=2 softtabstop=-1 fileencoding=utf-8:
import sounddevice
fs = 44100
seconds = 10
myrecording = sounddevice.rec(int(seconds * fs), samplerate=fs, channels=2)
import wave
obj = wave.open('sound.wav', 'w')
obj.setnchannels(1) # mono
obj.setsampwidth(2)
obj.setframerate(fs)
obj.writeframesraw(myrecording)
sounddevice.wait()
$ ./main.py
Traceback (most recent call last):
File "./main.py", line 7, in <module>
myrecording = sounddevice.rec(int(seconds * fs), samplerate=fs, channels=2)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 275, in rec
ctx.input_dtype, callback, blocking, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 2578, in start_stream
**kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 1416, in __init__
**_remove_self(locals()))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 895, in __init__
'Error opening {}'.format(self.__class__.__name__))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/sounddevice.py", line 2738, in _check
raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
Can you please provide your device list?
See https://python-sounddevice.readthedocs.io/en/0.4.1/CONTRIBUTING.html#reporting-problems
Here is the device list.
$ python3 -m sounddevice
> 0 MacBook Pro Microphone, Core Audio (1 in, 0 out)
< 1 MacBook Pro Speakers, Core Audio (0 in, 2 out)
OK, there you have it. You are requesting channels=2, but your input device only provides one channel for recording.
Does it work if you use channels=1?
BTW, I doubt that writing the WAV file will work as expected since the selected data types don't match.
I have changed this. The sound file can be generated, but I don't hear any sound recorded.
Would you please show me the other lines that I need to change? Thanks.
myrecording = sounddevice.rec(int(seconds * fs), samplerate=fs, channels=1)
You'll have to use sounddevice.wait() before you can use the recorded signal.
To play the recorded signal immediately after recording, you could try something like this:
myrecording = sounddevice.rec(int(seconds * fs), samplerate=fs, channels=1)
sounddevice.wait()
sounddevice.play(myrecording, samplerate=fs)
sounddevice.wait()
If you want to save the recorded signal to an audio file, I recommend using the soundfile module.
You could try something like this:
import soundfile as sf
sf.write('my_cool_recording.wav', myrecording, fs)
Apparently, I am having this same problem. I already had the number of channels to be 1. When checking the devices I got this :open_mouth:
In my case, the problem seems to be caused only when I use sounddevice inside a thread.
$ python3 -m sounddevice
0 sof-hda-dsp: - (hw:0,0), ALSA (2 in, 2 out)
1 sof-hda-dsp: - (hw:0,3), ALSA (0 in, 2 out)
2 sof-hda-dsp: - (hw:0,4), ALSA (0 in, 2 out)
3 sof-hda-dsp: - (hw:0,5), ALSA (0 in, 2 out)
4 sof-hda-dsp: - (hw:0,6), ALSA (2 in, 0 out)
5 sof-hda-dsp: - (hw:0,7), ALSA (2 in, 0 out)
6 sysdefault, ALSA (128 in, 128 out)
7 samplerate, ALSA (128 in, 128 out)
8 speexrate, ALSA (128 in, 128 out)
9 pulse, ALSA (32 in, 32 out)
10 upmix, ALSA (8 in, 8 out)
11 vdownmix, ALSA (6 in, 6 out)
12 dmix, ALSA (0 in, 2 out)
* 13 default, ALSA (32 in, 32 out)
What should I do?
@eduardo4jesus Using threads can be problematic, you should search the issues for "thread". If you don't find a solution, please crate a new issue with a detailed description.
https://python-sounddevice.readthedocs.io/en/0.4.6/CONTRIBUTING.html#reporting-problems