Search code examples
pythonpyaudio

PyAudio error: [Errno -9986] Could not get stream information


I've taken a modified version of the wire example from the PyAudio docs:

    def mic_to_vac_pipe(self):
        microphone_input_stream = self.pyaudio.open(
            format=self.pyaudio.get_format_from_width(2),
            channels=1,
            rate=self.RATE,
            input=True,
            output=True,
            input_device_index=1,
            frames_per_buffer=self.CHUNK_SIZE
        )

        cable_output_stream = self.pyaudio.open(
            format=self.pyaudio.get_format_from_width(2),
            channels=1,
            rate=self.RATE,
            output=True,
            output_device_index=5, # change to 8
            frames_per_buffer=self.CHUNK_SIZE
        )

        # This will last for 217483647 seconds, or 68.09 years
        for i in range(0, int(self.RATE / self.CHUNK_SIZE * 2147483647)):
            data = microphone_input_stream.read(self.CHUNK_SIZE)
            cable_output_stream.write(data, self.CHUNK_SIZE)

        microphone_input_stream.stop_stream()
        microphone_input_stream.close()
        cable_output_stream.stop_stream()
        cable_output_stream.close()

This used to work, until I moved it to a class, and now I get this error:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\threading.py", line 954, in _bootstrap_inner
    self.run()
  File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\threading.py", line 892, in run
    self._target(*self._args, **self._kwargs)
  File "d:\documents\coding\python\kyan\kyan\kyan.py", line 49, in mic_to_vac_pipe
    microphone_input_stream = self.pyaudio.open(
  File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\site-packages\pyaudio.py", line 750, in open
    stream = Stream(self, *args, **kwargs)
  File "C:\Users\pwill\AppData\Local\Programs\Python\Python39\lib\site-packages\pyaudio.py", line 441, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno -9986] Could not get stream information

I also get a similar error when using self.pyaudio.get_host_api_info_by_index(0) (OSError: [Errno -9978] Invalid host api info). Is it possible these errors are related?

I suspect it might be related to PortAudio internally, since bypassing the get_host_api_info_by_index(0) call (it's used to get the number of audio devices, so I can bypass by setting a hardcoded number for a device count) tells me that PortAudio is not initalized, even though PyAudio is initalized, which also initalizes PortAudio at the same time. How can I fix this?


Solution

  • I figured it out! My problem was that pyaudio.terminate() in the class's __init__ function. The only reason it stopped working when I added it to a class is because I also added it to a separate thread, which involves calling thread.join(), which is blocking terminate(). Moving this terminate() function to the proper spot fixes my problem.