python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

Python sounddeveice causes speakers to freeze upon error

Open BrickSigma opened this issue 3 years ago • 0 comments

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:

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.

BrickSigma avatar May 08 '22 08:05 BrickSigma