Hash :
0d763800
Author :
Date :
2023-03-09T15:10:00
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/*
Simple DirectMedia Layer
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_AUDIO_DRIVER_N3DS
#include "SDL_audio.h"
/* N3DS Audio driver */
#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();
}
/* Initialise the DSP service */
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;
}
/* Initialise internal state */
LightLock_Init(&this->hidden->lock);
CondVar_Init(&this->hidden->cv);
if (this->spec.channels > 2) {
this->spec.channels = 2;
}
/* Should not happen but better be safe. */
if (FindAudioFormat(this) < 0) {
return SDL_SetError("No supported audio format found.");
}
/* Update the fragment size as size in bytes */
SDL_CalculateAudioSpec(&this->spec);
/* Allocate mixing buffer */
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;
}
/* Setup callback */
audio_device = this;
ndspSetCallback(AudioFrameFinished, this);
dspHook(&dsp_hook, N3DSAUD_DspHook);
return 0;
}
static int N3DSAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
{
/* Delay to make this sort of simulate real audio input. */
SDL_Delay((this->spec.samples * 1000) / this->spec.freq);
/* always return a full buffer of silence. */
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--;
/* 0x18 is reserved for video, 0x30 is the default for main thread */
current_priority = SDL_clamp(current_priority, 0x19, 0x2F);
svcSetThreadPriority(CUR_THREAD_HANDLE, current_priority);
}
static SDL_bool N3DSAUDIO_Init(SDL_AudioDriverImpl *impl)
{
/* Set the function pointers */
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;
/* Should be possible, but micInit would fail */
impl->HasCaptureSupport = SDL_FALSE;
impl->CaptureFromDevice = N3DSAUDIO_CaptureFromDevice;
return SDL_TRUE; /* this audio target is available. */
}
AudioBootStrap N3DSAUDIO_bootstrap = {
N3DSAUDIO_DRIVER_NAME,
"SDL N3DS audio driver",
N3DSAUDIO_Init,
0
};
/**
* Cleans up all allocated memory, safe to call with null pointers
*/
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:
/* 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;
found_valid_format = SDL_TRUE;
break;
case 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;
found_valid_format = SDL_TRUE;
break;
default:
test_format = SDL_NextAudioFormat();
break;
}
}
return found_valid_format ? 0 : -1;
}
#endif /* SDL_AUDIO_DRIVER_N3DS */
/* vi: set sts=4 ts=4 sw=4 expandtab: */