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