/** seconds → m:ss.d */
export function mmss(t: number, decimals = 1): string {
  if (!isFinite(t)) return '0:00'
  const m = Math.floor(t / 60)
  const s = t - m * 60
  return `${m}:${s.toFixed(decimals).padStart(decimals ? 4 + decimals : 2, '0')}`
}

/** dB value → 0..1 across a loudness window (default -40..-3 LUFS) */
export function loudFrac(db: number, lo = -40, hi = -3): number {
  if (!isFinite(db)) return 0
  return Math.max(0, Math.min(1, (db - lo) / (hi - lo)))
}

export function fmtDb(v: number | null | undefined, unit = ''): string {
  return v == null ? '—' : `${v > 0 ? '+' : ''}${v.toFixed(1)}${unit}`
}
