2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
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 <stdlib.h>
|
2011-12-14 13:26:40 +00:00
|
|
|
#include <string.h>
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2014-06-04 00:01:15 +00:00
|
|
|
#ifdef GEKKO
|
2011-12-14 11:49:13 +00:00
|
|
|
#include <gccore.h>
|
|
|
|
#include <ogcsys.h>
|
2014-06-04 00:01:15 +00:00
|
|
|
#else
|
|
|
|
#include <cafe/ai.h>
|
|
|
|
#endif
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2015-09-14 01:44:06 +00:00
|
|
|
#include <boolean.h>
|
|
|
|
#include <retro_inline.h>
|
|
|
|
|
2015-11-23 18:40:09 +00:00
|
|
|
#include "../audio_driver.h"
|
2015-08-10 21:11:40 +00:00
|
|
|
#include "../../defines/gx_defines.h"
|
2014-06-04 16:54:40 +00:00
|
|
|
|
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;
|
|
|
|
|
2014-06-04 16:54:40 +00:00
|
|
|
OSCond cond;
|
2011-12-14 11:49:13 +00:00
|
|
|
bool nonblock;
|
2014-10-01 19:42:19 +00:00
|
|
|
bool is_paused;
|
2012-08-09 01:54:27 +00:00
|
|
|
} gx_audio_t;
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2013-11-07 22:47:28 +00:00
|
|
|
static volatile gx_audio_t *gx_audio_data;
|
2014-10-13 00:22:43 +00:00
|
|
|
static volatile bool stop_audio;
|
2013-11-07 22:47:28 +00:00
|
|
|
|
2011-12-14 11:49:13 +00:00
|
|
|
static void dma_callback(void)
|
|
|
|
{
|
2013-11-07 22:47:28 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)gx_audio_data;
|
2014-09-09 20:24:29 +00:00
|
|
|
|
2015-01-12 04:05:56 +00:00
|
|
|
if (stop_audio)
|
|
|
|
return;
|
2013-10-15 22:41:28 +00:00
|
|
|
|
2015-01-12 04:05:56 +00:00
|
|
|
/* Erase last chunk to avoid repeating audio. */
|
|
|
|
memset(wa->data[wa->dma_busy], 0, CHUNK_SIZE);
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2015-01-12 04:05:56 +00:00
|
|
|
wa->dma_busy = wa->dma_next;
|
|
|
|
wa->dma_next = (wa->dma_next + 1) & (BLOCKS - 1);
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2015-01-12 04:05:56 +00:00
|
|
|
DCFlushRange(wa->data[wa->dma_next], CHUNK_SIZE);
|
2014-10-13 00:22:43 +00:00
|
|
|
|
2015-01-12 04:05:56 +00:00
|
|
|
AIInitDMA((uint32_t)wa->data[wa->dma_next], CHUNK_SIZE);
|
|
|
|
|
|
|
|
OSSignalCond(wa->cond);
|
2011-12-14 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 20:24:29 +00:00
|
|
|
static void *gx_audio_init(const char *device,
|
2017-01-11 06:25:42 +00:00
|
|
|
unsigned rate, unsigned latency,
|
|
|
|
unsigned block_frames,
|
|
|
|
unsigned *new_rate)
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2015-03-20 20:31:16 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)memalign(32, sizeof(*wa));
|
2013-11-01 19:02:21 +00:00
|
|
|
if (!wa)
|
|
|
|
return NULL;
|
|
|
|
|
2014-05-29 22:24:09 +00:00
|
|
|
gx_audio_data = (gx_audio_t*)wa;
|
2013-11-07 22:47:28 +00:00
|
|
|
|
2013-11-01 19:02:21 +00:00
|
|
|
memset(wa, 0, sizeof(*wa));
|
2013-01-08 04:48:21 +00:00
|
|
|
|
2014-06-04 00:01:15 +00:00
|
|
|
AIInit(NULL);
|
|
|
|
AIRegisterDMACallback(dma_callback);
|
2012-06-25 19:27:41 +00:00
|
|
|
|
2012-06-25 20:46:09 +00:00
|
|
|
if (rate < 33000)
|
2012-06-25 19:27:41 +00:00
|
|
|
{
|
2014-06-04 00:01:15 +00:00
|
|
|
AISetDSPSampleRate(AI_SAMPLERATE_32KHZ);
|
2017-01-09 12:09:14 +00:00
|
|
|
*new_rate = 32000;
|
2012-06-25 19:27:41 +00:00
|
|
|
}
|
|
|
|
else
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2014-06-04 00:01:15 +00:00
|
|
|
AISetDSPSampleRate(AI_SAMPLERATE_48KHZ);
|
2017-01-09 12:09:14 +00:00
|
|
|
*new_rate = 48000;
|
2011-12-14 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2014-06-04 16:54:40 +00:00
|
|
|
OSInitThreadQueue(&wa->cond);
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2013-11-01 19:02:21 +00:00
|
|
|
wa->dma_write = BLOCKS - 1;
|
|
|
|
DCFlushRange(wa->data, sizeof(wa->data));
|
2014-10-13 00:22:43 +00:00
|
|
|
stop_audio = false;
|
2014-06-04 00:01:15 +00:00
|
|
|
AIInitDMA((uint32_t)wa->data[wa->dma_next], CHUNK_SIZE);
|
|
|
|
AIStartDMA();
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2013-11-01 19:02:21 +00:00
|
|
|
return wa;
|
2011-12-14 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2014-09-09 20:24:29 +00:00
|
|
|
/* Wii uses silly R, L, R, L interleaving. */
|
2015-03-15 03:41:11 +00:00
|
|
|
static INLINE void copy_swapped(uint32_t * restrict dst,
|
2014-09-09 20:24:29 +00:00
|
|
|
const uint32_t * restrict src, size_t size)
|
2012-08-24 21:13:18 +00:00
|
|
|
{
|
2013-12-12 21:10:11 +00:00
|
|
|
do
|
2012-08-24 21:13:18 +00:00
|
|
|
{
|
2013-12-12 21:10:11 +00:00
|
|
|
uint32_t s = *src++;
|
|
|
|
*dst++ = (s >> 16) | (s << 16);
|
|
|
|
}while(--size);
|
2012-08-24 21:13:18 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static ssize_t gx_audio_write(void *data, const void *buf_, size_t size)
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2016-09-08 09:41:58 +00:00
|
|
|
size_t frames = size >> 2;
|
2011-12-14 11:49:13 +00:00
|
|
|
const uint32_t *buf = buf_;
|
2016-09-08 09:41:58 +00:00
|
|
|
gx_audio_t *wa = data;
|
2014-06-04 16:54:40 +00:00
|
|
|
|
2011-12-14 11:49:13 +00:00
|
|
|
while (frames)
|
|
|
|
{
|
|
|
|
size_t to_write = CHUNK_FRAMES - wa->write_ptr;
|
2015-01-12 04:05:56 +00:00
|
|
|
|
2011-12-14 11:49:13 +00:00
|
|
|
if (frames < to_write)
|
|
|
|
to_write = frames;
|
|
|
|
|
2014-09-09 20:24:29 +00:00
|
|
|
/* FIXME: Nonblocking audio should break out of loop
|
|
|
|
* when it has nothing to write. */
|
|
|
|
while ((wa->dma_write == wa->dma_next ||
|
|
|
|
wa->dma_write == wa->dma_busy) && !wa->nonblock)
|
2014-06-04 16:54:40 +00:00
|
|
|
OSSleepThread(wa->cond);
|
2011-12-14 11:49:13 +00:00
|
|
|
|
2012-08-24 21:13:18 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static bool gx_audio_stop(void *data)
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2013-11-01 19:02:21 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)data;
|
2014-06-04 16:54:40 +00:00
|
|
|
|
|
|
|
if (!wa)
|
|
|
|
return false;
|
|
|
|
|
2014-06-04 00:01:15 +00:00
|
|
|
AIStopDMA();
|
2013-11-01 19:02:21 +00:00
|
|
|
memset(wa->data, 0, sizeof(wa->data));
|
|
|
|
DCFlushRange(wa->data, sizeof(wa->data));
|
2014-10-01 19:42:19 +00:00
|
|
|
wa->is_paused = true;
|
2011-12-14 11:49:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static void gx_audio_set_nonblock_state(void *data, bool state)
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2013-11-01 19:02:21 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)data;
|
2014-06-04 16:54:40 +00:00
|
|
|
|
2015-01-12 04:05:56 +00:00
|
|
|
if (wa)
|
|
|
|
wa->nonblock = state;
|
2011-12-14 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static bool gx_audio_start(void *data)
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2014-10-01 19:42:19 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)data;
|
2015-01-12 04:05:56 +00:00
|
|
|
|
|
|
|
if (!wa)
|
|
|
|
return false;
|
|
|
|
|
2014-06-04 00:01:15 +00:00
|
|
|
AIStartDMA();
|
2014-10-01 19:42:19 +00:00
|
|
|
wa->is_paused = false;
|
2011-12-14 11:49:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-01 19:42:19 +00:00
|
|
|
static bool gx_audio_alive(void *data)
|
|
|
|
{
|
|
|
|
gx_audio_t *wa = (gx_audio_t*)data;
|
2015-01-12 04:05:56 +00:00
|
|
|
if (!wa)
|
|
|
|
return false;
|
|
|
|
return !wa->is_paused;
|
2014-10-01 19:42:19 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static void gx_audio_free(void *data)
|
2011-12-14 11:49:13 +00:00
|
|
|
{
|
2013-11-01 19:02:21 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)data;
|
2014-06-04 16:54:40 +00:00
|
|
|
|
|
|
|
if (!wa)
|
|
|
|
return;
|
|
|
|
|
2014-10-13 00:22:43 +00:00
|
|
|
stop_audio = true;
|
2014-06-04 00:01:15 +00:00
|
|
|
AIStopDMA();
|
|
|
|
AIRegisterDMACallback(NULL);
|
2014-06-04 16:54:40 +00:00
|
|
|
|
|
|
|
if (wa->cond)
|
2016-02-12 21:58:59 +00:00
|
|
|
OSCloseThreadQueue(wa->cond);
|
2014-06-04 16:54:40 +00:00
|
|
|
wa->cond = 0;
|
|
|
|
|
2014-05-29 22:24:09 +00:00
|
|
|
free(data);
|
2011-12-14 11:49:13 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static size_t gx_audio_write_avail(void *data)
|
2012-06-26 18:11:52 +00:00
|
|
|
{
|
2013-11-01 19:02:21 +00:00
|
|
|
gx_audio_t *wa = (gx_audio_t*)data;
|
2014-09-09 20:24:29 +00:00
|
|
|
return ((wa->dma_busy - wa->dma_write + BLOCKS)
|
|
|
|
& (BLOCKS - 1)) * CHUNK_SIZE;
|
2012-06-26 18:11:52 +00:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:54:27 +00:00
|
|
|
static size_t gx_audio_buffer_size(void *data)
|
2012-06-26 18:11:52 +00:00
|
|
|
{
|
|
|
|
(void)data;
|
|
|
|
return BLOCKS * CHUNK_SIZE;
|
|
|
|
}
|
|
|
|
|
2014-05-22 21:51:30 +00:00
|
|
|
static bool gx_audio_use_float(void *data)
|
|
|
|
{
|
|
|
|
/* TODO/FIXME - verify */
|
|
|
|
(void)data;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-11 05:06:20 +00:00
|
|
|
audio_driver_t audio_gx = {
|
2014-05-13 15:09:17 +00:00
|
|
|
gx_audio_init,
|
|
|
|
gx_audio_write,
|
|
|
|
gx_audio_stop,
|
|
|
|
gx_audio_start,
|
2014-10-01 19:42:19 +00:00
|
|
|
gx_audio_alive,
|
2014-05-13 15:09:17 +00:00
|
|
|
gx_audio_set_nonblock_state,
|
|
|
|
gx_audio_free,
|
2014-05-22 21:51:30 +00:00
|
|
|
gx_audio_use_float,
|
2014-05-13 15:09:17 +00:00
|
|
|
"gx",
|
2016-04-26 15:55:20 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2014-05-13 15:09:17 +00:00
|
|
|
gx_audio_write_avail,
|
|
|
|
gx_audio_buffer_size,
|
2011-12-14 11:49:13 +00:00
|
|
|
};
|