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