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 00:45:18 +00:00
|
|
|
#include "driver.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <rsound.h>
|
2011-06-14 17:58:12 +00:00
|
|
|
#include "fifo_buffer.h"
|
2011-02-06 22:55:17 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include "SDL.h"
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
typedef struct rsd
|
|
|
|
{
|
|
|
|
rsound_t *rd;
|
2011-02-06 22:55:17 +00:00
|
|
|
bool nonblock;
|
|
|
|
volatile bool has_error;
|
|
|
|
|
2011-06-14 17:55:08 +00:00
|
|
|
fifo_buffer_t *buffer;
|
2011-02-06 22:55:17 +00:00
|
|
|
|
|
|
|
SDL_mutex *cond_lock;
|
|
|
|
SDL_cond *cond;
|
2010-05-28 00:45:18 +00:00
|
|
|
} rsd_t;
|
|
|
|
|
2011-02-06 22:55:17 +00:00
|
|
|
static ssize_t audio_cb(void *data, size_t bytes, void *userdata)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = userdata;
|
|
|
|
|
2011-06-14 17:55:08 +00:00
|
|
|
size_t avail = fifo_read_avail(rsd->buffer);
|
2011-02-06 22:55:17 +00:00
|
|
|
size_t write_size = bytes > avail ? avail : bytes;
|
2011-06-14 17:55:08 +00:00
|
|
|
fifo_read(rsd->buffer, data, write_size);
|
2011-02-06 22:55:17 +00:00
|
|
|
SDL_CondSignal(rsd->cond);
|
|
|
|
|
|
|
|
return write_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void err_cb(void *userdata)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = userdata;
|
|
|
|
rsd->has_error = true;
|
|
|
|
SDL_CondSignal(rsd->cond);
|
|
|
|
}
|
|
|
|
|
2011-05-15 13:57:47 +00:00
|
|
|
static void* __rsd_init(const char* device, unsigned rate, unsigned latency)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
|
|
|
rsd_t *rsd = calloc(1, sizeof(rsd_t));
|
|
|
|
if ( rsd == NULL )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rsound_t *rd;
|
|
|
|
|
|
|
|
if ( rsd_init(&rd) < 0 )
|
|
|
|
{
|
|
|
|
free(rsd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-02-06 22:55:17 +00:00
|
|
|
rsd->cond_lock = SDL_CreateMutex();
|
|
|
|
rsd->cond = SDL_CreateCond();
|
|
|
|
|
2011-06-14 17:55:08 +00:00
|
|
|
rsd->buffer = fifo_new(1024 * 4);
|
2011-02-06 22:55:17 +00:00
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
int channels = 2;
|
|
|
|
int format = RSD_S16_NE;
|
|
|
|
|
|
|
|
rsd_set_param(rd, RSD_CHANNELS, &channels);
|
|
|
|
rsd_set_param(rd, RSD_SAMPLERATE, &rate);
|
2011-02-06 22:55:17 +00:00
|
|
|
rsd_set_param(rd, RSD_LATENCY, &latency);
|
2010-05-28 00:45:18 +00:00
|
|
|
|
|
|
|
if ( device != NULL )
|
|
|
|
rsd_set_param(rd, RSD_HOST, (void*)device);
|
|
|
|
|
|
|
|
rsd_set_param(rd, RSD_FORMAT, &format);
|
|
|
|
|
2011-02-06 22:55:17 +00:00
|
|
|
rsd_set_callback(rd, audio_cb, err_cb, 256, rsd);
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
if ( rsd_start(rd) < 0 )
|
|
|
|
{
|
|
|
|
free(rsd);
|
|
|
|
rsd_free(rd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rsd->rd = rd;
|
|
|
|
return rsd;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t __rsd_write(void* data, const void* buf, size_t size)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = data;
|
|
|
|
|
2011-02-06 22:55:17 +00:00
|
|
|
if (rsd->has_error)
|
2010-05-28 00:45:18 +00:00
|
|
|
return -1;
|
|
|
|
|
2011-02-06 22:55:17 +00:00
|
|
|
if (rsd->nonblock)
|
2010-05-28 00:45:18 +00:00
|
|
|
{
|
2011-02-08 16:50:38 +00:00
|
|
|
rsd_callback_lock(rsd->rd);
|
2011-06-14 17:55:08 +00:00
|
|
|
size_t avail = fifo_write_avail(rsd->buffer);
|
2011-02-06 22:55:17 +00:00
|
|
|
size_t write_amt = avail > size ? size : avail;
|
2011-06-14 17:55:08 +00:00
|
|
|
fifo_write(rsd->buffer, buf, write_amt);
|
2011-02-08 16:50:38 +00:00
|
|
|
rsd_callback_unlock(rsd->rd);
|
2011-02-06 22:55:17 +00:00
|
|
|
return write_amt;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t written = 0;
|
|
|
|
while (written < size && !rsd->has_error)
|
|
|
|
{
|
2011-02-08 16:50:38 +00:00
|
|
|
rsd_callback_lock(rsd->rd);
|
2011-06-14 17:55:08 +00:00
|
|
|
size_t avail = fifo_write_avail(rsd->buffer);
|
2011-02-06 22:55:17 +00:00
|
|
|
|
|
|
|
if (avail == 0)
|
|
|
|
{
|
2011-02-08 16:50:38 +00:00
|
|
|
rsd_callback_unlock(rsd->rd);
|
2011-02-06 22:55:17 +00:00
|
|
|
if (!rsd->has_error)
|
|
|
|
{
|
|
|
|
SDL_mutexP(rsd->cond_lock);
|
|
|
|
SDL_CondWait(rsd->cond, rsd->cond_lock);
|
|
|
|
SDL_mutexV(rsd->cond_lock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t write_amt = size - written > avail ? avail : size - written;
|
2011-06-14 17:55:08 +00:00
|
|
|
fifo_write(rsd->buffer, (const char*)buf + written, write_amt);
|
2011-02-08 16:50:38 +00:00
|
|
|
rsd_callback_unlock(rsd->rd);
|
2011-02-06 22:55:17 +00:00
|
|
|
written += write_amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written;
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool __rsd_stop(void *data)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = data;
|
|
|
|
rsd_stop(rsd->rd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-16 16:40:17 +00:00
|
|
|
static void __rsd_set_nonblock_state(void *data, bool state)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = data;
|
|
|
|
rsd->nonblock = state;
|
|
|
|
}
|
|
|
|
|
2010-05-28 00:45:18 +00:00
|
|
|
static bool __rsd_start(void *data)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = data;
|
|
|
|
if ( rsd_start(rsd->rd) < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __rsd_free(void *data)
|
|
|
|
{
|
|
|
|
rsd_t *rsd = data;
|
|
|
|
|
|
|
|
rsd_stop(rsd->rd);
|
|
|
|
rsd_free(rsd->rd);
|
2011-02-06 22:55:17 +00:00
|
|
|
|
2011-06-14 17:55:08 +00:00
|
|
|
fifo_free(rsd->buffer);
|
2011-02-06 22:55:17 +00:00
|
|
|
SDL_DestroyMutex(rsd->cond_lock);
|
|
|
|
SDL_DestroyCond(rsd->cond);
|
|
|
|
|
2010-05-28 13:41:38 +00:00
|
|
|
free(rsd);
|
2010-05-28 00:45:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const audio_driver_t audio_rsound = {
|
|
|
|
.init = __rsd_init,
|
|
|
|
.write = __rsd_write,
|
|
|
|
.stop = __rsd_stop,
|
|
|
|
.start = __rsd_start,
|
2010-08-16 16:40:17 +00:00
|
|
|
.set_nonblock_state = __rsd_set_nonblock_state,
|
2010-12-29 19:05:57 +00:00
|
|
|
.free = __rsd_free,
|
|
|
|
.ident = "rsound"
|
2010-05-28 00:45:18 +00:00
|
|
|
};
|
|
|
|
|