Files
Lyra/worker/lyra_worker/quality.py
T
2026-07-10 18:22:47 +02:00

24 lines
737 B
Python

from lyra_worker.types import Candidate, Quality
_SOURCE_TIERS = {"qobuz": 0, "soulseek": 1, "youtube": 2}
def quality_class(q: Quality) -> int:
"""3 = hi-res lossless, 2 = CD lossless, 1 = lossy."""
if q.lossless:
hires = (q.bit_depth is not None and q.bit_depth > 16) or (
q.sample_rate is not None and q.sample_rate > 44100
)
return 3 if hires else 2
return 1
def source_tier(source: str) -> int:
"""Lower is better. Unknown sources rank worst."""
return _SOURCE_TIERS.get(source, 99)
def rank_key(c: Candidate) -> tuple[int, int]:
"""Sort key; larger is better. Quality class first, then source tier."""
return (quality_class(c.quality), -source_tier(c.source))