@threenative/core 0.3.6 → 0.3.9
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.
- package/README.md +3 -5
- package/capabilities.json +739 -50
- package/dist/{assets-CqvE429w.d.ts → assets-YUbXa3-Y.d.ts} +101 -2
- package/dist/{canvas-layer-C1-5CviZ.d.ts → canvas-layer-vCtanIeB.d.ts} +1 -1
- package/dist/{game-Jz4Fdoeb.d.ts → game-cb99J-YE.d.ts} +100 -31
- package/dist/hot.d.ts +4 -4
- package/dist/hot.js +23 -5
- package/dist/index.d.ts +1742 -29
- package/dist/index.js +22443 -12423
- package/dist/{model-lod-BDAUYgsS.d.ts → model-lod-CTpZ6imz.d.ts} +1 -1
- package/dist/net.js +39 -12
- package/dist/playtest.d.ts +4 -4
- package/dist/playtest.js +48 -9
- package/dist/react-css.js +23 -0
- package/dist/react.d.ts +2 -2
- package/dist/{renderer-9yEo9hPa.d.ts → renderer-rZm0hVqq.d.ts} +178 -5
- package/dist/ui-layer.d.ts +11 -2
- package/dist/ui-layer.js +76 -4
- package/dist/worker.d.ts +36 -0
- package/dist/worker.js +1088 -0
- package/dist/world.d.ts +11 -3
- package/dist/world.js +1117 -197
- package/mcp/animation-server.mjs +265 -0
- package/mcp/animation.mjs +22 -0
- package/mcp/call.mjs +105 -0
- package/mcp/devtools.mjs +5 -0
- package/mcp/engine-server.mjs +713 -37
- package/mcp/launch.mjs +3 -2
- package/mcp/servers.d.mts +2 -0
- package/mcp/servers.mjs +33 -8
- package/package.json +12 -8
- package/patches/three@0.185.1-prd269-upgrade.patch +308 -0
- package/patches/three@0.185.1.patch +481 -272
- package/src/animation-packet-state.ts +88 -0
- package/src/animation-pose-packet.ts +537 -0
- package/src/animation.ts +779 -31
- package/src/asset-cache.ts +719 -0
- package/src/assets.ts +521 -152
- package/src/audio.ts +29 -3
- package/src/auto-batch.ts +792 -0
- package/src/config.ts +2 -0
- package/src/frame-budget.ts +268 -2
- package/src/game.ts +525 -105
- package/src/grounding.ts +7 -6
- package/src/hot.ts +19 -5
- package/src/index.ts +381 -6
- package/src/input.ts +21 -2
- package/src/interpolation.ts +19 -1
- package/src/launch-diagnostics.ts +7 -2
- package/src/look/ao-multibounce.ts +29 -0
- package/src/look/ao-reach.ts +39 -0
- package/src/look/auto-exposure.ts +264 -0
- package/src/look/default-tone-mapping.ts +44 -0
- package/src/look/exposure-graph.ts +208 -0
- package/src/look/exposure-readback.ts +97 -0
- package/src/look/exposure-settings.ts +132 -0
- package/src/look/filmic-tone-mapping.ts +852 -0
- package/src/look/gi-registry.ts +55 -0
- package/src/look/local-exposure.ts +507 -0
- package/src/look/look-diagnostics.ts +24 -0
- package/src/look/motion-blur.ts +42 -0
- package/src/look/quality.ts +309 -0
- package/src/look/reflection-trace.ts +344 -0
- package/src/look/unreal-bloom.ts +295 -0
- package/src/look/vignette.ts +14 -0
- package/src/look/world-environment.ts +2033 -0
- package/src/loop.ts +25 -1
- package/src/merge-parts.ts +25 -10
- package/src/model-lod.ts +14 -0
- package/src/net-session.ts +58 -14
- package/src/ocean/spectral.ts +41 -7
- package/src/picking.ts +72 -5
- package/src/platform.ts +29 -1
- package/src/playtest.ts +54 -8
- package/src/profiling/FrameCounters.ts +166 -5
- package/src/profiling/FrameStages.ts +289 -0
- package/src/profiling/Spans.ts +39 -25
- package/src/profiling/debug-telemetry.ts +312 -0
- package/src/profiling/scene-warning.ts +42 -6
- package/src/projection-apply.ts +11 -3
- package/src/projection-content.ts +189 -0
- package/src/projection-marker.ts +6 -2
- package/src/projection-plan.ts +279 -28
- package/src/projection-skinned.ts +620 -5
- package/src/projection-stability.ts +16 -0
- package/src/projection-uniform.ts +5 -1
- package/src/quiet-markers.ts +56 -0
- package/src/render/budget-ladder.ts +256 -0
- package/src/render/chain.ts +89 -7
- package/src/render/exposure-units.ts +59 -0
- package/src/render/frame-time-budget.ts +162 -0
- package/src/render/gi-budget.ts +231 -0
- package/src/render/gi-visualize.ts +49 -0
- package/src/render/material-key.ts +1 -0
- package/src/render/probe-volume.ts +196 -36
- package/src/render/rough-diffuse.ts +177 -0
- package/src/render/scene-trace.ts +532 -0
- package/src/render/shadow-filter-budget.ts +237 -0
- package/src/render/sky-capture.ts +194 -0
- package/src/render/soft-shadow.ts +607 -0
- package/src/render/surface-cache.ts +418 -0
- package/src/render/traced-reflections.ts +408 -0
- package/src/render/two-sided-foliage.ts +120 -0
- package/src/render/virtual-shadow-pages.ts +112 -12
- package/src/render/virtual-shadow.ts +386 -38
- package/src/render-camera-cull.ts +536 -31
- package/src/render-pass-budget.ts +140 -22
- package/src/renderProjection.ts +65 -9
- package/src/renderer-config.ts +3 -2
- package/src/renderer.ts +289 -14
- package/src/resolution-scaler.ts +65 -7
- package/src/skeletal-mesh.ts +4 -0
- package/src/snow-field.ts +46 -0
- package/src/softbody.ts +6 -5
- package/src/startup-offscreen-bytes.ts +168 -0
- package/src/startup-readiness.ts +137 -2
- package/src/ui-bridge.ts +38 -0
- package/src/ui-hit-regions.ts +6 -2
- package/src/ui-layer.ts +1 -0
- package/src/ui-state.ts +32 -3
- package/src/version.ts +1 -1
- package/src/viewport.ts +8 -5
- package/src/warmup.ts +76 -10
- package/src/worker-audio.ts +579 -0
- package/src/worker.ts +842 -0
- package/src/world-cells.ts +4 -1
- package/gpl/LICENSE.GPL +0 -117
- package/gpl/convert.py +0 -192
- package/gpl/fixtures/make_world_fixture.py +0 -184
- package/gpl/recipes/_common.py +0 -180
- package/gpl/recipes/bake_ao.py +0 -111
- package/gpl/recipes/decimate.py +0 -70
- package/gpl/recipes/export_world.py +0 -699
- package/gpl/recipes/retarget.py +0 -131
- package/gpl/recipes/unwrap.py +0 -71
- package/mcp/blender-server.mjs +0 -689
- package/mcp/blender.mjs +0 -26
|
@@ -0,0 +1,579 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A stand-in `AudioContext` for a game running in a dedicated worker, where no Web Audio API
|
|
3
|
+
* exists (PRD-752). Every node is a handle; each call and property write crosses a `MessagePort`
|
|
4
|
+
* and runs on the page's real graph, so three's audio classes and `AudioBus` work unchanged.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
interface IRemoteAudioInit {
|
|
8
|
+
readonly sampleRate: number;
|
|
9
|
+
readonly currentTime: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
interface IClockReply {
|
|
13
|
+
readonly op: "clock";
|
|
14
|
+
readonly currentTime: number;
|
|
15
|
+
readonly state: AudioContextState;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface IGetOp {
|
|
19
|
+
readonly op: "get";
|
|
20
|
+
readonly id: number;
|
|
21
|
+
readonly key: string;
|
|
22
|
+
readonly out: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface ICallOp {
|
|
26
|
+
readonly op: "call";
|
|
27
|
+
readonly id: number;
|
|
28
|
+
readonly method: string;
|
|
29
|
+
readonly args: readonly unknown[];
|
|
30
|
+
readonly out: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ISetOp {
|
|
34
|
+
readonly op: "set";
|
|
35
|
+
readonly id: number;
|
|
36
|
+
readonly key: string;
|
|
37
|
+
readonly value: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface IDecodeOp {
|
|
41
|
+
readonly op: "decode";
|
|
42
|
+
readonly req: number;
|
|
43
|
+
readonly out: number;
|
|
44
|
+
readonly bytes: ArrayBuffer;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface IFireOp {
|
|
48
|
+
readonly op: "fire";
|
|
49
|
+
readonly fn: number;
|
|
50
|
+
readonly once?: boolean;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
interface IControlOp {
|
|
54
|
+
readonly op: "control";
|
|
55
|
+
readonly method: "resume" | "suspend" | "close";
|
|
56
|
+
readonly req: number;
|
|
57
|
+
}
|
|
58
|
+
interface IControlReply {
|
|
59
|
+
readonly op: "controlled";
|
|
60
|
+
readonly req: number;
|
|
61
|
+
readonly currentTime: number;
|
|
62
|
+
readonly state: AudioContextState;
|
|
63
|
+
readonly message?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
type PageOp = IGetOp | ICallOp | ISetOp | IDecodeOp | IControlOp;
|
|
67
|
+
|
|
68
|
+
interface IDecodedReply {
|
|
69
|
+
readonly op: "decoded";
|
|
70
|
+
readonly req: number;
|
|
71
|
+
readonly duration: number;
|
|
72
|
+
readonly length: number;
|
|
73
|
+
readonly numberOfChannels: number;
|
|
74
|
+
readonly sampleRate: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface IDecodeFailedReply {
|
|
78
|
+
readonly op: "decodeFailed";
|
|
79
|
+
readonly req: number;
|
|
80
|
+
readonly message: string;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
interface IDecodeSlot {
|
|
84
|
+
readonly out: number;
|
|
85
|
+
readonly resolve: (buffer: unknown) => void;
|
|
86
|
+
readonly reject: (error: Error) => void;
|
|
87
|
+
readonly onSuccess: ((buffer: unknown) => void) | undefined;
|
|
88
|
+
readonly onError: ((error: Error) => void) | undefined;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Keys whose page value is a node or an `AudioParam`. Reading one yields a child handle instead of
|
|
93
|
+
* a call, because `gain.gain` and `source.buffer` are objects to drive, not functions to run.
|
|
94
|
+
*/
|
|
95
|
+
const PARAM_DEFAULTS: Readonly<Record<string, number>> = {
|
|
96
|
+
gain: 1,
|
|
97
|
+
playbackRate: 1,
|
|
98
|
+
detune: 0,
|
|
99
|
+
positionX: 0,
|
|
100
|
+
positionY: 0,
|
|
101
|
+
positionZ: 0,
|
|
102
|
+
orientationX: 1,
|
|
103
|
+
orientationY: 0,
|
|
104
|
+
orientationZ: 0,
|
|
105
|
+
forwardX: 0,
|
|
106
|
+
forwardY: 0,
|
|
107
|
+
forwardZ: -1,
|
|
108
|
+
upX: 0,
|
|
109
|
+
upY: 1,
|
|
110
|
+
upZ: 0,
|
|
111
|
+
threshold: -24,
|
|
112
|
+
knee: 30,
|
|
113
|
+
ratio: 12,
|
|
114
|
+
attack: 0.003,
|
|
115
|
+
release: 0.25,
|
|
116
|
+
frequency: 350,
|
|
117
|
+
};
|
|
118
|
+
const OBJECT_KEYS: ReadonlySet<string> = new Set([
|
|
119
|
+
"destination",
|
|
120
|
+
"listener",
|
|
121
|
+
"Q",
|
|
122
|
+
...Object.keys(PARAM_DEFAULTS),
|
|
123
|
+
]);
|
|
124
|
+
|
|
125
|
+
const PARAM_METHODS: ReadonlySet<string> = new Set([
|
|
126
|
+
"setValueAtTime",
|
|
127
|
+
"linearRampToValueAtTime",
|
|
128
|
+
"exponentialRampToValueAtTime",
|
|
129
|
+
"setTargetAtTime",
|
|
130
|
+
"setValueCurveAtTime",
|
|
131
|
+
"cancelScheduledValues",
|
|
132
|
+
"cancelAndHoldAtTime",
|
|
133
|
+
]);
|
|
134
|
+
|
|
135
|
+
const METHODS: ReadonlySet<string> = new Set([
|
|
136
|
+
...PARAM_METHODS,
|
|
137
|
+
"connect",
|
|
138
|
+
"disconnect",
|
|
139
|
+
"addEventListener",
|
|
140
|
+
"removeEventListener",
|
|
141
|
+
"start",
|
|
142
|
+
"stop",
|
|
143
|
+
"setPosition",
|
|
144
|
+
"setOrientation",
|
|
145
|
+
"setVelocity",
|
|
146
|
+
"setPeriodicWave",
|
|
147
|
+
"getChannelData",
|
|
148
|
+
"copyFromChannel",
|
|
149
|
+
"copyToChannel",
|
|
150
|
+
"getFloatFrequencyData",
|
|
151
|
+
"getByteFrequencyData",
|
|
152
|
+
"getFloatTimeDomainData",
|
|
153
|
+
"getByteTimeDomainData",
|
|
154
|
+
"getFrequencyResponse",
|
|
155
|
+
"createGain",
|
|
156
|
+
"createBufferSource",
|
|
157
|
+
"createPanner",
|
|
158
|
+
"createStereoPanner",
|
|
159
|
+
"createBiquadFilter",
|
|
160
|
+
"createDynamicsCompressor",
|
|
161
|
+
"createOscillator",
|
|
162
|
+
"createConstantSource",
|
|
163
|
+
"createAnalyser",
|
|
164
|
+
"createBuffer",
|
|
165
|
+
"createChannelMerger",
|
|
166
|
+
"createChannelSplitter",
|
|
167
|
+
"createConvolver",
|
|
168
|
+
"createDelay",
|
|
169
|
+
"createIIRFilter",
|
|
170
|
+
"createMediaElementSource",
|
|
171
|
+
"createMediaStreamSource",
|
|
172
|
+
"createMediaStreamDestination",
|
|
173
|
+
"createPeriodicWave",
|
|
174
|
+
"createScriptProcessor",
|
|
175
|
+
"createWaveShaper",
|
|
176
|
+
]);
|
|
177
|
+
|
|
178
|
+
interface IParamReply {
|
|
179
|
+
readonly op: "param";
|
|
180
|
+
readonly id: number;
|
|
181
|
+
readonly values: Record<string, number>;
|
|
182
|
+
}
|
|
183
|
+
interface IReleasedReply {
|
|
184
|
+
readonly op: "released";
|
|
185
|
+
readonly id: number;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Worker side: a page-side `AudioContext`, with every node replaced by a proxy handle. */
|
|
189
|
+
export function createRemoteAudioContext(port: MessagePort, init: IRemoteAudioInit): AudioContext {
|
|
190
|
+
let anchoredAt = performance.now();
|
|
191
|
+
let currentTime = init.currentTime;
|
|
192
|
+
let state: AudioContextState = "suspended";
|
|
193
|
+
const functions = new Map<number, (...args: unknown[]) => unknown>();
|
|
194
|
+
const handleIds = new WeakMap<object, number>();
|
|
195
|
+
const slots = new Map<number, IDecodeSlot>();
|
|
196
|
+
const controls = new Map<number, { resolve: () => void; reject: (error: Error) => void }>();
|
|
197
|
+
const params = new Map<number, { cache: Map<string, unknown>; written: Set<string> }>();
|
|
198
|
+
let nextId = 1;
|
|
199
|
+
let nextFnId = 1;
|
|
200
|
+
|
|
201
|
+
// One message per task: AudioListener alone moves nine AudioParams a frame.
|
|
202
|
+
let queue: PageOp[] = [];
|
|
203
|
+
let transfers: Transferable[] = [];
|
|
204
|
+
const flush = (): void => {
|
|
205
|
+
port.postMessage({ op: "batch", ops: queue }, transfers);
|
|
206
|
+
queue = [];
|
|
207
|
+
transfers = [];
|
|
208
|
+
};
|
|
209
|
+
const post = (message: PageOp, transfer?: Transferable[]): void => {
|
|
210
|
+
if (queue.length === 0) queueMicrotask(flush);
|
|
211
|
+
queue.push(message);
|
|
212
|
+
if (transfer !== undefined) transfers.push(...transfer);
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
function encode(value: unknown): unknown {
|
|
216
|
+
if (typeof value === "function") {
|
|
217
|
+
const fnId = nextFnId++;
|
|
218
|
+
functions.set(fnId, value as (...args: unknown[]) => unknown);
|
|
219
|
+
return { $fn: fnId };
|
|
220
|
+
}
|
|
221
|
+
if (Array.isArray(value)) return value.map(encode);
|
|
222
|
+
if (value !== null && typeof value === "object") {
|
|
223
|
+
const id = handleIds.get(value);
|
|
224
|
+
if (id !== undefined) return { $h: id };
|
|
225
|
+
}
|
|
226
|
+
return value;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function readKey(
|
|
230
|
+
id: number,
|
|
231
|
+
cache: Map<string, unknown>,
|
|
232
|
+
key: string,
|
|
233
|
+
proxy: Record<string, unknown>,
|
|
234
|
+
): unknown {
|
|
235
|
+
const override = id === 0 ? contextValue(key) : undefined;
|
|
236
|
+
if (override !== undefined) return override.value;
|
|
237
|
+
if (cache.has(key)) return cache.get(key);
|
|
238
|
+
if (OBJECT_KEYS.has(key)) {
|
|
239
|
+
const out = nextId++;
|
|
240
|
+
const value = key === "Q" ? 1 : PARAM_DEFAULTS[key];
|
|
241
|
+
// Standard defaults are available in this task; the page sends the actual param bounds.
|
|
242
|
+
const child = makeHandle(
|
|
243
|
+
out,
|
|
244
|
+
value === undefined ? undefined : { value, defaultValue: value },
|
|
245
|
+
);
|
|
246
|
+
cache.set(key, child);
|
|
247
|
+
post({ key, op: "get", id, out });
|
|
248
|
+
return child;
|
|
249
|
+
}
|
|
250
|
+
if (!METHODS.has(key))
|
|
251
|
+
throw new Error(`Remote AudioContext: unknown property "${key}" on handle ${id}`);
|
|
252
|
+
return (...args: unknown[]) => {
|
|
253
|
+
const fluent = PARAM_METHODS.has(key);
|
|
254
|
+
if (fluent && !key.startsWith("cancel") && typeof args[0] === "number") {
|
|
255
|
+
cache.set("value", args[0]);
|
|
256
|
+
params.get(id)?.written.add("value");
|
|
257
|
+
}
|
|
258
|
+
const destination = key === "connect" ? args[0] : undefined;
|
|
259
|
+
const existing =
|
|
260
|
+
destination !== null && typeof destination === "object"
|
|
261
|
+
? handleIds.get(destination)
|
|
262
|
+
: undefined;
|
|
263
|
+
const out = fluent ? id : (existing ?? nextId++);
|
|
264
|
+
post({ args: args.map(encode), method: key, op: "call", id, out });
|
|
265
|
+
return fluent ? proxy : existing === undefined ? makeHandle(out) : destination;
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function makeHandle(id: number, seed?: Record<string, unknown>): Record<string, unknown> {
|
|
270
|
+
const cache = new Map<string, unknown>(Object.entries(seed ?? {}));
|
|
271
|
+
const written = new Set<string>();
|
|
272
|
+
let endedFn: number | undefined;
|
|
273
|
+
if (typeof seed?.value === "number") params.set(id, { cache, written });
|
|
274
|
+
const proxy = new Proxy({} as Record<string, unknown>, {
|
|
275
|
+
get: (_target, key) => {
|
|
276
|
+
if (typeof key === "symbol" || key === "then") return undefined;
|
|
277
|
+
return readKey(id, cache, key, proxy);
|
|
278
|
+
},
|
|
279
|
+
has: (_target, key) => typeof key === "string" && cache.has(key),
|
|
280
|
+
set: (_target, key, value) => {
|
|
281
|
+
if (typeof key === "symbol") return true;
|
|
282
|
+
if (key === "onended") {
|
|
283
|
+
if (endedFn !== undefined) functions.delete(endedFn);
|
|
284
|
+
endedFn = typeof value === "function" ? nextFnId : undefined;
|
|
285
|
+
}
|
|
286
|
+
cache.set(key, value);
|
|
287
|
+
written.add(key);
|
|
288
|
+
post({ key, op: "set", id, value: encode(value) });
|
|
289
|
+
return true;
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
handleIds.set(proxy, id);
|
|
293
|
+
return proxy;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function control(method: IControlOp["method"]): Promise<void> {
|
|
297
|
+
const req = nextId++;
|
|
298
|
+
return new Promise<void>((resolve, reject) => {
|
|
299
|
+
controls.set(req, { resolve, reject });
|
|
300
|
+
post({ op: "control", method, req });
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
function decodeAudioData(
|
|
305
|
+
data: ArrayBuffer,
|
|
306
|
+
onSuccess?: (buffer: unknown) => void,
|
|
307
|
+
onError?: (error: Error) => void,
|
|
308
|
+
): Promise<unknown> {
|
|
309
|
+
const req = nextId++;
|
|
310
|
+
const out = nextId++;
|
|
311
|
+
return new Promise<unknown>((resolve, reject) => {
|
|
312
|
+
slots.set(req, { onError, onSuccess, out, reject, resolve });
|
|
313
|
+
const bytes = data.slice(0);
|
|
314
|
+
post({ bytes, op: "decode", out, req }, [bytes]);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function contextValue(key: string): { readonly value: unknown } | undefined {
|
|
319
|
+
switch (key) {
|
|
320
|
+
case "currentTime":
|
|
321
|
+
return {
|
|
322
|
+
value: currentTime + (state === "running" ? (performance.now() - anchoredAt) / 1000 : 0),
|
|
323
|
+
};
|
|
324
|
+
case "sampleRate":
|
|
325
|
+
return { value: init.sampleRate };
|
|
326
|
+
case "state":
|
|
327
|
+
return { value: state };
|
|
328
|
+
case "resume":
|
|
329
|
+
case "suspend":
|
|
330
|
+
case "close":
|
|
331
|
+
return { value: () => control(key) };
|
|
332
|
+
case "decodeAudioData":
|
|
333
|
+
return { value: decodeAudioData };
|
|
334
|
+
default:
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
port.onmessage = (event: MessageEvent) => {
|
|
340
|
+
const reply = event.data as
|
|
341
|
+
| IDecodedReply
|
|
342
|
+
| IDecodeFailedReply
|
|
343
|
+
| IFireOp
|
|
344
|
+
| IClockReply
|
|
345
|
+
| IParamReply
|
|
346
|
+
| IReleasedReply
|
|
347
|
+
| IControlReply
|
|
348
|
+
| undefined;
|
|
349
|
+
if (reply?.op === "param") {
|
|
350
|
+
const param = params.get(reply.id);
|
|
351
|
+
if (param)
|
|
352
|
+
for (const [key, value] of Object.entries(reply.values)) {
|
|
353
|
+
if (!param.written.has(key)) param.cache.set(key, value);
|
|
354
|
+
}
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (reply?.op === "released") {
|
|
358
|
+
params.delete(reply.id);
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (reply?.op === "clock" || reply?.op === "controlled") {
|
|
362
|
+
currentTime = reply.currentTime;
|
|
363
|
+
state = reply.state;
|
|
364
|
+
anchoredAt = performance.now();
|
|
365
|
+
if (reply.op === "controlled") {
|
|
366
|
+
const pending = controls.get(reply.req);
|
|
367
|
+
controls.delete(reply.req);
|
|
368
|
+
if (reply.message === undefined) pending?.resolve();
|
|
369
|
+
else pending?.reject(new Error(reply.message));
|
|
370
|
+
}
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
if (reply?.op === "decoded") {
|
|
374
|
+
const slot = slots.get(reply.req);
|
|
375
|
+
if (slot === undefined) return;
|
|
376
|
+
slots.delete(reply.req);
|
|
377
|
+
const buffer = makeHandle(slot.out, {
|
|
378
|
+
duration: reply.duration,
|
|
379
|
+
length: reply.length,
|
|
380
|
+
numberOfChannels: reply.numberOfChannels,
|
|
381
|
+
sampleRate: reply.sampleRate,
|
|
382
|
+
});
|
|
383
|
+
slot.resolve(buffer);
|
|
384
|
+
slot.onSuccess?.(buffer);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
if (reply?.op === "decodeFailed") {
|
|
388
|
+
const slot = slots.get(reply.req);
|
|
389
|
+
if (slot === undefined) return;
|
|
390
|
+
slots.delete(reply.req);
|
|
391
|
+
const error = new Error(reply.message);
|
|
392
|
+
slot.onError?.(error);
|
|
393
|
+
slot.reject(error);
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
if (reply !== undefined && "fn" in reply) {
|
|
397
|
+
const fired = reply as IFireOp;
|
|
398
|
+
const fn = functions.get(fired.fn);
|
|
399
|
+
if (fired.once) functions.delete(fired.fn);
|
|
400
|
+
fn?.();
|
|
401
|
+
}
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
// quality-allow: a message-backed stand-in for the AudioContext a worker cannot create
|
|
405
|
+
return makeHandle(0) as unknown as AudioContext;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Page side: runs the real graph for a worker's handles until the returned stop is called. */
|
|
409
|
+
export function serveRemoteAudio(port: MessagePort, context: BaseAudioContext): () => void {
|
|
410
|
+
const objects = new Map<number, unknown>([[0, context]]);
|
|
411
|
+
const warned = new Set<string>();
|
|
412
|
+
const parents = new Map<number, number>();
|
|
413
|
+
// The worker numbers handles in order. One at or below the highest seen that is gone was released
|
|
414
|
+
// when its source ended; an op the worker sent before it heard that has nothing left to do.
|
|
415
|
+
let highest = 0;
|
|
416
|
+
let stopped = false;
|
|
417
|
+
const syncClock = (): void => {
|
|
418
|
+
if (!stopped)
|
|
419
|
+
port.postMessage({ op: "clock", currentTime: context.currentTime, state: context.state });
|
|
420
|
+
};
|
|
421
|
+
context.addEventListener("statechange", syncClock);
|
|
422
|
+
syncClock();
|
|
423
|
+
|
|
424
|
+
function decodeValue(value: unknown, once = false): unknown {
|
|
425
|
+
if (Array.isArray(value)) return value.map((entry) => decodeValue(entry));
|
|
426
|
+
if (value !== null && typeof value === "object") {
|
|
427
|
+
const handle = (value as { $h?: number }).$h;
|
|
428
|
+
if (typeof handle === "number") return objects.get(handle);
|
|
429
|
+
const fn = (value as { $fn?: number }).$fn;
|
|
430
|
+
if (typeof fn === "number") return () => port.postMessage({ fn, op: "fire", once });
|
|
431
|
+
}
|
|
432
|
+
return value;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function warn(op: string | undefined, error: unknown): void {
|
|
436
|
+
const name = op ?? "unknown";
|
|
437
|
+
if (warned.has(name)) return;
|
|
438
|
+
warned.add(name);
|
|
439
|
+
console.warn(`serveRemoteAudio: "${name}" failed; later failures stay quiet.`, error);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
async function serveDecode(op: IDecodeOp): Promise<void> {
|
|
443
|
+
try {
|
|
444
|
+
const buffer = await context.decodeAudioData(op.bytes);
|
|
445
|
+
if (stopped) return;
|
|
446
|
+
objects.set(op.out, buffer);
|
|
447
|
+
port.postMessage({
|
|
448
|
+
duration: buffer.duration,
|
|
449
|
+
length: buffer.length,
|
|
450
|
+
numberOfChannels: buffer.numberOfChannels,
|
|
451
|
+
op: "decoded",
|
|
452
|
+
req: op.req,
|
|
453
|
+
sampleRate: buffer.sampleRate,
|
|
454
|
+
});
|
|
455
|
+
} catch (error) {
|
|
456
|
+
if (stopped) return;
|
|
457
|
+
port.postMessage({
|
|
458
|
+
message: error instanceof Error ? error.message : String(error),
|
|
459
|
+
op: "decodeFailed",
|
|
460
|
+
req: op.req,
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
async function serveControl(op: IControlOp): Promise<void> {
|
|
466
|
+
let message: string | undefined;
|
|
467
|
+
try {
|
|
468
|
+
await (context as AudioContext)[op.method]();
|
|
469
|
+
} catch (error) {
|
|
470
|
+
message = error instanceof Error ? error.message : String(error);
|
|
471
|
+
}
|
|
472
|
+
if (!stopped)
|
|
473
|
+
port.postMessage({
|
|
474
|
+
op: "controlled",
|
|
475
|
+
req: op.req,
|
|
476
|
+
currentTime: context.currentTime,
|
|
477
|
+
state: context.state,
|
|
478
|
+
...(message === undefined ? {} : { message }),
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
port.onmessage = (event: MessageEvent) => {
|
|
483
|
+
if (stopped) return;
|
|
484
|
+
for (const op of (event.data as { ops: PageOp[] }).ops) run(op);
|
|
485
|
+
syncClock();
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
function release(id: number): void {
|
|
489
|
+
objects.delete(id);
|
|
490
|
+
port.postMessage({ op: "released", id });
|
|
491
|
+
parents.delete(id);
|
|
492
|
+
for (const [child, parent] of parents) if (parent === id) release(child);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
function released(op: PageOp): boolean {
|
|
496
|
+
const out = (op as { out?: unknown } | undefined)?.out;
|
|
497
|
+
if (typeof out === "number" && out > highest) highest = out;
|
|
498
|
+
const id = (op as { id?: unknown } | undefined)?.id;
|
|
499
|
+
return (
|
|
500
|
+
(op?.op === "call" || op?.op === "set") &&
|
|
501
|
+
typeof id === "number" &&
|
|
502
|
+
id <= highest &&
|
|
503
|
+
!objects.has(id)
|
|
504
|
+
);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
function run(op: PageOp): void {
|
|
508
|
+
if (released(op)) return;
|
|
509
|
+
try {
|
|
510
|
+
switch (op?.op) {
|
|
511
|
+
case "get": {
|
|
512
|
+
const source = objects.get(op.id) as Record<string, unknown> | undefined;
|
|
513
|
+
objects.set(op.out, source?.[op.key]);
|
|
514
|
+
parents.set(op.out, op.id);
|
|
515
|
+
const param = source?.[op.key] as Record<string, unknown> | undefined;
|
|
516
|
+
if (typeof param?.value === "number") {
|
|
517
|
+
const values = Object.fromEntries(
|
|
518
|
+
["value", "defaultValue", "minValue", "maxValue"].flatMap((key) =>
|
|
519
|
+
typeof param[key] === "number" ? [[key, param[key]]] : [],
|
|
520
|
+
),
|
|
521
|
+
);
|
|
522
|
+
port.postMessage({ op: "param", id: op.out, values });
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
case "call": {
|
|
527
|
+
const target = objects.get(op.id) as Record<string, unknown>;
|
|
528
|
+
const method = target[op.method] as (...args: unknown[]) => unknown;
|
|
529
|
+
const result = method.apply(
|
|
530
|
+
target,
|
|
531
|
+
op.args.map((arg) => decodeValue(arg)),
|
|
532
|
+
);
|
|
533
|
+
if (result !== null && typeof result === "object") objects.set(op.out, result);
|
|
534
|
+
if (
|
|
535
|
+
["createBufferSource", "createOscillator", "createConstantSource"].includes(op.method)
|
|
536
|
+
) {
|
|
537
|
+
const source = result as AudioScheduledSourceNode;
|
|
538
|
+
source.addEventListener(
|
|
539
|
+
"ended",
|
|
540
|
+
() => {
|
|
541
|
+
// A one-shot cannot restart; disconnect its graph edge and release its params too.
|
|
542
|
+
source.disconnect();
|
|
543
|
+
release(op.out);
|
|
544
|
+
},
|
|
545
|
+
{ once: true },
|
|
546
|
+
);
|
|
547
|
+
}
|
|
548
|
+
break;
|
|
549
|
+
}
|
|
550
|
+
case "set": {
|
|
551
|
+
const target = objects.get(op.id) as Record<string, unknown>;
|
|
552
|
+
target[op.key] = decodeValue(op.value, op.key === "onended");
|
|
553
|
+
break;
|
|
554
|
+
}
|
|
555
|
+
case "control": {
|
|
556
|
+
void serveControl(op);
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
case "decode": {
|
|
560
|
+
void serveDecode(op);
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
default:
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
} catch (error) {
|
|
567
|
+
warn(op.op, error);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
return () => {
|
|
572
|
+
stopped = true;
|
|
573
|
+
context.removeEventListener("statechange", syncClock);
|
|
574
|
+
objects.clear();
|
|
575
|
+
parents.clear();
|
|
576
|
+
port.onmessage = null;
|
|
577
|
+
port.close();
|
|
578
|
+
};
|
|
579
|
+
}
|