@xincli/opentui-core 0.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (476) hide show
  1. package/README.md +63 -0
  2. package/dev/keypress-debug-renderer.ts +148 -0
  3. package/dev/keypress-debug.ts +43 -0
  4. package/dev/print-env-vars.ts +32 -0
  5. package/dev/test-tmux-graphics-334.sh +68 -0
  6. package/dev/test-tmux-theme-queries.sh +151 -0
  7. package/dev/thai-debug-test.ts +68 -0
  8. package/dev/theme-mode.ts +290 -0
  9. package/docs/development.md +144 -0
  10. package/package.json +102 -0
  11. package/prebuilt/aarch64-android/libopentui.so +0 -0
  12. package/scripts/build-android.ts +226 -0
  13. package/scripts/build-native-termux.sh +525 -0
  14. package/scripts/build.ts +530 -0
  15. package/scripts/dist-test.ts +312 -0
  16. package/scripts/package-prebuilt.ts +110 -0
  17. package/scripts/publish.ts +60 -0
  18. package/scripts/test-node-hook.mjs +16 -0
  19. package/scripts/test-node.ts +241 -0
  20. package/scripts/vendor-deps.sh +73 -0
  21. package/src/NativeSpanFeed.ts +375 -0
  22. package/src/Renderable.ts +1878 -0
  23. package/src/__snapshots__/buffer.test.ts.snap +28 -0
  24. package/src/animation/Timeline.test.ts +2709 -0
  25. package/src/animation/Timeline.ts +598 -0
  26. package/src/ansi.ts +22 -0
  27. package/src/audio.ts +479 -0
  28. package/src/benchmark/attenuation-benchmark.ts +81 -0
  29. package/src/benchmark/audio-playback-benchmark.ts +344 -0
  30. package/src/benchmark/box-draw-benchmark.ts +1042 -0
  31. package/src/benchmark/colormatrix-benchmark.ts +123 -0
  32. package/src/benchmark/gain-benchmark.ts +80 -0
  33. package/src/benchmark/latest-all-bench-run.json +707 -0
  34. package/src/benchmark/latest-async-bench-run.json +336 -0
  35. package/src/benchmark/latest-default-bench-run.json +657 -0
  36. package/src/benchmark/latest-large-bench-run.json +707 -0
  37. package/src/benchmark/latest-quick-bench-run.json +207 -0
  38. package/src/benchmark/layout-benchmark.ts +2547 -0
  39. package/src/benchmark/markdown-benchmark.ts +1796 -0
  40. package/src/benchmark/native-span-feed-async-benchmark.ts +355 -0
  41. package/src/benchmark/native-span-feed-benchmark.md +56 -0
  42. package/src/benchmark/native-span-feed-benchmark.ts +596 -0
  43. package/src/benchmark/native-span-feed-compare.ts +280 -0
  44. package/src/benchmark/render-traversal-benchmark.ts +928 -0
  45. package/src/benchmark/text-buffer-render-benchmark.ts +874 -0
  46. package/src/benchmark/text-table-benchmark.ts +948 -0
  47. package/src/buffer.test.ts +292 -0
  48. package/src/buffer.ts +564 -0
  49. package/src/console.test.ts +629 -0
  50. package/src/console.ts +1253 -0
  51. package/src/edit-buffer.test.ts +1852 -0
  52. package/src/edit-buffer.ts +417 -0
  53. package/src/editor-view.test.ts +1056 -0
  54. package/src/editor-view.ts +290 -0
  55. package/src/index.ts +25 -0
  56. package/src/lib/KeyHandler.integration.test.ts +292 -0
  57. package/src/lib/KeyHandler.stopPropagation.test.ts +289 -0
  58. package/src/lib/KeyHandler.test.ts +662 -0
  59. package/src/lib/KeyHandler.ts +222 -0
  60. package/src/lib/RGBA.test.ts +60 -0
  61. package/src/lib/RGBA.ts +343 -0
  62. package/src/lib/ascii.font.ts +330 -0
  63. package/src/lib/border.test.ts +83 -0
  64. package/src/lib/border.ts +170 -0
  65. package/src/lib/bunfs.test.ts +27 -0
  66. package/src/lib/bunfs.ts +18 -0
  67. package/src/lib/clipboard.test.ts +63 -0
  68. package/src/lib/clipboard.ts +41 -0
  69. package/src/lib/clock.ts +35 -0
  70. package/src/lib/data-paths.test.ts +133 -0
  71. package/src/lib/data-paths.ts +109 -0
  72. package/src/lib/debounce.ts +106 -0
  73. package/src/lib/detect-links.test.ts +98 -0
  74. package/src/lib/detect-links.ts +56 -0
  75. package/src/lib/env.test.ts +228 -0
  76. package/src/lib/env.ts +209 -0
  77. package/src/lib/extmarks-history.ts +51 -0
  78. package/src/lib/extmarks-multiwidth.test.ts +324 -0
  79. package/src/lib/extmarks.test.ts +3547 -0
  80. package/src/lib/extmarks.ts +861 -0
  81. package/src/lib/fonts/block.json +405 -0
  82. package/src/lib/fonts/grid.json +265 -0
  83. package/src/lib/fonts/huge.json +741 -0
  84. package/src/lib/fonts/pallet.json +314 -0
  85. package/src/lib/fonts/shade.json +591 -0
  86. package/src/lib/fonts/slick.json +321 -0
  87. package/src/lib/fonts/tiny.json +69 -0
  88. package/src/lib/hast-styled-text.ts +59 -0
  89. package/src/lib/index.ts +22 -0
  90. package/src/lib/keybinding.internal.test.ts +342 -0
  91. package/src/lib/keybinding.internal.ts +182 -0
  92. package/src/lib/objects-in-viewport.test.ts +789 -0
  93. package/src/lib/objects-in-viewport.ts +153 -0
  94. package/src/lib/output.capture.ts +58 -0
  95. package/src/lib/parse.keypress-kitty.protocol.test.ts +347 -0
  96. package/src/lib/parse.keypress-kitty.test.ts +712 -0
  97. package/src/lib/parse.keypress-kitty.ts +470 -0
  98. package/src/lib/parse.keypress.test.ts +2020 -0
  99. package/src/lib/parse.keypress.ts +480 -0
  100. package/src/lib/parse.mouse.test.ts +552 -0
  101. package/src/lib/parse.mouse.ts +232 -0
  102. package/src/lib/paste.ts +18 -0
  103. package/src/lib/queue.ts +65 -0
  104. package/src/lib/render-geometry.ts +36 -0
  105. package/src/lib/renderable.validations.test.ts +87 -0
  106. package/src/lib/renderable.validations.ts +83 -0
  107. package/src/lib/scroll-acceleration.ts +98 -0
  108. package/src/lib/selection.ts +261 -0
  109. package/src/lib/singleton.ts +34 -0
  110. package/src/lib/stdin-parser.test.ts +2439 -0
  111. package/src/lib/stdin-parser.ts +2007 -0
  112. package/src/lib/styled-text.ts +178 -0
  113. package/src/lib/terminal-capability-detection.test.ts +226 -0
  114. package/src/lib/terminal-capability-detection.ts +98 -0
  115. package/src/lib/terminal-palette.test.ts +954 -0
  116. package/src/lib/terminal-palette.ts +441 -0
  117. package/src/lib/tree-sitter/assets/README.md +119 -0
  118. package/src/lib/tree-sitter/assets/javascript/highlights.scm +205 -0
  119. package/src/lib/tree-sitter/assets/javascript/tree-sitter-javascript.wasm +0 -0
  120. package/src/lib/tree-sitter/assets/markdown/highlights.scm +150 -0
  121. package/src/lib/tree-sitter/assets/markdown/injections.scm +27 -0
  122. package/src/lib/tree-sitter/assets/markdown/tree-sitter-markdown.wasm +0 -0
  123. package/src/lib/tree-sitter/assets/markdown_inline/highlights.scm +115 -0
  124. package/src/lib/tree-sitter/assets/markdown_inline/tree-sitter-markdown_inline.wasm +0 -0
  125. package/src/lib/tree-sitter/assets/typescript/highlights.scm +604 -0
  126. package/src/lib/tree-sitter/assets/typescript/tree-sitter-typescript.wasm +0 -0
  127. package/src/lib/tree-sitter/assets/update.ts +360 -0
  128. package/src/lib/tree-sitter/assets/zig/highlights.scm +284 -0
  129. package/src/lib/tree-sitter/assets/zig/tree-sitter-zig.wasm +0 -0
  130. package/src/lib/tree-sitter/assets.d.ts +9 -0
  131. package/src/lib/tree-sitter/cache.test.ts +291 -0
  132. package/src/lib/tree-sitter/client.test.ts +1434 -0
  133. package/src/lib/tree-sitter/client.ts +847 -0
  134. package/src/lib/tree-sitter/default-parsers.ts +133 -0
  135. package/src/lib/tree-sitter/download-utils.ts +148 -0
  136. package/src/lib/tree-sitter/index.ts +75 -0
  137. package/src/lib/tree-sitter/parser.worker.ts +1098 -0
  138. package/src/lib/tree-sitter/parsers-config.ts +81 -0
  139. package/src/lib/tree-sitter/resolve-ft.test.ts +55 -0
  140. package/src/lib/tree-sitter/resolve-ft.ts +189 -0
  141. package/src/lib/tree-sitter/types.ts +132 -0
  142. package/src/lib/tree-sitter/update-assets.ts +10 -0
  143. package/src/lib/tree-sitter-styled-text.test.ts +1253 -0
  144. package/src/lib/tree-sitter-styled-text.ts +314 -0
  145. package/src/lib/validate-dir-name.ts +55 -0
  146. package/src/lib/yoga.options.test.ts +628 -0
  147. package/src/lib/yoga.options.ts +346 -0
  148. package/src/native-event-worker-repro.test.ts +48 -0
  149. package/src/native-event-worker-repro.worker.ts +9 -0
  150. package/src/native-handle.test.ts +221 -0
  151. package/src/platform/ffi.test.ts +516 -0
  152. package/src/platform/ffi.ts +653 -0
  153. package/src/platform/runtime.test.ts +52 -0
  154. package/src/platform/runtime.ts +121 -0
  155. package/src/platform/test.ts +45 -0
  156. package/src/platform/worker-handler-cleanup.fixture.ts +12 -0
  157. package/src/platform/worker-onmessage-startup.fixture.ts +10 -0
  158. package/src/platform/worker-startup.fixture.ts +9 -0
  159. package/src/platform/worker.node-test.ts +153 -0
  160. package/src/platform/worker.ts +425 -0
  161. package/src/plugins/core-slot.ts +579 -0
  162. package/src/plugins/registry.ts +402 -0
  163. package/src/plugins/types.ts +46 -0
  164. package/src/post/effects.ts +948 -0
  165. package/src/post/filters.ts +512 -0
  166. package/src/post/matrices.ts +288 -0
  167. package/src/renderables/ASCIIFont.ts +219 -0
  168. package/src/renderables/Box.test.ts +474 -0
  169. package/src/renderables/Box.ts +353 -0
  170. package/src/renderables/Code.test.ts +2388 -0
  171. package/src/renderables/Code.ts +555 -0
  172. package/src/renderables/Diff.regression.test.ts +381 -0
  173. package/src/renderables/Diff.test.ts +3212 -0
  174. package/src/renderables/Diff.ts +1278 -0
  175. package/src/renderables/EditBufferRenderable.test.ts +288 -0
  176. package/src/renderables/EditBufferRenderable.ts +1152 -0
  177. package/src/renderables/FrameBuffer.ts +47 -0
  178. package/src/renderables/Input.test.ts +1310 -0
  179. package/src/renderables/Input.ts +273 -0
  180. package/src/renderables/LineNumberRenderable.ts +725 -0
  181. package/src/renderables/Markdown.ts +2144 -0
  182. package/src/renderables/ScrollBar.ts +423 -0
  183. package/src/renderables/ScrollBox.ts +903 -0
  184. package/src/renderables/Select.test.ts +1134 -0
  185. package/src/renderables/Select.ts +535 -0
  186. package/src/renderables/Slider.test.ts +456 -0
  187. package/src/renderables/Slider.ts +342 -0
  188. package/src/renderables/TabSelect.test.ts +238 -0
  189. package/src/renderables/TabSelect.ts +447 -0
  190. package/src/renderables/Text.selection-buffer.test.ts +118 -0
  191. package/src/renderables/Text.test.ts +2662 -0
  192. package/src/renderables/Text.ts +147 -0
  193. package/src/renderables/TextBufferRenderable.ts +504 -0
  194. package/src/renderables/TextNode.test.ts +1137 -0
  195. package/src/renderables/TextNode.ts +379 -0
  196. package/src/renderables/TextTable.test.ts +1411 -0
  197. package/src/renderables/TextTable.ts +1434 -0
  198. package/src/renderables/Textarea.ts +425 -0
  199. package/src/renderables/TimeToFirstDraw.ts +89 -0
  200. package/src/renderables/__snapshots__/Code.test.ts.snap +13 -0
  201. package/src/renderables/__snapshots__/Diff.test.ts.snap +785 -0
  202. package/src/renderables/__snapshots__/Text.test.ts.snap +421 -0
  203. package/src/renderables/__snapshots__/TextTable.test.ts.snap +215 -0
  204. package/src/renderables/__tests__/Code.test.ts +55 -0
  205. package/src/renderables/__tests__/LineNumberRenderable.scrollbox-simple.test.ts +144 -0
  206. package/src/renderables/__tests__/LineNumberRenderable.scrollbox.test.ts +827 -0
  207. package/src/renderables/__tests__/LineNumberRenderable.test.ts +1906 -0
  208. package/src/renderables/__tests__/LineNumberRenderable.wrapping.test.ts +85 -0
  209. package/src/renderables/__tests__/Markdown.code-colors.test.ts +288 -0
  210. package/src/renderables/__tests__/Markdown.test.ts +4175 -0
  211. package/src/renderables/__tests__/MultiRenderable.selection.test.ts +87 -0
  212. package/src/renderables/__tests__/Textarea.buffer.test.ts +683 -0
  213. package/src/renderables/__tests__/Textarea.destroyed-events.test.ts +675 -0
  214. package/src/renderables/__tests__/Textarea.editing.test.ts +2120 -0
  215. package/src/renderables/__tests__/Textarea.error-handling.test.ts +35 -0
  216. package/src/renderables/__tests__/Textarea.events.test.ts +738 -0
  217. package/src/renderables/__tests__/Textarea.highlights.test.ts +590 -0
  218. package/src/renderables/__tests__/Textarea.keybinding.test.ts +3275 -0
  219. package/src/renderables/__tests__/Textarea.paste.test.ts +357 -0
  220. package/src/renderables/__tests__/Textarea.rendering.test.ts +1927 -0
  221. package/src/renderables/__tests__/Textarea.scroll.test.ts +733 -0
  222. package/src/renderables/__tests__/Textarea.selection.test.ts +1589 -0
  223. package/src/renderables/__tests__/Textarea.stress.test.ts +670 -0
  224. package/src/renderables/__tests__/Textarea.undo-redo.test.ts +383 -0
  225. package/src/renderables/__tests__/Textarea.visual-lines.test.ts +310 -0
  226. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.code.test.ts.snap +221 -0
  227. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox-simple.test.ts.snap +89 -0
  228. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.scrollbox.test.ts.snap +457 -0
  229. package/src/renderables/__tests__/__snapshots__/LineNumberRenderable.test.ts.snap +158 -0
  230. package/src/renderables/__tests__/__snapshots__/Textarea.rendering.test.ts.snap +387 -0
  231. package/src/renderables/__tests__/markdown-parser.test.ts +241 -0
  232. package/src/renderables/__tests__/renderable-test-utils.ts +60 -0
  233. package/src/renderables/composition/README.md +8 -0
  234. package/src/renderables/composition/VRenderable.ts +32 -0
  235. package/src/renderables/composition/constructs.ts +127 -0
  236. package/src/renderables/composition/vnode.ts +277 -0
  237. package/src/renderables/index.ts +23 -0
  238. package/src/renderables/markdown-parser.ts +79 -0
  239. package/src/renderer-theme-mode.ts +199 -0
  240. package/src/renderer.ts +4975 -0
  241. package/src/runtime-plugin-support-configure.ts +63 -0
  242. package/src/runtime-plugin-support.ts +13 -0
  243. package/src/runtime-plugin.ts +626 -0
  244. package/src/specs/terminal-startup.md +64 -0
  245. package/src/syntax-style.test.ts +841 -0
  246. package/src/syntax-style.ts +274 -0
  247. package/src/testing/README.md +219 -0
  248. package/src/testing/bun-test-node.ts +880 -0
  249. package/src/testing/capture-spans.test.ts +216 -0
  250. package/src/testing/integration.test.ts +276 -0
  251. package/src/testing/manual-clock.ts +117 -0
  252. package/src/testing/mock-keys.test.ts +1378 -0
  253. package/src/testing/mock-keys.ts +457 -0
  254. package/src/testing/mock-mouse.test.ts +218 -0
  255. package/src/testing/mock-mouse.ts +247 -0
  256. package/src/testing/mock-tree-sitter-client.ts +81 -0
  257. package/src/testing/spy.ts +13 -0
  258. package/src/testing/terminal-capabilities.ts +47 -0
  259. package/src/testing/test-recorder.test.ts +410 -0
  260. package/src/testing/test-recorder.ts +133 -0
  261. package/src/testing/test-renderer.ts +375 -0
  262. package/src/testing/test-renderer.wait.test.ts +278 -0
  263. package/src/testing/test-streams.ts +31 -0
  264. package/src/testing.ts +9 -0
  265. package/src/tests/__snapshots__/absolute-positioning.snapshot.test.ts.snap +481 -0
  266. package/src/tests/__snapshots__/renderable.snapshot.test.ts.snap +19 -0
  267. package/src/tests/__snapshots__/scrollbox.test.ts.snap +29 -0
  268. package/src/tests/absolute-positioning.snapshot.test.ts +638 -0
  269. package/src/tests/allocator-stats.test.ts +53 -0
  270. package/src/tests/audio.test.ts +303 -0
  271. package/src/tests/destroy-during-render.test.ts +200 -0
  272. package/src/tests/destroy-on-exit.fixture.ts +36 -0
  273. package/src/tests/destroy-on-exit.test.ts +62 -0
  274. package/src/tests/ffi-borrowed-pointer-callsites.test.ts +153 -0
  275. package/src/tests/hover-cursor.test.ts +98 -0
  276. package/src/tests/native-backed-measurement-lifecycle.test.ts +133 -0
  277. package/src/tests/native-backed-measurement-parity.test.ts +322 -0
  278. package/src/tests/native-span-feed-async.test.ts +173 -0
  279. package/src/tests/native-span-feed-close.test.ts +120 -0
  280. package/src/tests/native-span-feed-coverage.test.ts +227 -0
  281. package/src/tests/native-span-feed-edge-cases.test.ts +384 -0
  282. package/src/tests/native-span-feed-use-after-free.test.ts +45 -0
  283. package/src/tests/opacity.test.ts +123 -0
  284. package/src/tests/renderable.snapshot.test.ts +524 -0
  285. package/src/tests/renderable.test.ts +1851 -0
  286. package/src/tests/renderer.clock.test.ts +270 -0
  287. package/src/tests/renderer.console-startup.test.ts +2151 -0
  288. package/src/tests/renderer.control.test.ts +458 -0
  289. package/src/tests/renderer.core-slot-binding.test.ts +962 -0
  290. package/src/tests/renderer.cursor.test.ts +26 -0
  291. package/src/tests/renderer.custom-stdout.test.ts +1112 -0
  292. package/src/tests/renderer.destroy-during-render.test.ts +147 -0
  293. package/src/tests/renderer.focus-restore.test.ts +257 -0
  294. package/src/tests/renderer.focus.test.ts +329 -0
  295. package/src/tests/renderer.idle.test.ts +219 -0
  296. package/src/tests/renderer.input.test.ts +2615 -0
  297. package/src/tests/renderer.kitty-flags.test.ts +195 -0
  298. package/src/tests/renderer.lifecycle.test.ts +34 -0
  299. package/src/tests/renderer.mouse.test.ts +1315 -0
  300. package/src/tests/renderer.notifications.test.ts +27 -0
  301. package/src/tests/renderer.palette.test.ts +1199 -0
  302. package/src/tests/renderer.remote.test.ts +247 -0
  303. package/src/tests/renderer.render-stats.test.ts +146 -0
  304. package/src/tests/renderer.scrollback-surface.test.ts +474 -0
  305. package/src/tests/renderer.selection.test.ts +138 -0
  306. package/src/tests/renderer.slot-registry.test.ts +684 -0
  307. package/src/tests/renderer.tracker.test.ts +202 -0
  308. package/src/tests/renderer.useMouse.test.ts +83 -0
  309. package/src/tests/runtime-plugin-node-modules-cycle.fixture.ts +76 -0
  310. package/src/tests/runtime-plugin-node-modules-import-like-string.fixture.ts +48 -0
  311. package/src/tests/runtime-plugin-node-modules-mjs.fixture.ts +43 -0
  312. package/src/tests/runtime-plugin-node-modules-no-bare-rewrite.fixture.ts +67 -0
  313. package/src/tests/runtime-plugin-node-modules-package-type-cache.fixture.ts +72 -0
  314. package/src/tests/runtime-plugin-node-modules-runtime-specifier.fixture.ts +44 -0
  315. package/src/tests/runtime-plugin-node-modules-scoped-package-bare-rewrite.fixture.ts +85 -0
  316. package/src/tests/runtime-plugin-path-alias.fixture.ts +43 -0
  317. package/src/tests/runtime-plugin-resolve-roots.fixture.ts +65 -0
  318. package/src/tests/runtime-plugin-support-configure.fixture.ts +31 -0
  319. package/src/tests/runtime-plugin-support-late-addition.fixture.ts +17 -0
  320. package/src/tests/runtime-plugin-support.fixture.ts +11 -0
  321. package/src/tests/runtime-plugin-support.test.ts +54 -0
  322. package/src/tests/runtime-plugin-windows-file-url.fixture.ts +30 -0
  323. package/src/tests/runtime-plugin.fixture.ts +40 -0
  324. package/src/tests/runtime-plugin.test.ts +369 -0
  325. package/src/tests/scrollbox-culling-bug.test.ts +114 -0
  326. package/src/tests/scrollbox-hitgrid-resize.test.ts +136 -0
  327. package/src/tests/scrollbox-hitgrid.test.ts +909 -0
  328. package/src/tests/scrollbox.test.ts +1895 -0
  329. package/src/tests/wrap-resize-perf.test.ts +276 -0
  330. package/src/tests/yoga-api-coverage.test.ts +409 -0
  331. package/src/tests/yoga-callback-stress.test.ts +122 -0
  332. package/src/tests/yoga-ffi.test.ts +117 -0
  333. package/src/tests/yoga-setters.test.ts +921 -0
  334. package/src/tests/yoga-upstream/YGAlignBaselineTest.test.ts +124 -0
  335. package/src/tests/yoga-upstream/YGComputedBorderTest.test.ts +27 -0
  336. package/src/tests/yoga-upstream/YGComputedMarginTest.test.ts +27 -0
  337. package/src/tests/yoga-upstream/YGComputedPaddingTest.test.ts +27 -0
  338. package/src/tests/yoga-upstream/YGDirtiedTest.test.ts +157 -0
  339. package/src/tests/yoga-upstream/YGErrataTest.test.ts +40 -0
  340. package/src/tests/yoga-upstream/YGFlexBasisAuto.test.ts +23 -0
  341. package/src/tests/yoga-upstream/YGHasNewLayout.test.ts +81 -0
  342. package/src/tests/yoga-upstream/YGMeasureCacheTest.test.ts +31 -0
  343. package/src/tests/yoga-upstream/YGMeasureTest.test.ts +70 -0
  344. package/src/tests/yoga-upstream/generated/YGAbsolutePositionTest.test.ts +1864 -0
  345. package/src/tests/yoga-upstream/generated/YGAlignContentTest.test.ts +5829 -0
  346. package/src/tests/yoga-upstream/generated/YGAlignItemsTest.test.ts +2444 -0
  347. package/src/tests/yoga-upstream/generated/YGAlignSelfTest.test.ts +293 -0
  348. package/src/tests/yoga-upstream/generated/YGAndroidNewsFeed.test.ts +315 -0
  349. package/src/tests/yoga-upstream/generated/YGAspectRatioTest.test.ts +231 -0
  350. package/src/tests/yoga-upstream/generated/YGAutoTest.test.ts +347 -0
  351. package/src/tests/yoga-upstream/generated/YGBorderTest.test.ts +256 -0
  352. package/src/tests/yoga-upstream/generated/YGBoxSizingTest.test.ts +2747 -0
  353. package/src/tests/yoga-upstream/generated/YGDimensionTest.test.ts +129 -0
  354. package/src/tests/yoga-upstream/generated/YGDisplayContentsTest.test.ts +109 -0
  355. package/src/tests/yoga-upstream/generated/YGDisplayTest.test.ts +1097 -0
  356. package/src/tests/yoga-upstream/generated/YGFlexDirectionTest.test.ts +4562 -0
  357. package/src/tests/yoga-upstream/generated/YGFlexTest.test.ts +677 -0
  358. package/src/tests/yoga-upstream/generated/YGFlexWrapTest.test.ts +2071 -0
  359. package/src/tests/yoga-upstream/generated/YGGapTest.test.ts +3303 -0
  360. package/src/tests/yoga-upstream/generated/YGIntrinsicSizeTest.test.ts +582 -0
  361. package/src/tests/yoga-upstream/generated/YGJustifyContentTest.test.ts +2150 -0
  362. package/src/tests/yoga-upstream/generated/YGMarginTest.test.ts +1963 -0
  363. package/src/tests/yoga-upstream/generated/YGMinMaxDimensionTest.test.ts +1446 -0
  364. package/src/tests/yoga-upstream/generated/YGPaddingTest.test.ts +354 -0
  365. package/src/tests/yoga-upstream/generated/YGPercentageTest.test.ts +1760 -0
  366. package/src/tests/yoga-upstream/generated/YGRoundingTest.test.ts +1161 -0
  367. package/src/tests/yoga-upstream/generated/YGSizeOverflowTest.test.ts +207 -0
  368. package/src/tests/yoga-upstream/generated/YGStaticPositionTest.test.ts +6338 -0
  369. package/src/tests/yoga-upstream/tools/MeasureCounter.ts +54 -0
  370. package/src/tests/yoga-upstream/tools/utils.ts +81 -0
  371. package/src/text-buffer-view.test.ts +722 -0
  372. package/src/text-buffer-view.ts +193 -0
  373. package/src/text-buffer.test.ts +512 -0
  374. package/src/text-buffer.ts +250 -0
  375. package/src/types.ts +199 -0
  376. package/src/utils.ts +88 -0
  377. package/src/yoga.ts +1146 -0
  378. package/src/zig/ansi.zig +502 -0
  379. package/src/zig/audio.zig +1206 -0
  380. package/src/zig/bench/README.md +50 -0
  381. package/src/zig/bench/buffer-color-blending_bench.zig +350 -0
  382. package/src/zig/bench/buffer-draw-box_bench.zig +786 -0
  383. package/src/zig/bench/buffer-draw-text-buffer_bench.zig +894 -0
  384. package/src/zig/bench/edit-buffer_bench.zig +476 -0
  385. package/src/zig/bench/editor-view_bench.zig +169 -0
  386. package/src/zig/bench/native-span-feed_bench.zig +107 -0
  387. package/src/zig/bench/rope-markers_bench.zig +713 -0
  388. package/src/zig/bench/rope_bench.zig +514 -0
  389. package/src/zig/bench/styled-text_bench.zig +476 -0
  390. package/src/zig/bench/text-buffer-coords_bench.zig +362 -0
  391. package/src/zig/bench/text-buffer-view_bench.zig +459 -0
  392. package/src/zig/bench/text-chunk-graphemes_bench.zig +272 -0
  393. package/src/zig/bench/utf8_bench.zig +799 -0
  394. package/src/zig/bench-utils.zig +431 -0
  395. package/src/zig/bench.zig +223 -0
  396. package/src/zig/buffer-methods.zig +215 -0
  397. package/src/zig/buffer.zig +2515 -0
  398. package/src/zig/build.zig +700 -0
  399. package/src/zig/build.zig.zon +19 -0
  400. package/src/zig/edit-buffer.zig +900 -0
  401. package/src/zig/editor-view.zig +812 -0
  402. package/src/zig/event-bus.zig +28 -0
  403. package/src/zig/event-emitter.zig +65 -0
  404. package/src/zig/file-logger.zig +92 -0
  405. package/src/zig/grapheme.zig +838 -0
  406. package/src/zig/handles.zig +267 -0
  407. package/src/zig/lib.zig +2813 -0
  408. package/src/zig/link.zig +349 -0
  409. package/src/zig/logger.zig +43 -0
  410. package/src/zig/mem-registry.zig +126 -0
  411. package/src/zig/miniaudio_shim.c +21 -0
  412. package/src/zig/native-renderable.zig +112 -0
  413. package/src/zig/native-span-feed-bench-lib.zig +7 -0
  414. package/src/zig/native-span-feed.zig +747 -0
  415. package/src/zig/renderer-output.zig +677 -0
  416. package/src/zig/renderer.zig +2106 -0
  417. package/src/zig/rope.zig +1220 -0
  418. package/src/zig/split-scrollback.zig +88 -0
  419. package/src/zig/syntax-style.zig +174 -0
  420. package/src/zig/terminal.zig +1652 -0
  421. package/src/zig/test.zig +82 -0
  422. package/src/zig/tests/README.md +18 -0
  423. package/src/zig/tests/ansi_test.zig +18 -0
  424. package/src/zig/tests/audio_test.zig +439 -0
  425. package/src/zig/tests/buffer-methods_test.zig +1111 -0
  426. package/src/zig/tests/buffer_test.zig +2926 -0
  427. package/src/zig/tests/edit-buffer-history_test.zig +299 -0
  428. package/src/zig/tests/edit-buffer_test.zig +1688 -0
  429. package/src/zig/tests/editor-view_test.zig +3454 -0
  430. package/src/zig/tests/event-emitter_test.zig +249 -0
  431. package/src/zig/tests/grapheme_test.zig +1329 -0
  432. package/src/zig/tests/handles_test.zig +123 -0
  433. package/src/zig/tests/link_test.zig +220 -0
  434. package/src/zig/tests/mem-registry_test.zig +473 -0
  435. package/src/zig/tests/memory_leak_regression_test.zig +234 -0
  436. package/src/zig/tests/native-renderable_test.zig +16 -0
  437. package/src/zig/tests/native-span-feed_test.zig +1266 -0
  438. package/src/zig/tests/renderer_test.zig +2604 -0
  439. package/src/zig/tests/rope-nested_test.zig +712 -0
  440. package/src/zig/tests/rope_fuzz_test.zig +238 -0
  441. package/src/zig/tests/rope_test.zig +2362 -0
  442. package/src/zig/tests/segment-merge.test.zig +148 -0
  443. package/src/zig/tests/split-scrollback_test.zig +70 -0
  444. package/src/zig/tests/syntax-style_test.zig +558 -0
  445. package/src/zig/tests/terminal_test.zig +1489 -0
  446. package/src/zig/tests/test-renderer.zig +126 -0
  447. package/src/zig/tests/text-buffer-drawing_test.zig +3291 -0
  448. package/src/zig/tests/text-buffer-highlights_test.zig +706 -0
  449. package/src/zig/tests/text-buffer-iterators_test.zig +776 -0
  450. package/src/zig/tests/text-buffer-segment_test.zig +320 -0
  451. package/src/zig/tests/text-buffer-selection_test.zig +1056 -0
  452. package/src/zig/tests/text-buffer-selection_viewport_test.zig +358 -0
  453. package/src/zig/tests/text-buffer-view_test.zig +3672 -0
  454. package/src/zig/tests/text-buffer_test.zig +2191 -0
  455. package/src/zig/tests/unicode-width-map.zon +3909 -0
  456. package/src/zig/tests/utf8_no_zwj_test.zig +260 -0
  457. package/src/zig/tests/utf8_test.zig +4060 -0
  458. package/src/zig/tests/utf8_wcwidth_cursor_test.zig +267 -0
  459. package/src/zig/tests/utf8_wcwidth_test.zig +357 -0
  460. package/src/zig/tests/word-wrap-editing_test.zig +498 -0
  461. package/src/zig/tests/wrap-cache-perf_test.zig +113 -0
  462. package/src/zig/tests/yoga_test.zig +65 -0
  463. package/src/zig/text-buffer-iterators.zig +496 -0
  464. package/src/zig/text-buffer-segment.zig +403 -0
  465. package/src/zig/text-buffer-view.zig +1428 -0
  466. package/src/zig/text-buffer.zig +1272 -0
  467. package/src/zig/utf8.zig +1954 -0
  468. package/src/zig/utils.zig +12 -0
  469. package/src/zig/uucode-stub.zig +76 -0
  470. package/src/zig/vendor/miniaudio/miniaudio.h +93468 -0
  471. package/src/zig/yoga.zig +658 -0
  472. package/src/zig-structs.ts +381 -0
  473. package/src/zig.ts +5260 -0
  474. package/tsconfig.build.json +17 -0
  475. package/tsconfig.json +27 -0
  476. package/tsconfig.node-test.json +138 -0
@@ -0,0 +1,1206 @@
1
+ const std = @import("std");
2
+ const c = @cImport({
3
+ @cInclude("vendor/miniaudio/miniaudio.h");
4
+ });
5
+
6
+ pub const Status = struct {
7
+ pub const ok: i32 = 0;
8
+ pub const err_invalid: i32 = -1;
9
+ pub const err_no_space: i32 = -2;
10
+ pub const err_decode: i32 = -3;
11
+ pub const err_not_found: i32 = -4;
12
+ pub const err_device: i32 = -5;
13
+ };
14
+
15
+ pub const max_voices: usize = 32;
16
+ pub const default_sample_rate: u32 = 48_000;
17
+ pub const default_playback_channels: u32 = 2;
18
+
19
+ pub const CreateOptions = extern struct {
20
+ sample_rate: u32 = default_sample_rate,
21
+ playback_channels: u32 = default_playback_channels,
22
+ };
23
+
24
+ pub const StartOptions = extern struct {
25
+ period_size_in_frames: u32 = 0,
26
+ period_size_in_milliseconds: u32 = 0,
27
+ periods: u32 = 0,
28
+ performance_profile: u8 = 0,
29
+ share_mode: u8 = 0,
30
+ no_pre_silenced_output_buffer: bool = false,
31
+ no_clip: bool = false,
32
+ no_disable_denormals: bool = false,
33
+ no_fixed_sized_callback: bool = false,
34
+ wasapi_no_auto_convert_src: bool = false,
35
+ wasapi_no_default_quality_src: bool = false,
36
+ alsa_no_mmap: bool = false,
37
+ alsa_no_auto_format: bool = false,
38
+ alsa_no_auto_channels: bool = false,
39
+ alsa_no_auto_resample: bool = false,
40
+ };
41
+
42
+ pub const VoiceOptions = extern struct {
43
+ volume: f32,
44
+ pan: f32,
45
+ loop: bool,
46
+ group_id: u32,
47
+ };
48
+
49
+ pub const Stats = extern struct {
50
+ sounds_loaded: u32,
51
+ voices_active: u32,
52
+ frames_mixed: u64,
53
+ lock_misses: u32,
54
+ last_peak: f32,
55
+ last_rms: f32,
56
+ };
57
+
58
+ const Sound = struct {
59
+ loaded: bool = true,
60
+ channels: u16,
61
+ sample_rate: u32,
62
+ samples: []f32,
63
+ };
64
+
65
+ const Voice = struct {
66
+ active: bool = false,
67
+ sound_index: usize = 0,
68
+ volume: f32 = 1,
69
+ pan: f32 = 0,
70
+ loop: bool = false,
71
+ group_id: u32 = 0,
72
+ buffer_ref: c.ma_audio_buffer_ref = undefined,
73
+ buffer_ready: bool = false,
74
+ sound: c.ma_sound = undefined,
75
+ sound_ready: bool = false,
76
+ };
77
+
78
+ const SoundGroup = struct {
79
+ name: []u8,
80
+ volume: f32 = 1,
81
+ node: c.ma_sound_group = undefined,
82
+ initialized: bool = false,
83
+ };
84
+
85
+ pub const Engine = struct {
86
+ allocator: std.mem.Allocator,
87
+ started: bool = false,
88
+ lock: std.Thread.Mutex = .{},
89
+ context: c.ma_context = undefined,
90
+ context_initialized: bool = false,
91
+ core: c.ma_engine = undefined,
92
+ core_initialized: bool = false,
93
+ sounds: std.ArrayList(Sound),
94
+ groups: std.ArrayList(*SoundGroup),
95
+ playback_devices: std.ArrayList(c.ma_device_info),
96
+ selected_playback_index: ?u32 = null,
97
+ voices: [max_voices]Voice,
98
+ master_volume: f32,
99
+ sample_rate: u32,
100
+ stats: Stats,
101
+ device: c.ma_device = undefined,
102
+ has_device: bool = false,
103
+ output_channels: u8 = 2,
104
+ lock_miss_count: u32 = 0,
105
+ tap_enabled: bool = false,
106
+ tap_channels: u8 = 2,
107
+ tap_capacity_frames: u32 = 0,
108
+ tap_write_frame: u32 = 0,
109
+ tap_frame_count: u32 = 0,
110
+ tap_buffer: ?[]f32 = null,
111
+
112
+ pub fn init(allocator: std.mem.Allocator, sample_rate: u32, output_channels: u8) Engine {
113
+ const normalized_sample_rate = if (sample_rate == 0) default_sample_rate else sample_rate;
114
+ return .{
115
+ .allocator = allocator,
116
+ .started = false,
117
+ .context = undefined,
118
+ .context_initialized = false,
119
+ .core = undefined,
120
+ .core_initialized = false,
121
+ .sounds = .empty,
122
+ .groups = .empty,
123
+ .playback_devices = .empty,
124
+ .selected_playback_index = null,
125
+ .voices = [_]Voice{.{}} ** max_voices,
126
+ .master_volume = 1,
127
+ .sample_rate = normalized_sample_rate,
128
+ .stats = .{
129
+ .sounds_loaded = 0,
130
+ .voices_active = 0,
131
+ .frames_mixed = 0,
132
+ .lock_misses = 0,
133
+ .last_peak = 0,
134
+ .last_rms = 0,
135
+ },
136
+ .device = undefined,
137
+ .has_device = false,
138
+ .output_channels = output_channels,
139
+ .lock_miss_count = 0,
140
+ .tap_enabled = false,
141
+ .tap_channels = 2,
142
+ .tap_capacity_frames = 0,
143
+ .tap_write_frame = 0,
144
+ .tap_frame_count = 0,
145
+ .tap_buffer = null,
146
+ };
147
+ }
148
+
149
+ pub fn deinit(self: *Engine) void {
150
+ self.lock.lock();
151
+ defer self.lock.unlock();
152
+
153
+ if (self.has_device) {
154
+ _ = c.ma_device_stop(&self.device);
155
+ c.ma_device_uninit(&self.device);
156
+ self.has_device = false;
157
+ }
158
+
159
+ for (&self.voices) |*voice| {
160
+ clearVoice(voice);
161
+ }
162
+
163
+ for (self.groups.items) |group| {
164
+ if (group.initialized) {
165
+ c.ma_sound_group_uninit(&group.node);
166
+ group.initialized = false;
167
+ }
168
+ self.allocator.free(group.name);
169
+ self.allocator.destroy(group);
170
+ }
171
+
172
+ for (self.sounds.items) |sound| {
173
+ if (sound.loaded) {
174
+ self.allocator.free(sound.samples);
175
+ }
176
+ }
177
+
178
+ self.sounds.deinit(self.allocator);
179
+ self.groups.deinit(self.allocator);
180
+ self.playback_devices.deinit(self.allocator);
181
+
182
+ if (self.tap_buffer) |buffer| {
183
+ self.allocator.free(buffer);
184
+ self.tap_buffer = null;
185
+ }
186
+
187
+ if (self.context_initialized) {
188
+ _ = c.ma_context_uninit(&self.context);
189
+ self.context_initialized = false;
190
+ }
191
+
192
+ if (self.core_initialized) {
193
+ c.ma_engine_uninit(&self.core);
194
+ self.core_initialized = false;
195
+ }
196
+ }
197
+
198
+ fn updateActiveVoiceCount(self: *Engine) void {
199
+ var active: u32 = 0;
200
+ for (self.voices) |voice| {
201
+ if (voice.active) active += 1;
202
+ }
203
+ self.stats.voices_active = active;
204
+ }
205
+
206
+ fn updateLoadedSoundCount(self: *Engine) void {
207
+ var loaded: u32 = 0;
208
+ for (self.sounds.items) |sound| {
209
+ if (sound.loaded) loaded += 1;
210
+ }
211
+ self.stats.sounds_loaded = loaded;
212
+ }
213
+ };
214
+
215
+ fn clamp(value: f32, min: f32, max: f32) f32 {
216
+ return @max(min, @min(max, value));
217
+ }
218
+
219
+ fn toMaBool8(value: bool) c.ma_bool8 {
220
+ return if (value) c.MA_TRUE else c.MA_FALSE;
221
+ }
222
+
223
+ fn toMaBool32(value: bool) c.ma_bool32 {
224
+ return if (value) c.MA_TRUE else c.MA_FALSE;
225
+ }
226
+
227
+ fn toPerformanceProfile(value: u8) c.ma_performance_profile {
228
+ if (value == 1) {
229
+ return c.ma_performance_profile_conservative;
230
+ }
231
+ return c.ma_performance_profile_low_latency;
232
+ }
233
+
234
+ fn toShareMode(value: u8) c.ma_share_mode {
235
+ if (value == 1) {
236
+ return c.ma_share_mode_exclusive;
237
+ }
238
+ return c.ma_share_mode_shared;
239
+ }
240
+
241
+ fn decoderAsDataSource(decoder: *c.ma_decoder) *c.ma_data_source {
242
+ return @ptrCast(decoder);
243
+ }
244
+
245
+ const DecodedDataSourceFormat = struct {
246
+ channels: u16,
247
+ sample_rate: u32,
248
+ };
249
+
250
+ fn incrementLockMisses(engine: *Engine) void {
251
+ _ = @atomicRmw(u32, &engine.lock_miss_count, .Add, 1, .monotonic);
252
+ }
253
+
254
+ fn loadLockMisses(engine: *Engine) u32 {
255
+ return @atomicLoad(u32, &engine.lock_miss_count, .monotonic);
256
+ }
257
+
258
+ fn ensureContextInitialized(engine: *Engine) i32 {
259
+ if (engine.context_initialized) return Status.ok;
260
+
261
+ if (c.ma_context_init(null, 0, null, &engine.context) != c.MA_SUCCESS) {
262
+ return Status.err_device;
263
+ }
264
+
265
+ engine.context_initialized = true;
266
+ return Status.ok;
267
+ }
268
+
269
+ fn refreshPlaybackDevicesLocked(engine: *Engine) i32 {
270
+ const context_status = ensureContextInitialized(engine);
271
+ if (context_status != Status.ok) return context_status;
272
+
273
+ var playback_infos: [*c]c.ma_device_info = null;
274
+ var playback_count: c.ma_uint32 = 0;
275
+ const result = c.ma_context_get_devices(&engine.context, &playback_infos, &playback_count, null, null);
276
+ if (result != c.MA_SUCCESS) return Status.err_device;
277
+
278
+ engine.playback_devices.clearRetainingCapacity();
279
+
280
+ if (playback_infos != null and playback_count > 0) {
281
+ const count: usize = @intCast(playback_count);
282
+ const devices = playback_infos[0..count];
283
+ engine.playback_devices.appendSlice(engine.allocator, devices) catch return Status.err_no_space;
284
+ }
285
+
286
+ if (engine.selected_playback_index) |selected_index| {
287
+ if (@as(usize, @intCast(selected_index)) >= engine.playback_devices.items.len) {
288
+ engine.selected_playback_index = null;
289
+ }
290
+ }
291
+
292
+ return Status.ok;
293
+ }
294
+
295
+ fn copyPlaybackDeviceName(device: *const c.ma_device_info, out_ptr: [*]u8, max_len: usize) usize {
296
+ if (max_len == 0) return 0;
297
+
298
+ var name_len: usize = 0;
299
+ while (name_len < device.name.len and device.name[name_len] != 0) : (name_len += 1) {}
300
+
301
+ const copy_len = @min(name_len, max_len);
302
+ for (0..copy_len) |i| {
303
+ out_ptr[i] = @bitCast(device.name[i]);
304
+ }
305
+
306
+ return copy_len;
307
+ }
308
+
309
+ fn writeTapFrames(engine: *Engine, source: []const f32, frame_count: u32, channels: u8) void {
310
+ if (!engine.tap_enabled or frame_count == 0 or channels == 0) return;
311
+ const tap_buffer = engine.tap_buffer orelse return;
312
+ if (engine.tap_capacity_frames == 0) return;
313
+
314
+ const source_channels: usize = channels;
315
+ const tap_channels: usize = engine.tap_channels;
316
+ for (0..@as(usize, frame_count)) |frame| {
317
+ const src = frame * source_channels;
318
+ const left = source[src];
319
+ const right = if (channels > 1) source[src + 1] else left;
320
+
321
+ const dst_frame: usize = engine.tap_write_frame;
322
+ const dst = dst_frame * tap_channels;
323
+ tap_buffer[dst] = left;
324
+ if (tap_channels > 1) {
325
+ tap_buffer[dst + 1] = right;
326
+ }
327
+
328
+ const next = engine.tap_write_frame + 1;
329
+ engine.tap_write_frame = if (next >= engine.tap_capacity_frames) 0 else next;
330
+ if (engine.tap_frame_count < engine.tap_capacity_frames) {
331
+ engine.tap_frame_count += 1;
332
+ }
333
+ }
334
+ }
335
+
336
+ fn clearVoice(voice: *Voice) void {
337
+ if (voice.sound_ready) {
338
+ c.ma_sound_uninit(&voice.sound);
339
+ voice.sound_ready = false;
340
+ }
341
+ if (voice.buffer_ready) {
342
+ c.ma_audio_buffer_ref_uninit(&voice.buffer_ref);
343
+ voice.buffer_ready = false;
344
+ }
345
+ voice.active = false;
346
+ voice.sound_index = 0;
347
+ voice.volume = 1;
348
+ voice.pan = 0;
349
+ voice.loop = false;
350
+ voice.group_id = 0;
351
+ }
352
+
353
+ fn getDataSourceFormat(data_source: *c.ma_data_source) !DecodedDataSourceFormat {
354
+ var format: c.ma_format = c.ma_format_unknown;
355
+ var channels: c.ma_uint32 = 0;
356
+ var sample_rate: c.ma_uint32 = 0;
357
+
358
+ const format_result = c.ma_data_source_get_data_format(data_source, &format, &channels, &sample_rate, null, 0);
359
+ if (format_result != c.MA_SUCCESS) return error.DecodeFailed;
360
+ if (channels == 0 or sample_rate == 0) return error.DecodeFailed;
361
+
362
+ return .{
363
+ .channels = std.math.cast(u16, channels) orelse return error.DecodeFailed,
364
+ .sample_rate = @intCast(sample_rate),
365
+ };
366
+ }
367
+
368
+ // Fast path when data source reports total frame length: seek to frame 0,
369
+ // allocate once for exact sample count, then trim if decoder returns fewer
370
+ // frames than advertised.
371
+ fn decodeSoundKnownLength(allocator: std.mem.Allocator, data_source: *c.ma_data_source, frame_count: c.ma_uint64, channels: u16, sample_rate: u32) !Sound {
372
+ const channel_count: usize = channels;
373
+ const frame_count_usize = std.math.cast(usize, frame_count) orelse return error.OutOfMemory;
374
+ const sample_count = try std.math.mul(usize, frame_count_usize, channel_count);
375
+
376
+ const seek_result = c.ma_data_source_seek_to_pcm_frame(data_source, 0);
377
+ if (seek_result != c.MA_SUCCESS) return error.DecodeFailed;
378
+
379
+ var samples = try allocator.alloc(f32, sample_count);
380
+ errdefer allocator.free(samples);
381
+
382
+ var frames_read: c.ma_uint64 = 0;
383
+ const result = c.ma_data_source_read_pcm_frames(data_source, samples.ptr, frame_count, &frames_read);
384
+ if (result != c.MA_SUCCESS and result != c.MA_AT_END) return error.DecodeFailed;
385
+
386
+ const frames_read_usize = std.math.cast(usize, frames_read) orelse return error.OutOfMemory;
387
+ const final_sample_count = try std.math.mul(usize, frames_read_usize, channel_count);
388
+ if (final_sample_count != sample_count) {
389
+ samples = try allocator.realloc(samples, final_sample_count);
390
+ }
391
+
392
+ return .{
393
+ .channels = channels,
394
+ .sample_rate = sample_rate,
395
+ .samples = samples,
396
+ };
397
+ }
398
+
399
+ // Fallback path when total frame length unknown: read fixed-size chunks
400
+ // until MA_AT_END, then pack all chunks into one flat sample array.
401
+ fn decodeSoundUnknownLength(allocator: std.mem.Allocator, data_source: *c.ma_data_source, channels: u16, sample_rate: u32) !Sound {
402
+ const channel_count: usize = channels;
403
+ const chunk_frames: c.ma_uint64 = 4096;
404
+ const chunk_frames_usize: usize = @intCast(chunk_frames);
405
+ const chunk_sample_count = try std.math.mul(usize, chunk_frames_usize, channel_count);
406
+ const chunk = try allocator.alloc(f32, chunk_sample_count);
407
+ defer allocator.free(chunk);
408
+
409
+ var samples = std.ArrayList(f32).empty;
410
+ errdefer samples.deinit(allocator);
411
+
412
+ while (true) {
413
+ var frames_read: c.ma_uint64 = 0;
414
+ const result = c.ma_data_source_read_pcm_frames(data_source, chunk.ptr, chunk_frames, &frames_read);
415
+ if (result != c.MA_SUCCESS and result != c.MA_AT_END) return error.DecodeFailed;
416
+
417
+ const frames_read_usize = std.math.cast(usize, frames_read) orelse return error.OutOfMemory;
418
+ const sample_count = try std.math.mul(usize, frames_read_usize, channel_count);
419
+ if (sample_count > 0) {
420
+ try samples.appendSlice(allocator, chunk[0..sample_count]);
421
+ }
422
+
423
+ if (result == c.MA_AT_END or frames_read == 0) break;
424
+ }
425
+
426
+ return .{
427
+ .loaded = true,
428
+ .channels = channels,
429
+ .sample_rate = sample_rate,
430
+ .samples = try samples.toOwnedSlice(allocator),
431
+ };
432
+ }
433
+
434
+ fn decodeSoundFromMemory(allocator: std.mem.Allocator, bytes: []const u8) !Sound {
435
+ var config = c.ma_decoder_config_init(c.ma_format_f32, 0, 0);
436
+ var decoder: c.ma_decoder = undefined;
437
+ const init_result = c.ma_decoder_init_memory(bytes.ptr, bytes.len, &config, &decoder);
438
+ if (init_result != c.MA_SUCCESS) return error.DecodeFailed;
439
+ defer _ = c.ma_decoder_uninit(&decoder);
440
+
441
+ const data_source = decoderAsDataSource(&decoder);
442
+ const decoded_format = try getDataSourceFormat(data_source);
443
+ var frame_count: c.ma_uint64 = 0;
444
+ const length_result = c.ma_data_source_get_length_in_pcm_frames(data_source, &frame_count);
445
+
446
+ if (length_result == c.MA_SUCCESS) {
447
+ return decodeSoundKnownLength(allocator, data_source, frame_count, decoded_format.channels, decoded_format.sample_rate);
448
+ }
449
+
450
+ return decodeSoundUnknownLength(allocator, data_source, decoded_format.channels, decoded_format.sample_rate);
451
+ }
452
+
453
+ fn initDefaultGroup(engine: *Engine) !void {
454
+ const group = try engine.allocator.create(SoundGroup);
455
+ errdefer engine.allocator.destroy(group);
456
+
457
+ group.* = .{
458
+ .name = try engine.allocator.dupe(u8, "default"),
459
+ };
460
+ errdefer engine.allocator.free(group.name);
461
+
462
+ const result = c.ma_sound_group_init(&engine.core, 0, null, &group.node);
463
+ if (result != c.MA_SUCCESS) return error.DeviceInitFailed;
464
+ group.initialized = true;
465
+ c.ma_sound_group_set_volume(&group.node, 1);
466
+
467
+ try engine.groups.append(engine.allocator, group);
468
+ }
469
+
470
+ fn reapFinishedVoices(engine: *Engine) void {
471
+ for (&engine.voices) |*voice| {
472
+ if (!voice.active or !voice.sound_ready) continue;
473
+
474
+ const playing = c.ma_sound_is_playing(&voice.sound) != c.MA_FALSE;
475
+ const at_end = c.ma_sound_at_end(&voice.sound) != c.MA_FALSE;
476
+
477
+ if (!playing and at_end) {
478
+ clearVoice(voice);
479
+ }
480
+ }
481
+ engine.updateActiveVoiceCount();
482
+ }
483
+
484
+ fn updateStatsFromBuffer(engine: *Engine, out: []const f32, frame_count: u32, channels: u8) void {
485
+ engine.stats.frames_mixed += frame_count;
486
+ engine.stats.lock_misses = loadLockMisses(engine);
487
+
488
+ if (frame_count == 0 or channels == 0 or out.len == 0) {
489
+ engine.stats.last_peak = 0;
490
+ engine.stats.last_rms = 0;
491
+ return;
492
+ }
493
+
494
+ var peak: f32 = 0;
495
+ var rms_acc: f64 = 0;
496
+ for (out) |sample| {
497
+ const abs_value = @abs(sample);
498
+ if (abs_value > peak) peak = abs_value;
499
+ rms_acc += @as(f64, sample) * @as(f64, sample);
500
+ }
501
+
502
+ const sample_count = @as(f64, @floatFromInt(frame_count)) * @as(f64, @floatFromInt(channels));
503
+ engine.stats.last_peak = peak;
504
+ engine.stats.last_rms = @floatCast(std.math.sqrt(rms_acc / @max(sample_count, 1)));
505
+ }
506
+
507
+ fn readEngineStereo(engine: *Engine, out_stereo: []f32, frame_count: u32) i32 {
508
+ if (out_stereo.len < @as(usize, frame_count) * 2) return Status.err_invalid;
509
+
510
+ var frames_read: c.ma_uint64 = 0;
511
+ const result = c.ma_engine_read_pcm_frames(&engine.core, out_stereo.ptr, frame_count, &frames_read);
512
+ if (result != c.MA_SUCCESS and result != c.MA_AT_END) {
513
+ return Status.err_device;
514
+ }
515
+
516
+ const frames_read_usize = std.math.cast(usize, frames_read) orelse return Status.err_device;
517
+ const requested = @as(usize, frame_count);
518
+ if (frames_read_usize < requested) {
519
+ const zero_start = frames_read_usize * 2;
520
+ const zero_end = requested * 2;
521
+ @memset(out_stereo[zero_start..zero_end], 0);
522
+ }
523
+
524
+ return Status.ok;
525
+ }
526
+
527
+ pub fn create(allocator: std.mem.Allocator, options_ptr: ?*const CreateOptions) ?*Engine {
528
+ const options = if (options_ptr) |opts| opts.* else CreateOptions{};
529
+ const sample_rate = if (options.sample_rate == 0) default_sample_rate else options.sample_rate;
530
+ const playback_channels = if (options.playback_channels == 0) default_playback_channels else options.playback_channels;
531
+ const max_channels: u32 = @intCast(c.MA_MAX_CHANNELS);
532
+ if (playback_channels == 0 or playback_channels > max_channels) return null;
533
+ const output_channels = std.math.cast(u8, playback_channels) orelse return null;
534
+
535
+ const engine = allocator.create(Engine) catch return null;
536
+ errdefer allocator.destroy(engine);
537
+ engine.* = Engine.init(allocator, sample_rate, output_channels);
538
+
539
+ var config = c.ma_engine_config_init();
540
+ config.noDevice = c.MA_TRUE;
541
+ config.channels = 2;
542
+ config.sampleRate = sample_rate;
543
+
544
+ if (c.ma_engine_init(&config, &engine.core) != c.MA_SUCCESS) return null;
545
+ engine.core_initialized = true;
546
+ engine.sample_rate = c.ma_engine_get_sample_rate(&engine.core);
547
+
548
+ initDefaultGroup(engine) catch return null;
549
+ return engine;
550
+ }
551
+
552
+ pub fn destroy(engine: *Engine) void {
553
+ const e = engine;
554
+ e.deinit();
555
+ e.allocator.destroy(e);
556
+ }
557
+
558
+ pub fn refreshPlaybackDevices(engine: *Engine) i32 {
559
+ const e = engine;
560
+ e.lock.lock();
561
+ defer e.lock.unlock();
562
+ return refreshPlaybackDevicesLocked(e);
563
+ }
564
+
565
+ pub fn getPlaybackDeviceCount(engine: *Engine) u32 {
566
+ const e = engine;
567
+ e.lock.lock();
568
+ defer e.lock.unlock();
569
+ return @intCast(e.playback_devices.items.len);
570
+ }
571
+
572
+ pub fn getPlaybackDeviceName(engine: *Engine, index: u32, out_ptr: ?[*]u8, max_len: usize) usize {
573
+ if (out_ptr == null) return 0;
574
+
575
+ const e = engine;
576
+ e.lock.lock();
577
+ defer e.lock.unlock();
578
+
579
+ const idx: usize = @intCast(index);
580
+ if (idx >= e.playback_devices.items.len) return 0;
581
+ return copyPlaybackDeviceName(&e.playback_devices.items[idx], out_ptr.?, max_len);
582
+ }
583
+
584
+ pub fn isPlaybackDeviceDefault(engine: *Engine, index: u32) bool {
585
+ const e = engine;
586
+ e.lock.lock();
587
+ defer e.lock.unlock();
588
+
589
+ const idx: usize = @intCast(index);
590
+ if (idx >= e.playback_devices.items.len) return false;
591
+ return e.playback_devices.items[idx].isDefault != c.MA_FALSE;
592
+ }
593
+
594
+ pub fn selectPlaybackDevice(engine: *Engine, index: u32) i32 {
595
+ const e = engine;
596
+ e.lock.lock();
597
+ defer e.lock.unlock();
598
+
599
+ if (e.started) return Status.err_invalid;
600
+ if (e.playback_devices.items.len == 0) {
601
+ const refresh_status = refreshPlaybackDevicesLocked(e);
602
+ if (refresh_status != Status.ok) return refresh_status;
603
+ }
604
+
605
+ const idx: usize = @intCast(index);
606
+ if (idx >= e.playback_devices.items.len) return Status.err_not_found;
607
+
608
+ e.selected_playback_index = index;
609
+ if (e.has_device) {
610
+ _ = c.ma_device_stop(&e.device);
611
+ c.ma_device_uninit(&e.device);
612
+ e.has_device = false;
613
+ }
614
+ return Status.ok;
615
+ }
616
+
617
+ pub fn clearPlaybackDeviceSelection(engine: *Engine) void {
618
+ const e = engine;
619
+ e.lock.lock();
620
+ defer e.lock.unlock();
621
+
622
+ e.selected_playback_index = null;
623
+ if (!e.started and e.has_device) {
624
+ _ = c.ma_device_stop(&e.device);
625
+ c.ma_device_uninit(&e.device);
626
+ e.has_device = false;
627
+ }
628
+ }
629
+
630
+ pub fn start(engine: *Engine, options_ptr: ?*const StartOptions) i32 {
631
+ const e = engine;
632
+ const options = if (options_ptr) |opts| opts.* else StartOptions{};
633
+ if (e.started and e.has_device) return Status.ok;
634
+
635
+ if (!e.has_device) {
636
+ const context_status = ensureContextInitialized(e);
637
+ if (context_status != Status.ok) return context_status;
638
+
639
+ var selected_device_id: ?*const c.ma_device_id = null;
640
+ if (e.selected_playback_index) |selected_index| {
641
+ if (e.playback_devices.items.len == 0) {
642
+ const refresh_status = refreshPlaybackDevicesLocked(e);
643
+ if (refresh_status != Status.ok) return refresh_status;
644
+ }
645
+
646
+ const idx: usize = @intCast(selected_index);
647
+ if (idx >= e.playback_devices.items.len) return Status.err_not_found;
648
+ selected_device_id = &e.playback_devices.items[idx].id;
649
+ }
650
+
651
+ var config = c.ma_device_config_init(c.ma_device_type_playback);
652
+ config.sampleRate = e.sample_rate;
653
+ config.periodSizeInFrames = options.period_size_in_frames;
654
+ config.periodSizeInMilliseconds = options.period_size_in_milliseconds;
655
+ config.periods = options.periods;
656
+ config.performanceProfile = toPerformanceProfile(options.performance_profile);
657
+ config.noPreSilencedOutputBuffer = toMaBool8(options.no_pre_silenced_output_buffer);
658
+ config.noClip = toMaBool8(options.no_clip);
659
+ config.noDisableDenormals = toMaBool8(options.no_disable_denormals);
660
+ config.noFixedSizedCallback = toMaBool8(options.no_fixed_sized_callback);
661
+ config.playback.format = c.ma_format_f32;
662
+ config.playback.channels = @intCast(e.output_channels);
663
+ config.playback.shareMode = toShareMode(options.share_mode);
664
+ config.playback.pDeviceID = selected_device_id;
665
+ config.wasapi.noAutoConvertSRC = toMaBool8(options.wasapi_no_auto_convert_src);
666
+ config.wasapi.noDefaultQualitySRC = toMaBool8(options.wasapi_no_default_quality_src);
667
+ config.alsa.noMMap = toMaBool32(options.alsa_no_mmap);
668
+ config.alsa.noAutoFormat = toMaBool32(options.alsa_no_auto_format);
669
+ config.alsa.noAutoChannels = toMaBool32(options.alsa_no_auto_channels);
670
+ config.alsa.noAutoResample = toMaBool32(options.alsa_no_auto_resample);
671
+ config.dataCallback = audioCallback;
672
+ config.pUserData = e;
673
+
674
+ const init_result = c.ma_device_init(&e.context, &config, &e.device);
675
+ if (init_result != c.MA_SUCCESS) {
676
+ return Status.err_device;
677
+ }
678
+
679
+ e.has_device = true;
680
+ const device_channels = e.device.playback.channels;
681
+ const max_channels: c.ma_uint32 = @intCast(c.MA_MAX_CHANNELS);
682
+ if (device_channels > 0 and device_channels <= max_channels) {
683
+ e.output_channels = std.math.cast(u8, device_channels) orelse e.output_channels;
684
+ }
685
+ }
686
+
687
+ const start_result = c.ma_device_start(&e.device);
688
+ if (start_result != c.MA_SUCCESS) {
689
+ c.ma_device_uninit(&e.device);
690
+ e.has_device = false;
691
+ return Status.err_device;
692
+ }
693
+
694
+ e.started = true;
695
+ return Status.ok;
696
+ }
697
+
698
+ pub fn startMixer(engine: *Engine) i32 {
699
+ engine.started = true;
700
+ return Status.ok;
701
+ }
702
+
703
+ pub fn stop(engine: *Engine) i32 {
704
+ const e = engine;
705
+ if (e.has_device) {
706
+ _ = c.ma_device_stop(&e.device);
707
+ c.ma_device_uninit(&e.device);
708
+ e.has_device = false;
709
+ }
710
+ e.started = false;
711
+ return Status.ok;
712
+ }
713
+
714
+ pub fn load(engine: *Engine, data_ptr: ?[*]const u8, data_len: usize, out_sound_id: ?*u32) i32 {
715
+ if (data_ptr == null or out_sound_id == null or data_len == 0) return Status.err_invalid;
716
+ const e = engine;
717
+ const encoded_audio = @as([*]const u8, @ptrCast(data_ptr.?))[0..data_len];
718
+ const sound = decodeSoundFromMemory(e.allocator, encoded_audio) catch return Status.err_decode;
719
+
720
+ e.lock.lock();
721
+ defer e.lock.unlock();
722
+
723
+ e.sounds.append(e.allocator, sound) catch {
724
+ e.allocator.free(sound.samples);
725
+ return Status.err_no_space;
726
+ };
727
+
728
+ out_sound_id.?.* = @intCast(e.sounds.items.len);
729
+ e.updateLoadedSoundCount();
730
+ return Status.ok;
731
+ }
732
+
733
+ pub fn unload(engine: *Engine, sound_id: u32) i32 {
734
+ if (sound_id == 0) return Status.err_invalid;
735
+ const e = engine;
736
+
737
+ e.lock.lock();
738
+ defer e.lock.unlock();
739
+
740
+ const sound_index: usize = @intCast(sound_id - 1);
741
+ if (sound_index >= e.sounds.items.len) return Status.err_not_found;
742
+
743
+ const sound = &e.sounds.items[sound_index];
744
+ if (!sound.loaded) return Status.err_not_found;
745
+
746
+ for (&e.voices) |*voice| {
747
+ if (voice.active and voice.sound_index == sound_index) {
748
+ clearVoice(voice);
749
+ }
750
+ }
751
+
752
+ const empty_samples = sound.samples[0..0];
753
+ e.allocator.free(sound.samples);
754
+ sound.samples = empty_samples;
755
+ sound.loaded = false;
756
+ sound.channels = 0;
757
+ sound.sample_rate = 0;
758
+
759
+ e.updateActiveVoiceCount();
760
+ e.updateLoadedSoundCount();
761
+ return Status.ok;
762
+ }
763
+
764
+ pub fn createGroup(engine: *Engine, name_ptr: ?[*]const u8, name_len: usize, out_group_id: ?*u32) i32 {
765
+ if (name_ptr == null or out_group_id == null) return Status.err_invalid;
766
+ const e = engine;
767
+ const name = @as([*]const u8, @ptrCast(name_ptr.?))[0..name_len];
768
+
769
+ e.lock.lock();
770
+ defer e.lock.unlock();
771
+
772
+ for (e.groups.items, 0..) |group, idx| {
773
+ if (std.mem.eql(u8, group.name, name)) {
774
+ out_group_id.?.* = @intCast(idx);
775
+ return Status.ok;
776
+ }
777
+ }
778
+
779
+ const group = e.allocator.create(SoundGroup) catch return Status.err_no_space;
780
+ errdefer e.allocator.destroy(group);
781
+
782
+ group.* = .{
783
+ .name = e.allocator.dupe(u8, name) catch return Status.err_no_space,
784
+ };
785
+ errdefer e.allocator.free(group.name);
786
+
787
+ const init_result = c.ma_sound_group_init(&e.core, 0, null, &group.node);
788
+ if (init_result != c.MA_SUCCESS) return Status.err_device;
789
+ group.initialized = true;
790
+ c.ma_sound_group_set_volume(&group.node, 1);
791
+
792
+ e.groups.append(e.allocator, group) catch {
793
+ c.ma_sound_group_uninit(&group.node);
794
+ e.allocator.free(group.name);
795
+ e.allocator.destroy(group);
796
+ return Status.err_no_space;
797
+ };
798
+
799
+ out_group_id.?.* = @intCast(e.groups.items.len - 1);
800
+ return Status.ok;
801
+ }
802
+
803
+ pub fn play(engine: *Engine, sound_id: u32, options_ptr: ?*const VoiceOptions, out_voice_id: ?*u32) i32 {
804
+ if (out_voice_id == null) return Status.err_invalid;
805
+ const e = engine;
806
+
807
+ e.lock.lock();
808
+ defer e.lock.unlock();
809
+ reapFinishedVoices(e);
810
+
811
+ if (sound_id == 0 or sound_id > @as(u32, @intCast(e.sounds.items.len))) return Status.err_not_found;
812
+
813
+ const options = if (options_ptr) |opts| opts.* else VoiceOptions{
814
+ .volume = 1,
815
+ .pan = 0,
816
+ .loop = false,
817
+ .group_id = 0,
818
+ };
819
+
820
+ const group_index: usize = @intCast(options.group_id);
821
+ if (group_index >= e.groups.items.len) return Status.err_invalid;
822
+
823
+ var free_index: ?usize = null;
824
+ for (e.voices, 0..) |voice, idx| {
825
+ if (!voice.active) {
826
+ free_index = idx;
827
+ break;
828
+ }
829
+ }
830
+ if (free_index == null) return Status.err_no_space;
831
+
832
+ const slot = &e.voices[free_index.?];
833
+ clearVoice(slot);
834
+
835
+ const sound = e.sounds.items[@intCast(sound_id - 1)];
836
+ if (!sound.loaded) return Status.err_not_found;
837
+ const frame_count: usize = sound.samples.len / @as(usize, sound.channels);
838
+
839
+ if (c.ma_audio_buffer_ref_init(c.ma_format_f32, sound.channels, sound.samples.ptr, @intCast(frame_count), &slot.buffer_ref) != c.MA_SUCCESS) {
840
+ return Status.err_device;
841
+ }
842
+ slot.buffer_ref.sampleRate = sound.sample_rate;
843
+ slot.buffer_ready = true;
844
+
845
+ const group_ptr = &e.groups.items[group_index].node;
846
+ const data_source: *c.ma_data_source = @ptrCast(&slot.buffer_ref);
847
+ const sound_flags: c.ma_uint32 = c.MA_SOUND_FLAG_NO_SPATIALIZATION | c.MA_SOUND_FLAG_NO_PITCH;
848
+ if (c.ma_sound_init_from_data_source(&e.core, data_source, sound_flags, group_ptr, &slot.sound) != c.MA_SUCCESS) {
849
+ clearVoice(slot);
850
+ return Status.err_device;
851
+ }
852
+ slot.sound_ready = true;
853
+
854
+ slot.active = true;
855
+ slot.sound_index = @intCast(sound_id - 1);
856
+ slot.volume = clamp(options.volume, 0, 4);
857
+ slot.pan = clamp(options.pan, -1, 1);
858
+ slot.loop = options.loop;
859
+ slot.group_id = options.group_id;
860
+
861
+ c.ma_sound_set_looping(&slot.sound, if (slot.loop) c.MA_TRUE else c.MA_FALSE);
862
+ c.ma_sound_set_pan(&slot.sound, slot.pan);
863
+ c.ma_sound_set_volume(&slot.sound, slot.volume);
864
+
865
+ if (c.ma_sound_start(&slot.sound) != c.MA_SUCCESS) {
866
+ clearVoice(slot);
867
+ return Status.err_device;
868
+ }
869
+
870
+ out_voice_id.?.* = @intCast(free_index.? + 1);
871
+ e.updateActiveVoiceCount();
872
+ return Status.ok;
873
+ }
874
+
875
+ pub fn stopVoice(engine: *Engine, voice_id: u32) i32 {
876
+ if (voice_id == 0) return Status.err_invalid;
877
+ const e = engine;
878
+
879
+ e.lock.lock();
880
+ defer e.lock.unlock();
881
+
882
+ const idx: usize = @intCast(voice_id - 1);
883
+ if (idx >= e.voices.len) return Status.err_not_found;
884
+ if (!e.voices[idx].active) return Status.err_not_found;
885
+
886
+ _ = c.ma_sound_stop(&e.voices[idx].sound);
887
+ clearVoice(&e.voices[idx]);
888
+ e.updateActiveVoiceCount();
889
+ return Status.ok;
890
+ }
891
+
892
+ pub fn setVoiceGroup(engine: *Engine, voice_id: u32, group_id: u32) i32 {
893
+ if (voice_id == 0) return Status.err_invalid;
894
+ const e = engine;
895
+
896
+ e.lock.lock();
897
+ defer e.lock.unlock();
898
+
899
+ const voice_index: usize = @intCast(voice_id - 1);
900
+ const group_index: usize = @intCast(group_id);
901
+ if (voice_index >= e.voices.len or group_index >= e.groups.items.len) return Status.err_invalid;
902
+
903
+ const voice = &e.voices[voice_index];
904
+ if (!voice.active or !voice.sound_ready or !voice.buffer_ready) return Status.err_not_found;
905
+
906
+ var cursor: c.ma_uint64 = 0;
907
+ _ = c.ma_sound_get_cursor_in_pcm_frames(&voice.sound, &cursor);
908
+ const was_playing = c.ma_sound_is_playing(&voice.sound) != c.MA_FALSE;
909
+
910
+ c.ma_sound_uninit(&voice.sound);
911
+ voice.sound_ready = false;
912
+
913
+ const group_ptr = &e.groups.items[group_index].node;
914
+ const data_source: *c.ma_data_source = @ptrCast(&voice.buffer_ref);
915
+ const sound_flags: c.ma_uint32 = c.MA_SOUND_FLAG_NO_SPATIALIZATION | c.MA_SOUND_FLAG_NO_PITCH;
916
+ if (c.ma_sound_init_from_data_source(&e.core, data_source, sound_flags, group_ptr, &voice.sound) != c.MA_SUCCESS) {
917
+ clearVoice(voice);
918
+ e.updateActiveVoiceCount();
919
+ return Status.err_device;
920
+ }
921
+
922
+ voice.sound_ready = true;
923
+ _ = c.ma_sound_seek_to_pcm_frame(&voice.sound, cursor);
924
+ c.ma_sound_set_looping(&voice.sound, if (voice.loop) c.MA_TRUE else c.MA_FALSE);
925
+ c.ma_sound_set_pan(&voice.sound, voice.pan);
926
+ c.ma_sound_set_volume(&voice.sound, voice.volume);
927
+
928
+ if (was_playing and c.ma_sound_start(&voice.sound) != c.MA_SUCCESS) {
929
+ clearVoice(voice);
930
+ e.updateActiveVoiceCount();
931
+ return Status.err_device;
932
+ }
933
+
934
+ voice.group_id = group_id;
935
+ return Status.ok;
936
+ }
937
+
938
+ pub fn setGroupVolume(engine: *Engine, group_id: u32, volume: f32) i32 {
939
+ const e = engine;
940
+
941
+ e.lock.lock();
942
+ defer e.lock.unlock();
943
+
944
+ const group_index: usize = @intCast(group_id);
945
+ if (group_index >= e.groups.items.len) return Status.err_invalid;
946
+
947
+ const clamped = clamp(volume, 0, 4);
948
+ e.groups.items[group_index].volume = clamped;
949
+ c.ma_sound_group_set_volume(&e.groups.items[group_index].node, clamped);
950
+ return Status.ok;
951
+ }
952
+
953
+ pub fn setMasterVolume(engine: *Engine, volume: f32) i32 {
954
+ const e = engine;
955
+
956
+ e.lock.lock();
957
+ defer e.lock.unlock();
958
+
959
+ const clamped = clamp(volume, 0, 4);
960
+ const result = c.ma_engine_set_volume(&e.core, clamped);
961
+ if (result != c.MA_SUCCESS) return Status.err_device;
962
+
963
+ e.master_volume = clamped;
964
+ return Status.ok;
965
+ }
966
+
967
+ pub fn enableTap(engine: *Engine, enabled: bool, capacity_frames: u32) i32 {
968
+ const e = engine;
969
+ e.lock.lock();
970
+ defer e.lock.unlock();
971
+
972
+ if (!enabled) {
973
+ e.tap_enabled = false;
974
+ e.tap_capacity_frames = 0;
975
+ e.tap_write_frame = 0;
976
+ e.tap_frame_count = 0;
977
+ if (e.tap_buffer) |buffer| {
978
+ e.allocator.free(buffer);
979
+ e.tap_buffer = null;
980
+ }
981
+ return Status.ok;
982
+ }
983
+
984
+ if (capacity_frames == 0) return Status.err_invalid;
985
+
986
+ const sample_count = std.math.mul(usize, @as(usize, capacity_frames), @as(usize, e.tap_channels)) catch return Status.err_no_space;
987
+ const next_buffer = e.allocator.alloc(f32, sample_count) catch return Status.err_no_space;
988
+ @memset(next_buffer, 0);
989
+
990
+ if (e.tap_buffer) |buffer| {
991
+ e.allocator.free(buffer);
992
+ }
993
+
994
+ e.tap_buffer = next_buffer;
995
+ e.tap_enabled = true;
996
+ e.tap_capacity_frames = capacity_frames;
997
+ e.tap_write_frame = 0;
998
+ e.tap_frame_count = 0;
999
+ return Status.ok;
1000
+ }
1001
+
1002
+ pub fn readTap(engine: *Engine, out_ptr: ?[*]f32, frame_count: u32, channels: u8, out_frames_read: ?*u32) i32 {
1003
+ if (out_ptr == null or out_frames_read == null) return Status.err_invalid;
1004
+ if (channels == 0) return Status.err_invalid;
1005
+
1006
+ const e = engine;
1007
+ e.lock.lock();
1008
+ defer e.lock.unlock();
1009
+
1010
+ const out = @as([*]f32, @ptrCast(out_ptr.?))[0 .. @as(usize, frame_count) * @as(usize, channels)];
1011
+ @memset(out, 0);
1012
+
1013
+ if (!e.tap_enabled or e.tap_capacity_frames == 0 or e.tap_frame_count == 0) {
1014
+ out_frames_read.?.* = 0;
1015
+ return Status.ok;
1016
+ }
1017
+
1018
+ const tap_buffer = e.tap_buffer orelse {
1019
+ out_frames_read.?.* = 0;
1020
+ return Status.ok;
1021
+ };
1022
+
1023
+ const available = @min(frame_count, e.tap_frame_count);
1024
+ if (available == 0) {
1025
+ out_frames_read.?.* = 0;
1026
+ return Status.ok;
1027
+ }
1028
+
1029
+ const tap_channels: usize = e.tap_channels;
1030
+ const capacity_frames: usize = e.tap_capacity_frames;
1031
+ const start_frame_u32 = if (e.tap_write_frame >= available)
1032
+ e.tap_write_frame - available
1033
+ else
1034
+ e.tap_capacity_frames - (available - e.tap_write_frame);
1035
+
1036
+ for (0..@as(usize, available)) |i| {
1037
+ const src_frame = (@as(usize, start_frame_u32) + i) % capacity_frames;
1038
+ const src = src_frame * tap_channels;
1039
+ const left = tap_buffer[src];
1040
+ const right = if (tap_channels > 1) tap_buffer[src + 1] else left;
1041
+
1042
+ const dst = i * @as(usize, channels);
1043
+ if (channels == 1) {
1044
+ out[dst] = clamp((left + right) * 0.5, -1, 1);
1045
+ continue;
1046
+ }
1047
+
1048
+ out[dst] = left;
1049
+ out[dst + 1] = right;
1050
+ }
1051
+
1052
+ out_frames_read.?.* = available;
1053
+ return Status.ok;
1054
+ }
1055
+
1056
+ fn audioCallback(device_ptr: ?*c.ma_device, output_ptr: ?*anyopaque, input_ptr: ?*const anyopaque, frame_count: c.ma_uint32) callconv(.c) void {
1057
+ _ = input_ptr;
1058
+ if (device_ptr == null or output_ptr == null) return;
1059
+
1060
+ const device = device_ptr.?;
1061
+ const output_channels = std.math.cast(usize, device.playback.channels) orelse return;
1062
+ if (output_channels == 0) return;
1063
+
1064
+ const stats_channels: u8 = if (output_channels <= std.math.maxInt(u8)) @intCast(output_channels) else std.math.maxInt(u8);
1065
+ const aligned_output: *align(@alignOf(f32)) anyopaque = @alignCast(output_ptr.?);
1066
+ const out = @as([*]f32, @ptrCast(aligned_output))[0 .. @as(usize, frame_count) * output_channels];
1067
+
1068
+ const user_data = device.pUserData orelse {
1069
+ @memset(out, 0);
1070
+ return;
1071
+ };
1072
+
1073
+ const engine: *Engine = @ptrCast(@alignCast(user_data));
1074
+
1075
+ if (!engine.lock.tryLock()) {
1076
+ @memset(out, 0);
1077
+ incrementLockMisses(engine);
1078
+ return;
1079
+ }
1080
+ defer engine.lock.unlock();
1081
+
1082
+ if (!engine.started) {
1083
+ @memset(out, 0);
1084
+ updateStatsFromBuffer(engine, out, @intCast(frame_count), stats_channels);
1085
+ reapFinishedVoices(engine);
1086
+ return;
1087
+ }
1088
+
1089
+ if (output_channels == 2) {
1090
+ const status = readEngineStereo(engine, out, @intCast(frame_count));
1091
+ if (status != Status.ok) {
1092
+ @memset(out, 0);
1093
+ }
1094
+ } else {
1095
+ @memset(out, 0);
1096
+
1097
+ var remaining: usize = frame_count;
1098
+ var frame_offset: usize = 0;
1099
+ var temp_stereo: [2048]f32 = undefined;
1100
+
1101
+ while (remaining > 0) {
1102
+ const chunk_frames: usize = @min(remaining, 1024);
1103
+ const stereo_slice = temp_stereo[0 .. chunk_frames * 2];
1104
+ const status = readEngineStereo(engine, stereo_slice, @intCast(chunk_frames));
1105
+ if (status != Status.ok) {
1106
+ @memset(out, 0);
1107
+ break;
1108
+ }
1109
+
1110
+ for (0..chunk_frames) |i| {
1111
+ const l = stereo_slice[i * 2];
1112
+ const r = stereo_slice[i * 2 + 1];
1113
+ const dst = (frame_offset + i) * output_channels;
1114
+ if (output_channels == 1) {
1115
+ out[dst] = clamp((l + r) * 0.5, -1, 1);
1116
+ } else {
1117
+ out[dst] = l;
1118
+ out[dst + 1] = r;
1119
+ }
1120
+ }
1121
+
1122
+ frame_offset += chunk_frames;
1123
+ remaining -= chunk_frames;
1124
+ }
1125
+ }
1126
+
1127
+ writeTapFrames(engine, out, @intCast(frame_count), stats_channels);
1128
+
1129
+ updateStatsFromBuffer(engine, out, @intCast(frame_count), stats_channels);
1130
+ reapFinishedVoices(engine);
1131
+ }
1132
+
1133
+ pub fn mixToBuffer(engine: *Engine, out_ptr: ?[*]f32, frame_count: u32, channels: u8) i32 {
1134
+ if (out_ptr == null) return Status.err_invalid;
1135
+ if (channels == 0) return Status.err_invalid;
1136
+
1137
+ const e = engine;
1138
+ e.lock.lock();
1139
+ defer e.lock.unlock();
1140
+
1141
+ const out = @as([*]f32, @ptrCast(out_ptr.?))[0 .. @as(usize, frame_count) * @as(usize, channels)];
1142
+ @memset(out, 0);
1143
+
1144
+ if (!e.started or frame_count == 0) {
1145
+ updateStatsFromBuffer(e, out, frame_count, channels);
1146
+ reapFinishedVoices(e);
1147
+ return Status.ok;
1148
+ }
1149
+
1150
+ if (channels == 2) {
1151
+ const status = readEngineStereo(e, out, frame_count);
1152
+ if (status != Status.ok) return status;
1153
+
1154
+ writeTapFrames(e, out, frame_count, channels);
1155
+
1156
+ updateStatsFromBuffer(e, out, frame_count, channels);
1157
+ reapFinishedVoices(e);
1158
+ return Status.ok;
1159
+ }
1160
+
1161
+ var remaining: usize = frame_count;
1162
+ var frame_offset: usize = 0;
1163
+ var temp_stereo: [2048]f32 = undefined;
1164
+
1165
+ while (remaining > 0) {
1166
+ const chunk_frames: usize = @min(remaining, 1024);
1167
+ const stereo_slice = temp_stereo[0 .. chunk_frames * 2];
1168
+ const status = readEngineStereo(e, stereo_slice, @intCast(chunk_frames));
1169
+ if (status != Status.ok) return status;
1170
+
1171
+ for (0..chunk_frames) |i| {
1172
+ const l = stereo_slice[i * 2];
1173
+ const r = stereo_slice[i * 2 + 1];
1174
+ const dst = (frame_offset + i) * @as(usize, channels);
1175
+
1176
+ if (channels == 1) {
1177
+ out[dst] = clamp((l + r) * 0.5, -1, 1);
1178
+ } else {
1179
+ out[dst] = l;
1180
+ out[dst + 1] = r;
1181
+ }
1182
+ }
1183
+
1184
+ frame_offset += chunk_frames;
1185
+ remaining -= chunk_frames;
1186
+ }
1187
+
1188
+ writeTapFrames(e, out, frame_count, channels);
1189
+
1190
+ updateStatsFromBuffer(e, out, frame_count, channels);
1191
+ reapFinishedVoices(e);
1192
+ return Status.ok;
1193
+ }
1194
+
1195
+ pub fn getStats(engine: *Engine, out_stats: ?*Stats) i32 {
1196
+ if (out_stats == null) return Status.err_invalid;
1197
+
1198
+ const e = engine;
1199
+ e.lock.lock();
1200
+ defer e.lock.unlock();
1201
+
1202
+ e.stats.lock_misses = loadLockMisses(e);
1203
+ reapFinishedVoices(e);
1204
+ out_stats.?.* = e.stats;
1205
+ return Status.ok;
1206
+ }