feat: previous/scrub controls, mood mix and artwork for downloads
CI / build (push) Successful in 2m16s
CI / build (push) Successful in 2m16s
Playback: - Previous button: after two seconds it restarts the current track, otherwise it restores the last played track and puts the outgoing one back in front of the queue. History is persisted in the playback snapshot. - The progress slider is draggable on every platform. Desktop previously refused to seek; it now restarts the decoder at the requested position, asking the server for a timeOffset on remote streams and skipping PCM frames for local files. Artwork: - Client downloads store the 320px cover next to the MP3 and Android sets it as MediaMetadata artwork data, so the notification and offline playback keep the cover. Deleting a download removes the image too. Moods: - New mood setting and editor slider choose the seed share of each radio batch; the server generates the rest from recommendations. Library: - Delta refresh now seeds its song map from both cached music and cached longform. Longform used to disappear after the first delta, which is why the artist list and artist playlists were empty while search still worked.
This commit is contained in:
1 parent
c0ff586686
commit
17bda94bfc
18 files changed
+390
-27
No files matched your search
+73
-6
@@ -15,7 +15,11 @@ import win.taptap.musicmanager.client.playback.AudioPlayer
|
||||
|
||||
/**
|
||||
* Desktop playback through the MP3SPI decoder and a PCM line. Pause/resume is a
|
||||
* decoder loop gate, so no audio device is reopened. Seeking is not supported.
|
||||
* decoder loop gate, so no audio device is reopened.
|
||||
*
|
||||
* Seeking restarts the decoder at the requested position. Remote streams ask
|
||||
* the server to transcode from that offset (`timeOffset`); local files skip the
|
||||
* decoded PCM frames. Both keep the reported position aligned with the song.
|
||||
*
|
||||
* Java Sound cannot open a line for the compressed MP3 format itself; the
|
||||
* decoded stream has to be converted to PCM_SIGNED first. Opening the encoded
|
||||
@@ -30,23 +34,49 @@ class DesktopAudioPlayer : AudioPlayer {
|
||||
private var worker: Thread? = null
|
||||
private val lock = Object()
|
||||
|
||||
private var currentSource: String? = null
|
||||
private var currentSong: Song? = null
|
||||
private var currentLocal = false
|
||||
private var currentArtwork: String? = null
|
||||
|
||||
@Volatile private var paused = false
|
||||
@Volatile private var stopped = false
|
||||
@Volatile private var finished = false
|
||||
@Volatile private var framesRead = 0L
|
||||
@Volatile private var baseSeconds = 0.0
|
||||
private var frameSize = 4
|
||||
private var frameRate = 44_100f
|
||||
private var duration = 0.0
|
||||
|
||||
override fun play(source: String, song: Song, local: Boolean, artworkUri: String?) {
|
||||
override fun play(
|
||||
source: String,
|
||||
song: Song,
|
||||
local: Boolean,
|
||||
artworkUri: String?,
|
||||
artworkBytes: ByteArray?,
|
||||
) {
|
||||
currentSource = source
|
||||
currentSong = song
|
||||
currentLocal = local
|
||||
currentArtwork = artworkUri
|
||||
start(source, song, local, startAtSeconds = 0.0)
|
||||
}
|
||||
|
||||
private fun start(source: String, song: Song, local: Boolean, startAtSeconds: Double) {
|
||||
stop()
|
||||
finished = false
|
||||
paused = false
|
||||
stopped = false
|
||||
framesRead = 0
|
||||
baseSeconds = startAtSeconds.coerceAtLeast(0.0)
|
||||
duration = song.duration.toDouble()
|
||||
|
||||
val raw = if (local) FileInputStream(File(source)) else URI(source).toURL().openStream()
|
||||
val requestSource = if (!local && startAtSeconds > 0) {
|
||||
withTimeOffset(source, startAtSeconds)
|
||||
} else {
|
||||
source
|
||||
}
|
||||
val raw = if (local) FileInputStream(File(requestSource)) else URI(requestSource).toURL().openStream()
|
||||
val buffered = BufferedInputStream(raw, 128 * 1024)
|
||||
input = buffered
|
||||
val encoded = AudioSystem.getAudioInputStream(buffered)
|
||||
@@ -55,6 +85,13 @@ class DesktopAudioPlayer : AudioPlayer {
|
||||
frameSize = audio.format.frameSize.takeIf { it > 0 } ?: 4
|
||||
frameRate = audio.format.frameRate.takeIf { it > 0 } ?: 44_100f
|
||||
|
||||
// A remote stream already starts at the whole-second offset; local
|
||||
// files and the fractional part are skipped in the decoded PCM.
|
||||
val skipSeconds = if (local) startAtSeconds else startAtSeconds - kotlin.math.floor(startAtSeconds)
|
||||
if (skipSeconds > 0) {
|
||||
skipPcm(audio, skipSeconds)
|
||||
}
|
||||
|
||||
val info = javax.sound.sampled.DataLine.Info(SourceDataLine::class.java, audio.format)
|
||||
val dataLine = AudioSystem.getLine(info) as SourceDataLine
|
||||
dataLine.open(audio.format)
|
||||
@@ -89,6 +126,23 @@ class DesktopAudioPlayer : AudioPlayer {
|
||||
}
|
||||
}
|
||||
|
||||
private fun skipPcm(audio: AudioInputStream, seconds: Double) {
|
||||
var remaining = (seconds * frameRate * frameSize).toLong().coerceAtLeast(0L)
|
||||
val buffer = ByteArray(16 * 1024)
|
||||
while (remaining > 0 && !stopped) {
|
||||
val skipped = runCatching { audio.skip(remaining) }.getOrDefault(0L)
|
||||
if (skipped > 0) {
|
||||
remaining -= skipped
|
||||
} else {
|
||||
val read = runCatching {
|
||||
audio.read(buffer, 0, minOf(buffer.size.toLong(), remaining).toInt())
|
||||
}.getOrDefault(-1)
|
||||
if (read <= 0) break
|
||||
remaining -= read
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun pause() {
|
||||
paused = true
|
||||
}
|
||||
@@ -122,19 +176,32 @@ class DesktopAudioPlayer : AudioPlayer {
|
||||
override fun isFinished(): Boolean = finished
|
||||
|
||||
override fun positionSeconds(): Double {
|
||||
val seconds = framesRead.toDouble() / frameSize / frameRate
|
||||
val seconds = baseSeconds + framesRead.toDouble() / frameSize / frameRate
|
||||
return seconds.coerceIn(0.0, duration.takeIf { it > 0 } ?: seconds)
|
||||
}
|
||||
|
||||
override fun durationSeconds(): Double = duration
|
||||
|
||||
override val canSeek: Boolean = false
|
||||
override val canSeek: Boolean = true
|
||||
|
||||
override fun seekTo(seconds: Double) = Unit
|
||||
override fun seekTo(seconds: Double) {
|
||||
val source = currentSource ?: return
|
||||
val song = currentSong ?: return
|
||||
val wasPaused = paused
|
||||
start(source, song, currentLocal, seconds.coerceAtLeast(0.0))
|
||||
if (wasPaused) pause()
|
||||
}
|
||||
|
||||
override fun release() {
|
||||
stop()
|
||||
}
|
||||
|
||||
/** Adds the server's `timeOffset` parameter, replacing an earlier one. */
|
||||
private fun withTimeOffset(source: String, seconds: Double): String {
|
||||
val cleaned = source.replace(Regex("[?&]timeOffset=\\d+"), "")
|
||||
val separator = if (cleaned.contains('?')) "&" else "?"
|
||||
return "$cleaned${separator}timeOffset=${seconds.toInt()}"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -49,6 +49,17 @@ class DesktopStorage : AppStorage {
|
||||
override fun downloadPath(songId: String): String? =
|
||||
File(downloads, "${safeName(songId)}.mp3").takeIf { it.isFile }?.absolutePath
|
||||
|
||||
override fun saveArtwork(songId: String, bytes: ByteArray): String? {
|
||||
if (bytes.isEmpty()) return null
|
||||
downloads.mkdirs()
|
||||
val file = File(downloads, "${safeName(songId)}.jpg")
|
||||
file.writeBytes(bytes)
|
||||
return file.absolutePath
|
||||
}
|
||||
|
||||
override fun artworkBytes(songId: String): ByteArray? =
|
||||
File(downloads, "${safeName(songId)}.jpg").takeIf { it.isFile }?.readBytes()
|
||||
|
||||
override fun listDownloads(): Map<String, Long> =
|
||||
downloads.listFiles()
|
||||
?.filter { it.isFile && it.extension == "mp3" }
|
||||
@@ -57,6 +68,7 @@ class DesktopStorage : AppStorage {
|
||||
|
||||
override fun deleteDownload(songId: String) {
|
||||
File(downloads, "${safeName(songId)}.mp3").delete()
|
||||
File(downloads, "${safeName(songId)}.jpg").delete()
|
||||
}
|
||||
|
||||
override fun storageUsedBytes(): Long =
|
||||
|
||||
Reference in new issue
Block a user