SDL_audio.c 43 KB

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