@bendyline/molen-client 0.0.11 → 0.0.12

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/dist/audio.mjs CHANGED
@@ -117,6 +117,40 @@ function unitRandom(...parts) {
117
117
  return hash32(...parts) / 4294967296;
118
118
  }
119
119
  //#endregion
120
+ //#region src/audio/rotor-acoustics.ts
121
+ const clamp = (n, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, n));
122
+ const finite = (n, fallback = 0) => typeof n === "number" && Number.isFinite(n) ? n : fallback;
123
+ /**
124
+ * An authored acoustic approximation, not a blade-element solver or measured noise prediction.
125
+ * Collective sets mean feathering; cyclic varies it by rotor azimuth. Bank/rate and descending
126
+ * inflow strengthen the loading envelope without changing governed RPM. NASA's maneuver study
127
+ * motivates these dependencies: https://ntrs.nasa.gov/citations/20160005979 .
128
+ */
129
+ function rotorAcoustics(spec, state, input, listenerLocal) {
130
+ const radians = Math.PI / 180;
131
+ const collective = (2 + 12 * clamp(finite(input?.power, .5))) * radians;
132
+ const cyclicPitch = clamp(finite(input?.pitch), -1, 1) * 6 * radians;
133
+ const cyclicRoll = clamp(finite(input?.roll), -1, 1) * 6 * radians;
134
+ const cyclic = clamp(Math.hypot(cyclicPitch, cyclicRoll) / (6 * radians));
135
+ const x = finite(listenerLocal[0]), z = finite(listenerLocal[2]);
136
+ const radius = Math.max(1, Math.hypot(x, z));
137
+ const loading = clamp(((collective + cyclicRoll * x / radius + cyclicPitch * z / radius) / radians - 2) / 12);
138
+ const maneuver = clamp(Math.abs(finite(state.rollRate)) + Math.abs(finite(state.pitchRate)) + (1 / Math.max(.45, Math.cos(finite(state.roll))) - 1) * .7);
139
+ const descent = clamp(-finite(state.verticalSpeed) / 7);
140
+ const forward = clamp(finite(state.airspeed) / 15) * clamp((90 - finite(state.airspeed)) / 40);
141
+ const interaction = state.grounded ? 0 : clamp(descent * forward + finite(state.vortexRing));
142
+ const rpm = clamp(finite(state.rpm), 0, 1.2);
143
+ return {
144
+ collective,
145
+ cyclicPitch,
146
+ cyclicRoll,
147
+ gain: rpm < .01 ? 0 : .65 + .45 * loading + .15 * cyclic + .15 * maneuver,
148
+ lowpassHz: 1200 + 4e3 * loading + 1600 * cyclic + 2200 * interaction + 1e3 * maneuver,
149
+ pulseHz: clamp(finite(spec.engine?.rotorAngularSpeed, 45) * rpm / (2 * Math.PI), .1, 30),
150
+ pulseDepth: clamp(.05 + .14 * loading + .2 * cyclic + .18 * maneuver + .25 * interaction, 0, .65)
151
+ };
152
+ }
153
+ //#endregion
120
154
  //#region src/audio/signals.ts
121
155
  function walk(value, path) {
122
156
  let cur = value;
@@ -287,6 +321,14 @@ function vec3(v) {
287
321
  function dist(a, b) {
288
322
  return Math.hypot(a[0] - b[0], a[1] - b[1], a[2] - b[2]);
289
323
  }
324
+ function vec4(value) {
325
+ return Array.isArray(value) && value.length === 4 && value.every(Number.isFinite) ? value : [
326
+ 0,
327
+ 0,
328
+ 0,
329
+ 1
330
+ ];
331
+ }
290
332
  function spatialOf(s) {
291
333
  return {
292
334
  refDistance: s.refDistance ?? DEFAULT_SPATIAL.refDistance,
@@ -801,8 +843,31 @@ var AudioDirector = class {
801
843
  const active = gate.update(evalWhen(d.when, entityCtx, gate.active), now);
802
844
  const position = this.entityPos(source, id);
803
845
  const spatial = this.spatialFor(d.spatial, sound, position);
804
- const gain = (sound.entry.gain ?? 1) * (d.gain ?? 1) * this.bound(d.gainFrom, entityCtx);
846
+ let gain = (sound.entry.gain ?? 1) * (d.gain ?? 1) * this.bound(d.gainFrom, entityCtx);
805
847
  const pitchMul = (d.pitch ?? 1) * this.bound(d.pitchFrom, entityCtx);
848
+ let tone;
849
+ const aircraft = source.get(id, "aircraft");
850
+ const state = source.get(id, "aircraftState");
851
+ if (aircraft?.spec.model === "helicopter" && state && sound.entry.tags?.includes("rotor")) {
852
+ const delta = ctx.listener.position.map((v, i) => v - (position?.[i] ?? 0));
853
+ const q = vec4(source.get(id, "transform")?.rot);
854
+ const [x, y, z] = delta;
855
+ const tx = 2 * (q[2] * y - q[1] * z);
856
+ const ty = 2 * (q[0] * z - q[2] * x);
857
+ const tz = 2 * (q[1] * x - q[0] * y);
858
+ const local = [
859
+ x + q[3] * tx - q[1] * tz + q[2] * ty,
860
+ y + q[3] * ty - q[2] * tx + q[0] * tz,
861
+ z + q[3] * tz - q[0] * ty + q[1] * tx
862
+ ];
863
+ const rotor = rotorAcoustics(aircraft.spec, state, source.get(id, "aircraftInput"), local);
864
+ gain *= rotor.gain;
865
+ tone = {
866
+ lowpassHz: rotor.lowpassHz,
867
+ pulseHz: rotor.pulseHz,
868
+ pulseDepth: rotor.pulseDepth
869
+ };
870
+ }
806
871
  const bus = d.bus ?? sound.entry.bus ?? "sfx";
807
872
  const loop = d.loop ?? sound.entry.loop ?? false;
808
873
  const autoplay = d.autoplay ?? true;
@@ -826,6 +891,7 @@ var AudioDirector = class {
826
891
  fadeOutS: .5,
827
892
  rampS: this.smoothing(.15, d.pitchFrom, d.gainFrom),
828
893
  priority: 3,
894
+ ...tone ? { tone } : {},
829
895
  entity: id,
830
896
  ...spatial ? { spatial } : {},
831
897
  ...position ? { position } : {}
@@ -1164,7 +1230,7 @@ var AudioDirector = class {
1164
1230
  }).filter(({ d, distance }) => {
1165
1231
  if (!d.spatial) return true;
1166
1232
  return distance <= d.spatial.maxDistance * (this.live.has(d.key) ? 1.1 : 1);
1167
- }).sort((a, b) => a.d.priority - b.d.priority || b.score - a.score);
1233
+ }).sort((a, b) => b.d.priority - a.d.priority || b.score - a.score);
1168
1234
  const budget = Math.max(0, (env?.maxVoices ?? 32) - (this.musicState?.voice ? 1 : 0));
1169
1235
  const kept = scored.slice(0, budget).map((s) => s.d);
1170
1236
  const keep = new Set(kept.map((d) => d.key));
@@ -1202,7 +1268,8 @@ var AudioDirector = class {
1202
1268
  ...d.spatial,
1203
1269
  position: d.position
1204
1270
  } } : {},
1205
- fadeInS: d.fadeInS
1271
+ fadeInS: d.fadeInS,
1272
+ ...d.tone ? { tone: d.tone } : {}
1206
1273
  });
1207
1274
  this.live.set(d.key, {
1208
1275
  key: d.key,
@@ -1214,6 +1281,7 @@ var AudioDirector = class {
1214
1281
  pitch,
1215
1282
  basePitch,
1216
1283
  fadeOutS: d.fadeOutS,
1284
+ ...d.tone ? { tone: d.tone } : {},
1217
1285
  ...d.position ? { position: d.position } : {},
1218
1286
  ...d.entity !== void 0 ? { entity: d.entity } : {}
1219
1287
  });
@@ -1225,6 +1293,11 @@ var AudioDirector = class {
1225
1293
  voice: live.voice
1226
1294
  };
1227
1295
  let changed = false;
1296
+ if (d.tone && (!live.tone || Math.abs(d.tone.lowpassHz - live.tone.lowpassHz) > 20 || Math.abs(d.tone.pulseHz - live.tone.pulseHz) > .02 || Math.abs(d.tone.pulseDepth - live.tone.pulseDepth) > .005)) {
1297
+ set.tone = d.tone;
1298
+ live.tone = d.tone;
1299
+ changed = true;
1300
+ }
1228
1301
  if (Math.abs(d.gain - live.gain) > .005) {
1229
1302
  set.gain = d.gain;
1230
1303
  live.gain = d.gain;
@@ -1512,6 +1585,7 @@ var WebAudioBackend = class {
1512
1585
  pitch: cmd.pitch
1513
1586
  };
1514
1587
  this.voices.set(cmd.voice, voice);
1588
+ if (cmd.tone) this.setTone(voice, cmd.tone, .01);
1515
1589
  this.load(cmd.ref).then((buffer) => {
1516
1590
  if (voice.stopped || this.disposed || !buffer) {
1517
1591
  if (!buffer) this.cleanup(cmd.voice, voice);
@@ -1527,7 +1601,7 @@ var WebAudioBackend = class {
1527
1601
  if (cmd.loop && cmd.loopStart !== void 0) src.loopStart = cmd.loopStart;
1528
1602
  if (cmd.loop && cmd.loopEnd !== void 0) src.loopEnd = cmd.loopEnd;
1529
1603
  src.playbackRate.value = voice.pitch;
1530
- src.connect(gain);
1604
+ src.connect(voice.tone?.filter ?? gain);
1531
1605
  const now = ctx.currentTime;
1532
1606
  const fade = cmd.fadeInS ?? 0;
1533
1607
  gain.gain.setValueAtTime(fade > 0 ? 0 : voice.targetGain, now);
@@ -1542,6 +1616,7 @@ var WebAudioBackend = class {
1542
1616
  if (!v || v.stopped) return;
1543
1617
  const now = this.context.currentTime;
1544
1618
  const tau = Math.max(.01, (cmd.rampS ?? .1) / 3);
1619
+ if (cmd.tone) this.setTone(v, cmd.tone, tau);
1545
1620
  if (cmd.gain !== void 0) {
1546
1621
  v.targetGain = cmd.gain;
1547
1622
  if (v.src) v.gain.gain.setTargetAtTime(cmd.gain, now, tau);
@@ -1557,6 +1632,45 @@ var WebAudioBackend = class {
1557
1632
  v.panner.positionZ.setTargetAtTime(z, now, .02);
1558
1633
  }
1559
1634
  }
1635
+ setTone(v, tone, tau) {
1636
+ const ctx = this.context;
1637
+ const amount = Math.max(0, Math.min(.9, tone.pulseDepth)) / 2;
1638
+ const frequency = Math.max(100, Math.min(2e4, tone.lowpassHz));
1639
+ const rate = Math.max(.1, Math.min(60, tone.pulseHz));
1640
+ if (!v.tone) {
1641
+ const filter = ctx.createBiquadFilter();
1642
+ filter.type = "lowpass";
1643
+ filter.Q.value = .7;
1644
+ filter.frequency.value = frequency;
1645
+ const pulse = ctx.createGain();
1646
+ pulse.gain.value = 1 - amount;
1647
+ const oscillator = ctx.createOscillator();
1648
+ oscillator.frequency.value = rate;
1649
+ const depth = ctx.createGain();
1650
+ depth.gain.value = amount;
1651
+ oscillator.type = "sine";
1652
+ oscillator.connect(depth);
1653
+ depth.connect(pulse.gain);
1654
+ filter.connect(pulse);
1655
+ pulse.connect(v.gain);
1656
+ if (v.src) {
1657
+ v.src.disconnect();
1658
+ v.src.connect(filter);
1659
+ }
1660
+ v.tone = {
1661
+ filter,
1662
+ pulse,
1663
+ oscillator,
1664
+ depth
1665
+ };
1666
+ oscillator.start();
1667
+ }
1668
+ const now = ctx.currentTime;
1669
+ v.tone.filter.frequency.setTargetAtTime(frequency, now, tau);
1670
+ v.tone.oscillator.frequency.setTargetAtTime(rate, now, tau);
1671
+ v.tone.depth.gain.setTargetAtTime(amount, now, tau);
1672
+ v.tone.pulse.gain.setTargetAtTime(1 - amount, now, tau);
1673
+ }
1560
1674
  stop(id, fadeS = .05) {
1561
1675
  const v = this.voices.get(id);
1562
1676
  if (!v || v.stopped) return;
@@ -1583,6 +1697,11 @@ var WebAudioBackend = class {
1583
1697
  v.src?.disconnect();
1584
1698
  v.gain.disconnect();
1585
1699
  v.panner?.disconnect();
1700
+ v.tone?.oscillator.stop();
1701
+ v.tone?.oscillator.disconnect();
1702
+ v.tone?.depth.disconnect();
1703
+ v.tone?.filter.disconnect();
1704
+ v.tone?.pulse.disconnect();
1586
1705
  } catch {}
1587
1706
  this.trimBuffers();
1588
1707
  }
@@ -1652,10 +1771,13 @@ function createAudioLayer(opts) {
1652
1771
  if (next?.onEvent) detachEvents = next.onEvent((event, tick) => director.handleEvent(event, tick));
1653
1772
  };
1654
1773
  let removeVisibility;
1774
+ let suspended = false;
1775
+ const applySuspended = () => backend.setSuspended?.(suspended || opts.suspendWhenHidden !== false && typeof document !== "undefined" && document.hidden);
1655
1776
  if (opts.suspendWhenHidden !== false && typeof document !== "undefined") {
1656
- const onVisibility = () => backend.setSuspended?.(document.hidden);
1777
+ const onVisibility = applySuspended;
1657
1778
  document.addEventListener("visibilitychange", onVisibility);
1658
1779
  removeVisibility = () => document.removeEventListener("visibilitychange", onVisibility);
1780
+ onVisibility();
1659
1781
  }
1660
1782
  return {
1661
1783
  director,
@@ -1730,7 +1852,10 @@ function createAudioLayer(opts) {
1730
1852
  setEnvironment: (environment) => director.setEnvironment(environment),
1731
1853
  setBanks: (banks) => director.setBank(mergeSoundbanks(banks)),
1732
1854
  unlock: () => backend.unlock(),
1733
- setSuspended: (s) => backend.setSuspended?.(s),
1855
+ setSuspended: (s) => {
1856
+ suspended = s;
1857
+ applySuspended();
1858
+ },
1734
1859
  stats: () => ({
1735
1860
  voices: director.voices().length,
1736
1861
  warnings: director.warnings().length,
@@ -1,4 +1,4 @@
1
- import { n as CameraPose } from "./renderer-Dzus24ZF.mjs";
1
+ import { n as CameraPose } from "./renderer-DAk7C8jH.mjs";
2
2
 
3
3
  //#region src/camera-track.d.ts
4
4
  interface CameraTrackKeyframe {