audio.odin 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // machine generated, do not edit
  2. package sokol_audio
  3. /*
  4. sokol_audio.h -- cross-platform audio-streaming API
  5. Project URL: https://github.com/floooh/sokol
  6. Do this:
  7. #define SOKOL_IMPL or
  8. #define SOKOL_AUDIO_IMPL
  9. before you include this file in *one* C or C++ file to create the
  10. implementation.
  11. Optionally provide the following defines with your own implementations:
  12. SOKOL_DUMMY_BACKEND - use a dummy backend
  13. SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
  14. SOKOL_AUDIO_API_DECL- public function declaration prefix (default: extern)
  15. SOKOL_API_DECL - same as SOKOL_AUDIO_API_DECL
  16. SOKOL_API_IMPL - public function implementation prefix (default: -)
  17. SAUDIO_RING_MAX_SLOTS - max number of slots in the push-audio ring buffer (default 1024)
  18. SAUDIO_OSX_USE_SYSTEM_HEADERS - define this to force inclusion of system headers on
  19. macOS instead of using embedded CoreAudio declarations
  20. If sokol_audio.h is compiled as a DLL, define the following before
  21. including the declaration or implementation:
  22. SOKOL_DLL
  23. On Windows, SOKOL_DLL will define SOKOL_AUDIO_API_DECL as __declspec(dllexport)
  24. or __declspec(dllimport) as needed.
  25. Link with the following libraries:
  26. - on macOS: AudioToolbox
  27. - on iOS: AudioToolbox, AVFoundation
  28. - on FreeBSD: asound
  29. - on Linux: asound
  30. - on Android: aaudio
  31. - on Windows with MSVC or Clang toolchain: no action needed, libs are defined in-source via pragma-comment-lib
  32. - on Windows with MINGW/MSYS2 gcc: compile with '-mwin32' and link with -lole32
  33. - on Vita: SceAudio
  34. - on 3DS: NDSP (libctru)
  35. FEATURE OVERVIEW
  36. ================
  37. You provide a mono- or stereo-stream of 32-bit float samples, which
  38. Sokol Audio feeds into platform-specific audio backends:
  39. - Windows: WASAPI
  40. - Linux: ALSA
  41. - FreeBSD: ALSA
  42. - macOS: CoreAudio
  43. - iOS: CoreAudio+AVAudioSession
  44. - emscripten: WebAudio with ScriptProcessorNode
  45. - Android: AAudio
  46. - Vita: SceAudio
  47. - 3DS: NDSP (libctru)
  48. Sokol Audio will not do any buffer mixing or volume control, if you have
  49. multiple independent input streams of sample data you need to perform the
  50. mixing yourself before forwarding the data to Sokol Audio.
  51. There are two mutually exclusive ways to provide the sample data:
  52. 1. Callback model: You provide a callback function, which will be called
  53. when Sokol Audio needs new samples. On all platforms except emscripten,
  54. this function is called from a separate thread.
  55. 2. Push model: Your code pushes small blocks of sample data from your
  56. main loop or a thread you created. The pushed data is stored in
  57. a ring buffer where it is pulled by the backend code when
  58. needed.
  59. The callback model is preferred because it is the most direct way to
  60. feed sample data into the audio backends and also has less moving parts
  61. (there is no ring buffer between your code and the audio backend).
  62. Sometimes it is not possible to generate the audio stream directly in a
  63. callback function running in a separate thread, for such cases Sokol Audio
  64. provides the push-model as a convenience.
  65. SOKOL AUDIO, SOLOUD AND MINIAUDIO
  66. =================================
  67. The WASAPI, ALSA and CoreAudio backend code has been taken from the
  68. SoLoud library (with some modifications, so any bugs in there are most
  69. likely my fault). If you need a more fully-featured audio solution, check
  70. out SoLoud, it's excellent:
  71. https://github.com/jarikomppa/soloud
  72. Another alternative which feature-wise is somewhere inbetween SoLoud and
  73. sokol-audio might be MiniAudio:
  74. https://github.com/mackron/miniaudio
  75. GLOSSARY
  76. ========
  77. - stream buffer:
  78. The internal audio data buffer, usually provided by the backend API. The
  79. size of the stream buffer defines the base latency, smaller buffers have
  80. lower latency but may cause audio glitches. Bigger buffers reduce or
  81. eliminate glitches, but have a higher base latency.
  82. - stream callback:
  83. Optional callback function which is called by Sokol Audio when it
  84. needs new samples. On Windows, macOS/iOS and Linux, this is called in
  85. a separate thread, on WebAudio, this is called per-frame in the
  86. browser thread.
  87. - channel:
  88. A discrete track of audio data, currently 1-channel (mono) and
  89. 2-channel (stereo) is supported and tested.
  90. - sample:
  91. The magnitude of an audio signal on one channel at a given time. In
  92. Sokol Audio, samples are 32-bit float numbers in the range -1.0 to
  93. +1.0.
  94. - frame:
  95. The tightly packed set of samples for all channels at a given time.
  96. For mono 1 frame is 1 sample. For stereo, 1 frame is 2 samples.
  97. - packet:
  98. In Sokol Audio, a small chunk of audio data that is moved from the
  99. main thread to the audio streaming thread in order to decouple the
  100. rate at which the main thread provides new audio data, and the
  101. streaming thread consuming audio data.
  102. WORKING WITH SOKOL AUDIO
  103. ========================
  104. First call saudio_setup() with your preferred audio playback options.
  105. In most cases you can stick with the default values, these provide
  106. a good balance between low-latency and glitch-free playback
  107. on all audio backends.
  108. You should always provide a logging callback to be aware of any
  109. warnings and errors. The easiest way is to use sokol_log.h for this:
  110. #include "sokol_log.h"
  111. // ...
  112. saudio_setup(&(saudio_desc){
  113. .logger = {
  114. .func = slog_func,
  115. }
  116. });
  117. If you want to use the callback-model, you need to provide a stream
  118. callback function either in saudio_desc.stream_cb or saudio_desc.stream_userdata_cb,
  119. otherwise keep both function pointers zero-initialized.
  120. Use push model and default playback parameters:
  121. saudio_setup(&(saudio_desc){ .logger.func = slog_func });
  122. Use stream callback model and default playback parameters:
  123. saudio_setup(&(saudio_desc){
  124. .stream_cb = my_stream_callback
  125. .logger.func = slog_func,
  126. });
  127. The standard stream callback doesn't have a user data argument, if you want
  128. that, use the alternative stream_userdata_cb and also set the user_data pointer:
  129. saudio_setup(&(saudio_desc){
  130. .stream_userdata_cb = my_stream_callback,
  131. .user_data = &my_data
  132. .logger.func = slog_func,
  133. });
  134. The following playback parameters can be provided through the
  135. saudio_desc struct:
  136. General parameters (both for stream-callback and push-model):
  137. int sample_rate -- the sample rate in Hz, default: 44100
  138. int num_channels -- number of channels, default: 1 (mono)
  139. int buffer_frames -- number of frames in streaming buffer, default: 2048
  140. The stream callback prototype (either with or without userdata):
  141. void (*stream_cb)(float* buffer, int num_frames, int num_channels)
  142. void (*stream_userdata_cb)(float* buffer, int num_frames, int num_channels, void* user_data)
  143. Function pointer to the user-provide stream callback.
  144. Push-model parameters:
  145. int packet_frames -- number of frames in a packet, default: 128
  146. int num_packets -- number of packets in ring buffer, default: 64
  147. The sample_rate and num_channels parameters are only hints for the audio
  148. backend, it isn't guaranteed that those are the values used for actual
  149. playback.
  150. To get the actual parameters, call the following functions after
  151. saudio_setup():
  152. int saudio_sample_rate(void)
  153. int saudio_channels(void);
  154. It's unlikely that the number of channels will be different than requested,
  155. but a different sample rate isn't uncommon.
  156. (NOTE: there's an yet unsolved issue when an audio backend might switch
  157. to a different sample rate when switching output devices, for instance
  158. plugging in a bluetooth headset, this case is currently not handled in
  159. Sokol Audio).
  160. You can check if audio initialization was successful with
  161. saudio_isvalid(). If backend initialization failed for some reason
  162. (for instance when there's no audio device in the machine), this
  163. will return false. Not checking for success won't do any harm, all
  164. Sokol Audio function will silently fail when called after initialization
  165. has failed, so apart from missing audio output, nothing bad will happen.
  166. Before your application exits, you should call
  167. saudio_shutdown();
  168. This stops the audio thread (on Linux, Windows and macOS/iOS) and
  169. properly shuts down the audio backend.
  170. THE STREAM CALLBACK MODEL
  171. =========================
  172. To use Sokol Audio in stream-callback-mode, provide a callback function
  173. like this in the saudio_desc struct when calling saudio_setup():
  174. void stream_cb(float* buffer, int num_frames, int num_channels) {
  175. ...
  176. }
  177. Or the alternative version with a user-data argument:
  178. void stream_userdata_cb(float* buffer, int num_frames, int num_channels, void* user_data) {
  179. my_data_t* my_data = (my_data_t*) user_data;
  180. ...
  181. }
  182. The job of the callback function is to fill the *buffer* with 32-bit
  183. float sample values.
  184. To output silence, fill the buffer with zeros:
  185. void stream_cb(float* buffer, int num_frames, int num_channels) {
  186. const int num_samples = num_frames * num_channels;
  187. for (int i = 0; i < num_samples; i++) {
  188. buffer[i] = 0.0f;
  189. }
  190. }
  191. For stereo output (num_channels == 2), the samples for the left
  192. and right channel are interleaved:
  193. void stream_cb(float* buffer, int num_frames, int num_channels) {
  194. assert(2 == num_channels);
  195. for (int i = 0; i < num_frames; i++) {
  196. buffer[2*i + 0] = ...; // left channel
  197. buffer[2*i + 1] = ...; // right channel
  198. }
  199. }
  200. Please keep in mind that the stream callback function is running in a
  201. separate thread, if you need to share data with the main thread you need
  202. to take care yourself to make the access to the shared data thread-safe!
  203. THE PUSH MODEL
  204. ==============
  205. To use the push-model for providing audio data, simply don't set (keep
  206. zero-initialized) the stream_cb field in the saudio_desc struct when
  207. calling saudio_setup().
  208. To provide sample data with the push model, call the saudio_push()
  209. function at regular intervals (for instance once per frame). You can
  210. call the saudio_expect() function to ask Sokol Audio how much room is
  211. in the ring buffer, but if you provide a continuous stream of data
  212. at the right sample rate, saudio_expect() isn't required (it's a simple
  213. way to sync/throttle your sample generation code with the playback
  214. rate though).
  215. With saudio_push() you may need to maintain your own intermediate sample
  216. buffer, since pushing individual sample values isn't very efficient.
  217. The following example is from the MOD player sample in
  218. sokol-samples (https://github.com/floooh/sokol-samples):
  219. const int num_frames = saudio_expect();
  220. if (num_frames > 0) {
  221. const int num_samples = num_frames * saudio_channels();
  222. read_samples(flt_buf, num_samples);
  223. saudio_push(flt_buf, num_frames);
  224. }
  225. Another option is to ignore saudio_expect(), and just push samples as they
  226. are generated in small batches. In this case you *need* to generate the
  227. samples at the right sample rate:
  228. The following example is taken from the Tiny Emulators project
  229. (https://github.com/floooh/chips-test), this is for mono playback,
  230. so (num_samples == num_frames):
  231. // tick the sound generator
  232. if (ay38910_tick(&sys->psg)) {
  233. // new sample is ready
  234. sys->sample_buffer[sys->sample_pos++] = sys->psg.sample;
  235. if (sys->sample_pos == sys->num_samples) {
  236. // new sample packet is ready
  237. saudio_push(sys->sample_buffer, sys->num_samples);
  238. sys->sample_pos = 0;
  239. }
  240. }
  241. THE WEBAUDIO BACKEND
  242. ====================
  243. The WebAudio backend is currently using a ScriptProcessorNode callback to
  244. feed the sample data into WebAudio. ScriptProcessorNode has been
  245. deprecated for a while because it is running from the main thread, with
  246. the default initialization parameters it works 'pretty well' though.
  247. Ultimately Sokol Audio will use Audio Worklets, but this requires a few
  248. more things to fall into place (Audio Worklets implemented everywhere,
  249. SharedArrayBuffers enabled again, and I need to figure out a 'low-cost'
  250. solution in terms of implementation effort, since Audio Worklets are
  251. a lot more complex than ScriptProcessorNode if the audio data needs to come
  252. from the main thread).
  253. The WebAudio backend is automatically selected when compiling for
  254. emscripten (__EMSCRIPTEN__ define exists).
  255. https://developers.google.com/web/updates/2017/12/audio-worklet
  256. https://developers.google.com/web/updates/2018/06/audio-worklet-design-pattern
  257. "Blob URLs": https://www.html5rocks.com/en/tutorials/workers/basics/
  258. Also see: https://blog.paul.cx/post/a-wait-free-spsc-ringbuffer-for-the-web/
  259. THE COREAUDIO BACKEND
  260. =====================
  261. The CoreAudio backend is selected on macOS and iOS (__APPLE__ is defined).
  262. Since the CoreAudio API is implemented in C (not Objective-C) on macOS the
  263. implementation part of Sokol Audio can be included into a C source file.
  264. However on iOS, Sokol Audio must be compiled as Objective-C due to it's
  265. reliance on the AVAudioSession object. The iOS code path support both
  266. being compiled with or without ARC (Automatic Reference Counting).
  267. For thread synchronisation, the CoreAudio backend will use the
  268. pthread_mutex_* functions.
  269. The incoming floating point samples will be directly forwarded to
  270. CoreAudio without further conversion.
  271. macOS and iOS applications that use Sokol Audio need to link with
  272. the AudioToolbox framework.
  273. THE WASAPI BACKEND
  274. ==================
  275. The WASAPI backend is automatically selected when compiling on Windows
  276. (_WIN32 is defined).
  277. For thread synchronisation a Win32 critical section is used.
  278. WASAPI may use a different size for its own streaming buffer then requested,
  279. so the base latency may be slightly bigger. The current backend implementation
  280. converts the incoming floating point sample values to signed 16-bit
  281. integers.
  282. The required Windows system DLLs are linked with #pragma comment(lib, ...),
  283. so you shouldn't need to add additional linker libs in the build process
  284. (otherwise this is a bug which should be fixed in sokol_audio.h).
  285. THE ALSA BACKEND
  286. ================
  287. The ALSA backend is automatically selected when compiling on Linux
  288. ('linux' is defined).
  289. For thread synchronisation, the pthread_mutex_* functions are used.
  290. Samples are directly forwarded to ALSA in 32-bit float format, no
  291. further conversion is taking place.
  292. You need to link with the 'asound' library, and the <alsa/asoundlib.h>
  293. header must be present (usually both are installed with some sort
  294. of ALSA development package).
  295. THE VITA BACKEND
  296. ================
  297. The VITA backend is automatically selected when compiling with vitasdk
  298. ('PSP2_SDK_VERSION' is defined).
  299. For thread synchronisation, the pthread_mutex_* functions are used.
  300. Samples are converted from float to short (uint16_t) to maintain
  301. all the same interface/api as other platforms.
  302. You may use any supported sample rate you wish, but all audio MUST
  303. match the same sample rate you choose.
  304. This uses the "BGM" port to allow selecting the sample rate ("Main"
  305. port is restricted to 48000 only).
  306. You need to link with the 'SceAudio' library, and the <psp2/audioout.h>
  307. header must be present (usually both are installed with the vitasdk).
  308. THE 3DS BACKEND
  309. ================
  310. The 3DS backend is automatically selected when compiling with libctru
  311. ('__3DS__' is defined).
  312. Running a separate thread on the older 3ds is not a good idea and I
  313. was not able to get it working without slowing down the main thread
  314. too much (it has a single core available with cooperative threads).
  315. The NDSP seems to work better by using its ndspSetCallback method.
  316. You may use any supported sample rate you wish, but all audio MUST
  317. match the same sample rate you choose or it will sound slowed down
  318. or sped up.
  319. The queue size and other NDSP specific parameters can be chosen by
  320. the provided 'saudio_n3ds_desc' type. Defaults will be used if
  321. nothing is provided.
  322. There is a known issue of a noticeable delay when starting a new
  323. sound on emulators. I was not able to improve this to my liking
  324. and ~300ms can be expected. This can be improved by using a lower
  325. buffer size than the 2048 default but I would not suggest under
  326. 1536. It may crash under 1408, and they must be in multiples of 128.
  327. Note: I was NOT able to reproduce this issue on a real device and
  328. the audio worked perfectly.
  329. MEMORY ALLOCATION OVERRIDE
  330. ==========================
  331. You can override the memory allocation functions at initialization time
  332. like this:
  333. void* my_alloc(size_t size, void* user_data) {
  334. return malloc(size);
  335. }
  336. void my_free(void* ptr, void* user_data) {
  337. free(ptr);
  338. }
  339. ...
  340. saudio_setup(&(saudio_desc){
  341. // ...
  342. .allocator = {
  343. .alloc_fn = my_alloc,
  344. .free_fn = my_free,
  345. .user_data = ...,
  346. }
  347. });
  348. ...
  349. If no overrides are provided, malloc and free will be used.
  350. This only affects memory allocation calls done by sokol_audio.h
  351. itself though, not any allocations in OS libraries.
  352. Memory allocation will only happen on the same thread where saudio_setup()
  353. was called, so you don't need to worry about thread-safety.
  354. ERROR REPORTING AND LOGGING
  355. ===========================
  356. To get any logging information at all you need to provide a logging callback in the setup call
  357. the easiest way is to use sokol_log.h:
  358. #include "sokol_log.h"
  359. saudio_setup(&(saudio_desc){ .logger.func = slog_func });
  360. To override logging with your own callback, first write a logging function like this:
  361. void my_log(const char* tag, // e.g. 'saudio'
  362. uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
  363. uint32_t log_item_id, // SAUDIO_LOGITEM_*
  364. const char* message_or_null, // a message string, may be nullptr in release mode
  365. uint32_t line_nr, // line number in sokol_audio.h
  366. const char* filename_or_null, // source filename, may be nullptr in release mode
  367. void* user_data)
  368. {
  369. ...
  370. }
  371. ...and then setup sokol-audio like this:
  372. saudio_setup(&(saudio_desc){
  373. .logger = {
  374. .func = my_log,
  375. .user_data = my_user_data,
  376. }
  377. });
  378. The provided logging function must be reentrant (e.g. be callable from
  379. different threads).
  380. If you don't want to provide your own custom logger it is highly recommended to use
  381. the standard logger in sokol_log.h instead, otherwise you won't see any warnings or
  382. errors.
  383. LICENSE
  384. =======
  385. zlib/libpng license
  386. Copyright (c) 2018 Andre Weissflog
  387. This software is provided 'as-is', without any express or implied warranty.
  388. In no event will the authors be held liable for any damages arising from the
  389. use of this software.
  390. Permission is granted to anyone to use this software for any purpose,
  391. including commercial applications, and to alter it and redistribute it
  392. freely, subject to the following restrictions:
  393. 1. The origin of this software must not be misrepresented; you must not
  394. claim that you wrote the original software. If you use this software in a
  395. product, an acknowledgment in the product documentation would be
  396. appreciated but is not required.
  397. 2. Altered source versions must be plainly marked as such, and must not
  398. be misrepresented as being the original software.
  399. 3. This notice may not be removed or altered from any source
  400. distribution.
  401. */
  402. import "core:c"
  403. _ :: c
  404. SOKOL_DEBUG :: #config(SOKOL_DEBUG, ODIN_DEBUG)
  405. DEBUG :: #config(SOKOL_AUDIO_DEBUG, SOKOL_DEBUG)
  406. USE_GL :: #config(SOKOL_USE_GL, false)
  407. USE_DLL :: #config(SOKOL_DLL, false)
  408. when ODIN_OS == .Windows {
  409. when USE_DLL {
  410. when USE_GL {
  411. when DEBUG { foreign import sokol_audio_clib { "../sokol_dll_windows_x64_gl_debug.lib" } }
  412. else { foreign import sokol_audio_clib { "../sokol_dll_windows_x64_gl_release.lib" } }
  413. } else {
  414. when DEBUG { foreign import sokol_audio_clib { "../sokol_dll_windows_x64_d3d11_debug.lib" } }
  415. else { foreign import sokol_audio_clib { "../sokol_dll_windows_x64_d3d11_release.lib" } }
  416. }
  417. } else {
  418. when USE_GL {
  419. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_windows_x64_gl_debug.lib" } }
  420. else { foreign import sokol_audio_clib { "sokol_audio_windows_x64_gl_release.lib" } }
  421. } else {
  422. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_windows_x64_d3d11_debug.lib" } }
  423. else { foreign import sokol_audio_clib { "sokol_audio_windows_x64_d3d11_release.lib" } }
  424. }
  425. }
  426. } else when ODIN_OS == .Darwin {
  427. when USE_DLL {
  428. when USE_GL && ODIN_ARCH == .arm64 && DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_arm64_gl_debug.dylib" } }
  429. else when USE_GL && ODIN_ARCH == .arm64 && !DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_arm64_gl_release.dylib" } }
  430. else when USE_GL && ODIN_ARCH == .amd64 && DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_x64_gl_debug.dylib" } }
  431. else when USE_GL && ODIN_ARCH == .amd64 && !DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_x64_gl_release.dylib" } }
  432. else when !USE_GL && ODIN_ARCH == .arm64 && DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_arm64_metal_debug.dylib" } }
  433. else when !USE_GL && ODIN_ARCH == .arm64 && !DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_arm64_metal_release.dylib" } }
  434. else when !USE_GL && ODIN_ARCH == .amd64 && DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_x64_metal_debug.dylib" } }
  435. else when !USE_GL && ODIN_ARCH == .amd64 && !DEBUG { foreign import sokol_audio_clib { "../dylib/sokol_dylib_macos_x64_metal_release.dylib" } }
  436. } else {
  437. when USE_GL {
  438. when ODIN_ARCH == .arm64 {
  439. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_macos_arm64_gl_debug.a", "system:AudioToolbox.framework" } }
  440. else { foreign import sokol_audio_clib { "sokol_audio_macos_arm64_gl_release.a", "system:AudioToolbox.framework" } }
  441. } else {
  442. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_macos_x64_gl_debug.a", "system:AudioToolbox.framework" } }
  443. else { foreign import sokol_audio_clib { "sokol_audio_macos_x64_gl_release.a", "system:AudioToolbox.framework" } }
  444. }
  445. } else {
  446. when ODIN_ARCH == .arm64 {
  447. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_macos_arm64_metal_debug.a", "system:AudioToolbox.framework" } }
  448. else { foreign import sokol_audio_clib { "sokol_audio_macos_arm64_metal_release.a", "system:AudioToolbox.framework" } }
  449. } else {
  450. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_macos_x64_metal_debug.a", "system:AudioToolbox.framework" } }
  451. else { foreign import sokol_audio_clib { "sokol_audio_macos_x64_metal_release.a", "system:AudioToolbox.framework" } }
  452. }
  453. }
  454. }
  455. } else when ODIN_OS == .Linux {
  456. when USE_DLL {
  457. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_linux_x64_gl_debug.so", "system:dl", "system:pthread" } }
  458. else { foreign import sokol_audio_clib { "sokol_audio_linux_x64_gl_release.so", "system:dl", "system:pthread" } }
  459. } else {
  460. when DEBUG { foreign import sokol_audio_clib { "sokol_audio_linux_x64_gl_debug.a", "system:asound", "system:dl", "system:pthread" } }
  461. else { foreign import sokol_audio_clib { "sokol_audio_linux_x64_gl_release.a", "system:asound", "system:dl", "system:pthread" } }
  462. }
  463. } else when ODIN_ARCH == .wasm32 || ODIN_ARCH == .wasm64p32 {
  464. // Feed sokol_audio_wasm_gl_debug.a or sokol_audio_wasm_gl_release.a into emscripten compiler.
  465. foreign import sokol_audio_clib { "env.o" }
  466. } else {
  467. #panic("This OS is currently not supported")
  468. }
  469. @(default_calling_convention="c", link_prefix="saudio_")
  470. foreign sokol_audio_clib {
  471. // setup sokol-audio
  472. setup :: proc(#by_ptr desc: Desc) ---
  473. // shutdown sokol-audio
  474. shutdown :: proc() ---
  475. // true after setup if audio backend was successfully initialized
  476. isvalid :: proc() -> bool ---
  477. // return the saudio_desc.user_data pointer
  478. userdata :: proc() -> rawptr ---
  479. // return a copy of the original saudio_desc struct
  480. query_desc :: proc() -> Desc ---
  481. // actual sample rate
  482. sample_rate :: proc() -> c.int ---
  483. // return actual backend buffer size in number of frames
  484. buffer_frames :: proc() -> c.int ---
  485. // actual number of channels
  486. channels :: proc() -> c.int ---
  487. // return true if audio context is currently suspended (only in WebAudio backend, all other backends return false)
  488. suspended :: proc() -> bool ---
  489. // get current number of frames to fill packet queue
  490. expect :: proc() -> c.int ---
  491. // push sample frames from main thread, returns number of frames actually pushed
  492. push :: proc(frames: ^f32, #any_int num_frames: c.int) -> c.int ---
  493. }
  494. Log_Item :: enum i32 {
  495. OK,
  496. MALLOC_FAILED,
  497. ALSA_SND_PCM_OPEN_FAILED,
  498. ALSA_FLOAT_SAMPLES_NOT_SUPPORTED,
  499. ALSA_REQUESTED_BUFFER_SIZE_NOT_SUPPORTED,
  500. ALSA_REQUESTED_CHANNEL_COUNT_NOT_SUPPORTED,
  501. ALSA_SND_PCM_HW_PARAMS_SET_RATE_NEAR_FAILED,
  502. ALSA_SND_PCM_HW_PARAMS_FAILED,
  503. ALSA_PTHREAD_CREATE_FAILED,
  504. WASAPI_CREATE_EVENT_FAILED,
  505. WASAPI_CREATE_DEVICE_ENUMERATOR_FAILED,
  506. WASAPI_GET_DEFAULT_AUDIO_ENDPOINT_FAILED,
  507. WASAPI_DEVICE_ACTIVATE_FAILED,
  508. WASAPI_AUDIO_CLIENT_INITIALIZE_FAILED,
  509. WASAPI_AUDIO_CLIENT_GET_BUFFER_SIZE_FAILED,
  510. WASAPI_AUDIO_CLIENT_GET_SERVICE_FAILED,
  511. WASAPI_AUDIO_CLIENT_SET_EVENT_HANDLE_FAILED,
  512. WASAPI_CREATE_THREAD_FAILED,
  513. AAUDIO_STREAMBUILDER_OPEN_STREAM_FAILED,
  514. AAUDIO_PTHREAD_CREATE_FAILED,
  515. AAUDIO_RESTARTING_STREAM_AFTER_ERROR,
  516. USING_AAUDIO_BACKEND,
  517. AAUDIO_CREATE_STREAMBUILDER_FAILED,
  518. COREAUDIO_NEW_OUTPUT_FAILED,
  519. COREAUDIO_ALLOCATE_BUFFER_FAILED,
  520. COREAUDIO_START_FAILED,
  521. BACKEND_BUFFER_SIZE_ISNT_MULTIPLE_OF_PACKET_SIZE,
  522. VITA_SCEAUDIO_OPEN_FAILED,
  523. VITA_PTHREAD_CREATE_FAILED,
  524. N3DS_NDSP_OPEN_FAILED,
  525. }
  526. /*
  527. saudio_logger
  528. Used in saudio_desc to provide a custom logging and error reporting
  529. callback to sokol-audio.
  530. */
  531. Logger :: struct {
  532. func : proc "c" (a0: cstring, a1: u32, a2: u32, a3: cstring, a4: u32, a5: cstring, a6: rawptr),
  533. user_data : rawptr,
  534. }
  535. /*
  536. saudio_allocator
  537. Used in saudio_desc to provide custom memory-alloc and -free functions
  538. to sokol_audio.h. If memory management should be overridden, both the
  539. alloc_fn and free_fn function must be provided (e.g. it's not valid to
  540. override one function but not the other).
  541. */
  542. Allocator :: struct {
  543. alloc_fn : proc "c" (a0: c.size_t, a1: rawptr) -> rawptr,
  544. free_fn : proc "c" (a0: rawptr, a1: rawptr),
  545. user_data : rawptr,
  546. }
  547. N3ds_Ndspinterptype :: enum i32 {
  548. DSP_INTERP_POLYPHASE = 0,
  549. DSP_INTERP_LINEAR = 1,
  550. DSP_INTERP_NONE = 2,
  551. }
  552. N3ds_Desc :: struct {
  553. queue_count : c.int,
  554. interpolation_type : N3ds_Ndspinterptype,
  555. channel_id : c.int,
  556. }
  557. Desc :: struct {
  558. sample_rate : c.int,
  559. num_channels : c.int,
  560. buffer_frames : c.int,
  561. packet_frames : c.int,
  562. num_packets : c.int,
  563. stream_cb : proc "c" (a0: ^f32, a1: c.int, a2: c.int),
  564. stream_userdata_cb : proc "c" (a0: ^f32, a1: c.int, a2: c.int, a3: rawptr),
  565. user_data : rawptr,
  566. n3ds : N3ds_Desc,
  567. allocator : Allocator,
  568. logger : Logger,
  569. }