spatialaudio / python-sounddevice

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

Internal PortAudio error [PaErrorCode -9986] #454

Open speeding-rocket opened 1 year ago

speeding-rocket commented 1 year ago

I have created the following script:

import sounddevice as sd

sample_rate = 44100 
duration = 2 
recording = sd.rec(int(seconds * sample_rate), samplerate=sample_rate, channels=1)
sd.wait()

When running this script, I get the following error message:

||PaMacCore (AUHAL)|| Error on line 1277: err='-66748', msg=Unknown Error
---------------------------------------------------------------------------
PortAudioError                            Traceback (most recent call last)
Cell In[5], line 5
      3 sample_rate = 44100
      4 duration = 2
----> 5 rec = sd.rec(int(sample_rate*duration), samplerate = sample_rate, channels = 1)
      6 sd.wait()

File ~/python-sounddevice/sounddevice.py:276, in rec(frames, samplerate, channels, dtype, out, mapping, blocking, **kwargs)
    273     ctx.read_indata(indata)
    274     ctx.callback_exit()
--> 276 ctx.start_stream(InputStream, samplerate, ctx.input_channels,
    277                  ctx.input_dtype, callback, blocking, **kwargs)
    278 return out

File ~/python-sounddevice/sounddevice.py:2582, in _CallbackContext.start_stream(self, StreamClass, samplerate, channels, dtype, callback, blocking, **kwargs)
   2579 def start_stream(self, StreamClass, samplerate, channels, dtype, callback,
   2580                  blocking, **kwargs):
   2581     stop()  # Stop previous playback/recording
-> 2582     self.stream = StreamClass(samplerate=samplerate,
   2583                               channels=channels,
   2584                               dtype=dtype,
   2585                               callback=callback,
   2586                               finished_callback=self.finished_callback,
   2587                               **kwargs)
   2588     self.stream.start()
   2589     global _last_callback

File ~/python-sounddevice/sounddevice.py:1421, in InputStream.__init__(self, samplerate, blocksize, device, channels, dtype, latency, extra_settings, callback, finished_callback, clip_off, dither_off, never_drop_input, prime_output_buffers_using_stream_callback)
   1391 def __init__(self, samplerate=None, blocksize=None,
   1392              device=None, channels=None, dtype=None, latency=None,
   1393              extra_settings=None, callback=None, finished_callback=None,
   1394              clip_off=None, dither_off=None, never_drop_input=None,
   1395              prime_output_buffers_using_stream_callback=None):
   1396     """PortAudio input stream (using NumPy).
   1397 
   1398     This has the same methods and attributes as `Stream`, except
   (...)
   1419 
   1420     """
-> 1421     _StreamBase.__init__(self, kind='input', wrap_callback='array',
   1422                          **_remove_self(locals()))

File ~/python-sounddevice/sounddevice.py:898, in _StreamBase.__init__(self, kind, samplerate, blocksize, device, channels, dtype, latency, extra_settings, callback, finished_callback, clip_off, dither_off, never_drop_input, prime_output_buffers_using_stream_callback, userdata, wrap_callback)
    896     userdata = _ffi.NULL
    897 self._ptr = _ffi.new('PaStream**')
--> 898 _check(_lib.Pa_OpenStream(self._ptr, iparameters, oparameters,
    899                           samplerate, blocksize, stream_flags,
    900                           callback_ptr, userdata),
    901        f'Error opening {self.__class__.__name__}')
    903 # dereference PaStream** --> PaStream*
    904 self._ptr = self._ptr[0]

File ~/python-sounddevice/sounddevice.py:2747, in _check(err, msg)
   2744     hosterror_info = host_api, info.errorCode, hosterror_text
   2745     raise PortAudioError(errormsg, err, hosterror_info)
-> 2747 raise PortAudioError(errormsg, err)

PortAudioError: Error opening InputStream: Internal PortAudio error [PaErrorCode -9986]

The list of devices is as follows:

$ python3 -m sounddevice

> 0 MacBook Pro Microphone, Core Audio (1 in, 0 out)
< 1 MacBook Pro Speakers, Core Audio (0 in, 2 out)
  2 Echo360 System Audio, Core Audio (2 in, 2 out)
  3 Microsoft Teams Audio, Core Audio (2 in, 2 out)
  4 ZoomAudioDevice, Core Audio (2 in, 2 out)
  5 Soundflower and Bose, Core Audio (0 in, 0 out)
  6 Soundflower and speakers, Core Audio (0 in, 2 out)
  7 SF and External Headphones, Core Audio (0 in, 0 out)

Could you please advise me on how to solve this problem?

PraxTube commented 1 year ago

The code you provided and the code you ran that gave the error are a little different.

import sounddevice as sd

sample_rate = 44100 
duration = 2 
# recording = sd.rec(int(seconds * sample_rate), samplerate=sample_rate, channels=1)
rec = sd.rec(int(sample_rate*duration), samplerate = sample_rate, channels = 1)
sd.wait()

Running this code doesn't give me any errors. Perhaps you could try to run one of the recording examples. If you get the same error there as well, then it has to be something about your set up (maybe not all dependencies are installed or something with your audio device is wrong).

Also, did you try it with any other channels (i.e. channels=2)?

speeding-rocket commented 1 year ago

Thank you very much for your feedback. Sorry for the typo in the code, I have copied a source code and error message from two different attempts. However the snippet from your comment still throws the same error.

I originally wanted to run it with two channels but it came with another error, PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998], which however is not surprising given that the default input has only one channel.

I have tried the recording examples from the link you list, and the same error comes up.

I have been installing and reinstalling PortAudio and various python audio libraries and their dependencies and I think that it is not my setup that is at fault. I ran an analogous code in pyaudio and I got the exact same error. I started to think that this might be an error on PortAudio side and subsequently raised an issue on their site (PortAudio/portaudio, #771) but so far I have had no answer.

Meanwhile, I was finally able to record sounds using the pvrecorder library. This appears to be a huge commercial resource but they do let users access some basic functionality for free.

This is however not ideal for me and I would still prefer to use sounddevice library. I would be grateful for any ideas for possible solutions of this hiccup.

mgeier commented 1 year ago

How exactly are you running your Python code? In the terminal?

It might be an issue about microphone permissions.

laughingrice commented 10 months ago

Updating:

I am seeing the same problem on an intel mac (13" 2017 running macos ventura 13.4.1).

The problem is with portaudio not sounddevice. Installing portaudio 19.7 from homebrew instead of 19.6 from anaconda (requires installing sounddevice with --no-deps) solves the problem (better solution would be to upgrade portaudio on anaconda to 19.7 but I do not see how to report that issue).

alternately, install everything from pip instead of anaconda.

Shows up as a bug on audacity as well https://github.com/PortAudio/portaudio/issues/395