@carbonenginejs/runtime-resource 0.1.0 → 0.8.0

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 (329) hide show
  1. package/FORMAT-PROVENANCE.md +155 -135
  2. package/LICENSE +21 -21
  3. package/NOTICE +29 -29
  4. package/README.md +548 -241
  5. package/dist/CjsMotherLode.js +1271 -56
  6. package/dist/CjsMotherLode.js.map +1 -1
  7. package/dist/CjsResMan.js +3005 -740
  8. package/dist/CjsResMan.js.map +1 -1
  9. package/dist/CjsResManQueue.js +214 -214
  10. package/dist/CjsResource.js +566 -366
  11. package/dist/CjsResource.js.map +1 -1
  12. package/dist/CjsResourceSource.js +59 -59
  13. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  14. package/dist/format/CjsBlueReader.js +269 -0
  15. package/dist/format/CjsBlueReader.js.map +1 -0
  16. package/dist/format/CjsFormat.js +192 -178
  17. package/dist/format/CjsFormat.js.map +1 -1
  18. package/dist/format/CjsReader.js +18 -0
  19. package/dist/format/CjsReader.js.map +1 -0
  20. package/dist/format/CjsResourceProbe.js +277 -269
  21. package/dist/format/CjsResourceProbe.js.map +1 -1
  22. package/dist/format/payloadContract.js +173 -173
  23. package/dist/formats/black/CjsBlackFormat.js +278 -272
  24. package/dist/formats/black/CjsBlackFormat.js.map +1 -1
  25. package/dist/formats/black/core/CjsBlackBinaryReader.js +153 -153
  26. package/dist/formats/black/core/CjsBlackPropertyReaders.js +382 -382
  27. package/dist/formats/black/core/CjsBlackReader.js +639 -760
  28. package/dist/formats/black/core/CjsBlackReader.js.map +1 -1
  29. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +433 -433
  30. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +4 -4
  31. package/dist/formats/black/core/blackConstants.js +7 -7
  32. package/dist/formats/black/core/blackDefinitions.js +10 -10
  33. package/dist/formats/black/core/blackEnums.js +6 -6
  34. package/dist/formats/black/core/blackSchema.js +3 -3
  35. package/dist/formats/black/core/blackVersion.js +22 -22
  36. package/dist/formats/black/core/helpers.js +182 -182
  37. package/dist/formats/black/core/schema.js +4 -4
  38. package/dist/formats/black/index.js +2 -2
  39. package/dist/formats/bnk/CjsBnkFormat.js +238 -245
  40. package/dist/formats/bnk/CjsBnkFormat.js.map +1 -1
  41. package/dist/formats/bnk/core/graph.js +140 -0
  42. package/dist/formats/bnk/core/graph.js.map +1 -0
  43. package/dist/formats/bnk/core/helpers.js +409 -354
  44. package/dist/formats/bnk/core/helpers.js.map +1 -1
  45. package/dist/formats/bnk/core/musicNodes.js +521 -0
  46. package/dist/formats/bnk/core/musicNodes.js.map +1 -0
  47. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  48. package/dist/formats/bnk/index.js +2 -3
  49. package/dist/formats/bnk/index.js.map +1 -1
  50. package/dist/formats/cmf/CjsCmfFormat.js +497 -491
  51. package/dist/formats/cmf/CjsCmfFormat.js.map +1 -1
  52. package/dist/formats/cmf/core/binary.js +118 -118
  53. package/dist/formats/cmf/core/buffers.js +233 -233
  54. package/dist/formats/cmf/core/constants.js +47 -47
  55. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  56. package/dist/formats/cmf/core/helpers.js +287 -287
  57. package/dist/formats/cmf/core/pack.js +276 -276
  58. package/dist/formats/cmf/core/schema.js +364 -364
  59. package/dist/formats/cmf/core/shared.js +268 -268
  60. package/dist/formats/cmf/core/writer.js +517 -517
  61. package/dist/formats/cmf/index.js +2 -2
  62. package/dist/formats/dds/CjsDdsFormat.js +193 -187
  63. package/dist/formats/dds/CjsDdsFormat.js.map +1 -1
  64. package/dist/formats/dds/core/bc6h.js +288 -0
  65. package/dist/formats/dds/core/bc6h.js.map +1 -0
  66. package/dist/formats/dds/core/bc7.js +251 -0
  67. package/dist/formats/dds/core/bc7.js.map +1 -0
  68. package/dist/formats/dds/core/helpers.js +815 -812
  69. package/dist/formats/dds/core/helpers.js.map +1 -1
  70. package/dist/formats/dds/index.js +2 -2
  71. package/dist/formats/fbx/CjsFbxFormat.js +266 -260
  72. package/dist/formats/fbx/CjsFbxFormat.js.map +1 -1
  73. package/dist/formats/fbx/core/helpers.js +3901 -3901
  74. package/dist/formats/fbx/index.js +2 -2
  75. package/dist/formats/flac/CjsFlacFormat.js +87 -81
  76. package/dist/formats/flac/CjsFlacFormat.js.map +1 -1
  77. package/dist/formats/flac/core/helpers.js +295 -295
  78. package/dist/formats/flac/index.js +2 -2
  79. package/dist/formats/gif/CjsGifFormat.js +87 -81
  80. package/dist/formats/gif/CjsGifFormat.js.map +1 -1
  81. package/dist/formats/gif/core/helpers.js +360 -360
  82. package/dist/formats/gif/index.js +2 -2
  83. package/dist/formats/gltf/CjsGltfFormat.js +290 -284
  84. package/dist/formats/gltf/CjsGltfFormat.js.map +1 -1
  85. package/dist/formats/gltf/core/helpers.js +292 -292
  86. package/dist/formats/gltf/core/json.js +79 -79
  87. package/dist/formats/gltf/core/parser.js +666 -666
  88. package/dist/formats/gltf/core/targets.js +163 -163
  89. package/dist/formats/gltf/index.js +2 -2
  90. package/dist/formats/index.js +23 -23
  91. package/dist/formats/jpeg/CjsJpegFormat.js +206 -200
  92. package/dist/formats/jpeg/CjsJpegFormat.js.map +1 -1
  93. package/dist/formats/jpeg/core/helpers.js +350 -350
  94. package/dist/formats/jpeg/core/jpeg.js +377 -377
  95. package/dist/formats/jpeg/index.js +2 -2
  96. package/dist/formats/mp3/CjsMp3Format.js +192 -186
  97. package/dist/formats/mp3/CjsMp3Format.js.map +1 -1
  98. package/dist/formats/mp3/core/helpers.js +338 -338
  99. package/dist/formats/mp3/index.js +2 -2
  100. package/dist/formats/mp4/CjsMp4Format.js +191 -185
  101. package/dist/formats/mp4/CjsMp4Format.js.map +1 -1
  102. package/dist/formats/mp4/core/helpers.js +449 -449
  103. package/dist/formats/mp4/index.js +2 -2
  104. package/dist/formats/obj/CjsObjFormat.js +253 -247
  105. package/dist/formats/obj/CjsObjFormat.js.map +1 -1
  106. package/dist/formats/obj/core/helpers.js +573 -573
  107. package/dist/formats/obj/core/json.js +64 -64
  108. package/dist/formats/obj/core/parser.js +321 -321
  109. package/dist/formats/obj/index.js +2 -2
  110. package/dist/formats/ogg/CjsOggFormat.js +88 -82
  111. package/dist/formats/ogg/CjsOggFormat.js.map +1 -1
  112. package/dist/formats/ogg/core/helpers.js +387 -387
  113. package/dist/formats/ogg/core/imdct.js +178 -178
  114. package/dist/formats/ogg/core/vorbis.js +999 -999
  115. package/dist/formats/ogg/index.js +2 -2
  116. package/dist/formats/png/CjsPngFormat.js +195 -189
  117. package/dist/formats/png/CjsPngFormat.js.map +1 -1
  118. package/dist/formats/png/core/helpers.js +583 -583
  119. package/dist/formats/png/index.js +2 -2
  120. package/dist/formats/red/CjsRedFormat.js +261 -247
  121. package/dist/formats/red/CjsRedFormat.js.map +1 -1
  122. package/dist/formats/red/core/CjsRedReader.js +194 -201
  123. package/dist/formats/red/core/CjsRedReader.js.map +1 -1
  124. package/dist/formats/red/core/blackDefinitions.js +3 -3
  125. package/dist/formats/red/core/helpers.js +139 -136
  126. package/dist/formats/red/core/helpers.js.map +1 -1
  127. package/dist/formats/red/core/redGraph.js +68 -68
  128. package/dist/formats/red/core/schema.js +4 -4
  129. package/dist/formats/red/index.js +2 -2
  130. package/dist/formats/stl/CjsStlFormat.js +365 -304
  131. package/dist/formats/stl/CjsStlFormat.js.map +1 -1
  132. package/dist/formats/stl/core/helpers.js +261 -257
  133. package/dist/formats/stl/core/helpers.js.map +1 -1
  134. package/dist/formats/stl/core/json.js +51 -51
  135. package/dist/formats/stl/core/stl.js +629 -544
  136. package/dist/formats/stl/core/stl.js.map +1 -1
  137. package/dist/formats/stl/core/targets.js +163 -163
  138. package/dist/formats/stl/index.js +2 -2
  139. package/dist/formats/tga/CjsTgaFormat.js +192 -186
  140. package/dist/formats/tga/CjsTgaFormat.js.map +1 -1
  141. package/dist/formats/tga/core/helpers.js +446 -446
  142. package/dist/formats/tga/index.js +2 -2
  143. package/dist/formats/wav/CjsWavFormat.js +192 -186
  144. package/dist/formats/wav/CjsWavFormat.js.map +1 -1
  145. package/dist/formats/wav/core/helpers.js +328 -328
  146. package/dist/formats/wav/index.js +2 -2
  147. package/dist/formats/webm/CjsWebmFormat.js +191 -185
  148. package/dist/formats/webm/CjsWebmFormat.js.map +1 -1
  149. package/dist/formats/webm/core/helpers.js +537 -537
  150. package/dist/formats/webm/index.js +2 -2
  151. package/dist/formats/webp/CjsWebpFormat.js +86 -80
  152. package/dist/formats/webp/CjsWebpFormat.js.map +1 -1
  153. package/dist/formats/webp/core/helpers.js +214 -214
  154. package/dist/formats/webp/index.js +2 -2
  155. package/dist/formats/wem/CjsWemFormat.js +241 -197
  156. package/dist/formats/wem/CjsWemFormat.js.map +1 -1
  157. package/dist/formats/wem/core/bitStream.js +259 -259
  158. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  159. package/dist/formats/wem/core/helpers.js +417 -336
  160. package/dist/formats/wem/core/helpers.js.map +1 -1
  161. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  162. package/dist/formats/wem/core/ptadpcm.js +77 -0
  163. package/dist/formats/wem/core/ptadpcm.js.map +1 -0
  164. package/dist/formats/wem/core/resolve.js +121 -0
  165. package/dist/formats/wem/core/resolve.js.map +1 -0
  166. package/dist/formats/wem/core/wemToOgg.js +485 -485
  167. package/dist/formats/wem/index.js +2 -2
  168. package/dist/formats/yaml/CjsYamlFormat.js +83 -77
  169. package/dist/formats/yaml/CjsYamlFormat.js.map +1 -1
  170. package/dist/formats/yaml/core/CjsYamlReader.js +305 -304
  171. package/dist/formats/yaml/core/CjsYamlReader.js.map +1 -1
  172. package/dist/formats/yaml/core/helpers.js +160 -160
  173. package/dist/formats/yaml/index.js +2 -2
  174. package/dist/index.js +48 -52
  175. package/dist/index.js.map +1 -1
  176. package/dist/resourcePath.js +18 -18
  177. package/dist/resourceStates.js +9 -9
  178. package/dist/{generated/resources → resources}/AudioGeometryResData.js +47 -47
  179. package/dist/resources/AudioGeometryResData.js.map +1 -0
  180. package/dist/{generated/resources → resources}/GStateBindingCallbackData.js +31 -31
  181. package/dist/resources/GStateBindingCallbackData.js.map +1 -0
  182. package/dist/{generated/resources → resources}/MeshDecalData.js +37 -37
  183. package/dist/resources/MeshDecalData.js.map +1 -0
  184. package/dist/{generated/resources → resources}/MeshDecalLodData.js +34 -34
  185. package/dist/resources/MeshDecalLodData.js.map +1 -0
  186. package/dist/resources/Tr2EffectRes.js +71 -71
  187. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -0
  188. package/dist/resources/Tr2GrannyIntersectionResult.js.map +1 -0
  189. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  190. package/dist/resources/Tr2ImageRes.js +114 -114
  191. package/dist/resources/Tr2LightProfileRes.js +40 -40
  192. package/dist/{generated/resources → resources}/Tr2MaterialArea.js +34 -34
  193. package/dist/resources/Tr2MaterialArea.js.map +1 -0
  194. package/dist/{generated/resources → resources}/Tr2MaterialMesh.js +31 -31
  195. package/dist/resources/Tr2MaterialMesh.js.map +1 -0
  196. package/dist/{generated/resources → resources}/Tr2MaterialRes.js +34 -34
  197. package/dist/resources/Tr2MaterialRes.js.map +1 -0
  198. package/dist/{generated/resources → resources}/Tr2ShaderPermutation.js +43 -43
  199. package/dist/resources/Tr2ShaderPermutation.js.map +1 -0
  200. package/dist/{generated/resources → resources}/Tr2TextureLodManager.js +100 -60
  201. package/dist/resources/Tr2TextureLodManager.js.map +1 -0
  202. package/dist/{generated/resources → resources}/Tr2TextureLodUpdateRequest.js +37 -37
  203. package/dist/resources/Tr2TextureLodUpdateRequest.js.map +1 -0
  204. package/dist/{generated/resources → resources}/Tr2TexturePackChannel.js +37 -37
  205. package/dist/resources/Tr2TexturePackChannel.js.map +1 -0
  206. package/dist/resources/Tr2TexturePipeline.js +68 -0
  207. package/dist/resources/Tr2TexturePipeline.js.map +1 -0
  208. package/dist/{generated/resources → resources}/Tr2TexturePipelineParams.js +34 -34
  209. package/dist/resources/Tr2TexturePipelineParams.js.map +1 -0
  210. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepCompress.js +40 -40
  211. package/dist/resources/Tr2TexturePipelineStepCompress.js.map +1 -0
  212. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepLimitSize.js +34 -34
  213. package/dist/resources/Tr2TexturePipelineStepLimitSize.js.map +1 -0
  214. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepLoad.js +31 -31
  215. package/dist/resources/Tr2TexturePipelineStepLoad.js.map +1 -0
  216. package/dist/{generated/resources → resources}/Tr2TexturePipelineStepPack.js +43 -43
  217. package/dist/resources/Tr2TexturePipelineStepPack.js.map +1 -0
  218. package/dist/resources/TriGeometryRes.js +239 -216
  219. package/dist/resources/TriGeometryRes.js.map +1 -1
  220. package/dist/{generated/resources → resources}/TriGeometryResAreaData.js +59 -59
  221. package/dist/resources/TriGeometryResAreaData.js.map +1 -0
  222. package/dist/{generated/resources → resources}/TriGeometryResJointData.js +38 -38
  223. package/dist/resources/TriGeometryResJointData.js.map +1 -0
  224. package/dist/{generated/resources → resources}/TriGeometryResLodData.js +88 -88
  225. package/dist/resources/TriGeometryResLodData.js.map +1 -0
  226. package/dist/{generated/resources → resources}/TriGeometryResMeshData.js +63 -63
  227. package/dist/resources/TriGeometryResMeshData.js.map +1 -0
  228. package/dist/{generated/resources → resources}/TriGeometryResSkeletonData.js +34 -34
  229. package/dist/resources/TriGeometryResSkeletonData.js.map +1 -0
  230. package/dist/resources/TriGrannyRes.js +43 -43
  231. package/dist/{generated/resources → resources}/TriJointBinding.js +38 -38
  232. package/dist/resources/TriJointBinding.js.map +1 -0
  233. package/dist/{generated/resources → resources}/TriMorphTargetGeometryConstants.js +46 -46
  234. package/dist/resources/TriMorphTargetGeometryConstants.js.map +1 -0
  235. package/dist/{generated/resources → resources}/TriRtGeometryConstants.js +88 -88
  236. package/dist/resources/TriRtGeometryConstants.js.map +1 -0
  237. package/dist/resources/TriTextureRes.js +304 -304
  238. package/dist/{generated/resources → resources}/enums.js +18 -18
  239. package/dist/resources/enums.js.map +1 -0
  240. package/dist/resources/resourceBoundary.js +47 -47
  241. package/dist/resources/texturePipelineBehavior.js +320 -0
  242. package/dist/resources/texturePipelineBehavior.js.map +1 -0
  243. package/dist/texture/CjsTextureArrayRes.js +406 -406
  244. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  245. package/format-notices/black/LICENSE +21 -21
  246. package/format-notices/black/NOTICE +47 -47
  247. package/format-notices/bnk/LICENSE +21 -21
  248. package/format-notices/bnk/NOTICE +20 -17
  249. package/format-notices/cmf/LICENSE +21 -21
  250. package/format-notices/cmf/NOTICE +36 -36
  251. package/format-notices/dds/LICENSE +21 -21
  252. package/format-notices/dds/NOTICE +14 -14
  253. package/format-notices/fbx/LICENSE +21 -21
  254. package/format-notices/fbx/NOTICE +14 -14
  255. package/format-notices/flac/LICENSE +21 -21
  256. package/format-notices/flac/NOTICE +14 -14
  257. package/format-notices/gif/LICENSE +21 -21
  258. package/format-notices/gif/NOTICE +14 -14
  259. package/format-notices/gltf/LICENSE +21 -21
  260. package/format-notices/gltf/NOTICE +27 -27
  261. package/format-notices/jpeg/LICENSE +21 -21
  262. package/format-notices/jpeg/NOTICE +14 -14
  263. package/format-notices/mp3/LICENSE +21 -21
  264. package/format-notices/mp3/NOTICE +14 -14
  265. package/format-notices/mp4/LICENSE +21 -21
  266. package/format-notices/mp4/NOTICE +14 -14
  267. package/format-notices/obj/LICENSE +21 -21
  268. package/format-notices/obj/NOTICE +26 -26
  269. package/format-notices/ogg/LICENSE +21 -21
  270. package/format-notices/ogg/NOTICE +28 -28
  271. package/format-notices/png/LICENSE +21 -21
  272. package/format-notices/png/NOTICE +14 -14
  273. package/format-notices/red/LICENSE +21 -21
  274. package/format-notices/red/NOTICE +31 -31
  275. package/format-notices/stl/LICENSE +21 -21
  276. package/format-notices/stl/NOTICE +21 -21
  277. package/format-notices/tga/LICENSE +21 -21
  278. package/format-notices/tga/NOTICE +14 -14
  279. package/format-notices/wav/LICENSE +21 -21
  280. package/format-notices/wav/NOTICE +14 -14
  281. package/format-notices/webm/LICENSE +21 -21
  282. package/format-notices/webm/NOTICE +14 -14
  283. package/format-notices/webp/LICENSE +21 -21
  284. package/format-notices/webp/NOTICE +14 -14
  285. package/format-notices/wem/LICENSE +57 -57
  286. package/format-notices/wem/NOTICE +33 -33
  287. package/format-notices/yaml/LICENSE +21 -21
  288. package/format-notices/yaml/NOTICE +44 -44
  289. package/package.json +51 -51
  290. package/resource-lifecycle.md +679 -487
  291. package/dist/generated/resources/AudioGeometryResData.js.map +0 -1
  292. package/dist/generated/resources/CmfVertexReader.js +0 -46
  293. package/dist/generated/resources/CmfVertexReader.js.map +0 -1
  294. package/dist/generated/resources/GStateBindingCallbackData.js.map +0 -1
  295. package/dist/generated/resources/MeshDecalData.js.map +0 -1
  296. package/dist/generated/resources/MeshDecalLodData.js.map +0 -1
  297. package/dist/generated/resources/Tr2AsyncSave.js +0 -46
  298. package/dist/generated/resources/Tr2AsyncSave.js.map +0 -1
  299. package/dist/generated/resources/Tr2CmfContents.js +0 -37
  300. package/dist/generated/resources/Tr2CmfContents.js.map +0 -1
  301. package/dist/generated/resources/Tr2GrannyIntersectionResult.js +0 -58
  302. package/dist/generated/resources/Tr2GrannyIntersectionResult.js.map +0 -1
  303. package/dist/generated/resources/Tr2LoadPrepareFence.js +0 -37
  304. package/dist/generated/resources/Tr2LoadPrepareFence.js.map +0 -1
  305. package/dist/generated/resources/Tr2MaterialArea.js.map +0 -1
  306. package/dist/generated/resources/Tr2MaterialMesh.js.map +0 -1
  307. package/dist/generated/resources/Tr2MaterialRes.js.map +0 -1
  308. package/dist/generated/resources/Tr2ShaderPermutation.js.map +0 -1
  309. package/dist/generated/resources/Tr2TextureLodManager.js.map +0 -1
  310. package/dist/generated/resources/Tr2TextureLodUpdateRequest.js.map +0 -1
  311. package/dist/generated/resources/Tr2TexturePackChannel.js.map +0 -1
  312. package/dist/generated/resources/Tr2TexturePipeline.js +0 -44
  313. package/dist/generated/resources/Tr2TexturePipeline.js.map +0 -1
  314. package/dist/generated/resources/Tr2TexturePipelineParams.js.map +0 -1
  315. package/dist/generated/resources/Tr2TexturePipelineStepCompress.js.map +0 -1
  316. package/dist/generated/resources/Tr2TexturePipelineStepLimitSize.js.map +0 -1
  317. package/dist/generated/resources/Tr2TexturePipelineStepLoad.js.map +0 -1
  318. package/dist/generated/resources/Tr2TexturePipelineStepPack.js.map +0 -1
  319. package/dist/generated/resources/TriGeometryResAreaData.js.map +0 -1
  320. package/dist/generated/resources/TriGeometryResJointData.js.map +0 -1
  321. package/dist/generated/resources/TriGeometryResLodData.js.map +0 -1
  322. package/dist/generated/resources/TriGeometryResMeshData.js.map +0 -1
  323. package/dist/generated/resources/TriGeometryResSkeletonData.js.map +0 -1
  324. package/dist/generated/resources/TriJointBinding.js.map +0 -1
  325. package/dist/generated/resources/TriMorphTargetGeometryConstants.js.map +0 -1
  326. package/dist/generated/resources/TriRtGeometryConstants.js.map +0 -1
  327. package/dist/generated/resources/enums.js.map +0 -1
  328. package/dist/generated/resources/index.js +0 -32
  329. package/dist/generated/resources/index.js.map +0 -1
@@ -1,485 +1,485 @@
1
- import { OggPageWriter, BitReader, ilog } from './bitStream.js';
2
- import { parseCodebookLibrary, rebuildCodebookById } from './codebookLibrary.js';
3
- import { getPackedCodebooksAotuv603 } from './packedCodebooksAotuv603.js';
4
-
5
- /**
6
- * Wwise Vorbis (.wem) to standard Ogg Vorbis repacker.
7
- *
8
- * This is a lossless container transform: the identification/comment/setup
9
- * headers are regenerated (setup codebooks expanded from the packed aoTuV
10
- * 6.03 library), modified audio packets get their type/window bits restored,
11
- * and packets are paginated with correct granule positions computed from the
12
- * mode block flags — so no separate revorb pass is needed. No audio is
13
- * decoded or re-encoded. Algorithm behavior follows the ww2ogg reference
14
- * (BSD-licensed) as an original reimplementation.
15
- */
16
-
17
- const VENDOR = "converted from Audiokinetic Wwise by @carbonenginejs/runtime-resource";
18
- function parseError(message) {
19
- const error = new Error(`wem: ${message}`);
20
- error.code = "CJS_FORMAT_PARSE_ERROR";
21
- return error;
22
- }
23
- function notSupported(message) {
24
- const error = new Error(`wem: ${message}`);
25
- error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
26
- return error;
27
- }
28
-
29
- /**
30
- * Parse the Wwise Vorbis stream layout needed for repacking.
31
- *
32
- * @param {Uint8Array} bytes Wem bytes.
33
- * @returns {object} Stream layout, vorb fields, and loop info.
34
- */
35
- function parseWemVorbisLayout(bytes) {
36
- if (bytes.length < 12) throw parseError("missing RIFF");
37
- const container = String.fromCharCode(bytes[0], bytes[1], bytes[2], bytes[3]);
38
- if (container !== "RIFF" && container !== "RIFX") throw parseError("missing RIFF");
39
- const littleEndian = container !== "RIFX";
40
- const readU16 = littleEndian ? readU16LE : readU16BE;
41
- const readU32 = littleEndian ? readU32LE : readU32BE;
42
- if (String.fromCharCode(bytes[8], bytes[9], bytes[10], bytes[11]) !== "WAVE") throw parseError("missing WAVE");
43
- const riffSize = readU32(bytes, 4) + 8;
44
- if (riffSize > bytes.length) throw parseError("RIFF truncated");
45
- let fmtOffset = -1,
46
- fmtSize = -1;
47
- let vorbOffset = -1,
48
- vorbSize = -1;
49
- let dataOffset = -1,
50
- dataSize = -1;
51
- let smplOffset = -1;
52
- let chunkOffset = 12;
53
- while (chunkOffset < riffSize) {
54
- if (chunkOffset + 8 > riffSize) throw parseError("chunk header truncated");
55
- const id = String.fromCharCode(bytes[chunkOffset], bytes[chunkOffset + 1], bytes[chunkOffset + 2], bytes[chunkOffset + 3]);
56
- const size = readU32(bytes, chunkOffset + 4);
57
- const contentOffset = chunkOffset + 8;
58
- if (id === "fmt ") {
59
- fmtOffset = contentOffset;
60
- fmtSize = size;
61
- } else if (id === "vorb") {
62
- vorbOffset = contentOffset;
63
- vorbSize = size;
64
- } else if (id === "data") {
65
- dataOffset = contentOffset;
66
- dataSize = size;
67
- } else if (id === "smpl") {
68
- smplOffset = contentOffset;
69
- }
70
- chunkOffset = contentOffset + size;
71
- }
72
- if (chunkOffset > riffSize) throw parseError("chunk truncated");
73
- if (fmtOffset === -1 || dataOffset === -1) throw parseError("expected fmt, data chunks");
74
- if (vorbOffset === -1 && fmtSize !== 0x42) throw parseError("expected 0x42 fmt if vorb missing (not Wwise Vorbis?)");
75
- if (vorbOffset !== -1 && fmtSize !== 0x28 && fmtSize !== 0x18 && fmtSize !== 0x12) throw parseError("bad fmt size");
76
- if (vorbOffset === -1 && fmtSize === 0x42) {
77
- vorbOffset = fmtOffset + 0x18;
78
- vorbSize = -1;
79
- }
80
- if (readU16(bytes, fmtOffset) !== 0xffff) throw parseError("bad codec id (not Wwise Vorbis)");
81
- const channels = readU16(bytes, fmtOffset + 2);
82
- const sampleRate = readU32(bytes, fmtOffset + 4);
83
- const avgBytesPerSecond = readU32(bytes, fmtOffset + 8);
84
- if (readU16(bytes, fmtOffset + 12) !== 0) throw parseError("bad block align");
85
- if (readU16(bytes, fmtOffset + 14) !== 0) throw parseError("expected 0 bps");
86
- if (readU16(bytes, fmtOffset + 16) !== fmtSize - 0x12) throw parseError("bad extra fmt length");
87
- if (vorbSize !== -1 && vorbSize !== 0x28 && vorbSize !== 0x2a && vorbSize !== 0x2c && vorbSize !== 0x32 && vorbSize !== 0x34) {
88
- throw parseError("bad vorb size");
89
- }
90
- if (vorbSize === 0x28 || vorbSize === 0x2c) {
91
- throw notSupported("old header-triad Wwise Vorbis is not supported by this repacker");
92
- }
93
- const sampleCount = readU32(bytes, vorbOffset);
94
- let noGranule = false;
95
- let modPackets = false;
96
- if (vorbSize === -1 || vorbSize === 0x2a) {
97
- noGranule = true;
98
- const modSignal = readU32(bytes, vorbOffset + 0x4);
99
- if (modSignal !== 0x4a && modSignal !== 0x4b && modSignal !== 0x69 && modSignal !== 0x70) {
100
- modPackets = true;
101
- }
102
- }
103
- const offsetInfoBase = vorbSize === -1 || vorbSize === 0x2a ? vorbOffset + 0x10 : vorbOffset + 0x18;
104
- const setupPacketOffset = readU32(bytes, offsetInfoBase);
105
- const firstAudioPacketOffset = readU32(bytes, offsetInfoBase + 4);
106
- const blocksizeBase = vorbSize === -1 || vorbSize === 0x2a ? vorbOffset + 0x24 : vorbOffset + 0x2c;
107
- const uid = readU32(bytes, blocksizeBase);
108
- const blocksize0Pow = bytes[blocksizeBase + 4];
109
- const blocksize1Pow = bytes[blocksizeBase + 5];
110
- let loop = null;
111
- if (smplOffset !== -1) {
112
- const loopCount = readU32(bytes, smplOffset + 0x1c);
113
- if (loopCount === 1) {
114
- const loopStart = readU32(bytes, smplOffset + 0x2c);
115
- let loopEnd = readU32(bytes, smplOffset + 0x30);
116
- loopEnd = loopEnd === 0 ? sampleCount : loopEnd + 1;
117
- if (loopStart < sampleCount && loopEnd <= sampleCount && loopStart <= loopEnd) {
118
- loop = {
119
- start: loopStart,
120
- end: loopEnd
121
- };
122
- }
123
- }
124
- }
125
- return {
126
- littleEndian,
127
- channels,
128
- sampleRate,
129
- avgBytesPerSecond,
130
- sampleCount,
131
- noGranule,
132
- modPackets,
133
- setupPacketOffset,
134
- firstAudioPacketOffset,
135
- uid,
136
- blocksize0Pow,
137
- blocksize1Pow,
138
- dataOffset,
139
- dataSize,
140
- loop
141
- };
142
- }
143
- function readPacketHeader(bytes, offset, layout) {
144
- const readU16 = layout.littleEndian ? readU16LE : readU16BE;
145
- const readU32 = layout.littleEndian ? readU32LE : readU32BE;
146
- const headerSize = layout.noGranule ? 2 : 6;
147
- const end = layout.dataOffset + layout.dataSize;
148
- if (offset + headerSize > end) throw parseError("packet header truncated");
149
- const size = readU16(bytes, offset);
150
- const granule = layout.noGranule ? 0 : readU32(bytes, offset + 2);
151
- return {
152
- size,
153
- granule,
154
- payloadOffset: offset + headerSize,
155
- nextOffset: offset + headerSize + size
156
- };
157
- }
158
- function writeVorbisHeaderType(writer, type) {
159
- writer.writeBits(type, 8);
160
- const word = "vorbis";
161
- for (let i = 0; i < 6; i++) writer.writeBits(word.charCodeAt(i), 8);
162
- }
163
- function generateHeaders(bytes, layout, library, writer) {
164
- // identification packet
165
- writeVorbisHeaderType(writer, 1);
166
- writer.writeBits(0, 32);
167
- writer.writeBits(layout.channels, 8);
168
- writer.writeBits(layout.sampleRate, 32);
169
- writer.writeBits(0, 32);
170
- writer.writeBits(layout.avgBytesPerSecond * 8 >>> 0, 32);
171
- writer.writeBits(0, 32);
172
- writer.writeBits(layout.blocksize0Pow, 4);
173
- writer.writeBits(layout.blocksize1Pow, 4);
174
- writer.writeBits(1, 1);
175
- writer.flushPage();
176
-
177
- // comment packet
178
- writeVorbisHeaderType(writer, 3);
179
- writer.writeBits(VENDOR.length, 32);
180
- for (let i = 0; i < VENDOR.length; i++) writer.writeBits(VENDOR.charCodeAt(i), 8);
181
- if (!layout.loop) {
182
- writer.writeBits(0, 32);
183
- } else {
184
- const comments = [`LoopStart=${layout.loop.start}`, `LoopEnd=${layout.loop.end}`];
185
- writer.writeBits(comments.length, 32);
186
- for (const comment of comments) {
187
- writer.writeBits(comment.length, 32);
188
- for (let i = 0; i < comment.length; i++) writer.writeBits(comment.charCodeAt(i), 8);
189
- }
190
- }
191
- writer.writeBits(1, 1);
192
- writer.flushPage();
193
-
194
- // setup packet: expand codebooks, copy floors/residues/mappings/modes
195
- writeVorbisHeaderType(writer, 5);
196
- const setupHeader = readPacketHeader(bytes, layout.dataOffset + layout.setupPacketOffset, layout);
197
- if (setupHeader.granule !== 0) throw parseError("setup packet granule != 0");
198
- const reader = new BitReader(bytes, setupHeader.payloadOffset);
199
- const codebookCountLess1 = reader.readBits(8);
200
- const codebookCount = codebookCountLess1 + 1;
201
- writer.writeBits(codebookCountLess1, 8);
202
- for (let i = 0; i < codebookCount; i++) {
203
- const codebookId = reader.readBits(10);
204
- rebuildCodebookById(library, codebookId, writer);
205
- }
206
-
207
- // time-domain transform placeholder
208
- writer.writeBits(0, 6);
209
- writer.writeBits(0, 16);
210
-
211
- // floors
212
- const floorCountLess1 = reader.readBits(6);
213
- const floorCount = floorCountLess1 + 1;
214
- writer.writeBits(floorCountLess1, 6);
215
- for (let i = 0; i < floorCount; i++) {
216
- writer.writeBits(1, 16);
217
- const floor1Partitions = reader.readBits(5);
218
- writer.writeBits(floor1Partitions, 5);
219
- const partitionClassList = new Array(floor1Partitions);
220
- let maximumClass = 0;
221
- for (let j = 0; j < floor1Partitions; j++) {
222
- const partitionClass = reader.readBits(4);
223
- writer.writeBits(partitionClass, 4);
224
- partitionClassList[j] = partitionClass;
225
- if (partitionClass > maximumClass) maximumClass = partitionClass;
226
- }
227
- const classDimensionsList = new Array(maximumClass + 1);
228
- for (let j = 0; j <= maximumClass; j++) {
229
- const classDimensionsLess1 = reader.readBits(3);
230
- writer.writeBits(classDimensionsLess1, 3);
231
- classDimensionsList[j] = classDimensionsLess1 + 1;
232
- const classSubclasses = reader.readBits(2);
233
- writer.writeBits(classSubclasses, 2);
234
- if (classSubclasses !== 0) {
235
- const masterbook = reader.readBits(8);
236
- writer.writeBits(masterbook, 8);
237
- if (masterbook >= codebookCount) throw parseError("invalid floor1 masterbook");
238
- }
239
- for (let k = 0; k < 1 << classSubclasses; k++) {
240
- const subclassBookPlus1 = reader.readBits(8);
241
- writer.writeBits(subclassBookPlus1, 8);
242
- if (subclassBookPlus1 - 1 >= 0 && subclassBookPlus1 - 1 >= codebookCount) {
243
- throw parseError("invalid floor1 subclass book");
244
- }
245
- }
246
- }
247
- writer.writeBits(reader.readBits(2), 2);
248
- const rangebits = reader.readBits(4);
249
- writer.writeBits(rangebits, 4);
250
- for (let j = 0; j < floor1Partitions; j++) {
251
- for (let k = 0; k < classDimensionsList[partitionClassList[j]]; k++) {
252
- writer.writeBits(reader.readBits(rangebits), rangebits);
253
- }
254
- }
255
- }
256
-
257
- // residues
258
- const residueCountLess1 = reader.readBits(6);
259
- const residueCount = residueCountLess1 + 1;
260
- writer.writeBits(residueCountLess1, 6);
261
- for (let i = 0; i < residueCount; i++) {
262
- const residueType = reader.readBits(2);
263
- writer.writeBits(residueType, 16);
264
- if (residueType > 2) throw parseError("invalid residue type");
265
- const residueBegin = reader.readBits(24);
266
- const residueEnd = reader.readBits(24);
267
- const residuePartitionSizeLess1 = reader.readBits(24);
268
- const residueClassificationsLess1 = reader.readBits(6);
269
- const residueClassbook = reader.readBits(8);
270
- const residueClassifications = residueClassificationsLess1 + 1;
271
- writer.writeBits(residueBegin, 24);
272
- writer.writeBits(residueEnd, 24);
273
- writer.writeBits(residuePartitionSizeLess1, 24);
274
- writer.writeBits(residueClassificationsLess1, 6);
275
- writer.writeBits(residueClassbook, 8);
276
- if (residueClassbook >= codebookCount) throw parseError("invalid residue classbook");
277
- const residueCascade = new Array(residueClassifications);
278
- for (let j = 0; j < residueClassifications; j++) {
279
- const lowBits = reader.readBits(3);
280
- writer.writeBits(lowBits, 3);
281
- const bitflag = reader.readBits(1);
282
- writer.writeBits(bitflag, 1);
283
- let highBits = 0;
284
- if (bitflag) {
285
- highBits = reader.readBits(5);
286
- writer.writeBits(highBits, 5);
287
- }
288
- residueCascade[j] = highBits * 8 + lowBits;
289
- }
290
- for (let j = 0; j < residueClassifications; j++) {
291
- for (let k = 0; k < 8; k++) {
292
- if (residueCascade[j] & 1 << k) {
293
- const residueBook = reader.readBits(8);
294
- writer.writeBits(residueBook, 8);
295
- if (residueBook >= codebookCount) throw parseError("invalid residue book");
296
- }
297
- }
298
- }
299
- }
300
-
301
- // mappings
302
- const mappingCountLess1 = reader.readBits(6);
303
- const mappingCount = mappingCountLess1 + 1;
304
- writer.writeBits(mappingCountLess1, 6);
305
- for (let i = 0; i < mappingCount; i++) {
306
- writer.writeBits(0, 16);
307
- const submapsFlag = reader.readBits(1);
308
- writer.writeBits(submapsFlag, 1);
309
- let submaps = 1;
310
- if (submapsFlag) {
311
- const submapsLess1 = reader.readBits(4);
312
- writer.writeBits(submapsLess1, 4);
313
- submaps = submapsLess1 + 1;
314
- }
315
- const squarePolarFlag = reader.readBits(1);
316
- writer.writeBits(squarePolarFlag, 1);
317
- if (squarePolarFlag) {
318
- const couplingStepsLess1 = reader.readBits(8);
319
- writer.writeBits(couplingStepsLess1, 8);
320
- const couplingBits = ilog(layout.channels - 1);
321
- for (let j = 0; j < couplingStepsLess1 + 1; j++) {
322
- const magnitude = reader.readBits(couplingBits);
323
- const angle = reader.readBits(couplingBits);
324
- writer.writeBits(magnitude, couplingBits);
325
- writer.writeBits(angle, couplingBits);
326
- if (angle === magnitude || magnitude >= layout.channels || angle >= layout.channels) {
327
- throw parseError("invalid coupling");
328
- }
329
- }
330
- }
331
- const mappingReserved = reader.readBits(2);
332
- writer.writeBits(mappingReserved, 2);
333
- if (mappingReserved !== 0) throw parseError("mapping reserved field nonzero");
334
- if (submaps > 1) {
335
- for (let j = 0; j < layout.channels; j++) {
336
- const mappingMux = reader.readBits(4);
337
- writer.writeBits(mappingMux, 4);
338
- if (mappingMux >= submaps) throw parseError("mapping_mux >= submaps");
339
- }
340
- }
341
- for (let j = 0; j < submaps; j++) {
342
- writer.writeBits(reader.readBits(8), 8);
343
- const floorNumber = reader.readBits(8);
344
- writer.writeBits(floorNumber, 8);
345
- if (floorNumber >= floorCount) throw parseError("invalid floor mapping");
346
- const residueNumber = reader.readBits(8);
347
- writer.writeBits(residueNumber, 8);
348
- if (residueNumber >= residueCount) throw parseError("invalid residue mapping");
349
- }
350
- }
351
-
352
- // modes
353
- const modeCountLess1 = reader.readBits(6);
354
- const modeCount = modeCountLess1 + 1;
355
- writer.writeBits(modeCountLess1, 6);
356
- const modeBlockflag = new Array(modeCount);
357
- const modeBits = ilog(modeCount - 1);
358
- for (let i = 0; i < modeCount; i++) {
359
- const blockFlag = reader.readBits(1);
360
- writer.writeBits(blockFlag, 1);
361
- modeBlockflag[i] = blockFlag !== 0;
362
- writer.writeBits(0, 16);
363
- writer.writeBits(0, 16);
364
- const mapping = reader.readBits(8);
365
- writer.writeBits(mapping, 8);
366
- if (mapping >= mappingCount) throw parseError("invalid mode mapping");
367
- }
368
- writer.writeBits(1, 1);
369
- writer.flushPage();
370
- if (Math.floor((reader.totalBitsRead + 7) / 8) !== setupHeader.size) {
371
- throw parseError("didn't read exactly setup packet");
372
- }
373
- if (setupHeader.nextOffset !== layout.dataOffset + layout.firstAudioPacketOffset) {
374
- throw parseError("first audio packet doesn't follow setup packet");
375
- }
376
- return {
377
- modeBlockflag,
378
- modeBits
379
- };
380
- }
381
-
382
- /**
383
- * Repack a Wwise Vorbis wem into a standard Ogg Vorbis stream.
384
- *
385
- * @param {Uint8Array} bytes Wem bytes (RIFF/RIFX with 0xFFFF Vorbis fmt).
386
- * @param {object} [options] Options.
387
- * @param {Uint8Array} [options.codebooks] Packed codebook library override
388
- * (defaults to the bundled aoTuV 6.03 library).
389
- * @returns {object} `{ bytes, sampleCount, sampleRate, channels, durationSeconds, loop, pageCount, modPackets }`.
390
- */
391
- function convertWemToOgg(bytes, options = {}) {
392
- const layout = parseWemVorbisLayout(bytes);
393
- const library = parseCodebookLibrary(options.codebooks || getPackedCodebooksAotuv603());
394
- const writer = new OggPageWriter();
395
- const {
396
- modeBlockflag,
397
- modeBits
398
- } = generateHeaders(bytes, layout, library, writer);
399
- const blocksize0 = 1 << layout.blocksize0Pow;
400
- const blocksize1 = 1 << layout.blocksize1Pow;
401
- const dataEnd = layout.dataOffset + layout.dataSize;
402
- let offset = layout.dataOffset + layout.firstAudioPacketOffset;
403
- let prevBlockflag = false;
404
- let prevBlocksize = 0;
405
- let granule = 0;
406
- let lastWemGranule = 0;
407
- while (offset < dataEnd) {
408
- const packet = readPacketHeader(bytes, offset, layout);
409
- if (packet.nextOffset > dataEnd) throw parseError("packet truncated");
410
- if (packet.size === 0) throw parseError("empty audio packet");
411
- const payload = bytes.subarray(packet.payloadOffset, packet.payloadOffset + packet.size);
412
- const payloadReader = new BitReader(payload);
413
- const isLast = packet.nextOffset >= dataEnd;
414
- let modeNumber;
415
- if (layout.modPackets) {
416
- // rebuild the packet type and window bits Wwise strips
417
- writer.writeBits(0, 1);
418
- modeNumber = payloadReader.readBits(modeBits);
419
- writer.writeBits(modeNumber, modeBits);
420
- const remainder = payloadReader.readBits(8 - modeBits);
421
- if (modeBlockflag[modeNumber]) {
422
- let nextBlockflag = false;
423
- if (!isLast) {
424
- const nextPacket = readPacketHeader(bytes, packet.nextOffset, layout);
425
- if (nextPacket.size > 0) {
426
- const nextReader = new BitReader(bytes, nextPacket.payloadOffset);
427
- nextBlockflag = modeBlockflag[nextReader.readBits(modeBits)];
428
- }
429
- }
430
- writer.writeBits(prevBlockflag ? 1 : 0, 1);
431
- writer.writeBits(nextBlockflag ? 1 : 0, 1);
432
- }
433
- prevBlockflag = modeBlockflag[modeNumber];
434
- writer.writeBits(remainder, 8 - modeBits);
435
- for (let i = 1; i < packet.size; i++) {
436
- writer.writeBits(payload[i], 8);
437
- }
438
- } else {
439
- // standard packet: copy verbatim, peek the mode for granule math
440
- const packetType = payloadReader.readBits(1);
441
- modeNumber = payloadReader.readBits(modeBits);
442
- if (packetType !== 0) throw parseError("audio packet is not type 0");
443
- for (let i = 0; i < packet.size; i++) {
444
- writer.writeBits(payload[i], 8);
445
- }
446
- }
447
- const blocksize = modeBlockflag[modeNumber] ? blocksize1 : blocksize0;
448
- if (prevBlocksize !== 0) {
449
- granule += prevBlocksize + blocksize >> 2;
450
- }
451
- prevBlocksize = blocksize;
452
- if (packet.granule) lastWemGranule = packet.granule;
453
- writer.setGranule(isLast ? Math.min(granule, layout.sampleCount) : granule);
454
- writer.flushPage(isLast);
455
- offset = packet.nextOffset;
456
- }
457
- if (offset > dataEnd) throw parseError("page truncated");
458
- return {
459
- bytes: writer.toBytes(),
460
- sampleCount: layout.sampleCount,
461
- sampleRate: layout.sampleRate,
462
- channels: layout.channels,
463
- durationSeconds: layout.sampleRate ? layout.sampleCount / layout.sampleRate : 0,
464
- loop: layout.loop,
465
- pageCount: writer.pageCount,
466
- modPackets: layout.modPackets,
467
- computedGranule: granule,
468
- lastWemGranule
469
- };
470
- }
471
- function readU16LE(bytes, offset) {
472
- return bytes[offset] | bytes[offset + 1] << 8;
473
- }
474
- function readU32LE(bytes, offset) {
475
- return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] * 0x1000000) >>> 0;
476
- }
477
- function readU16BE(bytes, offset) {
478
- return bytes[offset] << 8 | bytes[offset + 1];
479
- }
480
- function readU32BE(bytes, offset) {
481
- return bytes[offset] * 0x1000000 + (bytes[offset + 1] << 16) + (bytes[offset + 2] << 8) + bytes[offset + 3] >>> 0;
482
- }
483
-
484
- export { convertWemToOgg, parseWemVorbisLayout };
485
- //# sourceMappingURL=wemToOgg.js.map
1
+ import { OggPageWriter, BitReader, ilog } from './bitStream.js';
2
+ import { parseCodebookLibrary, rebuildCodebookById } from './codebookLibrary.js';
3
+ import { getPackedCodebooksAotuv603 } from './packedCodebooksAotuv603.js';
4
+
5
+ /**
6
+ * Wwise Vorbis (.wem) to standard Ogg Vorbis repacker.
7
+ *
8
+ * This is a lossless container transform: the identification/comment/setup
9
+ * headers are regenerated (setup codebooks expanded from the packed aoTuV
10
+ * 6.03 library), modified audio packets get their type/window bits restored,
11
+ * and packets are paginated with correct granule positions computed from the
12
+ * mode block flags — so no separate revorb pass is needed. No audio is
13
+ * decoded or re-encoded. Algorithm behavior follows the ww2ogg reference
14
+ * (BSD-licensed) as an original reimplementation.
15
+ */
16
+
17
+ const VENDOR = "converted from Audiokinetic Wwise by @carbonenginejs/runtime-resource";
18
+ function parseError(message) {
19
+ const error = new Error(`wem: ${message}`);
20
+ error.code = "CJS_FORMAT_PARSE_ERROR";
21
+ return error;
22
+ }
23
+ function notSupported(message) {
24
+ const error = new Error(`wem: ${message}`);
25
+ error.code = "CJS_FORMAT_OUTPUT_NOT_SUPPORTED";
26
+ return error;
27
+ }
28
+
29
+ /**
30
+ * Parse the Wwise Vorbis stream layout needed for repacking.
31
+ *
32
+ * @param {Uint8Array} bytes Wem bytes.
33
+ * @returns {object} Stream layout, vorb fields, and loop info.
34
+ */
35
+ function parseWemVorbisLayout(bytes) {
36
+ if (bytes.length < 12) throw parseError("missing RIFF");
37
+ const container = String.fromCharCode(bytes[0], bytes[1], bytes[2], bytes[3]);
38
+ if (container !== "RIFF" && container !== "RIFX") throw parseError("missing RIFF");
39
+ const littleEndian = container !== "RIFX";
40
+ const readU16 = littleEndian ? readU16LE : readU16BE;
41
+ const readU32 = littleEndian ? readU32LE : readU32BE;
42
+ if (String.fromCharCode(bytes[8], bytes[9], bytes[10], bytes[11]) !== "WAVE") throw parseError("missing WAVE");
43
+ const riffSize = readU32(bytes, 4) + 8;
44
+ if (riffSize > bytes.length) throw parseError("RIFF truncated");
45
+ let fmtOffset = -1,
46
+ fmtSize = -1;
47
+ let vorbOffset = -1,
48
+ vorbSize = -1;
49
+ let dataOffset = -1,
50
+ dataSize = -1;
51
+ let smplOffset = -1;
52
+ let chunkOffset = 12;
53
+ while (chunkOffset < riffSize) {
54
+ if (chunkOffset + 8 > riffSize) throw parseError("chunk header truncated");
55
+ const id = String.fromCharCode(bytes[chunkOffset], bytes[chunkOffset + 1], bytes[chunkOffset + 2], bytes[chunkOffset + 3]);
56
+ const size = readU32(bytes, chunkOffset + 4);
57
+ const contentOffset = chunkOffset + 8;
58
+ if (id === "fmt ") {
59
+ fmtOffset = contentOffset;
60
+ fmtSize = size;
61
+ } else if (id === "vorb") {
62
+ vorbOffset = contentOffset;
63
+ vorbSize = size;
64
+ } else if (id === "data") {
65
+ dataOffset = contentOffset;
66
+ dataSize = size;
67
+ } else if (id === "smpl") {
68
+ smplOffset = contentOffset;
69
+ }
70
+ chunkOffset = contentOffset + size;
71
+ }
72
+ if (chunkOffset > riffSize) throw parseError("chunk truncated");
73
+ if (fmtOffset === -1 || dataOffset === -1) throw parseError("expected fmt, data chunks");
74
+ if (vorbOffset === -1 && fmtSize !== 0x42) throw parseError("expected 0x42 fmt if vorb missing (not Wwise Vorbis?)");
75
+ if (vorbOffset !== -1 && fmtSize !== 0x28 && fmtSize !== 0x18 && fmtSize !== 0x12) throw parseError("bad fmt size");
76
+ if (vorbOffset === -1 && fmtSize === 0x42) {
77
+ vorbOffset = fmtOffset + 0x18;
78
+ vorbSize = -1;
79
+ }
80
+ if (readU16(bytes, fmtOffset) !== 0xffff) throw parseError("bad codec id (not Wwise Vorbis)");
81
+ const channels = readU16(bytes, fmtOffset + 2);
82
+ const sampleRate = readU32(bytes, fmtOffset + 4);
83
+ const avgBytesPerSecond = readU32(bytes, fmtOffset + 8);
84
+ if (readU16(bytes, fmtOffset + 12) !== 0) throw parseError("bad block align");
85
+ if (readU16(bytes, fmtOffset + 14) !== 0) throw parseError("expected 0 bps");
86
+ if (readU16(bytes, fmtOffset + 16) !== fmtSize - 0x12) throw parseError("bad extra fmt length");
87
+ if (vorbSize !== -1 && vorbSize !== 0x28 && vorbSize !== 0x2a && vorbSize !== 0x2c && vorbSize !== 0x32 && vorbSize !== 0x34) {
88
+ throw parseError("bad vorb size");
89
+ }
90
+ if (vorbSize === 0x28 || vorbSize === 0x2c) {
91
+ throw notSupported("old header-triad Wwise Vorbis is not supported by this repacker");
92
+ }
93
+ const sampleCount = readU32(bytes, vorbOffset);
94
+ let noGranule = false;
95
+ let modPackets = false;
96
+ if (vorbSize === -1 || vorbSize === 0x2a) {
97
+ noGranule = true;
98
+ const modSignal = readU32(bytes, vorbOffset + 0x4);
99
+ if (modSignal !== 0x4a && modSignal !== 0x4b && modSignal !== 0x69 && modSignal !== 0x70) {
100
+ modPackets = true;
101
+ }
102
+ }
103
+ const offsetInfoBase = vorbSize === -1 || vorbSize === 0x2a ? vorbOffset + 0x10 : vorbOffset + 0x18;
104
+ const setupPacketOffset = readU32(bytes, offsetInfoBase);
105
+ const firstAudioPacketOffset = readU32(bytes, offsetInfoBase + 4);
106
+ const blocksizeBase = vorbSize === -1 || vorbSize === 0x2a ? vorbOffset + 0x24 : vorbOffset + 0x2c;
107
+ const uid = readU32(bytes, blocksizeBase);
108
+ const blocksize0Pow = bytes[blocksizeBase + 4];
109
+ const blocksize1Pow = bytes[blocksizeBase + 5];
110
+ let loop = null;
111
+ if (smplOffset !== -1) {
112
+ const loopCount = readU32(bytes, smplOffset + 0x1c);
113
+ if (loopCount === 1) {
114
+ const loopStart = readU32(bytes, smplOffset + 0x2c);
115
+ let loopEnd = readU32(bytes, smplOffset + 0x30);
116
+ loopEnd = loopEnd === 0 ? sampleCount : loopEnd + 1;
117
+ if (loopStart < sampleCount && loopEnd <= sampleCount && loopStart <= loopEnd) {
118
+ loop = {
119
+ start: loopStart,
120
+ end: loopEnd
121
+ };
122
+ }
123
+ }
124
+ }
125
+ return {
126
+ littleEndian,
127
+ channels,
128
+ sampleRate,
129
+ avgBytesPerSecond,
130
+ sampleCount,
131
+ noGranule,
132
+ modPackets,
133
+ setupPacketOffset,
134
+ firstAudioPacketOffset,
135
+ uid,
136
+ blocksize0Pow,
137
+ blocksize1Pow,
138
+ dataOffset,
139
+ dataSize,
140
+ loop
141
+ };
142
+ }
143
+ function readPacketHeader(bytes, offset, layout) {
144
+ const readU16 = layout.littleEndian ? readU16LE : readU16BE;
145
+ const readU32 = layout.littleEndian ? readU32LE : readU32BE;
146
+ const headerSize = layout.noGranule ? 2 : 6;
147
+ const end = layout.dataOffset + layout.dataSize;
148
+ if (offset + headerSize > end) throw parseError("packet header truncated");
149
+ const size = readU16(bytes, offset);
150
+ const granule = layout.noGranule ? 0 : readU32(bytes, offset + 2);
151
+ return {
152
+ size,
153
+ granule,
154
+ payloadOffset: offset + headerSize,
155
+ nextOffset: offset + headerSize + size
156
+ };
157
+ }
158
+ function writeVorbisHeaderType(writer, type) {
159
+ writer.writeBits(type, 8);
160
+ const word = "vorbis";
161
+ for (let i = 0; i < 6; i++) writer.writeBits(word.charCodeAt(i), 8);
162
+ }
163
+ function generateHeaders(bytes, layout, library, writer) {
164
+ // identification packet
165
+ writeVorbisHeaderType(writer, 1);
166
+ writer.writeBits(0, 32);
167
+ writer.writeBits(layout.channels, 8);
168
+ writer.writeBits(layout.sampleRate, 32);
169
+ writer.writeBits(0, 32);
170
+ writer.writeBits(layout.avgBytesPerSecond * 8 >>> 0, 32);
171
+ writer.writeBits(0, 32);
172
+ writer.writeBits(layout.blocksize0Pow, 4);
173
+ writer.writeBits(layout.blocksize1Pow, 4);
174
+ writer.writeBits(1, 1);
175
+ writer.flushPage();
176
+
177
+ // comment packet
178
+ writeVorbisHeaderType(writer, 3);
179
+ writer.writeBits(VENDOR.length, 32);
180
+ for (let i = 0; i < VENDOR.length; i++) writer.writeBits(VENDOR.charCodeAt(i), 8);
181
+ if (!layout.loop) {
182
+ writer.writeBits(0, 32);
183
+ } else {
184
+ const comments = [`LoopStart=${layout.loop.start}`, `LoopEnd=${layout.loop.end}`];
185
+ writer.writeBits(comments.length, 32);
186
+ for (const comment of comments) {
187
+ writer.writeBits(comment.length, 32);
188
+ for (let i = 0; i < comment.length; i++) writer.writeBits(comment.charCodeAt(i), 8);
189
+ }
190
+ }
191
+ writer.writeBits(1, 1);
192
+ writer.flushPage();
193
+
194
+ // setup packet: expand codebooks, copy floors/residues/mappings/modes
195
+ writeVorbisHeaderType(writer, 5);
196
+ const setupHeader = readPacketHeader(bytes, layout.dataOffset + layout.setupPacketOffset, layout);
197
+ if (setupHeader.granule !== 0) throw parseError("setup packet granule != 0");
198
+ const reader = new BitReader(bytes, setupHeader.payloadOffset);
199
+ const codebookCountLess1 = reader.readBits(8);
200
+ const codebookCount = codebookCountLess1 + 1;
201
+ writer.writeBits(codebookCountLess1, 8);
202
+ for (let i = 0; i < codebookCount; i++) {
203
+ const codebookId = reader.readBits(10);
204
+ rebuildCodebookById(library, codebookId, writer);
205
+ }
206
+
207
+ // time-domain transform placeholder
208
+ writer.writeBits(0, 6);
209
+ writer.writeBits(0, 16);
210
+
211
+ // floors
212
+ const floorCountLess1 = reader.readBits(6);
213
+ const floorCount = floorCountLess1 + 1;
214
+ writer.writeBits(floorCountLess1, 6);
215
+ for (let i = 0; i < floorCount; i++) {
216
+ writer.writeBits(1, 16);
217
+ const floor1Partitions = reader.readBits(5);
218
+ writer.writeBits(floor1Partitions, 5);
219
+ const partitionClassList = new Array(floor1Partitions);
220
+ let maximumClass = 0;
221
+ for (let j = 0; j < floor1Partitions; j++) {
222
+ const partitionClass = reader.readBits(4);
223
+ writer.writeBits(partitionClass, 4);
224
+ partitionClassList[j] = partitionClass;
225
+ if (partitionClass > maximumClass) maximumClass = partitionClass;
226
+ }
227
+ const classDimensionsList = new Array(maximumClass + 1);
228
+ for (let j = 0; j <= maximumClass; j++) {
229
+ const classDimensionsLess1 = reader.readBits(3);
230
+ writer.writeBits(classDimensionsLess1, 3);
231
+ classDimensionsList[j] = classDimensionsLess1 + 1;
232
+ const classSubclasses = reader.readBits(2);
233
+ writer.writeBits(classSubclasses, 2);
234
+ if (classSubclasses !== 0) {
235
+ const masterbook = reader.readBits(8);
236
+ writer.writeBits(masterbook, 8);
237
+ if (masterbook >= codebookCount) throw parseError("invalid floor1 masterbook");
238
+ }
239
+ for (let k = 0; k < 1 << classSubclasses; k++) {
240
+ const subclassBookPlus1 = reader.readBits(8);
241
+ writer.writeBits(subclassBookPlus1, 8);
242
+ if (subclassBookPlus1 - 1 >= 0 && subclassBookPlus1 - 1 >= codebookCount) {
243
+ throw parseError("invalid floor1 subclass book");
244
+ }
245
+ }
246
+ }
247
+ writer.writeBits(reader.readBits(2), 2);
248
+ const rangebits = reader.readBits(4);
249
+ writer.writeBits(rangebits, 4);
250
+ for (let j = 0; j < floor1Partitions; j++) {
251
+ for (let k = 0; k < classDimensionsList[partitionClassList[j]]; k++) {
252
+ writer.writeBits(reader.readBits(rangebits), rangebits);
253
+ }
254
+ }
255
+ }
256
+
257
+ // residues
258
+ const residueCountLess1 = reader.readBits(6);
259
+ const residueCount = residueCountLess1 + 1;
260
+ writer.writeBits(residueCountLess1, 6);
261
+ for (let i = 0; i < residueCount; i++) {
262
+ const residueType = reader.readBits(2);
263
+ writer.writeBits(residueType, 16);
264
+ if (residueType > 2) throw parseError("invalid residue type");
265
+ const residueBegin = reader.readBits(24);
266
+ const residueEnd = reader.readBits(24);
267
+ const residuePartitionSizeLess1 = reader.readBits(24);
268
+ const residueClassificationsLess1 = reader.readBits(6);
269
+ const residueClassbook = reader.readBits(8);
270
+ const residueClassifications = residueClassificationsLess1 + 1;
271
+ writer.writeBits(residueBegin, 24);
272
+ writer.writeBits(residueEnd, 24);
273
+ writer.writeBits(residuePartitionSizeLess1, 24);
274
+ writer.writeBits(residueClassificationsLess1, 6);
275
+ writer.writeBits(residueClassbook, 8);
276
+ if (residueClassbook >= codebookCount) throw parseError("invalid residue classbook");
277
+ const residueCascade = new Array(residueClassifications);
278
+ for (let j = 0; j < residueClassifications; j++) {
279
+ const lowBits = reader.readBits(3);
280
+ writer.writeBits(lowBits, 3);
281
+ const bitflag = reader.readBits(1);
282
+ writer.writeBits(bitflag, 1);
283
+ let highBits = 0;
284
+ if (bitflag) {
285
+ highBits = reader.readBits(5);
286
+ writer.writeBits(highBits, 5);
287
+ }
288
+ residueCascade[j] = highBits * 8 + lowBits;
289
+ }
290
+ for (let j = 0; j < residueClassifications; j++) {
291
+ for (let k = 0; k < 8; k++) {
292
+ if (residueCascade[j] & 1 << k) {
293
+ const residueBook = reader.readBits(8);
294
+ writer.writeBits(residueBook, 8);
295
+ if (residueBook >= codebookCount) throw parseError("invalid residue book");
296
+ }
297
+ }
298
+ }
299
+ }
300
+
301
+ // mappings
302
+ const mappingCountLess1 = reader.readBits(6);
303
+ const mappingCount = mappingCountLess1 + 1;
304
+ writer.writeBits(mappingCountLess1, 6);
305
+ for (let i = 0; i < mappingCount; i++) {
306
+ writer.writeBits(0, 16);
307
+ const submapsFlag = reader.readBits(1);
308
+ writer.writeBits(submapsFlag, 1);
309
+ let submaps = 1;
310
+ if (submapsFlag) {
311
+ const submapsLess1 = reader.readBits(4);
312
+ writer.writeBits(submapsLess1, 4);
313
+ submaps = submapsLess1 + 1;
314
+ }
315
+ const squarePolarFlag = reader.readBits(1);
316
+ writer.writeBits(squarePolarFlag, 1);
317
+ if (squarePolarFlag) {
318
+ const couplingStepsLess1 = reader.readBits(8);
319
+ writer.writeBits(couplingStepsLess1, 8);
320
+ const couplingBits = ilog(layout.channels - 1);
321
+ for (let j = 0; j < couplingStepsLess1 + 1; j++) {
322
+ const magnitude = reader.readBits(couplingBits);
323
+ const angle = reader.readBits(couplingBits);
324
+ writer.writeBits(magnitude, couplingBits);
325
+ writer.writeBits(angle, couplingBits);
326
+ if (angle === magnitude || magnitude >= layout.channels || angle >= layout.channels) {
327
+ throw parseError("invalid coupling");
328
+ }
329
+ }
330
+ }
331
+ const mappingReserved = reader.readBits(2);
332
+ writer.writeBits(mappingReserved, 2);
333
+ if (mappingReserved !== 0) throw parseError("mapping reserved field nonzero");
334
+ if (submaps > 1) {
335
+ for (let j = 0; j < layout.channels; j++) {
336
+ const mappingMux = reader.readBits(4);
337
+ writer.writeBits(mappingMux, 4);
338
+ if (mappingMux >= submaps) throw parseError("mapping_mux >= submaps");
339
+ }
340
+ }
341
+ for (let j = 0; j < submaps; j++) {
342
+ writer.writeBits(reader.readBits(8), 8);
343
+ const floorNumber = reader.readBits(8);
344
+ writer.writeBits(floorNumber, 8);
345
+ if (floorNumber >= floorCount) throw parseError("invalid floor mapping");
346
+ const residueNumber = reader.readBits(8);
347
+ writer.writeBits(residueNumber, 8);
348
+ if (residueNumber >= residueCount) throw parseError("invalid residue mapping");
349
+ }
350
+ }
351
+
352
+ // modes
353
+ const modeCountLess1 = reader.readBits(6);
354
+ const modeCount = modeCountLess1 + 1;
355
+ writer.writeBits(modeCountLess1, 6);
356
+ const modeBlockflag = new Array(modeCount);
357
+ const modeBits = ilog(modeCount - 1);
358
+ for (let i = 0; i < modeCount; i++) {
359
+ const blockFlag = reader.readBits(1);
360
+ writer.writeBits(blockFlag, 1);
361
+ modeBlockflag[i] = blockFlag !== 0;
362
+ writer.writeBits(0, 16);
363
+ writer.writeBits(0, 16);
364
+ const mapping = reader.readBits(8);
365
+ writer.writeBits(mapping, 8);
366
+ if (mapping >= mappingCount) throw parseError("invalid mode mapping");
367
+ }
368
+ writer.writeBits(1, 1);
369
+ writer.flushPage();
370
+ if (Math.floor((reader.totalBitsRead + 7) / 8) !== setupHeader.size) {
371
+ throw parseError("didn't read exactly setup packet");
372
+ }
373
+ if (setupHeader.nextOffset !== layout.dataOffset + layout.firstAudioPacketOffset) {
374
+ throw parseError("first audio packet doesn't follow setup packet");
375
+ }
376
+ return {
377
+ modeBlockflag,
378
+ modeBits
379
+ };
380
+ }
381
+
382
+ /**
383
+ * Repack a Wwise Vorbis wem into a standard Ogg Vorbis stream.
384
+ *
385
+ * @param {Uint8Array} bytes Wem bytes (RIFF/RIFX with 0xFFFF Vorbis fmt).
386
+ * @param {object} [options] Options.
387
+ * @param {Uint8Array} [options.codebooks] Packed codebook library override
388
+ * (defaults to the bundled aoTuV 6.03 library).
389
+ * @returns {object} `{ bytes, sampleCount, sampleRate, channels, durationSeconds, loop, pageCount, modPackets }`.
390
+ */
391
+ function convertWemToOgg(bytes, options = {}) {
392
+ const layout = parseWemVorbisLayout(bytes);
393
+ const library = parseCodebookLibrary(options.codebooks || getPackedCodebooksAotuv603());
394
+ const writer = new OggPageWriter();
395
+ const {
396
+ modeBlockflag,
397
+ modeBits
398
+ } = generateHeaders(bytes, layout, library, writer);
399
+ const blocksize0 = 1 << layout.blocksize0Pow;
400
+ const blocksize1 = 1 << layout.blocksize1Pow;
401
+ const dataEnd = layout.dataOffset + layout.dataSize;
402
+ let offset = layout.dataOffset + layout.firstAudioPacketOffset;
403
+ let prevBlockflag = false;
404
+ let prevBlocksize = 0;
405
+ let granule = 0;
406
+ let lastWemGranule = 0;
407
+ while (offset < dataEnd) {
408
+ const packet = readPacketHeader(bytes, offset, layout);
409
+ if (packet.nextOffset > dataEnd) throw parseError("packet truncated");
410
+ if (packet.size === 0) throw parseError("empty audio packet");
411
+ const payload = bytes.subarray(packet.payloadOffset, packet.payloadOffset + packet.size);
412
+ const payloadReader = new BitReader(payload);
413
+ const isLast = packet.nextOffset >= dataEnd;
414
+ let modeNumber;
415
+ if (layout.modPackets) {
416
+ // rebuild the packet type and window bits Wwise strips
417
+ writer.writeBits(0, 1);
418
+ modeNumber = payloadReader.readBits(modeBits);
419
+ writer.writeBits(modeNumber, modeBits);
420
+ const remainder = payloadReader.readBits(8 - modeBits);
421
+ if (modeBlockflag[modeNumber]) {
422
+ let nextBlockflag = false;
423
+ if (!isLast) {
424
+ const nextPacket = readPacketHeader(bytes, packet.nextOffset, layout);
425
+ if (nextPacket.size > 0) {
426
+ const nextReader = new BitReader(bytes, nextPacket.payloadOffset);
427
+ nextBlockflag = modeBlockflag[nextReader.readBits(modeBits)];
428
+ }
429
+ }
430
+ writer.writeBits(prevBlockflag ? 1 : 0, 1);
431
+ writer.writeBits(nextBlockflag ? 1 : 0, 1);
432
+ }
433
+ prevBlockflag = modeBlockflag[modeNumber];
434
+ writer.writeBits(remainder, 8 - modeBits);
435
+ for (let i = 1; i < packet.size; i++) {
436
+ writer.writeBits(payload[i], 8);
437
+ }
438
+ } else {
439
+ // standard packet: copy verbatim, peek the mode for granule math
440
+ const packetType = payloadReader.readBits(1);
441
+ modeNumber = payloadReader.readBits(modeBits);
442
+ if (packetType !== 0) throw parseError("audio packet is not type 0");
443
+ for (let i = 0; i < packet.size; i++) {
444
+ writer.writeBits(payload[i], 8);
445
+ }
446
+ }
447
+ const blocksize = modeBlockflag[modeNumber] ? blocksize1 : blocksize0;
448
+ if (prevBlocksize !== 0) {
449
+ granule += prevBlocksize + blocksize >> 2;
450
+ }
451
+ prevBlocksize = blocksize;
452
+ if (packet.granule) lastWemGranule = packet.granule;
453
+ writer.setGranule(isLast ? Math.min(granule, layout.sampleCount) : granule);
454
+ writer.flushPage(isLast);
455
+ offset = packet.nextOffset;
456
+ }
457
+ if (offset > dataEnd) throw parseError("page truncated");
458
+ return {
459
+ bytes: writer.toBytes(),
460
+ sampleCount: layout.sampleCount,
461
+ sampleRate: layout.sampleRate,
462
+ channels: layout.channels,
463
+ durationSeconds: layout.sampleRate ? layout.sampleCount / layout.sampleRate : 0,
464
+ loop: layout.loop,
465
+ pageCount: writer.pageCount,
466
+ modPackets: layout.modPackets,
467
+ computedGranule: granule,
468
+ lastWemGranule
469
+ };
470
+ }
471
+ function readU16LE(bytes, offset) {
472
+ return bytes[offset] | bytes[offset + 1] << 8;
473
+ }
474
+ function readU32LE(bytes, offset) {
475
+ return (bytes[offset] | bytes[offset + 1] << 8 | bytes[offset + 2] << 16 | bytes[offset + 3] * 0x1000000) >>> 0;
476
+ }
477
+ function readU16BE(bytes, offset) {
478
+ return bytes[offset] << 8 | bytes[offset + 1];
479
+ }
480
+ function readU32BE(bytes, offset) {
481
+ return bytes[offset] * 0x1000000 + (bytes[offset + 1] << 16) + (bytes[offset + 2] << 8) + bytes[offset + 3] >>> 0;
482
+ }
483
+
484
+ export { convertWemToOgg, parseWemVorbisLayout };
485
+ //# sourceMappingURL=wemToOgg.js.map