mirror of
https://github.com/Psychotoxical/psysonic.git
synced 2026-07-22 15:25:46 +00:00
feat(enrichment): oximedia BPM/mood facts, mood search, and queue display (#863)
* feat(enrichment): oximedia BPM/mood facts, mood search, and queue UI Run client-side oximedia analysis after CPU seed and persist BPM, mood JSON, and searchable mood_tag facts. Add product mood groups (joy/sadness/dance/work/ romance) with Advanced Search filter on the local index, queue BPM/mood display, migration 008 mood_tag index, and refreshed licenses for oximedia crates. * fix(enrichment): keep mood_groups module comment in English * feat(search): virtual mood groups, anger filter, and Advanced Search UX Expand mood search via overlapping virtual groups (tag expansion only), add anger/Злость group, skip album/artist shortcuts for track-only filters, and simplify mood search UI (songs-only, hide type tabs). Fix CustomSelect spurious scrollbar on short option lists. * feat(analysis): unified track analysis plan and enqueue path Add TrackAnalysisPlan (waveform, LUFS, enrichment) with a single enqueue_track_analysis entry for all byte-backed triggers. Run enrichment when cache is full but library facts are missing; route playback, cache, and backfill through the planner. Fix browseTextSearch LocalSearchOpts tsc gap and remove obsolete read_seed_bytes_if_needed helper. * fix(analysis): wire playback dispatch, preload enrichment, and UI refresh Route stream, gapless, preload, and local-file playback through analysis_dispatch so BPM/mood enrichment runs when waveform/LUFS are already cached. Fix audio_preload cache-hit and hot-cache paths, emit preload-cancelled for retry, and add analysis:enrichment-updated plus content_cache_coverage key resolution. * fix(audio): preload local files from disk and stop analysis retry loop Seed hot/offline next tracks via LocalFilePlayback (512 MiB) instead of copying into the RAM preload slot. Keep bytePreloadingId set after preload-ready so progress ticks do not re-invoke audio_preload every second. * fix(enrichment): clippy, album bpm filter routing, and queue mood display Clippy-clean analysis_dispatch and engine imports; restrict track-derived album routing to mood_group/mood_tag only so bpm is skipped on album queries. Log enrichment plan errors with retry-all plan; filter queue mood labels to oximedia ids. * fix(enrichment): simplify mood_tag backfill branch in plan_track_enrichment Remove empty if-block; keep same behaviour when backfill fails and moods row exists. * docs: CHANGELOG and credits for track enrichment PR #863 * docs(credits): track enrichment PR #863 contributor line * chore(enrichment): clippy-clean plan branch and trim dead exports Collapse mood_tag backfill if for clippy; remove unused moodGroupById and OXIMEDIA_MOOD_LABELS re-exports; stop poll when server BPM is already known. * fix(enrichment): close R2/S1–S3 limits and Song Info BPM fallback Return TrackEnrichmentOutcome::Failed on oximedia errors so retries are not masked as complete; extract mood Advanced Search SQL, unify top-3 mood tag selection in mood_groups with TS invariant tests, and show measured BPM in Song Info when tag BPM is missing or zero. * fix(enrichment): restore offline coverage and show mood in Song Info Add unit tests for offline download cancel/clear registry after the analysis seed refactor dropped read_seed_bytes coverage; show localized mood labels in Song Info when library enrichment facts exist. * fix(enrichment): soft mood scoring and unblock offline cancel tests Replace oximedia quadrant happy/excited mapping with valence/arousal soft scores across all mood tags for display, storage, and backfill; fix offline cancel unit tests that deadlocked by calling clear while holding the global offline_cancel_flags mutex. * fix(enrichment): dedupe joy cluster and cap mood display at two labels Never show happy and excited together; pick one tag per V/A cluster, tighten oximedia recalibration, and limit queue/Song Info to two moods that pass a relative score floor. * fix(enrichment): disable oximedia mood labels in UI and search tags Oximedia 0.1.7 mood is a spectral energy heuristic, not independent mood weights; valence correlates with loud/bright audio and false-labels metal and lyrical tracks as happy. Hide queue/Song Info mood and stop writing mood_tag facts until a reliable detector lands; keep V/A/moods JSON stored. * fix(enrichment): disable oximedia mood analysis and add BPM advanced search Stop planning, running, and storing oximedia mood facts; purge accumulated mood rows via migration 009. Hide mood filters in Advanced Search, expose BPM range filter with dual-storage resolution, and show a BPM column in song results when that filter is active. * feat(search): analysis BPM priority, source tooltip, and filter UX Prefer analysis track_fact over file tags for BPM resolution; show source in list tooltips. Validate BPM range on blur, add clear button, fix double tooltip. * fix(enrichment): prefer analysis BPM in Song Info and queue tech row Show measured track_fact BPM before file tags until analysis completes; pick the highest-confidence analysis fact when several exist.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
MOOD_GROUP_IDS,
|
||||
MOOD_GROUPS,
|
||||
OXIMEDIA_MOOD_TAG_IDS,
|
||||
TOP_OXIMEDIA_MOOD_TAG_TEST_SCORES,
|
||||
moodScoresFromValenceArousal,
|
||||
topDistinctOximediaMoodTagIds,
|
||||
topDistinctOximediaMoodTagIdsFromValenceArousal,
|
||||
topOximediaMoodTagIds,
|
||||
} from './moodGroups';
|
||||
|
||||
/** Keep in sync with `psysonic_library::mood_groups` invariant tests. */
|
||||
describe('moodGroups catalog invariants', () => {
|
||||
it('group ids match Rust MOOD_GROUP_IDS', () => {
|
||||
expect([...MOOD_GROUP_IDS]).toEqual(['joy', 'sadness', 'dance', 'work', 'romance', 'anger']);
|
||||
});
|
||||
|
||||
it('every oximedia tag appears in at least one group', () => {
|
||||
for (const tag of OXIMEDIA_MOOD_TAG_IDS) {
|
||||
expect(MOOD_GROUPS.some(g => g.tags.includes(tag))).toBe(true);
|
||||
}
|
||||
});
|
||||
|
||||
it('joy group tags match Rust expand_mood_groups', () => {
|
||||
expect(MOOD_GROUPS.find(g => g.id === 'joy')?.tags).toEqual(['happy', 'excited']);
|
||||
});
|
||||
|
||||
it('work and romance groups overlap on calm/peaceful', () => {
|
||||
const work = MOOD_GROUPS.find(g => g.id === 'work')!.tags;
|
||||
const romance = MOOD_GROUPS.find(g => g.id === 'romance')!.tags;
|
||||
expect(work.some(t => romance.includes(t))).toBe(true);
|
||||
});
|
||||
|
||||
it('anger group tags match Rust expand_mood_groups', () => {
|
||||
expect(MOOD_GROUPS.find(g => g.id === 'anger')?.tags).toEqual(['angry', 'tense']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('moodScoresFromValenceArousal', () => {
|
||||
it('never shows both happy and excited for typical oximedia pop', () => {
|
||||
const labels = topDistinctOximediaMoodTagIdsFromValenceArousal(0.4, 0.75);
|
||||
expect(labels.includes('happy') && labels.includes('excited')).toBe(false);
|
||||
expect(labels.length).toBeLessThanOrEqual(2);
|
||||
});
|
||||
|
||||
it('prefers calm or peaceful for low arousal', () => {
|
||||
const labels = topDistinctOximediaMoodTagIdsFromValenceArousal(0.55, 0.42);
|
||||
expect(labels.some(id => id === 'calm' || id === 'peaceful')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('topOximediaMoodTagIds', () => {
|
||||
it('matches Rust top_oximedia_mood_tag_ids_from_moods_json test vector', () => {
|
||||
expect(topOximediaMoodTagIds(TOP_OXIMEDIA_MOOD_TAG_TEST_SCORES)).toEqual([
|
||||
'happy',
|
||||
'excited',
|
||||
'calm',
|
||||
]);
|
||||
});
|
||||
|
||||
it('sorts by score descending with id tie-break', () => {
|
||||
expect(topOximediaMoodTagIds({ calm: 0.2, happy: 0.9, excited: 0.5 })).toEqual([
|
||||
'happy',
|
||||
'excited',
|
||||
'calm',
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
/** Oximedia mood label ids — keep in sync with `psysonic_library::mood_groups` (see `moodGroups.test.ts`). */
|
||||
export const OXIMEDIA_MOOD_TAG_IDS = [
|
||||
'happy',
|
||||
'excited',
|
||||
'calm',
|
||||
'peaceful',
|
||||
'angry',
|
||||
'tense',
|
||||
'sad',
|
||||
'melancholic',
|
||||
] as const;
|
||||
|
||||
export type OximediaMoodTagId = (typeof OXIMEDIA_MOOD_TAG_IDS)[number];
|
||||
|
||||
export type MoodGroupId = 'joy' | 'sadness' | 'dance' | 'work' | 'romance' | 'anger';
|
||||
|
||||
/** Virtual mood groups for Advanced Search — overlaps are intentional. */
|
||||
export const MOOD_GROUPS: ReadonlyArray<{
|
||||
readonly id: MoodGroupId;
|
||||
readonly tags: readonly string[];
|
||||
}> = [
|
||||
{ id: 'joy', tags: ['happy', 'excited'] },
|
||||
{ id: 'sadness', tags: ['sad', 'melancholic'] },
|
||||
{ id: 'dance', tags: ['excited', 'happy', 'tense', 'angry'] },
|
||||
{ id: 'work', tags: ['calm', 'peaceful'] },
|
||||
{ id: 'romance', tags: ['peaceful', 'calm', 'melancholic'] },
|
||||
{ id: 'anger', tags: ['angry', 'tense'] },
|
||||
] as const;
|
||||
|
||||
export const MOOD_GROUP_IDS: readonly MoodGroupId[] = MOOD_GROUPS.map(g => g.id);
|
||||
|
||||
/** Valence/arousal anchor — keep in sync with Rust `mood_groups::MOOD_VA_ANCHORS`. */
|
||||
const MOOD_VA_ANCHORS: ReadonlyArray<{ readonly id: OximediaMoodTagId; readonly v: number; readonly a: number }> = [
|
||||
{ id: 'happy', v: 0.75, a: 0.72 },
|
||||
{ id: 'excited', v: 0.55, a: 0.88 },
|
||||
{ id: 'calm', v: 0.65, a: 0.22 },
|
||||
{ id: 'peaceful', v: 0.78, a: 0.12 },
|
||||
{ id: 'angry', v: -0.72, a: 0.82 },
|
||||
{ id: 'tense', v: -0.35, a: 0.68 },
|
||||
{ id: 'sad', v: -0.75, a: 0.28 },
|
||||
{ id: 'melancholic', v: -0.55, a: 0.18 },
|
||||
] as const;
|
||||
|
||||
const MOOD_VA_MAX_DIST = 1.35;
|
||||
const MOOD_VA_VALENCE_BIAS = 0.12;
|
||||
const MOOD_VA_VALENCE_SCALE = 1.4;
|
||||
const MOOD_VA_AROUSAL_OFFSET = 0.48;
|
||||
const MOOD_VA_AROUSAL_SCALE = 0.40;
|
||||
const MOOD_DISPLAY_MIN_RELATIVE = 0.55;
|
||||
const MOOD_DISPLAY_MIN_ABSOLUTE = 0.28;
|
||||
|
||||
/** One label per cluster in UI — mirrors Rust `MOOD_DISPLAY_CLUSTERS`. */
|
||||
const MOOD_DISPLAY_CLUSTERS: readonly (readonly OximediaMoodTagId[])[] = [
|
||||
['happy', 'excited'],
|
||||
['calm', 'peaceful'],
|
||||
['angry', 'tense'],
|
||||
['sad', 'melancholic'],
|
||||
] as const;
|
||||
|
||||
function moodDisplayCluster(tag: string): number | null {
|
||||
const idx = MOOD_DISPLAY_CLUSTERS.findIndex(cluster => cluster.includes(tag as OximediaMoodTagId));
|
||||
return idx >= 0 ? idx : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Soft scores for all oximedia mood tags from raw valence/arousal.
|
||||
* Oximedia's built-in mapper returns only two labels (often happy/excited);
|
||||
* we recalibrate V/A and score every catalog tag by distance to anchors.
|
||||
*/
|
||||
export function moodScoresFromValenceArousal(valence: number, arousal: number): Record<string, number> {
|
||||
const v = Math.max(-1, Math.min(1, (valence - MOOD_VA_VALENCE_BIAS) * MOOD_VA_VALENCE_SCALE));
|
||||
const a = Math.max(0, Math.min(1, (arousal - MOOD_VA_AROUSAL_OFFSET) / MOOD_VA_AROUSAL_SCALE));
|
||||
const scores: Record<string, number> = {};
|
||||
for (const anchor of MOOD_VA_ANCHORS) {
|
||||
const dv = v - anchor.v;
|
||||
const da = a - anchor.a;
|
||||
const dist = Math.sqrt(dv * dv + da * da);
|
||||
scores[anchor.id] = Math.max(0, 1 - dist / MOOD_VA_MAX_DIST);
|
||||
}
|
||||
return scores;
|
||||
}
|
||||
|
||||
/** Shared test vector with Rust `mood_groups::top_oximedia_mood_tag_ids_from_moods_json`. */
|
||||
export const TOP_OXIMEDIA_MOOD_TAG_TEST_SCORES = {
|
||||
noise: 0.99,
|
||||
calm: 0.2,
|
||||
happy: 0.9,
|
||||
excited: 0.5,
|
||||
} as const;
|
||||
|
||||
/** Top oximedia mood tag ids by score — mirrors Rust `top_oximedia_mood_tag_ids_from_scores`. */
|
||||
export function topOximediaMoodTagIds(
|
||||
scores: Record<string, number> | null | undefined,
|
||||
limit = 3,
|
||||
): string[] {
|
||||
if (!scores) return [];
|
||||
const allowed = new Set<string>(OXIMEDIA_MOOD_TAG_IDS);
|
||||
return Object.entries(scores)
|
||||
.filter(([id]) => allowed.has(id))
|
||||
.sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))
|
||||
.slice(0, limit)
|
||||
.map(([id]) => id);
|
||||
}
|
||||
|
||||
/** One tag per display cluster (never happy+excited together). Mirrors Rust distinct picker. */
|
||||
export function topDistinctOximediaMoodTagIds(
|
||||
scores: Record<string, number> | null | undefined,
|
||||
limit = 2,
|
||||
): string[] {
|
||||
if (!scores) return [];
|
||||
const ranked = topOximediaMoodTagIds(scores, OXIMEDIA_MOOD_TAG_IDS.length);
|
||||
if (ranked.length === 0) return [];
|
||||
const topScore = scores[ranked[0]] ?? 0;
|
||||
const usedClusters = new Set<number>();
|
||||
const out: string[] = [];
|
||||
for (const id of ranked) {
|
||||
const score = scores[id] ?? 0;
|
||||
if (score < MOOD_DISPLAY_MIN_ABSOLUTE || score < topScore * MOOD_DISPLAY_MIN_RELATIVE) continue;
|
||||
const cluster = moodDisplayCluster(id);
|
||||
if (cluster != null) {
|
||||
if (usedClusters.has(cluster)) continue;
|
||||
usedClusters.add(cluster);
|
||||
}
|
||||
out.push(id);
|
||||
if (out.length >= limit) break;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function topDistinctOximediaMoodTagIdsFromValenceArousal(
|
||||
valence: number,
|
||||
arousal: number,
|
||||
limit = 2,
|
||||
): string[] {
|
||||
return topDistinctOximediaMoodTagIds(moodScoresFromValenceArousal(valence, arousal), limit);
|
||||
}
|
||||
|
||||
/** Dedupe a stored tag list to one label per cluster (preserves rank order). */
|
||||
export function distinctOximediaMoodTagIds(tags: readonly string[], limit = 2): string[] {
|
||||
const scores = Object.fromEntries(tags.map((id, index) => [id, tags.length - index]));
|
||||
return topDistinctOximediaMoodTagIds(scores, limit);
|
||||
}
|
||||
@@ -127,6 +127,7 @@ const CONTRIBUTOR_ENTRIES = [
|
||||
'Player stats: local listening history tab with heatmap, year summary, recent days, and day drill-down (PR #849)',
|
||||
'Playback speed: global 0.5–2.0× with Speed / Varispeed / Pitch shift strategies, player bar popover, Orbit passthrough (PR #852)',
|
||||
'Local library index: full resync orphan sweep (IS-7) — remove server-deleted tracks after successful re-sync (PR #861)',
|
||||
'Track enrichment: oximedia BPM/mood analysis, mood-group Advanced Search, queue display, unified playback analysis dispatch (PR #863)',
|
||||
],
|
||||
},
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user