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