feat: mix mood radio by seed percentage and embed download artwork
CI / chart (push) Successful in 3s
CI / chart-push (push) Successful in 5s
CI / test (push) Successful in 1m50s
CI / image (push) Successful in 3m16s

Moods now store a seedPercent (0-100, default 50). RadioService rolls that
share independently for every queued slot: heads picks from the seed songs,
tails from the generated recommendation pool. When one pool is empty the other
fills the batch, so a mood never stalls. The DTO exposes both seedPercent and
the derived recommendationPercent, and create/patch accept seedPercent.

Automatic YT downloads now embed the video thumbnail as MP3 cover art so the
scanner publishes a cover for newly imported tracks.

V7 adds moods.seed_percent.
This commit is contained in:
TapTap committed 2026-11-15 17:07:04 +00:00
1 parent dd7bbde84d
commit c75f2971b4
11 files changed
+146 -6

No files matched your search

@@ -30,10 +30,11 @@ data class StartedSession(
* session are never repeated. The client asks for more whenever its buffer is
* low, so the queue is always generated with the latest signals.
*/
class RadioService {
class RadioService(
private val random: Random = Random.Default,
) {
private val log = LoggerFactory.getLogger(RadioService::class.java)
private val random = Random.Default
fun start(
mood: MoodRecord,
@@ -166,7 +167,14 @@ class RadioService {
)
}
val picks = pickDiverse(scored, limit)
val seedScored = scored.filter { (song, _) -> song.id in seedIds }
val recommendationScored = scored.filter { (song, _) -> song.id !in seedIds }
val picks = pickMixed(
seedScored = seedScored,
recommendationScored = recommendationScored,
limit = limit,
seedPercent = mood.seedPercent,
)
if (persist && sessionId != null && picks.isNotEmpty()) {
RadioRepository.append(sessionId, picks.map { it.id })
}
@@ -195,6 +203,60 @@ class RadioService {
return score
}
/**
* Picks a batch with the mood's requested seed share. Each slot rolls the
* percentage independently, so a 70% mood gets roughly seven seed tracks in
* ten. An exhausted pool falls back to the other one instead of stalling.
*/
internal fun pickMixed(
seedScored: List<Pair<SongRecord, Double>>,
recommendationScored: List<Pair<SongRecord, Double>>,
limit: Int,
seedPercent: Int,
artistSpacing: Int = 2,
albumSpacing: Int = 3,
): List<SongRecord> {
val seeds = seedScored
.sortedByDescending { it.second }
.toMutableList()
val recommendations = recommendationScored
.sortedByDescending { it.second }
.toMutableList()
val percent = seedPercent.coerceIn(0, 100)
val picked = ArrayList<SongRecord>(limit)
val artists = ArrayDeque<String>()
val albums = ArrayDeque<String>()
while (picked.size < limit && (seeds.isNotEmpty() || recommendations.isNotEmpty())) {
val preferSeed = when {
seeds.isEmpty() -> false
recommendations.isEmpty() -> true
else -> random.nextInt(100) < percent
}
val pool = if (preferSeed) seeds else recommendations
val song = pool.removeAt(diverseIndex(pool, artists, albums)).first
picked += song
artists.addLast(song.displayArtist)
if (artists.size > artistSpacing) artists.removeFirst()
albums.addLast(song.albumId)
if (albums.size > albumSpacing) albums.removeFirst()
}
return picked
}
private fun diverseIndex(
pool: List<Pair<SongRecord, Double>>,
artists: ArrayDeque<String>,
albums: ArrayDeque<String>,
): Int {
var index = pool.indexOfFirst { (song, _) ->
song.displayArtist !in artists && song.albumId !in albums
}
if (index < 0) index = pool.indexOfFirst { (song, _) -> song.displayArtist !in artists }
return if (index < 0) 0 else index
}
/**
* Picks the best remaining candidate while keeping artists and albums apart.
* An artist may return after [artistSpacing] other tracks, an album after