From 8ea9470454572c3ea8b3d2bc12f2a5824d4921f4 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Fri, 5 Aug 2022 11:03:43 +0200 Subject: [PATCH] fix draining/closing, take 2 commit 8abf06be introduced a pause() prior to draining, in an attempt to work around clearly broken pulseaudio client behavior for capture streams (drain() is supposed to imply a stop). but as the workaround was also applied to playback streams, it would cause nasty "clicks", as the stream would (obviously) stop before being resumed for draining. but draining is actually pointless for capture streams, as we're closing right afterwards, so the samples are lost anyway. what's more, destructors are not supposed to wait for anything, so draining in alsapcm_dealloc() was wrong to start with. so we remove it. note that this is a minor behavior change, which is reflected by the adjustment of the playback test to have an explicit close() at the end. finally, close() was also affected by the pulseaudio bug (which was not addressed before), so there we make draining exclusive to playback streams. --- alsaaudio.c | 8 +++----- playbacktest.py | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/alsaaudio.c b/alsaaudio.c index 1b60f57..b9d0407 100644 --- a/alsaaudio.c +++ b/alsaaudio.c @@ -528,11 +528,8 @@ alsapcm_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static void alsapcm_dealloc(alsapcm_t *self) { - if (self->handle) { - snd_pcm_pause(self->handle, 1); - snd_pcm_drain(self->handle); + if (self->handle) snd_pcm_close(self->handle); - } free(self->cardname); PyObject_Del(self); } @@ -546,7 +543,8 @@ alsapcm_close(alsapcm_t *self, PyObject *args) if (self->handle) { Py_BEGIN_ALLOW_THREADS - snd_pcm_drain(self->handle); + if (self->pcmtype == SND_PCM_STREAM_PLAYBACK) + snd_pcm_drain(self->handle); snd_pcm_close(self->handle); Py_END_ALLOW_THREADS diff --git a/playbacktest.py b/playbacktest.py index 874415b..74232ca 100755 --- a/playbacktest.py +++ b/playbacktest.py @@ -49,5 +49,5 @@ if __name__ == '__main__': while data: out.write(data) data = f.read(320) - + out.close()