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
|
2011-01-27 00:46:00 +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
|
2011-01-27 00:46:00 +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;
|
2011-01-27 00:46:00 +00: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 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-01-27 00:46:00 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-12-24 23:59:46 +00:00
|
|
|
#include "../driver.h"
|
2011-01-27 00:46:00 +00:00
|
|
|
#include <stdlib.h>
|
2011-08-20 01:22:19 +00:00
|
|
|
#include "xaudio-c/xaudio-c.h"
|
2011-12-24 23:59:46 +00:00
|
|
|
#include "../general.h"
|
2011-01-27 00:46:00 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
xaudio2_t *xa;
|
|
|
|
bool nonblock;
|
2012-02-14 08:15:09 +00:00
|
|
|
size_t bufsize;
|
2011-01-27 00:46:00 +00:00
|
|
|
} xa_t;
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static void *xa_init(const char *device, unsigned rate, unsigned latency)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
2011-06-09 21:17:10 +00:00
|
|
|
if (latency < 8)
|
|
|
|
latency = 8; // Do not allow shenanigans.
|
|
|
|
|
2011-12-25 18:31:05 +00:00
|
|
|
xa_t *xa = (xa_t*)calloc(1, sizeof(*xa));
|
|
|
|
if (!xa)
|
2011-01-27 00:46:00 +00:00
|
|
|
return NULL;
|
|
|
|
|
2011-01-28 19:57:13 +00:00
|
|
|
size_t bufsize = latency * rate / 1000;
|
2011-01-27 00:46:00 +00:00
|
|
|
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_LOG("XAudio2: Requesting %d ms latency, using %d ms latency.\n", latency, (int)bufsize * 1000 / rate);
|
2011-01-28 19:57:13 +00:00
|
|
|
|
2012-02-14 08:15:09 +00:00
|
|
|
xa->bufsize = bufsize * 2 * sizeof(float);
|
|
|
|
|
2012-03-30 17:53:12 +00:00
|
|
|
unsigned device_index = 0;
|
|
|
|
if (device)
|
|
|
|
device_index = strtoul(device, NULL, 0);
|
|
|
|
|
|
|
|
xa->xa = xaudio2_new(rate, 2, xa->bufsize, device_index);
|
2011-01-27 00:46:00 +00:00
|
|
|
if (!xa->xa)
|
|
|
|
{
|
2012-04-21 21:25:32 +00:00
|
|
|
RARCH_ERR("Failed to init XAudio2.\n");
|
2011-01-27 00:46:00 +00:00
|
|
|
free(xa);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-25 18:31:05 +00:00
|
|
|
|
2012-03-30 17:53:12 +00:00
|
|
|
if (g_extern.verbose)
|
|
|
|
xaudio2_enumerate_devices(xa->xa);
|
|
|
|
|
2011-01-27 00:46:00 +00:00
|
|
|
return xa;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static ssize_t xa_write(void *data, const void *buf, size_t size)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
xa_t *xa = (xa_t*)data;
|
2011-01-27 00:46:00 +00:00
|
|
|
if (xa->nonblock)
|
|
|
|
{
|
2011-08-20 01:22:19 +00:00
|
|
|
size_t avail = xaudio2_write_avail(xa->xa);
|
2011-08-21 17:27:11 +00:00
|
|
|
if (avail == 0)
|
|
|
|
return 0;
|
2011-01-27 00:46:00 +00:00
|
|
|
if (avail < size)
|
|
|
|
size = avail;
|
|
|
|
}
|
2011-12-25 18:31:05 +00:00
|
|
|
|
2011-08-21 17:27:11 +00:00
|
|
|
size_t ret = xaudio2_write(xa->xa, buf, size);
|
2012-01-14 01:03:49 +00:00
|
|
|
if (ret == 0 && size > 0)
|
2011-08-21 17:27:11 +00:00
|
|
|
return -1;
|
|
|
|
return ret;
|
2011-01-27 00:46:00 +00:00
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static bool xa_stop(void *data)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static void xa_set_nonblock_state(void *data, bool state)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
xa_t *xa = (xa_t*)data;
|
2011-01-27 00:46:00 +00:00
|
|
|
xa->nonblock = state;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static bool xa_start(void *data)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static bool xa_use_float(void *data)
|
2011-08-21 17:27:11 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-11-02 19:04:31 +00:00
|
|
|
static void xa_free(void *data)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
2011-12-24 12:46:12 +00:00
|
|
|
xa_t *xa = (xa_t*)data;
|
2011-08-20 01:22:19 +00:00
|
|
|
if (xa)
|
2011-01-27 00:46:00 +00:00
|
|
|
{
|
2011-08-20 01:22:19 +00:00
|
|
|
if (xa->xa)
|
|
|
|
xaudio2_free(xa->xa);
|
2011-01-27 00:46:00 +00:00
|
|
|
free(xa);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-14 08:15:09 +00:00
|
|
|
static size_t xa_write_avail(void *data)
|
|
|
|
{
|
|
|
|
xa_t *xa = (xa_t*)data;
|
|
|
|
return xaudio2_write_avail(xa->xa);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t xa_buffer_size(void *data)
|
|
|
|
{
|
|
|
|
xa_t *xa = (xa_t*)data;
|
|
|
|
return xa->bufsize;
|
|
|
|
}
|
|
|
|
|
2011-01-27 00:46:00 +00:00
|
|
|
const audio_driver_t audio_xa = {
|
2011-12-24 12:46:12 +00:00
|
|
|
xa_init,
|
|
|
|
xa_write,
|
|
|
|
xa_stop,
|
|
|
|
xa_start,
|
|
|
|
xa_set_nonblock_state,
|
|
|
|
xa_free,
|
|
|
|
xa_use_float,
|
2012-02-14 08:15:09 +00:00
|
|
|
"xaudio",
|
|
|
|
xa_write_avail,
|
|
|
|
xa_buffer_size,
|
2011-01-27 00:46:00 +00:00
|
|
|
};
|