feat: per-user volume leveling preference, synced across devices (M464 #4998)
release / govulncheck (push) Successful in 22s
release / go (push) Successful in 1m17s
release / web (push) Failing after 1m5s
release / android (push) Failing after 1m51s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / integration (push) Canceled after 4m12s
release / Build signed APK (releases and dev) (push) Canceled after 3m21s

Mode (off, auto, track, album), target (-18, -16, -14 LUFS) and boost
(within headroom, or fully with a limiter), stored per user on the server
so the web player, the Android app and casts apply the same one.

- Server: user_normalization_prefs (migration 0067), GET/PUT
  /api/me/normalization; a whole-body PUT, validated, last write wins.
- Web: Settings > Playback > Volume leveling. Saves at once, restores the
  old choice if the save fails, and caches the value for the player.
- Android: Settings card. The device keeps a copy for offline playback
  (Room v9 with an explicit migration, so the upgrade wipes nothing).
  Writes are offline-first: shown at once, PUT best effort, queued on
  failure (NORMALIZATION_SET, collapsed to the newest). A refresh never
  overwrites a change still queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-06 14:29:40 -04:00
co-authored by Claude Opus 5.5
parent e3aa8629d3
commit af36b2f24a
34 changed files with 1225 additions and 5 deletions
@@ -0,0 +1,66 @@
import * as storage from '$lib/util/safeLocalStorage';
import {
DEFAULT_NORMALIZATION,
getNormalization,
isNormalizationPrefs,
putNormalization,
type NormalizationPrefs
} from '$lib/api/normalization';
// The signed-in user's normalization preference. The server holds it; this
// keeps the last value seen in localStorage too, so the player levels the
// first track of a session before the fetch returns.
const STORAGE_KEY = 'minstrel:normalization';
function readCached(): NormalizationPrefs {
try {
const v: unknown = JSON.parse(storage.read(STORAGE_KEY) ?? 'null');
return isNormalizationPrefs(v) ? v : DEFAULT_NORMALIZATION;
} catch {
return DEFAULT_NORMALIZATION;
}
}
let _prefs = $state<NormalizationPrefs>(readCached());
// Bumped by every change made here. A load or a save that resolves after a
// newer change must not overwrite it.
let _generation = 0;
export const normalization = {
get value(): NormalizationPrefs {
return _prefs;
}
};
function commit(p: NormalizationPrefs): void {
_prefs = p;
storage.write(STORAGE_KEY, JSON.stringify(p));
}
/** Refreshes from the server; on failure the cached value stands. */
export async function loadNormalization(): Promise<void> {
const gen = _generation;
try {
const p = await getNormalization();
if (gen === _generation) commit(p);
} catch {
// Offline or signed out: keep what we had.
}
}
/**
* Applies [next] at once, then saves it. If the save fails the previous
* value is restored and the error rethrown for the caller to report.
*/
export async function setNormalization(next: NormalizationPrefs): Promise<void> {
const prev = _prefs;
const gen = ++_generation;
commit(next);
try {
const saved = await putNormalization(next);
if (gen === _generation) commit(saved);
} catch (err) {
if (gen === _generation) commit(prev);
throw err;
}
}