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
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:
@@ -0,0 +1,104 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import {
|
||||
NORMALIZATION_MODES,
|
||||
NORMALIZATION_TARGETS,
|
||||
type NormalizationPrefs
|
||||
} from '$lib/api/normalization';
|
||||
import {
|
||||
normalization,
|
||||
loadNormalization,
|
||||
setNormalization
|
||||
} from '$lib/stores/normalization.svelte';
|
||||
import { pushToast } from '$lib/stores/toast.svelte';
|
||||
|
||||
// Volume leveling (M464 #4998). The choice is the user's, stored on the
|
||||
// server, so it follows them to the Android app and to casts.
|
||||
onMount(() => {
|
||||
void loadNormalization();
|
||||
});
|
||||
|
||||
const modeLabels: Record<string, string> = {
|
||||
off: 'Off',
|
||||
auto: 'Auto',
|
||||
track: 'Track',
|
||||
album: 'Album'
|
||||
};
|
||||
const modeHints: Record<string, string> = {
|
||||
off: 'Tracks play at their mastered volume.',
|
||||
auto: 'Album gain for whole albums in order, track gain otherwise.',
|
||||
track: 'Every track at the same loudness.',
|
||||
album: 'Albums keep their own quiet and loud tracks.'
|
||||
};
|
||||
|
||||
async function change(patch: Partial<NormalizationPrefs>): Promise<void> {
|
||||
try {
|
||||
await setNormalization({ ...normalization.value, ...patch });
|
||||
} catch {
|
||||
pushToast('Could not save volume leveling.', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
const pill = (on: boolean) =>
|
||||
`flex cursor-pointer items-center rounded border border-border px-3 py-1.5 text-sm ${
|
||||
on ? 'bg-action-secondary text-action-fg' : 'text-text-primary hover:bg-surface-hover'
|
||||
}`;
|
||||
</script>
|
||||
|
||||
<div class="space-y-3">
|
||||
<fieldset class="space-y-2">
|
||||
<legend class="block text-sm text-text-secondary">Volume leveling</legend>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
{#each NORMALIZATION_MODES as m (m)}
|
||||
<label class={pill(normalization.value.mode === m)}>
|
||||
<input
|
||||
type="radio"
|
||||
name="normalization-mode"
|
||||
value={m}
|
||||
checked={normalization.value.mode === m}
|
||||
onchange={() => change({ mode: m })}
|
||||
class="sr-only"
|
||||
/>
|
||||
{modeLabels[m]}
|
||||
</label>
|
||||
{/each}
|
||||
</div>
|
||||
<p class="text-xs text-text-secondary">{modeHints[normalization.value.mode]}</p>
|
||||
</fieldset>
|
||||
|
||||
{#if normalization.value.mode !== 'off'}
|
||||
<fieldset class="space-y-2">
|
||||
<legend class="block text-sm text-text-secondary">Target loudness</legend>
|
||||
<div class="flex flex-wrap gap-2">
|
||||
{#each NORMALIZATION_TARGETS as t (t)}
|
||||
<label class={pill(normalization.value.target_lufs === t)}>
|
||||
<input
|
||||
type="radio"
|
||||
name="normalization-target"
|
||||
value={t}
|
||||
checked={normalization.value.target_lufs === t}
|
||||
onchange={() => change({ target_lufs: t })}
|
||||
class="sr-only"
|
||||
/>
|
||||
<span class="tabular-nums">{t} LUFS</span>
|
||||
</label>
|
||||
{/each}
|
||||
</div>
|
||||
<p class="text-xs text-text-secondary">Higher is louder; −18 leaves the most headroom.</p>
|
||||
</fieldset>
|
||||
|
||||
<label class="flex cursor-pointer items-center justify-between gap-3">
|
||||
<span>
|
||||
<span class="block text-sm text-text-primary">Boost quiet tracks fully</span>
|
||||
<span class="block text-xs text-text-secondary">A limiter catches the peaks.</span>
|
||||
</span>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={normalization.value.boost === 'limiter'}
|
||||
onchange={(e) =>
|
||||
change({ boost: (e.currentTarget as HTMLInputElement).checked ? 'limiter' : 'headroom' })}
|
||||
class="h-4 w-4 accent-accent"
|
||||
/>
|
||||
</label>
|
||||
{/if}
|
||||
</div>
|
||||
@@ -0,0 +1,65 @@
|
||||
import { afterEach, describe, expect, test, vi } from 'vitest';
|
||||
import { render, screen, fireEvent, waitFor } from '@testing-library/svelte';
|
||||
|
||||
vi.mock('$lib/api/normalization', async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import('$lib/api/normalization')>()),
|
||||
getNormalization: vi.fn(),
|
||||
putNormalization: vi.fn()
|
||||
}));
|
||||
vi.mock('$lib/stores/toast.svelte', () => ({ pushToast: vi.fn() }));
|
||||
|
||||
import NormalizationSetting from './NormalizationSetting.svelte';
|
||||
import { getNormalization, putNormalization } from '$lib/api/normalization';
|
||||
import { normalization } from '$lib/stores/normalization.svelte';
|
||||
import { pushToast } from '$lib/stores/toast.svelte';
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
describe('NormalizationSetting', () => {
|
||||
test('shows the server value and saves the whole preference on a change', async () => {
|
||||
vi.mocked(getNormalization).mockResolvedValue({ mode: 'track', target_lufs: -16, boost: 'headroom' });
|
||||
vi.mocked(putNormalization).mockImplementation(async (p) => p);
|
||||
render(NormalizationSetting);
|
||||
|
||||
await waitFor(() => expect(screen.getByRole('radio', { name: 'Track' })).toBeChecked());
|
||||
expect(screen.getByRole('radio', { name: '-16 LUFS' })).toBeChecked();
|
||||
|
||||
await fireEvent.click(screen.getByRole('radio', { name: 'Album' }));
|
||||
await waitFor(() =>
|
||||
expect(putNormalization).toHaveBeenCalledWith({ mode: 'album', target_lufs: -16, boost: 'headroom' })
|
||||
);
|
||||
await fireEvent.click(screen.getByRole('checkbox', { name: /boost quiet tracks/i }));
|
||||
await waitFor(() =>
|
||||
expect(putNormalization).toHaveBeenLastCalledWith({ mode: 'album', target_lufs: -16, boost: 'limiter' })
|
||||
);
|
||||
// The last value seen is cached for the player's first track of a session.
|
||||
expect(JSON.parse(localStorage.getItem('minstrel:normalization') ?? 'null')).toEqual({
|
||||
mode: 'album',
|
||||
target_lufs: -16,
|
||||
boost: 'limiter'
|
||||
});
|
||||
});
|
||||
|
||||
test('a failed save puts the previous choice back and says so', async () => {
|
||||
vi.mocked(getNormalization).mockResolvedValue({ mode: 'auto', target_lufs: -18, boost: 'headroom' });
|
||||
vi.mocked(putNormalization).mockRejectedValue(new Error('offline'));
|
||||
render(NormalizationSetting);
|
||||
await waitFor(() => expect(screen.getByRole('radio', { name: 'Auto' })).toBeChecked());
|
||||
|
||||
await fireEvent.click(screen.getByRole('radio', { name: 'Off' }));
|
||||
await waitFor(() => expect(pushToast).toHaveBeenCalledWith('Could not save volume leveling.', 'error'));
|
||||
expect(normalization.value.mode).toBe('auto');
|
||||
expect(screen.getByRole('radio', { name: 'Auto' })).toBeChecked();
|
||||
});
|
||||
|
||||
test('off hides the target and boost controls', async () => {
|
||||
vi.mocked(getNormalization).mockResolvedValue({ mode: 'off', target_lufs: -18, boost: 'headroom' });
|
||||
render(NormalizationSetting);
|
||||
await waitFor(() => expect(screen.getByRole('radio', { name: 'Off' })).toBeChecked());
|
||||
expect(screen.queryByRole('radio', { name: '-18 LUFS' })).toBeNull();
|
||||
expect(screen.queryByRole('checkbox')).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user