@jgengine/shell 0.8.0 → 0.10.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 (152) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/README.md +11 -0
  3. package/dist/GamePlayerShell.d.ts +10 -22
  4. package/dist/GamePlayerShell.js +709 -264
  5. package/dist/GameUiPreview.js +2 -1
  6. package/dist/audio/AudioComponents.js +25 -8
  7. package/dist/audio/audioEngine.d.ts +9 -0
  8. package/dist/audio/audioEngine.js +37 -0
  9. package/dist/audio/musicDirector.d.ts +31 -0
  10. package/dist/audio/musicDirector.js +125 -0
  11. package/dist/audio/musicVoices.d.ts +10 -0
  12. package/dist/audio/musicVoices.js +338 -0
  13. package/dist/audio/synthEngine.d.ts +9 -0
  14. package/dist/audio/synthEngine.js +57 -0
  15. package/dist/behaviour.d.ts +5 -24
  16. package/dist/behaviour.js +14 -50
  17. package/dist/behaviourAttach.d.ts +9 -0
  18. package/dist/behaviourAttach.js +32 -0
  19. package/dist/behaviourDriver.d.ts +7 -0
  20. package/dist/behaviourDriver.js +21 -0
  21. package/dist/camera/GameCameraRig.js +1 -1
  22. package/dist/camera/GameFirstPersonCamera.d.ts +3 -0
  23. package/dist/camera/GameFirstPersonCamera.js +35 -4
  24. package/dist/camera/GameInspectionCamera.js +12 -1
  25. package/dist/camera/GameOrbitCamera.js +44 -1
  26. package/dist/camera/PlayerFov.d.ts +18 -0
  27. package/dist/camera/PlayerFov.js +48 -0
  28. package/dist/camera/cameraBlendMath.d.ts +13 -0
  29. package/dist/camera/cameraBlendMath.js +36 -0
  30. package/dist/camera/cameraRigs.d.ts +3 -0
  31. package/dist/camera/cameraRigs.js +36 -33
  32. package/dist/camera/fovPreference.d.ts +25 -0
  33. package/dist/camera/fovPreference.js +73 -0
  34. package/dist/camera/index.d.ts +3 -0
  35. package/dist/camera/index.js +3 -0
  36. package/dist/camera/orbitCameraMath.d.ts +18 -0
  37. package/dist/camera/orbitCameraMath.js +6 -1
  38. package/dist/camera/rigMath.d.ts +10 -0
  39. package/dist/camera/rigMath.js +36 -1
  40. package/dist/camera/shakeChannel.d.ts +3 -17
  41. package/dist/camera/shakeChannel.js +2 -21
  42. package/dist/camera/shakeChannelMath.d.ts +8 -0
  43. package/dist/camera/shakeChannelMath.js +21 -0
  44. package/dist/cartridge.d.ts +145 -0
  45. package/dist/cartridge.js +246 -0
  46. package/dist/commandSink.d.ts +20 -0
  47. package/dist/commandSink.js +27 -0
  48. package/dist/defineGame.js +4 -1
  49. package/dist/devtools/CollisionDebugWorld.d.ts +5 -0
  50. package/dist/devtools/CollisionDebugWorld.js +181 -0
  51. package/dist/devtools/DevtoolsOverlay.d.ts +65 -1
  52. package/dist/devtools/DevtoolsOverlay.js +383 -41
  53. package/dist/devtools/collisionDebug.d.ts +57 -0
  54. package/dist/devtools/collisionDebug.js +127 -0
  55. package/dist/devtools/collisionDebugMath.d.ts +103 -0
  56. package/dist/devtools/collisionDebugMath.js +129 -0
  57. package/dist/environment/Daylight.d.ts +20 -8
  58. package/dist/environment/Daylight.js +63 -15
  59. package/dist/environment/EnvironmentScene.js +91 -35
  60. package/dist/environment/RoadRibbons.d.ts +7 -0
  61. package/dist/environment/RoadRibbons.js +57 -0
  62. package/dist/environment/groundPadMath.d.ts +2 -2
  63. package/dist/environment/groundPadMath.js +1 -1
  64. package/dist/environment/index.d.ts +2 -1
  65. package/dist/environment/index.js +1 -0
  66. package/dist/environment/skyLightingPolicy.d.ts +10 -0
  67. package/dist/environment/skyLightingPolicy.js +6 -0
  68. package/dist/input/mouseLook.d.ts +27 -0
  69. package/dist/input/mouseLook.js +35 -0
  70. package/dist/inputSink.d.ts +18 -0
  71. package/dist/inputSink.js +35 -0
  72. package/dist/materialOverride.d.ts +4 -6
  73. package/dist/materialOverride.js +12 -16
  74. package/dist/multiplayer.js +7 -3
  75. package/dist/pointer/PointerProbe.js +6 -2
  76. package/dist/pointer/pointerService.d.ts +3 -0
  77. package/dist/pointer/pointerService.js +6 -0
  78. package/dist/postfx/PostProcessing.d.ts +10 -0
  79. package/dist/postfx/PostProcessing.js +82 -0
  80. package/dist/postfx/gradeShader.d.ts +4 -0
  81. package/dist/postfx/gradeShader.js +64 -0
  82. package/dist/render/modelRender.d.ts +10 -1
  83. package/dist/render/modelRender.js +32 -5
  84. package/dist/render/resolveModel.d.ts +14 -0
  85. package/dist/render/resolveModel.js +24 -0
  86. package/dist/settings/QuickControls.d.ts +4 -0
  87. package/dist/settings/QuickControls.js +42 -0
  88. package/dist/settings/SettingsChrome.d.ts +1 -0
  89. package/dist/settings/SettingsChrome.js +10 -0
  90. package/dist/settings/SettingsMenu.d.ts +6 -0
  91. package/dist/settings/SettingsMenu.js +148 -0
  92. package/dist/settings/SettingsRuntime.d.ts +11 -0
  93. package/dist/settings/SettingsRuntime.js +19 -0
  94. package/dist/settings/appliedSettings.d.ts +14 -0
  95. package/dist/settings/appliedSettings.js +27 -0
  96. package/dist/settings/settingsController.d.ts +20 -0
  97. package/dist/settings/settingsController.js +165 -0
  98. package/dist/structures/GeneratedBuilding.d.ts +10 -0
  99. package/dist/structures/GeneratedBuilding.js +96 -8
  100. package/dist/structures/index.d.ts +1 -1
  101. package/dist/structures/index.js +1 -1
  102. package/dist/terrain/CarvedTerrain.d.ts +5 -1
  103. package/dist/terrain/CarvedTerrain.js +6 -5
  104. package/dist/terrain/GrassField.d.ts +3 -1
  105. package/dist/terrain/GrassField.js +4 -2
  106. package/dist/terrain/grassBudget.d.ts +3 -0
  107. package/dist/terrain/grassBudget.js +6 -0
  108. package/dist/terrain/grassGeometry.js +1 -1
  109. package/dist/terrain/terrainDetailMaterial.d.ts +14 -0
  110. package/dist/terrain/terrainDetailMaterial.js +90 -0
  111. package/dist/terrain/terrainMath.d.ts +8 -0
  112. package/dist/terrain/terrainMath.js +9 -0
  113. package/dist/touch/OrientationHint.d.ts +3 -0
  114. package/dist/touch/OrientationHint.js +13 -0
  115. package/dist/touch/TouchControlsOverlay.d.ts +17 -1
  116. package/dist/touch/TouchControlsOverlay.js +125 -9
  117. package/dist/visibility/CullingProvider.d.ts +21 -0
  118. package/dist/visibility/CullingProvider.js +134 -0
  119. package/dist/vision/FrustumSensorHud.d.ts +1 -6
  120. package/dist/vision/FrustumSensorHud.js +25 -27
  121. package/dist/vision/frustumSampleEqual.d.ts +2 -0
  122. package/dist/vision/frustumSampleEqual.js +10 -0
  123. package/dist/water/Ocean.js +1 -1
  124. package/dist/water/OceanConfig.d.ts +13 -0
  125. package/dist/water/OceanConfig.js +25 -17
  126. package/dist/water/index.d.ts +1 -1
  127. package/dist/water/index.js +1 -1
  128. package/dist/weather/FireSpreadLayer.js +7 -2
  129. package/dist/weather/RainField.d.ts +3 -1
  130. package/dist/weather/RainField.js +4 -4
  131. package/dist/weather/SnowField.d.ts +3 -1
  132. package/dist/weather/SnowField.js +4 -4
  133. package/dist/weather/fireSpreadPose.d.ts +2 -0
  134. package/dist/weather/fireSpreadPose.js +4 -0
  135. package/dist/weather/weatherMath.d.ts +5 -1
  136. package/dist/weather/weatherMath.js +7 -2
  137. package/dist/world/DataObjects.d.ts +1 -1
  138. package/dist/world/DataObjects.js +3 -1
  139. package/dist/world/SpriteBatch.d.ts +44 -0
  140. package/dist/world/SpriteBatch.js +112 -0
  141. package/dist/world/WorldHud.d.ts +6 -1
  142. package/dist/world/WorldHud.js +95 -48
  143. package/dist/world/entityPose.d.ts +14 -0
  144. package/dist/world/entityPose.js +10 -0
  145. package/dist/world/telegraphPulse.d.ts +1 -0
  146. package/dist/world/telegraphPulse.js +4 -0
  147. package/dist/world/worldBarSamples.d.ts +30 -0
  148. package/dist/world/worldBarSamples.js +57 -0
  149. package/dist/worldSync.d.ts +10 -0
  150. package/dist/worldSync.js +19 -0
  151. package/llms.txt +1522 -1143
  152. package/package.json +4 -4
@@ -2,6 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { useEffect, useState } from "react";
3
3
  import { createGameContext } from "@jgengine/core/runtime/gameContext";
4
4
  import { GameProvider } from "@jgengine/react/provider";
5
+ import { HudViewportProvider } from "@jgengine/react/hudViewport";
5
6
  const PREVIEW_USER_ID = "ui-preview";
6
7
  const TICK_STEP = 1 / 60;
7
8
  function runTicks(ctx, playable, seconds) {
@@ -59,5 +60,5 @@ export function GameUiPreview({ playable, scenario = defaultUiScenario, }) {
59
60
  if (ctx === null)
60
61
  return _jsx("div", { className: "h-full w-full bg-neutral-900" });
61
62
  const GameUI = playable.GameUI;
62
- return (_jsx("div", { "data-ui-preview-ready": true, className: "relative h-full w-full overflow-hidden", style: { background: "linear-gradient(180deg, #2a3d33 0%, #1a2320 55%, #141b18 100%)" }, children: _jsx(GameProvider, { context: ctx, children: _jsx(GameUI, {}) }) }));
63
+ return (_jsx("div", { "data-ui-preview-ready": true, className: "relative h-full w-full overflow-hidden", style: { background: "linear-gradient(180deg, #2a3d33 0%, #1a2320 55%, #141b18 100%)" }, children: _jsx(GameProvider, { context: ctx, children: _jsx(HudViewportProvider, { platforms: playable.platforms, config: playable.hudFit, children: _jsx(GameUI, {}) }) }) }));
63
64
  }
@@ -1,6 +1,6 @@
1
1
  import { useFrame, useThree } from "@react-three/fiber";
2
2
  import { useEffect, useRef } from "react";
3
- import { useSceneEntities, useSceneObjects } from "@jgengine/react/hooks";
3
+ import { useGameContext } from "@jgengine/react/provider";
4
4
  export function AudioListener({ engine }) {
5
5
  const camera = useThree((state) => state.camera);
6
6
  useFrame(() => {
@@ -8,18 +8,20 @@ export function AudioListener({ engine }) {
8
8
  });
9
9
  return null;
10
10
  }
11
- function useTrackedEmitters(engine, soundByKey, entries) {
11
+ function useTrackedEmitters(engine, soundByKey, readEntries) {
12
12
  const handles = useRef(new Map());
13
+ const lastPose = useRef(new Map());
13
14
  useEffect(() => () => {
14
15
  for (const handle of handles.current.values())
15
16
  handle.stop();
16
17
  handles.current.clear();
18
+ lastPose.current.clear();
17
19
  }, [engine]);
18
20
  useFrame(() => {
19
21
  if (soundByKey === undefined)
20
22
  return;
21
23
  const live = new Set();
22
- for (const entry of entries) {
24
+ for (const entry of readEntries()) {
23
25
  const soundId = soundByKey[entry.key];
24
26
  if (soundId === undefined)
25
27
  continue;
@@ -27,27 +29,42 @@ function useTrackedEmitters(engine, soundByKey, entries) {
27
29
  let handle = handles.current.get(entry.id);
28
30
  if (handle === undefined) {
29
31
  handle = engine.playLoop(soundId, { x: entry.position[0], y: entry.position[1], z: entry.position[2] }) ?? undefined;
30
- if (handle !== undefined)
32
+ if (handle !== undefined) {
31
33
  handles.current.set(entry.id, handle);
34
+ lastPose.current.set(entry.id, entry.position);
35
+ }
36
+ continue;
37
+ }
38
+ const prev = lastPose.current.get(entry.id);
39
+ if (prev !== undefined &&
40
+ prev[0] === entry.position[0] &&
41
+ prev[1] === entry.position[1] &&
42
+ prev[2] === entry.position[2]) {
32
43
  continue;
33
44
  }
34
45
  handle.setPosition({ x: entry.position[0], y: entry.position[1], z: entry.position[2] });
46
+ lastPose.current.set(entry.id, entry.position);
35
47
  }
36
48
  for (const [id, handle] of handles.current) {
37
49
  if (!live.has(id)) {
38
50
  handle.stop();
39
51
  handles.current.delete(id);
52
+ lastPose.current.delete(id);
40
53
  }
41
54
  }
42
55
  });
43
56
  }
44
57
  export function EntityAudioEmitters({ engine, entitySounds, }) {
45
- const entities = useSceneEntities();
46
- useTrackedEmitters(engine, entitySounds, entities.map((entity) => ({ id: entity.id, key: entity.name, position: entity.position })));
58
+ const ctx = useGameContext();
59
+ useTrackedEmitters(engine, entitySounds, () => ctx.scene.entity.list().map((entity) => ({ id: entity.id, key: entity.name, position: entity.position })));
47
60
  return null;
48
61
  }
49
62
  export function ObjectAudioEmitters({ engine, objectSounds, }) {
50
- const objects = useSceneObjects();
51
- useTrackedEmitters(engine, objectSounds, objects.map((object) => ({ id: object.instanceId, key: object.catalogId, position: object.position })));
63
+ const ctx = useGameContext();
64
+ useTrackedEmitters(engine, objectSounds, () => ctx.scene.object.list().map((object) => ({
65
+ id: object.instanceId,
66
+ key: object.catalogId,
67
+ position: object.position,
68
+ })));
52
69
  return null;
53
70
  }
@@ -1,4 +1,6 @@
1
1
  import { type AudioBusDef, type SoundDef } from "@jgengine/core/audio/audioFalloff";
2
+ import type { MusicTheme } from "@jgengine/core/audio/music";
3
+ import { type CrossfadeOptions } from "./musicDirector.js";
2
4
  export interface Vec3 {
3
5
  x: number;
4
6
  y: number;
@@ -7,6 +9,10 @@ export interface Vec3 {
7
9
  export interface AudioSceneConfig {
8
10
  sounds?: Record<string, SoundDef>;
9
11
  buses?: Record<string, AudioBusDef>;
12
+ /** Procedural music themes, crossfaded by {@link AudioEngine.playMusic}. Mixed through the `musicBus` (default "music") so the settings volume applies. */
13
+ music?: Record<string, MusicTheme>;
14
+ /** Bus id the procedural music director mixes through. Default "music". */
15
+ musicBus?: string;
10
16
  }
11
17
  export interface AudioEmitterHandle {
12
18
  setPosition(position: Vec3): void;
@@ -16,7 +22,10 @@ export interface AudioEngine {
16
22
  setListenerPose(position: Vec3): void;
17
23
  playOneShot(soundId: string, position?: Vec3): void;
18
24
  playLoop(soundId: string, position?: Vec3): AudioEmitterHandle | null;
25
+ /** Crossfade the procedural soundtrack to `themeId` (null fades out). No-op when no `music` catalog is configured. */
26
+ playMusic(themeId: string | null, options?: CrossfadeOptions): void;
19
27
  setBusGain(busId: string, gain: number): void;
28
+ setMasterGain(gain: number): void;
20
29
  resume(): void;
21
30
  dispose(): void;
22
31
  }
@@ -1,10 +1,14 @@
1
1
  import { distance3, resolveEmitterGain } from "@jgengine/core/audio/audioFalloff";
2
+ import { MusicDirector } from "./musicDirector.js";
3
+ import { createNoiseBuffer, realizeSynthPatch } from "./synthEngine.js";
2
4
  function createNoopEngine() {
3
5
  return {
4
6
  setListenerPose: () => undefined,
5
7
  playOneShot: () => undefined,
6
8
  playLoop: () => null,
9
+ playMusic: () => undefined,
7
10
  setBusGain: () => undefined,
11
+ setMasterGain: () => undefined,
8
12
  resume: () => undefined,
9
13
  dispose: () => undefined,
10
14
  };
@@ -42,6 +46,21 @@ export function createAudioEngine(config = {}) {
42
46
  }
43
47
  return node;
44
48
  }
49
+ let noiseBuffer = null;
50
+ function sharedNoiseBuffer() {
51
+ if (noiseBuffer === null)
52
+ noiseBuffer = createNoiseBuffer(context);
53
+ return noiseBuffer;
54
+ }
55
+ let director = null;
56
+ function musicDirector() {
57
+ if (config.music === undefined)
58
+ return null;
59
+ if (director === null) {
60
+ director = new MusicDirector(context, busGainNode(config.musicBus ?? "music"), config.music);
61
+ }
62
+ return director;
63
+ }
45
64
  const bufferCache = new Map();
46
65
  function loadBuffer(url) {
47
66
  let pending = bufferCache.get(url);
@@ -61,6 +80,15 @@ export function createAudioEngine(config = {}) {
61
80
  return null;
62
81
  const bus = busGainNode(sound.bus);
63
82
  let currentPosition = position ?? listenerPosition;
83
+ if (sound.synth !== undefined) {
84
+ const cueGain = context.createGain();
85
+ cueGain.gain.value = resolveEmitterGain(distance3(currentPosition, listenerPosition), sound, 1);
86
+ cueGain.connect(bus);
87
+ realizeSynthPatch(context, cueGain, sharedNoiseBuffer(), sound.synth);
88
+ return { setPosition: () => undefined, stop: () => undefined };
89
+ }
90
+ if (sound.url === undefined)
91
+ return null;
64
92
  let gainNode = null;
65
93
  let stopped = false;
66
94
  void loadBuffer(sound.url).then((buffer) => {
@@ -92,18 +120,27 @@ export function createAudioEngine(config = {}) {
92
120
  listenerPosition = position;
93
121
  },
94
122
  playOneShot(soundId, position) {
123
+ void context.resume().catch(() => undefined);
95
124
  playInternal(soundId, position, false);
96
125
  },
97
126
  playLoop(soundId, position) {
98
127
  return playInternal(soundId, position, true);
99
128
  },
129
+ playMusic(themeId, options) {
130
+ void context.resume().catch(() => undefined);
131
+ musicDirector()?.crossfadeTo(themeId, options);
132
+ },
100
133
  setBusGain(busId, gain) {
101
134
  busGainNode(busId).gain.value = gain;
102
135
  },
136
+ setMasterGain(gain) {
137
+ masterGain.gain.value = gain;
138
+ },
103
139
  resume() {
104
140
  void context.resume().catch(() => undefined);
105
141
  },
106
142
  dispose() {
143
+ director?.dispose();
107
144
  void context.close().catch(() => undefined);
108
145
  },
109
146
  };
@@ -0,0 +1,31 @@
1
+ import { type MusicTheme } from "@jgengine/core/audio/music";
2
+ /** Options for a music crossfade. */
3
+ export interface CrossfadeOptions {
4
+ /** Semitone shift applied to every note of the incoming theme (per-zone key). Default 0. */
5
+ transpose?: number;
6
+ }
7
+ /**
8
+ * Runs a set of looping {@link MusicTheme}s as crossfadeable layers on one shared
9
+ * Web Audio graph: master → compressor → destination, with a fixed convolution
10
+ * reverb send. `crossfadeTo` swaps the audible theme; a 110ms lookahead scheduler
11
+ * keeps each active layer's notes queued ahead of the clock so loops are seamless.
12
+ */
13
+ export declare class MusicDirector {
14
+ private readonly ctx;
15
+ private readonly master;
16
+ private readonly reverbSend;
17
+ private readonly layers;
18
+ private timer;
19
+ private volume;
20
+ private current;
21
+ constructor(ctx: BaseAudioContext, destination: AudioNode, themes: Record<string, MusicTheme>);
22
+ /** The currently faded-in theme id, or null when music is stopped. */
23
+ currentTheme(): string | null;
24
+ /** Fade to `themeId` (or fade all out when null). No-op if it is already current. */
25
+ crossfadeTo(themeId: string | null, options?: CrossfadeOptions): void;
26
+ /** Music volume 0..1, scaling the master gain. */
27
+ setVolume(volume: number): void;
28
+ /** Stop the scheduler and release the graph. */
29
+ dispose(): void;
30
+ private tick;
31
+ }
@@ -0,0 +1,125 @@
1
+ import { notesInWindow, themeLoopSeconds } from "@jgengine/core/audio/music";
2
+ import { playMusicNote } from "./musicVoices.js";
3
+ const LOOKAHEAD = 0.6;
4
+ const TICK_MS = 110;
5
+ const FADE_IN = 0.75;
6
+ const FADE_OUT = 0.35;
7
+ const MASTER_BASE = 0.2;
8
+ /**
9
+ * Runs a set of looping {@link MusicTheme}s as crossfadeable layers on one shared
10
+ * Web Audio graph: master → compressor → destination, with a fixed convolution
11
+ * reverb send. `crossfadeTo` swaps the audible theme; a 110ms lookahead scheduler
12
+ * keeps each active layer's notes queued ahead of the clock so loops are seamless.
13
+ */
14
+ export class MusicDirector {
15
+ ctx;
16
+ master;
17
+ reverbSend;
18
+ layers = new Map();
19
+ timer;
20
+ volume = 1;
21
+ current = null;
22
+ constructor(ctx, destination, themes) {
23
+ this.ctx = ctx;
24
+ this.master = ctx.createGain();
25
+ this.master.gain.value = MASTER_BASE;
26
+ const compressor = ctx.createDynamicsCompressor();
27
+ compressor.threshold.value = -18;
28
+ compressor.knee.value = 18;
29
+ compressor.ratio.value = 2.2;
30
+ compressor.attack.value = 0.015;
31
+ compressor.release.value = 0.25;
32
+ this.master.connect(compressor);
33
+ compressor.connect(destination);
34
+ const irSeconds = 2.6;
35
+ const len = Math.floor(ctx.sampleRate * irSeconds);
36
+ const ir = ctx.createBuffer(2, len, ctx.sampleRate);
37
+ for (let ch = 0; ch < 2; ch += 1) {
38
+ const data = ir.getChannelData(ch);
39
+ for (let i = 0; i < len; i += 1)
40
+ data[i] = (Math.random() * 2 - 1) * (1 - i / len) ** 2.4;
41
+ }
42
+ const reverb = ctx.createConvolver();
43
+ reverb.buffer = ir;
44
+ this.reverbSend = ctx.createGain();
45
+ this.reverbSend.gain.value = 0.55;
46
+ this.reverbSend.connect(reverb);
47
+ reverb.connect(this.master);
48
+ for (const [id, theme] of Object.entries(themes)) {
49
+ const gain = ctx.createGain();
50
+ gain.gain.value = 0;
51
+ gain.connect(this.master);
52
+ gain.connect(this.reverbSend);
53
+ this.layers.set(id, {
54
+ theme,
55
+ gain,
56
+ trim: theme.trim ?? 1,
57
+ active: false,
58
+ target: 0,
59
+ anchor: 0,
60
+ nextFrom: 0,
61
+ transpose: 0,
62
+ });
63
+ }
64
+ if (typeof setInterval === "function") {
65
+ this.timer = setInterval(() => this.tick(), TICK_MS);
66
+ }
67
+ }
68
+ /** The currently faded-in theme id, or null when music is stopped. */
69
+ currentTheme() {
70
+ return this.current;
71
+ }
72
+ /** Fade to `themeId` (or fade all out when null). No-op if it is already current. */
73
+ crossfadeTo(themeId, options = {}) {
74
+ if (themeId === this.current)
75
+ return;
76
+ this.current = themeId;
77
+ const now = this.ctx.currentTime;
78
+ for (const [id, layer] of this.layers) {
79
+ const target = id === themeId ? layer.trim : 0;
80
+ if (layer.target === target)
81
+ continue;
82
+ const rising = target > layer.target;
83
+ layer.target = target;
84
+ layer.gain.gain.setTargetAtTime(target, now, rising ? FADE_IN : FADE_OUT);
85
+ if (rising) {
86
+ layer.active = true;
87
+ layer.anchor = now + 0.15;
88
+ layer.nextFrom = layer.anchor - 1e-6;
89
+ layer.transpose = options.transpose ?? 0;
90
+ }
91
+ else {
92
+ layer.active = false;
93
+ }
94
+ }
95
+ }
96
+ /** Music volume 0..1, scaling the master gain. */
97
+ setVolume(volume) {
98
+ this.volume = Math.max(0, volume);
99
+ this.master.gain.value = MASTER_BASE * this.volume;
100
+ }
101
+ /** Stop the scheduler and release the graph. */
102
+ dispose() {
103
+ if (this.timer !== undefined)
104
+ clearInterval(this.timer);
105
+ this.timer = undefined;
106
+ this.master.disconnect();
107
+ this.reverbSend.disconnect();
108
+ }
109
+ tick() {
110
+ const horizon = this.ctx.currentTime + LOOKAHEAD;
111
+ const spbAvailable = 60;
112
+ for (const layer of this.layers.values()) {
113
+ if (!layer.active)
114
+ continue;
115
+ if (themeLoopSeconds(layer.theme) <= 0)
116
+ continue;
117
+ const spb = spbAvailable / layer.theme.bpm;
118
+ const notes = notesInWindow(layer.theme, layer.anchor, layer.nextFrom, horizon);
119
+ for (const { event, when } of notes) {
120
+ playMusicNote(this.ctx, event, when, spb, layer.gain, layer.transpose);
121
+ }
122
+ layer.nextFrom = horizon;
123
+ }
124
+ }
125
+ }
@@ -0,0 +1,10 @@
1
+ import { type NoteEvent } from "@jgengine/core/audio/music";
2
+ type Ctx = BaseAudioContext;
3
+ /**
4
+ * Play one theme note through its instrument voice into `out` (a per-layer gain
5
+ * node the director wires to master + reverb). `spb` is seconds-per-beat and
6
+ * `transpose` shifts the note in semitones for per-zone key changes. This is the
7
+ * engine's reusable instrument library; unknown instruments fall back to a sine.
8
+ */
9
+ export declare function playMusicNote(ctx: Ctx, event: NoteEvent, when: number, spb: number, out: GainNode, transpose?: number): void;
10
+ export {};