diff --git a/alsa.c b/alsa.c index 07d8c631f8..5dece09311 100644 --- a/alsa.c +++ b/alsa.c @@ -45,7 +45,7 @@ static void* __alsa_init(const char* device, int rate, int latency) //fprintf(stderr, "Opening device: %s\n", alsa_dev); - TRY_ALSA(snd_pcm_open(&alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, 0)); + TRY_ALSA(snd_pcm_open(&alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)); unsigned int latency_usec = latency * 1000; @@ -117,21 +117,41 @@ static ssize_t __alsa_write(void* data, const void* buf, size_t size) alsa_t *alsa = data; snd_pcm_sframes_t frames; - frames = snd_pcm_writei(alsa->pcm, buf, snd_pcm_bytes_to_frames(alsa->pcm, size)); + snd_pcm_sframes_t written = 0; + int rc; + size /= 4; // Frames to write - if ( frames == -EPIPE || frames == -EINTR || frames == -ESTRPIPE ) + while (written < size) { - if ( snd_pcm_recover(alsa->pcm, frames, 1) < 0 ) + if (!alsa->nonblock) + { + rc = snd_pcm_wait(alsa->pcm, -1); + if (rc == -EPIPE || rc == -ESTRPIPE) + { + if (snd_pcm_recover(alsa->pcm, rc, 1) < 0) + return -1; + continue; + } + } + + frames = snd_pcm_writei(alsa->pcm, (const uint32_t*)buf + written, size - written); + + if ( frames == -EPIPE || frames == -EINTR || frames == -ESTRPIPE ) + { + if ( snd_pcm_recover(alsa->pcm, frames, 1) < 0 ) + return -1; + + return 0; + } + else if ( frames == -EAGAIN && alsa->nonblock ) + return 0; + else if ( frames < 0 ) return -1; - return size; + written += frames; } - else if ( alsa->nonblock && frames == -EAGAIN ) - return 0; - else if ( frames < 0 ) - return -1; - return snd_pcm_frames_to_bytes(alsa->pcm, frames); + return snd_pcm_frames_to_bytes(alsa->pcm, size); } static bool __alsa_stop(void *data) @@ -142,10 +162,7 @@ static bool __alsa_stop(void *data) static void __alsa_set_nonblock_state(void *data, bool state) { alsa_t *alsa = data; - if (snd_pcm_nonblock(alsa->pcm, state) < 0) - fprintf(stderr, "SSNES [ERROR]: Could not set PCM to non-blocking. Will not be able to fast-forward.\n"); - else - alsa->nonblock = state; + alsa->nonblock = state; } static bool __alsa_start(void *data) diff --git a/roar.c b/roar.c index 3e93ad6a81..db94f0768b 100644 --- a/roar.c +++ b/roar.c @@ -21,33 +21,54 @@ #include #include #include +#include + +typedef struct +{ + roar_vs_t *vss; + bool nonblocking; +} roar_t; static void* __roar_init(const char* device, int rate, int latency) { int err; + roar_t *roar = calloc(1, sizeof(roar_t)); + if (roar == NULL) + return NULL; + roar_vs_t *vss; if ( (vss = roar_vs_new_simple(NULL, NULL, rate, 2, ROAR_CODEC_PCM_S_LE, 16, ROAR_DIR_PLAY, &err)) == NULL ) { fprintf(stderr, "roar_vs: \"%s\"\n", roar_vs_strerr(err)); + free(roar); return NULL; } - return vss; + roar->vss = vss; + + return roar; } static ssize_t __roar_write(void* data, const void* buf, size_t size) { - roar_vs_t *vss = data; + roar_t *roar = data; + ssize_t rc; if ( size == 0 ) return 0; int err; - if (roar_vs_write(vss, buf, size, &err) < 0) + size_t written = 0; + while (written < size) { - if (err == ROAR_ERROR_NONE) - return 0; - return -1; + if ((rc = roar_vs_write(roar->vss, (const char*)buf + written, size - written, &err)) < size) + { + if (rc < 0) + return -1; + else if (roar->nonblocking) + return 0; + } + written += rc; } return size; @@ -55,25 +76,29 @@ static ssize_t __roar_write(void* data, const void* buf, size_t size) static bool __roar_stop(void *data) { + (void)data; return true; } static void __roar_set_nonblock_state(void *data, bool state) { - roar_vs_t *vss = data; - if (roar_vs_blocking(vss, (state) ? ROAR_VS_FALSE : ROAR_VS_TRUE, NULL) < 0) + roar_t *roar = data; + if (roar_vs_blocking(roar->vss, (state) ? ROAR_VS_FALSE : ROAR_VS_TRUE, NULL) < 0) fprintf(stderr, "SSNES [ERROR]: Can't set nonblocking. Will not be able to fast-forward.\n"); + roar->nonblocking = state; } static bool __roar_start(void *data) { + (void)data; return true; } static void __roar_free(void *data) { - roar_vs_t *vss = data; - roar_vs_close(vss, ROAR_VS_TRUE, NULL); + roar_t *roar = data; + roar_vs_close(roar->vss, ROAR_VS_TRUE, NULL); + free(data); } const audio_driver_t audio_roar = { diff --git a/ssnes.c b/ssnes.c index eb85c41e13..d0d46cc2eb 100644 --- a/ssnes.c +++ b/ssnes.c @@ -77,7 +77,7 @@ static void save_file(const char* path, int type); // To avoid continous switching if we hold the button down, we require that the button must go from pressed, unpressed back to pressed to be able to toggle between then. #define AUDIO_CHUNK_SIZE_BLOCKING 64 -#define AUDIO_CHUNK_SIZE_NONBLOCKING 1024 // So we don't get complete line-noise when fast-forwarding audio. +#define AUDIO_CHUNK_SIZE_NONBLOCKING 2048 // So we don't get complete line-noise when fast-forwarding audio. static size_t audio_chunk_size = AUDIO_CHUNK_SIZE_BLOCKING; void set_fast_forward_button(bool new_button_state) {