RetroArch/audio/rsound.c

217 lines
4.6 KiB
C
Raw Normal View History

2012-04-21 21:13:50 +00:00
/* RetroArch - A frontend for libretro.
2012-01-08 00:08:18 +00:00
* Copyright (C) 2010-2012 - Hans-Kristian Arntzen
2010-05-28 16:21:33 +00:00
*
2012-04-21 21:13:50 +00:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2010-05-28 16:21:33 +00: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 21:13:50 +00:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2010-05-28 16:21:33 +00:00
* 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/>.
*/
2012-03-01 20:32:01 +00:00
#include "../driver.h"
2010-05-28 00:45:18 +00:00
#include <stdlib.h>
2012-04-21 21:25:32 +00:00
#ifdef RARCH_CONSOLE
2012-03-01 20:32:01 +00:00
#include "../console/librsound/rsound.h"
#else
2010-05-28 00:45:18 +00:00
#include <rsound.h>
2012-03-01 20:32:01 +00:00
#endif
#include "fifo_buffer.h"
2011-12-24 12:46:12 +00:00
#include "../boolean.h"
2012-01-27 23:32:59 +00:00
#include "../thread.h"
2010-05-28 00:45:18 +00:00
typedef struct rsd
{
rsound_t *rd;
bool nonblock;
volatile bool has_error;
2011-06-14 17:55:08 +00:00
fifo_buffer_t *buffer;
2012-01-27 23:32:59 +00:00
slock_t *cond_lock;
scond_t *cond;
2010-05-28 00:45:18 +00:00
} rsd_t;
static ssize_t audio_cb(void *data, size_t bytes, void *userdata)
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)userdata;
2011-06-14 17:55:08 +00:00
size_t avail = fifo_read_avail(rsd->buffer);
size_t write_size = bytes > avail ? avail : bytes;
2011-06-14 17:55:08 +00:00
fifo_read(rsd->buffer, data, write_size);
2012-01-27 23:32:59 +00:00
scond_signal(rsd->cond);
return write_size;
}
static void err_cb(void *userdata)
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)userdata;
rsd->has_error = true;
2012-01-27 23:32:59 +00:00
scond_signal(rsd->cond);
}
static void *rs_init(const char *device, unsigned rate, unsigned latency)
2010-05-28 00:45:18 +00:00
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)calloc(1, sizeof(rsd_t));
2011-10-15 10:54:47 +00:00
if (!rsd)
2010-05-28 00:45:18 +00:00
return NULL;
rsound_t *rd;
2011-10-15 10:54:47 +00:00
if (rsd_init(&rd) < 0)
2010-05-28 00:45:18 +00:00
{
free(rsd);
return NULL;
}
2012-01-27 23:32:59 +00:00
rsd->cond_lock = slock_new();
rsd->cond = scond_new();
2011-06-14 17:55:08 +00:00
rsd->buffer = fifo_new(1024 * 4);
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);
rsd_set_param(rd, RSD_LATENCY, &latency);
2010-05-28 00:45:18 +00:00
2011-10-15 10:54:47 +00:00
if (device)
2010-05-28 00:45:18 +00:00
rsd_set_param(rd, RSD_HOST, (void*)device);
rsd_set_param(rd, RSD_FORMAT, &format);
rsd_set_callback(rd, audio_cb, err_cb, 256, rsd);
2011-10-15 10:54:47 +00:00
if (rsd_start(rd) < 0)
2010-05-28 00:45:18 +00:00
{
free(rsd);
rsd_free(rd);
return NULL;
}
rsd->rd = rd;
return rsd;
}
static ssize_t rs_write(void *data, const void *buf, size_t size)
2010-05-28 00:45:18 +00:00
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)data;
2010-05-28 00:45:18 +00:00
if (rsd->has_error)
2010-05-28 00:45:18 +00:00
return -1;
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);
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);
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);
if (avail == 0)
{
2011-02-08 16:50:38 +00:00
rsd_callback_unlock(rsd->rd);
if (!rsd->has_error)
{
2012-01-27 23:32:59 +00:00
slock_lock(rsd->cond_lock);
scond_wait(rsd->cond, rsd->cond_lock);
slock_unlock(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);
written += write_amt;
}
}
return written;
2010-05-28 00:45:18 +00:00
}
}
static bool rs_stop(void *data)
2010-05-28 00:45:18 +00:00
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)data;
2010-05-28 00:45:18 +00:00
rsd_stop(rsd->rd);
return true;
}
static void rs_set_nonblock_state(void *data, bool state)
2010-08-16 16:40:17 +00:00
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)data;
2010-08-16 16:40:17 +00:00
rsd->nonblock = state;
}
static bool rs_start(void *data)
2010-05-28 00:45:18 +00:00
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)data;
2011-10-15 10:54:47 +00:00
if (rsd_start(rsd->rd) < 0)
2010-05-28 00:45:18 +00:00
return false;
return true;
}
static void rs_free(void *data)
2010-05-28 00:45:18 +00:00
{
2011-12-24 12:46:12 +00:00
rsd_t *rsd = (rsd_t*)data;
2010-05-28 00:45:18 +00:00
rsd_stop(rsd->rd);
rsd_free(rsd->rd);
2011-06-14 17:55:08 +00:00
fifo_free(rsd->buffer);
2012-01-27 23:32:59 +00:00
slock_free(rsd->cond_lock);
scond_free(rsd->cond);
2010-05-28 13:41:38 +00:00
free(rsd);
2010-05-28 00:45:18 +00:00
}
2012-02-14 00:16:37 +00:00
static size_t rs_write_avail(void *data)
{
rsd_t *rsd = (rsd_t*)data;
2012-02-14 00:16:37 +00:00
if (rsd->has_error)
return 0;
rsd_callback_lock(rsd->rd);
size_t val = fifo_write_avail(rsd->buffer);
rsd_callback_unlock(rsd->rd);
return val;
}
static size_t rs_buffer_size(void *data)
{
(void)data;
return 1024 * 4;
}
2010-05-28 00:45:18 +00:00
const audio_driver_t audio_rsound = {
2011-12-24 12:46:12 +00:00
rs_init,
rs_write,
rs_stop,
rs_start,
rs_set_nonblock_state,
rs_free,
NULL,
2012-02-14 00:16:37 +00:00
"rsound",
rs_write_avail,
rs_buffer_size,
2010-05-28 00:45:18 +00:00
};