SDL_audio.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  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. /* Allow access to a raw mixing buffer */
  20. #include "SDL.h"
  21. #include "SDL_audio.h"
  22. #include "SDL_audio_c.h"
  23. #include "SDL_audiomem.h"
  24. #include "SDL_sysaudio.h"
  25. #define _THIS SDL_AudioDevice *_this
  26. static SDL_AudioDriver current_audio;
  27. static SDL_AudioDevice *open_devices[16];
  28. /* !!! FIXME: These are wordy and unlocalized... */
  29. #define DEFAULT_OUTPUT_DEVNAME "System audio output device"
  30. #define DEFAULT_INPUT_DEVNAME "System audio capture device"
  31. /*
  32. * Not all of these will be compiled and linked in, but it's convenient
  33. * to have a complete list here and saves yet-another block of #ifdefs...
  34. * Please see bootstrap[], below, for the actual #ifdef mess.
  35. */
  36. extern AudioBootStrap BSD_AUDIO_bootstrap;
  37. extern AudioBootStrap DSP_bootstrap;
  38. extern AudioBootStrap ALSA_bootstrap;
  39. extern AudioBootStrap PULSEAUDIO_bootstrap;
  40. extern AudioBootStrap QSAAUDIO_bootstrap;
  41. extern AudioBootStrap SUNAUDIO_bootstrap;
  42. extern AudioBootStrap ARTS_bootstrap;
  43. extern AudioBootStrap ESD_bootstrap;
  44. #if SDL_AUDIO_DRIVER_NACL
  45. extern AudioBootStrap NACLAUD_bootstrap;
  46. #endif
  47. extern AudioBootStrap NAS_bootstrap;
  48. extern AudioBootStrap XAUDIO2_bootstrap;
  49. extern AudioBootStrap DSOUND_bootstrap;
  50. extern AudioBootStrap WINMM_bootstrap;
  51. extern AudioBootStrap PAUDIO_bootstrap;
  52. extern AudioBootStrap HAIKUAUDIO_bootstrap;
  53. extern AudioBootStrap COREAUDIO_bootstrap;
  54. extern AudioBootStrap SNDMGR_bootstrap;
  55. extern AudioBootStrap DISKAUD_bootstrap;
  56. extern AudioBootStrap DUMMYAUD_bootstrap;
  57. extern AudioBootStrap DCAUD_bootstrap;
  58. extern AudioBootStrap DART_bootstrap;
  59. extern AudioBootStrap NDSAUD_bootstrap;
  60. extern AudioBootStrap FUSIONSOUND_bootstrap;
  61. extern AudioBootStrap ANDROIDAUD_bootstrap;
  62. extern AudioBootStrap PSPAUD_bootstrap;
  63. extern AudioBootStrap SNDIO_bootstrap;
  64. /* Available audio drivers */
  65. static const AudioBootStrap *const bootstrap[] = {
  66. #if SDL_AUDIO_DRIVER_PULSEAUDIO
  67. &PULSEAUDIO_bootstrap,
  68. #endif
  69. #if SDL_AUDIO_DRIVER_ALSA
  70. &ALSA_bootstrap,
  71. #endif
  72. #if SDL_AUDIO_DRIVER_SNDIO
  73. &SNDIO_bootstrap,
  74. #endif
  75. #if SDL_AUDIO_DRIVER_BSD
  76. &BSD_AUDIO_bootstrap,
  77. #endif
  78. #if SDL_AUDIO_DRIVER_OSS
  79. &DSP_bootstrap,
  80. #endif
  81. #if SDL_AUDIO_DRIVER_QSA
  82. &QSAAUDIO_bootstrap,
  83. #endif
  84. #if SDL_AUDIO_DRIVER_SUNAUDIO
  85. &SUNAUDIO_bootstrap,
  86. #endif
  87. #if SDL_AUDIO_DRIVER_ARTS
  88. &ARTS_bootstrap,
  89. #endif
  90. #if SDL_AUDIO_DRIVER_ESD
  91. &ESD_bootstrap,
  92. #endif
  93. #if SDL_AUDIO_DRIVER_NACL
  94. &NACLAUD_bootstrap,
  95. #endif
  96. #if SDL_AUDIO_DRIVER_NAS
  97. &NAS_bootstrap,
  98. #endif
  99. #if SDL_AUDIO_DRIVER_XAUDIO2
  100. &XAUDIO2_bootstrap,
  101. #endif
  102. #if SDL_AUDIO_DRIVER_DSOUND
  103. &DSOUND_bootstrap,
  104. #endif
  105. #if SDL_AUDIO_DRIVER_WINMM
  106. &WINMM_bootstrap,
  107. #endif
  108. #if SDL_AUDIO_DRIVER_PAUDIO
  109. &PAUDIO_bootstrap,
  110. #endif
  111. #if SDL_AUDIO_DRIVER_HAIKU
  112. &HAIKUAUDIO_bootstrap,
  113. #endif
  114. #if SDL_AUDIO_DRIVER_COREAUDIO
  115. &COREAUDIO_bootstrap,
  116. #endif
  117. #if SDL_AUDIO_DRIVER_DISK
  118. &DISKAUD_bootstrap,
  119. #endif
  120. #if SDL_AUDIO_DRIVER_DUMMY
  121. &DUMMYAUD_bootstrap,
  122. #endif
  123. #if SDL_AUDIO_DRIVER_FUSIONSOUND
  124. &FUSIONSOUND_bootstrap,
  125. #endif
  126. #if SDL_AUDIO_DRIVER_ANDROID
  127. &ANDROIDAUD_bootstrap,
  128. #endif
  129. #if SDL_AUDIO_DRIVER_PSP
  130. &PSPAUD_bootstrap,
  131. #endif
  132. NULL
  133. };
  134. static SDL_AudioDevice *
  135. get_audio_device(SDL_AudioDeviceID id)
  136. {
  137. id--;
  138. if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
  139. SDL_SetError("Invalid audio device ID");
  140. return NULL;
  141. }
  142. return open_devices[id];
  143. }
  144. /* stubs for audio drivers that don't need a specific entry point... */
  145. static void
  146. SDL_AudioDetectDevices_Default(int iscapture, SDL_AddAudioDevice addfn)
  147. { /* no-op. */
  148. }
  149. static void
  150. SDL_AudioThreadInit_Default(_THIS)
  151. { /* no-op. */
  152. }
  153. static void
  154. SDL_AudioWaitDevice_Default(_THIS)
  155. { /* no-op. */
  156. }
  157. static void
  158. SDL_AudioPlayDevice_Default(_THIS)
  159. { /* no-op. */
  160. }
  161. static int
  162. SDL_AudioGetPendingBytes_Default(_THIS)
  163. {
  164. return 0;
  165. }
  166. static Uint8 *
  167. SDL_AudioGetDeviceBuf_Default(_THIS)
  168. {
  169. return NULL;
  170. }
  171. static void
  172. SDL_AudioWaitDone_Default(_THIS)
  173. { /* no-op. */
  174. }
  175. static void
  176. SDL_AudioCloseDevice_Default(_THIS)
  177. { /* no-op. */
  178. }
  179. static void
  180. SDL_AudioDeinitialize_Default(void)
  181. { /* no-op. */
  182. }
  183. static int
  184. SDL_AudioOpenDevice_Default(_THIS, const char *devname, int iscapture)
  185. {
  186. return -1;
  187. }
  188. static SDL_INLINE SDL_bool
  189. is_in_audio_device_thread(SDL_AudioDevice * device)
  190. {
  191. /* The device thread locks the same mutex, but not through the public API.
  192. This check is in case the application, in the audio callback,
  193. tries to lock the thread that we've already locked from the
  194. device thread...just in case we only have non-recursive mutexes. */
  195. if (device->thread && (SDL_ThreadID() == device->threadid)) {
  196. return SDL_TRUE;
  197. }
  198. return SDL_FALSE;
  199. }
  200. static void
  201. SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
  202. {
  203. if (!is_in_audio_device_thread(device)) {
  204. SDL_LockMutex(device->mixer_lock);
  205. }
  206. }
  207. static void
  208. SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
  209. {
  210. if (!is_in_audio_device_thread(device)) {
  211. SDL_UnlockMutex(device->mixer_lock);
  212. }
  213. }
  214. static void
  215. finalize_audio_entry_points(void)
  216. {
  217. /*
  218. * Fill in stub functions for unused driver entry points. This lets us
  219. * blindly call them without having to check for validity first.
  220. */
  221. #define FILL_STUB(x) \
  222. if (current_audio.impl.x == NULL) { \
  223. current_audio.impl.x = SDL_Audio##x##_Default; \
  224. }
  225. FILL_STUB(DetectDevices);
  226. FILL_STUB(OpenDevice);
  227. FILL_STUB(ThreadInit);
  228. FILL_STUB(WaitDevice);
  229. FILL_STUB(PlayDevice);
  230. FILL_STUB(GetPendingBytes);
  231. FILL_STUB(GetDeviceBuf);
  232. FILL_STUB(WaitDone);
  233. FILL_STUB(CloseDevice);
  234. FILL_STUB(LockDevice);
  235. FILL_STUB(UnlockDevice);
  236. FILL_STUB(Deinitialize);
  237. #undef FILL_STUB
  238. }
  239. #if 0 /* !!! FIXME: rewrite/remove this streamer code. */
  240. /* Streaming functions (for when the input and output buffer sizes are different) */
  241. /* Write [length] bytes from buf into the streamer */
  242. static void
  243. SDL_StreamWrite(SDL_AudioStreamer * stream, Uint8 * buf, int length)
  244. {
  245. int i;
  246. for (i = 0; i < length; ++i) {
  247. stream->buffer[stream->write_pos] = buf[i];
  248. ++stream->write_pos;
  249. }
  250. }
  251. /* Read [length] bytes out of the streamer into buf */
  252. static void
  253. SDL_StreamRead(SDL_AudioStreamer * stream, Uint8 * buf, int length)
  254. {
  255. int i;
  256. for (i = 0; i < length; ++i) {
  257. buf[i] = stream->buffer[stream->read_pos];
  258. ++stream->read_pos;
  259. }
  260. }
  261. static int
  262. SDL_StreamLength(SDL_AudioStreamer * stream)
  263. {
  264. return (stream->write_pos - stream->read_pos) % stream->max_len;
  265. }
  266. /* Initialize the stream by allocating the buffer and setting the read/write heads to the beginning */
  267. #if 0
  268. static int
  269. SDL_StreamInit(SDL_AudioStreamer * stream, int max_len, Uint8 silence)
  270. {
  271. /* First try to allocate the buffer */
  272. stream->buffer = (Uint8 *) SDL_malloc(max_len);
  273. if (stream->buffer == NULL) {
  274. return -1;
  275. }
  276. stream->max_len = max_len;
  277. stream->read_pos = 0;
  278. stream->write_pos = 0;
  279. /* Zero out the buffer */
  280. SDL_memset(stream->buffer, silence, max_len);
  281. return 0;
  282. }
  283. #endif
  284. /* Deinitialize the stream simply by freeing the buffer */
  285. static void
  286. SDL_StreamDeinit(SDL_AudioStreamer * stream)
  287. {
  288. SDL_free(stream->buffer);
  289. }
  290. #endif
  291. /* buffer queueing support... */
  292. /* this expects that you managed thread safety elsewhere. */
  293. static void
  294. free_audio_queue(SDL_AudioBufferQueue *buffer)
  295. {
  296. while (buffer) {
  297. SDL_AudioBufferQueue *next = buffer->next;
  298. SDL_free(buffer);
  299. buffer = next;
  300. }
  301. }
  302. static void SDLCALL
  303. SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int _len)
  304. {
  305. /* this function always holds the mixer lock before being called. */
  306. Uint32 len = (Uint32) _len;
  307. SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
  308. SDL_AudioBufferQueue *buffer;
  309. SDL_assert(device != NULL); /* this shouldn't ever happen, right?! */
  310. SDL_assert(_len >= 0); /* this shouldn't ever happen, right?! */
  311. while ((len > 0) && ((buffer = device->buffer_queue_head) != NULL)) {
  312. const Uint32 avail = buffer->datalen - buffer->startpos;
  313. const Uint32 cpy = SDL_min(len, avail);
  314. SDL_assert(device->queued_bytes >= avail);
  315. SDL_memcpy(stream, buffer->data + buffer->startpos, cpy);
  316. buffer->startpos += cpy;
  317. stream += cpy;
  318. device->queued_bytes -= cpy;
  319. len -= cpy;
  320. if (buffer->startpos == buffer->datalen) { /* packet is done, put it in the pool. */
  321. device->buffer_queue_head = buffer->next;
  322. SDL_assert((buffer->next != NULL) || (buffer == device->buffer_queue_tail));
  323. buffer->next = device->buffer_queue_pool;
  324. device->buffer_queue_pool = buffer;
  325. }
  326. }
  327. SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
  328. if (len > 0) { /* fill any remaining space in the stream with silence. */
  329. SDL_assert(device->buffer_queue_head == NULL);
  330. SDL_memset(stream, device->spec.silence, len);
  331. }
  332. if (device->buffer_queue_head == NULL) {
  333. device->buffer_queue_tail = NULL; /* in case we drained the queue entirely. */
  334. }
  335. }
  336. int
  337. SDL_QueueAudio(SDL_AudioDeviceID devid, const void *_data, Uint32 len)
  338. {
  339. SDL_AudioDevice *device = get_audio_device(devid);
  340. const Uint8 *data = (const Uint8 *) _data;
  341. SDL_AudioBufferQueue *orighead;
  342. SDL_AudioBufferQueue *origtail;
  343. Uint32 origlen;
  344. Uint32 datalen;
  345. if (!device) {
  346. return -1; /* get_audio_device() will have set the error state */
  347. }
  348. if (device->spec.callback != SDL_BufferQueueDrainCallback) {
  349. return SDL_SetError("Audio device has a callback, queueing not allowed");
  350. }
  351. current_audio.impl.LockDevice(device);
  352. orighead = device->buffer_queue_head;
  353. origtail = device->buffer_queue_tail;
  354. origlen = origtail ? origtail->datalen : 0;
  355. while (len > 0) {
  356. SDL_AudioBufferQueue *packet = device->buffer_queue_tail;
  357. SDL_assert(!packet || (packet->datalen <= SDL_AUDIOBUFFERQUEUE_PACKETLEN));
  358. if (!packet || (packet->datalen >= SDL_AUDIOBUFFERQUEUE_PACKETLEN)) {
  359. /* tail packet missing or completely full; we need a new packet. */
  360. packet = device->buffer_queue_pool;
  361. if (packet != NULL) {
  362. /* we have one available in the pool. */
  363. device->buffer_queue_pool = packet->next;
  364. } else {
  365. /* Have to allocate a new one! */
  366. packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
  367. if (packet == NULL) {
  368. /* uhoh, reset so we've queued nothing new, free what we can. */
  369. if (!origtail) {
  370. packet = device->buffer_queue_head; /* whole queue. */
  371. } else {
  372. packet = origtail->next; /* what we added to existing queue. */
  373. origtail->next = NULL;
  374. origtail->datalen = origlen;
  375. }
  376. device->buffer_queue_head = orighead;
  377. device->buffer_queue_tail = origtail;
  378. device->buffer_queue_pool = NULL;
  379. current_audio.impl.UnlockDevice(device);
  380. free_audio_queue(packet); /* give back what we can. */
  381. return SDL_OutOfMemory();
  382. }
  383. }
  384. packet->datalen = 0;
  385. packet->startpos = 0;
  386. packet->next = NULL;
  387. SDL_assert((device->buffer_queue_head != NULL) == (device->queued_bytes != 0));
  388. if (device->buffer_queue_tail == NULL) {
  389. device->buffer_queue_head = packet;
  390. } else {
  391. device->buffer_queue_tail->next = packet;
  392. }
  393. device->buffer_queue_tail = packet;
  394. }
  395. datalen = SDL_min(len, SDL_AUDIOBUFFERQUEUE_PACKETLEN - packet->datalen);
  396. SDL_memcpy(packet->data + packet->datalen, data, datalen);
  397. data += datalen;
  398. len -= datalen;
  399. packet->datalen += datalen;
  400. device->queued_bytes += datalen;
  401. }
  402. current_audio.impl.UnlockDevice(device);
  403. return 0;
  404. }
  405. Uint32
  406. SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
  407. {
  408. Uint32 retval = 0;
  409. SDL_AudioDevice *device = get_audio_device(devid);
  410. /* Nothing to do unless we're set up for queueing. */
  411. if (device && (device->spec.callback == SDL_BufferQueueDrainCallback)) {
  412. current_audio.impl.LockDevice(device);
  413. retval = device->queued_bytes + current_audio.impl.GetPendingBytes(device);
  414. current_audio.impl.UnlockDevice(device);
  415. }
  416. return retval;
  417. }
  418. void
  419. SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
  420. {
  421. SDL_AudioDevice *device = get_audio_device(devid);
  422. SDL_AudioBufferQueue *buffer = NULL;
  423. if (!device) {
  424. return; /* nothing to do. */
  425. }
  426. /* Blank out the device and release the mutex. Free it afterwards. */
  427. current_audio.impl.LockDevice(device);
  428. buffer = device->buffer_queue_head;
  429. device->buffer_queue_tail = NULL;
  430. device->buffer_queue_head = NULL;
  431. device->queued_bytes = 0;
  432. current_audio.impl.UnlockDevice(device);
  433. free_audio_queue(buffer);
  434. }
  435. #if defined(__ANDROID__)
  436. #include <android/log.h>
  437. #endif
  438. /* The general mixing thread function */
  439. int SDLCALL
  440. SDL_RunAudio(void *devicep)
  441. {
  442. SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
  443. Uint8 *stream;
  444. int stream_len;
  445. void *udata;
  446. void (SDLCALL * fill) (void *userdata, Uint8 * stream, int len);
  447. Uint32 delay;
  448. #if 0 /* !!! FIXME: rewrite/remove this streamer code. */
  449. /* For streaming when the buffer sizes don't match up */
  450. Uint8 *istream;
  451. int istream_len = 0;
  452. #endif
  453. /* The audio mixing is always a high priority thread */
  454. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
  455. /* Perform any thread setup */
  456. device->threadid = SDL_ThreadID();
  457. current_audio.impl.ThreadInit(device);
  458. /* Set up the mixing function */
  459. fill = device->spec.callback;
  460. udata = device->spec.userdata;
  461. /* By default do not stream */
  462. device->use_streamer = 0;
  463. if (device->convert.needed) {
  464. #if 0 /* !!! FIXME: I took len_div out of the structure. Use rate_incr instead? */
  465. /* If the result of the conversion alters the length, i.e. resampling is being used, use the streamer */
  466. if (device->convert.len_mult != 1 || device->convert.len_div != 1) {
  467. /* The streamer's maximum length should be twice whichever is larger: spec.size or len_cvt */
  468. stream_max_len = 2 * device->spec.size;
  469. if (device->convert.len_mult > device->convert.len_div) {
  470. stream_max_len *= device->convert.len_mult;
  471. stream_max_len /= device->convert.len_div;
  472. }
  473. if (SDL_StreamInit(&device->streamer, stream_max_len, silence) <
  474. 0)
  475. return -1;
  476. device->use_streamer = 1;
  477. /* istream_len should be the length of what we grab from the callback and feed to conversion,
  478. so that we get close to spec_size. I.e. we want device.spec_size = istream_len * u / d
  479. */
  480. istream_len =
  481. device->spec.size * device->convert.len_div /
  482. device->convert.len_mult;
  483. }
  484. #endif
  485. stream_len = device->convert.len;
  486. } else {
  487. stream_len = device->spec.size;
  488. }
  489. /* Calculate the delay while paused */
  490. delay = ((device->spec.samples * 1000) / device->spec.freq);
  491. /* Determine if the streamer is necessary here */
  492. #if 0 /* !!! FIXME: rewrite/remove this streamer code. */
  493. if (device->use_streamer == 1) {
  494. /* This code is almost the same as the old code. The difference is, instead of reading
  495. directly from the callback into "stream", then converting and sending the audio off,
  496. we go: callback -> "istream" -> (conversion) -> streamer -> stream -> device.
  497. However, reading and writing with streamer are done separately:
  498. - We only call the callback and write to the streamer when the streamer does not
  499. contain enough samples to output to the device.
  500. - We only read from the streamer and tell the device to play when the streamer
  501. does have enough samples to output.
  502. This allows us to perform resampling in the conversion step, where the output of the
  503. resampling process can be any number. We will have to see what a good size for the
  504. stream's maximum length is, but I suspect 2*max(len_cvt, stream_len) is a good figure.
  505. */
  506. while (device->enabled) {
  507. if (device->paused) {
  508. SDL_Delay(delay);
  509. continue;
  510. }
  511. /* Only read in audio if the streamer doesn't have enough already (if it does not have enough samples to output) */
  512. if (SDL_StreamLength(&device->streamer) < stream_len) {
  513. /* Set up istream */
  514. if (device->convert.needed) {
  515. if (device->convert.buf) {
  516. istream = device->convert.buf;
  517. } else {
  518. continue;
  519. }
  520. } else {
  521. /* FIXME: Ryan, this is probably wrong. I imagine we don't want to get
  522. * a device buffer both here and below in the stream output.
  523. */
  524. istream = current_audio.impl.GetDeviceBuf(device);
  525. if (istream == NULL) {
  526. istream = device->fake_stream;
  527. }
  528. }
  529. /* Read from the callback into the _input_ stream */
  530. SDL_LockMutex(device->mixer_lock);
  531. (*fill) (udata, istream, istream_len);
  532. SDL_UnlockMutex(device->mixer_lock);
  533. /* Convert the audio if necessary and write to the streamer */
  534. if (device->convert.needed) {
  535. SDL_ConvertAudio(&device->convert);
  536. if (istream == NULL) {
  537. istream = device->fake_stream;
  538. }
  539. /* SDL_memcpy(istream, device->convert.buf, device->convert.len_cvt); */
  540. SDL_StreamWrite(&device->streamer, device->convert.buf,
  541. device->convert.len_cvt);
  542. } else {
  543. SDL_StreamWrite(&device->streamer, istream, istream_len);
  544. }
  545. }
  546. /* Only output audio if the streamer has enough to output */
  547. if (SDL_StreamLength(&device->streamer) >= stream_len) {
  548. /* Set up the output stream */
  549. if (device->convert.needed) {
  550. if (device->convert.buf) {
  551. stream = device->convert.buf;
  552. } else {
  553. continue;
  554. }
  555. } else {
  556. stream = current_audio.impl.GetDeviceBuf(device);
  557. if (stream == NULL) {
  558. stream = device->fake_stream;
  559. }
  560. }
  561. /* Now read from the streamer */
  562. SDL_StreamRead(&device->streamer, stream, stream_len);
  563. /* Ready current buffer for play and change current buffer */
  564. if (stream != device->fake_stream) {
  565. current_audio.impl.PlayDevice(device);
  566. /* Wait for an audio buffer to become available */
  567. current_audio.impl.WaitDevice(device);
  568. } else {
  569. SDL_Delay(delay);
  570. }
  571. }
  572. }
  573. } else
  574. #endif
  575. {
  576. /* Otherwise, do not use the streamer. This is the old code. */
  577. const int silence = (int) device->spec.silence;
  578. /* Loop, filling the audio buffers */
  579. while (device->enabled) {
  580. /* Fill the current buffer with sound */
  581. if (device->convert.needed) {
  582. if (device->convert.buf) {
  583. stream = device->convert.buf;
  584. } else {
  585. continue;
  586. }
  587. } else {
  588. stream = current_audio.impl.GetDeviceBuf(device);
  589. if (stream == NULL) {
  590. stream = device->fake_stream;
  591. }
  592. }
  593. SDL_LockMutex(device->mixer_lock);
  594. if (device->paused) {
  595. SDL_memset(stream, silence, stream_len);
  596. } else {
  597. (*fill) (udata, stream, stream_len);
  598. }
  599. SDL_UnlockMutex(device->mixer_lock);
  600. /* Convert the audio if necessary */
  601. if (device->convert.needed) {
  602. SDL_ConvertAudio(&device->convert);
  603. stream = current_audio.impl.GetDeviceBuf(device);
  604. if (stream == NULL) {
  605. stream = device->fake_stream;
  606. }
  607. SDL_memcpy(stream, device->convert.buf,
  608. device->convert.len_cvt);
  609. }
  610. /* Ready current buffer for play and change current buffer */
  611. if (stream != device->fake_stream) {
  612. current_audio.impl.PlayDevice(device);
  613. /* Wait for an audio buffer to become available */
  614. current_audio.impl.WaitDevice(device);
  615. } else {
  616. SDL_Delay(delay);
  617. }
  618. }
  619. }
  620. /* Wait for the audio to drain.. */
  621. current_audio.impl.WaitDone(device);
  622. /* If necessary, deinit the streamer */
  623. #if 0 /* !!! FIXME: rewrite/remove this streamer code. */
  624. if (device->use_streamer == 1)
  625. SDL_StreamDeinit(&device->streamer);
  626. #endif
  627. return (0);
  628. }
  629. static SDL_AudioFormat
  630. SDL_ParseAudioFormat(const char *string)
  631. {
  632. #define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
  633. CHECK_FMT_STRING(U8);
  634. CHECK_FMT_STRING(S8);
  635. CHECK_FMT_STRING(U16LSB);
  636. CHECK_FMT_STRING(S16LSB);
  637. CHECK_FMT_STRING(U16MSB);
  638. CHECK_FMT_STRING(S16MSB);
  639. CHECK_FMT_STRING(U16SYS);
  640. CHECK_FMT_STRING(S16SYS);
  641. CHECK_FMT_STRING(U16);
  642. CHECK_FMT_STRING(S16);
  643. CHECK_FMT_STRING(S32LSB);
  644. CHECK_FMT_STRING(S32MSB);
  645. CHECK_FMT_STRING(S32SYS);
  646. CHECK_FMT_STRING(S32);
  647. CHECK_FMT_STRING(F32LSB);
  648. CHECK_FMT_STRING(F32MSB);
  649. CHECK_FMT_STRING(F32SYS);
  650. CHECK_FMT_STRING(F32);
  651. #undef CHECK_FMT_STRING
  652. return 0;
  653. }
  654. int
  655. SDL_GetNumAudioDrivers(void)
  656. {
  657. return (SDL_arraysize(bootstrap) - 1);
  658. }
  659. const char *
  660. SDL_GetAudioDriver(int index)
  661. {
  662. if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
  663. return (bootstrap[index]->name);
  664. }
  665. return (NULL);
  666. }
  667. int
  668. SDL_AudioInit(const char *driver_name)
  669. {
  670. int i = 0;
  671. int initialized = 0;
  672. int tried_to_init = 0;
  673. if (SDL_WasInit(SDL_INIT_AUDIO)) {
  674. SDL_AudioQuit(); /* shutdown driver if already running. */
  675. }
  676. SDL_memset(&current_audio, '\0', sizeof(current_audio));
  677. SDL_memset(open_devices, '\0', sizeof(open_devices));
  678. /* Select the proper audio driver */
  679. if (driver_name == NULL) {
  680. driver_name = SDL_getenv("SDL_AUDIODRIVER");
  681. }
  682. for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
  683. /* make sure we should even try this driver before doing so... */
  684. const AudioBootStrap *backend = bootstrap[i];
  685. if ((driver_name && (SDL_strncasecmp(backend->name, driver_name, SDL_strlen(driver_name)) != 0)) ||
  686. (!driver_name && backend->demand_only)) {
  687. continue;
  688. }
  689. tried_to_init = 1;
  690. SDL_memset(&current_audio, 0, sizeof(current_audio));
  691. current_audio.name = backend->name;
  692. current_audio.desc = backend->desc;
  693. initialized = backend->init(&current_audio.impl);
  694. }
  695. if (!initialized) {
  696. /* specific drivers will set the error message if they fail... */
  697. if (!tried_to_init) {
  698. if (driver_name) {
  699. SDL_SetError("Audio target '%s' not available", driver_name);
  700. } else {
  701. SDL_SetError("No available audio device");
  702. }
  703. }
  704. SDL_memset(&current_audio, 0, sizeof(current_audio));
  705. return (-1); /* No driver was available, so fail. */
  706. }
  707. finalize_audio_entry_points();
  708. return (0);
  709. }
  710. /*
  711. * Get the current audio driver name
  712. */
  713. const char *
  714. SDL_GetCurrentAudioDriver()
  715. {
  716. return current_audio.name;
  717. }
  718. static void
  719. free_device_list(char ***devices, int *devCount)
  720. {
  721. int i = *devCount;
  722. if ((i > 0) && (*devices != NULL)) {
  723. while (i--) {
  724. SDL_free((*devices)[i]);
  725. }
  726. }
  727. SDL_free(*devices);
  728. *devices = NULL;
  729. *devCount = 0;
  730. }
  731. static
  732. void SDL_AddCaptureAudioDevice(const char *_name)
  733. {
  734. char *name = NULL;
  735. void *ptr = SDL_realloc(current_audio.inputDevices,
  736. (current_audio.inputDeviceCount+1) * sizeof(char*));
  737. if (ptr == NULL) {
  738. return; /* oh well. */
  739. }
  740. current_audio.inputDevices = (char **) ptr;
  741. name = SDL_strdup(_name); /* if this returns NULL, that's okay. */
  742. current_audio.inputDevices[current_audio.inputDeviceCount++] = name;
  743. }
  744. static
  745. void SDL_AddOutputAudioDevice(const char *_name)
  746. {
  747. char *name = NULL;
  748. void *ptr = SDL_realloc(current_audio.outputDevices,
  749. (current_audio.outputDeviceCount+1) * sizeof(char*));
  750. if (ptr == NULL) {
  751. return; /* oh well. */
  752. }
  753. current_audio.outputDevices = (char **) ptr;
  754. name = SDL_strdup(_name); /* if this returns NULL, that's okay. */
  755. current_audio.outputDevices[current_audio.outputDeviceCount++] = name;
  756. }
  757. int
  758. SDL_GetNumAudioDevices(int iscapture)
  759. {
  760. int retval = 0;
  761. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  762. return -1;
  763. }
  764. if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
  765. return 0;
  766. }
  767. if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
  768. return 1;
  769. }
  770. if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
  771. return 1;
  772. }
  773. if (iscapture) {
  774. free_device_list(&current_audio.inputDevices,
  775. &current_audio.inputDeviceCount);
  776. current_audio.impl.DetectDevices(iscapture, SDL_AddCaptureAudioDevice);
  777. retval = current_audio.inputDeviceCount;
  778. } else {
  779. free_device_list(&current_audio.outputDevices,
  780. &current_audio.outputDeviceCount);
  781. current_audio.impl.DetectDevices(iscapture, SDL_AddOutputAudioDevice);
  782. retval = current_audio.outputDeviceCount;
  783. }
  784. return retval;
  785. }
  786. const char *
  787. SDL_GetAudioDeviceName(int index, int iscapture)
  788. {
  789. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  790. SDL_SetError("Audio subsystem is not initialized");
  791. return NULL;
  792. }
  793. if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
  794. SDL_SetError("No capture support");
  795. return NULL;
  796. }
  797. if (index < 0) {
  798. goto no_such_device;
  799. }
  800. if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
  801. if (index > 0) {
  802. goto no_such_device;
  803. }
  804. return DEFAULT_INPUT_DEVNAME;
  805. }
  806. if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
  807. if (index > 0) {
  808. goto no_such_device;
  809. }
  810. return DEFAULT_OUTPUT_DEVNAME;
  811. }
  812. if (iscapture) {
  813. if (index >= current_audio.inputDeviceCount) {
  814. goto no_such_device;
  815. }
  816. return current_audio.inputDevices[index];
  817. } else {
  818. if (index >= current_audio.outputDeviceCount) {
  819. goto no_such_device;
  820. }
  821. return current_audio.outputDevices[index];
  822. }
  823. no_such_device:
  824. SDL_SetError("No such device");
  825. return NULL;
  826. }
  827. static void
  828. close_audio_device(SDL_AudioDevice * device)
  829. {
  830. device->enabled = 0;
  831. if (device->thread != NULL) {
  832. SDL_WaitThread(device->thread, NULL);
  833. }
  834. if (device->mixer_lock != NULL) {
  835. SDL_DestroyMutex(device->mixer_lock);
  836. }
  837. SDL_FreeAudioMem(device->fake_stream);
  838. if (device->convert.needed) {
  839. SDL_FreeAudioMem(device->convert.buf);
  840. }
  841. if (device->opened) {
  842. current_audio.impl.CloseDevice(device);
  843. device->opened = 0;
  844. }
  845. free_audio_queue(device->buffer_queue_head);
  846. free_audio_queue(device->buffer_queue_pool);
  847. SDL_FreeAudioMem(device);
  848. }
  849. /*
  850. * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
  851. * Fills in a sanitized copy in (prepared).
  852. * Returns non-zero if okay, zero on fatal parameters in (orig).
  853. */
  854. static int
  855. prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
  856. {
  857. SDL_memcpy(prepared, orig, sizeof(SDL_AudioSpec));
  858. if (orig->freq == 0) {
  859. const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
  860. if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
  861. prepared->freq = 22050; /* a reasonable default */
  862. }
  863. }
  864. if (orig->format == 0) {
  865. const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
  866. if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
  867. prepared->format = AUDIO_S16; /* a reasonable default */
  868. }
  869. }
  870. switch (orig->channels) {
  871. case 0:{
  872. const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
  873. if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
  874. prepared->channels = 2; /* a reasonable default */
  875. }
  876. break;
  877. }
  878. case 1: /* Mono */
  879. case 2: /* Stereo */
  880. case 4: /* surround */
  881. case 6: /* surround with center and lfe */
  882. break;
  883. default:
  884. SDL_SetError("Unsupported number of audio channels.");
  885. return 0;
  886. }
  887. if (orig->samples == 0) {
  888. const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
  889. if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
  890. /* Pick a default of ~46 ms at desired frequency */
  891. /* !!! FIXME: remove this when the non-Po2 resampling is in. */
  892. const int samples = (prepared->freq / 1000) * 46;
  893. int power2 = 1;
  894. while (power2 < samples) {
  895. power2 *= 2;
  896. }
  897. prepared->samples = power2;
  898. }
  899. }
  900. /* Calculate the silence and size of the audio specification */
  901. SDL_CalculateAudioSpec(prepared);
  902. return 1;
  903. }
  904. static SDL_AudioDeviceID
  905. open_audio_device(const char *devname, int iscapture,
  906. const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
  907. int allowed_changes, int min_id)
  908. {
  909. SDL_AudioDeviceID id = 0;
  910. SDL_AudioSpec _obtained;
  911. SDL_AudioDevice *device;
  912. SDL_bool build_cvt;
  913. int i = 0;
  914. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  915. SDL_SetError("Audio subsystem is not initialized");
  916. return 0;
  917. }
  918. if ((iscapture) && (!current_audio.impl.HasCaptureSupport)) {
  919. SDL_SetError("No capture support");
  920. return 0;
  921. }
  922. if (!obtained) {
  923. obtained = &_obtained;
  924. }
  925. if (!prepare_audiospec(desired, obtained)) {
  926. return 0;
  927. }
  928. /* If app doesn't care about a specific device, let the user override. */
  929. if (devname == NULL) {
  930. devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
  931. }
  932. /*
  933. * Catch device names at the high level for the simple case...
  934. * This lets us have a basic "device enumeration" for systems that
  935. * don't have multiple devices, but makes sure the device name is
  936. * always NULL when it hits the low level.
  937. *
  938. * Also make sure that the simple case prevents multiple simultaneous
  939. * opens of the default system device.
  940. */
  941. if ((iscapture) && (current_audio.impl.OnlyHasDefaultInputDevice)) {
  942. if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
  943. SDL_SetError("No such device");
  944. return 0;
  945. }
  946. devname = NULL;
  947. for (i = 0; i < SDL_arraysize(open_devices); i++) {
  948. if ((open_devices[i]) && (open_devices[i]->iscapture)) {
  949. SDL_SetError("Audio device already open");
  950. return 0;
  951. }
  952. }
  953. }
  954. if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
  955. if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
  956. SDL_SetError("No such device");
  957. return 0;
  958. }
  959. devname = NULL;
  960. for (i = 0; i < SDL_arraysize(open_devices); i++) {
  961. if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
  962. SDL_SetError("Audio device already open");
  963. return 0;
  964. }
  965. }
  966. }
  967. device = (SDL_AudioDevice *) SDL_AllocAudioMem(sizeof(SDL_AudioDevice));
  968. if (device == NULL) {
  969. SDL_OutOfMemory();
  970. return 0;
  971. }
  972. SDL_zerop(device);
  973. device->spec = *obtained;
  974. device->enabled = 1;
  975. device->paused = 1;
  976. device->iscapture = iscapture;
  977. /* Create a semaphore for locking the sound buffers */
  978. if (!current_audio.impl.SkipMixerLock) {
  979. device->mixer_lock = SDL_CreateMutex();
  980. if (device->mixer_lock == NULL) {
  981. close_audio_device(device);
  982. SDL_SetError("Couldn't create mixer lock");
  983. return 0;
  984. }
  985. }
  986. /* force a device detection if we haven't done one yet. */
  987. if ( ((iscapture) && (current_audio.inputDevices == NULL)) ||
  988. ((!iscapture) && (current_audio.outputDevices == NULL)) ) {
  989. SDL_GetNumAudioDevices(iscapture);
  990. }
  991. if (current_audio.impl.OpenDevice(device, devname, iscapture) < 0) {
  992. close_audio_device(device);
  993. return 0;
  994. }
  995. device->opened = 1;
  996. /* Allocate a fake audio memory buffer */
  997. device->fake_stream = (Uint8 *)SDL_AllocAudioMem(device->spec.size);
  998. if (device->fake_stream == NULL) {
  999. close_audio_device(device);
  1000. SDL_OutOfMemory();
  1001. return 0;
  1002. }
  1003. /* See if we need to do any conversion */
  1004. build_cvt = SDL_FALSE;
  1005. if (obtained->freq != device->spec.freq) {
  1006. if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
  1007. obtained->freq = device->spec.freq;
  1008. } else {
  1009. build_cvt = SDL_TRUE;
  1010. }
  1011. }
  1012. if (obtained->format != device->spec.format) {
  1013. if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
  1014. obtained->format = device->spec.format;
  1015. } else {
  1016. build_cvt = SDL_TRUE;
  1017. }
  1018. }
  1019. if (obtained->channels != device->spec.channels) {
  1020. if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
  1021. obtained->channels = device->spec.channels;
  1022. } else {
  1023. build_cvt = SDL_TRUE;
  1024. }
  1025. }
  1026. /* If the audio driver changes the buffer size, accept it.
  1027. This needs to be done after the format is modified above,
  1028. otherwise it might not have the correct buffer size.
  1029. */
  1030. if (device->spec.samples != obtained->samples) {
  1031. obtained->samples = device->spec.samples;
  1032. SDL_CalculateAudioSpec(obtained);
  1033. }
  1034. if (build_cvt) {
  1035. /* Build an audio conversion block */
  1036. if (SDL_BuildAudioCVT(&device->convert,
  1037. obtained->format, obtained->channels,
  1038. obtained->freq,
  1039. device->spec.format, device->spec.channels,
  1040. device->spec.freq) < 0) {
  1041. close_audio_device(device);
  1042. return 0;
  1043. }
  1044. if (device->convert.needed) {
  1045. device->convert.len = (int) (((double) device->spec.size) /
  1046. device->convert.len_ratio);
  1047. device->convert.buf =
  1048. (Uint8 *) SDL_AllocAudioMem(device->convert.len *
  1049. device->convert.len_mult);
  1050. if (device->convert.buf == NULL) {
  1051. close_audio_device(device);
  1052. SDL_OutOfMemory();
  1053. return 0;
  1054. }
  1055. }
  1056. }
  1057. if (device->spec.callback == NULL) { /* use buffer queueing? */
  1058. /* pool a few packets to start. Enough for two callbacks. */
  1059. const int packetlen = SDL_AUDIOBUFFERQUEUE_PACKETLEN;
  1060. const int wantbytes = ((device->convert.needed) ? device->convert.len : device->spec.size) * 2;
  1061. const int wantpackets = (wantbytes / packetlen) + ((wantbytes % packetlen) ? packetlen : 0);
  1062. for (i = 0; i < wantpackets; i++) {
  1063. SDL_AudioBufferQueue *packet = (SDL_AudioBufferQueue *) SDL_malloc(sizeof (SDL_AudioBufferQueue));
  1064. if (packet) { /* don't care if this fails, we'll deal later. */
  1065. packet->datalen = 0;
  1066. packet->startpos = 0;
  1067. packet->next = device->buffer_queue_pool;
  1068. device->buffer_queue_pool = packet;
  1069. }
  1070. }
  1071. device->spec.callback = SDL_BufferQueueDrainCallback;
  1072. device->spec.userdata = device;
  1073. }
  1074. /* Find an available device ID and store the structure... */
  1075. for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
  1076. if (open_devices[id] == NULL) {
  1077. open_devices[id] = device;
  1078. break;
  1079. }
  1080. }
  1081. if (id == SDL_arraysize(open_devices)) {
  1082. SDL_SetError("Too many open audio devices");
  1083. close_audio_device(device);
  1084. return 0;
  1085. }
  1086. /* Start the audio thread if necessary */
  1087. if (!current_audio.impl.ProvidesOwnCallbackThread) {
  1088. /* Start the audio thread */
  1089. char name[64];
  1090. SDL_snprintf(name, sizeof (name), "SDLAudioDev%d", (int) (id + 1));
  1091. /* !!! FIXME: this is nasty. */
  1092. #if defined(__WIN32__) && !defined(HAVE_LIBC)
  1093. #undef SDL_CreateThread
  1094. #if SDL_DYNAMIC_API
  1095. device->thread = SDL_CreateThread_REAL(SDL_RunAudio, name, device, NULL, NULL);
  1096. #else
  1097. device->thread = SDL_CreateThread(SDL_RunAudio, name, device, NULL, NULL);
  1098. #endif
  1099. #else
  1100. device->thread = SDL_CreateThread(SDL_RunAudio, name, device);
  1101. #endif
  1102. if (device->thread == NULL) {
  1103. SDL_CloseAudioDevice(id + 1);
  1104. SDL_SetError("Couldn't create audio thread");
  1105. return 0;
  1106. }
  1107. }
  1108. return id + 1;
  1109. }
  1110. int
  1111. SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
  1112. {
  1113. SDL_AudioDeviceID id = 0;
  1114. /* Start up the audio driver, if necessary. This is legacy behaviour! */
  1115. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  1116. if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  1117. return (-1);
  1118. }
  1119. }
  1120. /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
  1121. if (open_devices[0] != NULL) {
  1122. SDL_SetError("Audio device is already opened");
  1123. return (-1);
  1124. }
  1125. if (obtained) {
  1126. id = open_audio_device(NULL, 0, desired, obtained,
  1127. SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
  1128. } else {
  1129. id = open_audio_device(NULL, 0, desired, NULL, 0, 1);
  1130. }
  1131. SDL_assert((id == 0) || (id == 1));
  1132. return ((id == 0) ? -1 : 0);
  1133. }
  1134. SDL_AudioDeviceID
  1135. SDL_OpenAudioDevice(const char *device, int iscapture,
  1136. const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
  1137. int allowed_changes)
  1138. {
  1139. return open_audio_device(device, iscapture, desired, obtained,
  1140. allowed_changes, 2);
  1141. }
  1142. SDL_AudioStatus
  1143. SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
  1144. {
  1145. SDL_AudioDevice *device = get_audio_device(devid);
  1146. SDL_AudioStatus status = SDL_AUDIO_STOPPED;
  1147. if (device && device->enabled) {
  1148. if (device->paused) {
  1149. status = SDL_AUDIO_PAUSED;
  1150. } else {
  1151. status = SDL_AUDIO_PLAYING;
  1152. }
  1153. }
  1154. return (status);
  1155. }
  1156. SDL_AudioStatus
  1157. SDL_GetAudioStatus(void)
  1158. {
  1159. return SDL_GetAudioDeviceStatus(1);
  1160. }
  1161. void
  1162. SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
  1163. {
  1164. SDL_AudioDevice *device = get_audio_device(devid);
  1165. if (device) {
  1166. current_audio.impl.LockDevice(device);
  1167. device->paused = pause_on;
  1168. current_audio.impl.UnlockDevice(device);
  1169. }
  1170. }
  1171. void
  1172. SDL_PauseAudio(int pause_on)
  1173. {
  1174. SDL_PauseAudioDevice(1, pause_on);
  1175. }
  1176. void
  1177. SDL_LockAudioDevice(SDL_AudioDeviceID devid)
  1178. {
  1179. /* Obtain a lock on the mixing buffers */
  1180. SDL_AudioDevice *device = get_audio_device(devid);
  1181. if (device) {
  1182. current_audio.impl.LockDevice(device);
  1183. }
  1184. }
  1185. void
  1186. SDL_LockAudio(void)
  1187. {
  1188. SDL_LockAudioDevice(1);
  1189. }
  1190. void
  1191. SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
  1192. {
  1193. /* Obtain a lock on the mixing buffers */
  1194. SDL_AudioDevice *device = get_audio_device(devid);
  1195. if (device) {
  1196. current_audio.impl.UnlockDevice(device);
  1197. }
  1198. }
  1199. void
  1200. SDL_UnlockAudio(void)
  1201. {
  1202. SDL_UnlockAudioDevice(1);
  1203. }
  1204. void
  1205. SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
  1206. {
  1207. SDL_AudioDevice *device = get_audio_device(devid);
  1208. if (device) {
  1209. close_audio_device(device);
  1210. open_devices[devid - 1] = NULL;
  1211. }
  1212. }
  1213. void
  1214. SDL_CloseAudio(void)
  1215. {
  1216. SDL_CloseAudioDevice(1);
  1217. }
  1218. void
  1219. SDL_AudioQuit(void)
  1220. {
  1221. SDL_AudioDeviceID i;
  1222. if (!current_audio.name) { /* not initialized?! */
  1223. return;
  1224. }
  1225. for (i = 0; i < SDL_arraysize(open_devices); i++) {
  1226. if (open_devices[i] != NULL) {
  1227. SDL_CloseAudioDevice(i+1);
  1228. }
  1229. }
  1230. /* Free the driver data */
  1231. current_audio.impl.Deinitialize();
  1232. free_device_list(&current_audio.outputDevices,
  1233. &current_audio.outputDeviceCount);
  1234. free_device_list(&current_audio.inputDevices,
  1235. &current_audio.inputDeviceCount);
  1236. SDL_memset(&current_audio, '\0', sizeof(current_audio));
  1237. SDL_memset(open_devices, '\0', sizeof(open_devices));
  1238. }
  1239. #define NUM_FORMATS 10
  1240. static int format_idx;
  1241. static int format_idx_sub;
  1242. static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
  1243. {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
  1244. AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
  1245. {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
  1246. AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
  1247. {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
  1248. AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
  1249. {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
  1250. AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
  1251. {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
  1252. AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
  1253. {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
  1254. AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
  1255. {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
  1256. AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
  1257. {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
  1258. AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
  1259. {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
  1260. AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
  1261. {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
  1262. AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
  1263. };
  1264. SDL_AudioFormat
  1265. SDL_FirstAudioFormat(SDL_AudioFormat format)
  1266. {
  1267. for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
  1268. if (format_list[format_idx][0] == format) {
  1269. break;
  1270. }
  1271. }
  1272. format_idx_sub = 0;
  1273. return (SDL_NextAudioFormat());
  1274. }
  1275. SDL_AudioFormat
  1276. SDL_NextAudioFormat(void)
  1277. {
  1278. if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
  1279. return (0);
  1280. }
  1281. return (format_list[format_idx][format_idx_sub++]);
  1282. }
  1283. void
  1284. SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
  1285. {
  1286. switch (spec->format) {
  1287. case AUDIO_U8:
  1288. spec->silence = 0x80;
  1289. break;
  1290. default:
  1291. spec->silence = 0x00;
  1292. break;
  1293. }
  1294. spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
  1295. spec->size *= spec->channels;
  1296. spec->size *= spec->samples;
  1297. }
  1298. /*
  1299. * Moved here from SDL_mixer.c, since it relies on internals of an opened
  1300. * audio device (and is deprecated, by the way!).
  1301. */
  1302. void
  1303. SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
  1304. {
  1305. /* Mix the user-level audio format */
  1306. SDL_AudioDevice *device = get_audio_device(1);
  1307. if (device != NULL) {
  1308. SDL_AudioFormat format;
  1309. if (device->convert.needed) {
  1310. format = device->convert.src_format;
  1311. } else {
  1312. format = device->spec.format;
  1313. }
  1314. SDL_MixAudioFormat(dst, src, format, len, volume);
  1315. }
  1316. }
  1317. /* vi: set ts=4 sw=4 expandtab: */