Apply perceptual curve to media and emit volume

This commit is contained in:
Jage9
2026-02-22 20:42:05 -05:00
parent 6f50169805
commit 1b5acd9381
4 changed files with 14 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import { AudioEngine } from './audioEngine';
import { connectEffectChain, disconnectEffectRuntime, type EffectId, type EffectRuntime } from './effects';
import { normalizeRadioEffect, normalizeRadioEffectValue } from './radioStationRuntime';
import { resolveSpatialMix } from './spatial';
import { volumePercentToGain } from './volume';
type EmitOutput = {
soundUrl: string;
@@ -23,7 +24,7 @@ type EmitSpatialConfig = {
facingDeg: number;
};
const ITEM_EMIT_BASE_GAIN = 0.3;
const ITEM_EMIT_BASE_GAIN = 1;
const SUBSCRIBE_PRELOAD_SQUARES = 5;
const UNSUBSCRIBE_HYSTERESIS_SQUARES = 8;
@@ -218,8 +219,7 @@ export class ItemEmitRuntime {
});
const gainValue = mix?.gain ?? 0;
const panValue = mix?.pan ?? 0;
const emitVolumeRaw = Number(item.params.emitVolume ?? 100);
const emitVolume = Number.isFinite(emitVolumeRaw) ? Math.max(0, Math.min(100, emitVolumeRaw)) / 100 : 1;
const emitVolume = volumePercentToGain(item.params.emitVolume, 100);
output.gain.gain.linearRampToValueAtTime(gainValue * emitVolume, audioCtx.currentTime + 0.1);
if (output.panner) {
const resolvedPan = this.audio.getOutputMode() === 'mono' ? 0 : Math.max(-1, Math.min(1, panValue));

View File

@@ -2,6 +2,7 @@ import { HEARING_RADIUS, type WorldItem } from '../state/gameState';
import { EFFECT_IDS, clampEffectLevel, connectEffectChain, disconnectEffectRuntime, type EffectId, type EffectRuntime } from './effects';
import { AudioEngine } from './audioEngine';
import { resolveSpatialMix } from './spatial';
import { volumePercentToGain } from './volume';
export const RADIO_CHANNEL_OPTIONS = ['stereo', 'mono', 'left', 'right'] as const;
export type RadioChannelMode = (typeof RADIO_CHANNEL_OPTIONS)[number];
@@ -265,8 +266,7 @@ export class RadioStationRuntime {
}
const streamUrl = String(item.params.streamUrl ?? '').trim();
const enabled = item.params.enabled !== false;
const mediaVolume = Number(item.params.mediaVolume ?? 50);
const normalizedVolume = Number.isFinite(mediaVolume) ? Math.max(0, Math.min(100, mediaVolume)) / 100 : 0.5;
const normalizedVolume = volumePercentToGain(item.params.mediaVolume, 50);
const effect = normalizeRadioEffect(item.params.mediaEffect);
const effectValue = normalizeRadioEffectValue(item.params.mediaEffectValue);
this.applyEffect(output, audioCtx, effect, effectValue);

View File

@@ -0,0 +1,8 @@
/** Converts a 0-100 slider value into gain using a perceptual smoothstep curve. */
export function volumePercentToGain(value: unknown, fallbackPercent: number): number {
const raw = Number(value);
const normalized = Number.isFinite(raw) ? Math.max(0, Math.min(100, raw)) / 100 : Math.max(0, Math.min(100, fallbackPercent)) / 100;
// Smoothstep keeps 0->0, 50->0.5, 100->1 while easing low/high ranges.
return normalized * normalized * (3 - 2 * normalized);
}