RetroArch/audio/drivers/alsa.c

313 lines
7.9 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2010-05-28 16:21:33 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2010-05-28 16:21:33 +00:00
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
2012-04-21 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2010-05-28 16:21:33 +00:00
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2010-05-28 16:21:33 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2015-01-12 04:05:56 +00:00
#include "../../driver.h"
2010-05-28 13:41:38 +00:00
#include <stdlib.h>
#include <alsa/asoundlib.h>
2015-01-12 04:05:56 +00:00
#include "../../general.h"
2010-05-28 13:41:38 +00:00
2011-10-15 10:54:47 +00:00
#define TRY_ALSA(x) if (x < 0) { \
2010-05-28 13:41:38 +00:00
goto error; \
}
typedef struct alsa
{
snd_pcm_t *pcm;
size_t buffer_size;
2010-08-16 17:16:03 +00:00
bool nonblock;
bool has_float;
bool can_pause;
bool is_paused;
2010-05-28 13:41:38 +00:00
} alsa_t;
static bool alsa_use_float(void *data)
{
2011-12-24 12:46:12 +00:00
alsa_t *alsa = (alsa_t*)data;
return alsa->has_float;
}
static bool find_float_format(snd_pcm_t *pcm, void *data)
{
snd_pcm_hw_params_t *params = (snd_pcm_hw_params_t*)data;
2015-01-10 03:26:19 +00:00
if (snd_pcm_hw_params_test_format(pcm, params, SND_PCM_FORMAT_FLOAT) == 0)
{
2012-04-21 21:25:32 +00:00
RARCH_LOG("ALSA: Using floating point format.\n");
return true;
}
2015-01-10 03:26:19 +00:00
2012-04-21 21:25:32 +00:00
RARCH_LOG("ALSA: Using signed 16-bit format.\n");
return false;
}
static void *alsa_init(const char *device, unsigned rate, unsigned latency)
2010-05-28 13:41:38 +00:00
{
2015-01-10 03:26:19 +00:00
snd_pcm_format_t format;
snd_pcm_uframes_t buffer_size;
2010-05-28 13:41:38 +00:00
snd_pcm_hw_params_t *params = NULL;
snd_pcm_sw_params_t *sw_params = NULL;
2010-05-28 13:41:38 +00:00
unsigned latency_usec = latency * 1000;
unsigned channels = 2;
2011-12-24 12:46:12 +00:00
unsigned periods = 4;
const char *alsa_dev = "default";
2015-01-10 03:26:19 +00:00
alsa_t *alsa = (alsa_t*)calloc(1, sizeof(alsa_t));
if (!alsa)
return NULL;
if (device)
alsa_dev = device;
2010-05-28 13:41:38 +00:00
2014-09-09 20:24:29 +00:00
TRY_ALSA(snd_pcm_open(
&alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK));
2010-05-28 13:41:38 +00:00
TRY_ALSA(snd_pcm_hw_params_malloc(&params));
alsa->has_float = find_float_format(alsa->pcm, params);
2011-12-24 12:46:12 +00:00
format = alsa->has_float ? SND_PCM_FORMAT_FLOAT : SND_PCM_FORMAT_S16;
2010-05-28 13:41:38 +00:00
TRY_ALSA(snd_pcm_hw_params_any(alsa->pcm, params));
2014-09-09 20:24:29 +00:00
TRY_ALSA(snd_pcm_hw_params_set_access(
alsa->pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED));
2010-05-28 13:41:38 +00:00
TRY_ALSA(snd_pcm_hw_params_set_format(alsa->pcm, params, format));
TRY_ALSA(snd_pcm_hw_params_set_channels(alsa->pcm, params, channels));
TRY_ALSA(snd_pcm_hw_params_set_rate(alsa->pcm, params, rate, 0));
2010-05-28 13:59:49 +00:00
2014-09-09 20:24:29 +00:00
TRY_ALSA(snd_pcm_hw_params_set_buffer_time_near(
alsa->pcm, params, &latency_usec, NULL));
TRY_ALSA(snd_pcm_hw_params_set_periods_near(
alsa->pcm, params, &periods, NULL));
2010-05-28 13:41:38 +00:00
TRY_ALSA(snd_pcm_hw_params(alsa->pcm, params));
2014-09-09 20:24:29 +00:00
/* Shouldn't have to bother with this,
* but some drivers are apparently broken. */
if (snd_pcm_hw_params_get_period_size(params, &buffer_size, NULL))
snd_pcm_hw_params_get_period_size_min(params, &buffer_size, NULL);
2012-04-21 21:25:32 +00:00
RARCH_LOG("ALSA: Period size: %d frames\n", (int)buffer_size);
if (snd_pcm_hw_params_get_buffer_size(params, &buffer_size))
snd_pcm_hw_params_get_buffer_size_max(params, &buffer_size);
2012-04-21 21:25:32 +00:00
RARCH_LOG("ALSA: Buffer size: %d frames\n", (int)buffer_size);
alsa->buffer_size = snd_pcm_frames_to_bytes(alsa->pcm, buffer_size);
alsa->can_pause = snd_pcm_hw_params_can_pause(params);
RARCH_LOG("ALSA: Can pause: %s.\n", alsa->can_pause ? "yes" : "no");
2010-05-28 13:41:38 +00:00
TRY_ALSA(snd_pcm_sw_params_malloc(&sw_params));
TRY_ALSA(snd_pcm_sw_params_current(alsa->pcm, sw_params));
2014-09-09 20:24:29 +00:00
TRY_ALSA(snd_pcm_sw_params_set_start_threshold(
alsa->pcm, sw_params, buffer_size / 2));
TRY_ALSA(snd_pcm_sw_params(alsa->pcm, sw_params));
2010-05-28 13:41:38 +00:00
snd_pcm_hw_params_free(params);
snd_pcm_sw_params_free(sw_params);
2010-05-28 13:41:38 +00:00
return alsa;
error:
2012-04-21 21:25:32 +00:00
RARCH_ERR("ALSA: Failed to initialize...\n");
if (params)
2010-05-28 13:41:38 +00:00
snd_pcm_hw_params_free(params);
if (sw_params)
snd_pcm_sw_params_free(sw_params);
if (alsa)
2010-05-28 13:41:38 +00:00
{
if (alsa->pcm)
2010-05-28 13:41:38 +00:00
snd_pcm_close(alsa->pcm);
free(alsa);
}
return NULL;
}
2012-04-25 17:49:11 +00:00
static ssize_t alsa_write(void *data, const void *buf_, size_t size_)
2010-05-28 13:41:38 +00:00
{
2015-01-10 03:26:19 +00:00
alsa_t *alsa = (alsa_t*)data;
const uint8_t *buf = (const uint8_t*)buf_;
2012-09-10 19:25:03 +00:00
bool eagain_retry = true;
2010-08-17 21:06:44 +00:00
snd_pcm_sframes_t written = 0;
2012-09-10 19:25:03 +00:00
snd_pcm_sframes_t size = snd_pcm_bytes_to_frames(alsa->pcm, size_);
2010-05-28 13:41:38 +00:00
2012-04-25 17:49:11 +00:00
while (size)
2010-05-28 13:41:38 +00:00
{
2015-01-10 03:26:19 +00:00
snd_pcm_sframes_t frames;
2010-08-17 21:06:44 +00:00
if (!alsa->nonblock)
{
2012-04-25 17:49:11 +00:00
int rc = snd_pcm_wait(alsa->pcm, -1);
2015-01-10 03:26:19 +00:00
2012-04-25 17:49:11 +00:00
if (rc == -EPIPE || rc == -ESTRPIPE || rc == -EINTR)
2010-08-17 21:06:44 +00:00
{
if (snd_pcm_recover(alsa->pcm, rc, 1) < 0)
2012-09-10 19:25:03 +00:00
{
RARCH_ERR("[ALSA]: (#1) Failed to recover from error (%s)\n",
snd_strerror(rc));
2010-08-17 21:06:44 +00:00
return -1;
2012-09-10 19:25:03 +00:00
}
2010-08-17 21:06:44 +00:00
continue;
}
}
2015-01-10 03:26:19 +00:00
frames = snd_pcm_writei(alsa->pcm, buf, size);
2010-08-17 21:06:44 +00:00
2011-10-15 10:54:47 +00:00
if (frames == -EPIPE || frames == -EINTR || frames == -ESTRPIPE)
2010-08-17 21:06:44 +00:00
{
2011-10-15 10:54:47 +00:00
if (snd_pcm_recover(alsa->pcm, frames, 1) < 0)
2012-09-10 19:25:03 +00:00
{
RARCH_ERR("[ALSA]: (#2) Failed to recover from error (%s)\n",
snd_strerror(frames));
2010-08-17 21:06:44 +00:00
return -1;
2012-09-10 19:25:03 +00:00
}
2010-08-17 21:06:44 +00:00
2012-04-25 17:49:11 +00:00
break;
2010-08-17 21:06:44 +00:00
}
2014-09-09 20:24:29 +00:00
else if (frames == -EAGAIN && !alsa->nonblock)
2012-09-10 19:25:03 +00:00
{
2014-09-09 20:24:29 +00:00
/* Definitely not supposed to happen. */
2012-09-10 19:25:03 +00:00
RARCH_WARN("[ALSA]: poll() was signaled, but EAGAIN returned from write.\n"
"Your ALSA driver might be subtly broken.\n");
if (eagain_retry)
{
eagain_retry = false;
continue;
}
2014-08-27 01:11:06 +00:00
return written;
2012-09-10 19:25:03 +00:00
}
2014-09-09 20:24:29 +00:00
else if (frames == -EAGAIN) /* Expected if we're running nonblock. */
2012-09-14 19:41:58 +00:00
return written;
2011-10-15 10:54:47 +00:00
else if (frames < 0)
2012-09-10 19:25:03 +00:00
{
2014-09-09 20:24:29 +00:00
RARCH_ERR("[ALSA]: Unknown error occured (%s).\n",
snd_strerror(frames));
2010-05-28 13:41:38 +00:00
return -1;
2012-09-10 19:25:03 +00:00
}
2010-05-28 13:41:38 +00:00
2010-08-17 21:06:44 +00:00
written += frames;
2014-09-09 20:24:29 +00:00
buf += (frames << 1) *
(alsa->has_float ? sizeof(float) : sizeof(int16_t));
2012-09-10 19:25:03 +00:00
size -= frames;
2010-05-28 13:41:38 +00:00
}
2012-04-25 17:49:11 +00:00
return written;
2010-05-28 13:41:38 +00:00
}
static bool alsa_alive(void *data)
{
alsa_t *alsa = (alsa_t*)data;
2015-01-10 03:26:19 +00:00
if (!alsa)
return false;
return !alsa->is_paused;
}
static bool alsa_stop(void *data)
2010-05-28 13:41:38 +00:00
{
alsa_t *alsa = (alsa_t*)data;
2014-08-27 01:11:06 +00:00
if (alsa->can_pause
&& !alsa->is_paused)
{
int ret = snd_pcm_pause(alsa->pcm, 1);
if (ret < 0)
return false;
alsa->is_paused = true;
}
2014-08-27 01:11:06 +00:00
return true;
2010-05-28 13:41:38 +00:00
}
static void alsa_set_nonblock_state(void *data, bool state)
2010-08-16 17:16:03 +00:00
{
2011-12-24 12:46:12 +00:00
alsa_t *alsa = (alsa_t*)data;
2010-08-17 21:06:44 +00:00
alsa->nonblock = state;
2010-08-16 17:16:03 +00:00
}
static bool alsa_start(void *data)
2010-05-28 13:41:38 +00:00
{
alsa_t *alsa = (alsa_t*)data;
if (alsa->can_pause
&& alsa->is_paused)
{
int ret = snd_pcm_pause(alsa->pcm, 0);
if (ret < 0)
{
2014-09-09 20:24:29 +00:00
RARCH_ERR("[ALSA]: Failed to unpause: %s.\n",
snd_strerror(ret));
return false;
}
alsa->is_paused = false;
}
2014-08-27 01:11:06 +00:00
return true;
2010-05-28 13:41:38 +00:00
}
static void alsa_free(void *data)
2010-05-28 13:41:38 +00:00
{
2011-12-24 12:46:12 +00:00
alsa_t *alsa = (alsa_t*)data;
2012-04-25 17:49:11 +00:00
2011-10-15 10:54:47 +00:00
if (alsa)
2010-05-28 13:41:38 +00:00
{
2011-10-15 10:54:47 +00:00
if (alsa->pcm)
2010-05-28 13:41:38 +00:00
{
snd_pcm_drop(alsa->pcm);
snd_pcm_close(alsa->pcm);
}
free(alsa);
}
}
static size_t alsa_write_avail(void *data)
{
alsa_t *alsa = (alsa_t*)data;
snd_pcm_sframes_t avail = snd_pcm_avail(alsa->pcm);
2015-01-10 03:26:19 +00:00
if (avail < 0)
{
2014-09-09 20:24:29 +00:00
#if 0
RARCH_WARN("[ALSA]: snd_pcm_avail() failed: %s\n",
snd_strerror(avail));
#endif
return alsa->buffer_size;
}
return snd_pcm_frames_to_bytes(alsa->pcm, avail);
}
static size_t alsa_buffer_size(void *data)
{
alsa_t *alsa = (alsa_t*)data;
return alsa->buffer_size;
}
2014-09-11 05:06:20 +00:00
audio_driver_t audio_alsa = {
2011-12-24 12:46:12 +00:00
alsa_init,
alsa_write,
alsa_stop,
alsa_start,
alsa_alive,
2011-12-24 12:46:12 +00:00
alsa_set_nonblock_state,
alsa_free,
alsa_use_float,
"alsa",
alsa_write_avail,
alsa_buffer_size,
2010-05-28 13:41:38 +00:00
};