python-sounddevice
python-sounddevice copied to clipboard
Python sounddeveice causes speakers to freeze upon error
I'm working on a visual sound equalizer using the sounddevice module to record audio from my speakers for analysis. After some time of the code running, the following error arises:
Hello from the pygame community. https://www.pygame.org/contribute.html
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\...\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\...\Python39\lib\threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "C:\...\Media recorder\main.py", line 39, in test
data = analyse(record())
File "C:\...\Media recorder\recorder.py", line 9, in record
recording = sd.rec(frames=int(fs*length), dtype="int16", blocking=True, channels=1)
File "C:\...\Python39\lib\site-packages\sounddevice.py", line 274, in rec
ctx.start_stream(InputStream, samplerate, ctx.input_channels,
File "C:\...\Python39\lib\site-packages\sounddevice.py", line 2576, in start_stream
self.stream = StreamClass(samplerate=samplerate,
File "C:\...\Python39\lib\site-packages\sounddevice.py", line 1415, in __init__
_StreamBase.__init__(self, kind='input', wrap_callback='array',
File "C:\...\Python39\lib\site-packages\sounddevice.py", line 892, in __init__
_check(_lib.Pa_OpenStream(self._ptr, iparameters, oparameters,
File "C:\...\Python39\lib\site-packages\sounddevice.py", line 2741, in _check
raise PortAudioError(errormsg, err)
sounddevice.PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]
Here are some more details:
- I'm using python 3.9.9
- Included modules: Threading, SciPy (1.8.0), Numba (0.55.1), Numpy (1.21.1), sounddevice (0.4.4), and pygame (2.1.2)
- This is my code so far: https://github.com/BrickSigma/Desktop-stereo
MAIN ISSUE The main issue is once this error comes up, the speaker completely stop working. Even after closing the program the speakers refuse to respond. Only after restarting the computer will they work again. I think it might have something to do with overloading the speakers with the constant recording an the multi threading. I also think it could be something to do with the sample rate as I've set it to 44100 and the recording time to 1/9 of a second.