SDL_n3dsaudio.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifdef SDL_AUDIO_DRIVER_N3DS
  20. /* N3DS Audio driver */
  21. #include "../SDL_sysaudio.h"
  22. #include "SDL_n3dsaudio.h"
  23. #define N3DSAUDIO_DRIVER_NAME "n3ds"
  24. static dspHookCookie dsp_hook;
  25. static SDL_AudioDevice *audio_device;
  26. static void FreePrivateData(_THIS);
  27. static int FindAudioFormat(_THIS);
  28. static SDL_INLINE void
  29. contextLock(_THIS)
  30. {
  31. LightLock_Lock(&this->hidden->lock);
  32. }
  33. static SDL_INLINE void
  34. contextUnlock(_THIS)
  35. {
  36. LightLock_Unlock(&this->hidden->lock);
  37. }
  38. static void
  39. N3DSAUD_LockAudio(_THIS)
  40. {
  41. contextLock(this);
  42. }
  43. static void
  44. N3DSAUD_UnlockAudio(_THIS)
  45. {
  46. contextUnlock(this);
  47. }
  48. static void
  49. N3DSAUD_DspHook(DSP_HookType hook)
  50. {
  51. if (hook == DSPHOOK_ONCANCEL) {
  52. contextLock(audio_device);
  53. audio_device->hidden->isCancelled = SDL_TRUE;
  54. SDL_AtomicSet(&audio_device->enabled, SDL_FALSE);
  55. CondVar_Broadcast(&audio_device->hidden->cv);
  56. contextUnlock(audio_device);
  57. }
  58. }
  59. static void
  60. AudioFrameFinished(void *device)
  61. {
  62. bool shouldBroadcast = false;
  63. unsigned i;
  64. SDL_AudioDevice *this = (SDL_AudioDevice *) device;
  65. contextLock(this);
  66. for (i = 0; i < NUM_BUFFERS; i++) {
  67. if (this->hidden->waveBuf[i].status == NDSP_WBUF_DONE) {
  68. this->hidden->waveBuf[i].status = NDSP_WBUF_FREE;
  69. shouldBroadcast = SDL_TRUE;
  70. }
  71. }
  72. if (shouldBroadcast) {
  73. CondVar_Broadcast(&this->hidden->cv);
  74. }
  75. contextUnlock(this);
  76. }
  77. static int
  78. N3DSAUDIO_OpenDevice(_THIS, const char *devname)
  79. {
  80. Result ndsp_init_res;
  81. Uint8 *data_vaddr;
  82. float mix[12];
  83. this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof *this->hidden);
  84. if (this->hidden == NULL) {
  85. return SDL_OutOfMemory();
  86. }
  87. /* Initialise the DSP service */
  88. ndsp_init_res = ndspInit();
  89. if (R_FAILED(ndsp_init_res)) {
  90. if ((R_SUMMARY(ndsp_init_res) == RS_NOTFOUND) && (R_MODULE(ndsp_init_res) == RM_DSP)) {
  91. SDL_SetError("DSP init failed: dspfirm.cdc missing!");
  92. } else {
  93. SDL_SetError("DSP init failed. Error code: 0x%lX", ndsp_init_res);
  94. }
  95. return -1;
  96. }
  97. /* Initialise internal state */
  98. LightLock_Init(&this->hidden->lock);
  99. CondVar_Init(&this->hidden->cv);
  100. if (this->spec.channels > 2) {
  101. this->spec.channels = 2;
  102. }
  103. /* Should not happen but better be safe. */
  104. if (FindAudioFormat(this) < 0) {
  105. return SDL_SetError("No supported audio format found.");
  106. }
  107. /* Update the fragment size as size in bytes */
  108. SDL_CalculateAudioSpec(&this->spec);
  109. /* Allocate mixing buffer */
  110. if (this->spec.size >= SDL_MAX_UINT32 / 2) {
  111. return SDL_SetError("Mixing buffer is too large.");
  112. }
  113. this->hidden->mixlen = this->spec.size;
  114. this->hidden->mixbuf = (Uint8 *) SDL_malloc(this->spec.size);
  115. if (this->hidden->mixbuf == NULL) {
  116. return SDL_OutOfMemory();
  117. }
  118. SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
  119. data_vaddr = (Uint8 *) linearAlloc(this->hidden->mixlen * NUM_BUFFERS);
  120. if (data_vaddr == NULL) {
  121. return SDL_OutOfMemory();
  122. }
  123. SDL_memset(data_vaddr, 0, this->hidden->mixlen * NUM_BUFFERS);
  124. DSP_FlushDataCache(data_vaddr, this->hidden->mixlen * NUM_BUFFERS);
  125. this->hidden->nextbuf = 0;
  126. this->hidden->channels = this->spec.channels;
  127. this->hidden->samplerate = this->spec.freq;
  128. ndspChnReset(0);
  129. ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
  130. ndspChnSetRate(0, this->spec.freq);
  131. ndspChnSetFormat(0, this->hidden->format);
  132. SDL_memset(mix, 0, sizeof(mix));
  133. mix[0] = 1.0;
  134. mix[1] = 1.0;
  135. ndspChnSetMix(0, mix);
  136. SDL_memset(this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
  137. for (unsigned i = 0; i < NUM_BUFFERS; i++) {
  138. this->hidden->waveBuf[i].data_vaddr = data_vaddr;
  139. this->hidden->waveBuf[i].nsamples = this->hidden->mixlen / this->hidden->bytePerSample;
  140. data_vaddr += this->hidden->mixlen;
  141. }
  142. /* Setup callback */
  143. audio_device = this;
  144. ndspSetCallback(AudioFrameFinished, this);
  145. dspHook(&dsp_hook, N3DSAUD_DspHook);
  146. return 0;
  147. }
  148. static int
  149. N3DSAUDIO_CaptureFromDevice(_THIS, void *buffer, int buflen)
  150. {
  151. /* Delay to make this sort of simulate real audio input. */
  152. SDL_Delay((this->spec.samples * 1000) / this->spec.freq);
  153. /* always return a full buffer of silence. */
  154. SDL_memset(buffer, this->spec.silence, buflen);
  155. return buflen;
  156. }
  157. static void
  158. N3DSAUDIO_PlayDevice(_THIS)
  159. {
  160. size_t nextbuf;
  161. size_t sampleLen;
  162. contextLock(this);
  163. nextbuf = this->hidden->nextbuf;
  164. sampleLen = this->hidden->mixlen;
  165. if (this->hidden->isCancelled ||
  166. this->hidden->waveBuf[nextbuf].status != NDSP_WBUF_FREE) {
  167. contextUnlock(this);
  168. return;
  169. }
  170. this->hidden->nextbuf = (nextbuf + 1) % NUM_BUFFERS;
  171. contextUnlock(this);
  172. memcpy((void *) this->hidden->waveBuf[nextbuf].data_vaddr,
  173. this->hidden->mixbuf, sampleLen);
  174. DSP_FlushDataCache(this->hidden->waveBuf[nextbuf].data_vaddr, sampleLen);
  175. ndspChnWaveBufAdd(0, &this->hidden->waveBuf[nextbuf]);
  176. }
  177. static void
  178. N3DSAUDIO_WaitDevice(_THIS)
  179. {
  180. contextLock(this);
  181. while (!this->hidden->isCancelled &&
  182. this->hidden->waveBuf[this->hidden->nextbuf].status != NDSP_WBUF_FREE) {
  183. CondVar_Wait(&this->hidden->cv, &this->hidden->lock);
  184. }
  185. contextUnlock(this);
  186. }
  187. static Uint8 *
  188. N3DSAUDIO_GetDeviceBuf(_THIS)
  189. {
  190. return this->hidden->mixbuf;
  191. }
  192. static void
  193. N3DSAUDIO_CloseDevice(_THIS)
  194. {
  195. contextLock(this);
  196. dspUnhook(&dsp_hook);
  197. ndspSetCallback(NULL, NULL);
  198. if (!this->hidden->isCancelled) {
  199. ndspChnReset(0);
  200. memset(this->hidden->waveBuf, 0, sizeof(ndspWaveBuf) * NUM_BUFFERS);
  201. CondVar_Broadcast(&this->hidden->cv);
  202. }
  203. contextUnlock(this);
  204. ndspExit();
  205. FreePrivateData(this);
  206. }
  207. static void
  208. N3DSAUDIO_ThreadInit(_THIS)
  209. {
  210. s32 current_priority;
  211. svcGetThreadPriority(&current_priority, CUR_THREAD_HANDLE);
  212. current_priority--;
  213. /* 0x18 is reserved for video, 0x30 is the default for main thread */
  214. current_priority = SDL_clamp(current_priority, 0x19, 0x2F);
  215. svcSetThreadPriority(CUR_THREAD_HANDLE, current_priority);
  216. }
  217. static SDL_bool
  218. N3DSAUDIO_Init(SDL_AudioDriverImpl *impl)
  219. {
  220. /* Set the function pointers */
  221. impl->OpenDevice = N3DSAUDIO_OpenDevice;
  222. impl->PlayDevice = N3DSAUDIO_PlayDevice;
  223. impl->WaitDevice = N3DSAUDIO_WaitDevice;
  224. impl->GetDeviceBuf = N3DSAUDIO_GetDeviceBuf;
  225. impl->CloseDevice = N3DSAUDIO_CloseDevice;
  226. impl->ThreadInit = N3DSAUDIO_ThreadInit;
  227. impl->LockDevice = N3DSAUD_LockAudio;
  228. impl->UnlockDevice = N3DSAUD_UnlockAudio;
  229. impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
  230. /* Should be possible, but micInit would fail */
  231. impl->HasCaptureSupport = SDL_FALSE;
  232. impl->CaptureFromDevice = N3DSAUDIO_CaptureFromDevice;
  233. return SDL_TRUE; /* this audio target is available. */
  234. }
  235. AudioBootStrap N3DSAUDIO_bootstrap = {
  236. N3DSAUDIO_DRIVER_NAME,
  237. "SDL N3DS audio driver",
  238. N3DSAUDIO_Init,
  239. 0
  240. };
  241. /**
  242. * Cleans up all allocated memory, safe to call with null pointers
  243. */
  244. static void
  245. FreePrivateData(_THIS)
  246. {
  247. if (!this->hidden) {
  248. return;
  249. }
  250. if (this->hidden->waveBuf[0].data_vaddr) {
  251. linearFree((void *) this->hidden->waveBuf[0].data_vaddr);
  252. }
  253. if (this->hidden->mixbuf) {
  254. SDL_free(this->hidden->mixbuf);
  255. this->hidden->mixbuf = NULL;
  256. }
  257. SDL_free(this->hidden);
  258. this->hidden = NULL;
  259. }
  260. static int
  261. FindAudioFormat(_THIS)
  262. {
  263. SDL_bool found_valid_format = SDL_FALSE;
  264. Uint16 test_format = SDL_FirstAudioFormat(this->spec.format);
  265. while (!found_valid_format && test_format) {
  266. this->spec.format = test_format;
  267. switch (test_format) {
  268. case AUDIO_S8:
  269. /* Signed 8-bit audio supported */
  270. this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM8 : NDSP_FORMAT_MONO_PCM8;
  271. this->hidden->isSigned = 1;
  272. this->hidden->bytePerSample = this->spec.channels;
  273. found_valid_format = SDL_TRUE;
  274. break;
  275. case AUDIO_S16:
  276. /* Signed 16-bit audio supported */
  277. this->hidden->format = (this->spec.channels == 2) ? NDSP_FORMAT_STEREO_PCM16 : NDSP_FORMAT_MONO_PCM16;
  278. this->hidden->isSigned = 1;
  279. this->hidden->bytePerSample = this->spec.channels * 2;
  280. found_valid_format = SDL_TRUE;
  281. break;
  282. default:
  283. test_format = SDL_NextAudioFormat();
  284. break;
  285. }
  286. }
  287. return found_valid_format ? 0 : -1;
  288. }
  289. #endif /* SDL_AUDIO_DRIVER_N3DS */
  290. /* vi: set sts=4 ts=4 sw=4 expandtab: */