RetroArch/360/xenon360_audio.c

117 lines
2.7 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
*
2012-04-21 21:13:50 +00:00
* RetroArch 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.
*
2012-04-21 21:13:50 +00:00
* RetroArch 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.
*
2012-04-21 21:31:57 +00:00
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "../driver.h"
#include <stdlib.h>
2011-12-13 22:38:05 +00:00
#include <stdbool.h>
#include "../general.h"
#include <xenon_sound/sound.h>
#define SOUND_FREQUENCY 48000
2011-12-13 23:25:04 +00:00
#define MAX_BUFFER 2048
typedef struct
{
2011-12-13 22:44:55 +00:00
uint32_t buffer[2048];
bool nonblock;
2011-12-13 22:38:05 +00:00
} xenon_audio_t;
static void *xenon360_init(const char *device, unsigned rate, unsigned latency)
{
2011-12-13 22:38:05 +00:00
static bool inited = false;
if (!inited)
{
xenon_sound_init();
inited = true;
}
g_settings.audio.out_rate = SOUND_FREQUENCY;
return calloc(1, sizeof(xenon_audio_t));
}
2011-12-13 22:44:55 +00:00
static inline uint32_t bswap_32(uint32_t val)
{
2011-12-13 23:36:28 +00:00
return (val >> 24) | (val << 24) | ((val >> 8) & 0xff00) | ((val << 8) & 0xff0000);
2011-12-13 22:44:55 +00:00
}
static ssize_t xenon360_write(void *data, const void *buf, size_t size)
{
2011-12-13 22:38:05 +00:00
xenon_audio_t *xa = data;
size_t written = 0;
2011-12-13 22:44:55 +00:00
const uint32_t *in_buf = buf;
2011-12-13 23:25:04 +00:00
for (size_t i = 0; i < (size >> 2); i++)
2011-12-13 22:44:55 +00:00
xa->buffer[i] = bswap_32(in_buf[i]);
2011-12-13 22:38:05 +00:00
if (!xa->nonblock)
{
2011-12-13 23:25:04 +00:00
while (xenon_sound_get_unplayed() >= MAX_BUFFER)
2011-12-13 22:38:05 +00:00
{
// libxenon doesn't have proper synchronization primitives for this :(
udelay(50);
}
2011-12-13 22:44:55 +00:00
xenon_sound_submit(xa->buffer, size);
2011-12-13 22:38:05 +00:00
written = size;
}
else
{
2011-12-13 23:25:04 +00:00
if (xenon_sound_get_unplayed() < MAX_BUFFER)
{
2011-12-13 22:44:55 +00:00
xenon_sound_submit(xa->buffer, size);
2011-12-13 23:25:04 +00:00
written = size;
}
}
2011-12-13 22:38:05 +00:00
return written;
}
static bool xenon360_stop(void *data)
{
(void)data;
return true;
}
static void xenon360_set_nonblock_state(void *data, bool state)
{
2011-12-13 22:38:05 +00:00
xenon_audio_t *xa = data;
xa->nonblock = state;
}
static bool xenon360_start(void *data)
{
(void)data;
return true;
}
static void xenon360_free(void *data)
{
2011-12-13 22:38:05 +00:00
if (data)
free(data);
}
const audio_driver_t audio_xenon360 = {
.init = xenon360_init,
.write = xenon360_write,
.stop = xenon360_stop,
.start = xenon360_start,
.set_nonblock_state = xenon360_set_nonblock_state,
.free = xenon360_free,
.ident = "xenon360"
};
2011-12-13 22:38:05 +00:00