SDL_sysaudio.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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_hashtable.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. #define AUDIO_SPECS_EQUAL(x, y) (((x).format == (y).format) && ((x).channels == (y).channels) && ((x).freq == (y).freq))
  49. typedef struct SDL_AudioDevice SDL_AudioDevice;
  50. typedef struct SDL_LogicalAudioDevice SDL_LogicalAudioDevice;
  51. // Used by src/SDL.c to initialize a particular audio driver.
  52. extern int SDL_InitAudio(const char *driver_name);
  53. // Used by src/SDL.c to shut down previously-initialized audio.
  54. extern void SDL_QuitAudio(void);
  55. // Function to get a list of audio formats, ordered most similar to `format` to least, 0-terminated. Don't free results.
  56. const SDL_AudioFormat *SDL_ClosestAudioFormats(SDL_AudioFormat format);
  57. // Must be called at least once before using converters.
  58. extern void SDL_ChooseAudioConverters(void);
  59. extern void SDL_SetupAudioResampler(void);
  60. /* Backends should call this as devices are added to the system (such as
  61. a USB headset being plugged in), and should also be called for
  62. for every device found during DetectDevices(). */
  63. extern SDL_AudioDevice *SDL_AddAudioDevice(const SDL_bool iscapture, const char *name, const SDL_AudioSpec *spec, void *handle);
  64. /* Backends should call this if an opened audio device is lost.
  65. This can happen due to i/o errors, or a device being unplugged, etc. */
  66. extern void SDL_AudioDeviceDisconnected(SDL_AudioDevice *device);
  67. // Backends should call this if the system default device changes.
  68. extern void SDL_DefaultAudioDeviceChanged(SDL_AudioDevice *new_default_device);
  69. // Backends should call this if a device's format is changing (opened or not); SDL will update state and carry on with the new format.
  70. extern int SDL_AudioDeviceFormatChanged(SDL_AudioDevice *device, const SDL_AudioSpec *newspec, int new_sample_frames);
  71. // Same as above, but assume the device is already locked.
  72. extern int SDL_AudioDeviceFormatChangedAlreadyLocked(SDL_AudioDevice *device, const SDL_AudioSpec *newspec, int new_sample_frames);
  73. // Find the SDL_AudioDevice associated with the handle supplied to SDL_AddAudioDevice. NULL if not found. DOES NOT LOCK THE DEVICE.
  74. extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByHandle(void *handle);
  75. // Find an SDL_AudioDevice, selected by a callback. NULL if not found. DOES NOT LOCK THE DEVICE.
  76. extern SDL_AudioDevice *SDL_FindPhysicalAudioDeviceByCallback(SDL_bool (*callback)(SDL_AudioDevice *device, void *userdata), void *userdata);
  77. // Backends should call this if they change the device format, channels, freq, or sample_frames to keep other state correct.
  78. extern void SDL_UpdatedAudioDeviceFormat(SDL_AudioDevice *device);
  79. // Backends can call this to get a standardized name for a thread to power a specific audio device.
  80. extern char *SDL_GetAudioThreadName(SDL_AudioDevice *device, char *buf, size_t buflen);
  81. // These functions are the heart of the audio threads. Backends can call them directly if they aren't using the SDL-provided thread.
  82. extern void SDL_OutputAudioThreadSetup(SDL_AudioDevice *device);
  83. extern SDL_bool SDL_OutputAudioThreadIterate(SDL_AudioDevice *device);
  84. extern void SDL_OutputAudioThreadShutdown(SDL_AudioDevice *device);
  85. extern void SDL_CaptureAudioThreadSetup(SDL_AudioDevice *device);
  86. extern SDL_bool SDL_CaptureAudioThreadIterate(SDL_AudioDevice *device);
  87. extern void SDL_CaptureAudioThreadShutdown(SDL_AudioDevice *device);
  88. extern void SDL_AudioThreadFinalize(SDL_AudioDevice *device);
  89. // this gets used from the audio device threads. It has rules, don't use this if you don't know how to use it!
  90. extern void ConvertAudio(int num_frames, const void *src, SDL_AudioFormat src_format, int src_channels,
  91. void *dst, SDL_AudioFormat dst_format, int dst_channels, void* scratch);
  92. // Special case to let something in SDL_audiocvt.c access something in SDL_audio.c. Don't use this.
  93. extern void OnAudioStreamCreated(SDL_AudioStream *stream);
  94. extern void OnAudioStreamDestroy(SDL_AudioStream *stream);
  95. typedef struct SDL_AudioDriverImpl
  96. {
  97. void (*DetectDevices)(SDL_AudioDevice **default_output, SDL_AudioDevice **default_capture);
  98. int (*OpenDevice)(SDL_AudioDevice *device);
  99. void (*ThreadInit)(SDL_AudioDevice *device); // Called by audio thread at start
  100. void (*ThreadDeinit)(SDL_AudioDevice *device); // Called by audio thread at end
  101. int (*WaitDevice)(SDL_AudioDevice *device);
  102. int (*PlayDevice)(SDL_AudioDevice *device, const Uint8 *buffer, int buflen); // buffer and buflen are always from GetDeviceBuf, passed here for convenience.
  103. Uint8 *(*GetDeviceBuf)(SDL_AudioDevice *device, int *buffer_size);
  104. int (*WaitCaptureDevice)(SDL_AudioDevice *device);
  105. int (*CaptureFromDevice)(SDL_AudioDevice *device, void *buffer, int buflen);
  106. void (*FlushCapture)(SDL_AudioDevice *device);
  107. void (*CloseDevice)(SDL_AudioDevice *device);
  108. void (*FreeDeviceHandle)(SDL_AudioDevice *device); // SDL is done with this device; free the handle from SDL_AddAudioDevice()
  109. void (*Deinitialize)(void);
  110. // Some flags to push duplicate code into the core and reduce #ifdefs.
  111. SDL_bool ProvidesOwnCallbackThread; // !!! FIXME: rename this, it's not a callback thread anymore.
  112. SDL_bool HasCaptureSupport;
  113. SDL_bool OnlyHasDefaultOutputDevice;
  114. SDL_bool OnlyHasDefaultCaptureDevice; // !!! FIXME: is there ever a time where you'd have a default output and not a default capture (or vice versa)?
  115. } SDL_AudioDriverImpl;
  116. typedef struct SDL_AudioDriver
  117. {
  118. const char *name; // The name of this audio driver
  119. const char *desc; // The description of this audio driver
  120. SDL_AudioDriverImpl impl; // the backend's interface
  121. SDL_RWLock *device_hash_lock; // A rwlock that protects `device_hash`
  122. SDL_HashTable *device_hash; // the collection of currently-available audio devices (capture, playback, logical and physical!)
  123. SDL_AudioStream *existing_streams; // a list of all existing SDL_AudioStreams.
  124. SDL_AudioDeviceID default_output_device_id;
  125. SDL_AudioDeviceID default_capture_device_id;
  126. // !!! FIXME: most (all?) of these don't have to be atomic.
  127. SDL_AtomicInt output_device_count;
  128. SDL_AtomicInt capture_device_count;
  129. SDL_AtomicInt last_device_instance_id; // increments on each device add to provide unique instance IDs
  130. SDL_AtomicInt shutting_down; // non-zero during SDL_Quit, so we known not to accept any last-minute device hotplugs.
  131. } SDL_AudioDriver;
  132. struct SDL_AudioQueue; // forward decl.
  133. struct SDL_AudioStream
  134. {
  135. SDL_Mutex* lock;
  136. SDL_PropertiesID props;
  137. SDL_AudioStreamCallback get_callback;
  138. void *get_callback_userdata;
  139. SDL_AudioStreamCallback put_callback;
  140. void *put_callback_userdata;
  141. SDL_AudioSpec src_spec;
  142. SDL_AudioSpec dst_spec;
  143. float freq_ratio;
  144. struct SDL_AudioQueue* queue;
  145. Uint64 total_bytes_queued;
  146. SDL_AudioSpec input_spec; // The spec of input data currently being processed
  147. Sint64 resample_offset;
  148. Uint8 *work_buffer; // used for scratch space during data conversion/resampling.
  149. size_t work_buffer_allocation;
  150. Uint8 *history_buffer; // history for left padding and future sample rate changes.
  151. size_t history_buffer_allocation;
  152. SDL_bool simplified; // SDL_TRUE if created via SDL_OpenAudioDeviceStream
  153. SDL_LogicalAudioDevice *bound_device;
  154. SDL_AudioStream *next_binding;
  155. SDL_AudioStream *prev_binding;
  156. SDL_AudioStream *prev; // linked list of all existing streams (so we can free them on shutdown).
  157. SDL_AudioStream *next; // linked list of all existing streams (so we can free them on shutdown).
  158. };
  159. /* Logical devices are an abstraction in SDL3; you can open the same physical
  160. device multiple times, and each will result in an object with its own set
  161. of bound audio streams, etc, even though internally these are all processed
  162. as a group when mixing the final output for the physical device. */
  163. struct SDL_LogicalAudioDevice
  164. {
  165. // the unique instance ID of this device.
  166. SDL_AudioDeviceID instance_id;
  167. // The physical device associated with this opened device.
  168. SDL_AudioDevice *physical_device;
  169. // If whole logical device is paused (process no streams bound to this device).
  170. SDL_AtomicInt paused;
  171. // double-linked list of all audio streams currently bound to this opened device.
  172. SDL_AudioStream *bound_streams;
  173. // SDL_TRUE if this was opened as a default device.
  174. SDL_bool opened_as_default;
  175. // SDL_TRUE if device was opened with SDL_OpenAudioDeviceStream (so it forbids binding changes, etc).
  176. SDL_bool simplified;
  177. // If non-NULL, callback into the app that lets them access the final postmix buffer.
  178. SDL_AudioPostmixCallback postmix;
  179. // App-supplied pointer for postmix callback.
  180. void *postmix_userdata;
  181. // double-linked list of opened devices on the same physical device.
  182. SDL_LogicalAudioDevice *next;
  183. SDL_LogicalAudioDevice *prev;
  184. };
  185. struct SDL_AudioDevice
  186. {
  187. // A mutex for locking access to this struct
  188. SDL_Mutex *lock;
  189. // human-readable name of the device. ("SoundBlaster Pro 16")
  190. char *name;
  191. // the unique instance ID of this device.
  192. SDL_AudioDeviceID instance_id;
  193. // a way for the backend to identify this device _when not opened_
  194. void *handle;
  195. // The device's current audio specification
  196. SDL_AudioSpec spec;
  197. int buffer_size;
  198. // The device's default audio specification
  199. SDL_AudioSpec default_spec;
  200. // Number of sample frames the devices wants per-buffer.
  201. int sample_frames;
  202. // Value to use for SDL_memset to silence a buffer in this device's format
  203. int silence_value;
  204. // non-zero if we are signaling the audio thread to end.
  205. SDL_AtomicInt shutdown;
  206. // non-zero if we want the device to be destroyed (so audio thread knows to do it on termination).
  207. SDL_AtomicInt condemned;
  208. // non-zero if this was a disconnected default device and we're waiting for its replacement.
  209. SDL_AtomicInt zombie;
  210. // non-zero if this has a thread running (which might be `thread` or something provided by the backend!)
  211. SDL_AtomicInt thread_alive;
  212. // SDL_TRUE if this is a capture device instead of an output device
  213. SDL_bool iscapture;
  214. // SDL_TRUE if audio thread can skip silence/mix/convert stages and just do a basic memcpy.
  215. SDL_bool simple_copy;
  216. // Scratch buffers used for mixing.
  217. Uint8 *work_buffer;
  218. Uint8 *mix_buffer;
  219. float *postmix_buffer;
  220. // Size of work_buffer (and mix_buffer) in bytes.
  221. int work_buffer_size;
  222. // A thread to feed the audio device
  223. SDL_Thread *thread;
  224. // SDL_TRUE if this physical device is currently opened by the backend.
  225. SDL_bool currently_opened;
  226. // Data private to this driver
  227. struct SDL_PrivateAudioData *hidden;
  228. // All logical devices associated with this physical device.
  229. SDL_LogicalAudioDevice *logical_devices;
  230. };
  231. typedef struct AudioBootStrap
  232. {
  233. const char *name;
  234. const char *desc;
  235. SDL_bool (*init)(SDL_AudioDriverImpl *impl);
  236. SDL_bool demand_only; // if SDL_TRUE: request explicitly, or it won't be available.
  237. } AudioBootStrap;
  238. // Not all of these are available in a given build. Use #ifdefs, etc.
  239. extern AudioBootStrap PIPEWIRE_bootstrap;
  240. extern AudioBootStrap PULSEAUDIO_bootstrap;
  241. extern AudioBootStrap ALSA_bootstrap;
  242. extern AudioBootStrap JACK_bootstrap;
  243. extern AudioBootStrap SNDIO_bootstrap;
  244. extern AudioBootStrap NETBSDAUDIO_bootstrap;
  245. extern AudioBootStrap DSP_bootstrap;
  246. extern AudioBootStrap WASAPI_bootstrap;
  247. extern AudioBootStrap DSOUND_bootstrap;
  248. extern AudioBootStrap WINMM_bootstrap;
  249. extern AudioBootStrap HAIKUAUDIO_bootstrap;
  250. extern AudioBootStrap COREAUDIO_bootstrap;
  251. extern AudioBootStrap DISKAUDIO_bootstrap;
  252. extern AudioBootStrap DUMMYAUDIO_bootstrap;
  253. extern AudioBootStrap AAUDIO_bootstrap;
  254. extern AudioBootStrap openslES_bootstrap; // !!! FIXME: capitalize this to match the others
  255. extern AudioBootStrap ANDROIDAUDIO_bootstrap;
  256. extern AudioBootStrap PS2AUDIO_bootstrap;
  257. extern AudioBootStrap PSPAUDIO_bootstrap;
  258. extern AudioBootStrap VITAAUD_bootstrap;
  259. extern AudioBootStrap N3DSAUDIO_bootstrap;
  260. extern AudioBootStrap EMSCRIPTENAUDIO_bootstrap;
  261. extern AudioBootStrap QSAAUDIO_bootstrap;
  262. #endif // SDL_sysaudio_h_