Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667/** * The volume slider is a *perception*, the gain is a *voltage*, and the two are * not the same scale. * * A gain of 0.5 is −6 dB, which does not sound "half as loud" — it sounds * roughly a third as loud. Wiring the slider straight to `GainNode.gain`, as * this player did, spends the entire useful part of the control in its top * tenth of travel: everything below ~0.8 was fiddling, everything above was a * cliff, and an arrow key could double or halve the room. * * So the slider position (0…1, what the user sees and what is stored) is * mapped to gain in decibels, which makes every step along the slider the same * perceived change — the definition of a logarithmic control. Each 1% of * travel is the same ratio, not the same increment. * * Two details that are not decoration: * * - `level === 0` returns exactly 0, not 10^(FLOOR/20). A decibel scale never * reaches silence — −45 dB is a very quiet sound, not the absence of one — * so a mute that is not really a mute is a hiss you cannot get rid of. * - The *level* is the stored state, not the gain. If the curve is ever * retuned (a ReplayGain pre-amp will want headroom above unity), a saved * 0.6 still means "where I left it" instead of silently meaning something * else under the new mapping. */
/** * Gain at the bottom of the slider. −45 dB: low enough that the last few percent * is genuinely inaudible rather than "quiet", high enough that the range people * actually live in — a master gain of 0.1 to 0.5 — gets roughly the upper half * of the control instead of a sliver at the top. */export const VOLUME_FLOOR_DB = -45;
const DECIBELS_PER_LEVEL = VOLUME_FLOOR_DB;
/** Slider position (0…1) → linear gain (0…1) for `GainNode.gain`. */export function levelToGain(level: number): number { if (!Number.isFinite(level) || level <= 0) return 0; // mute means silent, not −45 dB if (level >= 1) return 1; return Math.pow(10, (DECIBELS_PER_LEVEL * (1 - level)) / 20);}
/** The inverse, for anything that has a gain and needs to show a position. */export function gainToLevel(gain: number): number { if (!Number.isFinite(gain) || gain <= 0) return 0; if (gain >= 1) return 1; return 1 - (20 * Math.log10(gain)) / DECIBELS_PER_LEVEL;}
/** The slider's own units, as the number the control is really sitting at. */export function formatVolume(level: number): string { const v = Number.isFinite(level) ? Math.min(1, Math.max(0, level)) : 0; return `${Math.round(v * 100)}%`;}
/** * What that position does to the signal, in the unit people who ask about * volume actually think in. Shown beside the percentage so the log curve is * never a mystery: the same 10% of travel always says the same number of dB. */export function formatVolumeDb(level: number): string { const v = Number.isFinite(level) ? Math.min(1, Math.max(0, level)) : 0; if (v <= 0) return "mute"; return `${(DECIBELS_PER_LEVEL * (1 - v)).toFixed(1)} dB`;}