From 599a24ec50818e0a389c4fe73758acddcd048865 Mon Sep 17 00:00:00 2001 From: Themaister Date: Wed, 12 May 2010 13:44:26 +0200 Subject: Fix audio stuttering at start of audio stream. Reading from the audio stream at the start of a stream may cause it to return 0, where the callback simply wrote 0s rather than wait for the stream to write audio back to it. --- plugins/alsa/alsa.c | 16 +++++++++------- plugins/oss/oss.c | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'plugins') diff --git a/plugins/alsa/alsa.c b/plugins/alsa/alsa.c index edc6d63a..bf51d9e2 100644 --- a/plugins/alsa/alsa.c +++ b/plugins/alsa/alsa.c @@ -58,7 +58,7 @@ static char conf_alsa_soundcard[100] = "default"; //static snd_async_handler_t *pcm_callback; -static void +static int palsa_callback (char *stream, int len); #if 0 @@ -497,9 +497,13 @@ palsa_thread (void *context) { int written = 0; snd_pcm_sframes_t frames_to_deliver = snd_pcm_avail_update (audio); while (frames_to_deliver >= period_size) { + err = 0; char buf[period_size * 4]; - palsa_callback (buf, period_size * 4); - err = snd_pcm_writei (audio, buf, period_size); + int bytes_to_write = palsa_callback (buf, period_size * 4); + + if ( bytes_to_write >= 4 ) + err = snd_pcm_writei (audio, buf, snd_pcm_bytes_to_frames(audio, bytes_to_write)); + if (err < 0) { if (err == -ESTRPIPE) { fprintf (stderr, "alsa: trying to recover from suspend... (error=%d, %s)\n", err, snd_strerror (err)); @@ -523,7 +527,7 @@ palsa_thread (void *context) { } } -static void +static int palsa_callback (char *stream, int len) { int bytesread = deadbeef->streamer_read (stream, len); @@ -564,9 +568,7 @@ palsa_callback (char *stream, int len) { ((int16_t*)stream)[i] = (int16_t)(((int32_t)(((int16_t*)stream)[i])) * ivolume / 1000); } #endif - if (bytesread < len) { - memset (stream + bytesread, 0, len-bytesread); - } + return bytesread; } static int diff --git a/plugins/oss/oss.c b/plugins/oss/oss.c index 53ba8575..c98a9f83 100644 --- a/plugins/oss/oss.c +++ b/plugins/oss/oss.c @@ -53,7 +53,7 @@ static uintptr_t mutex; static void oss_thread (void *context); -static void +static int oss_callback (char *stream, int len); static int @@ -244,13 +244,17 @@ oss_thread (void *context) { usleep (10000); continue; } + + int res = 0; char buf[BLOCKSIZE]; - oss_callback (buf, sizeof (buf)); + int write_size = oss_callback (buf, sizeof (buf)); deadbeef->mutex_lock (mutex); - int res = write (fd, buf, sizeof (buf)); + if ( write_size > 0 ) + res = write (fd, buf, write_size); + deadbeef->mutex_unlock (mutex); - if (res != sizeof (buf)) { + if (res != write_size) { perror ("oss write"); fprintf (stderr, "oss: failed to write buffer\n"); } @@ -258,7 +262,7 @@ oss_thread (void *context) { } } -static void +static int oss_callback (char *stream, int len) { int bytesread = deadbeef->streamer_read (stream, len); int16_t ivolume = deadbeef->volume_get_amp () * 1000; @@ -266,9 +270,7 @@ oss_callback (char *stream, int len) { ((int16_t*)stream)[i] = (int16_t)(((int32_t)(((int16_t*)stream)[i])) * ivolume / 1000); } - if (bytesread < len) { - memset (stream + bytesread, 0, len-bytesread); - } + return bytesread; } static int -- cgit v1.2.3