n3dsaudio: Updated (but untested!) for SDL3 audio API.

This commit is contained in:
Ryan C. Gordon
2023-07-21 10:55:33 -04:00
parent ba27176106
commit b0d89868c6
2 changed files with 108 additions and 155 deletions

View File

@@ -22,7 +22,7 @@
#ifdef SDL_AUDIO_DRIVER_N3DS #ifdef SDL_AUDIO_DRIVER_N3DS
/* N3DS Audio driver */ // N3DS Audio driver
#include "../SDL_sysaudio.h" #include "../SDL_sysaudio.h"
#include "SDL_n3dsaudio.h" #include "SDL_n3dsaudio.h"
@@ -32,27 +32,24 @@
static dspHookCookie dsp_hook; static dspHookCookie dsp_hook;
static SDL_AudioDevice *audio_device; static SDL_AudioDevice *audio_device;
static void FreePrivateData(SDL_AudioDevice *_this); static SDL_INLINE void contextLock(SDL_AudioDevice *device)
static int FindAudioFormat(SDL_AudioDevice *_this);
static SDL_INLINE void contextLock(SDL_AudioDevice *_this)
{ {
LightLock_Lock(&_this->hidden->lock); LightLock_Lock(&device->hidden->lock);
} }
static SDL_INLINE void contextUnlock(SDL_AudioDevice *_this) static SDL_INLINE void contextUnlock(SDL_AudioDevice *device)
{ {
LightLock_Unlock(&_this->hidden->lock); LightLock_Unlock(&device->hidden->lock);
} }
static void N3DSAUD_LockAudio(SDL_AudioDevice *_this) static void N3DSAUD_LockAudio(SDL_AudioDevice *device)
{ {
contextLock(_this); contextLock(device);
} }
static void N3DSAUD_UnlockAudio(SDL_AudioDevice *_this) static void N3DSAUD_UnlockAudio(SDL_AudioDevice *device)
{ {
contextUnlock(_this); contextUnlock(device);
} }
static void N3DSAUD_DspHook(DSP_HookType hook) static void N3DSAUD_DspHook(DSP_HookType hook)
@@ -70,36 +67,36 @@ static void AudioFrameFinished(void *device)
{ {
bool shouldBroadcast = false; bool shouldBroadcast = false;
unsigned i; unsigned i;
SDL_AudioDevice *_this = (SDL_AudioDevice *)device; SDL_AudioDevice *device = (SDL_AudioDevice *)device;
contextLock(_this); contextLock(device);
for (i = 0; i < NUM_BUFFERS; i++) { for (i = 0; i < NUM_BUFFERS; i++) {
if (_this->hidden->waveBuf[i].status == NDSP_WBUF_DONE) { if (device->hidden->waveBuf[i].status == NDSP_WBUF_DONE) {
_this->hidden->waveBuf[i].status = NDSP_WBUF_FREE; device->hidden->waveBuf[i].status = NDSP_WBUF_FREE;
shouldBroadcast = SDL_TRUE; shouldBroadcast = SDL_TRUE;
} }
} }
if (shouldBroadcast) { if (shouldBroadcast) {
CondVar_Broadcast(&_this->hidden->cv); CondVar_Broadcast(&device->hidden->cv);
} }
contextUnlock(_this); contextUnlock(device);
} }
static int N3DSAUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname) static int N3DSAUDIO_OpenDevice(SDL_AudioDevice *device)
{ {
Result ndsp_init_res; Result ndsp_init_res;
Uint8 *data_vaddr; Uint8 *data_vaddr;
float mix[12]; float mix[12];
_this->hidden = (struct SDL_PrivateAudioData *)SDL_calloc(1, sizeof(*_this->hidden));
if (_this->hidden == NULL) { device->hidden = (struct SDL_PrivateAudioData *)SDL_calloc(1, sizeof(*device->hidden));
if (device->hidden == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
/* Initialise the DSP service */ // Initialise the DSP service
ndsp_init_res = ndspInit(); ndsp_init_res = ndspInit();
if (R_FAILED(ndsp_init_res)) { if (R_FAILED(ndsp_init_res)) {
if ((R_SUMMARY(ndsp_init_res) == RS_NOTFOUND) && (R_MODULE(ndsp_init_res) == RM_DSP)) { if ((R_SUMMARY(ndsp_init_res) == RS_NOTFOUND) && (R_MODULE(ndsp_init_res) == RM_DSP)) {
@@ -110,158 +107,168 @@ static int N3DSAUDIO_OpenDevice(SDL_AudioDevice *_this, const char *devname)
return -1; return -1;
} }
/* Initialise internal state */ // Initialise internal state
LightLock_Init(&_this->hidden->lock); LightLock_Init(&device->hidden->lock);
CondVar_Init(&_this->hidden->cv); CondVar_Init(&device->hidden->cv);
if (_this->spec.channels > 2) { if (device->spec.channels > 2) {
_this->spec.channels = 2; device->spec.channels = 2;
} }
/* Should not happen but better be safe. */ Uint32 format = 0;
if (FindAudioFormat(_this) < 0) { SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(device->spec.format);
while ((test_format = *(closefmts++)) != 0) {
if (test_format == SDL_AUDIO_S8) { // Signed 8-bit audio supported
format = (device->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM8 : NDSP_FORMAT_MONO_PCM8;
break;
} else if (test_format == SDL_AUDIO_S16) { // Signed 16-bit audio supported
format = (device->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16;
break;
}
}
if (!test_format) { // shouldn't happen, but just in case...
return SDL_SetError("No supported audio format found."); return SDL_SetError("No supported audio format found.");
} }
/* Update the fragment size as size in bytes */ device->spec.format = test_format;
SDL_CalculateAudioSpec(&_this->spec);
/* Allocate mixing buffer */ // Update the fragment size as size in bytes
if (_this->spec.size >= SDL_MAX_UINT32 / 2) { SDL_UpdatedAudioDeviceFormat(device);
// Allocate mixing buffer
if (device->buffer_size >= SDL_MAX_UINT32 / 2) {
return SDL_SetError("Mixing buffer is too large."); return SDL_SetError("Mixing buffer is too large.");
} }
_this->hidden->mixlen = _this->spec.size; device->hidden->mixbuf = (Uint8 *)SDL_malloc(device->buffer_size);
_this->hidden->mixbuf = (Uint8 *)SDL_malloc(_this->spec.size); if (device->hidden->mixbuf == NULL) {
if (_this->hidden->mixbuf == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
SDL_memset(_this->hidden->mixbuf, _this->spec.silence, _this->spec.size); SDL_memset(device->hidden->mixbuf, device->silence_value, device->buffer_size);
data_vaddr = (Uint8 *)linearAlloc(_this->hidden->mixlen * NUM_BUFFERS); data_vaddr = (Uint8 *)linearAlloc(device->buffer_size * NUM_BUFFERS);
if (data_vaddr == NULL) { if (data_vaddr == NULL) {
return SDL_OutOfMemory(); return SDL_OutOfMemory();
} }
SDL_memset(data_vaddr, 0, _this->hidden->mixlen * NUM_BUFFERS); SDL_memset(data_vaddr, 0, device->buffer_size * NUM_BUFFERS);
DSP_FlushDataCache(data_vaddr, _this->hidden->mixlen * NUM_BUFFERS); DSP_FlushDataCache(data_vaddr, device->buffer_size * NUM_BUFFERS);
_this->hidden->nextbuf = 0; device->hidden->nextbuf = 0;
_this->hidden->channels = _this->spec.channels;
_this->hidden->samplerate = _this->spec.freq;
ndspChnReset(0); ndspChnReset(0);
ndspChnSetInterp(0, NDSP_INTERP_LINEAR); ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
ndspChnSetRate(0, _this->spec.freq); ndspChnSetRate(0, device->spec.freq);
ndspChnSetFormat(0, _this->hidden->format); ndspChnSetFormat(0, device->hidden->format);
SDL_memset(mix, 0, sizeof(mix)); SDL_zeroa(mix);
mix[0] = 1.0; mix[0] = mix[1] = 1.0f;
mix[1] = 1.0;
ndspChnSetMix(0, mix); ndspChnSetMix(0, mix);
SDL_memset(_this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS); SDL_memset(device->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
const int sample_frame_size = device->spec.channels * (SDL_AUDIO_BITSIZE(device->spec.format) / 8);
for (unsigned i = 0; i < NUM_BUFFERS; i++) { for (unsigned i = 0; i < NUM_BUFFERS; i++) {
_this->hidden->waveBuf[i].data_vaddr = data_vaddr; device->hidden->waveBuf[i].data_vaddr = data_vaddr;
_this->hidden->waveBuf[i].nsamples = _this->hidden->mixlen / _this->hidden->bytePerSample; device->hidden->waveBuf[i].nsamples = device->buffer_size / sample_frame_size;
data_vaddr += _this->hidden->mixlen; data_vaddr += device->buffer_size;
} }
/* Setup callback */ // Setup callback
audio_device = _this; audio_device = device;
ndspSetCallback(AudioFrameFinished, _this); ndspSetCallback(AudioFrameFinished, device);
dspHook(&dsp_hook, N3DSAUD_DspHook); dspHook(&dsp_hook, N3DSAUD_DspHook);
return 0; return 0;
} }
static int N3DSAUDIO_CaptureFromDevice(SDL_AudioDevice *_this, void *buffer, int buflen) static void N3DSAUDIO_PlayDevice(SDL_AudioDevice *device, const Uint8 *buffer, int buflen)
{ {
/* Delay to make this sort of simulate real audio input. */ contextLock(device);
SDL_Delay((_this->spec.samples * 1000) / _this->spec.freq);
/* always return a full buffer of silence. */ const size_t nextbuf = device->hidden->nextbuf;
SDL_memset(buffer, _this->spec.silence, buflen);
return buflen;
}
static void N3DSAUDIO_PlayDevice(SDL_AudioDevice *_this) if (device->hidden->isCancelled ||
{ device->hidden->waveBuf[nextbuf].status != NDSP_WBUF_FREE) {
size_t nextbuf; contextUnlock(device);
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; return;
} }
_this->hidden->nextbuf = (nextbuf + 1) % NUM_BUFFERS; device->hidden->nextbuf = (nextbuf + 1) % NUM_BUFFERS;
contextUnlock(_this); contextUnlock(device);
SDL_memcpy((void *)_this->hidden->waveBuf[nextbuf].data_vaddr, SDL_memcpy((void *)device->hidden->waveBuf[nextbuf].data_vaddr, buffer, buflen);
_this->hidden->mixbuf, sampleLen); DSP_FlushDataCache(device->hidden->waveBuf[nextbuf].data_vaddr, sampleLen);
DSP_FlushDataCache(_this->hidden->waveBuf[nextbuf].data_vaddr, sampleLen);
ndspChnWaveBufAdd(0, &_this->hidden->waveBuf[nextbuf]); ndspChnWaveBufAdd(0, &device->hidden->waveBuf[nextbuf]);
} }
static void N3DSAUDIO_WaitDevice(SDL_AudioDevice *_this) static void N3DSAUDIO_WaitDevice(SDL_AudioDevice *device)
{ {
contextLock(_this); contextLock(device);
while (!_this->hidden->isCancelled && while (!device->hidden->isCancelled && !SDL_AtomicGet(&device->shutdown) &&
_this->hidden->waveBuf[_this->hidden->nextbuf].status != NDSP_WBUF_FREE) { device->hidden->waveBuf[device->hidden->nextbuf].status != NDSP_WBUF_FREE) {
CondVar_Wait(&_this->hidden->cv, &_this->hidden->lock); CondVar_Wait(&device->hidden->cv, &device->hidden->lock);
} }
contextUnlock(_this); contextUnlock(device);
} }
static Uint8 *N3DSAUDIO_GetDeviceBuf(SDL_AudioDevice *_this) static Uint8 *N3DSAUDIO_GetDeviceBuf(SDL_AudioDevice *device, int *buffer_size)
{ {
return _this->hidden->mixbuf; return device->hidden->mixbuf;
} }
static void N3DSAUDIO_CloseDevice(SDL_AudioDevice *_this) static void N3DSAUDIO_CloseDevice(SDL_AudioDevice *device)
{ {
contextLock(_this); if (!device->hidden) {
return;
}
contextLock(device);
dspUnhook(&dsp_hook); dspUnhook(&dsp_hook);
ndspSetCallback(NULL, NULL); ndspSetCallback(NULL, NULL);
if (!_this->hidden->isCancelled) { if (!device->hidden->isCancelled) {
ndspChnReset(0); ndspChnReset(0);
SDL_memset(_this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS); SDL_memset(device->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
CondVar_Broadcast(&_this->hidden->cv); CondVar_Broadcast(&device->hidden->cv);
} }
contextUnlock(_this); contextUnlock(device);
ndspExit(); ndspExit();
FreePrivateData(_this); if (device->hidden->waveBuf[0].data_vaddr) {
linearFree((void *)device->hidden->waveBuf[0].data_vaddr);
} }
static void N3DSAUDIO_ThreadInit(SDL_AudioDevice *_this) if (device->hidden->mixbuf) {
SDL_free(device->hidden->mixbuf);
device->hidden->mixbuf = NULL;
}
SDL_free(device->hidden);
device->hidden = NULL;
}
static void N3DSAUDIO_ThreadInit(SDL_AudioDevice *device)
{ {
s32 current_priority; s32 current_priority;
svcGetThreadPriority(&current_priority, CUR_THREAD_HANDLE); svcGetThreadPriority(&current_priority, CUR_THREAD_HANDLE);
current_priority--; current_priority--;
/* 0x18 is reserved for video, 0x30 is the default for main thread */ // 0x18 is reserved for video, 0x30 is the default for main thread
current_priority = SDL_clamp(current_priority, 0x19, 0x2F); current_priority = SDL_clamp(current_priority, 0x19, 0x2F);
svcSetThreadPriority(CUR_THREAD_HANDLE, current_priority); svcSetThreadPriority(CUR_THREAD_HANDLE, current_priority);
} }
static SDL_bool N3DSAUDIO_Init(SDL_AudioDriverImpl *impl) static SDL_bool N3DSAUDIO_Init(SDL_AudioDriverImpl *impl)
{ {
/* Set the function pointers */
impl->OpenDevice = N3DSAUDIO_OpenDevice; impl->OpenDevice = N3DSAUDIO_OpenDevice;
impl->PlayDevice = N3DSAUDIO_PlayDevice; impl->PlayDevice = N3DSAUDIO_PlayDevice;
impl->WaitDevice = N3DSAUDIO_WaitDevice; impl->WaitDevice = N3DSAUDIO_WaitDevice;
@@ -272,11 +279,10 @@ static SDL_bool N3DSAUDIO_Init(SDL_AudioDriverImpl *impl)
impl->UnlockDevice = N3DSAUD_UnlockAudio; impl->UnlockDevice = N3DSAUD_UnlockAudio;
impl->OnlyHasDefaultOutputDevice = SDL_TRUE; impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
/* Should be possible, but micInit would fail */ // Should be possible, but micInit would fail
impl->HasCaptureSupport = SDL_FALSE; impl->HasCaptureSupport = SDL_FALSE;
impl->CaptureFromDevice = N3DSAUDIO_CaptureFromDevice;
return SDL_TRUE; /* this audio target is available. */ return SDL_TRUE;
} }
AudioBootStrap N3DSAUDIO_bootstrap = { AudioBootStrap N3DSAUDIO_bootstrap = {
@@ -286,51 +292,4 @@ AudioBootStrap N3DSAUDIO_bootstrap = {
0 0
}; };
/** #endif // SDL_AUDIO_DRIVER_N3DS
* Cleans up all allocated memory, safe to call with null pointers
*/
static void FreePrivateData(SDL_AudioDevice *_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(SDL_AudioDevice *_this)
{
SDL_AudioFormat test_format;
const SDL_AudioFormat *closefmts = SDL_ClosestAudioFormats(_this->spec.format);
while ((test_format = *(closefmts++)) != 0) {
_this->spec.format = test_format;
switch (test_format) {
case SDL_AUDIO_S8:
/* Signed 8-bit audio supported */
_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;
return 0;
case SDL_AUDIO_S16:
/* Signed 16-bit audio supported */
_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;
return 0;
}
}
return -1;
}
#endif /* SDL_AUDIO_DRIVER_N3DS */

View File

@@ -30,12 +30,6 @@ struct SDL_PrivateAudioData
{ {
/* Speaker data */ /* Speaker data */
Uint8 *mixbuf; Uint8 *mixbuf;
Uint32 mixlen;
Uint32 format;
Uint32 samplerate;
Uint32 channels;
Uint8 bytePerSample;
Uint32 isSigned;
Uint32 nextbuf; Uint32 nextbuf;
ndspWaveBuf waveBuf[NUM_BUFFERS]; ndspWaveBuf waveBuf[NUM_BUFFERS];
LightLock lock; LightLock lock;