2011-01-17 19:54:58 +00:00
|
|
|
/* SSNES - A Super Nintendo Entertainment System (SNES) Emulator frontend for libsnes.
|
2011-01-23 19:29:28 +00:00
|
|
|
* Copyright (C) 2010-2011 - Hans-Kristian Arntzen
|
2010-05-28 16:21:33 +00:00
|
|
|
*
|
|
|
|
* Some code herein may be based on code found in BSNES.
|
|
|
|
*
|
|
|
|
* SSNES is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* SSNES is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with SSNES.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-05-28 13:41:38 +00:00
|
|
|
#include "driver.h"
|
|
|
|
#include <stdlib.h>
|
2011-06-25 15:15:58 +00:00
|
|
|
#include <asoundlib.h>
|
2011-01-15 19:37:42 +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;
|
2010-08-16 17:16:03 +00:00
|
|
|
bool nonblock;
|
2011-01-15 19:37:42 +00:00
|
|
|
bool has_float;
|
2010-05-28 13:41:38 +00:00
|
|
|
} alsa_t;
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static bool alsa_use_float(void *data)
|
2011-01-15 19:37:42 +00:00
|
|
|
{
|
|
|
|
alsa_t *alsa = data;
|
|
|
|
return alsa->has_float;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool find_float_format(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
|
|
|
|
{
|
|
|
|
if (snd_pcm_hw_params_test_format(pcm, params, SND_PCM_FORMAT_FLOAT) == 0)
|
|
|
|
{
|
|
|
|
SSNES_LOG("ALSA: Using floating point format.\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
SSNES_LOG("ALSA: Using signed 16-bit format.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static void *alsa_init(const char *device, unsigned rate, unsigned latency)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
|
|
|
alsa_t *alsa = calloc(1, sizeof(alsa_t));
|
2011-10-15 10:54:47 +00:00
|
|
|
if (!alsa)
|
2010-05-28 13:41:38 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
snd_pcm_hw_params_t *params = NULL;
|
2011-01-15 20:11:22 +00:00
|
|
|
snd_pcm_sw_params_t *sw_params = NULL;
|
2010-05-28 13:41:38 +00:00
|
|
|
|
|
|
|
const char *alsa_dev = "default";
|
2011-10-15 10:54:47 +00:00
|
|
|
if (device)
|
2010-05-28 13:41:38 +00:00
|
|
|
alsa_dev = device;
|
|
|
|
|
2010-08-17 21:06:44 +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
|
|
|
|
|
|
|
unsigned int latency_usec = latency * 1000;
|
|
|
|
|
|
|
|
TRY_ALSA(snd_pcm_hw_params_malloc(¶ms));
|
2011-01-15 19:37:42 +00:00
|
|
|
alsa->has_float = find_float_format(alsa->pcm, params);
|
|
|
|
snd_pcm_format_t format = alsa->has_float ? SND_PCM_FORMAT_FLOAT : SND_PCM_FORMAT_S16;
|
|
|
|
unsigned int channels = 2;
|
2010-05-28 13:41:38 +00:00
|
|
|
|
|
|
|
TRY_ALSA(snd_pcm_hw_params_any(alsa->pcm, params));
|
|
|
|
TRY_ALSA(snd_pcm_hw_params_set_access(alsa->pcm, params, SND_PCM_ACCESS_RW_INTERLEAVED));
|
|
|
|
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
|
|
|
|
2011-01-15 19:37:42 +00:00
|
|
|
TRY_ALSA(snd_pcm_hw_params_set_buffer_time_near(alsa->pcm, params, &latency_usec, NULL));
|
|
|
|
unsigned periods = 4;
|
|
|
|
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));
|
|
|
|
|
2010-05-28 18:58:56 +00:00
|
|
|
snd_pcm_uframes_t buffer_size;
|
2011-01-15 19:37:42 +00:00
|
|
|
snd_pcm_hw_params_get_period_size(params, &buffer_size, NULL);
|
|
|
|
SSNES_LOG("ALSA: Period size: %d frames\n", (int)buffer_size);
|
2010-05-28 18:58:56 +00:00
|
|
|
snd_pcm_hw_params_get_buffer_size(params, &buffer_size);
|
2011-01-15 19:37:42 +00:00
|
|
|
SSNES_LOG("ALSA: Buffer size: %d frames\n", (int)buffer_size);
|
2010-05-28 13:41:38 +00:00
|
|
|
|
2011-01-15 20:11:22 +00:00
|
|
|
TRY_ALSA(snd_pcm_sw_params_malloc(&sw_params));
|
|
|
|
TRY_ALSA(snd_pcm_sw_params_current(alsa->pcm, sw_params));
|
|
|
|
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);
|
2011-01-15 20:11:22 +00:00
|
|
|
snd_pcm_sw_params_free(sw_params);
|
2010-05-28 13:41:38 +00:00
|
|
|
|
|
|
|
return alsa;
|
|
|
|
|
|
|
|
error:
|
2011-01-15 19:37:42 +00:00
|
|
|
SSNES_ERR("ALSA: Failed to initialize...\n");
|
|
|
|
if (params)
|
2010-05-28 13:41:38 +00:00
|
|
|
snd_pcm_hw_params_free(params);
|
|
|
|
|
2011-01-15 20:11:22 +00:00
|
|
|
if (sw_params)
|
|
|
|
snd_pcm_sw_params_free(sw_params);
|
|
|
|
|
2011-01-15 19:37:42 +00:00
|
|
|
if (alsa)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
2011-01-15 19:37:42 +00:00
|
|
|
if (alsa->pcm)
|
2010-05-28 13:41:38 +00:00
|
|
|
snd_pcm_close(alsa->pcm);
|
|
|
|
|
|
|
|
free(alsa);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static ssize_t alsa_write(void *data, const void *buf, size_t size)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
|
|
|
alsa_t *alsa = data;
|
|
|
|
|
|
|
|
snd_pcm_sframes_t frames;
|
2010-08-17 21:06:44 +00:00
|
|
|
snd_pcm_sframes_t written = 0;
|
|
|
|
int rc;
|
2011-01-15 19:37:42 +00:00
|
|
|
size = snd_pcm_bytes_to_frames(alsa->pcm, size); // Frames to write
|
2010-05-28 13:41:38 +00:00
|
|
|
|
2010-08-17 21:06:44 +00:00
|
|
|
while (written < size)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
2010-08-17 21:06:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-15 19:37:42 +00:00
|
|
|
frames = snd_pcm_writei(alsa->pcm, (const char*)buf + written * 2 * (alsa->has_float ? sizeof(float) : sizeof(int16_t)), size - written);
|
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)
|
2010-08-17 21:06:44 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2011-10-15 10:54:47 +00:00
|
|
|
else if (frames == -EAGAIN && alsa->nonblock)
|
2010-08-17 21:06:44 +00:00
|
|
|
return 0;
|
2011-10-15 10:54:47 +00:00
|
|
|
else if (frames < 0)
|
2010-05-28 13:41:38 +00:00
|
|
|
return -1;
|
|
|
|
|
2010-08-17 21:06:44 +00:00
|
|
|
written += frames;
|
2010-05-28 13:41:38 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 21:06:44 +00:00
|
|
|
return snd_pcm_frames_to_bytes(alsa->pcm, size);
|
2010-05-28 13:41:38 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static bool alsa_stop(void *data)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static void alsa_set_nonblock_state(void *data, bool state)
|
2010-08-16 17:16:03 +00:00
|
|
|
{
|
|
|
|
alsa_t *alsa = data;
|
2010-08-17 21:06:44 +00:00
|
|
|
alsa->nonblock = state;
|
2010-08-16 17:16:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static bool alsa_start(void *data)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static void alsa_free(void *data)
|
2010-05-28 13:41:38 +00:00
|
|
|
{
|
|
|
|
alsa_t *alsa = data;
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const audio_driver_t audio_alsa = {
|
2011-11-02 19:04:31 +00:00
|
|
|
.init = alsa_init,
|
|
|
|
.write = alsa_write,
|
|
|
|
.stop = alsa_stop,
|
|
|
|
.start = alsa_start,
|
|
|
|
.use_float = alsa_use_float,
|
|
|
|
.set_nonblock_state = alsa_set_nonblock_state,
|
|
|
|
.free = alsa_free,
|
2010-12-29 19:05:57 +00:00
|
|
|
.ident = "alsa"
|
2010-05-28 13:41:38 +00:00
|
|
|
};
|
|
|
|
|