PortAudio / portaudio

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.
Other
1.43k stars 297 forks source link

[Errno -9986] Internal PortAudio error #771

Open speeding-rocket opened 1 year ago

speeding-rocket commented 1 year ago

Hi,

I know that issues about this bug have already appeared several times in the past, but all issues appear to have been closed and none of the proposed solutions has worked for me. I cannot open a new stream using neither sounddevice nor pyaudio. I have MacBook Pro 2019 with Ventura 13.2.1 and a freshly (re)installed PortAudio using brew uninstall portaudio && brew install portaudio --HEAD.

When running a pyaudio code, I get the following error message:

||PaMacCore (AUHAL)|| Error on line 1277: err='-66748', msg=Unknown Error
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
Cell In[5], line 1
----> 1 stream = p.open(format=sample_format,
      2                 channels=channels,
      3                 rate=fs,
      4                 frames_per_buffer=chunk,
      5                 input=True)
      7 frames = []

File /opt/anaconda3/lib/python3.9/site-packages/pyaudio.py:750, in PyAudio.open(self, *args, **kwargs)
    742 def open(self, *args, **kwargs):
    743     """
    744     Open a new stream. See constructor for
    745     :py:func:`Stream.__init__` for parameter details.
    746 
    747     :returns: A new :py:class:`Stream`
    748     """
--> 750     stream = Stream(self, *args, **kwargs)
    751     self._streams.add(stream)
    752     return stream

File /opt/anaconda3/lib/python3.9/site-packages/pyaudio.py:441, in Stream.__init__(self, PA_manager, rate, channels, format, input, output, input_device_index, output_device_index, frames_per_buffer, start, input_host_api_specific_stream_info, output_host_api_specific_stream_info, stream_callback)
    438     arguments['stream_callback'] = stream_callback
    440 # calling pa.open returns a stream object
--> 441 self._stream = pa.open(**arguments)
    443 self._input_latency = self._stream.inputLatency
    444 self._output_latency = self._stream.outputLatency

OSError: [Errno -9986] Internal PortAudio error

The code snippet is:

import pyaudio
import wave

chunk = 1024  
sample_format = pyaudio.paInt16  
channels = 1
fs = 44100  
seconds = 2
filename = "output.wav"

p = pyaudio.PyAudio()

stream = p.open(format=sample_format,
                channels=channels,
                rate=fs,
                frames_per_buffer=chunk,
                input=True)

frames = [] 
...

A very similar error message appears when I run an analogous script in sounddevice.

Could you please advise me on how to solve this problem? Apologies if I have missed a workable solution in the list of issues.

philburk commented 1 year ago

I have a 2019 Macbook Pro. I will upgrade to OS 13.2.1 and run my uni tests.

RossBencina commented 1 year ago

Hi, internal PortAudio error is a very generic error that can have many possible causes. It is not very likely that other tickets have the same cause as yours unless they are on Mac, with the same calling parameters. If you have found such a ticket, please link to it.

You should start by seeking support with pyaudio. For us to help, we really need a reproducible test case in C, not Python. For example, we can't tell from your test case exactly how PortAudio is being called -- there is no specification of the device that you are trying to open.

You could improve this bug report by giving the details of your audio hardware.

One suggestion to try would be to open the stream in stereo channels = 2 and see if you get the same error.

speeding-rocket commented 1 year ago

Hello, thanks for coming back to me about this. I am using just the default mic of my mac laptop as audio hardware.

I started with two channels and I got the following error,

PortAudioError: Error opening InputStream: Invalid number of channels [PaErrorCode -9998]

which however was to be expected because the mic had one channel only:

> 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)

I also attempted to use sounddevice, getting the same error, so I am not sure this lies with pyaudio.

Meanwhile after much searching I was able to record sounds with the default laptop mic using pvrecorder, a commercial app that has a small set of functionality accesible free of charge. I would still prefer to use sounddevice to record sounds for my project, or pyaudio at a pinch.

Also, if it is of any help, Audacity works for me on the same hardware. Audacity uses the PortAudio library if I am not mistaken.

I would be grateful for any further feedback you could think of. Thanks for your work on this.

RossBencina commented 1 year ago

Certainly we would like to fix the issue too. The problem is understanding how PortAudio is invoked so that we can reproduce the issue.

In your stack trace there is the line:

File /opt/anaconda3/lib/python3.9/site-packages/pyaudio.py:441, in Stream.__init__(self, PA_manager, rate, channels, format, input, output, input_device_index, output_device_index, frames_per_buffer, start, input_host_api_specific_stream_info, output_host_api_specific_stream_info, stream_callback)
    438     arguments['stream_callback'] = stream_callback
    440 # calling pa.open returns a stream object
--> 441 self._stream = pa.open(**arguments)
    443 self._input_latency = self._stream.inputLatency
    444 self._output_latency = self._stream.outputLatency

Would it be possible for you to edit that file and print the arguments dictionary just prior to call to pa.open on line 441?

Also, if it is of any help, Audacity works for me on the same hardware. Audacity uses the PortAudio library if I am not mistaken.

Yes I believe that Audacity does use PortAudio. The obvious possibilities are:

Either way, if you are using the latest version of PortAudio, the bug is present in our code.

MarkDunne commented 1 year ago

Hi @RossBencina,

I am getting exactly the same issue as @speeding-rocket. Here are the arguments printed out from running the original snippet above.

{'rate': 44100, 'channels': 1, 'format': 8, 'input': True, 'output': False, 'input_device_index': None, 'output_device_index': None, 'frames_per_buffer': 1024}

Edit: For what it's worth i've tried with both 44100 and 48000 sample rates but get the same issue