feat: discover YT recommendations and tune the radio
yt-dlp now feeds mood discovery. Queries come from the free-text hint, seed artists and genres; they run in parallel, are filtered to music-length videos, matched against library songs by normalized title and artist, scored, and stored as review candidates. Refresh, accept and reject endpoints are added: accepting a library match adds a seed immediately, while accepted tracks that are not owned wait for the future downloader. Decisions survive refreshes and stale undecided candidates are pruned. A background pass refreshes stale moods every hour, and the image ships a pinned yt-dlp binary (YTDLP_PATH, also added to the Nix shell and chart). Radio now avoids tracks played in the last 7 days when the pool is large enough, spaces artists and albums across a batch, and weighs skips three times as heavily as completions. The chart moves to 0.2.2.
This commit is contained in:
1 parent
67eed8d262
commit
c5aa000d1a
18 files changed
+1061
-37
No files matched your search
@@ -1,5 +1,7 @@
|
||||
package win.taptap.musicmanager.radio
|
||||
|
||||
import java.time.Instant
|
||||
import java.time.temporal.ChronoUnit
|
||||
import java.util.UUID
|
||||
import kotlin.math.max
|
||||
import kotlin.random.Random
|
||||
@@ -140,15 +142,27 @@ class RadioService {
|
||||
}
|
||||
}
|
||||
|
||||
val recent = RadioRepository.recentSongIds(
|
||||
user.name,
|
||||
Instant.now().minus(RECENT_DAYS, ChronoUnit.DAYS),
|
||||
)
|
||||
// In a normal-sized library, skip songs from the last week entirely.
|
||||
// Small libraries keep them as low-scored fallbacks instead of going silent.
|
||||
if (candidates.size > limit * 3) {
|
||||
candidates.keys.removeAll(recent - seedIds)
|
||||
}
|
||||
|
||||
val affinity = RadioRepository.affinity(user.name, candidates.keys)
|
||||
val scored = candidates.values.map { song ->
|
||||
song to score(
|
||||
song to scoreCandidate(
|
||||
song = song,
|
||||
seedIds = seedIds,
|
||||
seedGenres = seedGenres,
|
||||
seedArtistIds = seedArtistIds,
|
||||
query = query,
|
||||
affinity = affinity[song.id]?.score ?: 0.0,
|
||||
recent = recent,
|
||||
jitter = random.nextDouble(0.0, 2.0),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -159,50 +173,69 @@ class RadioService {
|
||||
return picks
|
||||
}
|
||||
|
||||
private fun score(
|
||||
internal fun scoreCandidate(
|
||||
song: SongRecord,
|
||||
seedIds: Set<String>,
|
||||
seedGenres: List<String>,
|
||||
seedArtistIds: List<String>,
|
||||
query: String?,
|
||||
affinity: Double,
|
||||
recent: Set<String> = emptySet(),
|
||||
jitter: Double = 0.0,
|
||||
): Double {
|
||||
var score = random.nextDouble(0.0, 2.0)
|
||||
var score = jitter
|
||||
if (song.id in seedIds) score += 10.0
|
||||
if (song.artistId != null && song.artistId in seedArtistIds) score += 3.0
|
||||
score += song.genres.count { candidate ->
|
||||
seedGenres.any { it.equals(candidate, ignoreCase = true) }
|
||||
} * 1.5
|
||||
if (query != null && song.title.contains(query, ignoreCase = true)) score += 2.0
|
||||
score += affinity.coerceIn(-6.0, 6.0)
|
||||
score += affinity.coerceIn(-8.0, 8.0)
|
||||
if (song.id in recent) score -= RECENT_PENALTY
|
||||
return score
|
||||
}
|
||||
|
||||
/**
|
||||
* Picks the best remaining candidate, preferring a different artist than the
|
||||
* previous track so a batch does not become one album on repeat.
|
||||
* Picks the best remaining candidate while keeping artists and albums apart.
|
||||
* An artist may return after [artistSpacing] other tracks, an album after
|
||||
* [albumSpacing]. Fallbacks relax the constraints rather than stalling.
|
||||
*/
|
||||
private fun pickDiverse(
|
||||
internal fun pickDiverse(
|
||||
scored: List<Pair<SongRecord, Double>>,
|
||||
limit: Int,
|
||||
artistSpacing: Int = 2,
|
||||
albumSpacing: Int = 3,
|
||||
): List<SongRecord> {
|
||||
val remaining = scored
|
||||
.sortedWith(compareByDescending<Pair<SongRecord, Double>> { it.second })
|
||||
.toMutableList()
|
||||
val picked = ArrayList<SongRecord>(limit)
|
||||
var lastArtist: String? = null
|
||||
val artists = ArrayDeque<String>()
|
||||
val albums = ArrayDeque<String>()
|
||||
|
||||
while (picked.size < limit && remaining.isNotEmpty()) {
|
||||
val different = remaining.indexOfFirst { it.first.displayArtist != lastArtist }
|
||||
val index = if (different >= 0) different else 0
|
||||
var index = remaining.indexOfFirst { (song, _) ->
|
||||
song.displayArtist !in artists && song.albumId !in albums
|
||||
}
|
||||
if (index < 0) {
|
||||
index = remaining.indexOfFirst { (song, _) -> song.displayArtist !in artists }
|
||||
}
|
||||
if (index < 0) index = 0
|
||||
|
||||
val song = remaining.removeAt(index).first
|
||||
picked += song
|
||||
lastArtist = song.displayArtist
|
||||
|
||||
artists.addLast(song.displayArtist)
|
||||
if (artists.size > artistSpacing) artists.removeFirst()
|
||||
albums.addLast(song.albumId)
|
||||
if (albums.size > albumSpacing) albums.removeFirst()
|
||||
}
|
||||
return picked
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val POOL_LIMIT = 400
|
||||
const val RECENT_DAYS = 7L
|
||||
const val RECENT_PENALTY = 6.0
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user