From 5c9c4f42c36702ccf010288c8bf32c9800babf97 Mon Sep 17 00:00:00 2001 From: Themaister Date: Sat, 15 Jan 2011 20:37:42 +0100 Subject: [PATCH] Clean up float handling a bit. ALSA now checks if floating point is supported to avoid one extra conversion, also clean up the driver itself a bit. --- audio/alsa.c | 74 +++++++++++++++++++++++++--------------------------- audio/jack.c | 8 +++++- driver.c | 2 ++ driver.h | 2 +- general.h | 2 ++ ssnes.c | 2 +- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/audio/alsa.c b/audio/alsa.c index 4e6145141e..0d759599f8 100644 --- a/audio/alsa.c +++ b/audio/alsa.c @@ -19,6 +19,7 @@ #include "driver.h" #include #include +#include "general.h" #define TRY_ALSA(x) if ( x < 0 ) { \ goto error; \ @@ -28,8 +29,26 @@ typedef struct alsa { snd_pcm_t *pcm; bool nonblock; + bool has_float; } alsa_t; +static bool __alsa_use_float(void *data) +{ + 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; +} + static void* __alsa_init(const char* device, int rate, int latency) { alsa_t *alsa = calloc(1, sizeof(alsa_t)); @@ -37,22 +56,19 @@ static void* __alsa_init(const char* device, int rate, int latency) return NULL; snd_pcm_hw_params_t *params = NULL; - snd_pcm_sw_params_t *sw_params = NULL; const char *alsa_dev = "default"; if ( device != NULL ) alsa_dev = device; - //fprintf(stderr, "Opening device: %s\n", alsa_dev); - TRY_ALSA(snd_pcm_open(&alsa->pcm, alsa_dev, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)); unsigned int latency_usec = latency * 1000; - snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE; - unsigned int channels = 2; - TRY_ALSA(snd_pcm_hw_params_malloc(¶ms)); + 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; 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)); @@ -60,51 +76,30 @@ static void* __alsa_init(const char* device, int rate, int latency) 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)); - // We test if we can run the latencies we are allowed, if not, fallback to *_near. - - if ( snd_pcm_hw_params_set_buffer_time_max(alsa->pcm, params, &latency_usec, NULL) < 0) - { - latency_usec = latency * 1000; - TRY_ALSA(snd_pcm_hw_params_set_buffer_time_near(alsa->pcm, params, &latency_usec, NULL)) - } - - latency_usec = (latency < 32) ? 10000 : latency * 250; - if ( snd_pcm_hw_params_set_period_time_max(alsa->pcm, params, &latency_usec, NULL) ) - { - latency_usec = (latency < 32) ? 10000 : latency * 250; - TRY_ALSA(snd_pcm_hw_params_set_period_time_near(alsa->pcm, params, &latency_usec, NULL)); - } + 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)); TRY_ALSA(snd_pcm_hw_params(alsa->pcm, params)); snd_pcm_uframes_t buffer_size; - //snd_pcm_hw_params_get_period_size(params, &buffer_size, NULL); - //fprintf(stderr, "ALSA Period size: %d frames\n", (int)alsa_sizes); + snd_pcm_hw_params_get_period_size(params, &buffer_size, NULL); + SSNES_LOG("ALSA: Period size: %d frames\n", (int)buffer_size); snd_pcm_hw_params_get_buffer_size(params, &buffer_size); - //fprintf(stderr, "Buffer size: %d frames\n", (int)alsa_sizes); + SSNES_LOG("ALSA: Buffer size: %d frames\n", (int)buffer_size); - if (snd_pcm_sw_params_malloc(&sw_params) < 0) - goto error; - - 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)); - - snd_pcm_sw_params_free(sw_params); snd_pcm_hw_params_free(params); return alsa; error: - if ( params != NULL ) + SSNES_ERR("ALSA: Failed to initialize...\n"); + if (params) snd_pcm_hw_params_free(params); - if ( sw_params != NULL ) - snd_pcm_sw_params_free(sw_params); - - if ( alsa != NULL ) + if (alsa) { - if ( alsa->pcm != NULL ) + if (alsa->pcm) snd_pcm_close(alsa->pcm); free(alsa); @@ -119,7 +114,7 @@ static ssize_t __alsa_write(void* data, const void* buf, size_t size) snd_pcm_sframes_t frames; snd_pcm_sframes_t written = 0; int rc; - size /= 4; // Frames to write + size = snd_pcm_bytes_to_frames(alsa->pcm, size); // Frames to write while (written < size) { @@ -134,7 +129,7 @@ static ssize_t __alsa_write(void* data, const void* buf, size_t size) } } - frames = snd_pcm_writei(alsa->pcm, (const uint32_t*)buf + written, size - written); + frames = snd_pcm_writei(alsa->pcm, (const char*)buf + written * 2 * (alsa->has_float ? sizeof(float) : sizeof(int16_t)), size - written); if ( frames == -EPIPE || frames == -EINTR || frames == -ESTRPIPE ) { @@ -189,6 +184,7 @@ const audio_driver_t audio_alsa = { .write = __alsa_write, .stop = __alsa_stop, .start = __alsa_start, + .use_float = __alsa_use_float, .set_nonblock_state = __alsa_set_nonblock_state, .free = __alsa_free, .ident = "alsa" diff --git a/audio/jack.c b/audio/jack.c index 4e1edae821..fa2ce44f2a 100644 --- a/audio/jack.c +++ b/audio/jack.c @@ -285,6 +285,12 @@ static void __jack_free(void *data) free(jd); } +static bool __jack_use_float(void *data) +{ + (void)data; + return true; +} + const audio_driver_t audio_jack = { .init = __jack_init, .write = __jack_write, @@ -292,7 +298,7 @@ const audio_driver_t audio_jack = { .start = __jack_start, .set_nonblock_state = __jack_set_nonblock_state, .free = __jack_free, - .float_samples = true, + .use_float = __jack_use_float, .ident = "jack" }; diff --git a/driver.c b/driver.c index 4e78e6439b..b181457575 100644 --- a/driver.c +++ b/driver.c @@ -149,6 +149,8 @@ void init_audio(void) if ( driver.audio_data == NULL ) g_extern.audio_active = false; + if (g_extern.audio_active && driver.audio->use_float && driver.audio->use_float(driver.audio_data)) + g_extern.audio_data.use_float = true; if (!g_settings.audio.sync && g_extern.audio_active) { diff --git a/driver.h b/driver.h index 7b8303b1be..239ba30647 100644 --- a/driver.h +++ b/driver.h @@ -61,7 +61,7 @@ typedef struct audio_driver bool (*start)(void* data); void (*set_nonblock_state)(void* data, bool toggle); // Should we care about blocking in audio thread? Fast forwarding. void (*free)(void* data); - bool float_samples; // Defines if driver will take standard floating point samples, or int16_t samples. + bool (*use_float)(void *data); // Defines if driver will take standard floating point samples, or int16_t samples. const char *ident; } audio_driver_t; diff --git a/general.h b/general.h index 174257b3f5..c55a598427 100644 --- a/general.h +++ b/general.h @@ -125,6 +125,8 @@ struct global size_t nonblock_chunk_size; size_t block_chunk_size; + bool use_float; + float *outsamples; int16_t *conv_outsamples; } audio_data; diff --git a/ssnes.c b/ssnes.c index ab6201ceb3..b639fe19a6 100644 --- a/ssnes.c +++ b/ssnes.c @@ -176,7 +176,7 @@ static void audio_sample(uint16_t left, uint16_t right) src_process(g_extern.source, &src_data); - if (driver.audio->float_samples) + if (g_extern.audio_data.use_float) { if (driver.audio->write(driver.audio_data, g_extern.audio_data.outsamples, src_data.output_frames_gen * sizeof(float) * 2) < 0) {