Use target smoothing for continuous spatial audio

This commit is contained in:
Jage9
2026-02-22 21:27:27 -05:00
parent 3d741b358a
commit 6c453d56ff
4 changed files with 9 additions and 6 deletions

View File

@@ -30,6 +30,7 @@ type SoundSpec = {
type OutputMode = 'stereo' | 'mono';
const SPATIAL_RAMP_SECONDS = 0.2;
const SPATIAL_TIME_CONSTANT_SECONDS = SPATIAL_RAMP_SECONDS / 3;
export class AudioEngine {
private audioCtx: AudioContext | null = null;
@@ -301,7 +302,7 @@ export class AudioEngine {
const gainValue = mix?.gain ?? 0;
const listenGain = Number.isFinite(peer.listenGain) ? Math.max(0, peer.listenGain as number) : 1;
const panValue = mix?.pan ?? 0;
peer.gain.gain.linearRampToValueAtTime(gainValue * listenGain, this.audioCtx.currentTime + SPATIAL_RAMP_SECONDS);
peer.gain.gain.setTargetAtTime(gainValue * listenGain, this.audioCtx.currentTime, SPATIAL_TIME_CONSTANT_SECONDS);
if (peer.panner) {
const resolvedPan = this.outputMode === 'mono' ? 0 : Math.max(-1, Math.min(1, panValue));
peer.panner.pan.setValueAtTime(resolvedPan, this.audioCtx.currentTime);