SDL_audio.c 54 KB

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