2026-02-21 16:13:48 -05:00
|
|
|
import { HEARING_RADIUS, type WorldItem } from '../state/gameState';
|
2026-02-21 23:17:18 -05:00
|
|
|
import { getItemTypeGlobalProperties } from '../items/itemRegistry';
|
2026-02-21 16:13:48 -05:00
|
|
|
import { AudioEngine } from './audioEngine';
|
2026-02-21 22:55:20 -05:00
|
|
|
import { connectEffectChain, disconnectEffectRuntime, type EffectId, type EffectRuntime } from './effects';
|
|
|
|
|
import { normalizeRadioEffect, normalizeRadioEffectValue } from './radioStationRuntime';
|
2026-02-21 20:08:43 -05:00
|
|
|
import { resolveSpatialMix } from './spatial';
|
2026-02-21 16:13:48 -05:00
|
|
|
|
|
|
|
|
type EmitOutput = {
|
|
|
|
|
soundUrl: string;
|
|
|
|
|
element: HTMLAudioElement;
|
|
|
|
|
source: MediaElementAudioSourceNode;
|
2026-02-21 22:55:20 -05:00
|
|
|
effectInput: GainNode;
|
|
|
|
|
effectRuntime: EffectRuntime | null;
|
|
|
|
|
effect: EffectId;
|
|
|
|
|
effectValue: number;
|
2026-02-21 16:13:48 -05:00
|
|
|
gain: GainNode;
|
|
|
|
|
panner: StereoPannerNode | null;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-21 19:37:08 -05:00
|
|
|
type EmitSpatialConfig = {
|
|
|
|
|
range: number;
|
|
|
|
|
directional: boolean;
|
|
|
|
|
facingDeg: number;
|
|
|
|
|
};
|
|
|
|
|
|
2026-02-21 16:30:31 -05:00
|
|
|
const ITEM_EMIT_BASE_GAIN = 0.3;
|
|
|
|
|
|
2026-02-21 23:07:37 -05:00
|
|
|
function resolveEmitPlaybackRate(raw: unknown): number {
|
|
|
|
|
const speed = Number(raw);
|
|
|
|
|
const clamped = Number.isFinite(speed) ? Math.max(0, Math.min(100, speed)) : 50;
|
|
|
|
|
if (clamped <= 50) {
|
|
|
|
|
return 0.5 + (clamped / 50) * 0.5;
|
|
|
|
|
}
|
|
|
|
|
return 1 + ((clamped - 50) / 50) * 1;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 23:17:18 -05:00
|
|
|
function setElementPreservesPitch(element: HTMLAudioElement, enabled: boolean): void {
|
|
|
|
|
const target = element as HTMLAudioElement & {
|
|
|
|
|
preservesPitch?: boolean;
|
|
|
|
|
mozPreservesPitch?: boolean;
|
|
|
|
|
webkitPreservesPitch?: boolean;
|
|
|
|
|
};
|
|
|
|
|
if ('preservesPitch' in target) target.preservesPitch = enabled;
|
|
|
|
|
if ('mozPreservesPitch' in target) target.mozPreservesPitch = enabled;
|
|
|
|
|
if ('webkitPreservesPitch' in target) target.webkitPreservesPitch = enabled;
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 02:12:03 -05:00
|
|
|
function resolveEmitRates(item: WorldItem): { playbackRate: number; preservePitch: boolean } {
|
2026-02-21 23:17:18 -05:00
|
|
|
const globals = getItemTypeGlobalProperties(item.type);
|
|
|
|
|
const speed = resolveEmitPlaybackRate(item.params.emitSoundSpeed ?? globals.emitSoundSpeed ?? 50);
|
|
|
|
|
const tempo = resolveEmitPlaybackRate(item.params.emitSoundTempo ?? globals.emitSoundTempo ?? 50);
|
2026-02-21 23:26:28 -05:00
|
|
|
const playbackRate = Math.max(0.25, Math.min(4, speed * tempo));
|
|
|
|
|
const preservePitch = Math.abs(speed - 1) < 0.001;
|
2026-02-22 02:12:03 -05:00
|
|
|
return { playbackRate, preservePitch };
|
2026-02-21 23:17:18 -05:00
|
|
|
}
|
|
|
|
|
|
2026-02-21 16:13:48 -05:00
|
|
|
export class ItemEmitRuntime {
|
|
|
|
|
private readonly outputs = new Map<string, EmitOutput>();
|
2026-02-21 16:30:31 -05:00
|
|
|
private layerEnabled = true;
|
2026-02-21 16:13:48 -05:00
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private readonly audio: AudioEngine,
|
|
|
|
|
private readonly resolveSoundUrl: (soundPath: string) => string,
|
2026-02-21 19:37:08 -05:00
|
|
|
private readonly getSpatialConfig: (item: WorldItem) => EmitSpatialConfig,
|
2026-02-21 16:13:48 -05:00
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
cleanup(itemId: string): void {
|
|
|
|
|
const output = this.outputs.get(itemId);
|
|
|
|
|
if (!output) return;
|
|
|
|
|
output.element.pause();
|
|
|
|
|
output.element.src = '';
|
|
|
|
|
output.source.disconnect();
|
2026-02-21 22:55:20 -05:00
|
|
|
output.effectInput.disconnect();
|
|
|
|
|
disconnectEffectRuntime(output.effectRuntime);
|
2026-02-21 16:13:48 -05:00
|
|
|
output.gain.disconnect();
|
|
|
|
|
output.panner?.disconnect();
|
|
|
|
|
this.outputs.delete(itemId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanupAll(): void {
|
|
|
|
|
for (const itemId of Array.from(this.outputs.keys())) {
|
|
|
|
|
this.cleanup(itemId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 16:30:31 -05:00
|
|
|
async setLayerEnabled(enabled: boolean, items: Iterable<WorldItem>): Promise<void> {
|
|
|
|
|
this.layerEnabled = enabled;
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
this.cleanupAll();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
await this.sync(items);
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-21 16:13:48 -05:00
|
|
|
async sync(items: Iterable<WorldItem>): Promise<void> {
|
2026-02-21 16:30:31 -05:00
|
|
|
if (!this.layerEnabled) {
|
|
|
|
|
this.cleanupAll();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-02-21 16:13:48 -05:00
|
|
|
const validIds = new Set<string>();
|
|
|
|
|
await this.audio.ensureContext();
|
|
|
|
|
const audioCtx = this.audio.context;
|
|
|
|
|
if (!audioCtx) return;
|
|
|
|
|
|
|
|
|
|
for (const item of items) {
|
2026-02-21 22:20:15 -05:00
|
|
|
const emitSound = String(item.params.emitSound ?? item.emitSound ?? '').trim();
|
|
|
|
|
const enabled = item.params.enabled !== false;
|
|
|
|
|
const soundUrl = enabled ? this.resolveSoundUrl(emitSound) : '';
|
2026-02-21 16:13:48 -05:00
|
|
|
if (!soundUrl || item.carrierId) {
|
|
|
|
|
this.cleanup(item.id);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
validIds.add(item.id);
|
|
|
|
|
const existing = this.outputs.get(item.id);
|
|
|
|
|
if (existing && existing.soundUrl === soundUrl) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (existing) {
|
|
|
|
|
this.cleanup(item.id);
|
|
|
|
|
}
|
|
|
|
|
const element = new Audio(soundUrl);
|
|
|
|
|
element.loop = true;
|
|
|
|
|
element.preload = 'none';
|
|
|
|
|
element.crossOrigin = 'anonymous';
|
|
|
|
|
const source = audioCtx.createMediaElementSource(element);
|
2026-02-21 22:55:20 -05:00
|
|
|
const effectInput = audioCtx.createGain();
|
2026-02-21 16:13:48 -05:00
|
|
|
const gain = audioCtx.createGain();
|
|
|
|
|
gain.gain.value = 0;
|
|
|
|
|
let panner: StereoPannerNode | null = null;
|
2026-02-21 22:55:20 -05:00
|
|
|
source.connect(effectInput);
|
|
|
|
|
const effect = normalizeRadioEffect(item.params.emitEffect);
|
|
|
|
|
const effectValue = normalizeRadioEffectValue(item.params.emitEffectValue);
|
|
|
|
|
const effectRuntime = connectEffectChain(audioCtx, effectInput, gain, effect, effectValue);
|
2026-02-21 23:17:18 -05:00
|
|
|
const initialRates = resolveEmitRates(item);
|
|
|
|
|
setElementPreservesPitch(element, initialRates.preservePitch);
|
2026-02-22 02:12:03 -05:00
|
|
|
element.playbackRate = initialRates.playbackRate;
|
2026-02-21 16:13:48 -05:00
|
|
|
if (this.audio.supportsStereoPanner()) {
|
|
|
|
|
panner = audioCtx.createStereoPanner();
|
|
|
|
|
gain.connect(panner).connect(audioCtx.destination);
|
|
|
|
|
} else {
|
|
|
|
|
gain.connect(audioCtx.destination);
|
|
|
|
|
}
|
2026-02-21 22:55:20 -05:00
|
|
|
this.outputs.set(item.id, { soundUrl, element, source, effectInput, effectRuntime, effect, effectValue, gain, panner });
|
2026-02-21 16:13:48 -05:00
|
|
|
void element.play().catch(() => undefined);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const itemId of Array.from(this.outputs.keys())) {
|
|
|
|
|
if (!validIds.has(itemId)) {
|
|
|
|
|
this.cleanup(itemId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateSpatialAudio(items: Map<string, WorldItem>, playerPosition: { x: number; y: number }): void {
|
2026-02-21 16:30:31 -05:00
|
|
|
if (!this.layerEnabled) return;
|
2026-02-21 16:13:48 -05:00
|
|
|
const audioCtx = this.audio.context;
|
|
|
|
|
if (!audioCtx) return;
|
|
|
|
|
|
|
|
|
|
for (const [itemId, output] of this.outputs.entries()) {
|
|
|
|
|
const item = items.get(itemId);
|
|
|
|
|
if (!item || item.carrierId) {
|
|
|
|
|
output.gain.gain.linearRampToValueAtTime(0, audioCtx.currentTime + 0.05);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2026-02-21 22:55:20 -05:00
|
|
|
const effect = normalizeRadioEffect(item.params.emitEffect);
|
|
|
|
|
const effectValue = normalizeRadioEffectValue(item.params.emitEffectValue);
|
|
|
|
|
if (output.effect !== effect || output.effectValue !== effectValue) {
|
|
|
|
|
output.effectInput.disconnect();
|
|
|
|
|
disconnectEffectRuntime(output.effectRuntime);
|
|
|
|
|
output.effectRuntime = connectEffectChain(audioCtx, output.effectInput, output.gain, effect, effectValue);
|
|
|
|
|
output.effect = effect;
|
|
|
|
|
output.effectValue = effectValue;
|
|
|
|
|
}
|
2026-02-21 23:17:18 -05:00
|
|
|
const nextRates = resolveEmitRates(item);
|
|
|
|
|
setElementPreservesPitch(output.element, nextRates.preservePitch);
|
|
|
|
|
const nextPlaybackRate = nextRates.playbackRate;
|
2026-02-22 02:12:03 -05:00
|
|
|
if (Math.abs(output.element.playbackRate - nextPlaybackRate) > 0.001) {
|
|
|
|
|
output.element.playbackRate = nextPlaybackRate;
|
2026-02-21 23:07:37 -05:00
|
|
|
}
|
2026-02-21 19:37:08 -05:00
|
|
|
const spatialConfig = this.getSpatialConfig(item);
|
2026-02-21 19:25:26 -05:00
|
|
|
const mix = resolveSpatialMix({
|
|
|
|
|
dx: item.x - playerPosition.x,
|
|
|
|
|
dy: item.y - playerPosition.y,
|
2026-02-21 19:37:08 -05:00
|
|
|
range: Math.max(1, spatialConfig.range || HEARING_RADIUS),
|
2026-02-21 19:25:26 -05:00
|
|
|
baseGain: ITEM_EMIT_BASE_GAIN,
|
|
|
|
|
nearFieldDistance: 1,
|
|
|
|
|
nearFieldGain: 1,
|
|
|
|
|
nearFieldCenterPan: true,
|
2026-02-21 19:37:08 -05:00
|
|
|
directional: {
|
|
|
|
|
enabled: spatialConfig.directional,
|
|
|
|
|
facingDeg: spatialConfig.facingDeg,
|
|
|
|
|
coneDeg: 120,
|
2026-02-21 20:12:53 -05:00
|
|
|
rearGain: 0.4,
|
2026-02-21 19:37:08 -05:00
|
|
|
},
|
2026-02-21 19:25:26 -05:00
|
|
|
});
|
|
|
|
|
const gainValue = mix?.gain ?? 0;
|
|
|
|
|
const panValue = mix?.pan ?? 0;
|
2026-02-21 22:38:48 -05:00
|
|
|
const emitVolumeRaw = Number(item.params.emitVolume ?? 100);
|
|
|
|
|
const emitVolume = Number.isFinite(emitVolumeRaw) ? Math.max(0, Math.min(100, emitVolumeRaw)) / 100 : 1;
|
|
|
|
|
output.gain.gain.linearRampToValueAtTime(gainValue * emitVolume, audioCtx.currentTime + 0.1);
|
2026-02-21 16:13:48 -05:00
|
|
|
if (output.panner) {
|
|
|
|
|
const resolvedPan = this.audio.getOutputMode() === 'mono' ? 0 : Math.max(-1, Math.min(1, panValue));
|
|
|
|
|
output.panner.pan.linearRampToValueAtTime(resolvedPan, audioCtx.currentTime + 0.1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|