@carbonenginejs/runtime-resource 0.8.0 → 0.9.1

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 (297) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +31 -29
  3. package/README.md +67 -548
  4. package/dist/CjsMotherLode.js +1271 -1271
  5. package/dist/CjsResMan.js +3005 -3005
  6. package/dist/CjsResManQueue.js +214 -214
  7. package/dist/CjsResource.js +566 -566
  8. package/dist/CjsResourceSource.js +68 -59
  9. package/dist/CjsResourceSource.js.map +1 -1
  10. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  11. package/dist/format/CjsBlueReader.js +269 -269
  12. package/dist/format/CjsFormat.js +197 -192
  13. package/dist/format/CjsFormat.js.map +1 -1
  14. package/dist/format/CjsReader.js +18 -18
  15. package/dist/format/CjsResourceProbe.js +277 -277
  16. package/dist/format/payloadContract.js +173 -173
  17. package/dist/formats/black/CjsBlackFormat.js +278 -278
  18. package/dist/formats/black/core/CjsBlackBinaryReader.js +157 -153
  19. package/dist/formats/black/core/CjsBlackBinaryReader.js.map +1 -1
  20. package/dist/formats/black/core/CjsBlackPropertyReaders.js +387 -382
  21. package/dist/formats/black/core/CjsBlackPropertyReaders.js.map +1 -1
  22. package/dist/formats/black/core/CjsBlackReader.js +639 -639
  23. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +437 -433
  24. package/dist/formats/black/core/CjsBlackSchemaRegistry.js.map +1 -1
  25. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js +4 -0
  26. package/dist/formats/black/core/black-schema-v1-2026-07-23.json.js.map +1 -0
  27. package/dist/formats/black/core/blackConstants.js +7 -7
  28. package/dist/formats/black/core/blackDefinitions.js +10 -10
  29. package/dist/formats/black/core/blackDefinitions.js.map +1 -1
  30. package/dist/formats/black/core/blackEnums.js +6 -6
  31. package/dist/formats/black/core/blackSchema.js +3 -3
  32. package/dist/formats/black/core/blackVersion.js +22 -22
  33. package/dist/formats/black/core/helpers.js +182 -182
  34. package/dist/formats/black/core/schema.js +4 -4
  35. package/dist/formats/black/index.js +2 -2
  36. package/dist/formats/bnk/CjsBnkFormat.js +125 -125
  37. package/dist/formats/bnk/core/graph.js +140 -140
  38. package/dist/formats/bnk/core/helpers.js +409 -409
  39. package/dist/formats/bnk/core/musicNodes.js +493 -489
  40. package/dist/formats/bnk/core/musicNodes.js.map +1 -1
  41. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  42. package/dist/formats/bnk/index.js +2 -2
  43. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  44. package/dist/formats/cmf/core/binary.js +123 -118
  45. package/dist/formats/cmf/core/binary.js.map +1 -1
  46. package/dist/formats/cmf/core/buffers.js +233 -233
  47. package/dist/formats/cmf/core/constants.js +47 -47
  48. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  49. package/dist/formats/cmf/core/helpers.js +287 -287
  50. package/dist/formats/cmf/core/pack.js +276 -276
  51. package/dist/formats/cmf/core/schema.js +364 -364
  52. package/dist/formats/cmf/core/shared.js +268 -268
  53. package/dist/formats/cmf/core/writer.js +517 -517
  54. package/dist/formats/cmf/index.js +2 -2
  55. package/dist/formats/dds/CjsDdsFormat.js +200 -193
  56. package/dist/formats/dds/CjsDdsFormat.js.map +1 -1
  57. package/dist/formats/dds/core/bc6h.js +288 -288
  58. package/dist/formats/dds/core/bc7.js +256 -251
  59. package/dist/formats/dds/core/bc7.js.map +1 -1
  60. package/dist/formats/dds/core/helpers.js +815 -815
  61. package/dist/formats/dds/index.js +2 -2
  62. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  63. package/dist/formats/fbx/core/helpers.js +3911 -3901
  64. package/dist/formats/fbx/core/helpers.js.map +1 -1
  65. package/dist/formats/fbx/index.js +2 -2
  66. package/dist/formats/flac/CjsFlacFormat.js +93 -87
  67. package/dist/formats/flac/CjsFlacFormat.js.map +1 -1
  68. package/dist/formats/flac/core/helpers.js +295 -295
  69. package/dist/formats/flac/index.js +2 -2
  70. package/dist/formats/gif/CjsGifFormat.js +92 -87
  71. package/dist/formats/gif/CjsGifFormat.js.map +1 -1
  72. package/dist/formats/gif/core/helpers.js +360 -360
  73. package/dist/formats/gif/index.js +2 -2
  74. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  75. package/dist/formats/gltf/core/helpers.js +292 -292
  76. package/dist/formats/gltf/core/json.js +79 -79
  77. package/dist/formats/gltf/core/parser.js +666 -666
  78. package/dist/formats/gltf/core/targets.js +163 -163
  79. package/dist/formats/gltf/index.js +2 -2
  80. package/dist/formats/gr2/CjsGr2Format.js +46 -0
  81. package/dist/formats/gr2/CjsGr2Format.js.map +1 -0
  82. package/dist/formats/gr2/core/CjsFormatGr2.js +273 -0
  83. package/dist/formats/gr2/core/CjsFormatGr2.js.map +1 -0
  84. package/dist/formats/gr2/core/bitknit2.js +280 -0
  85. package/dist/formats/gr2/core/bitknit2.js.map +1 -0
  86. package/dist/formats/gr2/core/curves.js +1047 -0
  87. package/dist/formats/gr2/core/curves.js.map +1 -0
  88. package/dist/formats/gr2/core/gsf.js +72 -0
  89. package/dist/formats/gr2/core/gsf.js.map +1 -0
  90. package/dist/formats/gr2/core/helpers.js +332 -0
  91. package/dist/formats/gr2/core/helpers.js.map +1 -0
  92. package/dist/formats/gr2/core/json.js +622 -0
  93. package/dist/formats/gr2/core/json.js.map +1 -0
  94. package/dist/formats/gr2/core/oodle1.js +388 -0
  95. package/dist/formats/gr2/core/oodle1.js.map +1 -0
  96. package/dist/formats/gr2/core/reader.js +617 -0
  97. package/dist/formats/gr2/core/reader.js.map +1 -0
  98. package/dist/formats/gr2/core/tangents.js +48 -0
  99. package/dist/formats/gr2/core/tangents.js.map +1 -0
  100. package/dist/formats/gr2/core/targets.js +351 -0
  101. package/dist/formats/gr2/core/targets.js.map +1 -0
  102. package/dist/formats/gr2/index.js +3 -0
  103. package/dist/formats/gr2/index.js.map +1 -0
  104. package/dist/formats/index.js +30 -23
  105. package/dist/formats/index.js.map +1 -1
  106. package/dist/formats/jpeg/CjsJpegFormat.js +212 -206
  107. package/dist/formats/jpeg/CjsJpegFormat.js.map +1 -1
  108. package/dist/formats/jpeg/core/helpers.js +350 -350
  109. package/dist/formats/jpeg/core/jpeg.js +387 -377
  110. package/dist/formats/jpeg/core/jpeg.js.map +1 -1
  111. package/dist/formats/jpeg/index.js +2 -2
  112. package/dist/formats/mp3/CjsMp3Format.js +197 -192
  113. package/dist/formats/mp3/CjsMp3Format.js.map +1 -1
  114. package/dist/formats/mp3/core/helpers.js +338 -338
  115. package/dist/formats/mp3/index.js +2 -2
  116. package/dist/formats/mp4/CjsMp4Format.js +197 -191
  117. package/dist/formats/mp4/CjsMp4Format.js.map +1 -1
  118. package/dist/formats/mp4/core/helpers.js +449 -449
  119. package/dist/formats/mp4/index.js +2 -2
  120. package/dist/formats/obj/CjsObjFormat.js +253 -253
  121. package/dist/formats/obj/core/helpers.js +573 -573
  122. package/dist/formats/obj/core/json.js +64 -64
  123. package/dist/formats/obj/core/parser.js +321 -321
  124. package/dist/formats/obj/index.js +2 -2
  125. package/dist/formats/ogg/CjsOggFormat.js +94 -88
  126. package/dist/formats/ogg/CjsOggFormat.js.map +1 -1
  127. package/dist/formats/ogg/core/helpers.js +387 -387
  128. package/dist/formats/ogg/core/imdct.js +178 -178
  129. package/dist/formats/ogg/core/vorbis.js +1004 -999
  130. package/dist/formats/ogg/core/vorbis.js.map +1 -1
  131. package/dist/formats/ogg/index.js +2 -2
  132. package/dist/formats/png/CjsPngFormat.js +201 -195
  133. package/dist/formats/png/CjsPngFormat.js.map +1 -1
  134. package/dist/formats/png/core/helpers.js +583 -583
  135. package/dist/formats/png/index.js +2 -2
  136. package/dist/formats/red/CjsRedFormat.js +261 -261
  137. package/dist/formats/red/core/CjsRedReader.js +194 -194
  138. package/dist/formats/red/core/blackDefinitions.js +3 -3
  139. package/dist/formats/red/core/helpers.js +139 -139
  140. package/dist/formats/red/core/redGraph.js +68 -68
  141. package/dist/formats/red/core/schema.js +4 -4
  142. package/dist/formats/red/index.js +2 -2
  143. package/dist/formats/stl/CjsStlFormat.js +365 -365
  144. package/dist/formats/stl/core/helpers.js +261 -261
  145. package/dist/formats/stl/core/json.js +51 -51
  146. package/dist/formats/stl/core/stl.js +634 -629
  147. package/dist/formats/stl/core/stl.js.map +1 -1
  148. package/dist/formats/stl/core/targets.js +163 -163
  149. package/dist/formats/stl/index.js +2 -2
  150. package/dist/formats/tga/CjsTgaFormat.js +197 -192
  151. package/dist/formats/tga/CjsTgaFormat.js.map +1 -1
  152. package/dist/formats/tga/core/helpers.js +446 -446
  153. package/dist/formats/tga/index.js +2 -2
  154. package/dist/formats/wav/CjsWavFormat.js +198 -192
  155. package/dist/formats/wav/CjsWavFormat.js.map +1 -1
  156. package/dist/formats/wav/core/helpers.js +328 -328
  157. package/dist/formats/wav/index.js +2 -2
  158. package/dist/formats/webm/CjsWebmFormat.js +197 -191
  159. package/dist/formats/webm/CjsWebmFormat.js.map +1 -1
  160. package/dist/formats/webm/core/helpers.js +537 -537
  161. package/dist/formats/webm/index.js +2 -2
  162. package/dist/formats/webp/CjsWebpFormat.js +91 -86
  163. package/dist/formats/webp/CjsWebpFormat.js.map +1 -1
  164. package/dist/formats/webp/core/helpers.js +214 -214
  165. package/dist/formats/webp/index.js +2 -2
  166. package/dist/formats/wem/CjsWemFormat.js +242 -241
  167. package/dist/formats/wem/CjsWemFormat.js.map +1 -1
  168. package/dist/formats/wem/core/bitStream.js +259 -259
  169. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  170. package/dist/formats/wem/core/helpers.js +417 -417
  171. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  172. package/dist/formats/wem/core/ptadpcm.js +77 -77
  173. package/dist/formats/wem/core/resolve.js +121 -121
  174. package/dist/formats/wem/core/wemToOgg.js +485 -485
  175. package/dist/formats/wem/index.js +2 -2
  176. package/dist/formats/yaml/CjsYamlFormat.js +89 -83
  177. package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
  178. package/dist/formats/yaml/core/CjsYamlReader.js +311 -305
  179. package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
  180. package/dist/formats/yaml/core/helpers.js +160 -160
  181. package/dist/formats/yaml/index.js +2 -2
  182. package/dist/index.js +49 -48
  183. package/dist/index.js.map +1 -1
  184. package/dist/resourcePath.js +18 -18
  185. package/dist/resourceStates.js +9 -9
  186. package/dist/resources/AudioGeometryResData.js +47 -47
  187. package/dist/resources/GStateBindingCallbackData.js +31 -31
  188. package/dist/resources/MeshDecalData.js +37 -37
  189. package/dist/resources/MeshDecalLodData.js +34 -34
  190. package/dist/resources/Tr2EffectRes.js +71 -71
  191. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -60
  192. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  193. package/dist/resources/Tr2ImageRes.js +114 -114
  194. package/dist/resources/Tr2LightProfileRes.js +40 -40
  195. package/dist/resources/Tr2MaterialArea.js +34 -34
  196. package/dist/resources/Tr2MaterialMesh.js +31 -31
  197. package/dist/resources/Tr2MaterialRes.js +34 -34
  198. package/dist/resources/Tr2ShaderPermutation.js +43 -43
  199. package/dist/resources/Tr2TextureLodManager.js +80 -80
  200. package/dist/resources/Tr2TextureLodUpdateRequest.js +37 -37
  201. package/dist/resources/Tr2TexturePackChannel.js +37 -37
  202. package/dist/resources/Tr2TexturePipeline.js +52 -52
  203. package/dist/resources/Tr2TexturePipelineParams.js +34 -34
  204. package/dist/resources/Tr2TexturePipelineStepCompress.js +40 -40
  205. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js +22 -0
  206. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
  207. package/dist/resources/Tr2TexturePipelineStepLimitSize.js +34 -34
  208. package/dist/resources/Tr2TexturePipelineStepLoad.js +31 -31
  209. package/dist/resources/Tr2TexturePipelineStepPack.js +43 -43
  210. package/dist/resources/TriGeometryRes.js +239 -239
  211. package/dist/resources/TriGeometryResAreaData.js +59 -59
  212. package/dist/resources/TriGeometryResJointData.js +38 -38
  213. package/dist/resources/TriGeometryResLodData.js +88 -88
  214. package/dist/resources/TriGeometryResMeshData.js +63 -63
  215. package/dist/resources/TriGeometryResSkeletonData.js +34 -34
  216. package/dist/resources/TriGrannyRes.js +43 -43
  217. package/dist/resources/TriJointBinding.js +38 -38
  218. package/dist/resources/TriMorphTargetGeometryConstants.js +46 -46
  219. package/dist/resources/TriRtGeometryConstants.js +88 -88
  220. package/dist/resources/TriTextureRes.js +304 -304
  221. package/dist/resources/enums.js +18 -18
  222. package/dist/resources/resourceBoundary.js +47 -47
  223. package/dist/resources/texturePipelineBehavior.js +308 -308
  224. package/dist/texture/CjsTextureArrayRes.js +406 -406
  225. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  226. package/docs/README.md +73 -0
  227. package/docs/architecture.md +86 -0
  228. package/docs/concepts/resource-lifecycle.md +217 -0
  229. package/docs/formats/README.md +105 -0
  230. package/docs/formats/gr2.md +161 -0
  231. package/{FORMAT-PROVENANCE.md → docs/formats/provenance.md} +173 -155
  232. package/docs/formats/stl.md +37 -0
  233. package/docs/formats/wwise.md +44 -0
  234. package/docs/reference/classes/README.md +33 -0
  235. package/docs/reference/classes/core.md +106 -0
  236. package/docs/reference/classes/dropped.md +46 -0
  237. package/docs/reference/classes/formats.md +522 -0
  238. package/docs/reference/classes/resources.md +346 -0
  239. package/docs/reference/classes/texture.md +26 -0
  240. package/docs/reference/events.md +92 -0
  241. package/docs/reference/motherlode-cache.md +244 -0
  242. package/docs/reference/queues.md +102 -0
  243. package/docs/reference/reload.md +107 -0
  244. package/docs/reference/texture-arrays.md +113 -0
  245. package/docs/reference/texture-pipeline.md +53 -0
  246. package/docs/roadmap.md +104 -0
  247. package/format-notices/black/LICENSE +21 -21
  248. package/format-notices/black/NOTICE +47 -47
  249. package/format-notices/bnk/LICENSE +21 -21
  250. package/format-notices/bnk/NOTICE +20 -20
  251. package/format-notices/cmf/LICENSE +21 -21
  252. package/format-notices/cmf/NOTICE +36 -36
  253. package/format-notices/dds/LICENSE +21 -21
  254. package/format-notices/dds/NOTICE +14 -14
  255. package/format-notices/fbx/LICENSE +21 -21
  256. package/format-notices/fbx/NOTICE +14 -14
  257. package/format-notices/flac/LICENSE +21 -21
  258. package/format-notices/flac/NOTICE +14 -14
  259. package/format-notices/gif/LICENSE +21 -21
  260. package/format-notices/gif/NOTICE +14 -14
  261. package/format-notices/gltf/LICENSE +21 -21
  262. package/format-notices/gltf/NOTICE +27 -27
  263. package/format-notices/gr2/LICENSE +21 -0
  264. package/format-notices/gr2/NOTICE +60 -0
  265. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -0
  266. package/format-notices/jpeg/LICENSE +21 -21
  267. package/format-notices/jpeg/NOTICE +14 -14
  268. package/format-notices/mp3/LICENSE +21 -21
  269. package/format-notices/mp3/NOTICE +14 -14
  270. package/format-notices/mp4/LICENSE +21 -21
  271. package/format-notices/mp4/NOTICE +14 -14
  272. package/format-notices/obj/LICENSE +21 -21
  273. package/format-notices/obj/NOTICE +26 -26
  274. package/format-notices/ogg/LICENSE +21 -21
  275. package/format-notices/ogg/NOTICE +28 -28
  276. package/format-notices/png/LICENSE +21 -21
  277. package/format-notices/png/NOTICE +14 -14
  278. package/format-notices/red/LICENSE +21 -21
  279. package/format-notices/red/NOTICE +31 -31
  280. package/format-notices/stl/LICENSE +21 -21
  281. package/format-notices/stl/NOTICE +21 -21
  282. package/format-notices/tga/LICENSE +21 -21
  283. package/format-notices/tga/NOTICE +14 -14
  284. package/format-notices/wav/LICENSE +21 -21
  285. package/format-notices/wav/NOTICE +14 -14
  286. package/format-notices/webm/LICENSE +21 -21
  287. package/format-notices/webm/NOTICE +14 -14
  288. package/format-notices/webp/LICENSE +21 -21
  289. package/format-notices/webp/NOTICE +14 -14
  290. package/format-notices/wem/LICENSE +57 -57
  291. package/format-notices/wem/NOTICE +33 -33
  292. package/format-notices/yaml/LICENSE +21 -21
  293. package/format-notices/yaml/NOTICE +44 -44
  294. package/package.json +52 -51
  295. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +0 -4
  296. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js.map +0 -1
  297. package/resource-lifecycle.md +0 -679
@@ -1,999 +1,1004 @@
1
- import { imdct, vorbisWindowSlope } from './imdct.js';
2
-
3
- /**
4
- * Pure-JS Ogg Vorbis PCM decoder.
5
- *
6
- * Implements Vorbis I decode: setup-header codebooks (huffman + VQ), floor
7
- * type 1 with the spec's exact integer line rendering, residue types 0/1/2,
8
- * square-polar channel de-coupling, IMDCT, and windowed center-aligned
9
- * overlap-add with final granule trimming. Behavior follows the Vorbis I
10
- * specification with stb_vorbis (public domain) consulted as a reference;
11
- * floor type 0 streams are rejected (never produced by modern encoders).
12
- */
13
-
14
- const NO_CODE = 255;
15
- const FAST_BITS = 10;
16
- const FAST_SIZE = 1 << FAST_BITS;
17
-
18
- /**
19
- * The floor1 amplitude table from the Vorbis I specification
20
- * (inverse_db_table); indexed by the integer floor curve value.
21
- */
22
- const INVERSE_DB_TABLE = new Float32Array([1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07, 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07, 1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07, 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07, 2.9163793e-07, 3.1059021e-07, 3.3077411e-07, 3.5226968e-07, 3.7516214e-07, 3.9954229e-07, 4.2550680e-07, 4.5315863e-07, 4.8260743e-07, 5.1396998e-07, 5.4737065e-07, 5.8294187e-07, 6.2082472e-07, 6.6116941e-07, 7.0413592e-07, 7.4989464e-07, 7.9862701e-07, 8.5052630e-07, 9.0579828e-07, 9.6466216e-07, 1.0273513e-06, 1.0941144e-06, 1.1652161e-06, 1.2409384e-06, 1.3215816e-06, 1.4074654e-06, 1.4989305e-06, 1.5963394e-06, 1.7000785e-06, 1.8105592e-06, 1.9282195e-06, 2.0535261e-06, 2.1869758e-06, 2.3290978e-06, 2.4804557e-06, 2.6416497e-06, 2.8133190e-06, 2.9961443e-06, 3.1908506e-06, 3.3982101e-06, 3.6190449e-06, 3.8542308e-06, 4.1047004e-06, 4.3714470e-06, 4.6555282e-06, 4.9580707e-06, 5.2802740e-06, 5.6234160e-06, 5.9888572e-06, 6.3780469e-06, 6.7925283e-06, 7.2339451e-06, 7.7040476e-06, 8.2047000e-06, 8.7378876e-06, 9.3057248e-06, 9.9104632e-06, 1.0554501e-05, 1.1240392e-05, 1.1970856e-05, 1.2748789e-05, 1.3577278e-05, 1.4459606e-05, 1.5399272e-05, 1.6400004e-05, 1.7465768e-05, 1.8600792e-05, 1.9809576e-05, 2.1096914e-05, 2.2467911e-05, 2.3928002e-05, 2.5482978e-05, 2.7139006e-05, 2.8902651e-05, 3.0780908e-05, 3.2781225e-05, 3.4911534e-05, 3.7180282e-05, 3.9596466e-05, 4.2169667e-05, 4.4910090e-05, 4.7828601e-05, 5.0936773e-05, 5.4246931e-05, 5.7772202e-05, 6.1526565e-05, 6.5524908e-05, 6.9783085e-05, 7.4317983e-05, 7.9147585e-05, 8.4291040e-05, 8.9768747e-05, 9.5602426e-05, 0.00010181521, 0.00010843174, 0.00011547824, 0.00012298267, 0.00013097477, 0.00013948625, 0.00014855085, 0.00015820453, 0.00016848555, 0.00017943469, 0.00019109536, 0.00020351382, 0.00021673929, 0.00023082423, 0.00024582449, 0.00026179955, 0.00027881276, 0.00029693158, 0.00031622787, 0.00033677814, 0.00035866388, 0.00038197188, 0.00040679456, 0.00043323036, 0.00046138411, 0.00049136745, 0.00052329927, 0.00055730621, 0.00059352311, 0.00063209358, 0.00067317058, 0.00071691700, 0.00076350630, 0.00081312324, 0.00086596457, 0.00092223983, 0.00098217216, 0.0010459992, 0.0011139742, 0.0011863665, 0.0012634633, 0.0013455702, 0.0014330129, 0.0015261382, 0.0016253153, 0.0017309374, 0.0018434235, 0.0019632195, 0.0020908006, 0.0022266726, 0.0023713743, 0.0025254795, 0.0026895994, 0.0028643847, 0.0030505286, 0.0032487691, 0.0034598925, 0.0036847358, 0.0039241906, 0.0041792066, 0.0044507950, 0.0047400328, 0.0050480668, 0.0053761186, 0.0057254891, 0.0060975636, 0.0064938176, 0.0069158225, 0.0073652516, 0.0078438871, 0.0083536271, 0.0088964928, 0.0094746370, 0.0100903520, 0.0107460800, 0.0114444210, 0.0121881440, 0.0129801980, 0.0138237250, 0.0147220680, 0.0156787910, 0.0166976870, 0.0177827970, 0.0189384230, 0.0201691490, 0.0214798540, 0.0228757350, 0.0243623300, 0.0259455310, 0.0276316180, 0.0294272760, 0.0313396260, 0.0333762520, 0.0355452280, 0.0378551570, 0.0403151990, 0.0429351080, 0.0457252730, 0.0486967580, 0.0518613480, 0.0552315910, 0.0588208500, 0.0626433610, 0.0667142790, 0.0710497490, 0.0756669620, 0.0805842270, 0.0858210440, 0.0913981790, 0.0973377470, 0.1036633000, 0.1103999300, 0.1175743400, 0.1252149800, 0.1333521500, 0.1420181300, 0.1512472700, 0.1610761700, 0.1715438000, 0.1826916800, 0.1945640200, 0.2072078800, 0.2206734200, 0.2350140200, 0.2502865600, 0.2665515900, 0.2838736100, 0.3023213200, 0.3219678600, 0.3428911400, 0.3651741400, 0.3889052100, 0.4141784700, 0.4410941200, 0.4697589000, 0.5002864800, 0.5327979100, 0.5674221200, 0.6042964000, 0.6435669900, 0.6853895900, 0.7299300700, 0.7773650400, 0.8278826000, 0.8816830700, 0.9389798000, 1.0]);
23
- function decodeError(message) {
24
- const error = new Error(`ogg: ${message}`);
25
- error.code = "CJS_FORMAT_PARSE_ERROR";
26
- return error;
27
- }
28
- function ilog(value) {
29
- let result = 0;
30
- let v = value | 0;
31
- if (v < 0) return 0;
32
- while (v) {
33
- result++;
34
- v >>= 1;
35
- }
36
- return result;
37
- }
38
- function float32Unpack(x) {
39
- const mantissa = x & 0x1fffff;
40
- const sign = x & 0x80000000;
41
- const exponent = (x & 0x7fe00000) >>> 21;
42
- const value = sign ? -mantissa : mantissa;
43
- return value * Math.pow(2, exponent - 788);
44
- }
45
- function lookup1Values(entries, dimensions) {
46
- if (dimensions === 0) return 0;
47
- const bits = ilog(entries);
48
- let vals = entries >>> Math.floor((bits - 1) * (dimensions - 1) / dimensions);
49
- for (;;) {
50
- let acc = 1;
51
- let acc1 = 1;
52
- for (let i = 0; i < dimensions; i++) {
53
- acc *= vals;
54
- acc1 *= vals + 1;
55
- }
56
- if (acc <= entries && acc1 > entries) return vals;
57
- if (acc > entries) vals--;else vals++;
58
- }
59
- }
60
-
61
- /**
62
- * LSB-first bit reader over one Vorbis packet; reading past the end sets
63
- * `eop` instead of throwing (end-of-packet is a defined decode condition).
64
- */
65
- class PacketReader {
66
- constructor(bytes) {
67
- this.bytes = bytes;
68
- this.position = 0;
69
- this.bitBuffer = 0;
70
- this.bitsLeft = 0;
71
- this.eop = false;
72
- }
73
- readBits(count) {
74
- let value = 0;
75
- for (let i = 0; i < count; i++) {
76
- if (this.bitsLeft === 0) {
77
- if (this.position >= this.bytes.length) {
78
- this.eop = true;
79
- return -1;
80
- }
81
- this.bitBuffer = this.bytes[this.position++];
82
- this.bitsLeft = 8;
83
- }
84
- value |= (this.bitBuffer & 1) << i;
85
- this.bitBuffer >>= 1;
86
- this.bitsLeft--;
87
- }
88
- return value >>> 0;
89
- }
90
- }
91
- class Codebook {
92
- constructor() {
93
- this.dimensions = 0;
94
- this.entries = 0;
95
- this.lookupType = 0;
96
- this.sequenceP = false;
97
- this.quantvals = 0;
98
- this.values = null;
99
- this.fastTable = null;
100
- this.treeNodes = null;
101
- this.maxLength = 0;
102
- }
103
- buildHuffman(lengths) {
104
- const available = new Uint32Array(33);
105
- this.fastTable = new Int32Array(FAST_SIZE).fill(-1);
106
- // flat binary trie: two child slots per node; negative = -(symbol+1)
107
- const nodes = [0, 0];
108
- let assigned = false;
109
- for (let symbol = 0; symbol < lengths.length; symbol++) {
110
- const length = lengths[symbol];
111
- if (length === NO_CODE) continue;
112
- if (length > this.maxLength) this.maxLength = length;
113
- let code;
114
- if (!assigned) {
115
- code = 0;
116
- assigned = true;
117
- for (let i = 1; i <= length; i++) available[i] = 1 << 32 - i >>> 0;
118
- } else {
119
- let z = length;
120
- while (z > 0 && !available[z]) z--;
121
- if (z === 0) throw decodeError("over-specified huffman codebook");
122
- code = available[z];
123
- available[z] = 0;
124
- if (z !== length) {
125
- for (let y = length; y > z; y--) {
126
- available[y] = code + (1 << 32 - y) >>> 0;
127
- }
128
- }
129
- }
130
-
131
- // reversed code: first stream bit in bit 0
132
- let reversed = 0;
133
- for (let i = 0; i < length; i++) {
134
- reversed |= (code >>> 31 - i & 1) << i;
135
- }
136
- reversed >>>= 0;
137
- if (length <= FAST_BITS) {
138
- for (let z = reversed; z < FAST_SIZE; z += 1 << length) {
139
- this.fastTable[z] = symbol << 6 | length;
140
- }
141
- } else {
142
- let node = 0;
143
- for (let i = 0; i < length; i++) {
144
- const bit = reversed >>> i & 1;
145
- const slot = node * 2 + bit;
146
- if (i === length - 1) {
147
- nodes[slot] = -(symbol + 1);
148
- } else {
149
- if (!nodes[slot]) {
150
- nodes[slot] = nodes.length / 2;
151
- nodes.push(0, 0);
152
- }
153
- node = nodes[slot];
154
- }
155
- }
156
- }
157
- }
158
- this.treeNodes = nodes;
159
- }
160
- decode(reader) {
161
- // fast path: peek up to FAST_BITS without consuming
162
- let peek = 0;
163
- let peekBits = 0;
164
- const savedPosition = reader.position;
165
- const savedBuffer = reader.bitBuffer;
166
- const savedLeft = reader.bitsLeft;
167
- while (peekBits < FAST_BITS) {
168
- const bit = reader.readBits(1);
169
- if (bit < 0) break;
170
- peek |= bit << peekBits;
171
- peekBits++;
172
- }
173
- reader.position = savedPosition;
174
- reader.bitBuffer = savedBuffer;
175
- reader.bitsLeft = savedLeft;
176
- reader.eop = false;
177
- if (peekBits === FAST_BITS) {
178
- const hit = this.fastTable[peek];
179
- if (hit >= 0) {
180
- reader.readBits(hit & 63);
181
- return hit >> 6;
182
- }
183
- } else {
184
- // short remainder: try fast table over what's left
185
- const hit = this.fastTable[peek];
186
- if (hit >= 0 && (hit & 63) <= peekBits) {
187
- reader.readBits(hit & 63);
188
- return hit >> 6;
189
- }
190
- }
191
-
192
- // slow path: walk the trie bit by bit
193
- let node = 0;
194
- for (let i = 0; i < this.maxLength; i++) {
195
- const bit = reader.readBits(1);
196
- if (bit < 0) return -1;
197
- const value = this.treeNodes[node * 2 + bit];
198
- if (value < 0) return -value - 1;
199
- if (value === 0) throw decodeError("invalid huffman code in stream");
200
- node = value;
201
- }
202
- throw decodeError("huffman walk exceeded max code length");
203
- }
204
- vqValue(symbol, element) {
205
- if (this.lookupType === 1) {
206
- let index = symbol;
207
- for (let i = 0; i < element; i++) index = Math.floor(index / this.quantvals);
208
- return this.values[index % this.quantvals];
209
- }
210
- return this.values[symbol * this.dimensions + element];
211
- }
212
- }
213
- function parseCodebook(reader) {
214
- const sync = reader.readBits(24);
215
- if (sync !== 0x564342) throw decodeError("invalid codebook sync pattern");
216
- const book = new Codebook();
217
- book.dimensions = reader.readBits(16);
218
- book.entries = reader.readBits(24);
219
- const lengths = new Uint8Array(book.entries);
220
- const ordered = reader.readBits(1);
221
- if (ordered) {
222
- let currentEntry = 0;
223
- let currentLength = reader.readBits(5) + 1;
224
- while (currentEntry < book.entries) {
225
- const number = reader.readBits(ilog(book.entries - currentEntry));
226
- if (reader.eop) throw decodeError("codebook lengths truncated");
227
- for (let i = 0; i < number; i++) lengths[currentEntry + i] = currentLength;
228
- currentEntry += number;
229
- currentLength++;
230
- if (currentEntry > book.entries) throw decodeError("codebook entry overflow");
231
- }
232
- } else {
233
- const sparse = reader.readBits(1);
234
- for (let i = 0; i < book.entries; i++) {
235
- const present = sparse ? reader.readBits(1) : 1;
236
- lengths[i] = present ? reader.readBits(5) + 1 : NO_CODE;
237
- }
238
- }
239
- if (reader.eop) throw decodeError("codebook lengths truncated");
240
- book.buildHuffman(lengths);
241
- book.lookupType = reader.readBits(4);
242
- if (book.lookupType > 2) throw decodeError(`invalid codebook lookup type ${book.lookupType}`);
243
- if (book.lookupType > 0) {
244
- const minimum = float32Unpack(reader.readBits(32));
245
- const delta = float32Unpack(reader.readBits(32));
246
- const valueBits = reader.readBits(4) + 1;
247
- book.sequenceP = reader.readBits(1) === 1;
248
- const count = book.lookupType === 1 ? lookup1Values(book.entries, book.dimensions) : book.entries * book.dimensions;
249
- book.quantvals = book.lookupType === 1 ? count : 0;
250
- book.values = new Float32Array(count);
251
- for (let i = 0; i < count; i++) {
252
- book.values[i] = minimum + reader.readBits(valueBits) * delta;
253
- }
254
- if (reader.eop) throw decodeError("codebook lookup values truncated");
255
- }
256
- return book;
257
- }
258
- function parseFloor(reader, codebooks) {
259
- const type = reader.readBits(16);
260
- if (type !== 1) {
261
- throw decodeError(`floor type ${type} is not supported (only floor 1)`);
262
- }
263
- const floor = {
264
- partitions: reader.readBits(5),
265
- partitionClassList: [],
266
- classDimensions: [],
267
- classSubclasses: [],
268
- classMasterbooks: [],
269
- subclassBooks: [],
270
- multiplier: 0,
271
- rangebits: 0,
272
- xList: [0, 0]
273
- };
274
- let maximumClass = -1;
275
- for (let i = 0; i < floor.partitions; i++) {
276
- const partitionClass = reader.readBits(4);
277
- floor.partitionClassList.push(partitionClass);
278
- if (partitionClass > maximumClass) maximumClass = partitionClass;
279
- }
280
- for (let i = 0; i <= maximumClass; i++) {
281
- floor.classDimensions.push(reader.readBits(3) + 1);
282
- const subclasses = reader.readBits(2);
283
- floor.classSubclasses.push(subclasses);
284
- floor.classMasterbooks.push(subclasses ? reader.readBits(8) : -1);
285
- const books = [];
286
- for (let j = 0; j < 1 << subclasses; j++) {
287
- books.push(reader.readBits(8) - 1);
288
- }
289
- floor.subclassBooks.push(books);
290
- }
291
- floor.multiplier = reader.readBits(2) + 1;
292
- floor.rangebits = reader.readBits(4);
293
- floor.xList = [0, 1 << floor.rangebits];
294
- for (let i = 0; i < floor.partitions; i++) {
295
- for (let j = 0; j < floor.classDimensions[floor.partitionClassList[i]]; j++) {
296
- floor.xList.push(reader.readBits(floor.rangebits));
297
- }
298
- }
299
- if (reader.eop) throw decodeError("floor configuration truncated");
300
- for (const bookIndex of [...floor.classMasterbooks, ...floor.subclassBooks.flat()]) {
301
- if (bookIndex >= codebooks.length) throw decodeError("floor references invalid codebook");
302
- }
303
-
304
- // sorted render order over unique X positions, computed once
305
- floor.sortedIndex = floor.xList.map((x, index) => ({
306
- x,
307
- index
308
- })).sort((a, b) => a.x - b.x || a.index - b.index).map(entry => entry.index);
309
-
310
- // neighbor tables (over original ordering) computed once
311
- floor.lowNeighbors = [];
312
- floor.highNeighbors = [];
313
- for (let i = 2; i < floor.xList.length; i++) {
314
- let low = 0;
315
- let high = 1;
316
- for (let j = 0; j < i; j++) {
317
- if (floor.xList[j] < floor.xList[i] && floor.xList[j] >= floor.xList[low]) low = j;
318
- if (floor.xList[j] > floor.xList[i] && floor.xList[j] <= floor.xList[high]) high = j;
319
- }
320
- floor.lowNeighbors.push(low);
321
- floor.highNeighbors.push(high);
322
- }
323
- return floor;
324
- }
325
- function parseResidue(reader, codebooks) {
326
- const type = reader.readBits(16);
327
- if (type > 2) throw decodeError(`invalid residue type ${type}`);
328
- const residue = {
329
- type,
330
- begin: reader.readBits(24),
331
- end: reader.readBits(24),
332
- partSize: reader.readBits(24) + 1,
333
- classifications: reader.readBits(6) + 1,
334
- classbook: reader.readBits(8),
335
- books: []
336
- };
337
- if (residue.classbook >= codebooks.length) throw decodeError("residue classbook out of range");
338
- const cascades = [];
339
- for (let i = 0; i < residue.classifications; i++) {
340
- const lowBits = reader.readBits(3);
341
- const flag = reader.readBits(1);
342
- const highBits = flag ? reader.readBits(5) : 0;
343
- cascades.push(highBits * 8 + lowBits);
344
- }
345
- for (let i = 0; i < residue.classifications; i++) {
346
- const passBooks = [];
347
- for (let pass = 0; pass < 8; pass++) {
348
- if (cascades[i] & 1 << pass) {
349
- const bookIndex = reader.readBits(8);
350
- if (bookIndex >= codebooks.length) throw decodeError("residue book out of range");
351
- if (codebooks[bookIndex].lookupType === 0) throw decodeError("residue book has no lookup values");
352
- passBooks.push(bookIndex);
353
- } else {
354
- passBooks.push(-1);
355
- }
356
- }
357
- residue.books.push(passBooks);
358
- }
359
- if (reader.eop) throw decodeError("residue configuration truncated");
360
- return residue;
361
- }
362
- function parseMapping(reader, channels, floorCount, residueCount) {
363
- const type = reader.readBits(16);
364
- if (type !== 0) throw decodeError(`invalid mapping type ${type}`);
365
- const mapping = {
366
- submaps: 1,
367
- couplingSteps: [],
368
- mux: new Array(channels).fill(0),
369
- submapFloor: [],
370
- submapResidue: []
371
- };
372
- if (reader.readBits(1)) mapping.submaps = reader.readBits(4) + 1;
373
- if (reader.readBits(1)) {
374
- const steps = reader.readBits(8) + 1;
375
- const bits = ilog(channels - 1);
376
- for (let i = 0; i < steps; i++) {
377
- const magnitude = reader.readBits(bits);
378
- const angle = reader.readBits(bits);
379
- if (magnitude === angle || magnitude >= channels || angle >= channels) {
380
- throw decodeError("invalid channel coupling");
381
- }
382
- mapping.couplingSteps.push({
383
- magnitude,
384
- angle
385
- });
386
- }
387
- }
388
- if (reader.readBits(2) !== 0) throw decodeError("mapping reserved bits nonzero");
389
- if (mapping.submaps > 1) {
390
- for (let i = 0; i < channels; i++) {
391
- mapping.mux[i] = reader.readBits(4);
392
- if (mapping.mux[i] >= mapping.submaps) throw decodeError("mapping mux out of range");
393
- }
394
- }
395
- for (let i = 0; i < mapping.submaps; i++) {
396
- reader.readBits(8);
397
- const floorIndex = reader.readBits(8);
398
- const residueIndex = reader.readBits(8);
399
- if (floorIndex >= floorCount || residueIndex >= residueCount) {
400
- throw decodeError("mapping references invalid floor or residue");
401
- }
402
- mapping.submapFloor.push(floorIndex);
403
- mapping.submapResidue.push(residueIndex);
404
- }
405
- return mapping;
406
- }
407
- const FLOOR1_RANGES = [256, 128, 86, 64];
408
- function renderPoint(x0, y0, x1, y1, x) {
409
- const dy = y1 - y0;
410
- const adx = x1 - x0;
411
- const err = Math.floor(Math.abs(dy) * (x - x0) / adx);
412
- return dy < 0 ? y0 - err : y0 + err;
413
- }
414
- function decodeFloor1Posts(reader, floor, codebooks) {
415
- if (reader.readBits(1) !== 1) return null;
416
- const range = FLOOR1_RANGES[floor.multiplier - 1];
417
- const posts = new Int32Array(floor.xList.length);
418
- const yBits = ilog(range - 1);
419
- posts[0] = reader.readBits(yBits);
420
- posts[1] = reader.readBits(yBits);
421
- let offset = 2;
422
- for (let i = 0; i < floor.partitions; i++) {
423
- const partitionClass = floor.partitionClassList[i];
424
- const cdim = floor.classDimensions[partitionClass];
425
- const cbits = floor.classSubclasses[partitionClass];
426
- const csub = (1 << cbits) - 1;
427
- let cval = 0;
428
- if (cbits) {
429
- cval = codebooks[floor.classMasterbooks[partitionClass]].decode(reader);
430
- if (cval < 0) return null;
431
- }
432
- for (let j = 0; j < cdim; j++) {
433
- const bookIndex = floor.subclassBooks[partitionClass][cval & csub];
434
- cval >>= cbits;
435
- if (bookIndex >= 0) {
436
- const value = codebooks[bookIndex].decode(reader);
437
- if (value < 0) return null;
438
- posts[offset + j] = value;
439
- } else {
440
- posts[offset + j] = 0;
441
- }
442
- }
443
- offset += cdim;
444
- }
445
- if (reader.eop) return null;
446
- return posts;
447
- }
448
- function computeFloor1Curve(floor, posts, n2, output) {
449
- const range = FLOOR1_RANGES[floor.multiplier - 1];
450
- const count = floor.xList.length;
451
- const finalY = new Int32Array(count);
452
- const step2 = new Uint8Array(count);
453
- step2[0] = 1;
454
- step2[1] = 1;
455
- finalY[0] = posts[0];
456
- finalY[1] = posts[1];
457
- for (let i = 2; i < count; i++) {
458
- const low = floor.lowNeighbors[i - 2];
459
- const high = floor.highNeighbors[i - 2];
460
- const predicted = renderPoint(floor.xList[low], finalY[low], floor.xList[high], finalY[high], floor.xList[i]);
461
- const value = posts[i];
462
- const highroom = range - predicted;
463
- const lowroom = predicted;
464
- const room = (highroom < lowroom ? highroom : lowroom) * 2;
465
- if (value) {
466
- step2[low] = 1;
467
- step2[high] = 1;
468
- step2[i] = 1;
469
- if (value >= room) {
470
- finalY[i] = highroom > lowroom ? value - lowroom + predicted : predicted - value + highroom - 1;
471
- } else {
472
- finalY[i] = value & 1 ? predicted - (value + 1 >> 1) : predicted + (value >> 1);
473
- }
474
- } else {
475
- step2[i] = 0;
476
- finalY[i] = predicted;
477
- }
478
- }
479
- output.fill(0);
480
- let lx = 0;
481
- let ly = finalY[floor.sortedIndex[0]] * floor.multiplier;
482
- for (const index of floor.sortedIndex) {
483
- if (!step2[index]) continue;
484
- const hx = floor.xList[index];
485
- const hy = finalY[index] * floor.multiplier;
486
- if (hx > lx) renderLine(lx, ly, hx, hy, output, n2);
487
- if (hx >= n2) return;
488
- lx = hx;
489
- ly = hy;
490
- }
491
- if (lx < n2) {
492
- for (let i = lx; i < n2; i++) output[i] = INVERSE_DB_TABLE[ly & 255];
493
- }
494
- }
495
- function renderLine(x0, y0, x1, y1, output, n) {
496
- const dy = y1 - y0;
497
- const adx = x1 - x0;
498
- let ady = Math.abs(dy);
499
- const base = dy / adx | 0;
500
- const sy = dy < 0 ? base - 1 : base + 1;
501
- let x = x0;
502
- let y = y0;
503
- let err = 0;
504
- ady -= Math.abs(base) * adx;
505
- const limit = x1 > n ? n : x1;
506
- if (x < limit) output[x] = INVERSE_DB_TABLE[y & 255];
507
- for (x++; x < limit; x++) {
508
- err += ady;
509
- if (err >= adx) {
510
- err -= adx;
511
- y += sy;
512
- } else {
513
- y += base;
514
- }
515
- output[x] = INVERSE_DB_TABLE[y & 255];
516
- }
517
- }
518
- function decodeResidue(residue, reader, codebooks, buffers, doNotDecode, n2) {
519
- const channelCount = buffers.length;
520
- const classbook = codebooks[residue.classbook];
521
- const classwords = classbook.dimensions;
522
- const actualSize = residue.type === 2 ? n2 * channelCount : n2;
523
- const limitBegin = Math.min(residue.begin, actualSize);
524
- const limitEnd = Math.min(residue.end, actualSize);
525
- const nRead = limitEnd - limitBegin;
526
- const partRead = Math.floor(nRead / residue.partSize);
527
- if (partRead <= 0) return;
528
- if (residue.type === 2 && channelCount > 1) {
529
- if (doNotDecode.every(Boolean)) return;
530
- const classifications = new Int32Array(partRead + classwords);
531
- for (let pass = 0; pass < 8; pass++) {
532
- let pcount = 0;
533
- while (pcount < partRead) {
534
- if (pass === 0) {
535
- let symbol = classbook.decode(reader);
536
- if (symbol < 0) return;
537
- for (let i = classwords - 1; i >= 0; i--) {
538
- classifications[i + pcount] = symbol % residue.classifications;
539
- symbol = Math.floor(symbol / residue.classifications);
540
- }
541
- }
542
- for (let i = 0; i < classwords && pcount < partRead; i++, pcount++) {
543
- const z = residue.begin + pcount * residue.partSize;
544
- const bookIndex = residue.books[classifications[pcount]][pass];
545
- if (bookIndex < 0) continue;
546
- const book = codebooks[bookIndex];
547
- let cInter = z % channelCount;
548
- let pInter = Math.floor(z / channelCount);
549
- let decoded = 0;
550
- while (decoded < residue.partSize) {
551
- const symbol = book.decode(reader);
552
- if (symbol < 0) return;
553
- let last = 0;
554
- for (let e = 0; e < book.dimensions; e++) {
555
- const value = book.vqValue(symbol, e) + (book.sequenceP ? last : 0);
556
- if (book.sequenceP) last = value;
557
- buffers[cInter][pInter] += value;
558
- cInter++;
559
- if (cInter === channelCount) {
560
- cInter = 0;
561
- pInter++;
562
- }
563
- }
564
- decoded += book.dimensions;
565
- }
566
- }
567
- }
568
- }
569
- return;
570
- }
571
- const classifications = [];
572
- for (let j = 0; j < channelCount; j++) classifications.push(new Int32Array(partRead + classwords));
573
- for (let pass = 0; pass < 8; pass++) {
574
- let pcount = 0;
575
- while (pcount < partRead) {
576
- if (pass === 0) {
577
- for (let j = 0; j < channelCount; j++) {
578
- if (doNotDecode[j]) continue;
579
- let symbol = classbook.decode(reader);
580
- if (symbol < 0) return;
581
- for (let i = classwords - 1; i >= 0; i--) {
582
- classifications[j][i + pcount] = symbol % residue.classifications;
583
- symbol = Math.floor(symbol / residue.classifications);
584
- }
585
- }
586
- }
587
- for (let i = 0; i < classwords && pcount < partRead; i++, pcount++) {
588
- for (let j = 0; j < channelCount; j++) {
589
- if (doNotDecode[j]) continue;
590
- const bookIndex = residue.books[classifications[j][pcount]][pass];
591
- if (bookIndex < 0) continue;
592
- const book = codebooks[bookIndex];
593
- const target = buffers[j];
594
- const offset = residue.begin + pcount * residue.partSize;
595
- if (residue.type === 0) {
596
- const step = Math.floor(residue.partSize / book.dimensions);
597
- for (let k = 0; k < step; k++) {
598
- const symbol = book.decode(reader);
599
- if (symbol < 0) return;
600
- let last = 0;
601
- for (let e = 0; e < book.dimensions; e++) {
602
- const value = book.vqValue(symbol, e) + (book.sequenceP ? last : 0);
603
- if (book.sequenceP) last = value;
604
- target[offset + k + e * step] += value;
605
- }
606
- }
607
- } else {
608
- let k = 0;
609
- while (k < residue.partSize) {
610
- const symbol = book.decode(reader);
611
- if (symbol < 0) return;
612
- let last = 0;
613
- for (let e = 0; e < book.dimensions; e++) {
614
- const value = book.vqValue(symbol, e) + (book.sequenceP ? last : 0);
615
- if (book.sequenceP) last = value;
616
- target[offset + k + e] += value;
617
- }
618
- k += book.dimensions;
619
- }
620
- }
621
- }
622
- }
623
- }
624
- }
625
- }
626
- function buildWindow(n, blockflag, prevFlag, nextFlag, blocksize0) {
627
- const window = new Float32Array(n);
628
- const center = n >> 1;
629
- let leftStart = 0;
630
- let leftEnd = center;
631
- let leftN = center;
632
- let rightStart = center;
633
- let rightEnd = n;
634
- let rightN = center;
635
- if (blockflag && !prevFlag) {
636
- leftStart = (n >> 2) - (blocksize0 >> 2);
637
- leftEnd = (n >> 2) + (blocksize0 >> 2);
638
- leftN = blocksize0 >> 1;
639
- }
640
- if (blockflag && !nextFlag) {
641
- rightStart = n - (n >> 2) - (blocksize0 >> 2);
642
- rightEnd = n - (n >> 2) + (blocksize0 >> 2);
643
- rightN = blocksize0 >> 1;
644
- }
645
- const leftSlope = vorbisWindowSlope(leftN);
646
- const rightSlope = vorbisWindowSlope(rightN);
647
- for (let i = leftStart; i < leftEnd; i++) window[i] = leftSlope[i - leftStart];
648
- for (let i = leftEnd; i < rightStart; i++) window[i] = 1;
649
- for (let i = rightStart; i < rightEnd; i++) window[i] = rightSlope[rightN - 1 - (i - rightStart)];
650
- return window;
651
- }
652
- const windowVariantCache = new Map();
653
- function getWindow(n, blockflag, prevFlag, nextFlag, blocksize0) {
654
- const key = `${n}:${blockflag ? 1 : 0}${prevFlag ? 1 : 0}${nextFlag ? 1 : 0}:${blocksize0}`;
655
- let window = windowVariantCache.get(key);
656
- if (!window) {
657
- window = buildWindow(n, blockflag, prevFlag, nextFlag, blocksize0);
658
- windowVariantCache.set(key, window);
659
- }
660
- return window;
661
- }
662
-
663
- /**
664
- * Split an Ogg stream into the first logical stream's packets.
665
- *
666
- * @param {Uint8Array} bytes Ogg bytes (already CRC-validated by inspection).
667
- * @returns {object} `{ packets, lastGranule }` for the first stream.
668
- */
669
- function extractOggPackets(bytes) {
670
- const packets = [];
671
- let pending = [];
672
- let serial = null;
673
- let lastGranule = 0;
674
- let offset = 0;
675
- while (offset + 27 <= bytes.length) {
676
- if (bytes[offset] !== 0x4f || bytes[offset + 1] !== 0x67 || bytes[offset + 2] !== 0x67 || bytes[offset + 3] !== 0x53) {
677
- throw decodeError(`invalid page at byte ${offset}`);
678
- }
679
- const headerType = bytes[offset + 5];
680
- let granule = 0;
681
- for (let i = 7; i >= 0; i--) granule = granule * 256 + bytes[offset + 6 + i];
682
- const pageSerial = (bytes[offset + 14] | bytes[offset + 15] << 8 | bytes[offset + 16] << 16 | bytes[offset + 17] * 0x1000000) >>> 0;
683
- const segmentCount = bytes[offset + 26];
684
- const tableOffset = offset + 27;
685
- let cursor = tableOffset + segmentCount;
686
- const isBos = (headerType & 0x02) !== 0;
687
- if (serial === null && isBos) serial = pageSerial;
688
- const ours = pageSerial === serial;
689
- for (let i = 0; i < segmentCount; i++) {
690
- const length = bytes[tableOffset + i];
691
- if (ours && length > 0) pending.push(bytes.subarray(cursor, cursor + length));
692
- cursor += length;
693
- if (length < 255 && ours) {
694
- packets.push(concatChunks(pending));
695
- pending = [];
696
- }
697
- }
698
- if (ours && granule !== 0xffffffffffffffff && Number.isSafeInteger(granule)) {
699
- lastGranule = granule;
700
- }
701
- offset = cursor;
702
- }
703
- if (pending.length) packets.push(concatChunks(pending));
704
- return {
705
- packets,
706
- lastGranule
707
- };
708
- }
709
- function concatChunks(chunks) {
710
- if (chunks.length === 1) return chunks[0];
711
- let total = 0;
712
- for (const chunk of chunks) total += chunk.length;
713
- const bytes = new Uint8Array(total);
714
- let cursor = 0;
715
- for (const chunk of chunks) {
716
- bytes.set(chunk, cursor);
717
- cursor += chunk.length;
718
- }
719
- return bytes;
720
- }
721
- function expectHeader(packet, type) {
722
- if (packet.length < 7 || packet[0] !== type || packet[1] !== 0x76 || packet[2] !== 0x6f || packet[3] !== 0x72 || packet[4] !== 0x62 || packet[5] !== 0x69 || packet[6] !== 0x73) {
723
- throw decodeError(`missing vorbis header packet type ${type}`);
724
- }
725
- const reader = new PacketReader(packet);
726
- reader.readBits(8);
727
- for (let i = 0; i < 6; i++) reader.readBits(8);
728
- return reader;
729
- }
730
- function parseComments(packet) {
731
- const reader = new PacketReader(packet);
732
- reader.readBits(8);
733
- for (let i = 0; i < 6; i++) reader.readBits(8);
734
- const readString = () => {
735
- const length = reader.readBits(32);
736
- let text = "";
737
- for (let i = 0; i < length; i++) text += String.fromCharCode(reader.readBits(8));
738
- return text;
739
- };
740
- const vendor = readString();
741
- const count = reader.readBits(32);
742
- const comments = [];
743
- for (let i = 0; i < count && !reader.eop; i++) comments.push(readString());
744
- let loop = null;
745
- let loopStart = null;
746
- let loopEnd = null;
747
- for (const comment of comments) {
748
- const match = /^(loopstart|loopend)=(\d+)$/iu.exec(comment);
749
- if (!match) continue;
750
- if (match[1].toLowerCase() === "loopstart") loopStart = Number(match[2]);else loopEnd = Number(match[2]);
751
- }
752
- if (loopStart !== null && loopEnd !== null) loop = {
753
- start: loopStart,
754
- end: loopEnd
755
- };
756
- return {
757
- vendor,
758
- comments,
759
- loop
760
- };
761
- }
762
-
763
- /**
764
- * Decode an Ogg Vorbis stream to planar float PCM.
765
- *
766
- * @param {Uint8Array} bytes Complete Ogg Vorbis stream bytes.
767
- * @returns {object} `{ channels, sampleRate, sampleCount, channelData, vendor, comments, loop, packetCount }`.
768
- */
769
- function decodeVorbis(bytes) {
770
- const {
771
- packets,
772
- lastGranule
773
- } = extractOggPackets(bytes);
774
- if (packets.length < 3) throw decodeError("stream has no audio packets");
775
-
776
- // identification header
777
- const identReader = expectHeader(packets[0], 1);
778
- if (identReader.readBits(32) !== 0) throw decodeError("unsupported vorbis version");
779
- const channels = identReader.readBits(8);
780
- const sampleRate = identReader.readBits(32);
781
- identReader.readBits(32);
782
- identReader.readBits(32);
783
- identReader.readBits(32);
784
- const blocksize0 = 1 << identReader.readBits(4);
785
- const blocksize1 = 1 << identReader.readBits(4);
786
- if (!channels || !sampleRate) throw decodeError("invalid identification header");
787
- if (blocksize0 > blocksize1 || blocksize0 < 64 || blocksize1 > 8192) {
788
- throw decodeError("invalid blocksizes");
789
- }
790
- const {
791
- vendor,
792
- comments,
793
- loop
794
- } = parseComments(packets[1]);
795
-
796
- // setup header
797
- const setup = expectHeader(packets[2], 5);
798
- const codebooks = [];
799
- const codebookCount = setup.readBits(8) + 1;
800
- for (let i = 0; i < codebookCount; i++) codebooks.push(parseCodebook(setup));
801
- const timeCount = setup.readBits(6) + 1;
802
- for (let i = 0; i < timeCount; i++) {
803
- if (setup.readBits(16) !== 0) throw decodeError("nonzero time-domain transform");
804
- }
805
- const floors = [];
806
- const floorCount = setup.readBits(6) + 1;
807
- for (let i = 0; i < floorCount; i++) floors.push(parseFloor(setup, codebooks));
808
- const residues = [];
809
- const residueCount = setup.readBits(6) + 1;
810
- for (let i = 0; i < residueCount; i++) residues.push(parseResidue(setup, codebooks));
811
- const mappings = [];
812
- const mappingCount = setup.readBits(6) + 1;
813
- for (let i = 0; i < mappingCount; i++) mappings.push(parseMapping(setup, channels, floorCount, residueCount));
814
- const modes = [];
815
- const modeCount = setup.readBits(6) + 1;
816
- for (let i = 0; i < modeCount; i++) {
817
- const blockflag = setup.readBits(1) === 1;
818
- if (setup.readBits(16) !== 0 || setup.readBits(16) !== 0) {
819
- throw decodeError("nonzero mode window/transform type");
820
- }
821
- const mapping = setup.readBits(8);
822
- if (mapping >= mappingCount) throw decodeError("mode references invalid mapping");
823
- modes.push({
824
- blockflag,
825
- mapping
826
- });
827
- }
828
- if (setup.readBits(1) !== 1) throw decodeError("missing setup framing bit");
829
- const modeBits = ilog(modeCount - 1);
830
-
831
- // synthesis state
832
- const n1 = blocksize1;
833
- const spectrum = [];
834
- const floorCurves = [];
835
- const imdctOut = [];
836
- for (let j = 0; j < channels; j++) {
837
- spectrum.push(new Float32Array(n1 >> 1));
838
- floorCurves.push(new Float32Array(n1 >> 1));
839
- imdctOut.push(new Float32Array(n1));
840
- }
841
- const zeroChannel = new Array(channels).fill(false);
842
- const floorPosts = new Array(channels).fill(null);
843
- const tails = [];
844
- for (let j = 0; j < channels; j++) tails.push(new Float32Array(n1 >> 1));
845
- let prevN = 0;
846
- const outputChunks = [];
847
- let totalSamples = 0;
848
- let packetCount = 0;
849
- for (let p = 3; p < packets.length; p++) {
850
- const reader = new PacketReader(packets[p]);
851
- if (reader.readBits(1) !== 0) continue;
852
- const modeNumber = reader.readBits(modeBits);
853
- if (modeNumber < 0 || modeNumber >= modeCount) continue;
854
- const mode = modes[modeNumber];
855
- const mapping = mappings[mode.mapping];
856
- const n = mode.blockflag ? blocksize1 : blocksize0;
857
- const n2 = n >> 1;
858
- let prevFlag = false;
859
- let nextFlag = false;
860
- if (mode.blockflag) {
861
- prevFlag = reader.readBits(1) === 1;
862
- nextFlag = reader.readBits(1) === 1;
863
- }
864
-
865
- // floors
866
- for (let j = 0; j < channels; j++) {
867
- const floor = floors[mapping.submapFloor[mapping.mux[j]]];
868
- floorPosts[j] = decodeFloor1Posts(reader, floor, codebooks);
869
- zeroChannel[j] = floorPosts[j] === null;
870
- }
871
-
872
- // coupling keeps partners decoding together
873
- const reallyZero = zeroChannel.slice();
874
- for (const step of mapping.couplingSteps) {
875
- if (!zeroChannel[step.magnitude] || !zeroChannel[step.angle]) {
876
- zeroChannel[step.magnitude] = false;
877
- zeroChannel[step.angle] = false;
878
- }
879
- }
880
-
881
- // residues per submap
882
- for (let j = 0; j < channels; j++) spectrum[j].fill(0);
883
- for (let s = 0; s < mapping.submaps; s++) {
884
- const submapChannels = [];
885
- const doNotDecode = [];
886
- for (let j = 0; j < channels; j++) {
887
- if (mapping.mux[j] !== s) continue;
888
- submapChannels.push(spectrum[j]);
889
- doNotDecode.push(zeroChannel[j]);
890
- }
891
- if (!submapChannels.length) continue;
892
- decodeResidue(residues[mapping.submapResidue[s]], reader, codebooks, submapChannels, doNotDecode, n2);
893
- }
894
-
895
- // inverse coupling (reverse order)
896
- for (let i = mapping.couplingSteps.length - 1; i >= 0; i--) {
897
- const {
898
- magnitude,
899
- angle
900
- } = mapping.couplingSteps[i];
901
- const magnitudeVector = spectrum[magnitude];
902
- const angleVector = spectrum[angle];
903
- for (let k = 0; k < n2; k++) {
904
- const m = magnitudeVector[k];
905
- const a = angleVector[k];
906
- let newM;
907
- let newA;
908
- if (m > 0) {
909
- if (a > 0) {
910
- newM = m;
911
- newA = m - a;
912
- } else {
913
- newA = m;
914
- newM = m + a;
915
- }
916
- } else if (a > 0) {
917
- newM = m;
918
- newA = m + a;
919
- } else {
920
- newA = m;
921
- newM = m - a;
922
- }
923
- magnitudeVector[k] = newM;
924
- angleVector[k] = newA;
925
- }
926
- }
927
-
928
- // floor curve multiply + IMDCT + window
929
- const window = getWindow(n, mode.blockflag, prevFlag, nextFlag, blocksize0);
930
- for (let j = 0; j < channels; j++) {
931
- const out = imdctOut[j];
932
- if (reallyZero[j]) {
933
- out.fill(0, 0, n);
934
- continue;
935
- }
936
- const floor = floors[mapping.submapFloor[mapping.mux[j]]];
937
- computeFloor1Curve(floor, floorPosts[j], n2, floorCurves[j]);
938
- const curve = floorCurves[j];
939
- const spec = spectrum[j];
940
- for (let k = 0; k < n2; k++) spec[k] *= curve[k];
941
- imdct(spec.subarray(0, n2), out, n);
942
- for (let k = 0; k < n; k++) out[k] *= window[k];
943
- }
944
-
945
- // center-aligned overlap-add
946
- if (prevN) {
947
- const outCount = prevN + n >> 2;
948
- const curStart = (prevN >> 2) - (n >> 2);
949
- const chunk = [];
950
- for (let j = 0; j < channels; j++) {
951
- const samples = new Float32Array(outCount);
952
- const tail = tails[j];
953
- const cur = imdctOut[j];
954
- for (let i = 0; i < outCount; i++) {
955
- let value = i < prevN >> 1 ? tail[i] : 0;
956
- const curIndex = i - curStart;
957
- if (curIndex >= 0) value += cur[curIndex];
958
- samples[i] = value;
959
- }
960
- chunk.push(samples);
961
- }
962
- outputChunks.push(chunk);
963
- totalSamples += outCount;
964
- }
965
- for (let j = 0; j < channels; j++) {
966
- tails[j].set(imdctOut[j].subarray(n >> 1, n));
967
- }
968
- prevN = n;
969
- packetCount++;
970
- }
971
-
972
- // assemble planar output, trimmed to the stream's final granule
973
- const finalCount = lastGranule && lastGranule < totalSamples ? lastGranule : totalSamples;
974
- const channelData = [];
975
- for (let j = 0; j < channels; j++) channelData.push(new Float32Array(finalCount));
976
- let cursor = 0;
977
- for (const chunk of outputChunks) {
978
- const copyCount = Math.min(chunk[0].length, finalCount - cursor);
979
- if (copyCount <= 0) break;
980
- for (let j = 0; j < channels; j++) {
981
- channelData[j].set(chunk[j].subarray(0, copyCount), cursor);
982
- }
983
- cursor += copyCount;
984
- }
985
- return {
986
- channels,
987
- sampleRate,
988
- sampleCount: finalCount,
989
- channelData,
990
- vendor,
991
- comments,
992
- loop,
993
- packetCount,
994
- declaredGranule: lastGranule
995
- };
996
- }
997
-
998
- export { decodeVorbis, extractOggPackets };
999
- //# sourceMappingURL=vorbis.js.map
1
+ import { imdct, vorbisWindowSlope } from './imdct.js';
2
+
3
+ /**
4
+ * Pure-JS Ogg Vorbis PCM decoder.
5
+ *
6
+ * Implements Vorbis I decode: setup-header codebooks (huffman + VQ), floor
7
+ * type 1 with the spec's exact integer line rendering, residue types 0/1/2,
8
+ * square-polar channel de-coupling, IMDCT, and windowed center-aligned
9
+ * overlap-add with final granule trimming. Behavior follows the Vorbis I
10
+ * specification with stb_vorbis (public domain) consulted as a reference;
11
+ * floor type 0 streams are rejected (never produced by modern encoders).
12
+ */
13
+
14
+ const NO_CODE = 255;
15
+ const FAST_BITS = 10;
16
+ const FAST_SIZE = 1 << FAST_BITS;
17
+
18
+ /**
19
+ * The floor1 amplitude table from the Vorbis I specification
20
+ * (inverse_db_table); indexed by the integer floor curve value.
21
+ */
22
+ const INVERSE_DB_TABLE = new Float32Array([1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07, 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07, 1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07, 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07, 2.9163793e-07, 3.1059021e-07, 3.3077411e-07, 3.5226968e-07, 3.7516214e-07, 3.9954229e-07, 4.2550680e-07, 4.5315863e-07, 4.8260743e-07, 5.1396998e-07, 5.4737065e-07, 5.8294187e-07, 6.2082472e-07, 6.6116941e-07, 7.0413592e-07, 7.4989464e-07, 7.9862701e-07, 8.5052630e-07, 9.0579828e-07, 9.6466216e-07, 1.0273513e-06, 1.0941144e-06, 1.1652161e-06, 1.2409384e-06, 1.3215816e-06, 1.4074654e-06, 1.4989305e-06, 1.5963394e-06, 1.7000785e-06, 1.8105592e-06, 1.9282195e-06, 2.0535261e-06, 2.1869758e-06, 2.3290978e-06, 2.4804557e-06, 2.6416497e-06, 2.8133190e-06, 2.9961443e-06, 3.1908506e-06, 3.3982101e-06, 3.6190449e-06, 3.8542308e-06, 4.1047004e-06, 4.3714470e-06, 4.6555282e-06, 4.9580707e-06, 5.2802740e-06, 5.6234160e-06, 5.9888572e-06, 6.3780469e-06, 6.7925283e-06, 7.2339451e-06, 7.7040476e-06, 8.2047000e-06, 8.7378876e-06, 9.3057248e-06, 9.9104632e-06, 1.0554501e-05, 1.1240392e-05, 1.1970856e-05, 1.2748789e-05, 1.3577278e-05, 1.4459606e-05, 1.5399272e-05, 1.6400004e-05, 1.7465768e-05, 1.8600792e-05, 1.9809576e-05, 2.1096914e-05, 2.2467911e-05, 2.3928002e-05, 2.5482978e-05, 2.7139006e-05, 2.8902651e-05, 3.0780908e-05, 3.2781225e-05, 3.4911534e-05, 3.7180282e-05, 3.9596466e-05, 4.2169667e-05, 4.4910090e-05, 4.7828601e-05, 5.0936773e-05, 5.4246931e-05, 5.7772202e-05, 6.1526565e-05, 6.5524908e-05, 6.9783085e-05, 7.4317983e-05, 7.9147585e-05, 8.4291040e-05, 8.9768747e-05, 9.5602426e-05, 0.00010181521, 0.00010843174, 0.00011547824, 0.00012298267, 0.00013097477, 0.00013948625, 0.00014855085, 0.00015820453, 0.00016848555, 0.00017943469, 0.00019109536, 0.00020351382, 0.00021673929, 0.00023082423, 0.00024582449, 0.00026179955, 0.00027881276, 0.00029693158, 0.00031622787, 0.00033677814, 0.00035866388, 0.00038197188, 0.00040679456, 0.00043323036, 0.00046138411, 0.00049136745, 0.00052329927, 0.00055730621, 0.00059352311, 0.00063209358, 0.00067317058, 0.00071691700, 0.00076350630, 0.00081312324, 0.00086596457, 0.00092223983, 0.00098217216, 0.0010459992, 0.0011139742, 0.0011863665, 0.0012634633, 0.0013455702, 0.0014330129, 0.0015261382, 0.0016253153, 0.0017309374, 0.0018434235, 0.0019632195, 0.0020908006, 0.0022266726, 0.0023713743, 0.0025254795, 0.0026895994, 0.0028643847, 0.0030505286, 0.0032487691, 0.0034598925, 0.0036847358, 0.0039241906, 0.0041792066, 0.0044507950, 0.0047400328, 0.0050480668, 0.0053761186, 0.0057254891, 0.0060975636, 0.0064938176, 0.0069158225, 0.0073652516, 0.0078438871, 0.0083536271, 0.0088964928, 0.0094746370, 0.0100903520, 0.0107460800, 0.0114444210, 0.0121881440, 0.0129801980, 0.0138237250, 0.0147220680, 0.0156787910, 0.0166976870, 0.0177827970, 0.0189384230, 0.0201691490, 0.0214798540, 0.0228757350, 0.0243623300, 0.0259455310, 0.0276316180, 0.0294272760, 0.0313396260, 0.0333762520, 0.0355452280, 0.0378551570, 0.0403151990, 0.0429351080, 0.0457252730, 0.0486967580, 0.0518613480, 0.0552315910, 0.0588208500, 0.0626433610, 0.0667142790, 0.0710497490, 0.0756669620, 0.0805842270, 0.0858210440, 0.0913981790, 0.0973377470, 0.1036633000, 0.1103999300, 0.1175743400, 0.1252149800, 0.1333521500, 0.1420181300, 0.1512472700, 0.1610761700, 0.1715438000, 0.1826916800, 0.1945640200, 0.2072078800, 0.2206734200, 0.2350140200, 0.2502865600, 0.2665515900, 0.2838736100, 0.3023213200, 0.3219678600, 0.3428911400, 0.3651741400, 0.3889052100, 0.4141784700, 0.4410941200, 0.4697589000, 0.5002864800, 0.5327979100, 0.5674221200, 0.6042964000, 0.6435669900, 0.6853895900, 0.7299300700, 0.7773650400, 0.8278826000, 0.8816830700, 0.9389798000, 1.0]);
23
+ function decodeError(message) {
24
+ const error = new Error(`ogg: ${message}`);
25
+ error.code = "CJS_FORMAT_PARSE_ERROR";
26
+ return error;
27
+ }
28
+ function ilog(value) {
29
+ let result = 0;
30
+ let v = value | 0;
31
+ if (v < 0) return 0;
32
+ while (v) {
33
+ result++;
34
+ v >>= 1;
35
+ }
36
+ return result;
37
+ }
38
+ function float32Unpack(x) {
39
+ const mantissa = x & 0x1fffff;
40
+ const sign = x & 0x80000000;
41
+ const exponent = (x & 0x7fe00000) >>> 21;
42
+ const value = sign ? -mantissa : mantissa;
43
+ return value * Math.pow(2, exponent - 788);
44
+ }
45
+ function lookup1Values(entries, dimensions) {
46
+ if (dimensions === 0) return 0;
47
+ const bits = ilog(entries);
48
+ let vals = entries >>> Math.floor((bits - 1) * (dimensions - 1) / dimensions);
49
+ for (;;) {
50
+ let acc = 1;
51
+ let acc1 = 1;
52
+ for (let i = 0; i < dimensions; i++) {
53
+ acc *= vals;
54
+ acc1 *= vals + 1;
55
+ }
56
+ if (acc <= entries && acc1 > entries) return vals;
57
+ if (acc > entries) vals--;else vals++;
58
+ }
59
+ }
60
+
61
+ /**
62
+ * LSB-first bit reader over one Vorbis packet; reading past the end sets
63
+ * `eop` instead of throwing (end-of-packet is a defined decode condition).
64
+ */
65
+ class PacketReader {
66
+ constructor(bytes) {
67
+ this.bytes = bytes;
68
+ this.position = 0;
69
+ this.bitBuffer = 0;
70
+ this.bitsLeft = 0;
71
+ this.eop = false;
72
+ }
73
+ readBits(count) {
74
+ let value = 0;
75
+ for (let i = 0; i < count; i++) {
76
+ if (this.bitsLeft === 0) {
77
+ if (this.position >= this.bytes.length) {
78
+ this.eop = true;
79
+ return -1;
80
+ }
81
+ this.bitBuffer = this.bytes[this.position++];
82
+ this.bitsLeft = 8;
83
+ }
84
+ value |= (this.bitBuffer & 1) << i;
85
+ this.bitBuffer >>= 1;
86
+ this.bitsLeft--;
87
+ }
88
+ return value >>> 0;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Vorbis codebook that builds Huffman decode trees and VQ lookup vectors for
94
+ * scalar and vector packet decoding.
95
+ */
96
+ class Codebook {
97
+ constructor() {
98
+ this.dimensions = 0;
99
+ this.entries = 0;
100
+ this.lookupType = 0;
101
+ this.sequenceP = false;
102
+ this.quantvals = 0;
103
+ this.values = null;
104
+ this.fastTable = null;
105
+ this.treeNodes = null;
106
+ this.maxLength = 0;
107
+ }
108
+ buildHuffman(lengths) {
109
+ const available = new Uint32Array(33);
110
+ this.fastTable = new Int32Array(FAST_SIZE).fill(-1);
111
+ // flat binary trie: two child slots per node; negative = -(symbol+1)
112
+ const nodes = [0, 0];
113
+ let assigned = false;
114
+ for (let symbol = 0; symbol < lengths.length; symbol++) {
115
+ const length = lengths[symbol];
116
+ if (length === NO_CODE) continue;
117
+ if (length > this.maxLength) this.maxLength = length;
118
+ let code;
119
+ if (!assigned) {
120
+ code = 0;
121
+ assigned = true;
122
+ for (let i = 1; i <= length; i++) available[i] = 1 << 32 - i >>> 0;
123
+ } else {
124
+ let z = length;
125
+ while (z > 0 && !available[z]) z--;
126
+ if (z === 0) throw decodeError("over-specified huffman codebook");
127
+ code = available[z];
128
+ available[z] = 0;
129
+ if (z !== length) {
130
+ for (let y = length; y > z; y--) {
131
+ available[y] = code + (1 << 32 - y) >>> 0;
132
+ }
133
+ }
134
+ }
135
+
136
+ // reversed code: first stream bit in bit 0
137
+ let reversed = 0;
138
+ for (let i = 0; i < length; i++) {
139
+ reversed |= (code >>> 31 - i & 1) << i;
140
+ }
141
+ reversed >>>= 0;
142
+ if (length <= FAST_BITS) {
143
+ for (let z = reversed; z < FAST_SIZE; z += 1 << length) {
144
+ this.fastTable[z] = symbol << 6 | length;
145
+ }
146
+ } else {
147
+ let node = 0;
148
+ for (let i = 0; i < length; i++) {
149
+ const bit = reversed >>> i & 1;
150
+ const slot = node * 2 + bit;
151
+ if (i === length - 1) {
152
+ nodes[slot] = -(symbol + 1);
153
+ } else {
154
+ if (!nodes[slot]) {
155
+ nodes[slot] = nodes.length / 2;
156
+ nodes.push(0, 0);
157
+ }
158
+ node = nodes[slot];
159
+ }
160
+ }
161
+ }
162
+ }
163
+ this.treeNodes = nodes;
164
+ }
165
+ decode(reader) {
166
+ // fast path: peek up to FAST_BITS without consuming
167
+ let peek = 0;
168
+ let peekBits = 0;
169
+ const savedPosition = reader.position;
170
+ const savedBuffer = reader.bitBuffer;
171
+ const savedLeft = reader.bitsLeft;
172
+ while (peekBits < FAST_BITS) {
173
+ const bit = reader.readBits(1);
174
+ if (bit < 0) break;
175
+ peek |= bit << peekBits;
176
+ peekBits++;
177
+ }
178
+ reader.position = savedPosition;
179
+ reader.bitBuffer = savedBuffer;
180
+ reader.bitsLeft = savedLeft;
181
+ reader.eop = false;
182
+ if (peekBits === FAST_BITS) {
183
+ const hit = this.fastTable[peek];
184
+ if (hit >= 0) {
185
+ reader.readBits(hit & 63);
186
+ return hit >> 6;
187
+ }
188
+ } else {
189
+ // short remainder: try fast table over what's left
190
+ const hit = this.fastTable[peek];
191
+ if (hit >= 0 && (hit & 63) <= peekBits) {
192
+ reader.readBits(hit & 63);
193
+ return hit >> 6;
194
+ }
195
+ }
196
+
197
+ // slow path: walk the trie bit by bit
198
+ let node = 0;
199
+ for (let i = 0; i < this.maxLength; i++) {
200
+ const bit = reader.readBits(1);
201
+ if (bit < 0) return -1;
202
+ const value = this.treeNodes[node * 2 + bit];
203
+ if (value < 0) return -value - 1;
204
+ if (value === 0) throw decodeError("invalid huffman code in stream");
205
+ node = value;
206
+ }
207
+ throw decodeError("huffman walk exceeded max code length");
208
+ }
209
+ vqValue(symbol, element) {
210
+ if (this.lookupType === 1) {
211
+ let index = symbol;
212
+ for (let i = 0; i < element; i++) index = Math.floor(index / this.quantvals);
213
+ return this.values[index % this.quantvals];
214
+ }
215
+ return this.values[symbol * this.dimensions + element];
216
+ }
217
+ }
218
+ function parseCodebook(reader) {
219
+ const sync = reader.readBits(24);
220
+ if (sync !== 0x564342) throw decodeError("invalid codebook sync pattern");
221
+ const book = new Codebook();
222
+ book.dimensions = reader.readBits(16);
223
+ book.entries = reader.readBits(24);
224
+ const lengths = new Uint8Array(book.entries);
225
+ const ordered = reader.readBits(1);
226
+ if (ordered) {
227
+ let currentEntry = 0;
228
+ let currentLength = reader.readBits(5) + 1;
229
+ while (currentEntry < book.entries) {
230
+ const number = reader.readBits(ilog(book.entries - currentEntry));
231
+ if (reader.eop) throw decodeError("codebook lengths truncated");
232
+ for (let i = 0; i < number; i++) lengths[currentEntry + i] = currentLength;
233
+ currentEntry += number;
234
+ currentLength++;
235
+ if (currentEntry > book.entries) throw decodeError("codebook entry overflow");
236
+ }
237
+ } else {
238
+ const sparse = reader.readBits(1);
239
+ for (let i = 0; i < book.entries; i++) {
240
+ const present = sparse ? reader.readBits(1) : 1;
241
+ lengths[i] = present ? reader.readBits(5) + 1 : NO_CODE;
242
+ }
243
+ }
244
+ if (reader.eop) throw decodeError("codebook lengths truncated");
245
+ book.buildHuffman(lengths);
246
+ book.lookupType = reader.readBits(4);
247
+ if (book.lookupType > 2) throw decodeError(`invalid codebook lookup type ${book.lookupType}`);
248
+ if (book.lookupType > 0) {
249
+ const minimum = float32Unpack(reader.readBits(32));
250
+ const delta = float32Unpack(reader.readBits(32));
251
+ const valueBits = reader.readBits(4) + 1;
252
+ book.sequenceP = reader.readBits(1) === 1;
253
+ const count = book.lookupType === 1 ? lookup1Values(book.entries, book.dimensions) : book.entries * book.dimensions;
254
+ book.quantvals = book.lookupType === 1 ? count : 0;
255
+ book.values = new Float32Array(count);
256
+ for (let i = 0; i < count; i++) {
257
+ book.values[i] = minimum + reader.readBits(valueBits) * delta;
258
+ }
259
+ if (reader.eop) throw decodeError("codebook lookup values truncated");
260
+ }
261
+ return book;
262
+ }
263
+ function parseFloor(reader, codebooks) {
264
+ const type = reader.readBits(16);
265
+ if (type !== 1) {
266
+ throw decodeError(`floor type ${type} is not supported (only floor 1)`);
267
+ }
268
+ const floor = {
269
+ partitions: reader.readBits(5),
270
+ partitionClassList: [],
271
+ classDimensions: [],
272
+ classSubclasses: [],
273
+ classMasterbooks: [],
274
+ subclassBooks: [],
275
+ multiplier: 0,
276
+ rangebits: 0,
277
+ xList: [0, 0]
278
+ };
279
+ let maximumClass = -1;
280
+ for (let i = 0; i < floor.partitions; i++) {
281
+ const partitionClass = reader.readBits(4);
282
+ floor.partitionClassList.push(partitionClass);
283
+ if (partitionClass > maximumClass) maximumClass = partitionClass;
284
+ }
285
+ for (let i = 0; i <= maximumClass; i++) {
286
+ floor.classDimensions.push(reader.readBits(3) + 1);
287
+ const subclasses = reader.readBits(2);
288
+ floor.classSubclasses.push(subclasses);
289
+ floor.classMasterbooks.push(subclasses ? reader.readBits(8) : -1);
290
+ const books = [];
291
+ for (let j = 0; j < 1 << subclasses; j++) {
292
+ books.push(reader.readBits(8) - 1);
293
+ }
294
+ floor.subclassBooks.push(books);
295
+ }
296
+ floor.multiplier = reader.readBits(2) + 1;
297
+ floor.rangebits = reader.readBits(4);
298
+ floor.xList = [0, 1 << floor.rangebits];
299
+ for (let i = 0; i < floor.partitions; i++) {
300
+ for (let j = 0; j < floor.classDimensions[floor.partitionClassList[i]]; j++) {
301
+ floor.xList.push(reader.readBits(floor.rangebits));
302
+ }
303
+ }
304
+ if (reader.eop) throw decodeError("floor configuration truncated");
305
+ for (const bookIndex of [...floor.classMasterbooks, ...floor.subclassBooks.flat()]) {
306
+ if (bookIndex >= codebooks.length) throw decodeError("floor references invalid codebook");
307
+ }
308
+
309
+ // sorted render order over unique X positions, computed once
310
+ floor.sortedIndex = floor.xList.map((x, index) => ({
311
+ x,
312
+ index
313
+ })).sort((a, b) => a.x - b.x || a.index - b.index).map(entry => entry.index);
314
+
315
+ // neighbor tables (over original ordering) computed once
316
+ floor.lowNeighbors = [];
317
+ floor.highNeighbors = [];
318
+ for (let i = 2; i < floor.xList.length; i++) {
319
+ let low = 0;
320
+ let high = 1;
321
+ for (let j = 0; j < i; j++) {
322
+ if (floor.xList[j] < floor.xList[i] && floor.xList[j] >= floor.xList[low]) low = j;
323
+ if (floor.xList[j] > floor.xList[i] && floor.xList[j] <= floor.xList[high]) high = j;
324
+ }
325
+ floor.lowNeighbors.push(low);
326
+ floor.highNeighbors.push(high);
327
+ }
328
+ return floor;
329
+ }
330
+ function parseResidue(reader, codebooks) {
331
+ const type = reader.readBits(16);
332
+ if (type > 2) throw decodeError(`invalid residue type ${type}`);
333
+ const residue = {
334
+ type,
335
+ begin: reader.readBits(24),
336
+ end: reader.readBits(24),
337
+ partSize: reader.readBits(24) + 1,
338
+ classifications: reader.readBits(6) + 1,
339
+ classbook: reader.readBits(8),
340
+ books: []
341
+ };
342
+ if (residue.classbook >= codebooks.length) throw decodeError("residue classbook out of range");
343
+ const cascades = [];
344
+ for (let i = 0; i < residue.classifications; i++) {
345
+ const lowBits = reader.readBits(3);
346
+ const flag = reader.readBits(1);
347
+ const highBits = flag ? reader.readBits(5) : 0;
348
+ cascades.push(highBits * 8 + lowBits);
349
+ }
350
+ for (let i = 0; i < residue.classifications; i++) {
351
+ const passBooks = [];
352
+ for (let pass = 0; pass < 8; pass++) {
353
+ if (cascades[i] & 1 << pass) {
354
+ const bookIndex = reader.readBits(8);
355
+ if (bookIndex >= codebooks.length) throw decodeError("residue book out of range");
356
+ if (codebooks[bookIndex].lookupType === 0) throw decodeError("residue book has no lookup values");
357
+ passBooks.push(bookIndex);
358
+ } else {
359
+ passBooks.push(-1);
360
+ }
361
+ }
362
+ residue.books.push(passBooks);
363
+ }
364
+ if (reader.eop) throw decodeError("residue configuration truncated");
365
+ return residue;
366
+ }
367
+ function parseMapping(reader, channels, floorCount, residueCount) {
368
+ const type = reader.readBits(16);
369
+ if (type !== 0) throw decodeError(`invalid mapping type ${type}`);
370
+ const mapping = {
371
+ submaps: 1,
372
+ couplingSteps: [],
373
+ mux: new Array(channels).fill(0),
374
+ submapFloor: [],
375
+ submapResidue: []
376
+ };
377
+ if (reader.readBits(1)) mapping.submaps = reader.readBits(4) + 1;
378
+ if (reader.readBits(1)) {
379
+ const steps = reader.readBits(8) + 1;
380
+ const bits = ilog(channels - 1);
381
+ for (let i = 0; i < steps; i++) {
382
+ const magnitude = reader.readBits(bits);
383
+ const angle = reader.readBits(bits);
384
+ if (magnitude === angle || magnitude >= channels || angle >= channels) {
385
+ throw decodeError("invalid channel coupling");
386
+ }
387
+ mapping.couplingSteps.push({
388
+ magnitude,
389
+ angle
390
+ });
391
+ }
392
+ }
393
+ if (reader.readBits(2) !== 0) throw decodeError("mapping reserved bits nonzero");
394
+ if (mapping.submaps > 1) {
395
+ for (let i = 0; i < channels; i++) {
396
+ mapping.mux[i] = reader.readBits(4);
397
+ if (mapping.mux[i] >= mapping.submaps) throw decodeError("mapping mux out of range");
398
+ }
399
+ }
400
+ for (let i = 0; i < mapping.submaps; i++) {
401
+ reader.readBits(8);
402
+ const floorIndex = reader.readBits(8);
403
+ const residueIndex = reader.readBits(8);
404
+ if (floorIndex >= floorCount || residueIndex >= residueCount) {
405
+ throw decodeError("mapping references invalid floor or residue");
406
+ }
407
+ mapping.submapFloor.push(floorIndex);
408
+ mapping.submapResidue.push(residueIndex);
409
+ }
410
+ return mapping;
411
+ }
412
+ const FLOOR1_RANGES = [256, 128, 86, 64];
413
+ function renderPoint(x0, y0, x1, y1, x) {
414
+ const dy = y1 - y0;
415
+ const adx = x1 - x0;
416
+ const err = Math.floor(Math.abs(dy) * (x - x0) / adx);
417
+ return dy < 0 ? y0 - err : y0 + err;
418
+ }
419
+ function decodeFloor1Posts(reader, floor, codebooks) {
420
+ if (reader.readBits(1) !== 1) return null;
421
+ const range = FLOOR1_RANGES[floor.multiplier - 1];
422
+ const posts = new Int32Array(floor.xList.length);
423
+ const yBits = ilog(range - 1);
424
+ posts[0] = reader.readBits(yBits);
425
+ posts[1] = reader.readBits(yBits);
426
+ let offset = 2;
427
+ for (let i = 0; i < floor.partitions; i++) {
428
+ const partitionClass = floor.partitionClassList[i];
429
+ const cdim = floor.classDimensions[partitionClass];
430
+ const cbits = floor.classSubclasses[partitionClass];
431
+ const csub = (1 << cbits) - 1;
432
+ let cval = 0;
433
+ if (cbits) {
434
+ cval = codebooks[floor.classMasterbooks[partitionClass]].decode(reader);
435
+ if (cval < 0) return null;
436
+ }
437
+ for (let j = 0; j < cdim; j++) {
438
+ const bookIndex = floor.subclassBooks[partitionClass][cval & csub];
439
+ cval >>= cbits;
440
+ if (bookIndex >= 0) {
441
+ const value = codebooks[bookIndex].decode(reader);
442
+ if (value < 0) return null;
443
+ posts[offset + j] = value;
444
+ } else {
445
+ posts[offset + j] = 0;
446
+ }
447
+ }
448
+ offset += cdim;
449
+ }
450
+ if (reader.eop) return null;
451
+ return posts;
452
+ }
453
+ function computeFloor1Curve(floor, posts, n2, output) {
454
+ const range = FLOOR1_RANGES[floor.multiplier - 1];
455
+ const count = floor.xList.length;
456
+ const finalY = new Int32Array(count);
457
+ const step2 = new Uint8Array(count);
458
+ step2[0] = 1;
459
+ step2[1] = 1;
460
+ finalY[0] = posts[0];
461
+ finalY[1] = posts[1];
462
+ for (let i = 2; i < count; i++) {
463
+ const low = floor.lowNeighbors[i - 2];
464
+ const high = floor.highNeighbors[i - 2];
465
+ const predicted = renderPoint(floor.xList[low], finalY[low], floor.xList[high], finalY[high], floor.xList[i]);
466
+ const value = posts[i];
467
+ const highroom = range - predicted;
468
+ const lowroom = predicted;
469
+ const room = (highroom < lowroom ? highroom : lowroom) * 2;
470
+ if (value) {
471
+ step2[low] = 1;
472
+ step2[high] = 1;
473
+ step2[i] = 1;
474
+ if (value >= room) {
475
+ finalY[i] = highroom > lowroom ? value - lowroom + predicted : predicted - value + highroom - 1;
476
+ } else {
477
+ finalY[i] = value & 1 ? predicted - (value + 1 >> 1) : predicted + (value >> 1);
478
+ }
479
+ } else {
480
+ step2[i] = 0;
481
+ finalY[i] = predicted;
482
+ }
483
+ }
484
+ output.fill(0);
485
+ let lx = 0;
486
+ let ly = finalY[floor.sortedIndex[0]] * floor.multiplier;
487
+ for (const index of floor.sortedIndex) {
488
+ if (!step2[index]) continue;
489
+ const hx = floor.xList[index];
490
+ const hy = finalY[index] * floor.multiplier;
491
+ if (hx > lx) renderLine(lx, ly, hx, hy, output, n2);
492
+ if (hx >= n2) return;
493
+ lx = hx;
494
+ ly = hy;
495
+ }
496
+ if (lx < n2) {
497
+ for (let i = lx; i < n2; i++) output[i] = INVERSE_DB_TABLE[ly & 255];
498
+ }
499
+ }
500
+ function renderLine(x0, y0, x1, y1, output, n) {
501
+ const dy = y1 - y0;
502
+ const adx = x1 - x0;
503
+ let ady = Math.abs(dy);
504
+ const base = dy / adx | 0;
505
+ const sy = dy < 0 ? base - 1 : base + 1;
506
+ let x = x0;
507
+ let y = y0;
508
+ let err = 0;
509
+ ady -= Math.abs(base) * adx;
510
+ const limit = x1 > n ? n : x1;
511
+ if (x < limit) output[x] = INVERSE_DB_TABLE[y & 255];
512
+ for (x++; x < limit; x++) {
513
+ err += ady;
514
+ if (err >= adx) {
515
+ err -= adx;
516
+ y += sy;
517
+ } else {
518
+ y += base;
519
+ }
520
+ output[x] = INVERSE_DB_TABLE[y & 255];
521
+ }
522
+ }
523
+ function decodeResidue(residue, reader, codebooks, buffers, doNotDecode, n2) {
524
+ const channelCount = buffers.length;
525
+ const classbook = codebooks[residue.classbook];
526
+ const classwords = classbook.dimensions;
527
+ const actualSize = residue.type === 2 ? n2 * channelCount : n2;
528
+ const limitBegin = Math.min(residue.begin, actualSize);
529
+ const limitEnd = Math.min(residue.end, actualSize);
530
+ const nRead = limitEnd - limitBegin;
531
+ const partRead = Math.floor(nRead / residue.partSize);
532
+ if (partRead <= 0) return;
533
+ if (residue.type === 2 && channelCount > 1) {
534
+ if (doNotDecode.every(Boolean)) return;
535
+ const classifications = new Int32Array(partRead + classwords);
536
+ for (let pass = 0; pass < 8; pass++) {
537
+ let pcount = 0;
538
+ while (pcount < partRead) {
539
+ if (pass === 0) {
540
+ let symbol = classbook.decode(reader);
541
+ if (symbol < 0) return;
542
+ for (let i = classwords - 1; i >= 0; i--) {
543
+ classifications[i + pcount] = symbol % residue.classifications;
544
+ symbol = Math.floor(symbol / residue.classifications);
545
+ }
546
+ }
547
+ for (let i = 0; i < classwords && pcount < partRead; i++, pcount++) {
548
+ const z = residue.begin + pcount * residue.partSize;
549
+ const bookIndex = residue.books[classifications[pcount]][pass];
550
+ if (bookIndex < 0) continue;
551
+ const book = codebooks[bookIndex];
552
+ let cInter = z % channelCount;
553
+ let pInter = Math.floor(z / channelCount);
554
+ let decoded = 0;
555
+ while (decoded < residue.partSize) {
556
+ const symbol = book.decode(reader);
557
+ if (symbol < 0) return;
558
+ let last = 0;
559
+ for (let e = 0; e < book.dimensions; e++) {
560
+ const value = book.vqValue(symbol, e) + (book.sequenceP ? last : 0);
561
+ if (book.sequenceP) last = value;
562
+ buffers[cInter][pInter] += value;
563
+ cInter++;
564
+ if (cInter === channelCount) {
565
+ cInter = 0;
566
+ pInter++;
567
+ }
568
+ }
569
+ decoded += book.dimensions;
570
+ }
571
+ }
572
+ }
573
+ }
574
+ return;
575
+ }
576
+ const classifications = [];
577
+ for (let j = 0; j < channelCount; j++) classifications.push(new Int32Array(partRead + classwords));
578
+ for (let pass = 0; pass < 8; pass++) {
579
+ let pcount = 0;
580
+ while (pcount < partRead) {
581
+ if (pass === 0) {
582
+ for (let j = 0; j < channelCount; j++) {
583
+ if (doNotDecode[j]) continue;
584
+ let symbol = classbook.decode(reader);
585
+ if (symbol < 0) return;
586
+ for (let i = classwords - 1; i >= 0; i--) {
587
+ classifications[j][i + pcount] = symbol % residue.classifications;
588
+ symbol = Math.floor(symbol / residue.classifications);
589
+ }
590
+ }
591
+ }
592
+ for (let i = 0; i < classwords && pcount < partRead; i++, pcount++) {
593
+ for (let j = 0; j < channelCount; j++) {
594
+ if (doNotDecode[j]) continue;
595
+ const bookIndex = residue.books[classifications[j][pcount]][pass];
596
+ if (bookIndex < 0) continue;
597
+ const book = codebooks[bookIndex];
598
+ const target = buffers[j];
599
+ const offset = residue.begin + pcount * residue.partSize;
600
+ if (residue.type === 0) {
601
+ const step = Math.floor(residue.partSize / book.dimensions);
602
+ for (let k = 0; k < step; k++) {
603
+ const symbol = book.decode(reader);
604
+ if (symbol < 0) return;
605
+ let last = 0;
606
+ for (let e = 0; e < book.dimensions; e++) {
607
+ const value = book.vqValue(symbol, e) + (book.sequenceP ? last : 0);
608
+ if (book.sequenceP) last = value;
609
+ target[offset + k + e * step] += value;
610
+ }
611
+ }
612
+ } else {
613
+ let k = 0;
614
+ while (k < residue.partSize) {
615
+ const symbol = book.decode(reader);
616
+ if (symbol < 0) return;
617
+ let last = 0;
618
+ for (let e = 0; e < book.dimensions; e++) {
619
+ const value = book.vqValue(symbol, e) + (book.sequenceP ? last : 0);
620
+ if (book.sequenceP) last = value;
621
+ target[offset + k + e] += value;
622
+ }
623
+ k += book.dimensions;
624
+ }
625
+ }
626
+ }
627
+ }
628
+ }
629
+ }
630
+ }
631
+ function buildWindow(n, blockflag, prevFlag, nextFlag, blocksize0) {
632
+ const window = new Float32Array(n);
633
+ const center = n >> 1;
634
+ let leftStart = 0;
635
+ let leftEnd = center;
636
+ let leftN = center;
637
+ let rightStart = center;
638
+ let rightEnd = n;
639
+ let rightN = center;
640
+ if (blockflag && !prevFlag) {
641
+ leftStart = (n >> 2) - (blocksize0 >> 2);
642
+ leftEnd = (n >> 2) + (blocksize0 >> 2);
643
+ leftN = blocksize0 >> 1;
644
+ }
645
+ if (blockflag && !nextFlag) {
646
+ rightStart = n - (n >> 2) - (blocksize0 >> 2);
647
+ rightEnd = n - (n >> 2) + (blocksize0 >> 2);
648
+ rightN = blocksize0 >> 1;
649
+ }
650
+ const leftSlope = vorbisWindowSlope(leftN);
651
+ const rightSlope = vorbisWindowSlope(rightN);
652
+ for (let i = leftStart; i < leftEnd; i++) window[i] = leftSlope[i - leftStart];
653
+ for (let i = leftEnd; i < rightStart; i++) window[i] = 1;
654
+ for (let i = rightStart; i < rightEnd; i++) window[i] = rightSlope[rightN - 1 - (i - rightStart)];
655
+ return window;
656
+ }
657
+ const windowVariantCache = new Map();
658
+ function getWindow(n, blockflag, prevFlag, nextFlag, blocksize0) {
659
+ const key = `${n}:${blockflag ? 1 : 0}${prevFlag ? 1 : 0}${nextFlag ? 1 : 0}:${blocksize0}`;
660
+ let window = windowVariantCache.get(key);
661
+ if (!window) {
662
+ window = buildWindow(n, blockflag, prevFlag, nextFlag, blocksize0);
663
+ windowVariantCache.set(key, window);
664
+ }
665
+ return window;
666
+ }
667
+
668
+ /**
669
+ * Split an Ogg stream into the first logical stream's packets.
670
+ *
671
+ * @param {Uint8Array} bytes Ogg bytes (already CRC-validated by inspection).
672
+ * @returns {object} `{ packets, lastGranule }` for the first stream.
673
+ */
674
+ function extractOggPackets(bytes) {
675
+ const packets = [];
676
+ let pending = [];
677
+ let serial = null;
678
+ let lastGranule = 0;
679
+ let offset = 0;
680
+ while (offset + 27 <= bytes.length) {
681
+ if (bytes[offset] !== 0x4f || bytes[offset + 1] !== 0x67 || bytes[offset + 2] !== 0x67 || bytes[offset + 3] !== 0x53) {
682
+ throw decodeError(`invalid page at byte ${offset}`);
683
+ }
684
+ const headerType = bytes[offset + 5];
685
+ let granule = 0;
686
+ for (let i = 7; i >= 0; i--) granule = granule * 256 + bytes[offset + 6 + i];
687
+ const pageSerial = (bytes[offset + 14] | bytes[offset + 15] << 8 | bytes[offset + 16] << 16 | bytes[offset + 17] * 0x1000000) >>> 0;
688
+ const segmentCount = bytes[offset + 26];
689
+ const tableOffset = offset + 27;
690
+ let cursor = tableOffset + segmentCount;
691
+ const isBos = (headerType & 0x02) !== 0;
692
+ if (serial === null && isBos) serial = pageSerial;
693
+ const ours = pageSerial === serial;
694
+ for (let i = 0; i < segmentCount; i++) {
695
+ const length = bytes[tableOffset + i];
696
+ if (ours && length > 0) pending.push(bytes.subarray(cursor, cursor + length));
697
+ cursor += length;
698
+ if (length < 255 && ours) {
699
+ packets.push(concatChunks(pending));
700
+ pending = [];
701
+ }
702
+ }
703
+ if (ours && granule !== 0xffffffffffffffff && Number.isSafeInteger(granule)) {
704
+ lastGranule = granule;
705
+ }
706
+ offset = cursor;
707
+ }
708
+ if (pending.length) packets.push(concatChunks(pending));
709
+ return {
710
+ packets,
711
+ lastGranule
712
+ };
713
+ }
714
+ function concatChunks(chunks) {
715
+ if (chunks.length === 1) return chunks[0];
716
+ let total = 0;
717
+ for (const chunk of chunks) total += chunk.length;
718
+ const bytes = new Uint8Array(total);
719
+ let cursor = 0;
720
+ for (const chunk of chunks) {
721
+ bytes.set(chunk, cursor);
722
+ cursor += chunk.length;
723
+ }
724
+ return bytes;
725
+ }
726
+ function expectHeader(packet, type) {
727
+ if (packet.length < 7 || packet[0] !== type || packet[1] !== 0x76 || packet[2] !== 0x6f || packet[3] !== 0x72 || packet[4] !== 0x62 || packet[5] !== 0x69 || packet[6] !== 0x73) {
728
+ throw decodeError(`missing vorbis header packet type ${type}`);
729
+ }
730
+ const reader = new PacketReader(packet);
731
+ reader.readBits(8);
732
+ for (let i = 0; i < 6; i++) reader.readBits(8);
733
+ return reader;
734
+ }
735
+ function parseComments(packet) {
736
+ const reader = new PacketReader(packet);
737
+ reader.readBits(8);
738
+ for (let i = 0; i < 6; i++) reader.readBits(8);
739
+ const readString = () => {
740
+ const length = reader.readBits(32);
741
+ let text = "";
742
+ for (let i = 0; i < length; i++) text += String.fromCharCode(reader.readBits(8));
743
+ return text;
744
+ };
745
+ const vendor = readString();
746
+ const count = reader.readBits(32);
747
+ const comments = [];
748
+ for (let i = 0; i < count && !reader.eop; i++) comments.push(readString());
749
+ let loop = null;
750
+ let loopStart = null;
751
+ let loopEnd = null;
752
+ for (const comment of comments) {
753
+ const match = /^(loopstart|loopend)=(\d+)$/iu.exec(comment);
754
+ if (!match) continue;
755
+ if (match[1].toLowerCase() === "loopstart") loopStart = Number(match[2]);else loopEnd = Number(match[2]);
756
+ }
757
+ if (loopStart !== null && loopEnd !== null) loop = {
758
+ start: loopStart,
759
+ end: loopEnd
760
+ };
761
+ return {
762
+ vendor,
763
+ comments,
764
+ loop
765
+ };
766
+ }
767
+
768
+ /**
769
+ * Decode an Ogg Vorbis stream to planar float PCM.
770
+ *
771
+ * @param {Uint8Array} bytes Complete Ogg Vorbis stream bytes.
772
+ * @returns {object} `{ channels, sampleRate, sampleCount, channelData, vendor, comments, loop, packetCount }`.
773
+ */
774
+ function decodeVorbis(bytes) {
775
+ const {
776
+ packets,
777
+ lastGranule
778
+ } = extractOggPackets(bytes);
779
+ if (packets.length < 3) throw decodeError("stream has no audio packets");
780
+
781
+ // identification header
782
+ const identReader = expectHeader(packets[0], 1);
783
+ if (identReader.readBits(32) !== 0) throw decodeError("unsupported vorbis version");
784
+ const channels = identReader.readBits(8);
785
+ const sampleRate = identReader.readBits(32);
786
+ identReader.readBits(32);
787
+ identReader.readBits(32);
788
+ identReader.readBits(32);
789
+ const blocksize0 = 1 << identReader.readBits(4);
790
+ const blocksize1 = 1 << identReader.readBits(4);
791
+ if (!channels || !sampleRate) throw decodeError("invalid identification header");
792
+ if (blocksize0 > blocksize1 || blocksize0 < 64 || blocksize1 > 8192) {
793
+ throw decodeError("invalid blocksizes");
794
+ }
795
+ const {
796
+ vendor,
797
+ comments,
798
+ loop
799
+ } = parseComments(packets[1]);
800
+
801
+ // setup header
802
+ const setup = expectHeader(packets[2], 5);
803
+ const codebooks = [];
804
+ const codebookCount = setup.readBits(8) + 1;
805
+ for (let i = 0; i < codebookCount; i++) codebooks.push(parseCodebook(setup));
806
+ const timeCount = setup.readBits(6) + 1;
807
+ for (let i = 0; i < timeCount; i++) {
808
+ if (setup.readBits(16) !== 0) throw decodeError("nonzero time-domain transform");
809
+ }
810
+ const floors = [];
811
+ const floorCount = setup.readBits(6) + 1;
812
+ for (let i = 0; i < floorCount; i++) floors.push(parseFloor(setup, codebooks));
813
+ const residues = [];
814
+ const residueCount = setup.readBits(6) + 1;
815
+ for (let i = 0; i < residueCount; i++) residues.push(parseResidue(setup, codebooks));
816
+ const mappings = [];
817
+ const mappingCount = setup.readBits(6) + 1;
818
+ for (let i = 0; i < mappingCount; i++) mappings.push(parseMapping(setup, channels, floorCount, residueCount));
819
+ const modes = [];
820
+ const modeCount = setup.readBits(6) + 1;
821
+ for (let i = 0; i < modeCount; i++) {
822
+ const blockflag = setup.readBits(1) === 1;
823
+ if (setup.readBits(16) !== 0 || setup.readBits(16) !== 0) {
824
+ throw decodeError("nonzero mode window/transform type");
825
+ }
826
+ const mapping = setup.readBits(8);
827
+ if (mapping >= mappingCount) throw decodeError("mode references invalid mapping");
828
+ modes.push({
829
+ blockflag,
830
+ mapping
831
+ });
832
+ }
833
+ if (setup.readBits(1) !== 1) throw decodeError("missing setup framing bit");
834
+ const modeBits = ilog(modeCount - 1);
835
+
836
+ // synthesis state
837
+ const n1 = blocksize1;
838
+ const spectrum = [];
839
+ const floorCurves = [];
840
+ const imdctOut = [];
841
+ for (let j = 0; j < channels; j++) {
842
+ spectrum.push(new Float32Array(n1 >> 1));
843
+ floorCurves.push(new Float32Array(n1 >> 1));
844
+ imdctOut.push(new Float32Array(n1));
845
+ }
846
+ const zeroChannel = new Array(channels).fill(false);
847
+ const floorPosts = new Array(channels).fill(null);
848
+ const tails = [];
849
+ for (let j = 0; j < channels; j++) tails.push(new Float32Array(n1 >> 1));
850
+ let prevN = 0;
851
+ const outputChunks = [];
852
+ let totalSamples = 0;
853
+ let packetCount = 0;
854
+ for (let p = 3; p < packets.length; p++) {
855
+ const reader = new PacketReader(packets[p]);
856
+ if (reader.readBits(1) !== 0) continue;
857
+ const modeNumber = reader.readBits(modeBits);
858
+ if (modeNumber < 0 || modeNumber >= modeCount) continue;
859
+ const mode = modes[modeNumber];
860
+ const mapping = mappings[mode.mapping];
861
+ const n = mode.blockflag ? blocksize1 : blocksize0;
862
+ const n2 = n >> 1;
863
+ let prevFlag = false;
864
+ let nextFlag = false;
865
+ if (mode.blockflag) {
866
+ prevFlag = reader.readBits(1) === 1;
867
+ nextFlag = reader.readBits(1) === 1;
868
+ }
869
+
870
+ // floors
871
+ for (let j = 0; j < channels; j++) {
872
+ const floor = floors[mapping.submapFloor[mapping.mux[j]]];
873
+ floorPosts[j] = decodeFloor1Posts(reader, floor, codebooks);
874
+ zeroChannel[j] = floorPosts[j] === null;
875
+ }
876
+
877
+ // coupling keeps partners decoding together
878
+ const reallyZero = zeroChannel.slice();
879
+ for (const step of mapping.couplingSteps) {
880
+ if (!zeroChannel[step.magnitude] || !zeroChannel[step.angle]) {
881
+ zeroChannel[step.magnitude] = false;
882
+ zeroChannel[step.angle] = false;
883
+ }
884
+ }
885
+
886
+ // residues per submap
887
+ for (let j = 0; j < channels; j++) spectrum[j].fill(0);
888
+ for (let s = 0; s < mapping.submaps; s++) {
889
+ const submapChannels = [];
890
+ const doNotDecode = [];
891
+ for (let j = 0; j < channels; j++) {
892
+ if (mapping.mux[j] !== s) continue;
893
+ submapChannels.push(spectrum[j]);
894
+ doNotDecode.push(zeroChannel[j]);
895
+ }
896
+ if (!submapChannels.length) continue;
897
+ decodeResidue(residues[mapping.submapResidue[s]], reader, codebooks, submapChannels, doNotDecode, n2);
898
+ }
899
+
900
+ // inverse coupling (reverse order)
901
+ for (let i = mapping.couplingSteps.length - 1; i >= 0; i--) {
902
+ const {
903
+ magnitude,
904
+ angle
905
+ } = mapping.couplingSteps[i];
906
+ const magnitudeVector = spectrum[magnitude];
907
+ const angleVector = spectrum[angle];
908
+ for (let k = 0; k < n2; k++) {
909
+ const m = magnitudeVector[k];
910
+ const a = angleVector[k];
911
+ let newM;
912
+ let newA;
913
+ if (m > 0) {
914
+ if (a > 0) {
915
+ newM = m;
916
+ newA = m - a;
917
+ } else {
918
+ newA = m;
919
+ newM = m + a;
920
+ }
921
+ } else if (a > 0) {
922
+ newM = m;
923
+ newA = m + a;
924
+ } else {
925
+ newA = m;
926
+ newM = m - a;
927
+ }
928
+ magnitudeVector[k] = newM;
929
+ angleVector[k] = newA;
930
+ }
931
+ }
932
+
933
+ // floor curve multiply + IMDCT + window
934
+ const window = getWindow(n, mode.blockflag, prevFlag, nextFlag, blocksize0);
935
+ for (let j = 0; j < channels; j++) {
936
+ const out = imdctOut[j];
937
+ if (reallyZero[j]) {
938
+ out.fill(0, 0, n);
939
+ continue;
940
+ }
941
+ const floor = floors[mapping.submapFloor[mapping.mux[j]]];
942
+ computeFloor1Curve(floor, floorPosts[j], n2, floorCurves[j]);
943
+ const curve = floorCurves[j];
944
+ const spec = spectrum[j];
945
+ for (let k = 0; k < n2; k++) spec[k] *= curve[k];
946
+ imdct(spec.subarray(0, n2), out, n);
947
+ for (let k = 0; k < n; k++) out[k] *= window[k];
948
+ }
949
+
950
+ // center-aligned overlap-add
951
+ if (prevN) {
952
+ const outCount = prevN + n >> 2;
953
+ const curStart = (prevN >> 2) - (n >> 2);
954
+ const chunk = [];
955
+ for (let j = 0; j < channels; j++) {
956
+ const samples = new Float32Array(outCount);
957
+ const tail = tails[j];
958
+ const cur = imdctOut[j];
959
+ for (let i = 0; i < outCount; i++) {
960
+ let value = i < prevN >> 1 ? tail[i] : 0;
961
+ const curIndex = i - curStart;
962
+ if (curIndex >= 0) value += cur[curIndex];
963
+ samples[i] = value;
964
+ }
965
+ chunk.push(samples);
966
+ }
967
+ outputChunks.push(chunk);
968
+ totalSamples += outCount;
969
+ }
970
+ for (let j = 0; j < channels; j++) {
971
+ tails[j].set(imdctOut[j].subarray(n >> 1, n));
972
+ }
973
+ prevN = n;
974
+ packetCount++;
975
+ }
976
+
977
+ // assemble planar output, trimmed to the stream's final granule
978
+ const finalCount = lastGranule && lastGranule < totalSamples ? lastGranule : totalSamples;
979
+ const channelData = [];
980
+ for (let j = 0; j < channels; j++) channelData.push(new Float32Array(finalCount));
981
+ let cursor = 0;
982
+ for (const chunk of outputChunks) {
983
+ const copyCount = Math.min(chunk[0].length, finalCount - cursor);
984
+ if (copyCount <= 0) break;
985
+ for (let j = 0; j < channels; j++) {
986
+ channelData[j].set(chunk[j].subarray(0, copyCount), cursor);
987
+ }
988
+ cursor += copyCount;
989
+ }
990
+ return {
991
+ channels,
992
+ sampleRate,
993
+ sampleCount: finalCount,
994
+ channelData,
995
+ vendor,
996
+ comments,
997
+ loop,
998
+ packetCount,
999
+ declaredGranule: lastGranule
1000
+ };
1001
+ }
1002
+
1003
+ export { decodeVorbis, extractOggPackets };
1004
+ //# sourceMappingURL=vorbis.js.map