#include "../../SDL_internal.h"
#ifdef SDL_AUDIO_DRIVER_N3DS
#include "SDL_audio.h"
#include "../SDL_sysaudio.h"
#include "SDL_n3dsaudio.h"
#include "SDL_timer.h"
#define N3DSAUDIO_DRIVER_NAME "n3ds"
static dspHookCookie dsp_hook;
static SDL_AudioDevice *audio_device;
static void FreePrivateData(_THIS);
static int FindAudioFormat(_THIS);
static SDL_INLINE void
contextLock(_THIS)
{
LightLock_Lock(&this->hidden->lock);
}
static SDL_INLINE void
contextUnlock(_THIS)
{
LightLock_Unlock(&this->hidden->lock);
}
static void
N3DSAUD_LockAudio(_THIS)
{
contextLock(this);
}
static void
N3DSAUD_UnlockAudio(_THIS)
{
contextUnlock(this);
}
static void
N3DSAUD_DspHook(DSP_HookType hook)
{
if (hook == DSPHOOK_ONCANCEL) {
contextLock(audio_device);
audio_device->hidden->isCancelled = SDL_TRUE;
SDL_AtomicSet(&audio_device->enabled, SDL_FALSE);
CondVar_Broadcast(&audio_device->hidden->cv);
contextUnlock(audio_device);
}
}
static void
AudioFrameFinished(void *device)
{
bool shouldBroadcast = false;
unsigned i;
SDL_AudioDevice *this = (SDL_AudioDevice *) device;
contextLock(this);
for (i = 0; i < NUM_BUFFERS; i++) {
if (this->hidden->waveBuf[i].status == NDSP_WBUF_DONE) {
this->hidden->waveBuf[i].status = NDSP_WBUF_FREE;
shouldBroadcast = SDL_TRUE;
}
}
if (shouldBroadcast) {
CondVar_Broadcast(&this->hidden->cv);
}
contextUnlock(this);
}
static int
N3DSAUDIO_OpenDevice(_THIS, const char *devname)
{
Result ndsp_init_res;
Uint8 *data_vaddr;
float mix[12];
this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof *this->hidden);
if (this->hidden == NULL) {
return SDL_OutOfMemory();
}
ndsp_init_res = ndspInit();
if (R_FAILED(ndsp_init_res)) {
if ((R_SUMMARY(ndsp_init_res) == RS_NOTFOUND) && (R_MODULE(ndsp_init_res) == RM_DSP)) {
SDL_SetError("DSP init failed: dspfirm.cdc missing!");
} else {
SDL_SetError("DSP init failed. Error code: 0x%lX", ndsp_init_res);
}
return -1;
}
LightLock_Init(&this->hidden->lock);
CondVar_Init(&this->hidden->cv);
if (this->spec.channels > 2) {
this->spec.channels = 2;
}
if (FindAudioFormat(this) < 0) {
return SDL_SetError("No supported audio format found.");
}
SDL_CalculateAudioSpec(&this->spec);
if (this->spec.size >= SDL_MAX_UINT32 / 2) {
return SDL_SetError("Mixing buffer is too large.");
}
this->hidden->mixlen = this->spec.size;
this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
if (this->hidden->mixbuf == NULL) {
return SDL_OutOfMemory();
}
SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
data_vaddr = (Uint8 *) linearAlloc(this->hidden->mixlen * NUM_BUFFERS);
if (data_vaddr == NULL) {
return SDL_OutOfMemory();
}
SDL_memset(data_vaddr, 0, this->hidden->mixlen * NUM_BUFFERS);
DSP_FlushDataCache(data_vaddr, this->hidden->mixlen * NUM_BUFFERS);
this->hidden->nextbuf = 0;
this->hidden->channels = this->spec.channels;
this->hidden->samplerate = this->spec.freq;
ndspChnReset(0);
ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
ndspChnSetRate(0, this->spec.freq);
ndspChnSetFormat(0, this->hidden->format);
SDL_memset(mix, 0, sizeof(mix));
mix[0] = 1.0;
mix[1] = 1.0;
ndspChnSetMix(0, mix);
SDL_memset(this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
for (unsigned i = 0; i < NUM_BUFFERS; i++) {
this->hidden->waveBuf[i].data_vaddr = data_vaddr;
this->hidden->waveBuf[i].nsamples = this->hidden->mixlen / this->hidden->bytePerSample;
data_vaddr += this->hidden->mixlen;
}
audio_device = this;
ndspSetCallback(AudioFrameFinished, this);
dspHook(&dsp_hook, N3DSAUD_DspHook);
return 0;
}
static int
N3DSAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
{
SDL_Delay((this->spec.samples * 1000) / this->spec.freq);
SDL_memset(buffer, this->spec.silence, buflen);
return buflen;
}
static void
N3DSAUDIO_PlayDevice(_THIS)
{
size_t nextbuf;
size_t sampleLen;
contextLock(this);
nextbuf = this->hidden->nextbuf;
sampleLen = this->hidden->mixlen;
if (this->hidden->isCancelled ||
this->hidden->waveBuf[nextbuf].status != NDSP_WBUF_FREE) {
contextUnlock(this);
return;
}
this->hidden->nextbuf = (nextbuf + 1) % NUM_BUFFERS;
contextUnlock(this);
memcpy((void *) this->hidden->waveBuf[nextbuf].data_vaddr,
this->hidden->mixbuf, sampleLen);
DSP_FlushDataCache(this->hidden->waveBuf[nextbuf].data_vaddr, sampleLen);
ndspChnWaveBufAdd(0, &this->hidden->waveBuf[nextbuf]);
}
static void
N3DSAUDIO_WaitDevice(_THIS)
{
contextLock(this);
while (!this->hidden->isCancelled &&
this->hidden->waveBuf[this->hidden->nextbuf].status != NDSP_WBUF_FREE) {
CondVar_Wait(&this->hidden->cv, &this->hidden->lock);
}
contextUnlock(this);
}
static Uint8 *
N3DSAUDIO_GetDeviceBuf(_THIS)
{
return this->hidden->mixbuf;
}
static void
N3DSAUDIO_CloseDevice(_THIS)
{
contextLock(this);
dspUnhook(&dsp_hook);
ndspSetCallback(NULL, NULL);
if (!this->hidden->isCancelled) {
ndspChnReset(0);
memset(this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
CondVar_Broadcast(&this->hidden->cv);
}
contextUnlock(this);
ndspExit();
FreePrivateData(this);
}
static void
N3DSAUDIO_ThreadInit(_THIS)
{
s32 current_priority;
svcGetThreadPriority(¤t_priority, CUR_THREAD_HANDLE);
current_priority--;
current_priority = SDL_clamp(current_priority, 0x19, 0x2F);
svcSetThreadPriority(CUR_THREAD_HANDLE, current_priority);
}
static SDL_bool
N3DSAUDIO_Init(SDL_AudioDriverImpl *impl)
{
impl->OpenDevice = N3DSAUDIO_OpenDevice;
impl->PlayDevice = N3DSAUDIO_PlayDevice;
impl->WaitDevice = N3DSAUDIO_WaitDevice;
impl->GetDeviceBuf = N3DSAUDIO_GetDeviceBuf;
impl->CloseDevice = N3DSAUDIO_CloseDevice;
impl->ThreadInit = N3DSAUDIO_ThreadInit;
impl->LockDevice = N3DSAUD_LockAudio;
impl->UnlockDevice = N3DSAUD_UnlockAudio;
impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
impl->HasCaptureSupport = SDL_FALSE;
impl->CaptureFromDevice = N3DSAUDIO_CaptureFromDevice;
return SDL_TRUE;
}
AudioBootStrap N3DSAUDIO_bootstrap = {
N3DSAUDIO_DRIVER_NAME,
"SDL N3DS audio driver",
N3DSAUDIO_Init,
0
};
static void
FreePrivateData(_THIS)
{
if (!this->hidden) {
return;
}
if (this->hidden->waveBuf[0].data_vaddr) {
linearFree((void *) this->hidden->waveBuf[0].data_vaddr);
}
if (this->hidden->mixbuf) {
SDL_free(this->hidden->mixbuf);
this->hidden->mixbuf = NULL;
}
SDL_free(this->hidden);
this->hidden = NULL;
}
static int
FindAudioFormat(_THIS)
{
SDL_bool found_valid_format = SDL_FALSE;
Uint16 test_format = SDL_FirstAudioFormat(this->spec.format);
while (!found_valid_format && test_format) {
this->spec.format = test_format;
switch (test_format) {
case AUDIO_S8:
this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM8 : NDSP_FORMAT_MONO_PCM8;
this->hidden->isSigned = 1;
this->hidden->bytePerSample = this->spec.channels;
found_valid_format = SDL_TRUE;
break;
case AUDIO_S16:
this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16;
this->hidden->isSigned = 1;
this->hidden->bytePerSample = this->spec.channels * 2;
found_valid_format = SDL_TRUE;
break;
default:
test_format = SDL_NextAudioFormat();
break;
}
}
return found_valid_format ? 0 : -1;
}
#endif