diff options
author | Themaister <maister@archlinux.us> | 2010-05-12 13:44:26 +0200 |
---|---|---|
committer | Alexey Yakovenko <wakeroid@gmail.com> | 2010-05-12 21:28:22 +0200 |
commit | 599a24ec50818e0a389c4fe73758acddcd048865 (patch) | |
tree | 03ee3d601aefec29be0ffad64bf0add4345a9c8e /plugins | |
parent | f9367cdd7fe79619630193357967ed41200f6f8b (diff) |
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.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/alsa/alsa.c | 16 | ||||
-rw-r--r-- | plugins/oss/oss.c | 18 |
2 files changed, 19 insertions, 15 deletions
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 |