Smooth spatial transitions with 0.2s ramps
This commit is contained in:
@@ -29,6 +29,7 @@ type SoundSpec = {
|
||||
};
|
||||
|
||||
type OutputMode = 'stereo' | 'mono';
|
||||
const SPATIAL_RAMP_SECONDS = 0.2;
|
||||
|
||||
export class AudioEngine {
|
||||
private audioCtx: AudioContext | null = null;
|
||||
@@ -300,7 +301,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 + 0.1);
|
||||
peer.gain.gain.linearRampToValueAtTime(gainValue * listenGain, this.audioCtx.currentTime + SPATIAL_RAMP_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);
|
||||
|
||||
@@ -27,6 +27,7 @@ type EmitSpatialConfig = {
|
||||
const ITEM_EMIT_BASE_GAIN = 1;
|
||||
const SUBSCRIBE_PRELOAD_SQUARES = 5;
|
||||
const UNSUBSCRIBE_HYSTERESIS_SQUARES = 8;
|
||||
const SPATIAL_RAMP_SECONDS = 0.2;
|
||||
|
||||
/** Maps a 0-100 speed control to playback-rate range used by emitted audio. */
|
||||
function resolveEmitPlaybackRate(raw: unknown): number {
|
||||
@@ -223,10 +224,10 @@ export class ItemEmitRuntime {
|
||||
const gainValue = mix?.gain ?? 0;
|
||||
const panValue = mix?.pan ?? 0;
|
||||
const emitVolume = volumePercentToGain(item.params.emitVolume, 100);
|
||||
output.gain.gain.linearRampToValueAtTime(gainValue * emitVolume, audioCtx.currentTime + 0.1);
|
||||
output.gain.gain.linearRampToValueAtTime(gainValue * emitVolume, audioCtx.currentTime + SPATIAL_RAMP_SECONDS);
|
||||
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);
|
||||
output.panner.pan.linearRampToValueAtTime(resolvedPan, audioCtx.currentTime + SPATIAL_RAMP_SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,6 +165,7 @@ type RadioSpatialConfig = {
|
||||
|
||||
const SUBSCRIBE_PRELOAD_SQUARES = 5;
|
||||
const UNSUBSCRIBE_HYSTERESIS_SQUARES = 8;
|
||||
const SPATIAL_RAMP_SECONDS = 0.2;
|
||||
|
||||
export class RadioStationRuntime {
|
||||
private readonly sharedRadioSources = new Map<string, SharedRadioSource>();
|
||||
@@ -295,10 +296,10 @@ export class RadioStationRuntime {
|
||||
});
|
||||
const gainValue = mix?.gain ?? 0;
|
||||
const panValue = mix?.pan ?? 0;
|
||||
output.gain.gain.linearRampToValueAtTime(gainValue, audioCtx.currentTime + 0.1);
|
||||
output.gain.gain.linearRampToValueAtTime(gainValue, audioCtx.currentTime + SPATIAL_RAMP_SECONDS);
|
||||
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);
|
||||
output.panner.pan.linearRampToValueAtTime(resolvedPan, audioCtx.currentTime + SPATIAL_RAMP_SECONDS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,8 @@ export function resolveSpatialMix(options: SpatialMixOptions): SpatialMixResult
|
||||
}
|
||||
|
||||
const volumeRatio = Math.max(0, 1 - distance / effectiveRange);
|
||||
let gain = baseGain * Math.pow(volumeRatio, 2);
|
||||
const shapedVolume = volumeRatio * volumeRatio * (3 - 2 * volumeRatio);
|
||||
let gain = baseGain * shapedVolume;
|
||||
const clampedX = Math.max(-range, Math.min(range, dx));
|
||||
let pan = Math.sin((clampedX / range) * (Math.PI / 2));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user