diff --git a/audio/alsa.c b/audio/alsa.c index 75a8b51f7b..da60885209 100644 --- a/audio/alsa.c +++ b/audio/alsa.c @@ -42,35 +42,37 @@ static bool alsa_use_float(void *data) static bool find_float_format(snd_pcm_t *pcm, void *data) { snd_pcm_hw_params_t *params = (snd_pcm_hw_params_t*)data; + if (snd_pcm_hw_params_test_format(pcm, params, SND_PCM_FORMAT_FLOAT) == 0) { RARCH_LOG("ALSA: Using floating point format.\n"); return true; } + RARCH_LOG("ALSA: Using signed 16-bit format.\n"); return false; } static void *alsa_init(const char *device, unsigned rate, unsigned latency) { - alsa_t *alsa = (alsa_t*)calloc(1, sizeof(alsa_t)); - if (!alsa) - return NULL; - + snd_pcm_format_t format; + snd_pcm_uframes_t buffer_size; snd_pcm_hw_params_t *params = NULL; snd_pcm_sw_params_t *sw_params = NULL; unsigned latency_usec = latency * 1000; unsigned channels = 2; unsigned periods = 4; - snd_pcm_format_t format; const char *alsa_dev = "default"; + alsa_t *alsa = (alsa_t*)calloc(1, sizeof(alsa_t)); + + if (!alsa) + return NULL; + if (device) alsa_dev = device; - snd_pcm_uframes_t buffer_size; - TRY_ALSA(snd_pcm_open( &alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)); TRY_ALSA(snd_pcm_hw_params_malloc(¶ms)); @@ -134,18 +136,20 @@ error: static ssize_t alsa_write(void *data, const void *buf_, size_t size_) { - alsa_t *alsa = (alsa_t*)data; - const uint8_t *buf = (const uint8_t*)buf_; - + alsa_t *alsa = (alsa_t*)data; + const uint8_t *buf = (const uint8_t*)buf_; bool eagain_retry = true; snd_pcm_sframes_t written = 0; snd_pcm_sframes_t size = snd_pcm_bytes_to_frames(alsa->pcm, size_); while (size) { + snd_pcm_sframes_t frames; + if (!alsa->nonblock) { int rc = snd_pcm_wait(alsa->pcm, -1); + if (rc == -EPIPE || rc == -ESTRPIPE || rc == -EINTR) { if (snd_pcm_recover(alsa->pcm, rc, 1) < 0) @@ -158,7 +162,7 @@ static ssize_t alsa_write(void *data, const void *buf_, size_t size_) } } - snd_pcm_sframes_t frames = snd_pcm_writei(alsa->pcm, buf, size); + frames = snd_pcm_writei(alsa->pcm, buf, size); if (frames == -EPIPE || frames == -EINTR || frames == -ESTRPIPE) { @@ -205,9 +209,9 @@ static ssize_t alsa_write(void *data, const void *buf_, size_t size_) static bool alsa_alive(void *data) { alsa_t *alsa = (alsa_t*)data; - if (alsa) - return !alsa->is_paused; - return false; + if (!alsa) + return false; + return !alsa->is_paused; } static bool alsa_stop(void *data) @@ -273,8 +277,8 @@ static void alsa_free(void *data) static size_t alsa_write_avail(void *data) { alsa_t *alsa = (alsa_t*)data; - snd_pcm_sframes_t avail = snd_pcm_avail(alsa->pcm); + if (avail < 0) { #if 0