SDL_audio.c 37 KB

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