xstudio-linux 0.0.0-stage → 0.2.4

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 (222) hide show
  1. package/CHANGELOG.md +469 -0
  2. package/LICENSE +31 -0
  3. package/README.md +542 -2
  4. package/dist/adapter-cli/cli.d.ts +28 -0
  5. package/dist/adapter-cli/cli.js +233 -0
  6. package/dist/adapter-cli/cli.js.map +1 -0
  7. package/dist/adapter-mcp/server.d.ts +81 -0
  8. package/dist/adapter-mcp/server.js +473 -0
  9. package/dist/adapter-mcp/server.js.map +1 -0
  10. package/dist/config.d.ts +45 -0
  11. package/dist/config.js +78 -0
  12. package/dist/config.js.map +1 -0
  13. package/dist/core/catalog/pronunciation.d.ts +47 -0
  14. package/dist/core/catalog/pronunciation.js +402 -0
  15. package/dist/core/catalog/pronunciation.js.map +1 -0
  16. package/dist/core/catalog/singers.d.ts +46 -0
  17. package/dist/core/catalog/singers.js +122 -0
  18. package/dist/core/catalog/singers.js.map +1 -0
  19. package/dist/core/contracts.d.ts +348 -0
  20. package/dist/core/contracts.js +17 -0
  21. package/dist/core/contracts.js.map +1 -0
  22. package/dist/core/degrade/chain.d.ts +80 -0
  23. package/dist/core/degrade/chain.js +137 -0
  24. package/dist/core/degrade/chain.js.map +1 -0
  25. package/dist/core/diagnose/error-resolver.d.ts +51 -0
  26. package/dist/core/diagnose/error-resolver.js +202 -0
  27. package/dist/core/diagnose/error-resolver.js.map +1 -0
  28. package/dist/core/guard/skill-gate.d.ts +89 -0
  29. package/dist/core/guard/skill-gate.js +121 -0
  30. package/dist/core/guard/skill-gate.js.map +1 -0
  31. package/dist/core/midi/convert.d.ts +31 -0
  32. package/dist/core/midi/convert.js +125 -0
  33. package/dist/core/midi/convert.js.map +1 -0
  34. package/dist/core/midi/smf.d.ts +37 -0
  35. package/dist/core/midi/smf.js +422 -0
  36. package/dist/core/midi/smf.js.map +1 -0
  37. package/dist/core/render/artifacts.d.ts +37 -0
  38. package/dist/core/render/artifacts.js +417 -0
  39. package/dist/core/render/artifacts.js.map +1 -0
  40. package/dist/core/render/orchestrator.d.ts +110 -0
  41. package/dist/core/render/orchestrator.js +974 -0
  42. package/dist/core/render/orchestrator.js.map +1 -0
  43. package/dist/core/svip/codec.d.ts +30 -0
  44. package/dist/core/svip/codec.js +664 -0
  45. package/dist/core/svip/codec.js.map +1 -0
  46. package/dist/core/svip/document.d.ts +31 -0
  47. package/dist/core/svip/document.js +158 -0
  48. package/dist/core/svip/document.js.map +1 -0
  49. package/dist/core/svip/editor.d.ts +71 -0
  50. package/dist/core/svip/editor.js +245 -0
  51. package/dist/core/svip/editor.js.map +1 -0
  52. package/dist/core/svip/model.d.ts +74 -0
  53. package/dist/core/svip/model.js +158 -0
  54. package/dist/core/svip/model.js.map +1 -0
  55. package/dist/core/svip/tick.d.ts +21 -0
  56. package/dist/core/svip/tick.js +59 -0
  57. package/dist/core/svip/tick.js.map +1 -0
  58. package/dist/core/svip/validator.d.ts +39 -0
  59. package/dist/core/svip/validator.js +267 -0
  60. package/dist/core/svip/validator.js.map +1 -0
  61. package/dist/host/linux.d.ts +15 -0
  62. package/dist/host/linux.js +42 -0
  63. package/dist/host/linux.js.map +1 -0
  64. package/dist/host/sidecar.d.ts +61 -0
  65. package/dist/host/sidecar.js +365 -0
  66. package/dist/host/sidecar.js.map +1 -0
  67. package/dist/host/wine-launch.d.ts +60 -0
  68. package/dist/host/wine-launch.js +77 -0
  69. package/dist/host/wine-launch.js.map +1 -0
  70. package/dist/host/wine-paths.d.ts +30 -0
  71. package/dist/host/wine-paths.js +103 -0
  72. package/dist/host/wine-paths.js.map +1 -0
  73. package/dist/index.d.ts +20 -0
  74. package/dist/index.js +17 -0
  75. package/dist/index.js.map +1 -0
  76. package/dist/services.d.ts +55 -0
  77. package/dist/services.js +78 -0
  78. package/dist/services.js.map +1 -0
  79. package/dist/shared/errors.d.ts +61 -0
  80. package/dist/shared/errors.js +125 -0
  81. package/dist/shared/errors.js.map +1 -0
  82. package/dist/shared/paths.d.ts +8 -0
  83. package/dist/shared/paths.js +30 -0
  84. package/dist/shared/paths.js.map +1 -0
  85. package/dist/shared/schema.d.ts +26 -0
  86. package/dist/shared/schema.js +68 -0
  87. package/dist/shared/schema.js.map +1 -0
  88. package/dist/shared/types.d.ts +43 -0
  89. package/dist/shared/types.js +19 -0
  90. package/dist/shared/types.js.map +1 -0
  91. package/dist/tests/unit/artifacts.test.d.ts +1 -0
  92. package/dist/tests/unit/artifacts.test.js +243 -0
  93. package/dist/tests/unit/artifacts.test.js.map +1 -0
  94. package/dist/tests/unit/data-plane.test.d.ts +1 -0
  95. package/dist/tests/unit/data-plane.test.js +153 -0
  96. package/dist/tests/unit/data-plane.test.js.map +1 -0
  97. package/dist/tests/unit/degrade.test.d.ts +1 -0
  98. package/dist/tests/unit/degrade.test.js +133 -0
  99. package/dist/tests/unit/degrade.test.js.map +1 -0
  100. package/dist/tests/unit/diagnosis.test.d.ts +1 -0
  101. package/dist/tests/unit/diagnosis.test.js +66 -0
  102. package/dist/tests/unit/diagnosis.test.js.map +1 -0
  103. package/dist/tests/unit/errors.test.d.ts +1 -0
  104. package/dist/tests/unit/errors.test.js +83 -0
  105. package/dist/tests/unit/errors.test.js.map +1 -0
  106. package/dist/tests/unit/mcp-server.test.d.ts +1 -0
  107. package/dist/tests/unit/mcp-server.test.js +267 -0
  108. package/dist/tests/unit/mcp-server.test.js.map +1 -0
  109. package/dist/tests/unit/mcp-smoke.d.ts +6 -0
  110. package/dist/tests/unit/mcp-smoke.js +7 -0
  111. package/dist/tests/unit/mcp-smoke.js.map +1 -0
  112. package/dist/tests/unit/mcp-smoke.test.d.ts +1 -0
  113. package/dist/tests/unit/mcp-smoke.test.js +196 -0
  114. package/dist/tests/unit/mcp-smoke.test.js.map +1 -0
  115. package/dist/tests/unit/midi.test.d.ts +1 -0
  116. package/dist/tests/unit/midi.test.js +255 -0
  117. package/dist/tests/unit/midi.test.js.map +1 -0
  118. package/dist/tests/unit/pronunciation.test.d.ts +1 -0
  119. package/dist/tests/unit/pronunciation.test.js +83 -0
  120. package/dist/tests/unit/pronunciation.test.js.map +1 -0
  121. package/dist/tests/unit/registry-contract.test.d.ts +1 -0
  122. package/dist/tests/unit/registry-contract.test.js +114 -0
  123. package/dist/tests/unit/registry-contract.test.js.map +1 -0
  124. package/dist/tests/unit/render-orchestrator.test.d.ts +1 -0
  125. package/dist/tests/unit/render-orchestrator.test.js +548 -0
  126. package/dist/tests/unit/render-orchestrator.test.js.map +1 -0
  127. package/dist/tests/unit/sidecar.test.d.ts +1 -0
  128. package/dist/tests/unit/sidecar.test.js +299 -0
  129. package/dist/tests/unit/sidecar.test.js.map +1 -0
  130. package/dist/tests/unit/singers.test.d.ts +1 -0
  131. package/dist/tests/unit/singers.test.js +73 -0
  132. package/dist/tests/unit/singers.test.js.map +1 -0
  133. package/dist/tests/unit/skill-gate.test.d.ts +1 -0
  134. package/dist/tests/unit/skill-gate.test.js +207 -0
  135. package/dist/tests/unit/skill-gate.test.js.map +1 -0
  136. package/dist/tests/unit/svip-codec.test.d.ts +1 -0
  137. package/dist/tests/unit/svip-codec.test.js +182 -0
  138. package/dist/tests/unit/svip-codec.test.js.map +1 -0
  139. package/dist/tests/unit/svip-document.test.d.ts +1 -0
  140. package/dist/tests/unit/svip-document.test.js +137 -0
  141. package/dist/tests/unit/svip-document.test.js.map +1 -0
  142. package/dist/tests/unit/svip-editor.test.d.ts +1 -0
  143. package/dist/tests/unit/svip-editor.test.js +275 -0
  144. package/dist/tests/unit/svip-editor.test.js.map +1 -0
  145. package/dist/tests/unit/svip-tick.test.d.ts +1 -0
  146. package/dist/tests/unit/svip-tick.test.js +66 -0
  147. package/dist/tests/unit/svip-tick.test.js.map +1 -0
  148. package/dist/tools/common.d.ts +87 -0
  149. package/dist/tools/common.js +174 -0
  150. package/dist/tools/common.js.map +1 -0
  151. package/dist/tools/control.d.ts +21 -0
  152. package/dist/tools/control.js +376 -0
  153. package/dist/tools/control.js.map +1 -0
  154. package/dist/tools/data.d.ts +37 -0
  155. package/dist/tools/data.js +680 -0
  156. package/dist/tools/data.js.map +1 -0
  157. package/dist/tools/definition.d.ts +60 -0
  158. package/dist/tools/definition.js +44 -0
  159. package/dist/tools/definition.js.map +1 -0
  160. package/dist/tools/degrade.d.ts +3 -0
  161. package/dist/tools/degrade.js +30 -0
  162. package/dist/tools/degrade.js.map +1 -0
  163. package/dist/tools/guide.d.ts +10 -0
  164. package/dist/tools/guide.js +66 -0
  165. package/dist/tools/guide.js.map +1 -0
  166. package/dist/tools/open.d.ts +3 -0
  167. package/dist/tools/open.js +74 -0
  168. package/dist/tools/open.js.map +1 -0
  169. package/dist/tools/probe.d.ts +8 -0
  170. package/dist/tools/probe.js +132 -0
  171. package/dist/tools/probe.js.map +1 -0
  172. package/dist/tools/registry.d.ts +10 -0
  173. package/dist/tools/registry.js +16 -0
  174. package/dist/tools/registry.js.map +1 -0
  175. package/dist/version.d.ts +4 -0
  176. package/dist/version.js +19 -0
  177. package/dist/version.js.map +1 -0
  178. package/docs/DESIGN.md +561 -0
  179. package/docs/RECON-GUI.md +271 -0
  180. package/docs/RECON.md +339 -0
  181. package/docs/SPEC.md +786 -0
  182. package/docs/WINE.md +193 -0
  183. package/examples/.scratch/01-midi-to-wudi/wudi-demo.svip3 +0 -0
  184. package/examples/.scratch/01-midi-to-wudi/wudi-melody.mid +0 -0
  185. package/examples/.scratch/01-midi-to-wudi/wudi-roundtrip.mid +0 -0
  186. package/examples/.scratch/02-three-singers/melody.chenziyu.svip3 +0 -0
  187. package/examples/.scratch/02-three-singers/melody.hechang.svip3 +0 -0
  188. package/examples/.scratch/02-three-singers/melody.svip3 +0 -0
  189. package/examples/.scratch/02-three-singers/melody.wudi.svip3 +0 -0
  190. package/examples/.scratch/03-velocity-diction/phrase.svip3 +0 -0
  191. package/examples/.scratch/03-velocity-diction/phrase.svip3.bak.20261206-163755-200 +0 -0
  192. package/examples/.scratch/03-velocity-diction/phrase.svip3.bak.20261206-163755-203 +0 -0
  193. package/examples/.scratch/03-velocity-diction/phrase.svip3.bak.20261206-163755-204 +0 -0
  194. package/examples/.scratch/04-unknown-dialog-abort/dialog-demo.svip3 +0 -0
  195. package/examples/01-midi-to-wudi.mjs +144 -0
  196. package/examples/02-three-singers-compare.mjs +140 -0
  197. package/examples/03-velocity-diction-rerender.mjs +160 -0
  198. package/examples/04-unknown-dialog-abort.mjs +126 -0
  199. package/examples/README.md +101 -0
  200. package/examples/lib/harness.mjs +274 -0
  201. package/fixtures/midi/melody-480.mid +0 -0
  202. package/fixtures/midi/melody-96.mid +0 -0
  203. package/fixtures/svip/1.svip3 +0 -0
  204. package/fixtures/svip/3.svip3 +0 -0
  205. package/fixtures/uia/README.md +37 -0
  206. package/fixtures/uia/export-dialog.snapshot.json +446 -0
  207. package/fixtures/uia/main-window.snapshot.json +2997 -0
  208. package/fixtures/uia/open-dialog.snapshot.json +103 -0
  209. package/icon.svg +10 -0
  210. package/package.json +84 -4
  211. package/proto/xstudio.proto +128 -0
  212. package/scripts/check-contract.mjs +184 -0
  213. package/scripts/entry-smoke.mjs +26 -0
  214. package/scripts/inspect-svip.mjs +161 -0
  215. package/scripts/setup-venv.sh +93 -0
  216. package/scripts/wine-gui-probe.py +302 -0
  217. package/sidecar/xstudio_uia.py +1478 -0
  218. package/skills/xstudio-gui/SKILL.md +196 -0
  219. package/skills/xstudio-gui/assertions.md +103 -0
  220. package/skills/xstudio-gui/dialogs.md +90 -0
  221. package/skills/xstudio-gui/gestures.md +158 -0
  222. package/skills/xstudio-gui/ui-map.json +242 -0
@@ -0,0 +1,664 @@
1
+ /**
2
+ * .svip3 protobuf codec.
3
+ *
4
+ * The schema mirrors `proto/xstudio.proto` (the documentation copy) with
5
+ * `google.protobuf.Any` inlined as a local message so that parsing needs no
6
+ * import resolution or network access.
7
+ *
8
+ * Two protobufjs behaviours drive the design:
9
+ * 1. protobufjs 7.6.6 silently DROPS unknown fields. X Studio may add fields
10
+ * in future versions, so a raw-wire scanner records every unknown field
11
+ * (tag + payload as hex) into `$unknowns` on the model node that owns it,
12
+ * and the custom writer re-emits those bytes verbatim.
13
+ * 2. protobufjs' canonical field order and proto3 default omission match the
14
+ * bytes X Studio writes, so decode -> encode is byte-identical on real
15
+ * project files (see fixtures/svip/*.svip3).
16
+ *
17
+ * Unknown fields are appended at the end of their message on re-encode. The
18
+ * protobuf wire format does not require field order, so this is safe.
19
+ *
20
+ * @module xstudio-linux/core/svip/codec
21
+ */
22
+ import { createRequire } from 'node:module';
23
+ import { ErrorCode, XStudioError } from '../../shared/errors.js';
24
+ import { cloneModel } from './model.js';
25
+ const require = createRequire(import.meta.url);
26
+ // protobufjs is CommonJS; Node's ESM interop only exposes a default export for
27
+ // `module.exports = ...`, so load it through createRequire.
28
+ const protobuf = require('protobufjs');
29
+ /**
30
+ * Runtime schema. Kept textually identical to proto/xstudio.proto except that
31
+ * `google.protobuf.Any` is inlined as `Any` (no import resolution needed).
32
+ */
33
+ const RUNTIME_PROTO = `
34
+ syntax = "proto3";
35
+ package xstudio.proto;
36
+
37
+ message Project {
38
+ string source_path = 1;
39
+ string app_version = 2;
40
+ uint32 internal_clock_base = 3;
41
+ TempoInfo tempo = 4;
42
+ TimeSignature time_sig = 5;
43
+ Any track_data = 6;
44
+ GainInfo gain = 7;
45
+ uint32 unknown_field_9 = 9;
46
+ uint32 total_ticks = 11;
47
+ uint32 flags = 12;
48
+ DisplayMeta display = 14;
49
+ }
50
+
51
+ message TempoInfo {
52
+ uint32 bpm_x100 = 2;
53
+ }
54
+
55
+ message TimeSignature {
56
+ NumeratorDenominator ns = 2;
57
+ }
58
+
59
+ message NumeratorDenominator {
60
+ uint32 numerator = 1;
61
+ uint32 denominator = 2;
62
+ }
63
+
64
+ message GainInfo {
65
+ float linear_gain = 1;
66
+ }
67
+
68
+ message DisplayMeta {
69
+ string placeholder = 2;
70
+ }
71
+
72
+ message Any {
73
+ string type_url = 1;
74
+ bytes value = 2;
75
+ }
76
+
77
+ message SingingTrack {
78
+ float velocity_offset = 1;
79
+ string singer_name = 4;
80
+ string ui_color_hex = 6;
81
+ uint32 track_type = 7;
82
+ TrackPayload payload = 8;
83
+ string uuid = 9;
84
+ float pan_db = 12;
85
+ }
86
+
87
+ message TrackPayload {
88
+ string label = 1;
89
+ uint32 sub_type = 2;
90
+ uint32 start_tick = 4;
91
+ uint32 end_tick = 6;
92
+ repeated Note notes = 8;
93
+ uint64 visibility_mask = 18;
94
+ string short_id = 19;
95
+ }
96
+
97
+ message Note {
98
+ optional uint32 position_tick = 1;
99
+ uint32 length_tick = 2;
100
+ uint32 midi_pitch = 3;
101
+ string lyric_text = 4;
102
+ string phoneme_roman = 5;
103
+ uint32 note_flag = 14;
104
+ float expr_param_1 = 15;
105
+ float expr_param_2 = 16;
106
+ }
107
+ `;
108
+ const root = protobuf.parse(RUNTIME_PROTO).root;
109
+ root.resolveAll();
110
+ const ProjectType = root.lookupType('xstudio.proto.Project');
111
+ const SingingTrackType = root.lookupType('xstudio.proto.SingingTrack');
112
+ /** The singing-track suffix every supported `track_data.type_url` ends with. */
113
+ const SINGING_TRACK_TYPE_SUFFIX = '/xstudio.proto.SingingTrack';
114
+ /**
115
+ * Unknown fields of the nested time-signature numerator/denominator message.
116
+ * The fixed contract has no model slot for that inner message, so they ride on
117
+ * a non-enumerable symbol property of the `timeSig` object; cloneModel and
118
+ * modelsEqual both preserve/compare it.
119
+ */
120
+ const NS_UNKNOWNS = Symbol('xstudio-linux.svip.timeSigNsUnknowns');
121
+ // ---------------------------------------------------------------------------
122
+ // Hex helpers
123
+ // ---------------------------------------------------------------------------
124
+ /** Encode bytes as lower-case hex (used for {@link UnknownField.raw}). */
125
+ export function bytesToHex(bytes) {
126
+ let out = '';
127
+ for (const byte of bytes)
128
+ out += byte.toString(16).padStart(2, '0');
129
+ return out;
130
+ }
131
+ /** Decode lower-case/upper-case hex into bytes. */
132
+ export function hexToBytes(hex) {
133
+ if (hex.length % 2 !== 0) {
134
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, `hex string has an odd length: ${hex.length}`);
135
+ }
136
+ if (!/^[0-9a-fA-F]*$/.test(hex)) {
137
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, 'hex string contains non-hex characters');
138
+ }
139
+ const out = new Uint8Array(hex.length / 2);
140
+ for (let index = 0; index < out.length; index += 1) {
141
+ out[index] = Number.parseInt(hex.slice(index * 2, index * 2 + 2), 16);
142
+ }
143
+ return out;
144
+ }
145
+ // ---------------------------------------------------------------------------
146
+ // Decode
147
+ // ---------------------------------------------------------------------------
148
+ /**
149
+ * Decode a .svip3 project.
150
+ *
151
+ * @throws {XStudioError} PROJECT_DECODE_FAILED when the bytes are not a valid
152
+ * project message.
153
+ */
154
+ export function decodeProject(bytes) {
155
+ let plain;
156
+ let scan;
157
+ try {
158
+ const message = ProjectType.decode(bytes);
159
+ plain = toPlain(ProjectType, message);
160
+ scan = scanMessage(bytes, ProjectType);
161
+ }
162
+ catch (error) {
163
+ throw new XStudioError(ErrorCode.PROJECT_DECODE_FAILED, `cannot decode .svip3 project: ${errorMessage(error)}`, {
164
+ cause: error,
165
+ });
166
+ }
167
+ return projectFromPlain(plain, scan);
168
+ }
169
+ /** Decode a `SingingTrack` payload (exported for tests and diagnostics). */
170
+ export function decodeSingingTrack(bytes) {
171
+ const message = SingingTrackType.decode(bytes);
172
+ return singingFromPlain(toPlain(SingingTrackType, message), scanMessage(bytes, SingingTrackType));
173
+ }
174
+ function projectFromPlain(plain, scan) {
175
+ const tempoPlain = asRecord(plain.tempo);
176
+ const tempo = { bpmX100: asNumber(tempoPlain.bpmX100) };
177
+ attachUnknowns(tempo, childScan(scan, 'tempo')?.unknowns);
178
+ const timeSigPlain = asRecord(plain.timeSig);
179
+ const nsPlain = asRecord(timeSigPlain.ns);
180
+ const timeSig = {
181
+ numerator: asNumber(nsPlain.numerator),
182
+ denominator: asNumber(nsPlain.denominator),
183
+ };
184
+ const timeSigScan = childScan(scan, 'timeSig');
185
+ attachUnknowns(timeSig, timeSigScan?.unknowns);
186
+ const nsUnknowns = childScan(timeSigScan, 'ns')?.unknowns;
187
+ if (nsUnknowns !== undefined && nsUnknowns.length > 0) {
188
+ Object.defineProperty(timeSig, NS_UNKNOWNS, {
189
+ value: nsUnknowns,
190
+ enumerable: false,
191
+ writable: true,
192
+ configurable: true,
193
+ });
194
+ }
195
+ const gainPlain = asRecord(plain.gain);
196
+ const gain = { linearGain: asNumber(gainPlain.linearGain) };
197
+ attachUnknowns(gain, childScan(scan, 'gain')?.unknowns);
198
+ const displayPlain = asRecord(plain.display);
199
+ const display = { placeholder: asString(displayPlain.placeholder) };
200
+ attachUnknowns(display, childScan(scan, 'display')?.unknowns);
201
+ const trackDataPlain = asRecord(plain.trackData);
202
+ const typeUrl = asString(trackDataPlain.typeUrl);
203
+ const value = asBytes(trackDataPlain.value);
204
+ const singing = decodeSinging(typeUrl, value);
205
+ const trackData = { typeUrl, singing };
206
+ if (singing === null && value !== undefined)
207
+ trackData.rawValue = value;
208
+ attachUnknowns(trackData, childScan(scan, 'trackData')?.unknowns);
209
+ const project = {
210
+ sourcePath: asString(plain.sourcePath),
211
+ appVersion: asString(plain.appVersion),
212
+ internalClockBase: asNumber(plain.internalClockBase),
213
+ tempo,
214
+ timeSig,
215
+ trackData,
216
+ gain,
217
+ unknownField9: asNumber(plain.unknownField_9),
218
+ totalTicks: asNumber(plain.totalTicks),
219
+ flags: asNumber(plain.flags),
220
+ display,
221
+ };
222
+ attachUnknowns(project, scan.unknowns);
223
+ return project;
224
+ }
225
+ function decodeSinging(typeUrl, value) {
226
+ if (value === undefined)
227
+ return null;
228
+ if (!isSingingTypeUrl(typeUrl))
229
+ return null;
230
+ return decodeSingingTrack(value);
231
+ }
232
+ function isSingingTypeUrl(typeUrl) {
233
+ if (typeUrl === '')
234
+ return false;
235
+ return typeUrl.endsWith(SINGING_TRACK_TYPE_SUFFIX);
236
+ }
237
+ function singingFromPlain(plain, scan) {
238
+ const payloadPlain = asRecord(plain.payload);
239
+ const payloadScan = childScan(scan, 'payload');
240
+ const notePlains = asArray(payloadPlain.notes);
241
+ // Note scans are children of the *payload* message scan, not of the track.
242
+ const noteScans = payloadScan?.children.get('notes') ?? [];
243
+ const notes = notePlains.map((notePlain, index) => {
244
+ const note = asRecord(notePlain);
245
+ const model = {
246
+ positionTick: asNumber(note.positionTick),
247
+ lengthTick: asNumber(note.lengthTick),
248
+ midiPitch: asNumber(note.midiPitch),
249
+ lyricText: asString(note.lyricText),
250
+ phonemeRoman: asString(note.phonemeRoman),
251
+ noteFlag: asNumber(note.noteFlag),
252
+ exprParam1: asNumber(note.exprParam_1),
253
+ exprParam2: asNumber(note.exprParam_2),
254
+ };
255
+ attachUnknowns(model, noteScans[index]?.unknowns);
256
+ return model;
257
+ });
258
+ const payload = {
259
+ label: asString(payloadPlain.label),
260
+ subType: asNumber(payloadPlain.subType),
261
+ startTick: asNumber(payloadPlain.startTick),
262
+ endTick: asNumber(payloadPlain.endTick),
263
+ notes,
264
+ visibilityMask: asDecimalString(payloadPlain.visibilityMask),
265
+ shortId: asString(payloadPlain.shortId),
266
+ };
267
+ attachUnknowns(payload, payloadScan?.unknowns);
268
+ const track = {
269
+ velocityOffset: asNumber(plain.velocityOffset),
270
+ singerName: asString(plain.singerName),
271
+ uiColorHex: asString(plain.uiColorHex),
272
+ trackType: asNumber(plain.trackType),
273
+ payload,
274
+ uuid: asString(plain.uuid),
275
+ panDb: asNumber(plain.panDb),
276
+ };
277
+ attachUnknowns(track, scan.unknowns);
278
+ return track;
279
+ }
280
+ // ---------------------------------------------------------------------------
281
+ // Encode
282
+ // ---------------------------------------------------------------------------
283
+ /**
284
+ * Encode a project back to .svip3 bytes.
285
+ *
286
+ * Field order and proto3 default omission match protobufjs' canonical encoder,
287
+ * which reproduces real X Studio files byte-for-byte. `$unknowns` are appended
288
+ * to the end of their owning message.
289
+ *
290
+ * @throws {XStudioError} PROJECT_INVALID when the model holds values the wire
291
+ * format cannot represent.
292
+ */
293
+ export function encodeProject(model) {
294
+ try {
295
+ return encodeProjectUnsafe(model);
296
+ }
297
+ catch (error) {
298
+ if (error instanceof XStudioError)
299
+ throw error;
300
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, `cannot encode .svip3 project: ${errorMessage(error)}`, {
301
+ cause: error,
302
+ });
303
+ }
304
+ }
305
+ function encodeProjectUnsafe(model) {
306
+ const writer = protobuf.Writer.create();
307
+ if (model.sourcePath)
308
+ writer.uint32(0x0a).string(model.sourcePath);
309
+ if (model.appVersion)
310
+ writer.uint32(0x12).string(model.appVersion);
311
+ if (model.internalClockBase !== 0)
312
+ writer.uint32(0x18).uint32(uint32(model.internalClockBase, 'internal_clock_base'));
313
+ writer.uint32(0x22).bytes(encodeTempo(model.tempo));
314
+ writer.uint32(0x2a).bytes(encodeTimeSignature(model.timeSig));
315
+ writer.uint32(0x32).bytes(encodeTrackData(model.trackData));
316
+ writer.uint32(0x3a).bytes(encodeGain(model.gain));
317
+ if (model.unknownField9 !== 0)
318
+ writer.uint32(0x48).uint32(uint32(model.unknownField9, 'unknown_field_9'));
319
+ if (model.totalTicks !== 0)
320
+ writer.uint32(0x58).uint32(uint32(model.totalTicks, 'total_ticks'));
321
+ if (model.flags !== 0)
322
+ writer.uint32(0x60).uint32(uint32(model.flags, 'flags'));
323
+ writer.uint32(0x72).bytes(encodeDisplay(model.display));
324
+ return withUnknowns(writer.finish(), model.$unknowns);
325
+ }
326
+ function encodeTempo(tempo) {
327
+ const writer = protobuf.Writer.create();
328
+ if (tempo.bpmX100 !== 0)
329
+ writer.uint32(0x10).uint32(uint32(tempo.bpmX100, 'tempo.bpm_x100'));
330
+ return withUnknowns(writer.finish(), tempo.$unknowns);
331
+ }
332
+ function encodeTimeSignature(timeSig) {
333
+ const nsWriter = protobuf.Writer.create();
334
+ if (timeSig.numerator !== 0)
335
+ nsWriter.uint32(0x08).uint32(uint32(timeSig.numerator, 'time_sig.numerator'));
336
+ if (timeSig.denominator !== 0)
337
+ nsWriter.uint32(0x10).uint32(uint32(timeSig.denominator, 'time_sig.denominator'));
338
+ const nsUnknowns = timeSig[NS_UNKNOWNS];
339
+ const nsBytes = withUnknowns(nsWriter.finish(), nsUnknowns);
340
+ const writer = protobuf.Writer.create();
341
+ writer.uint32(0x12).bytes(nsBytes);
342
+ return withUnknowns(writer.finish(), timeSig.$unknowns);
343
+ }
344
+ function encodeGain(gain) {
345
+ const writer = protobuf.Writer.create();
346
+ if (gain.linearGain !== 0)
347
+ writer.uint32(0x0d).float(float(gain.linearGain, 'gain.linear_gain'));
348
+ return withUnknowns(writer.finish(), gain.$unknowns);
349
+ }
350
+ function encodeDisplay(display) {
351
+ const writer = protobuf.Writer.create();
352
+ if (display.placeholder)
353
+ writer.uint32(0x12).string(display.placeholder);
354
+ return withUnknowns(writer.finish(), display.$unknowns);
355
+ }
356
+ function encodeTrackData(trackData) {
357
+ const writer = protobuf.Writer.create();
358
+ if (trackData.typeUrl)
359
+ writer.uint32(0x0a).string(trackData.typeUrl);
360
+ const value = trackData.singing !== null
361
+ ? encodeSingingTrack(trackData.singing)
362
+ : (trackData.rawValue !== undefined ? new Uint8Array(trackData.rawValue) : new Uint8Array(0));
363
+ writer.uint32(0x12).bytes(value);
364
+ return withUnknowns(writer.finish(), trackData.$unknowns);
365
+ }
366
+ function encodeSingingTrack(track) {
367
+ const writer = protobuf.Writer.create();
368
+ if (track.velocityOffset !== 0)
369
+ writer.uint32(0x0d).float(float(track.velocityOffset, 'velocity_offset'));
370
+ if (track.singerName)
371
+ writer.uint32(0x22).string(track.singerName);
372
+ if (track.uiColorHex)
373
+ writer.uint32(0x32).string(track.uiColorHex);
374
+ if (track.trackType !== 0)
375
+ writer.uint32(0x38).uint32(uint32(track.trackType, 'track_type'));
376
+ writer.uint32(0x42).bytes(encodePayload(track.payload));
377
+ if (track.uuid)
378
+ writer.uint32(0x4a).string(track.uuid);
379
+ if (track.panDb !== 0)
380
+ writer.uint32(0x65).float(float(track.panDb, 'pan_db'));
381
+ return withUnknowns(writer.finish(), track.$unknowns);
382
+ }
383
+ function encodePayload(payload) {
384
+ const writer = protobuf.Writer.create();
385
+ if (payload.label)
386
+ writer.uint32(0x0a).string(payload.label);
387
+ if (payload.subType !== 0)
388
+ writer.uint32(0x10).uint32(uint32(payload.subType, 'sub_type'));
389
+ if (payload.startTick !== 0)
390
+ writer.uint32(0x20).uint32(uint32(payload.startTick, 'start_tick'));
391
+ if (payload.endTick !== 0)
392
+ writer.uint32(0x30).uint32(uint32(payload.endTick, 'end_tick'));
393
+ for (const note of payload.notes)
394
+ writer.uint32(0x42).bytes(encodeNote(note));
395
+ const visibilityMask = encodeVisibilityMask(payload.visibilityMask);
396
+ if (visibilityMask !== undefined)
397
+ writer.uint32(0x90).uint64(visibilityMask);
398
+ if (payload.shortId)
399
+ writer.uint32(0x9a).string(payload.shortId);
400
+ return withUnknowns(writer.finish(), payload.$unknowns);
401
+ }
402
+ function encodeNote(note) {
403
+ const writer = protobuf.Writer.create();
404
+ // `position_tick` is proto3 optional but the model has no presence flag; X
405
+ // Studio's canonical form omits the field when it is 0, which is exactly
406
+ // what proto3 implicit presence does.
407
+ if (note.positionTick !== 0)
408
+ writer.uint32(0x08).uint32(uint32(note.positionTick, 'note.position_tick'));
409
+ if (note.lengthTick !== 0)
410
+ writer.uint32(0x10).uint32(uint32(note.lengthTick, 'note.length_tick'));
411
+ if (note.midiPitch !== 0)
412
+ writer.uint32(0x18).uint32(uint32(note.midiPitch, 'note.midi_pitch'));
413
+ if (note.lyricText)
414
+ writer.uint32(0x22).string(note.lyricText);
415
+ if (note.phonemeRoman)
416
+ writer.uint32(0x2a).string(note.phonemeRoman);
417
+ if (note.noteFlag !== 0)
418
+ writer.uint32(0x70).uint32(uint32(note.noteFlag, 'note.note_flag'));
419
+ if (note.exprParam1 !== 0)
420
+ writer.uint32(0x7d).float(float(note.exprParam1, 'note.expr_param_1'));
421
+ if (note.exprParam2 !== 0)
422
+ writer.uint32(0x85).float(float(note.exprParam2, 'note.expr_param_2'));
423
+ return withUnknowns(writer.finish(), note.$unknowns);
424
+ }
425
+ function encodeVisibilityMask(mask) {
426
+ const raw = (mask ?? '').trim();
427
+ if (raw === '' || raw === '0')
428
+ return undefined;
429
+ if (!/^\d+$/.test(raw)) {
430
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, `visibility_mask must be a decimal string, got ${JSON.stringify(mask)}`);
431
+ }
432
+ const value = BigInt(raw);
433
+ if (value < 0n || value > 0xffffffffffffffffn) {
434
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, `visibility_mask is outside the uint64 range: ${raw}`);
435
+ }
436
+ return value.toString();
437
+ }
438
+ // ---------------------------------------------------------------------------
439
+ // Model equality
440
+ // ---------------------------------------------------------------------------
441
+ /** Structural equality used to decide whether a document actually changed. */
442
+ export function modelsEqual(a, b) {
443
+ return deepEqual(a, b);
444
+ }
445
+ function deepEqual(a, b) {
446
+ if (a === b)
447
+ return true;
448
+ if (typeof a === 'number' && typeof b === 'number') {
449
+ return Number.isNaN(a) && Number.isNaN(b);
450
+ }
451
+ if (a === null || b === null || typeof a !== 'object' || typeof b !== 'object')
452
+ return false;
453
+ if (a instanceof Uint8Array || b instanceof Uint8Array) {
454
+ if (!(a instanceof Uint8Array) || !(b instanceof Uint8Array))
455
+ return false;
456
+ if (a.length !== b.length)
457
+ return false;
458
+ for (let index = 0; index < a.length; index += 1) {
459
+ if (a[index] !== b[index])
460
+ return false;
461
+ }
462
+ return true;
463
+ }
464
+ if (Array.isArray(a) || Array.isArray(b)) {
465
+ if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length)
466
+ return false;
467
+ for (let index = 0; index < a.length; index += 1) {
468
+ if (!deepEqual(a[index], b[index]))
469
+ return false;
470
+ }
471
+ return true;
472
+ }
473
+ const aRecord = a;
474
+ const bRecord = b;
475
+ const aKeys = definedKeys(aRecord);
476
+ const bKeys = definedKeys(bRecord);
477
+ if (aKeys.length !== bKeys.length)
478
+ return false;
479
+ for (const key of aKeys) {
480
+ if (!bKeys.includes(key))
481
+ return false;
482
+ if (!deepEqual(aRecord[key], bRecord[key]))
483
+ return false;
484
+ }
485
+ return true;
486
+ }
487
+ function definedKeys(record) {
488
+ return Reflect.ownKeys(record).filter((key) => record[key] !== undefined);
489
+ }
490
+ // ---------------------------------------------------------------------------
491
+ // Raw-wire scanner (unknown-field preservation)
492
+ // ---------------------------------------------------------------------------
493
+ function scanMessage(bytes, type) {
494
+ const node = { unknowns: [], children: new Map() };
495
+ const fieldsById = new Map();
496
+ for (const field of type.fieldsArray)
497
+ fieldsById.set(field.id, field);
498
+ let offset = 0;
499
+ while (offset < bytes.length) {
500
+ const start = offset;
501
+ const tag = readVarint(bytes, offset);
502
+ offset = tag.next;
503
+ if (tag.value === 0)
504
+ throw new Error('invalid protobuf tag 0');
505
+ const fieldNo = Math.floor(tag.value / 8);
506
+ const wireType = tag.value % 8;
507
+ let payloadStart = -1;
508
+ let payloadEnd = -1;
509
+ if (wireType === 0) {
510
+ offset = readVarint(bytes, offset).next;
511
+ }
512
+ else if (wireType === 1) {
513
+ offset += 8;
514
+ if (offset > bytes.length)
515
+ throw new Error('truncated fixed64 field');
516
+ }
517
+ else if (wireType === 2) {
518
+ const length = readVarint(bytes, offset);
519
+ offset = length.next;
520
+ payloadStart = offset;
521
+ offset += length.value;
522
+ payloadEnd = offset;
523
+ if (offset > bytes.length)
524
+ throw new Error('truncated length-delimited field');
525
+ }
526
+ else if (wireType === 5) {
527
+ offset += 4;
528
+ if (offset > bytes.length)
529
+ throw new Error('truncated fixed32 field');
530
+ }
531
+ else {
532
+ throw new Error(`unsupported protobuf wire type ${wireType} for field ${fieldNo}`);
533
+ }
534
+ const field = fieldsById.get(fieldNo);
535
+ const known = field !== undefined && wireTypeOf(field) === wireType;
536
+ if (known && field.resolvedType instanceof protobuf.Type) {
537
+ const child = scanMessage(bytes.subarray(payloadStart, payloadEnd), field.resolvedType);
538
+ const siblings = node.children.get(field.name) ?? [];
539
+ siblings.push(child);
540
+ node.children.set(field.name, siblings);
541
+ }
542
+ else if (!known) {
543
+ node.unknowns.push({ no: fieldNo, wireType, raw: bytesToHex(bytes.subarray(start, offset)) });
544
+ }
545
+ }
546
+ return node;
547
+ }
548
+ function wireTypeOf(field) {
549
+ if (field.resolvedType instanceof protobuf.Type)
550
+ return 2;
551
+ switch (field.type) {
552
+ case 'float':
553
+ case 'fixed32':
554
+ case 'sfixed32':
555
+ return 5;
556
+ case 'double':
557
+ case 'fixed64':
558
+ case 'sfixed64':
559
+ return 1;
560
+ case 'string':
561
+ case 'bytes':
562
+ return 2;
563
+ case 'group':
564
+ return 3;
565
+ default:
566
+ return 0;
567
+ }
568
+ }
569
+ function readVarint(bytes, offset) {
570
+ let value = 0;
571
+ let multiplier = 1;
572
+ let index = offset;
573
+ for (let count = 0; count < 10; count += 1) {
574
+ if (index >= bytes.length)
575
+ throw new Error('truncated varint');
576
+ const byte = bytes[index];
577
+ index += 1;
578
+ value += (byte & 0x7f) * multiplier;
579
+ if ((byte & 0x80) === 0)
580
+ return { value, next: index };
581
+ multiplier *= 128;
582
+ }
583
+ throw new Error('varint exceeds 10 bytes');
584
+ }
585
+ // ---------------------------------------------------------------------------
586
+ // Small helpers
587
+ // ---------------------------------------------------------------------------
588
+ function toPlain(type, message) {
589
+ return type.toObject(message, {
590
+ longs: String,
591
+ bytes: Uint8Array,
592
+ defaults: false,
593
+ arrays: true,
594
+ objects: true,
595
+ });
596
+ }
597
+ function childScan(node, fieldName) {
598
+ return node?.children.get(fieldName)?.[0];
599
+ }
600
+ function attachUnknowns(target, unknowns) {
601
+ if (unknowns !== undefined && unknowns.length > 0)
602
+ target.$unknowns = unknowns;
603
+ }
604
+ function withUnknowns(known, unknowns) {
605
+ if (unknowns === undefined || unknowns.length === 0)
606
+ return known;
607
+ const chunks = [known];
608
+ for (const unknown of unknowns)
609
+ chunks.push(hexToBytes(unknown.raw));
610
+ let total = 0;
611
+ for (const chunk of chunks)
612
+ total += chunk.length;
613
+ const out = new Uint8Array(total);
614
+ let cursor = 0;
615
+ for (const chunk of chunks) {
616
+ out.set(chunk, cursor);
617
+ cursor += chunk.length;
618
+ }
619
+ return out;
620
+ }
621
+ function asRecord(value) {
622
+ return value !== null && typeof value === 'object' ? value : {};
623
+ }
624
+ function asArray(value) {
625
+ return Array.isArray(value) ? value : [];
626
+ }
627
+ function asNumber(value) {
628
+ return typeof value === 'number' && Number.isFinite(value) ? value : 0;
629
+ }
630
+ function asString(value) {
631
+ return typeof value === 'string' ? value : '';
632
+ }
633
+ function asBytes(value) {
634
+ if (value instanceof Uint8Array)
635
+ return new Uint8Array(value);
636
+ return undefined;
637
+ }
638
+ function asDecimalString(value) {
639
+ if (typeof value === 'string')
640
+ return value;
641
+ if (typeof value === 'number' && Number.isFinite(value))
642
+ return BigInt(Math.trunc(value)).toString();
643
+ if (typeof value === 'bigint')
644
+ return value.toString();
645
+ return '0';
646
+ }
647
+ function uint32(value, label) {
648
+ if (!Number.isInteger(value) || value < 0 || value > 0xffffffff) {
649
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, `${label} must be an unsigned 32-bit integer, got ${String(value)}`);
650
+ }
651
+ return value;
652
+ }
653
+ function float(value, label) {
654
+ if (!Number.isFinite(value)) {
655
+ throw new XStudioError(ErrorCode.PROJECT_INVALID, `${label} must be a finite number, got ${String(value)}`);
656
+ }
657
+ return value;
658
+ }
659
+ function errorMessage(error) {
660
+ return error instanceof Error ? error.message : String(error);
661
+ }
662
+ /** Deep-clone helper re-exported so callers do not need a second import. */
663
+ export { cloneModel };
664
+ //# sourceMappingURL=codec.js.map