RetroArch/audio/drivers/sdl_audio.c

279 lines
6.2 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2017-01-22 13:40:32 +01:00
* Copyright (C) 2011-2017 - Daniel De Matteis
*
2012-04-21 23:13:50 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-01-07 15:50:16 +01: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 23:13:50 +02:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-01-07 15:50:16 +01: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 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-01-07 15:50:16 +01:00
* If not, see <http://www.gnu.org/licenses/>.
*/
2019-06-17 12:49:21 +02:00
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
2015-09-14 03:44:06 +02:00
#include <boolean.h>
#include <rthreads/rthreads.h>
#include <queues/fifo_queue.h>
2015-03-15 04:41:11 +01:00
#include <retro_inline.h>
2017-06-28 04:41:38 +02:00
#include <retro_math.h>
2011-01-07 15:50:16 +01:00
2017-12-31 14:31:51 +01:00
#include "SDL.h"
#include "SDL_audio.h"
#include "../audio_driver.h"
#include "../../verbosity.h"
2011-01-07 15:50:16 +01:00
typedef struct sdl_audio
{
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2011-12-25 16:36:56 +01:00
slock_t *lock;
scond_t *cond;
2016-05-11 19:02:43 +02:00
#endif
2011-06-14 19:55:08 +02:00
fifo_buffer_t *buffer;
bool nonblock;
bool is_paused;
2011-01-07 15:50:16 +01:00
} sdl_audio_t;
static void sdl_audio_cb(void *data, Uint8 *stream, int len)
{
2016-09-08 11:41:58 +02:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
size_t avail = FIFO_READ_AVAIL(sdl->buffer);
2011-12-24 13:46:12 +01:00
size_t write_size = len > (int)avail ? avail : len;
2015-01-12 05:05:56 +01:00
2011-06-14 19:55:08 +02:00
fifo_read(sdl->buffer, stream, write_size);
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2011-12-25 16:36:56 +01:00
scond_signal(sdl->cond);
2016-05-11 19:02:43 +02:00
#endif
2011-01-07 15:50:16 +01:00
2015-01-12 05:05:56 +01:00
/* If underrun, fill rest with silence. */
2011-01-07 15:50:16 +01:00
memset(stream + write_size, 0, len - write_size);
}
2015-03-15 04:41:11 +01:00
static INLINE int find_num_frames(int rate, int latency)
{
int frames = (rate * latency) / 1000;
2015-01-12 05:05:56 +01:00
/* SDL only likes 2^n sized buffers. */
return next_pow2(frames);
}
2015-01-12 05:05:56 +01:00
static void *sdl_audio_init(const char *device,
unsigned rate, unsigned latency,
unsigned block_frames,
unsigned *new_rate)
2011-01-07 15:50:16 +01:00
{
2015-01-12 05:05:56 +01:00
int frames;
size_t bufsize;
SDL_AudioSpec out;
2021-03-23 12:46:25 +00:00
SDL_AudioSpec spec = {0};
void *tmp = NULL;
sdl_audio_t *sdl = NULL;
uint32_t sdl_subsystem_flags = SDL_WasInit(0);
2015-03-20 21:31:16 +01:00
(void)device;
2015-01-12 05:05:56 +01:00
2021-03-23 12:46:25 +00:00
/* Initialise audio subsystem, if required */
if (sdl_subsystem_flags == 0)
{
if (SDL_Init(SDL_INIT_AUDIO) < 0)
return NULL;
}
2021-03-23 12:46:25 +00:00
else if ((sdl_subsystem_flags & SDL_INIT_AUDIO) == 0)
{
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
return NULL;
}
2011-01-07 15:50:16 +01:00
2015-01-12 05:05:56 +01:00
sdl = (sdl_audio_t*)calloc(1, sizeof(*sdl));
2011-01-07 15:50:16 +01:00
if (!sdl)
return NULL;
/* We have to buffer up some data ourselves, so we let SDL
2015-01-12 05:05:56 +01:00
* carry approximately half of the latency.
*
* SDL double buffers audio and we do as well. */
2017-03-24 01:46:28 +01:00
frames = find_num_frames(rate, latency / 4);
2016-09-08 11:41:58 +02:00
spec.freq = rate;
spec.format = AUDIO_S16SYS;
2011-12-24 13:46:12 +01:00
spec.channels = 2;
2016-09-08 11:41:58 +02:00
spec.samples = frames; /* This is in audio frames, not samples ... :( */
2011-12-24 13:46:12 +01:00
spec.callback = sdl_audio_cb;
spec.userdata = sdl;
2011-01-07 15:50:16 +01:00
if (SDL_OpenAudio(&spec, &out) < 0)
{
2017-03-24 01:46:28 +01:00
RARCH_ERR("[SDL audio]: Failed to open SDL audio: %s\n", SDL_GetError());
goto error;
2011-01-07 15:50:16 +01:00
}
2015-03-20 21:31:16 +01:00
*new_rate = out.freq;
2011-01-07 15:50:16 +01:00
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2016-09-08 11:41:58 +02:00
sdl->lock = slock_new();
sdl->cond = scond_new();
2016-05-11 19:02:43 +02:00
#endif
2011-01-07 15:50:16 +01:00
RARCH_LOG("[SDL audio]: Requested %u ms latency, got %d ms\n",
latency, (int)(out.samples * 4 * 1000 / (*new_rate)));
2015-01-12 05:05:56 +01:00
/* Create a buffer twice as big as needed and prefill the buffer. */
2016-09-08 11:41:58 +02:00
bufsize = out.samples * 4 * sizeof(int16_t);
tmp = calloc(1, bufsize);
2011-06-14 19:55:08 +02:00
sdl->buffer = fifo_new(bufsize);
2015-01-12 05:05:56 +01:00
2011-01-07 15:50:16 +01:00
if (tmp)
{
2011-06-14 19:55:08 +02:00
fifo_write(sdl->buffer, tmp, bufsize);
2011-01-07 15:50:16 +01:00
free(tmp);
}
SDL_PauseAudio(0);
return sdl;
2017-03-24 01:46:28 +01:00
error:
free(sdl);
return NULL;
2011-01-07 15:50:16 +01:00
}
static ssize_t sdl_audio_write(void *data, const void *buf, size_t size)
2011-01-07 15:50:16 +01:00
{
ssize_t ret = 0;
2011-12-24 13:46:12 +01:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2011-01-07 15:50:16 +01:00
if (sdl->nonblock)
{
2015-01-12 05:05:56 +01:00
size_t avail, write_amt;
2011-01-07 15:50:16 +01:00
SDL_LockAudio();
avail = FIFO_WRITE_AVAIL(sdl->buffer);
2015-01-12 05:05:56 +01:00
write_amt = avail > size ? size : avail;
2011-06-14 19:55:08 +02:00
fifo_write(sdl->buffer, buf, write_amt);
2011-01-07 15:50:16 +01:00
SDL_UnlockAudio();
ret = write_amt;
}
else
{
size_t written = 0;
2015-01-12 05:05:56 +01:00
2011-01-07 15:50:16 +01:00
while (written < size)
{
2015-01-12 05:05:56 +01:00
size_t avail;
2011-01-07 15:50:16 +01:00
SDL_LockAudio();
avail = FIFO_WRITE_AVAIL(sdl->buffer);
2011-01-07 15:50:16 +01:00
if (avail == 0)
{
SDL_UnlockAudio();
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2011-12-25 16:36:56 +01:00
slock_lock(sdl->lock);
scond_wait(sdl->cond, sdl->lock);
slock_unlock(sdl->lock);
2016-05-11 19:02:43 +02:00
#endif
2011-01-07 15:50:16 +01:00
}
else
{
size_t write_amt = size - written > avail ? avail : size - written;
2011-06-14 19:55:08 +02:00
fifo_write(sdl->buffer, (const char*)buf + written, write_amt);
2011-01-07 15:50:16 +01:00
SDL_UnlockAudio();
written += write_amt;
}
}
ret = written;
}
return ret;
}
static bool sdl_audio_stop(void *data)
{
sdl_audio_t *sdl = (sdl_audio_t*)data;
sdl->is_paused = true;
2011-01-07 15:50:16 +01:00
SDL_PauseAudio(1);
return true;
}
static bool sdl_audio_alive(void *data)
{
sdl_audio_t *sdl = (sdl_audio_t*)data;
2015-01-12 05:05:56 +01:00
if (!sdl)
return false;
return !sdl->is_paused;
}
static bool sdl_audio_start(void *data, bool is_shutdown)
2011-01-07 15:50:16 +01:00
{
sdl_audio_t *sdl = (sdl_audio_t*)data;
sdl->is_paused = false;
2011-01-07 15:50:16 +01:00
SDL_PauseAudio(0);
return true;
}
static void sdl_audio_set_nonblock_state(void *data, bool state)
{
2011-12-24 13:46:12 +01:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2015-01-12 05:05:56 +01:00
if (sdl)
sdl->nonblock = state;
2011-01-07 15:50:16 +01:00
}
static void sdl_audio_free(void *data)
{
2015-01-12 05:05:56 +01:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2011-01-07 15:50:16 +01:00
SDL_CloseAudio();
if (sdl)
{
2011-06-14 19:55:08 +02:00
fifo_free(sdl->buffer);
2016-05-11 19:02:43 +02:00
#ifdef HAVE_THREADS
2011-12-25 16:36:56 +01:00
slock_free(sdl->lock);
scond_free(sdl->cond);
2016-05-11 19:02:43 +02:00
#endif
2011-01-07 15:50:16 +01:00
}
free(sdl);
}
static bool sdl_audio_use_float(void *data)
{
(void)data;
return false;
}
static size_t sdl_audio_write_avail(void *data)
{
/* stub */
(void)data;
return 0;
}
2014-09-11 07:06:20 +02:00
audio_driver_t audio_sdl = {
2011-12-24 13:46:12 +01:00
sdl_audio_init,
sdl_audio_write,
sdl_audio_stop,
sdl_audio_start,
sdl_audio_alive,
2011-12-24 13:46:12 +01:00
sdl_audio_set_nonblock_state,
sdl_audio_free,
sdl_audio_use_float,
2014-08-10 19:54:08 -03:00
#ifdef HAVE_SDL2
"sdl2",
2014-08-10 19:54:08 -03:00
#else
"sdl",
2014-08-10 19:54:08 -03:00
#endif
NULL,
NULL,
sdl_audio_write_avail,
NULL
2011-01-07 15:50:16 +01:00
};