RetroArch/gx/gx_audio.c

194 lines
4.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
2011-12-14 11:49:13 +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-12-14 11:49:13 +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-12-14 11:49:13 +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-12-14 11:49:13 +00:00
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "../driver.h"
#include <stdlib.h>
#include <stdbool.h>
#include "../general.h"
2011-12-14 13:26:40 +00:00
#include <malloc.h>
#include <string.h>
2011-12-14 11:49:13 +00:00
#include <gccore.h>
#include <ogcsys.h>
2012-06-25 20:33:40 +00:00
#define CHUNK_FRAMES 64
2011-12-14 11:49:13 +00:00
#define CHUNK_SIZE (CHUNK_FRAMES * sizeof(uint32_t))
2012-06-25 20:33:40 +00:00
#define BLOCKS 16
2011-12-14 11:49:13 +00:00
typedef struct
{
uint32_t data[BLOCKS][CHUNK_FRAMES];
volatile unsigned dma_busy;
volatile unsigned dma_next;
volatile unsigned dma_write;
size_t write_ptr;
lwpq_t cond;
bool nonblock;
} gx_audio_t;
2011-12-14 11:49:13 +00:00
static gx_audio_t *g_audio;
2011-12-14 11:49:13 +00:00
static void dma_callback(void)
{
g_audio->dma_busy = g_audio->dma_next;
g_audio->dma_next = (g_audio->dma_next + 1) & (BLOCKS - 1);
DCFlushRange(g_audio->data[g_audio->dma_next], CHUNK_SIZE);
AUDIO_InitDMA((u32)g_audio->data[g_audio->dma_next], CHUNK_SIZE);
LWP_ThreadSignal(g_audio->cond);
}
static void *gx_audio_init(const char *device, unsigned rate, unsigned latency)
2011-12-14 11:49:13 +00:00
{
2012-06-25 19:27:41 +00:00
AUDIO_Init(NULL);
AUDIO_RegisterDMACallback(dma_callback);
2012-06-25 20:46:09 +00:00
if (rate < 33000)
2012-06-25 19:27:41 +00:00
{
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_32KHZ);
g_settings.audio.out_rate = 32000;
}
else
2011-12-14 11:49:13 +00:00
{
AUDIO_SetDSPSampleRate(AI_SAMPLERATE_48KHZ);
g_settings.audio.out_rate = 48000;
}
2012-06-25 19:27:41 +00:00
if (!g_audio)
{
g_audio = memalign(32, sizeof(*g_audio));
memset(g_audio, 0, sizeof(*g_audio));
LWP_InitQueue(&g_audio->cond);
}
else
{
memset(g_audio->data, 0, sizeof(g_audio->data));
g_audio->dma_busy = g_audio->dma_next = 0;
g_audio->write_ptr = 0;
g_audio->nonblock = false;
}
2011-12-14 11:49:13 +00:00
g_audio->dma_write = BLOCKS - 1;
DCFlushRange(g_audio->data, sizeof(g_audio->data));
AUDIO_InitDMA((u32)g_audio->data[g_audio->dma_next], CHUNK_SIZE);
AUDIO_StartDMA();
return g_audio;
}
// Wii uses silly R, L, R, L interleaving ...
static inline void copy_swapped(uint32_t * restrict dst, const uint32_t * restrict src, size_t size)
{
for (size_t i = 0; i < size; i++)
{
uint32_t s = src[x];
dst[i] = (s >> 16) | (s << 16);
}
}
static ssize_t gx_audio_write(void *data, const void *buf_, size_t size)
2011-12-14 11:49:13 +00:00
{
gx_audio_t *wa = data;
2011-12-14 11:49:13 +00:00
size_t frames = size >> 2;
const uint32_t *buf = buf_;
while (frames)
{
size_t to_write = CHUNK_FRAMES - wa->write_ptr;
if (frames < to_write)
to_write = frames;
// FIXME: Nonblocking audio should break out of loop when it has nothing to write.
2011-12-14 20:44:03 +00:00
while ((wa->dma_write == wa->dma_next || wa->dma_write == wa->dma_busy) && !wa->nonblock)
2011-12-14 11:49:13 +00:00
LWP_ThreadSleep(wa->cond);
copy_swapped(wa->data[wa->dma_write] + wa->write_ptr, buf, to_write);
2011-12-14 11:49:13 +00:00
wa->write_ptr += to_write;
frames -= to_write;
buf += to_write;
if (wa->write_ptr >= CHUNK_FRAMES)
{
wa->write_ptr -= CHUNK_FRAMES;
wa->dma_write = (wa->dma_write + 1) & (BLOCKS - 1);
}
}
return size;
}
static bool gx_audio_stop(void *data)
2011-12-14 11:49:13 +00:00
{
(void)data;
AUDIO_StopDMA();
return true;
}
static void gx_audio_set_nonblock_state(void *data, bool state)
2011-12-14 11:49:13 +00:00
{
gx_audio_t *wa = data;
2011-12-14 11:49:13 +00:00
wa->nonblock = state;
}
static bool gx_audio_start(void *data)
2011-12-14 11:49:13 +00:00
{
(void)data;
AUDIO_StartDMA();
return true;
}
static void gx_audio_free(void *data)
2011-12-14 11:49:13 +00:00
{
AUDIO_StopDMA();
2012-06-25 19:27:41 +00:00
AUDIO_RegisterDMACallback(NULL);
if (g_audio && g_audio->cond)
{
LWP_CloseQueue(g_audio->cond);
g_audio->cond = 0;
}
2011-12-14 11:49:13 +00:00
if (data)
free(data);
g_audio = NULL;
}
static size_t gx_audio_write_avail(void *data)
{
gx_audio_t *wa = data;
return ((wa->dma_busy - wa->dma_write + BLOCKS) & (BLOCKS - 1)) * CHUNK_SIZE;
}
static size_t gx_audio_buffer_size(void *data)
{
(void)data;
return BLOCKS * CHUNK_SIZE;
}
const audio_driver_t audio_gx = {
.init = gx_audio_init,
.write = gx_audio_write,
.stop = gx_audio_stop,
.start = gx_audio_start,
.set_nonblock_state = gx_audio_set_nonblock_state,
.free = gx_audio_free,
.ident = "gx",
.write_avail = gx_audio_write_avail,
.buffer_size = gx_audio_buffer_size,
2011-12-14 11:49:13 +00:00
};