@carbonenginejs/runtime-resource 0.8.0 → 0.9.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 (259) hide show
  1. package/LICENSE +21 -21
  2. package/NOTICE +29 -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 +59 -59
  9. package/dist/_virtual/_rollupPluginBabelHelpers.js +153 -153
  10. package/dist/format/CjsBlueReader.js +269 -269
  11. package/dist/format/CjsFormat.js +192 -192
  12. package/dist/format/CjsReader.js +18 -18
  13. package/dist/format/CjsResourceProbe.js +277 -277
  14. package/dist/format/payloadContract.js +173 -173
  15. package/dist/formats/black/CjsBlackFormat.js +278 -278
  16. package/dist/formats/black/core/CjsBlackBinaryReader.js +153 -153
  17. package/dist/formats/black/core/CjsBlackPropertyReaders.js +382 -382
  18. package/dist/formats/black/core/CjsBlackReader.js +639 -639
  19. package/dist/formats/black/core/CjsBlackSchemaRegistry.js +433 -433
  20. package/dist/formats/black/core/black-schema-v1-2026-07-11.json.js +4 -4
  21. package/dist/formats/black/core/blackConstants.js +7 -7
  22. package/dist/formats/black/core/blackDefinitions.js +10 -10
  23. package/dist/formats/black/core/blackEnums.js +6 -6
  24. package/dist/formats/black/core/blackSchema.js +3 -3
  25. package/dist/formats/black/core/blackVersion.js +22 -22
  26. package/dist/formats/black/core/helpers.js +182 -182
  27. package/dist/formats/black/core/schema.js +4 -4
  28. package/dist/formats/black/index.js +2 -2
  29. package/dist/formats/bnk/CjsBnkFormat.js +125 -125
  30. package/dist/formats/bnk/core/graph.js +140 -140
  31. package/dist/formats/bnk/core/helpers.js +409 -409
  32. package/dist/formats/bnk/core/musicNodes.js +489 -489
  33. package/dist/formats/bnk/core/soundbanksInfo.js +246 -246
  34. package/dist/formats/bnk/index.js +2 -2
  35. package/dist/formats/cmf/CjsCmfFormat.js +497 -497
  36. package/dist/formats/cmf/core/binary.js +118 -118
  37. package/dist/formats/cmf/core/buffers.js +233 -233
  38. package/dist/formats/cmf/core/constants.js +47 -47
  39. package/dist/formats/cmf/core/gr2Anim.js +453 -453
  40. package/dist/formats/cmf/core/helpers.js +287 -287
  41. package/dist/formats/cmf/core/pack.js +276 -276
  42. package/dist/formats/cmf/core/schema.js +364 -364
  43. package/dist/formats/cmf/core/shared.js +268 -268
  44. package/dist/formats/cmf/core/writer.js +517 -517
  45. package/dist/formats/cmf/index.js +2 -2
  46. package/dist/formats/dds/CjsDdsFormat.js +193 -193
  47. package/dist/formats/dds/core/bc6h.js +288 -288
  48. package/dist/formats/dds/core/bc7.js +251 -251
  49. package/dist/formats/dds/core/helpers.js +815 -815
  50. package/dist/formats/dds/index.js +2 -2
  51. package/dist/formats/fbx/CjsFbxFormat.js +266 -266
  52. package/dist/formats/fbx/core/helpers.js +3901 -3901
  53. package/dist/formats/fbx/index.js +2 -2
  54. package/dist/formats/flac/CjsFlacFormat.js +87 -87
  55. package/dist/formats/flac/core/helpers.js +295 -295
  56. package/dist/formats/flac/index.js +2 -2
  57. package/dist/formats/gif/CjsGifFormat.js +87 -87
  58. package/dist/formats/gif/core/helpers.js +360 -360
  59. package/dist/formats/gif/index.js +2 -2
  60. package/dist/formats/gltf/CjsGltfFormat.js +290 -290
  61. package/dist/formats/gltf/core/helpers.js +292 -292
  62. package/dist/formats/gltf/core/json.js +79 -79
  63. package/dist/formats/gltf/core/parser.js +666 -666
  64. package/dist/formats/gltf/core/targets.js +163 -163
  65. package/dist/formats/gltf/index.js +2 -2
  66. package/dist/formats/gr2/CjsGr2Format.js +46 -0
  67. package/dist/formats/gr2/CjsGr2Format.js.map +1 -0
  68. package/dist/formats/gr2/core/CjsFormatGr2.js +273 -0
  69. package/dist/formats/gr2/core/CjsFormatGr2.js.map +1 -0
  70. package/dist/formats/gr2/core/bitknit2.js +280 -0
  71. package/dist/formats/gr2/core/bitknit2.js.map +1 -0
  72. package/dist/formats/gr2/core/curves.js +1047 -0
  73. package/dist/formats/gr2/core/curves.js.map +1 -0
  74. package/dist/formats/gr2/core/gsf.js +72 -0
  75. package/dist/formats/gr2/core/gsf.js.map +1 -0
  76. package/dist/formats/gr2/core/helpers.js +332 -0
  77. package/dist/formats/gr2/core/helpers.js.map +1 -0
  78. package/dist/formats/gr2/core/json.js +622 -0
  79. package/dist/formats/gr2/core/json.js.map +1 -0
  80. package/dist/formats/gr2/core/oodle1.js +388 -0
  81. package/dist/formats/gr2/core/oodle1.js.map +1 -0
  82. package/dist/formats/gr2/core/reader.js +617 -0
  83. package/dist/formats/gr2/core/reader.js.map +1 -0
  84. package/dist/formats/gr2/core/tangents.js +48 -0
  85. package/dist/formats/gr2/core/tangents.js.map +1 -0
  86. package/dist/formats/gr2/core/targets.js +351 -0
  87. package/dist/formats/gr2/core/targets.js.map +1 -0
  88. package/dist/formats/gr2/index.js +3 -0
  89. package/dist/formats/gr2/index.js.map +1 -0
  90. package/dist/formats/index.js +30 -23
  91. package/dist/formats/index.js.map +1 -1
  92. package/dist/formats/jpeg/CjsJpegFormat.js +206 -206
  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 -192
  97. package/dist/formats/mp3/core/helpers.js +338 -338
  98. package/dist/formats/mp3/index.js +2 -2
  99. package/dist/formats/mp4/CjsMp4Format.js +191 -191
  100. package/dist/formats/mp4/core/helpers.js +449 -449
  101. package/dist/formats/mp4/index.js +2 -2
  102. package/dist/formats/obj/CjsObjFormat.js +253 -253
  103. package/dist/formats/obj/core/helpers.js +573 -573
  104. package/dist/formats/obj/core/json.js +64 -64
  105. package/dist/formats/obj/core/parser.js +321 -321
  106. package/dist/formats/obj/index.js +2 -2
  107. package/dist/formats/ogg/CjsOggFormat.js +88 -88
  108. package/dist/formats/ogg/core/helpers.js +387 -387
  109. package/dist/formats/ogg/core/imdct.js +178 -178
  110. package/dist/formats/ogg/core/vorbis.js +999 -999
  111. package/dist/formats/ogg/index.js +2 -2
  112. package/dist/formats/png/CjsPngFormat.js +195 -195
  113. package/dist/formats/png/core/helpers.js +583 -583
  114. package/dist/formats/png/index.js +2 -2
  115. package/dist/formats/red/CjsRedFormat.js +261 -261
  116. package/dist/formats/red/core/CjsRedReader.js +194 -194
  117. package/dist/formats/red/core/blackDefinitions.js +3 -3
  118. package/dist/formats/red/core/helpers.js +139 -139
  119. package/dist/formats/red/core/redGraph.js +68 -68
  120. package/dist/formats/red/core/schema.js +4 -4
  121. package/dist/formats/red/index.js +2 -2
  122. package/dist/formats/stl/CjsStlFormat.js +365 -365
  123. package/dist/formats/stl/core/helpers.js +261 -261
  124. package/dist/formats/stl/core/json.js +51 -51
  125. package/dist/formats/stl/core/stl.js +629 -629
  126. package/dist/formats/stl/core/targets.js +163 -163
  127. package/dist/formats/stl/index.js +2 -2
  128. package/dist/formats/tga/CjsTgaFormat.js +192 -192
  129. package/dist/formats/tga/core/helpers.js +446 -446
  130. package/dist/formats/tga/index.js +2 -2
  131. package/dist/formats/wav/CjsWavFormat.js +192 -192
  132. package/dist/formats/wav/core/helpers.js +328 -328
  133. package/dist/formats/wav/index.js +2 -2
  134. package/dist/formats/webm/CjsWebmFormat.js +191 -191
  135. package/dist/formats/webm/core/helpers.js +537 -537
  136. package/dist/formats/webm/index.js +2 -2
  137. package/dist/formats/webp/CjsWebpFormat.js +86 -86
  138. package/dist/formats/webp/core/helpers.js +214 -214
  139. package/dist/formats/webp/index.js +2 -2
  140. package/dist/formats/wem/CjsWemFormat.js +241 -241
  141. package/dist/formats/wem/core/bitStream.js +259 -259
  142. package/dist/formats/wem/core/codebookLibrary.js +164 -164
  143. package/dist/formats/wem/core/helpers.js +417 -417
  144. package/dist/formats/wem/core/packedCodebooksAotuv603.js +30 -30
  145. package/dist/formats/wem/core/ptadpcm.js +77 -77
  146. package/dist/formats/wem/core/resolve.js +121 -121
  147. package/dist/formats/wem/core/wemToOgg.js +485 -485
  148. package/dist/formats/wem/index.js +2 -2
  149. package/dist/formats/yaml/CjsYamlFormat.js +83 -83
  150. package/dist/formats/yaml/core/CjsYamlReader.js +305 -305
  151. package/dist/formats/yaml/core/helpers.js +160 -160
  152. package/dist/formats/yaml/index.js +2 -2
  153. package/dist/index.js +49 -48
  154. package/dist/index.js.map +1 -1
  155. package/dist/resourcePath.js +18 -18
  156. package/dist/resourceStates.js +9 -9
  157. package/dist/resources/AudioGeometryResData.js +47 -47
  158. package/dist/resources/GStateBindingCallbackData.js +31 -31
  159. package/dist/resources/MeshDecalData.js +37 -37
  160. package/dist/resources/MeshDecalLodData.js +34 -34
  161. package/dist/resources/Tr2EffectRes.js +71 -71
  162. package/dist/resources/Tr2GrannyIntersectionResult.js +60 -60
  163. package/dist/resources/Tr2GrannyStateRes.js +44 -44
  164. package/dist/resources/Tr2ImageRes.js +114 -114
  165. package/dist/resources/Tr2LightProfileRes.js +40 -40
  166. package/dist/resources/Tr2MaterialArea.js +34 -34
  167. package/dist/resources/Tr2MaterialMesh.js +31 -31
  168. package/dist/resources/Tr2MaterialRes.js +34 -34
  169. package/dist/resources/Tr2ShaderPermutation.js +43 -43
  170. package/dist/resources/Tr2TextureLodManager.js +80 -80
  171. package/dist/resources/Tr2TextureLodUpdateRequest.js +37 -37
  172. package/dist/resources/Tr2TexturePackChannel.js +37 -37
  173. package/dist/resources/Tr2TexturePipeline.js +52 -52
  174. package/dist/resources/Tr2TexturePipelineParams.js +34 -34
  175. package/dist/resources/Tr2TexturePipelineStepCompress.js +40 -40
  176. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js +22 -0
  177. package/dist/resources/Tr2TexturePipelineStepGenerateMips.js.map +1 -0
  178. package/dist/resources/Tr2TexturePipelineStepLimitSize.js +34 -34
  179. package/dist/resources/Tr2TexturePipelineStepLoad.js +31 -31
  180. package/dist/resources/Tr2TexturePipelineStepPack.js +43 -43
  181. package/dist/resources/TriGeometryRes.js +239 -239
  182. package/dist/resources/TriGeometryResAreaData.js +59 -59
  183. package/dist/resources/TriGeometryResJointData.js +38 -38
  184. package/dist/resources/TriGeometryResLodData.js +88 -88
  185. package/dist/resources/TriGeometryResMeshData.js +63 -63
  186. package/dist/resources/TriGeometryResSkeletonData.js +34 -34
  187. package/dist/resources/TriGrannyRes.js +43 -43
  188. package/dist/resources/TriJointBinding.js +38 -38
  189. package/dist/resources/TriMorphTargetGeometryConstants.js +46 -46
  190. package/dist/resources/TriRtGeometryConstants.js +88 -88
  191. package/dist/resources/TriTextureRes.js +304 -304
  192. package/dist/resources/enums.js +18 -18
  193. package/dist/resources/resourceBoundary.js +47 -47
  194. package/dist/resources/texturePipelineBehavior.js +308 -308
  195. package/dist/texture/CjsTextureArrayRes.js +406 -406
  196. package/dist/texture/CjsTextureParameterProxy.js +133 -133
  197. package/docs/README.md +69 -0
  198. package/docs/architecture.md +86 -0
  199. package/docs/concepts/resource-lifecycle.md +217 -0
  200. package/docs/formats/README.md +104 -0
  201. package/{FORMAT-PROVENANCE.md → docs/formats/provenance.md} +173 -155
  202. package/docs/formats/stl.md +37 -0
  203. package/docs/formats/wwise.md +44 -0
  204. package/docs/reference/events.md +92 -0
  205. package/docs/reference/motherlode-cache.md +244 -0
  206. package/docs/reference/queues.md +102 -0
  207. package/docs/reference/reload.md +107 -0
  208. package/docs/reference/texture-arrays.md +113 -0
  209. package/docs/reference/texture-pipeline.md +53 -0
  210. package/docs/roadmap.md +104 -0
  211. package/format-notices/black/LICENSE +21 -21
  212. package/format-notices/black/NOTICE +47 -47
  213. package/format-notices/bnk/LICENSE +21 -21
  214. package/format-notices/bnk/NOTICE +20 -20
  215. package/format-notices/cmf/LICENSE +21 -21
  216. package/format-notices/cmf/NOTICE +36 -36
  217. package/format-notices/dds/LICENSE +21 -21
  218. package/format-notices/dds/NOTICE +14 -14
  219. package/format-notices/fbx/LICENSE +21 -21
  220. package/format-notices/fbx/NOTICE +14 -14
  221. package/format-notices/flac/LICENSE +21 -21
  222. package/format-notices/flac/NOTICE +14 -14
  223. package/format-notices/gif/LICENSE +21 -21
  224. package/format-notices/gif/NOTICE +14 -14
  225. package/format-notices/gltf/LICENSE +21 -21
  226. package/format-notices/gltf/NOTICE +27 -27
  227. package/format-notices/gr2/LICENSE +21 -0
  228. package/format-notices/gr2/NOTICE +60 -0
  229. package/format-notices/gr2/THIRD-PARTY-NOTICES.md +93 -0
  230. package/format-notices/jpeg/LICENSE +21 -21
  231. package/format-notices/jpeg/NOTICE +14 -14
  232. package/format-notices/mp3/LICENSE +21 -21
  233. package/format-notices/mp3/NOTICE +14 -14
  234. package/format-notices/mp4/LICENSE +21 -21
  235. package/format-notices/mp4/NOTICE +14 -14
  236. package/format-notices/obj/LICENSE +21 -21
  237. package/format-notices/obj/NOTICE +26 -26
  238. package/format-notices/ogg/LICENSE +21 -21
  239. package/format-notices/ogg/NOTICE +28 -28
  240. package/format-notices/png/LICENSE +21 -21
  241. package/format-notices/png/NOTICE +14 -14
  242. package/format-notices/red/LICENSE +21 -21
  243. package/format-notices/red/NOTICE +31 -31
  244. package/format-notices/stl/LICENSE +21 -21
  245. package/format-notices/stl/NOTICE +21 -21
  246. package/format-notices/tga/LICENSE +21 -21
  247. package/format-notices/tga/NOTICE +14 -14
  248. package/format-notices/wav/LICENSE +21 -21
  249. package/format-notices/wav/NOTICE +14 -14
  250. package/format-notices/webm/LICENSE +21 -21
  251. package/format-notices/webm/NOTICE +14 -14
  252. package/format-notices/webp/LICENSE +21 -21
  253. package/format-notices/webp/NOTICE +14 -14
  254. package/format-notices/wem/LICENSE +57 -57
  255. package/format-notices/wem/NOTICE +33 -33
  256. package/format-notices/yaml/LICENSE +21 -21
  257. package/format-notices/yaml/NOTICE +44 -44
  258. package/package.json +52 -51
  259. package/resource-lifecycle.md +0 -679
@@ -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