SDL_sysaudio.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #ifndef SDL_sysaudio_h_
  20. #define SDL_sysaudio_h_
  21. #include "../SDL_dataqueue.h"
  22. #define DEBUG_AUDIOSTREAM 0
  23. #define DEBUG_AUDIO_CONVERT 0
  24. #if DEBUG_AUDIO_CONVERT
  25. #define LOG_DEBUG_AUDIO_CONVERT(from, to) SDL_Log("SDL_AUDIO_CONVERT: Converting %s to %s.\n", from, to);
  26. #else
  27. #define LOG_DEBUG_AUDIO_CONVERT(from, to)
  28. #endif
  29. // These pointers get set during SDL_ChooseAudioConverters() to various SIMD implementations.
  30. extern void (*SDL_Convert_S8_to_F32)(float *dst, const Sint8 *src, int num_samples);
  31. extern void (*SDL_Convert_U8_to_F32)(float *dst, const Uint8 *src, int num_samples);
  32. extern void (*SDL_Convert_S16_to_F32)(float *dst, const Sint16 *src, int num_samples);
  33. extern void (*SDL_Convert_S32_to_F32)(float *dst, const Sint32 *src, int num_samples);
  34. extern void (*SDL_Convert_F32_to_S8)(Sint8 *dst, const float *src, int num_samples);
  35. extern void (*SDL_Convert_F32_to_U8)(Uint8 *dst, const float *src, int num_samples);
  36. extern void (*SDL_Convert_F32_to_S16)(Sint16 *dst, const float *src, int num_samples);
  37. extern void (*SDL_Convert_F32_to_S32)(Sint32 *dst, const float *src, int num_samples);
  38. // !!! FIXME: These are wordy and unlocalized...
  39. #define DEFAULT_OUTPUT_DEVNAME "System audio output device"
  40. #define DEFAULT_INPUT_DEVNAME "System audio capture device"
  41. // these are used when no better specifics are known. We default to CD audio quality.
  42. #define DEFAULT_AUDIO_OUTPUT_FORMAT SDL_AUDIO_S16
  43. #define DEFAULT_AUDIO_OUTPUT_CHANNELS 2
  44. #define DEFAULT_AUDIO_OUTPUT_FREQUENCY 44100
  45. #define DEFAULT_AUDIO_CAPTURE_FORMAT SDL_AUDIO_S16
  46. #define DEFAULT_AUDIO_CAPTURE_CHANNELS 1
  47. #define DEFAULT_AUDIO_CAPTURE_FREQUENCY 44100
  48. typedef struct SDL_AudioDevice SDL_AudioDevice;
  49. typedef struct SDL_LogicalAudioDevice SDL_LogicalAudioDevice;
  50. // Used by src/SDL.c to initialize a particular audio driver.
  51. extern int SDL_InitAudio(const char *driver_name);
  52. // Used by src/SDL.c to shut down previously-initialized audio.
  53. extern void SDL_QuitAudio(void);
  54. // Function to get a list of audio formats, ordered most similar to `format` to least, 0-terminated. Don't free results.
  55. const SDL_AudioFormat *SDL_ClosestAudioFormats(SDL_AudioFormat format);
  56. // Must be called at least once before using converters (SDL_CreateAudioStream will call it !!! FIXME but probably shouldn't).
  57. extern void SDL_ChooseAudioConverters(void);
  58. /* Backends should call this as devices are added to the system (such as
  59. a USB headset being plugged in), and should also be called for
  60. for every device found during DetectDevices(). */
  61. extern SDL_AudioDevice *SDL_AddAudioDevice(const SDL_bool iscapture, const char *name, const SDL_AudioSpec *spec, void *handle);
  62. /* Backends should call this if an opened audio device is lost.
  63. This can happen due to i/o errors, or a device being unplugged, etc. */
  64. extern void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device);
  65. // Backends should call this if the system default device changes.
  66. extern void SDL_DefaultAudioDeviceChanged(SDL_AudioDevice *new_default_device);
  67. // Find the SDL_AudioDevice associated with the handle supplied to SDL_AddAudioDevice. NULL if not found. DOES NOT LOCK THE DEVICE.
  68. extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle);
  69. // Find an SDL_AudioDevice, selected by a callback. NULL if not found. DOES NOT LOCK THE DEVICE.
  70. extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(SDL_bool (*callback)(SDL_AudioDevice *device, void *userdata), void *userdata);
  71. // Backends should call this if they change the device format, channels, freq, or sample_frames to keep other state correct.
  72. extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device);
  73. // Backends can call this to get a standardized name for a thread to power a specific audio device.
  74. char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen);
  75. // These functions are the heart of the audio threads. Backends can call them directly if they aren't using the SDL-provided thread.
  76. extern void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device);
  77. extern SDL_bool SDL_OutputAudioThreadIterate(SDL_AudioDevice *device);
  78. extern void SDL_OutputAudioThreadShutdown(SDL_AudioDevice *device);
  79. extern void SDL_CaptureAudioThreadSetup(SDL_AudioDevice *device);
  80. extern SDL_bool SDL_CaptureAudioThreadIterate(SDL_AudioDevice *device);
  81. extern void SDL_CaptureAudioThreadShutdown(SDL_AudioDevice *device);
  82. extern void SDL_AudioThreadFinalize(SDL_AudioDevice *device);
  83. typedef struct SDL_AudioDriverImpl
  84. {
  85. void (*DetectDevices)(SDL_AudioDevice **default_output, SDL_AudioDevice **default_capture);
  86. int (*OpenDevice)(SDL_AudioDevice *device);
  87. void (*ThreadInit)(SDL_AudioDevice *device); // Called by audio thread at start
  88. void (*ThreadDeinit)(SDL_AudioDevice *device); // Called by audio thread at end
  89. void (*WaitDevice)(SDL_AudioDevice *device);
  90. void (*PlayDevice)(SDL_AudioDevice *device, const Uint8 *buffer, int buflen); // buffer and buflen are always from GetDeviceBuf, passed here for convenience.
  91. Uint8 *(*GetDeviceBuf)(SDL_AudioDevice *device, int *buffer_size);
  92. void (*WaitCaptureDevice)(SDL_AudioDevice *device);
  93. int (*CaptureFromDevice)(SDL_AudioDevice *device, void *buffer, int buflen);
  94. void (*FlushCapture)(SDL_AudioDevice *device);
  95. void (*CloseDevice)(SDL_AudioDevice *device);
  96. void (*FreeDeviceHandle)(SDL_AudioDevice *device); // SDL is done with this device; free the handle from SDL_AddAudioDevice()
  97. void (*Deinitialize)(void);
  98. // Some flags to push duplicate code into the core and reduce #ifdefs.
  99. SDL_bool ProvidesOwnCallbackThread; // !!! FIXME: rename this, it's not a callback thread anymore.
  100. SDL_bool HasCaptureSupport;
  101. SDL_bool OnlyHasDefaultOutputDevice;
  102. SDL_bool OnlyHasDefaultCaptureDevice;
  103. SDL_bool AllowsArbitraryDeviceNames;
  104. } SDL_AudioDriverImpl;
  105. typedef struct SDL_AudioDriver
  106. {
  107. const char *name; // The name of this audio driver
  108. const char *desc; // The description of this audio driver
  109. SDL_AudioDriverImpl impl; // the backend's interface
  110. SDL_RWLock *device_list_lock; // A mutex for device detection
  111. SDL_AudioDevice *output_devices; // the list of currently-available audio output devices.
  112. SDL_AudioDevice *capture_devices; // the list of currently-available audio capture devices.
  113. SDL_AudioDeviceID default_output_device_id;
  114. SDL_AudioDeviceID default_capture_device_id;
  115. SDL_AtomicInt output_device_count;
  116. SDL_AtomicInt capture_device_count;
  117. SDL_AtomicInt last_device_instance_id; // increments on each device add to provide unique instance IDs
  118. SDL_AtomicInt shutting_down; // non-zero during SDL_Quit, so we known not to accept any last-minute device hotplugs.
  119. } SDL_AudioDriver;
  120. struct SDL_AudioStream
  121. {
  122. SDL_DataQueue *queue;
  123. SDL_Mutex *lock; // this is just a copy of `queue`'s mutex. We share a lock.
  124. SDL_AudioStreamRequestCallback get_callback;
  125. void *get_callback_userdata;
  126. SDL_AudioStreamRequestCallback put_callback;
  127. void *put_callback_userdata;
  128. Uint8 *work_buffer; // used for scratch space during data conversion/resampling.
  129. Uint8 *history_buffer; // history for left padding and future sample rate changes.
  130. Uint8 *future_buffer; // stuff that left the queue for the right padding and will be next read's data.
  131. float *left_padding; // left padding for resampling.
  132. float *right_padding; // right padding for resampling.
  133. SDL_bool flushed;
  134. size_t work_buffer_allocation;
  135. size_t history_buffer_allocation;
  136. size_t future_buffer_allocation;
  137. size_t resampler_padding_allocation;
  138. int resampler_padding_frames;
  139. int history_buffer_frames;
  140. int future_buffer_filled_frames;
  141. SDL_AudioSpec src_spec;
  142. SDL_AudioSpec dst_spec;
  143. int src_sample_frame_size;
  144. int dst_sample_frame_size;
  145. int max_sample_frame_size;
  146. int pre_resample_channels;
  147. int packetlen;
  148. SDL_LogicalAudioDevice *bound_device;
  149. SDL_AudioStream *next_binding;
  150. SDL_AudioStream *prev_binding;
  151. };
  152. /* Logical devices are an abstraction in SDL3; you can open the same physical
  153. device multiple times, and each will result in an object with its own set
  154. of bound audio streams, etc, even though internally these are all processed
  155. as a group when mixing the final output for the physical device. */
  156. struct SDL_LogicalAudioDevice
  157. {
  158. // the unique instance ID of this device.
  159. SDL_AudioDeviceID instance_id;
  160. // The physical device associated with this opened device.
  161. SDL_AudioDevice *physical_device;
  162. // If whole logical device is paused (process no streams bound to this device).
  163. SDL_AtomicInt paused;
  164. // double-linked list of all audio streams currently bound to this opened device.
  165. SDL_AudioStream *bound_streams;
  166. // SDL_TRUE if this was opened as a default device.
  167. SDL_bool is_default;
  168. // double-linked list of opened devices on the same physical device.
  169. SDL_LogicalAudioDevice *next;
  170. SDL_LogicalAudioDevice *prev;
  171. };
  172. struct SDL_AudioDevice
  173. {
  174. // A mutex for locking access to this struct
  175. SDL_Mutex *lock;
  176. // human-readable name of the device. ("SoundBlaster Pro 16")
  177. char *name;
  178. // the unique instance ID of this device.
  179. SDL_AudioDeviceID instance_id;
  180. // a way for the backend to identify this device _when not opened_
  181. void *handle;
  182. // The device's current audio specification
  183. SDL_AudioSpec spec;
  184. Uint32 buffer_size;
  185. // The device's default audio specification
  186. SDL_AudioSpec default_spec;
  187. // Number of sample frames the devices wants per-buffer.
  188. int sample_frames;
  189. // Value to use for SDL_memset to silence a buffer in this device's format
  190. int silence_value;
  191. // non-zero if we are signaling the audio thread to end.
  192. SDL_AtomicInt shutdown;
  193. // non-zero if we want the device to be destroyed (so audio thread knows to do it on termination).
  194. SDL_AtomicInt condemned;
  195. // non-zero if this was a disconnected default device and we're waiting for its replacement.
  196. SDL_AtomicInt zombie;
  197. // non-zero if this has a thread running (which might be `thread` or something provided by the backend!)
  198. SDL_AtomicInt thread_alive;
  199. // SDL_TRUE if this is a capture device instead of an output device
  200. SDL_bool iscapture;
  201. // Scratch buffer used for mixing.
  202. Uint8 *work_buffer;
  203. // A thread to feed the audio device
  204. SDL_Thread *thread;
  205. // SDL_TRUE if this physical device is currently opened by the backend.
  206. SDL_bool is_opened;
  207. // Data private to this driver
  208. struct SDL_PrivateAudioData *hidden;
  209. // All logical devices associated with this physical device.
  210. SDL_LogicalAudioDevice *logical_devices;
  211. // double-linked list of all physical devices.
  212. struct SDL_AudioDevice *prev;
  213. struct SDL_AudioDevice *next;
  214. };
  215. typedef struct AudioBootStrap
  216. {
  217. const char *name;
  218. const char *desc;
  219. SDL_bool (*init)(SDL_AudioDriverImpl *impl);
  220. SDL_bool demand_only; // if SDL_TRUE: request explicitly, or it won't be available.
  221. } AudioBootStrap;
  222. // Not all of these are available in a given build. Use #ifdefs, etc.
  223. extern AudioBootStrap PIPEWIRE_bootstrap;
  224. extern AudioBootStrap PULSEAUDIO_bootstrap;
  225. extern AudioBootStrap ALSA_bootstrap;
  226. extern AudioBootStrap JACK_bootstrap;
  227. extern AudioBootStrap SNDIO_bootstrap;
  228. extern AudioBootStrap NETBSDAUDIO_bootstrap;
  229. extern AudioBootStrap DSP_bootstrap;
  230. extern AudioBootStrap WASAPI_bootstrap;
  231. extern AudioBootStrap DSOUND_bootstrap;
  232. extern AudioBootStrap WINMM_bootstrap;
  233. extern AudioBootStrap HAIKUAUDIO_bootstrap;
  234. extern AudioBootStrap COREAUDIO_bootstrap;
  235. extern AudioBootStrap DISKAUDIO_bootstrap;
  236. extern AudioBootStrap DUMMYAUDIO_bootstrap;
  237. extern AudioBootStrap AAUDIO_bootstrap;
  238. extern AudioBootStrap openslES_bootstrap; // !!! FIXME: capitalize this to match the others
  239. extern AudioBootStrap ANDROIDAUDIO_bootstrap;
  240. extern AudioBootStrap PS2AUDIO_bootstrap;
  241. extern AudioBootStrap PSPAUDIO_bootstrap;
  242. extern AudioBootStrap VITAAUD_bootstrap;
  243. extern AudioBootStrap N3DSAUDIO_bootstrap;
  244. extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
  245. extern AudioBootStrap QSAAUDIO_bootstrap;
  246. extern SDL_AudioDevice *get_audio_dev(SDL_AudioDeviceID id);
  247. extern int get_max_num_audio_dev(void);
  248. #endif // SDL_sysaudio_h_