RetroArch/audio/sdl.c

200 lines
4.9 KiB
C
Raw Normal View History

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
2011-01-07 14:50:16 +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/>.
*/
#include "driver.h"
#include <stdlib.h>
2011-12-24 12:46:12 +00:00
#include "../boolean.h"
2011-01-07 14:50:16 +00:00
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "SDL.h"
#include "SDL_audio.h"
#include "SDL_thread.h"
2011-01-07 14:50:16 +00:00
#include "general.h"
#include "fifo_buffer.h"
2011-01-07 14:50:16 +00:00
typedef struct sdl_audio
{
bool nonblock;
SDL_mutex *lock;
SDL_cond *cond;
2011-06-14 17:55:08 +00:00
fifo_buffer_t *buffer;
2011-01-07 14:50:16 +00:00
} sdl_audio_t;
static void sdl_audio_cb(void *data, Uint8 *stream, int len)
{
2011-12-24 12:46:12 +00:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2011-01-07 14:50:16 +00:00
2011-06-14 17:55:08 +00:00
size_t avail = fifo_read_avail(sdl->buffer);
2011-12-24 12:46:12 +00:00
size_t write_size = len > (int)avail ? avail : len;
2011-06-14 17:55:08 +00:00
fifo_read(sdl->buffer, stream, write_size);
2011-01-07 14:50:16 +00:00
SDL_CondSignal(sdl->cond);
// If underrun, fill rest with silence.
memset(stream + write_size, 0, len - write_size);
}
static inline int find_num_frames(int rate, int latency)
{
int frames = (rate * latency) / 1000;
// SDL only likes 2^n sized buffers.
return next_pow2(frames);
}
2011-11-02 18:31:36 +00:00
static void *sdl_audio_init(const char *device, unsigned rate, unsigned latency)
2011-01-07 14:50:16 +00:00
{
(void)device;
2011-01-07 14:50:16 +00:00
if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0)
return NULL;
2011-12-24 12:46:12 +00:00
sdl_audio_t *sdl = (sdl_audio_t*)calloc(1, sizeof(*sdl));
2011-01-07 14:50:16 +00:00
if (!sdl)
return NULL;
2011-01-07 19:37:30 +00:00
// We have to buffer up some data ourselves, so we let SDL carry approx half of the latency. SDL double buffers audio and we do as well.
int frames = find_num_frames(rate, latency / 4);
2011-12-24 12:46:12 +00:00
SDL_AudioSpec spec = {0};
spec.freq = rate;
spec.format = AUDIO_S16SYS;
spec.channels = 2;
spec.samples = frames; // This is in audio frames, not samples ... :(
spec.callback = sdl_audio_cb;
spec.userdata = sdl;
2011-01-07 14:50:16 +00:00
SDL_AudioSpec out;
if (SDL_OpenAudio(&spec, &out) < 0)
{
SSNES_ERR("Failed to open SDL audio: %s\n", SDL_GetError());
free(sdl);
return 0;
}
g_settings.audio.out_rate = out.freq;
sdl->lock = SDL_CreateMutex();
sdl->cond = SDL_CreateCond();
2011-01-07 19:37:30 +00:00
SSNES_LOG("SDL audio: Requested %d ms latency, got %d ms\n", latency, (int)(out.samples * 4 * 1000 / g_settings.audio.out_rate));
2011-01-07 14:50:16 +00:00
// Create a buffer twice as big as needed and prefill the buffer.
size_t bufsize = out.samples * 4 * sizeof(int16_t);
void *tmp = calloc(1, bufsize);
2011-06-14 17:55:08 +00:00
sdl->buffer = fifo_new(bufsize);
2011-01-07 14:50:16 +00:00
if (tmp)
{
2011-06-14 17:55:08 +00:00
fifo_write(sdl->buffer, tmp, bufsize);
2011-01-07 14:50:16 +00:00
free(tmp);
}
SDL_PauseAudio(0);
return sdl;
}
2011-11-02 18:31:36 +00:00
static ssize_t sdl_audio_write(void *data, const void *buf, size_t size)
2011-01-07 14:50:16 +00:00
{
2011-12-24 12:46:12 +00:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2011-01-07 14:50:16 +00:00
ssize_t ret = 0;
if (sdl->nonblock)
{
SDL_LockAudio();
2011-06-14 17:55:08 +00:00
size_t avail = fifo_write_avail(sdl->buffer);
2011-01-07 14:50:16 +00:00
size_t write_amt = avail > size ? size : avail;
2011-06-14 17:55:08 +00:00
fifo_write(sdl->buffer, buf, write_amt);
2011-01-07 14:50:16 +00:00
SDL_UnlockAudio();
ret = write_amt;
}
else
{
size_t written = 0;
while (written < size)
{
SDL_LockAudio();
2011-06-14 17:55:08 +00:00
size_t avail = fifo_write_avail(sdl->buffer);
2011-01-07 14:50:16 +00:00
if (avail == 0)
{
SDL_UnlockAudio();
SDL_mutexP(sdl->lock);
SDL_CondWait(sdl->cond, sdl->lock);
SDL_mutexV(sdl->lock);
}
else
{
size_t write_amt = size - written > avail ? avail : size - written;
2011-06-14 17:55:08 +00:00
fifo_write(sdl->buffer, (const char*)buf + written, write_amt);
2011-01-07 14:50:16 +00:00
SDL_UnlockAudio();
written += write_amt;
}
}
ret = written;
}
return ret;
}
static bool sdl_audio_stop(void *data)
{
(void)data;
SDL_PauseAudio(1);
return true;
}
static bool sdl_audio_start(void *data)
{
(void)data;
SDL_PauseAudio(0);
return true;
}
static void sdl_audio_set_nonblock_state(void *data, bool state)
{
2011-12-24 12:46:12 +00:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2011-01-07 14:50:16 +00:00
sdl->nonblock = state;
}
static void sdl_audio_free(void *data)
{
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
2011-12-24 12:46:12 +00:00
sdl_audio_t *sdl = (sdl_audio_t*)data;
2011-01-07 14:50:16 +00:00
if (sdl)
{
2011-06-14 17:55:08 +00:00
fifo_free(sdl->buffer);
2011-01-07 14:50:16 +00:00
SDL_DestroyMutex(sdl->lock);
SDL_DestroyCond(sdl->cond);
}
free(sdl);
}
const audio_driver_t audio_sdl = {
2011-12-24 12:46:12 +00:00
sdl_audio_init,
sdl_audio_write,
sdl_audio_stop,
sdl_audio_start,
sdl_audio_set_nonblock_state,
sdl_audio_free,
NULL,
"sdl"
2011-01-07 14:50:16 +00:00
};