SDL_audio.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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_audio_c.h"
  21. #include "SDL_sysaudio.h"
  22. #include "../thread/SDL_systhread.h"
  23. #include "../SDL_utils_c.h"
  24. #define _THIS SDL_AudioDevice *_this
  25. static SDL_AudioDriver current_audio;
  26. static SDL_AudioDevice *open_devices[16];
  27. /* Available audio drivers */
  28. static const AudioBootStrap *const bootstrap[] = {
  29. #if SDL_AUDIO_DRIVER_PULSEAUDIO
  30. &PULSEAUDIO_bootstrap,
  31. #endif
  32. #if SDL_AUDIO_DRIVER_ALSA
  33. &ALSA_bootstrap,
  34. #endif
  35. #if SDL_AUDIO_DRIVER_SNDIO
  36. &SNDIO_bootstrap,
  37. #endif
  38. #if SDL_AUDIO_DRIVER_NETBSD
  39. &NETBSDAUDIO_bootstrap,
  40. #endif
  41. #if SDL_AUDIO_DRIVER_WASAPI
  42. &WASAPI_bootstrap,
  43. #endif
  44. #if SDL_AUDIO_DRIVER_DSOUND
  45. &DSOUND_bootstrap,
  46. #endif
  47. #if SDL_AUDIO_DRIVER_HAIKU
  48. &HAIKUAUDIO_bootstrap,
  49. #endif
  50. #if SDL_AUDIO_DRIVER_COREAUDIO
  51. &COREAUDIO_bootstrap,
  52. #endif
  53. #if SDL_AUDIO_DRIVER_AAUDIO
  54. &aaudio_bootstrap,
  55. #endif
  56. #if SDL_AUDIO_DRIVER_OPENSLES
  57. &openslES_bootstrap,
  58. #endif
  59. #if SDL_AUDIO_DRIVER_ANDROID
  60. &ANDROIDAUDIO_bootstrap,
  61. #endif
  62. #if SDL_AUDIO_DRIVER_PS2
  63. &PS2AUDIO_bootstrap,
  64. #endif
  65. #if SDL_AUDIO_DRIVER_PSP
  66. &PSPAUDIO_bootstrap,
  67. #endif
  68. #if SDL_AUDIO_DRIVER_VITA
  69. &VITAAUD_bootstrap,
  70. #endif
  71. #if SDL_AUDIO_DRIVER_N3DS
  72. &N3DSAUDIO_bootstrap,
  73. #endif
  74. #if SDL_AUDIO_DRIVER_EMSCRIPTEN
  75. &EMSCRIPTENAUDIO_bootstrap,
  76. #endif
  77. #if SDL_AUDIO_DRIVER_JACK
  78. &JACK_bootstrap,
  79. #endif
  80. #if SDL_AUDIO_DRIVER_PIPEWIRE
  81. &PIPEWIRE_bootstrap,
  82. #endif
  83. #if SDL_AUDIO_DRIVER_OSS
  84. &DSP_bootstrap,
  85. #endif
  86. #if SDL_AUDIO_DRIVER_DISK
  87. &DISKAUDIO_bootstrap,
  88. #endif
  89. #if SDL_AUDIO_DRIVER_DUMMY
  90. &DUMMYAUDIO_bootstrap,
  91. #endif
  92. NULL
  93. };
  94. #ifdef HAVE_LIBSAMPLERATE_H
  95. #ifdef SDL_LIBSAMPLERATE_DYNAMIC
  96. static void *SRC_lib = NULL;
  97. #endif
  98. SDL_bool SRC_available = SDL_FALSE;
  99. int SRC_converter = 0;
  100. SRC_STATE* (*SRC_src_new)(int converter_type, int channels, int *error) = NULL;
  101. int (*SRC_src_process)(SRC_STATE *state, SRC_DATA *data) = NULL;
  102. int (*SRC_src_reset)(SRC_STATE *state) = NULL;
  103. SRC_STATE* (*SRC_src_delete)(SRC_STATE *state) = NULL;
  104. const char* (*SRC_src_strerror)(int error) = NULL;
  105. int (*SRC_src_simple)(SRC_DATA *data, int converter_type, int channels) = NULL;
  106. static SDL_bool
  107. LoadLibSampleRate(void)
  108. {
  109. const char *hint = SDL_GetHint(SDL_HINT_AUDIO_RESAMPLING_MODE);
  110. SRC_available = SDL_FALSE;
  111. SRC_converter = 0;
  112. if (!hint || *hint == '0' || SDL_strcasecmp(hint, "default") == 0) {
  113. return SDL_FALSE; /* don't load anything. */
  114. } else if (*hint == '1' || SDL_strcasecmp(hint, "fast") == 0) {
  115. SRC_converter = SRC_SINC_FASTEST;
  116. } else if (*hint == '2' || SDL_strcasecmp(hint, "medium") == 0) {
  117. SRC_converter = SRC_SINC_MEDIUM_QUALITY;
  118. } else if (*hint == '3' || SDL_strcasecmp(hint, "best") == 0) {
  119. SRC_converter = SRC_SINC_BEST_QUALITY;
  120. } else {
  121. return SDL_FALSE; /* treat it like "default", don't load anything. */
  122. }
  123. #ifdef SDL_LIBSAMPLERATE_DYNAMIC
  124. SDL_assert(SRC_lib == NULL);
  125. SRC_lib = SDL_LoadObject(SDL_LIBSAMPLERATE_DYNAMIC);
  126. if (!SRC_lib) {
  127. SDL_ClearError();
  128. return SDL_FALSE;
  129. }
  130. SRC_src_new = (SRC_STATE* (*)(int converter_type, int channels, int *error))SDL_LoadFunction(SRC_lib, "src_new");
  131. SRC_src_process = (int (*)(SRC_STATE *state, SRC_DATA *data))SDL_LoadFunction(SRC_lib, "src_process");
  132. SRC_src_reset = (int(*)(SRC_STATE *state))SDL_LoadFunction(SRC_lib, "src_reset");
  133. SRC_src_delete = (SRC_STATE* (*)(SRC_STATE *state))SDL_LoadFunction(SRC_lib, "src_delete");
  134. SRC_src_strerror = (const char* (*)(int error))SDL_LoadFunction(SRC_lib, "src_strerror");
  135. SRC_src_simple = (int(*)(SRC_DATA *data, int converter_type, int channels))SDL_LoadFunction(SRC_lib, "src_simple");
  136. if (!SRC_src_new || !SRC_src_process || !SRC_src_reset || !SRC_src_delete || !SRC_src_strerror || !SRC_src_simple) {
  137. SDL_UnloadObject(SRC_lib);
  138. SRC_lib = NULL;
  139. return SDL_FALSE;
  140. }
  141. #else
  142. SRC_src_new = src_new;
  143. SRC_src_process = src_process;
  144. SRC_src_reset = src_reset;
  145. SRC_src_delete = src_delete;
  146. SRC_src_strerror = src_strerror;
  147. SRC_src_simple = src_simple;
  148. #endif
  149. SRC_available = SDL_TRUE;
  150. return SDL_TRUE;
  151. }
  152. static void
  153. UnloadLibSampleRate(void)
  154. {
  155. #ifdef SDL_LIBSAMPLERATE_DYNAMIC
  156. if (SRC_lib != NULL) {
  157. SDL_UnloadObject(SRC_lib);
  158. }
  159. SRC_lib = NULL;
  160. #endif
  161. SRC_available = SDL_FALSE;
  162. SRC_src_new = NULL;
  163. SRC_src_process = NULL;
  164. SRC_src_reset = NULL;
  165. SRC_src_delete = NULL;
  166. SRC_src_strerror = NULL;
  167. }
  168. #endif
  169. static SDL_AudioDevice *
  170. get_audio_device(SDL_AudioDeviceID id)
  171. {
  172. id--;
  173. if ((id >= SDL_arraysize(open_devices)) || (open_devices[id] == NULL)) {
  174. SDL_SetError("Invalid audio device ID");
  175. return NULL;
  176. }
  177. return open_devices[id];
  178. }
  179. /* stubs for audio drivers that don't need a specific entry point... */
  180. static void
  181. SDL_AudioDetectDevices_Default(void)
  182. {
  183. /* you have to write your own implementation if these assertions fail. */
  184. SDL_assert(current_audio.impl.OnlyHasDefaultOutputDevice);
  185. SDL_assert(current_audio.impl.OnlyHasDefaultCaptureDevice || !current_audio.impl.HasCaptureSupport);
  186. SDL_AddAudioDevice(SDL_FALSE, DEFAULT_OUTPUT_DEVNAME, NULL, (void *) ((size_t) 0x1));
  187. if (current_audio.impl.HasCaptureSupport) {
  188. SDL_AddAudioDevice(SDL_TRUE, DEFAULT_INPUT_DEVNAME, NULL, (void *) ((size_t) 0x2));
  189. }
  190. }
  191. static void
  192. SDL_AudioThreadInit_Default(_THIS)
  193. { /* no-op. */
  194. }
  195. static void
  196. SDL_AudioThreadDeinit_Default(_THIS)
  197. { /* no-op. */
  198. }
  199. static void
  200. SDL_AudioWaitDevice_Default(_THIS)
  201. { /* no-op. */
  202. }
  203. static void
  204. SDL_AudioPlayDevice_Default(_THIS)
  205. { /* no-op. */
  206. }
  207. static Uint8 *
  208. SDL_AudioGetDeviceBuf_Default(_THIS)
  209. {
  210. return NULL;
  211. }
  212. static int
  213. SDL_AudioCaptureFromDevice_Default(_THIS, void *buffer, int buflen)
  214. {
  215. return -1; /* just fail immediately. */
  216. }
  217. static void
  218. SDL_AudioFlushCapture_Default(_THIS)
  219. { /* no-op. */
  220. }
  221. static void
  222. SDL_AudioCloseDevice_Default(_THIS)
  223. { /* no-op. */
  224. }
  225. static void
  226. SDL_AudioDeinitialize_Default(void)
  227. { /* no-op. */
  228. }
  229. static void
  230. SDL_AudioFreeDeviceHandle_Default(void *handle)
  231. { /* no-op. */
  232. }
  233. static int
  234. SDL_AudioOpenDevice_Default(_THIS, const char *devname)
  235. {
  236. return SDL_Unsupported();
  237. }
  238. static SDL_INLINE SDL_bool
  239. is_in_audio_device_thread(SDL_AudioDevice * device)
  240. {
  241. /* The device thread locks the same mutex, but not through the public API.
  242. This check is in case the application, in the audio callback,
  243. tries to lock the thread that we've already locked from the
  244. device thread...just in case we only have non-recursive mutexes. */
  245. if (device->thread && (SDL_ThreadID() == device->threadid)) {
  246. return SDL_TRUE;
  247. }
  248. return SDL_FALSE;
  249. }
  250. static void
  251. SDL_AudioLockDevice_Default(SDL_AudioDevice * device)
  252. {
  253. if (!is_in_audio_device_thread(device)) {
  254. SDL_LockMutex(device->mixer_lock);
  255. }
  256. }
  257. static void
  258. SDL_AudioUnlockDevice_Default(SDL_AudioDevice * device)
  259. {
  260. if (!is_in_audio_device_thread(device)) {
  261. SDL_UnlockMutex(device->mixer_lock);
  262. }
  263. }
  264. static void
  265. finish_audio_entry_points_init(void)
  266. {
  267. /*
  268. * Fill in stub functions for unused driver entry points. This lets us
  269. * blindly call them without having to check for validity first.
  270. */
  271. #define FILL_STUB(x) \
  272. if (current_audio.impl.x == NULL) { \
  273. current_audio.impl.x = SDL_Audio##x##_Default; \
  274. }
  275. FILL_STUB(DetectDevices);
  276. FILL_STUB(OpenDevice);
  277. FILL_STUB(ThreadInit);
  278. FILL_STUB(ThreadDeinit);
  279. FILL_STUB(WaitDevice);
  280. FILL_STUB(PlayDevice);
  281. FILL_STUB(GetDeviceBuf);
  282. FILL_STUB(CaptureFromDevice);
  283. FILL_STUB(FlushCapture);
  284. FILL_STUB(CloseDevice);
  285. FILL_STUB(LockDevice);
  286. FILL_STUB(UnlockDevice);
  287. FILL_STUB(FreeDeviceHandle);
  288. FILL_STUB(Deinitialize);
  289. #undef FILL_STUB
  290. }
  291. /* device hotplug support... */
  292. static int
  293. add_audio_device(const char *name, SDL_AudioSpec *spec, void *handle, SDL_AudioDeviceItem **devices, int *devCount)
  294. {
  295. int retval = -1;
  296. SDL_AudioDeviceItem *item;
  297. const SDL_AudioDeviceItem *i;
  298. int dupenum = 0;
  299. SDL_assert(handle != NULL); /* we reserve NULL, audio backends can't use it. */
  300. SDL_assert(name != NULL);
  301. item = (SDL_AudioDeviceItem *) SDL_malloc(sizeof (SDL_AudioDeviceItem));
  302. if (!item) {
  303. return SDL_OutOfMemory();
  304. }
  305. item->original_name = SDL_strdup(name);
  306. if (!item->original_name) {
  307. SDL_free(item);
  308. return SDL_OutOfMemory();
  309. }
  310. item->dupenum = 0;
  311. item->name = item->original_name;
  312. if (spec != NULL) {
  313. SDL_copyp(&item->spec, spec);
  314. } else {
  315. SDL_zero(item->spec);
  316. }
  317. item->handle = handle;
  318. SDL_LockMutex(current_audio.detectionLock);
  319. for (i = *devices; i != NULL; i = i->next) {
  320. if (SDL_strcmp(name, i->original_name) == 0) {
  321. dupenum = i->dupenum + 1;
  322. break; /* stop at the highest-numbered dupe. */
  323. }
  324. }
  325. if (dupenum) {
  326. const size_t len = SDL_strlen(name) + 16;
  327. char *replacement = (char *) SDL_malloc(len);
  328. if (!replacement) {
  329. SDL_UnlockMutex(current_audio.detectionLock);
  330. SDL_free(item->original_name);
  331. SDL_free(item);
  332. return SDL_OutOfMemory();
  333. }
  334. SDL_snprintf(replacement, len, "%s (%d)", name, dupenum + 1);
  335. item->dupenum = dupenum;
  336. item->name = replacement;
  337. }
  338. item->next = *devices;
  339. *devices = item;
  340. retval = (*devCount)++; /* !!! FIXME: this should be an atomic increment */
  341. SDL_UnlockMutex(current_audio.detectionLock);
  342. return retval;
  343. }
  344. static SDL_INLINE int
  345. add_capture_device(const char *name, SDL_AudioSpec *spec, void *handle)
  346. {
  347. SDL_assert(current_audio.impl.HasCaptureSupport);
  348. return add_audio_device(name, spec, handle, &current_audio.inputDevices, &current_audio.inputDeviceCount);
  349. }
  350. static SDL_INLINE int
  351. add_output_device(const char *name, SDL_AudioSpec *spec, void *handle)
  352. {
  353. return add_audio_device(name, spec, handle, &current_audio.outputDevices, &current_audio.outputDeviceCount);
  354. }
  355. static void
  356. free_device_list(SDL_AudioDeviceItem **devices, int *devCount)
  357. {
  358. SDL_AudioDeviceItem *item, *next;
  359. for (item = *devices; item != NULL; item = next) {
  360. next = item->next;
  361. if (item->handle != NULL) {
  362. current_audio.impl.FreeDeviceHandle(item->handle);
  363. }
  364. /* these two pointers are the same if not a duplicate devname */
  365. if (item->name != item->original_name) {
  366. SDL_free(item->name);
  367. }
  368. SDL_free(item->original_name);
  369. SDL_free(item);
  370. }
  371. *devices = NULL;
  372. *devCount = 0;
  373. }
  374. /* The audio backends call this when a new device is plugged in. */
  375. void
  376. SDL_AddAudioDevice(const SDL_bool iscapture, const char *name, SDL_AudioSpec *spec, void *handle)
  377. {
  378. const int device_index = iscapture ? add_capture_device(name, spec, handle) : add_output_device(name, spec, handle);
  379. if (device_index != -1) {
  380. /* Post the event, if desired */
  381. if (SDL_GetEventState(SDL_AUDIODEVICEADDED) == SDL_ENABLE) {
  382. SDL_Event event;
  383. SDL_zero(event);
  384. event.adevice.type = SDL_AUDIODEVICEADDED;
  385. event.adevice.which = device_index;
  386. event.adevice.iscapture = iscapture;
  387. SDL_PushEvent(&event);
  388. }
  389. }
  390. }
  391. /* The audio backends call this when a currently-opened device is lost. */
  392. void SDL_OpenedAudioDeviceDisconnected(SDL_AudioDevice *device)
  393. {
  394. SDL_assert(get_audio_device(device->id) == device);
  395. if (!SDL_AtomicGet(&device->enabled)) {
  396. return; /* don't report disconnects more than once. */
  397. }
  398. if (SDL_AtomicGet(&device->shutdown)) {
  399. return; /* don't report disconnect if we're trying to close device. */
  400. }
  401. /* Ends the audio callback and mark the device as STOPPED, but the
  402. app still needs to close the device to free resources. */
  403. current_audio.impl.LockDevice(device);
  404. SDL_AtomicSet(&device->enabled, 0);
  405. current_audio.impl.UnlockDevice(device);
  406. /* Post the event, if desired */
  407. if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
  408. SDL_Event event;
  409. SDL_zero(event);
  410. event.adevice.type = SDL_AUDIODEVICEREMOVED;
  411. event.adevice.which = device->id;
  412. event.adevice.iscapture = device->iscapture ? 1 : 0;
  413. SDL_PushEvent(&event);
  414. }
  415. }
  416. static void
  417. mark_device_removed(void *handle, SDL_AudioDeviceItem *devices, SDL_bool *removedFlag)
  418. {
  419. SDL_AudioDeviceItem *item;
  420. SDL_assert(handle != NULL);
  421. for (item = devices; item != NULL; item = item->next) {
  422. if (item->handle == handle) {
  423. item->handle = NULL;
  424. *removedFlag = SDL_TRUE;
  425. return;
  426. }
  427. }
  428. }
  429. /* The audio backends call this when a device is removed from the system. */
  430. void
  431. SDL_RemoveAudioDevice(const SDL_bool iscapture, void *handle)
  432. {
  433. int device_index;
  434. SDL_AudioDevice *device = NULL;
  435. SDL_bool device_was_opened = SDL_FALSE;
  436. SDL_LockMutex(current_audio.detectionLock);
  437. if (iscapture) {
  438. mark_device_removed(handle, current_audio.inputDevices, &current_audio.captureDevicesRemoved);
  439. } else {
  440. mark_device_removed(handle, current_audio.outputDevices, &current_audio.outputDevicesRemoved);
  441. }
  442. for (device_index = 0; device_index < SDL_arraysize(open_devices); device_index++) {
  443. device = open_devices[device_index];
  444. if (device != NULL && device->handle == handle) {
  445. device_was_opened = SDL_TRUE;
  446. SDL_OpenedAudioDeviceDisconnected(device);
  447. break;
  448. }
  449. }
  450. /* Devices that aren't opened, as of 2.24.0, will post an
  451. SDL_AUDIODEVICEREMOVED event with the `which` field set to zero.
  452. Apps can use this to decide if they need to refresh a list of
  453. available devices instead of closing an opened one.
  454. Note that opened devices will send the non-zero event in
  455. SDL_OpenedAudioDeviceDisconnected(). */
  456. if (!device_was_opened) {
  457. if (SDL_GetEventState(SDL_AUDIODEVICEREMOVED) == SDL_ENABLE) {
  458. SDL_Event event;
  459. SDL_zero(event);
  460. event.adevice.type = SDL_AUDIODEVICEREMOVED;
  461. event.adevice.which = 0;
  462. event.adevice.iscapture = iscapture ? 1 : 0;
  463. SDL_PushEvent(&event);
  464. }
  465. }
  466. SDL_UnlockMutex(current_audio.detectionLock);
  467. current_audio.impl.FreeDeviceHandle(handle);
  468. }
  469. /* buffer queueing support... */
  470. static void SDLCALL
  471. SDL_BufferQueueDrainCallback(void *userdata, Uint8 *stream, int len)
  472. {
  473. /* this function always holds the mixer lock before being called. */
  474. SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
  475. size_t dequeued;
  476. SDL_assert(device != NULL); /* this shouldn't ever happen, right?! */
  477. SDL_assert(!device->iscapture); /* this shouldn't ever happen, right?! */
  478. SDL_assert(len >= 0); /* this shouldn't ever happen, right?! */
  479. dequeued = SDL_ReadFromDataQueue(device->buffer_queue, stream, len);
  480. stream += dequeued;
  481. len -= (int) dequeued;
  482. if (len > 0) { /* fill any remaining space in the stream with silence. */
  483. SDL_assert(SDL_CountDataQueue(device->buffer_queue) == 0);
  484. SDL_memset(stream, device->callbackspec.silence, len);
  485. }
  486. }
  487. static void SDLCALL
  488. SDL_BufferQueueFillCallback(void *userdata, Uint8 *stream, int len)
  489. {
  490. /* this function always holds the mixer lock before being called. */
  491. SDL_AudioDevice *device = (SDL_AudioDevice *) userdata;
  492. SDL_assert(device != NULL); /* this shouldn't ever happen, right?! */
  493. SDL_assert(device->iscapture); /* this shouldn't ever happen, right?! */
  494. SDL_assert(len >= 0); /* this shouldn't ever happen, right?! */
  495. /* note that if this needs to allocate more space and run out of memory,
  496. we have no choice but to quietly drop the data and hope it works out
  497. later, but you probably have bigger problems in this case anyhow. */
  498. SDL_WriteToDataQueue(device->buffer_queue, stream, len);
  499. }
  500. int
  501. SDL_QueueAudio(SDL_AudioDeviceID devid, const void *data, Uint32 len)
  502. {
  503. SDL_AudioDevice *device = get_audio_device(devid);
  504. int rc = 0;
  505. if (!device) {
  506. return -1; /* get_audio_device() will have set the error state */
  507. } else if (device->iscapture) {
  508. return SDL_SetError("This is a capture device, queueing not allowed");
  509. } else if (device->callbackspec.callback != SDL_BufferQueueDrainCallback) {
  510. return SDL_SetError("Audio device has a callback, queueing not allowed");
  511. }
  512. if (len > 0) {
  513. current_audio.impl.LockDevice(device);
  514. rc = SDL_WriteToDataQueue(device->buffer_queue, data, len);
  515. current_audio.impl.UnlockDevice(device);
  516. }
  517. return rc;
  518. }
  519. Uint32
  520. SDL_DequeueAudio(SDL_AudioDeviceID devid, void *data, Uint32 len)
  521. {
  522. SDL_AudioDevice *device = get_audio_device(devid);
  523. Uint32 rc;
  524. if ( (len == 0) || /* nothing to do? */
  525. (!device) || /* called with bogus device id */
  526. (!device->iscapture) || /* playback devices can't dequeue */
  527. (device->callbackspec.callback != SDL_BufferQueueFillCallback) ) { /* not set for queueing */
  528. return 0; /* just report zero bytes dequeued. */
  529. }
  530. current_audio.impl.LockDevice(device);
  531. rc = (Uint32) SDL_ReadFromDataQueue(device->buffer_queue, data, len);
  532. current_audio.impl.UnlockDevice(device);
  533. return rc;
  534. }
  535. Uint32
  536. SDL_GetQueuedAudioSize(SDL_AudioDeviceID devid)
  537. {
  538. Uint32 retval = 0;
  539. SDL_AudioDevice *device = get_audio_device(devid);
  540. if (!device) {
  541. return 0;
  542. }
  543. /* Nothing to do unless we're set up for queueing. */
  544. if (device->callbackspec.callback == SDL_BufferQueueDrainCallback ||
  545. device->callbackspec.callback == SDL_BufferQueueFillCallback)
  546. {
  547. current_audio.impl.LockDevice(device);
  548. retval = (Uint32) SDL_CountDataQueue(device->buffer_queue);
  549. current_audio.impl.UnlockDevice(device);
  550. }
  551. return retval;
  552. }
  553. void
  554. SDL_ClearQueuedAudio(SDL_AudioDeviceID devid)
  555. {
  556. SDL_AudioDevice *device = get_audio_device(devid);
  557. if (!device) {
  558. return; /* nothing to do. */
  559. }
  560. /* Blank out the device and release the mutex. Free it afterwards. */
  561. current_audio.impl.LockDevice(device);
  562. /* Keep up to two packets in the pool to reduce future memory allocation pressure. */
  563. SDL_ClearDataQueue(device->buffer_queue, SDL_AUDIOBUFFERQUEUE_PACKETLEN * 2);
  564. current_audio.impl.UnlockDevice(device);
  565. }
  566. /* The general mixing thread function */
  567. static int SDLCALL
  568. SDL_RunAudio(void *devicep)
  569. {
  570. SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
  571. void *udata = device->callbackspec.userdata;
  572. SDL_AudioCallback callback = device->callbackspec.callback;
  573. int data_len = 0;
  574. Uint8 *data;
  575. SDL_assert(!device->iscapture);
  576. #if SDL_AUDIO_DRIVER_ANDROID
  577. {
  578. /* Set thread priority to THREAD_PRIORITY_AUDIO */
  579. extern void Android_JNI_AudioSetThreadPriority(int, int);
  580. Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
  581. }
  582. #else
  583. /* The audio mixing is always a high priority thread */
  584. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_TIME_CRITICAL);
  585. #endif
  586. /* Perform any thread setup */
  587. device->threadid = SDL_ThreadID();
  588. current_audio.impl.ThreadInit(device);
  589. /* Loop, filling the audio buffers */
  590. while (!SDL_AtomicGet(&device->shutdown)) {
  591. data_len = device->callbackspec.size;
  592. /* Fill the current buffer with sound */
  593. if (!device->stream && SDL_AtomicGet(&device->enabled)) {
  594. SDL_assert(data_len == device->spec.size);
  595. data = current_audio.impl.GetDeviceBuf(device);
  596. } else {
  597. /* if the device isn't enabled, we still write to the
  598. work_buffer, so the app's callback will fire with
  599. a regular frequency, in case they depend on that
  600. for timing or progress. They can use hotplug
  601. now to know if the device failed.
  602. Streaming playback uses work_buffer, too. */
  603. data = NULL;
  604. }
  605. if (data == NULL) {
  606. data = device->work_buffer;
  607. }
  608. /* !!! FIXME: this should be LockDevice. */
  609. SDL_LockMutex(device->mixer_lock);
  610. if (SDL_AtomicGet(&device->paused)) {
  611. SDL_memset(data, device->callbackspec.silence, data_len);
  612. } else {
  613. callback(udata, data, data_len);
  614. }
  615. SDL_UnlockMutex(device->mixer_lock);
  616. if (device->stream) {
  617. /* Stream available audio to device, converting/resampling. */
  618. /* if this fails...oh well. We'll play silence here. */
  619. SDL_AudioStreamPut(device->stream, data, data_len);
  620. while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->spec.size)) {
  621. int got;
  622. data = SDL_AtomicGet(&device->enabled) ? current_audio.impl.GetDeviceBuf(device) : NULL;
  623. got = SDL_AudioStreamGet(device->stream, data ? data : device->work_buffer, device->spec.size);
  624. SDL_assert((got <= 0) || (got == device->spec.size));
  625. if (data == NULL) { /* device is having issues... */
  626. const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
  627. SDL_Delay(delay); /* wait for as long as this buffer would have played. Maybe device recovers later? */
  628. } else {
  629. if (got != device->spec.size) {
  630. SDL_memset(data, device->spec.silence, device->spec.size);
  631. }
  632. current_audio.impl.PlayDevice(device);
  633. current_audio.impl.WaitDevice(device);
  634. }
  635. }
  636. } else if (data == device->work_buffer) {
  637. /* nothing to do; pause like we queued a buffer to play. */
  638. const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
  639. SDL_Delay(delay);
  640. } else { /* writing directly to the device. */
  641. /* queue this buffer and wait for it to finish playing. */
  642. current_audio.impl.PlayDevice(device);
  643. current_audio.impl.WaitDevice(device);
  644. }
  645. }
  646. /* Wait for the audio to drain. */
  647. SDL_Delay(((device->spec.samples * 1000) / device->spec.freq) * 2);
  648. current_audio.impl.ThreadDeinit(device);
  649. return 0;
  650. }
  651. /* !!! FIXME: this needs to deal with device spec changes. */
  652. /* The general capture thread function */
  653. static int SDLCALL
  654. SDL_CaptureAudio(void *devicep)
  655. {
  656. SDL_AudioDevice *device = (SDL_AudioDevice *) devicep;
  657. const int silence = (int) device->spec.silence;
  658. const Uint32 delay = ((device->spec.samples * 1000) / device->spec.freq);
  659. const int data_len = device->spec.size;
  660. Uint8 *data;
  661. void *udata = device->callbackspec.userdata;
  662. SDL_AudioCallback callback = device->callbackspec.callback;
  663. SDL_assert(device->iscapture);
  664. #if SDL_AUDIO_DRIVER_ANDROID
  665. {
  666. /* Set thread priority to THREAD_PRIORITY_AUDIO */
  667. extern void Android_JNI_AudioSetThreadPriority(int, int);
  668. Android_JNI_AudioSetThreadPriority(device->iscapture, device->id);
  669. }
  670. #else
  671. /* The audio mixing is always a high priority thread */
  672. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
  673. #endif
  674. /* Perform any thread setup */
  675. device->threadid = SDL_ThreadID();
  676. current_audio.impl.ThreadInit(device);
  677. /* Loop, filling the audio buffers */
  678. while (!SDL_AtomicGet(&device->shutdown)) {
  679. int still_need;
  680. Uint8 *ptr;
  681. if (SDL_AtomicGet(&device->paused)) {
  682. SDL_Delay(delay); /* just so we don't cook the CPU. */
  683. if (device->stream) {
  684. SDL_AudioStreamClear(device->stream);
  685. }
  686. current_audio.impl.FlushCapture(device); /* dump anything pending. */
  687. continue;
  688. }
  689. /* Fill the current buffer with sound */
  690. still_need = data_len;
  691. /* Use the work_buffer to hold data read from the device. */
  692. data = device->work_buffer;
  693. SDL_assert(data != NULL);
  694. ptr = data;
  695. /* We still read from the device when "paused" to keep the state sane,
  696. and block when there isn't data so this thread isn't eating CPU.
  697. But we don't process it further or call the app's callback. */
  698. if (!SDL_AtomicGet(&device->enabled)) {
  699. SDL_Delay(delay); /* try to keep callback firing at normal pace. */
  700. } else {
  701. while (still_need > 0) {
  702. const int rc = current_audio.impl.CaptureFromDevice(device, ptr, still_need);
  703. SDL_assert(rc <= still_need); /* device should not overflow buffer. :) */
  704. if (rc > 0) {
  705. still_need -= rc;
  706. ptr += rc;
  707. } else { /* uhoh, device failed for some reason! */
  708. SDL_OpenedAudioDeviceDisconnected(device);
  709. break;
  710. }
  711. }
  712. }
  713. if (still_need > 0) {
  714. /* Keep any data we already read, silence the rest. */
  715. SDL_memset(ptr, silence, still_need);
  716. }
  717. if (device->stream) {
  718. /* if this fails...oh well. */
  719. SDL_AudioStreamPut(device->stream, data, data_len);
  720. while (SDL_AudioStreamAvailable(device->stream) >= ((int) device->callbackspec.size)) {
  721. const int got = SDL_AudioStreamGet(device->stream, device->work_buffer, device->callbackspec.size);
  722. SDL_assert((got < 0) || (got == device->callbackspec.size));
  723. if (got != device->callbackspec.size) {
  724. SDL_memset(device->work_buffer, device->spec.silence, device->callbackspec.size);
  725. }
  726. /* !!! FIXME: this should be LockDevice. */
  727. SDL_LockMutex(device->mixer_lock);
  728. if (!SDL_AtomicGet(&device->paused)) {
  729. callback(udata, device->work_buffer, device->callbackspec.size);
  730. }
  731. SDL_UnlockMutex(device->mixer_lock);
  732. }
  733. } else { /* feeding user callback directly without streaming. */
  734. /* !!! FIXME: this should be LockDevice. */
  735. SDL_LockMutex(device->mixer_lock);
  736. if (!SDL_AtomicGet(&device->paused)) {
  737. callback(udata, data, device->callbackspec.size);
  738. }
  739. SDL_UnlockMutex(device->mixer_lock);
  740. }
  741. }
  742. current_audio.impl.FlushCapture(device);
  743. current_audio.impl.ThreadDeinit(device);
  744. return 0;
  745. }
  746. static SDL_AudioFormat
  747. SDL_ParseAudioFormat(const char *string)
  748. {
  749. #define CHECK_FMT_STRING(x) if (SDL_strcmp(string, #x) == 0) return AUDIO_##x
  750. CHECK_FMT_STRING(U8);
  751. CHECK_FMT_STRING(S8);
  752. CHECK_FMT_STRING(U16LSB);
  753. CHECK_FMT_STRING(S16LSB);
  754. CHECK_FMT_STRING(U16MSB);
  755. CHECK_FMT_STRING(S16MSB);
  756. CHECK_FMT_STRING(U16SYS);
  757. CHECK_FMT_STRING(S16SYS);
  758. CHECK_FMT_STRING(U16);
  759. CHECK_FMT_STRING(S16);
  760. CHECK_FMT_STRING(S32LSB);
  761. CHECK_FMT_STRING(S32MSB);
  762. CHECK_FMT_STRING(S32SYS);
  763. CHECK_FMT_STRING(S32);
  764. CHECK_FMT_STRING(F32LSB);
  765. CHECK_FMT_STRING(F32MSB);
  766. CHECK_FMT_STRING(F32SYS);
  767. CHECK_FMT_STRING(F32);
  768. #undef CHECK_FMT_STRING
  769. return 0;
  770. }
  771. int
  772. SDL_GetNumAudioDrivers(void)
  773. {
  774. return SDL_arraysize(bootstrap) - 1;
  775. }
  776. const char *
  777. SDL_GetAudioDriver(int index)
  778. {
  779. if (index >= 0 && index < SDL_GetNumAudioDrivers()) {
  780. return bootstrap[index]->name;
  781. }
  782. return NULL;
  783. }
  784. int
  785. SDL_AudioInit(const char *driver_name)
  786. {
  787. int i;
  788. SDL_bool initialized = SDL_FALSE, tried_to_init = SDL_FALSE;
  789. if (SDL_GetCurrentAudioDriver()) {
  790. SDL_AudioQuit(); /* shutdown driver if already running. */
  791. }
  792. SDL_zeroa(open_devices);
  793. /* Select the proper audio driver */
  794. if (driver_name == NULL) {
  795. driver_name = SDL_GetHint(SDL_HINT_AUDIODRIVER);
  796. }
  797. if (driver_name != NULL && *driver_name != 0) {
  798. const char *driver_attempt = driver_name;
  799. while (driver_attempt != NULL && *driver_attempt != 0 && !initialized) {
  800. const char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  801. size_t driver_attempt_len = (driver_attempt_end != NULL) ? (driver_attempt_end - driver_attempt)
  802. : SDL_strlen(driver_attempt);
  803. #if SDL_AUDIO_DRIVER_DSOUND
  804. /* SDL 1.2 uses the name "dsound", so we'll support both. */
  805. if (driver_attempt_len == SDL_strlen("dsound") &&
  806. (SDL_strncasecmp(driver_attempt, "dsound", driver_attempt_len) == 0)) {
  807. driver_attempt = "directsound";
  808. driver_attempt_len = SDL_strlen("directsound");
  809. }
  810. #endif
  811. #if SDL_AUDIO_DRIVER_PULSEAUDIO
  812. /* SDL 1.2 uses the name "pulse", so we'll support both. */
  813. if (driver_attempt_len == SDL_strlen("pulse") &&
  814. (SDL_strncasecmp(driver_attempt, "pulse", driver_attempt_len) == 0)) {
  815. driver_attempt = "pulseaudio";
  816. driver_attempt_len = SDL_strlen("pulseaudio");
  817. }
  818. #endif
  819. for (i = 0; bootstrap[i]; ++i) {
  820. if ((driver_attempt_len == SDL_strlen(bootstrap[i]->name)) &&
  821. (SDL_strncasecmp(bootstrap[i]->name, driver_attempt, driver_attempt_len) == 0)) {
  822. tried_to_init = SDL_TRUE;
  823. SDL_zero(current_audio);
  824. current_audio.name = bootstrap[i]->name;
  825. current_audio.desc = bootstrap[i]->desc;
  826. initialized = bootstrap[i]->init(&current_audio.impl);
  827. break;
  828. }
  829. }
  830. driver_attempt = (driver_attempt_end != NULL) ? (driver_attempt_end + 1) : NULL;
  831. }
  832. } else {
  833. for (i = 0; (!initialized) && (bootstrap[i]); ++i) {
  834. if (bootstrap[i]->demand_only) {
  835. continue;
  836. }
  837. tried_to_init = SDL_TRUE;
  838. SDL_zero(current_audio);
  839. current_audio.name = bootstrap[i]->name;
  840. current_audio.desc = bootstrap[i]->desc;
  841. initialized = bootstrap[i]->init(&current_audio.impl);
  842. }
  843. }
  844. if (!initialized) {
  845. /* specific drivers will set the error message if they fail... */
  846. if (!tried_to_init) {
  847. if (driver_name) {
  848. SDL_SetError("Audio target '%s' not available", driver_name);
  849. } else {
  850. SDL_SetError("No available audio device");
  851. }
  852. }
  853. SDL_zero(current_audio);
  854. return -1; /* No driver was available, so fail. */
  855. }
  856. current_audio.detectionLock = SDL_CreateMutex();
  857. finish_audio_entry_points_init();
  858. /* Make sure we have a list of devices available at startup. */
  859. current_audio.impl.DetectDevices();
  860. #ifdef HAVE_LIBSAMPLERATE_H
  861. LoadLibSampleRate();
  862. #endif
  863. return 0;
  864. }
  865. /*
  866. * Get the current audio driver name
  867. */
  868. const char *
  869. SDL_GetCurrentAudioDriver()
  870. {
  871. return current_audio.name;
  872. }
  873. /* Clean out devices that we've removed but had to keep around for stability. */
  874. static void
  875. clean_out_device_list(SDL_AudioDeviceItem **devices, int *devCount, SDL_bool *removedFlag)
  876. {
  877. SDL_AudioDeviceItem *item = *devices;
  878. SDL_AudioDeviceItem *prev = NULL;
  879. int total = 0;
  880. while (item) {
  881. SDL_AudioDeviceItem *next = item->next;
  882. if (item->handle != NULL) {
  883. total++;
  884. prev = item;
  885. } else {
  886. if (prev) {
  887. prev->next = next;
  888. } else {
  889. *devices = next;
  890. }
  891. /* these two pointers are the same if not a duplicate devname */
  892. if (item->name != item->original_name) {
  893. SDL_free(item->name);
  894. }
  895. SDL_free(item->original_name);
  896. SDL_free(item);
  897. }
  898. item = next;
  899. }
  900. *devCount = total;
  901. *removedFlag = SDL_FALSE;
  902. }
  903. int
  904. SDL_GetNumAudioDevices(int iscapture)
  905. {
  906. int retval = 0;
  907. if (!SDL_GetCurrentAudioDriver()) {
  908. return -1;
  909. }
  910. SDL_LockMutex(current_audio.detectionLock);
  911. if (iscapture && current_audio.captureDevicesRemoved) {
  912. clean_out_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount, &current_audio.captureDevicesRemoved);
  913. }
  914. if (!iscapture && current_audio.outputDevicesRemoved) {
  915. clean_out_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount, &current_audio.outputDevicesRemoved);
  916. }
  917. retval = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
  918. SDL_UnlockMutex(current_audio.detectionLock);
  919. return retval;
  920. }
  921. const char *
  922. SDL_GetAudioDeviceName(int index, int iscapture)
  923. {
  924. SDL_AudioDeviceItem *item;
  925. int i;
  926. const char *retval;
  927. if (!SDL_GetCurrentAudioDriver()) {
  928. SDL_SetError("Audio subsystem is not initialized");
  929. return NULL;
  930. }
  931. SDL_LockMutex(current_audio.detectionLock);
  932. item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
  933. i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
  934. if (index >= 0 && index < i) {
  935. for (i--; i > index; i--, item = item->next) {
  936. SDL_assert(item != NULL);
  937. }
  938. SDL_assert(item != NULL);
  939. retval = item->name;
  940. } else {
  941. SDL_InvalidParamError("index");
  942. retval = NULL;
  943. }
  944. SDL_UnlockMutex(current_audio.detectionLock);
  945. return retval;
  946. }
  947. int
  948. SDL_GetAudioDeviceSpec(int index, int iscapture, SDL_AudioSpec *spec)
  949. {
  950. SDL_AudioDeviceItem *item;
  951. int i, retval;
  952. if (spec == NULL) {
  953. return SDL_InvalidParamError("spec");
  954. }
  955. if (!SDL_GetCurrentAudioDriver()) {
  956. return SDL_SetError("Audio subsystem is not initialized");
  957. }
  958. SDL_LockMutex(current_audio.detectionLock);
  959. item = iscapture ? current_audio.inputDevices : current_audio.outputDevices;
  960. i = iscapture ? current_audio.inputDeviceCount : current_audio.outputDeviceCount;
  961. if (index >= 0 && index < i) {
  962. for (i--; i > index; i--, item = item->next) {
  963. SDL_assert(item != NULL);
  964. }
  965. SDL_assert(item != NULL);
  966. SDL_copyp(spec, &item->spec);
  967. retval = 0;
  968. } else {
  969. retval = SDL_InvalidParamError("index");
  970. }
  971. SDL_UnlockMutex(current_audio.detectionLock);
  972. return retval;
  973. }
  974. int
  975. SDL_GetDefaultAudioInfo(char **name, SDL_AudioSpec *spec, int iscapture)
  976. {
  977. if (spec == NULL) {
  978. return SDL_InvalidParamError("spec");
  979. }
  980. if (!SDL_GetCurrentAudioDriver()) {
  981. return SDL_SetError("Audio subsystem is not initialized");
  982. }
  983. if (current_audio.impl.GetDefaultAudioInfo == NULL) {
  984. return SDL_Unsupported();
  985. }
  986. return current_audio.impl.GetDefaultAudioInfo(name, spec, iscapture);
  987. }
  988. static void
  989. close_audio_device(SDL_AudioDevice * device)
  990. {
  991. if (!device) {
  992. return;
  993. }
  994. /* make sure the device is paused before we do anything else, so the
  995. audio callback definitely won't fire again. */
  996. current_audio.impl.LockDevice(device);
  997. SDL_AtomicSet(&device->paused, 1);
  998. SDL_AtomicSet(&device->shutdown, 1);
  999. SDL_AtomicSet(&device->enabled, 0);
  1000. current_audio.impl.UnlockDevice(device);
  1001. if (device->thread != NULL) {
  1002. SDL_WaitThread(device->thread, NULL);
  1003. }
  1004. if (device->mixer_lock != NULL) {
  1005. SDL_DestroyMutex(device->mixer_lock);
  1006. }
  1007. SDL_free(device->work_buffer);
  1008. SDL_FreeAudioStream(device->stream);
  1009. if (device->id > 0) {
  1010. SDL_AudioDevice *opendev = open_devices[device->id - 1];
  1011. SDL_assert((opendev == device) || (opendev == NULL));
  1012. if (opendev == device) {
  1013. open_devices[device->id - 1] = NULL;
  1014. }
  1015. }
  1016. if (device->hidden != NULL) {
  1017. current_audio.impl.CloseDevice(device);
  1018. }
  1019. SDL_FreeDataQueue(device->buffer_queue);
  1020. SDL_free(device);
  1021. }
  1022. /*
  1023. * Sanity check desired AudioSpec for SDL_OpenAudio() in (orig).
  1024. * Fills in a sanitized copy in (prepared).
  1025. * Returns non-zero if okay, zero on fatal parameters in (orig).
  1026. */
  1027. static int
  1028. prepare_audiospec(const SDL_AudioSpec * orig, SDL_AudioSpec * prepared)
  1029. {
  1030. SDL_copyp(prepared, orig);
  1031. if (orig->freq == 0) {
  1032. const char *env = SDL_getenv("SDL_AUDIO_FREQUENCY");
  1033. if ((!env) || ((prepared->freq = SDL_atoi(env)) == 0)) {
  1034. prepared->freq = 22050; /* a reasonable default */
  1035. }
  1036. }
  1037. if (orig->format == 0) {
  1038. const char *env = SDL_getenv("SDL_AUDIO_FORMAT");
  1039. if ((!env) || ((prepared->format = SDL_ParseAudioFormat(env)) == 0)) {
  1040. prepared->format = AUDIO_S16; /* a reasonable default */
  1041. }
  1042. }
  1043. if (orig->channels == 0) {
  1044. const char *env = SDL_getenv("SDL_AUDIO_CHANNELS");
  1045. if ((!env) || ((prepared->channels = (Uint8) SDL_atoi(env)) == 0)) {
  1046. prepared->channels = 2; /* a reasonable default */
  1047. }
  1048. } else if (orig->channels > 8) {
  1049. SDL_SetError("Unsupported number of audio channels.");
  1050. return 0;
  1051. }
  1052. if (orig->samples == 0) {
  1053. const char *env = SDL_getenv("SDL_AUDIO_SAMPLES");
  1054. if ((!env) || ((prepared->samples = (Uint16) SDL_atoi(env)) == 0)) {
  1055. /* Pick a default of ~46 ms at desired frequency */
  1056. /* !!! FIXME: remove this when the non-Po2 resampling is in. */
  1057. const int samples = (prepared->freq / 1000) * 46;
  1058. int power2 = 1;
  1059. while (power2 < samples) {
  1060. power2 *= 2;
  1061. }
  1062. prepared->samples = power2;
  1063. }
  1064. }
  1065. /* Calculate the silence and size of the audio specification */
  1066. SDL_CalculateAudioSpec(prepared);
  1067. return 1;
  1068. }
  1069. static SDL_AudioDeviceID
  1070. open_audio_device(const char *devname, int iscapture,
  1071. const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
  1072. int allowed_changes, int min_id)
  1073. {
  1074. const SDL_bool is_internal_thread = (desired->callback == NULL);
  1075. SDL_AudioDeviceID id = 0;
  1076. SDL_AudioSpec _obtained;
  1077. SDL_AudioDevice *device;
  1078. SDL_bool build_stream;
  1079. void *handle = NULL;
  1080. int i = 0;
  1081. if (!SDL_GetCurrentAudioDriver()) {
  1082. SDL_SetError("Audio subsystem is not initialized");
  1083. return 0;
  1084. }
  1085. if (iscapture && !current_audio.impl.HasCaptureSupport) {
  1086. SDL_SetError("No capture support");
  1087. return 0;
  1088. }
  1089. SDL_LockMutex(current_audio.detectionLock);
  1090. /* Find an available device ID... */
  1091. for (id = min_id - 1; id < SDL_arraysize(open_devices); id++) {
  1092. if (open_devices[id] == NULL) {
  1093. break;
  1094. }
  1095. }
  1096. if (id == SDL_arraysize(open_devices)) {
  1097. SDL_SetError("Too many open audio devices");
  1098. SDL_UnlockMutex(current_audio.detectionLock);
  1099. return 0;
  1100. }
  1101. if (!obtained) {
  1102. obtained = &_obtained;
  1103. }
  1104. if (!prepare_audiospec(desired, obtained)) {
  1105. SDL_UnlockMutex(current_audio.detectionLock);
  1106. return 0;
  1107. }
  1108. /* If app doesn't care about a specific device, let the user override. */
  1109. if (devname == NULL) {
  1110. devname = SDL_getenv("SDL_AUDIO_DEVICE_NAME");
  1111. }
  1112. /*
  1113. * Catch device names at the high level for the simple case...
  1114. * This lets us have a basic "device enumeration" for systems that
  1115. * don't have multiple devices, but makes sure the device name is
  1116. * always NULL when it hits the low level.
  1117. *
  1118. * Also make sure that the simple case prevents multiple simultaneous
  1119. * opens of the default system device.
  1120. */
  1121. if ((iscapture) && (current_audio.impl.OnlyHasDefaultCaptureDevice)) {
  1122. if ((devname) && (SDL_strcmp(devname, DEFAULT_INPUT_DEVNAME) != 0)) {
  1123. SDL_SetError("No such device");
  1124. SDL_UnlockMutex(current_audio.detectionLock);
  1125. return 0;
  1126. }
  1127. devname = NULL;
  1128. for (i = 0; i < SDL_arraysize(open_devices); i++) {
  1129. if ((open_devices[i]) && (open_devices[i]->iscapture)) {
  1130. SDL_SetError("Audio device already open");
  1131. SDL_UnlockMutex(current_audio.detectionLock);
  1132. return 0;
  1133. }
  1134. }
  1135. } else if ((!iscapture) && (current_audio.impl.OnlyHasDefaultOutputDevice)) {
  1136. if ((devname) && (SDL_strcmp(devname, DEFAULT_OUTPUT_DEVNAME) != 0)) {
  1137. SDL_UnlockMutex(current_audio.detectionLock);
  1138. SDL_SetError("No such device");
  1139. return 0;
  1140. }
  1141. devname = NULL;
  1142. for (i = 0; i < SDL_arraysize(open_devices); i++) {
  1143. if ((open_devices[i]) && (!open_devices[i]->iscapture)) {
  1144. SDL_UnlockMutex(current_audio.detectionLock);
  1145. SDL_SetError("Audio device already open");
  1146. return 0;
  1147. }
  1148. }
  1149. } else if (devname != NULL) {
  1150. /* if the app specifies an exact string, we can pass the backend
  1151. an actual device handle thingey, which saves them the effort of
  1152. figuring out what device this was (such as, reenumerating
  1153. everything again to find the matching human-readable name).
  1154. It might still need to open a device based on the string for,
  1155. say, a network audio server, but this optimizes some cases. */
  1156. SDL_AudioDeviceItem *item;
  1157. for (item = iscapture ? current_audio.inputDevices : current_audio.outputDevices; item; item = item->next) {
  1158. if ((item->handle != NULL) && (SDL_strcmp(item->name, devname) == 0)) {
  1159. handle = item->handle;
  1160. break;
  1161. }
  1162. }
  1163. }
  1164. if (!current_audio.impl.AllowsArbitraryDeviceNames) {
  1165. /* has to be in our device list, or the default device. */
  1166. if ((handle == NULL) && (devname != NULL)) {
  1167. SDL_SetError("No such device.");
  1168. SDL_UnlockMutex(current_audio.detectionLock);
  1169. return 0;
  1170. }
  1171. }
  1172. device = (SDL_AudioDevice *) SDL_calloc(1, sizeof (SDL_AudioDevice));
  1173. if (device == NULL) {
  1174. SDL_OutOfMemory();
  1175. SDL_UnlockMutex(current_audio.detectionLock);
  1176. return 0;
  1177. }
  1178. device->id = id + 1;
  1179. device->spec = *obtained;
  1180. device->iscapture = iscapture ? SDL_TRUE : SDL_FALSE;
  1181. device->handle = handle;
  1182. SDL_AtomicSet(&device->shutdown, 0); /* just in case. */
  1183. SDL_AtomicSet(&device->paused, 1);
  1184. SDL_AtomicSet(&device->enabled, 1);
  1185. /* Create a mutex for locking the sound buffers */
  1186. if (current_audio.impl.LockDevice == SDL_AudioLockDevice_Default) {
  1187. device->mixer_lock = SDL_CreateMutex();
  1188. if (device->mixer_lock == NULL) {
  1189. close_audio_device(device);
  1190. SDL_UnlockMutex(current_audio.detectionLock);
  1191. SDL_SetError("Couldn't create mixer lock");
  1192. return 0;
  1193. }
  1194. }
  1195. /* For backends that require a power-of-two value for spec.samples, take the
  1196. * value we got from 'desired' and round up to the nearest value
  1197. */
  1198. if (!current_audio.impl.SupportsNonPow2Samples && device->spec.samples > 0) {
  1199. device->spec.samples = SDL_powerof2(device->spec.samples);
  1200. }
  1201. if (current_audio.impl.OpenDevice(device, devname) < 0) {
  1202. close_audio_device(device);
  1203. SDL_UnlockMutex(current_audio.detectionLock);
  1204. return 0;
  1205. }
  1206. /* if your target really doesn't need it, set it to 0x1 or something. */
  1207. /* otherwise, close_audio_device() won't call impl.CloseDevice(). */
  1208. SDL_assert(device->hidden != NULL);
  1209. /* See if we need to do any conversion */
  1210. build_stream = SDL_FALSE;
  1211. if (obtained->freq != device->spec.freq) {
  1212. if (allowed_changes & SDL_AUDIO_ALLOW_FREQUENCY_CHANGE) {
  1213. obtained->freq = device->spec.freq;
  1214. } else {
  1215. build_stream = SDL_TRUE;
  1216. }
  1217. }
  1218. if (obtained->format != device->spec.format) {
  1219. if (allowed_changes & SDL_AUDIO_ALLOW_FORMAT_CHANGE) {
  1220. obtained->format = device->spec.format;
  1221. } else {
  1222. build_stream = SDL_TRUE;
  1223. }
  1224. }
  1225. if (obtained->channels != device->spec.channels) {
  1226. if (allowed_changes & SDL_AUDIO_ALLOW_CHANNELS_CHANGE) {
  1227. obtained->channels = device->spec.channels;
  1228. } else {
  1229. build_stream = SDL_TRUE;
  1230. }
  1231. }
  1232. if (device->spec.samples != obtained->samples) {
  1233. if (allowed_changes & SDL_AUDIO_ALLOW_SAMPLES_CHANGE) {
  1234. obtained->samples = device->spec.samples;
  1235. } else {
  1236. build_stream = SDL_TRUE;
  1237. }
  1238. }
  1239. SDL_CalculateAudioSpec(obtained); /* recalc after possible changes. */
  1240. device->callbackspec = *obtained;
  1241. if (build_stream) {
  1242. if (iscapture) {
  1243. device->stream = SDL_NewAudioStream(device->spec.format,
  1244. device->spec.channels, device->spec.freq,
  1245. obtained->format, obtained->channels, obtained->freq);
  1246. } else {
  1247. device->stream = SDL_NewAudioStream(obtained->format, obtained->channels,
  1248. obtained->freq, device->spec.format,
  1249. device->spec.channels, device->spec.freq);
  1250. }
  1251. if (!device->stream) {
  1252. close_audio_device(device);
  1253. SDL_UnlockMutex(current_audio.detectionLock);
  1254. return 0;
  1255. }
  1256. }
  1257. if (device->spec.callback == NULL) { /* use buffer queueing? */
  1258. /* pool a few packets to start. Enough for two callbacks. */
  1259. device->buffer_queue = SDL_NewDataQueue(SDL_AUDIOBUFFERQUEUE_PACKETLEN, obtained->size * 2);
  1260. if (!device->buffer_queue) {
  1261. close_audio_device(device);
  1262. SDL_UnlockMutex(current_audio.detectionLock);
  1263. SDL_SetError("Couldn't create audio buffer queue");
  1264. return 0;
  1265. }
  1266. device->callbackspec.callback = iscapture ? SDL_BufferQueueFillCallback : SDL_BufferQueueDrainCallback;
  1267. device->callbackspec.userdata = device;
  1268. }
  1269. /* Allocate a scratch audio buffer */
  1270. device->work_buffer_len = build_stream ? device->callbackspec.size : 0;
  1271. if (device->spec.size > device->work_buffer_len) {
  1272. device->work_buffer_len = device->spec.size;
  1273. }
  1274. SDL_assert(device->work_buffer_len > 0);
  1275. device->work_buffer = (Uint8 *) SDL_malloc(device->work_buffer_len);
  1276. if (device->work_buffer == NULL) {
  1277. close_audio_device(device);
  1278. SDL_UnlockMutex(current_audio.detectionLock);
  1279. SDL_OutOfMemory();
  1280. return 0;
  1281. }
  1282. open_devices[id] = device; /* add it to our list of open devices. */
  1283. /* Start the audio thread if necessary */
  1284. if (!current_audio.impl.ProvidesOwnCallbackThread) {
  1285. /* Start the audio thread */
  1286. /* !!! FIXME: we don't force the audio thread stack size here if it calls into user code, but maybe we should? */
  1287. /* buffer queueing callback only needs a few bytes, so make the stack tiny. */
  1288. const size_t stacksize = is_internal_thread ? 64 * 1024 : 0;
  1289. char threadname[64];
  1290. SDL_snprintf(threadname, sizeof (threadname), "SDLAudio%c%d", (iscapture) ? 'C' : 'P', (int) device->id);
  1291. device->thread = SDL_CreateThreadInternal(iscapture ? SDL_CaptureAudio : SDL_RunAudio, threadname, stacksize, device);
  1292. if (device->thread == NULL) {
  1293. close_audio_device(device);
  1294. SDL_SetError("Couldn't create audio thread");
  1295. SDL_UnlockMutex(current_audio.detectionLock);
  1296. return 0;
  1297. }
  1298. }
  1299. SDL_UnlockMutex(current_audio.detectionLock);
  1300. return device->id;
  1301. }
  1302. int
  1303. SDL_OpenAudio(SDL_AudioSpec * desired, SDL_AudioSpec * obtained)
  1304. {
  1305. SDL_AudioDeviceID id = 0;
  1306. /* Start up the audio driver, if necessary. This is legacy behaviour! */
  1307. if (!SDL_WasInit(SDL_INIT_AUDIO)) {
  1308. if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  1309. return -1;
  1310. }
  1311. }
  1312. /* SDL_OpenAudio() is legacy and can only act on Device ID #1. */
  1313. if (open_devices[0] != NULL) {
  1314. return SDL_SetError("Audio device is already opened");
  1315. }
  1316. if (obtained) {
  1317. id = open_audio_device(NULL, 0, desired, obtained,
  1318. SDL_AUDIO_ALLOW_ANY_CHANGE, 1);
  1319. } else {
  1320. SDL_AudioSpec _obtained;
  1321. SDL_zero(_obtained);
  1322. id = open_audio_device(NULL, 0, desired, &_obtained, 0, 1);
  1323. /* On successful open, copy calculated values into 'desired'. */
  1324. if (id > 0) {
  1325. desired->size = _obtained.size;
  1326. desired->silence = _obtained.silence;
  1327. }
  1328. }
  1329. SDL_assert((id == 0) || (id == 1));
  1330. return (id == 0) ? -1 : 0;
  1331. }
  1332. SDL_AudioDeviceID
  1333. SDL_OpenAudioDevice(const char *device, int iscapture,
  1334. const SDL_AudioSpec * desired, SDL_AudioSpec * obtained,
  1335. int allowed_changes)
  1336. {
  1337. return open_audio_device(device, iscapture, desired, obtained,
  1338. allowed_changes, 2);
  1339. }
  1340. SDL_AudioStatus
  1341. SDL_GetAudioDeviceStatus(SDL_AudioDeviceID devid)
  1342. {
  1343. SDL_AudioDevice *device = get_audio_device(devid);
  1344. SDL_AudioStatus status = SDL_AUDIO_STOPPED;
  1345. if (device && SDL_AtomicGet(&device->enabled)) {
  1346. if (SDL_AtomicGet(&device->paused)) {
  1347. status = SDL_AUDIO_PAUSED;
  1348. } else {
  1349. status = SDL_AUDIO_PLAYING;
  1350. }
  1351. }
  1352. return status;
  1353. }
  1354. SDL_AudioStatus
  1355. SDL_GetAudioStatus(void)
  1356. {
  1357. return SDL_GetAudioDeviceStatus(1);
  1358. }
  1359. void
  1360. SDL_PauseAudioDevice(SDL_AudioDeviceID devid, int pause_on)
  1361. {
  1362. SDL_AudioDevice *device = get_audio_device(devid);
  1363. if (device) {
  1364. current_audio.impl.LockDevice(device);
  1365. SDL_AtomicSet(&device->paused, pause_on ? 1 : 0);
  1366. current_audio.impl.UnlockDevice(device);
  1367. }
  1368. }
  1369. void
  1370. SDL_PauseAudio(int pause_on)
  1371. {
  1372. SDL_PauseAudioDevice(1, pause_on);
  1373. }
  1374. void
  1375. SDL_LockAudioDevice(SDL_AudioDeviceID devid)
  1376. {
  1377. /* Obtain a lock on the mixing buffers */
  1378. SDL_AudioDevice *device = get_audio_device(devid);
  1379. if (device) {
  1380. current_audio.impl.LockDevice(device);
  1381. }
  1382. }
  1383. void
  1384. SDL_LockAudio(void)
  1385. {
  1386. SDL_LockAudioDevice(1);
  1387. }
  1388. void
  1389. SDL_UnlockAudioDevice(SDL_AudioDeviceID devid)
  1390. {
  1391. /* Obtain a lock on the mixing buffers */
  1392. SDL_AudioDevice *device = get_audio_device(devid);
  1393. if (device) {
  1394. current_audio.impl.UnlockDevice(device);
  1395. }
  1396. }
  1397. void
  1398. SDL_UnlockAudio(void)
  1399. {
  1400. SDL_UnlockAudioDevice(1);
  1401. }
  1402. void
  1403. SDL_CloseAudioDevice(SDL_AudioDeviceID devid)
  1404. {
  1405. close_audio_device(get_audio_device(devid));
  1406. }
  1407. void
  1408. SDL_CloseAudio(void)
  1409. {
  1410. SDL_CloseAudioDevice(1);
  1411. }
  1412. void
  1413. SDL_AudioQuit(void)
  1414. {
  1415. SDL_AudioDeviceID i;
  1416. if (!current_audio.name) { /* not initialized?! */
  1417. return;
  1418. }
  1419. for (i = 0; i < SDL_arraysize(open_devices); i++) {
  1420. close_audio_device(open_devices[i]);
  1421. }
  1422. free_device_list(&current_audio.outputDevices, &current_audio.outputDeviceCount);
  1423. free_device_list(&current_audio.inputDevices, &current_audio.inputDeviceCount);
  1424. /* Free the driver data */
  1425. current_audio.impl.Deinitialize();
  1426. SDL_DestroyMutex(current_audio.detectionLock);
  1427. SDL_zero(current_audio);
  1428. SDL_zeroa(open_devices);
  1429. #ifdef HAVE_LIBSAMPLERATE_H
  1430. UnloadLibSampleRate();
  1431. #endif
  1432. }
  1433. #define NUM_FORMATS 10
  1434. static int format_idx;
  1435. static int format_idx_sub;
  1436. static SDL_AudioFormat format_list[NUM_FORMATS][NUM_FORMATS] = {
  1437. {AUDIO_U8, AUDIO_S8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
  1438. AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
  1439. {AUDIO_S8, AUDIO_U8, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB,
  1440. AUDIO_U16MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB},
  1441. {AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S32LSB,
  1442. AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
  1443. {AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S32MSB,
  1444. AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
  1445. {AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S32LSB,
  1446. AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_U8, AUDIO_S8},
  1447. {AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_S16MSB, AUDIO_S16LSB, AUDIO_S32MSB,
  1448. AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_U8, AUDIO_S8},
  1449. {AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S16LSB,
  1450. AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
  1451. {AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S16MSB,
  1452. AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
  1453. {AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S16LSB,
  1454. AUDIO_S16MSB, AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U8, AUDIO_S8},
  1455. {AUDIO_F32MSB, AUDIO_F32LSB, AUDIO_S32MSB, AUDIO_S32LSB, AUDIO_S16MSB,
  1456. AUDIO_S16LSB, AUDIO_U16MSB, AUDIO_U16LSB, AUDIO_U8, AUDIO_S8},
  1457. };
  1458. SDL_AudioFormat
  1459. SDL_FirstAudioFormat(SDL_AudioFormat format)
  1460. {
  1461. for (format_idx = 0; format_idx < NUM_FORMATS; ++format_idx) {
  1462. if (format_list[format_idx][0] == format) {
  1463. break;
  1464. }
  1465. }
  1466. format_idx_sub = 0;
  1467. return SDL_NextAudioFormat();
  1468. }
  1469. SDL_AudioFormat
  1470. SDL_NextAudioFormat(void)
  1471. {
  1472. if ((format_idx == NUM_FORMATS) || (format_idx_sub == NUM_FORMATS)) {
  1473. return 0;
  1474. }
  1475. return format_list[format_idx][format_idx_sub++];
  1476. }
  1477. Uint8
  1478. SDL_SilenceValueForFormat(const SDL_AudioFormat format)
  1479. {
  1480. switch (format) {
  1481. /* !!! FIXME: 0x80 isn't perfect for U16, but we can't fit 0x8000 in a
  1482. !!! FIXME: byte for SDL_memset() use. This is actually 0.1953 percent
  1483. !!! FIXME: off from silence. Maybe just don't use U16. */
  1484. case AUDIO_U16LSB:
  1485. case AUDIO_U16MSB:
  1486. case AUDIO_U8:
  1487. return 0x80;
  1488. default: break;
  1489. }
  1490. return 0x00;
  1491. }
  1492. void
  1493. SDL_CalculateAudioSpec(SDL_AudioSpec * spec)
  1494. {
  1495. spec->silence = SDL_SilenceValueForFormat(spec->format);
  1496. spec->size = SDL_AUDIO_BITSIZE(spec->format) / 8;
  1497. spec->size *= spec->channels;
  1498. spec->size *= spec->samples;
  1499. }
  1500. /*
  1501. * Moved here from SDL_mixer.c, since it relies on internals of an opened
  1502. * audio device (and is deprecated, by the way!).
  1503. */
  1504. void
  1505. SDL_MixAudio(Uint8 * dst, const Uint8 * src, Uint32 len, int volume)
  1506. {
  1507. /* Mix the user-level audio format */
  1508. SDL_AudioDevice *device = get_audio_device(1);
  1509. if (device != NULL) {
  1510. SDL_MixAudioFormat(dst, src, device->callbackspec.format, len, volume);
  1511. }
  1512. }
  1513. /* vi: set ts=4 sw=4 expandtab: */