The following suppressions of exceptions in audio.BasePyAudioSourceReader can lead to bugs when reinstanciating or reopening PyAudioSourceReader, because the PortAudio resources have not been released:
def close(self):
"""Stop audio stream"""
with suppress(Exception):
self._fp.stop_stream()
with suppress(Exception):
self._fp.close()
self._terminate_pyaudio()
@classmethod
def _terminate_pyaudio(cls):
with suppress(Exception):
if cls._pyaudio_instance is not None and isinstance(
cls._pyaudio_instance, (pyaudio.PyAudio,)
):
cls._pyaudio_instance.terminate()
cls._pyaudio_instance = None
An alternative could be to try to close / terminate PyAudioSourceReader several times in a loop, until success or a maximum number of attempts has been reached, in which case an exception would be raised for good.
The following suppressions of exceptions in
audio.BasePyAudioSourceReadercan lead to bugs when reinstanciating or reopening PyAudioSourceReader, because the PortAudio resources have not been released:An alternative could be to try to close / terminate PyAudioSourceReader several times in a loop, until success or a maximum number of attempts has been reached, in which case an exception would be raised for good.