TaylorSMarks / playsound

Pure Python, cross platform, single function module with no dependencies for playing sounds.
Other
517 stars 114 forks source link

Issues when playing many short sounds asynchronously (Ubuntu 20.04, Python 3.8.10) #111

Open kyp44 opened 2 years ago

kyp44 commented 2 years ago

I am experiencing issues when playing a lot of short sound clips very often asynchronously, which don't happen if I play them with blocking instead.

I rigged up a little test program (demo.py in demo.zip) that demonstrates the problem. On my system, the sounds stop playing after about 34 seconds. The program runs in silence for about another 1:20 and then playsound throws a PlaysoundException, which is shown in the attached output.txt. This behavior is very consistent currently on repeated runs. Also included is demo-block.py, which is identical but blocks instead when calling playsound. This seems to run indefinitely with sounds always playing. If I run both at the same time, I hear audio from both until demo.py stops playing audio, at which point the blocking version completely blocks (seemingly at a lower level than Python as I can't SIGINT out of it with Ctrl+C) until either demo.py is broken or terminates because of the exception, at which point demo-block.py continues running normally and playing sounds.

This may very be an issue with pulseaudio and/or gstreamer (which I understand is used by playsound) rather than playsound, but it's almost like playing too many sounds asynchronously screws something up somewhere as though something is not being cleaned up properly after each asynchronous clip is done playing, and some limit is reached. In any case let me know whether my test program does or does not reproduce this problem or anything similar for anyone else.

Lastly, I did check that all the tests pass just fine with the latest playsound cloned from here.

If it matters, I am on Ubuntu 20.04.3 LTS and using Python 3.8.10. Please let me know if there's any other info I can gather or other scenarios to try.

kyp44 commented 2 years ago

Was just able to reproduce this on a different machine, though this is also running Ubuntu 20.04.3 LTS with Python 3.8.10.

bbbart commented 2 years ago

I'm not sure if this is related, but it have the same smell...

I have a simple application that, from time to time, plays a short WAV file. When I play with with block=False, I can play it exactly four times (consistently four) and then it stops working. With 'stops working' I mean that the Python app works just fine, no complaints there, but that there is no sound audible through the speakers. Also, other software supposedly playing sounds stop producing any.

What I see, is that every time playsound is being triggered, another line is permanently added to the the listing produced by lsof /dev/snd/pcmC0D0p.

When I set block=True, however, this problem is solved (but it of course creates other problems for my use case :-)). Then, lsof only lists my app while the sound is being played.

I looks like the application launched by playsound in non-blocking mode never releases its grip on the sound device.

Does anyone here perhaps have a clue how to avoid this problem?

Platform: Raspberry Pi, Linux kernel 5.10.103-v7l+, Python 3.7.3, playsound 1.3.0