bastibe / SoundCard

A Pure-Python Real-Time Audio Library
https://soundcard.readthedocs.io
BSD 3-Clause "New" or "Revised" License
680 stars 69 forks source link

the import blocked when there's no PulseAudio running #98

Closed zhujisheng closed 4 years ago

zhujisheng commented 4 years ago

When I import soundcard in the enviroment without PulseAudio running, the program block. It's caused by the code:

        while self._pa_context_get_state(self.context) != _pa.PA_CONTEXT_READY:
            time.sleep(0.001)

When I want to use this lib in some user-oriented applications, it's not convenient.

bastibe commented 4 years ago

Could you elaborate on when and where you might be running without pulseaudio?

I presume that there is some other error code that we might catch in this case? Or would we simply need to install a global timeout at which point we stop the above loop and raise an error?

At any rate, I'd be grateful for a pull request.

zhujisheng commented 4 years ago

I just want to implement a media_player integration for HomeAssistant using pulseaudio. Because HomeAssistant is used by end user on various system, it's hard to pre-define that there is pulseaudio running. Because HomeAssistant is on Python’s built-in asyncio, when an import operation blocks, the whole process blocks.

bastibe commented 4 years ago

Just so you know, asyncio is poison for audio. You will need to use threads for audio. Real time waits for nobody and is not compatible with cooperative multitasking.

Anyway, if this is important to you, I'd be grateful for a pull request.

zhujisheng commented 4 years ago

https://github.com/bastibe/SoundCard/pull/100