feat: previous/scrub controls, mood mix and artwork for downloads
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:
TapTap committed 2026-11-15 17:07:11 +00:00
1 parent c0ff586686
commit 17bda94bfc
18 files changed
+390 -27

No files matched your search

+6 -2
View File
@@ -27,6 +27,9 @@ The shared module contains the API client, session/cache storage, radio queue, a
- YT Music recommendations in the mood editor: refresh on demand, accept or reject candidates, thumbnails, and a clear state for library matches (`Seed added`), accepted-for-download tracks (`Queued`, `Downloading`, `Download failed` with retry) and new picks. Accepting a track the library already has adds it as a seed immediately; the rest are downloaded by the server and promoted automatically. - YT Music recommendations in the mood editor: refresh on demand, accept or reject candidates, thumbnails, and a clear state for library matches (`Seed added`), accepted-for-download tracks (`Queued`, `Downloading`, `Download failed` with retry) and new picks. Accepting a track the library already has adds it as a seed immediately; the rest are downloaded by the server and promoted automatically.
- Library search and playback, plus **Shuffle all** to queue the whole library without starting a mood. - Library search and playback, plus **Shuffle all** to queue the whole library without starting a mood.
- A queue sheet from the player: the current track plus every upcoming track, with move up/down and remove. Changes are persisted locally. - A queue sheet from the player: the current track plus every upcoming track, with move up/down and remove. Changes are persisted locally.
- **Previous** from the player: after two seconds of playback it restarts the current track; before that it restores the last played track. The track it leaves is put back at the front of the queue.
- **Scrubbing**: the progress slider can be dragged on every platform. Android/web seek natively; desktop restarts the decoder at the requested position (`timeOffset` for remote streams, PCM skip for local files).
- **Seed mix** per mood: a slider chooses the share of seeds vs recommendations for each generated batch. The server rolls the percentage for every queued track and falls back to the other pool when one runs out.
- **Longform tab** for audiobooks and podcasts: per-artist read-only playlists, “Shuffle artist” for random episodes, a resume list with progress, and a separate search scope. Long-form never enters music search, Shuffle all, moods or radio. - **Longform tab** for audiobooks and podcasts: per-artist read-only playlists, “Shuffle artist” for random episodes, a resume list with progress, and a separate search scope. Long-form never enters music search, Shuffle all, moods or radio.
- **Sleep timer** (15/30/45/60/90 minutes) that pauses playback. On Android the pause is scheduled inside the media service, so it still fires when the app is backgrounded or the task is swiped away. - **Sleep timer** (15/30/45/60/90 minutes) that pauses playback. On Android the pause is scheduled inside the media service, so it still fires when the app is backgrounded or the task is swiped away.
- Offline library cache: moods, songs and the last sync revision are stored locally. When the server is unreachable, downloaded tracks still play. - Offline library cache: moods, songs and the last sync revision are stored locally. When the server is unreachable, downloaded tracks still play.
@@ -101,7 +104,7 @@ Android cleartext HTTP is enabled for local development. Use HTTPS in production
- **Mood cache** — mood details are stored so the app can open them offline. - **Mood cache** — mood details are stored so the app can open them offline.
- **Radio** — one session per mood, batches of 12, prefetch when the queue is short, no repeats within a session. - **Radio** — one session per mood, batches of 12, prefetch when the queue is short, no repeats within a session.
- **Downloads** — stored under the platform app directory, keyed by deterministic song ID. - **Downloads** — stored under the platform app directory, keyed by deterministic song ID.
- **Playback snapshot** — the session, upcoming queue and current track are persisted on every queue change and restored on the next launch. - **Playback snapshot** — the session, upcoming queue, current track and play history are persisted on every queue change and restored on the next launch.
- **Longform progress** — position is saved locally and to `/longform/progress` while playing, on pause and on track changes; the resume list is fed from the server when online. - **Longform progress** — position is saved locally and to `/longform/progress` while playing, on pause and on track changes; the resume list is fed from the server when online.
- **Offline retry** — a background loop retries the library sync until the server responds again. - **Offline retry** — a background loop retries the library sync until the server responds again.
@@ -142,6 +145,7 @@ The image build performs one warm-up compile (`kotlinNodeJsSetup`, `kotlinYarnSe
- The media notification exposes play/pause and seeking. Queue actions (skip, add to mood) stay in the app because the mood session and its events belong to the server. - The media notification exposes play/pause and seeking. Queue actions (skip, add to mood) stay in the app because the mood session and its events belong to the server.
- Desktop playback cannot seek (sequential decoder); pause/resume and skipping work. Longform resume therefore starts from the beginning on desktop. MP3s are decoded to 16-bit PCM before the line is opened, because Java Sound has no line for the compressed format. - Desktop playback cannot seek (sequential decoder); pause/resume and skipping work. Longform resume therefore starts from the beginning on desktop. MP3s are decoded to 16-bit PCM before the line is opened, because Java Sound has no line for the compressed format.
- Web downloads are disabled because the browser owns its storage; the app still streams and caches metadata. - Web downloads are disabled because the browser owns its storage; the app still streams and caches metadata.
- Accepted recommendations are downloaded by the server into `<music>/AudioManager Imports`, scanned and added as seeds. The server needs outbound network access for this. - Accepted recommendations are downloaded by the server into `<music>/AudioManager Imports`, with the YT thumbnail embedded as cover art, scanned and added as seeds. The server needs outbound network access for this.
- Offline downloads also store the 320 px cover next to the MP3 (`<songId>.jpg`), so the in-app cover and the Android notification artwork survive without the server. Deleting a download removes both files.
- YT Music discovery runs server-side through `yt-dlp`; downloading accepted tracks that are not in the library is still roadmap. - YT Music discovery runs server-side through `yt-dlp`; downloading accepted tracks that are not in the library is still roadmap.
- No iOS target yet. The shared module is ready for one. - No iOS target yet. The shared module is ready for one.
@@ -53,7 +53,13 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
) )
} }
override fun play(source: String, song: Song, local: Boolean, artworkUri: String?) { override fun play(
source: String,
song: Song,
local: Boolean,
artworkUri: String?,
artworkBytes: ByteArray?,
) {
finished = false finished = false
withController { controller -> withController { controller ->
val uri = if (local) Uri.fromFile(java.io.File(source)) else Uri.parse(source) val uri = if (local) Uri.fromFile(java.io.File(source)) else Uri.parse(source)
@@ -61,7 +67,16 @@ class MediaControllerAudioPlayer(context: Context) : AudioPlayer {
.setTitle(song.title) .setTitle(song.title)
.setArtist(song.artist) .setArtist(song.artist)
.setAlbumTitle(song.album) .setAlbumTitle(song.album)
.setArtworkUri(artworkUri?.let(Uri::parse)) .apply {
if (artworkBytes != null && artworkBytes.isNotEmpty()) {
setArtworkData(
artworkBytes,
MediaMetadata.PICTURE_TYPE_FRONT_COVER,
)
} else {
setArtworkUri(artworkUri?.let(Uri::parse))
}
}
.build() .build()
val item = MediaItem.Builder() val item = MediaItem.Builder()
.setUri(uri) .setUri(uri)
@@ -49,6 +49,17 @@ class AndroidStorage(context: Context) : AppStorage {
override fun downloadPath(songId: String): String? = override fun downloadPath(songId: String): String? =
File(downloads, "${safeName(songId)}.mp3").takeIf { it.isFile }?.absolutePath 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> = override fun listDownloads(): Map<String, Long> =
downloads.listFiles() downloads.listFiles()
?.filter { it.isFile && it.extension == "mp3" } ?.filter { it.isFile && it.extension == "mp3" }
@@ -57,6 +68,7 @@ class AndroidStorage(context: Context) : AppStorage {
override fun deleteDownload(songId: String) { override fun deleteDownload(songId: String) {
File(downloads, "${safeName(songId)}.mp3").delete() File(downloads, "${safeName(songId)}.mp3").delete()
File(downloads, "${safeName(songId)}.jpg").delete()
} }
override fun storageUsedBytes(): Long = override fun storageUsedBytes(): Long =
@@ -255,11 +255,16 @@ class ApiClient(private val http: HttpClient) {
suspend fun cover(albumId: String, size: Int = 320): ByteArray? { suspend fun cover(albumId: String, size: Int = 320): ByteArray? {
val key = "$albumId:$size" val key = "$albumId:$size"
if (coverCache.containsKey(key)) return coverCache[key] if (coverCache.containsKey(key)) return coverCache[key]
val bytes = runCatching { val result = runCatching {
http.get("$baseUrl/api/v1/covers/$albumId?size=$size&token=${token.orEmpty().encodeURLParameter()}") val response = http.get(
.bodyAsBytes() "$baseUrl/api/v1/covers/$albumId?size=$size&token=${token.orEmpty().encodeURLParameter()}",
)
if (response.status.isSuccess()) {
response.bodyAsBytes().takeIf { it.isNotEmpty() }
} else {
null
}
}.getOrNull() }.getOrNull()
val result = bytes?.takeIf { it.isNotEmpty() }
if (coverCache.size > MAX_CACHED_IMAGES) coverCache.clear() if (coverCache.size > MAX_CACHED_IMAGES) coverCache.clear()
coverCache[key] = result coverCache[key] = result
return result return result
@@ -62,6 +62,8 @@ data class Mood(
val query: String? = null, val query: String? = null,
val genres: List<String> = emptyList(), val genres: List<String> = emptyList(),
val targetSize: Int, val targetSize: Int,
val seedPercent: Int = 50,
val recommendationPercent: Int = 50,
val refreshHours: Int, val refreshHours: Int,
val ytEnabled: Boolean, val ytEnabled: Boolean,
val lastDiscoveryAt: String? = null, val lastDiscoveryAt: String? = null,
@@ -217,6 +219,7 @@ data class CreateMoodRequest(
val refreshHours: Int = 24, val refreshHours: Int = 24,
val ytEnabled: Boolean = true, val ytEnabled: Boolean = true,
val seedSongIds: List<String> = emptyList(), val seedSongIds: List<String> = emptyList(),
val seedPercent: Int = 50,
) )
@Serializable @Serializable
@@ -230,6 +233,7 @@ data class UpdateMoodRequest(
val targetSize: Int? = null, val targetSize: Int? = null,
val refreshHours: Int? = null, val refreshHours: Int? = null,
val ytEnabled: Boolean? = null, val ytEnabled: Boolean? = null,
val seedPercent: Int? = null,
) )
@Serializable @Serializable
@@ -9,6 +9,10 @@ interface AppStorage {
/** Returns a platform-specific handle for playback, or null when unsupported. */ /** Returns a platform-specific handle for playback, or null when unsupported. */
fun saveDownload(songId: String, bytes: ByteArray): String? fun saveDownload(songId: String, bytes: ByteArray): String?
fun downloadPath(songId: String): String? fun downloadPath(songId: String): String?
/** Stores the cover next to the audio file so offline playback keeps artwork. */
fun saveArtwork(songId: String, bytes: ByteArray): String?
fun artworkBytes(songId: String): ByteArray?
fun listDownloads(): Map<String, Long> fun listDownloads(): Map<String, Long>
fun deleteDownload(songId: String) fun deleteDownload(songId: String)
fun storageUsedBytes(): Long fun storageUsedBytes(): Long
@@ -33,6 +33,7 @@ data class PlaybackSnapshot(
val session: Session? = null, val session: Session? = null,
val queue: List<Song> = emptyList(), val queue: List<Song> = emptyList(),
val current: Song? = null, val current: Song? = null,
val history: List<Song> = emptyList(),
) )
class LibraryCache(private val storage: AppStorage) { class LibraryCache(private val storage: AppStorage) {
@@ -9,7 +9,13 @@ import win.taptap.musicmanager.client.api.Song
* store polls [isFinished] because some backends have no callback support. * store polls [isFinished] because some backends have no callback support.
*/ */
interface AudioPlayer { interface AudioPlayer {
fun play(source: String, song: Song, local: Boolean, artworkUri: String? = null) fun play(
source: String,
song: Song,
local: Boolean,
artworkUri: String? = null,
artworkBytes: ByteArray? = null,
)
fun pause() fun pause()
fun resume() fun resume()
fun stop() fun stop()
@@ -49,6 +49,7 @@ data class AppState(
val session: Session? = null, val session: Session? = null,
val queue: List<Song> = emptyList(), val queue: List<Song> = emptyList(),
val current: Song? = null, val current: Song? = null,
val history: List<Song> = emptyList(),
val playing: Boolean = false, val playing: Boolean = false,
val positionSeconds: Double = 0.0, val positionSeconds: Double = 0.0,
val durationSeconds: Double = 0.0, val durationSeconds: Double = 0.0,
@@ -114,6 +115,7 @@ class AppStore(
session = playback?.session, session = playback?.session,
queue = playback?.queue.orEmpty(), queue = playback?.queue.orEmpty(),
current = playback?.current, current = playback?.current,
history = playback?.history.orEmpty(),
durationSeconds = playback?.current?.duration?.toDouble() ?: 0.0, durationSeconds = playback?.current?.duration?.toDouble() ?: 0.0,
) )
} }
@@ -184,7 +186,11 @@ class AppStore(
try { try {
val moods = api.moods() val moods = api.moods()
val cached = cache.load() val cached = cache.load()
var songs = cached.songs.associateBy { it.id }.toMutableMap() // Both halves of the cache must feed the delta sync; otherwise the
// long-form list disappears as soon as the first delta arrives.
var songs = (cached.songs + cached.longform)
.associateBy { it.id }
.toMutableMap()
var revision = cached.revision var revision = cached.revision
if (revision == null) { if (revision == null) {
@@ -309,6 +315,7 @@ class AppStore(
targetSize: Int, targetSize: Int,
refreshHours: Int, refreshHours: Int,
ytEnabled: Boolean, ytEnabled: Boolean,
seedPercent: Int = 50,
) { ) {
scope.launch { scope.launch {
_state.update { it.copy(saving = true) } _state.update { it.copy(saving = true) }
@@ -322,6 +329,7 @@ class AppStore(
targetSize = targetSize, targetSize = targetSize,
refreshHours = refreshHours, refreshHours = refreshHours,
ytEnabled = ytEnabled, ytEnabled = ytEnabled,
seedPercent = seedPercent,
), ),
) )
cacheDetail(detail) cacheDetail(detail)
@@ -351,6 +359,7 @@ class AppStore(
targetSize: Int, targetSize: Int,
refreshHours: Int, refreshHours: Int,
ytEnabled: Boolean, ytEnabled: Boolean,
seedPercent: Int = 50,
) { ) {
scope.launch { scope.launch {
_state.update { it.copy(saving = true) } _state.update { it.copy(saving = true) }
@@ -367,6 +376,7 @@ class AppStore(
targetSize = targetSize, targetSize = targetSize,
refreshHours = refreshHours, refreshHours = refreshHours,
ytEnabled = ytEnabled, ytEnabled = ytEnabled,
seedPercent = seedPercent,
), ),
) )
cacheDetail(updated) cacheDetail(updated)
@@ -526,6 +536,7 @@ class AppStore(
session = null, session = null,
queue = emptyList(), queue = emptyList(),
current = song, current = song,
history = pushHistory(it.history, it.current),
playing = true, playing = true,
positionSeconds = 0.0, positionSeconds = 0.0,
durationSeconds = song.duration.toDouble(), durationSeconds = song.duration.toDouble(),
@@ -592,6 +603,7 @@ class AppStore(
session = null, session = null,
queue = emptyList(), queue = emptyList(),
current = song, current = song,
history = pushHistory(it.history, it.current),
playing = true, playing = true,
positionSeconds = startAtSeconds, positionSeconds = startAtSeconds,
durationSeconds = song.duration.toDouble(), durationSeconds = song.duration.toDouble(),
@@ -760,6 +772,52 @@ class AppStore(
playNext() playNext()
} }
/**
* Back button semantics: after two seconds of playback the current track
* restarts, otherwise the previous track is restored. The outgoing track is
* put back at the front of the queue so nothing is lost.
*/
fun previous() {
val state = _state.value
val song = state.current ?: return
if (state.positionSeconds > 2.0) {
restartCurrent(song)
return
}
val target = state.history.lastOrNull()
if (target == null) {
restartCurrent(song)
return
}
reportCurrentProgress(completed = false)
_state.update {
it.copy(
history = it.history.dropLast(1),
queue = listOf(song) + it.queue,
current = target,
playing = true,
positionSeconds = 0.0,
durationSeconds = target.duration.toDouble(),
)
}
persistPlayback()
startSong(target)
report("started", target.id)
}
private fun restartCurrent(song: Song) {
reportCurrentProgress(completed = false)
if (player.canSeek) {
player.seekTo(0.0)
_state.update { it.copy(positionSeconds = 0.0) }
} else {
_state.update { it.copy(playing = true, positionSeconds = 0.0) }
startSong(song)
}
}
fun seekTo(seconds: Double) { fun seekTo(seconds: Double) {
if (!player.canSeek) return if (!player.canSeek) return
player.seekTo(seconds) player.seekTo(seconds)
@@ -840,6 +898,12 @@ class AppStore(
try { try {
val bytes = api.download(song.id) val bytes = api.download(song.id)
val path = storage.saveDownload(song.id, bytes) val path = storage.saveDownload(song.id, bytes)
if (path != null) {
runCatching { api.cover(song.albumId, 320) }
.getOrNull()
?.takeIf { it.isNotEmpty() }
?.let { storage.saveArtwork(song.id, it) }
}
if (path == null) { if (path == null) {
_state.update { _state.update {
it.copy( it.copy(
@@ -953,6 +1017,7 @@ class AppStore(
it.copy( it.copy(
queue = it.queue.drop(1), queue = it.queue.drop(1),
current = song, current = song,
history = pushHistory(it.history, it.current),
playing = true, playing = true,
positionSeconds = 0.0, positionSeconds = 0.0,
durationSeconds = song.duration.toDouble(), durationSeconds = song.duration.toDouble(),
@@ -990,6 +1055,7 @@ class AppStore(
song = song, song = song,
local = localPath != null, local = localPath != null,
artworkUri = api.coverUrl(song.albumId), artworkUri = api.coverUrl(song.albumId),
artworkBytes = storage.artworkBytes(song.id),
) )
if (startAtSeconds > 0 && player.canSeek) { if (startAtSeconds > 0 && player.canSeek) {
player.seekTo(startAtSeconds) player.seekTo(startAtSeconds)
@@ -1073,6 +1139,7 @@ class AppStore(
_state.update { _state.update {
it.copy( it.copy(
current = null, current = null,
history = emptyList(),
playing = false, playing = false,
queue = emptyList(), queue = emptyList(),
session = null, session = null,
@@ -1105,6 +1172,7 @@ class AppStore(
session = state.session, session = state.session,
queue = state.queue, queue = state.queue,
current = state.current, current = state.current,
history = state.history,
), ),
) )
} }
@@ -1123,6 +1191,10 @@ class AppStore(
} }
} }
/** History is bounded so the snapshot stays small. */
private fun pushHistory(history: List<Song>, current: Song?): List<Song> =
if (current == null) history else (history + current).takeLast(MAX_HISTORY)
private fun longformFolders(): Set<Int> = private fun longformFolders(): Set<Int> =
_state.value.user?.longformFolders?.toSet().orEmpty() _state.value.user?.longformFolders?.toSet().orEmpty()
@@ -1179,6 +1251,7 @@ private const val RECONNECT_INITIAL_MS = 15_000L
private const val RECONNECT_MAX_MS = 300_000L private const val RECONNECT_MAX_MS = 300_000L
private const val DOWNLOAD_ATTEMPTS = 3 private const val DOWNLOAD_ATTEMPTS = 3
private const val PROGRESS_POLL_TICKS = 12 private const val PROGRESS_POLL_TICKS = 12
private const val MAX_HISTORY = 50
private fun LongformProgressItem.toProgress(): LongformProgress = LongformProgress( private fun LongformProgressItem.toProgress(): LongformProgress = LongformProgress(
songId = song.id, songId = song.id,
@@ -29,6 +29,7 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Slider
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Switch import androidx.compose.material3.Switch
import androidx.compose.material3.Text import androidx.compose.material3.Text
@@ -44,6 +45,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt
import win.taptap.musicmanager.client.api.Recommendation import win.taptap.musicmanager.client.api.Recommendation
import win.taptap.musicmanager.client.api.Song import win.taptap.musicmanager.client.api.Song
import win.taptap.musicmanager.client.store.AppState import win.taptap.musicmanager.client.store.AppState
@@ -74,6 +76,7 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
var target by remember(mood.id, mood.changedAt) { mutableStateOf(mood.targetSize.toString()) } var target by remember(mood.id, mood.changedAt) { mutableStateOf(mood.targetSize.toString()) }
var hours by remember(mood.id, mood.changedAt) { mutableStateOf(mood.refreshHours.toString()) } var hours by remember(mood.id, mood.changedAt) { mutableStateOf(mood.refreshHours.toString()) }
var yt by remember(mood.id, mood.changedAt) { mutableStateOf(mood.ytEnabled) } var yt by remember(mood.id, mood.changedAt) { mutableStateOf(mood.ytEnabled) }
var seedPercent by remember(mood.id, mood.changedAt) { mutableStateOf(mood.seedPercent.toFloat()) }
val pendingDownloads = detail.recommendations.count { val pendingDownloads = detail.recommendations.count {
it.download?.state == "queued" || it.download?.state == "downloading" it.download?.state == "queued" || it.download?.state == "downloading"
@@ -109,6 +112,11 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
Text("${mood.seedCount} seeds", style = MaterialTheme.typography.labelMedium) Text("${mood.seedCount} seeds", style = MaterialTheme.typography.labelMedium)
Text("·", color = MaterialTheme.colorScheme.onSurfaceVariant) Text("·", color = MaterialTheme.colorScheme.onSurfaceVariant)
Text("${mood.targetSize} batch", style = MaterialTheme.typography.labelMedium) Text("${mood.targetSize} batch", style = MaterialTheme.typography.labelMedium)
Text("·", color = MaterialTheme.colorScheme.onSurfaceVariant)
Text(
"${mood.seedPercent}% seeds",
style = MaterialTheme.typography.labelMedium,
)
Spacer(Modifier.weight(1f)) Spacer(Modifier.weight(1f))
Button(onClick = { store.playMood(mood.id) }) { Button(onClick = { store.playMood(mood.id) }) {
Icon(Icons.Rounded.PlayArrow, contentDescription = null, modifier = Modifier.size(18.dp)) Icon(Icons.Rounded.PlayArrow, contentDescription = null, modifier = Modifier.size(18.dp))
@@ -296,6 +304,23 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
) )
} }
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
Text(
"Seed mix: ${seedPercent.roundToInt()}% seeds · ${100 - seedPercent.roundToInt()}% recommendations",
style = MaterialTheme.typography.bodySmall,
)
Slider(
value = seedPercent,
onValueChange = { seedPercent = it },
valueRange = 0f..100f,
steps = 9,
)
Text(
"Each queued track rolls this share independently; if one side runs out, the other fills in.",
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
)
}
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Text("YT Music discovery", Modifier.weight(1f)) Text("YT Music discovery", Modifier.weight(1f))
Switch(checked = yt, onCheckedChange = { yt = it }) Switch(checked = yt, onCheckedChange = { yt = it })
@@ -312,6 +337,7 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
targetSize = target.toIntOrNull() ?: mood.targetSize, targetSize = target.toIntOrNull() ?: mood.targetSize,
refreshHours = hours.toIntOrNull() ?: mood.refreshHours, refreshHours = hours.toIntOrNull() ?: mood.refreshHours,
ytEnabled = yt, ytEnabled = yt,
seedPercent = seedPercent.roundToInt(),
) )
}, },
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
@@ -31,6 +31,7 @@ import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Slider
import androidx.compose.material3.Switch import androidx.compose.material3.Switch
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
@@ -46,6 +47,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlin.math.roundToInt
import win.taptap.musicmanager.client.api.Mood import win.taptap.musicmanager.client.api.Mood
import win.taptap.musicmanager.client.store.AppState import win.taptap.musicmanager.client.store.AppState
import win.taptap.musicmanager.client.store.AppStore import win.taptap.musicmanager.client.store.AppStore
@@ -114,9 +116,9 @@ fun MoodsScreen(state: AppState, store: AppStore) {
CreateMoodDialog( CreateMoodDialog(
saving = state.saving, saving = state.saving,
onDismiss = { creating = false }, onDismiss = { creating = false },
onCreate = { name, description, genres, query, target, hours, yt -> onCreate = { name, description, genres, query, target, hours, yt, seedPercent ->
creating = false creating = false
store.createMood(name, description, genres, query, target, hours, yt) store.createMood(name, description, genres, query, target, hours, yt, seedPercent)
}, },
) )
} }
@@ -184,7 +186,7 @@ private fun MoodCard(mood: Mood, onOpen: () -> Unit, onPlay: () -> Unit) {
private fun CreateMoodDialog( private fun CreateMoodDialog(
saving: Boolean, saving: Boolean,
onDismiss: () -> Unit, onDismiss: () -> Unit,
onCreate: (String, String?, List<String>, String?, Int, Int, Boolean) -> Unit, onCreate: (String, String?, List<String>, String?, Int, Int, Boolean, Int) -> Unit,
) { ) {
var name by remember { mutableStateOf("") } var name by remember { mutableStateOf("") }
var description by remember { mutableStateOf("") } var description by remember { mutableStateOf("") }
@@ -193,6 +195,7 @@ private fun CreateMoodDialog(
var target by remember { mutableStateOf("30") } var target by remember { mutableStateOf("30") }
var hours by remember { mutableStateOf("24") } var hours by remember { mutableStateOf("24") }
var yt by remember { mutableStateOf(true) } var yt by remember { mutableStateOf(true) }
var seedPercent by remember { mutableStateOf(50f) }
AlertDialog( AlertDialog(
onDismissRequest = onDismiss, onDismissRequest = onDismiss,
@@ -242,6 +245,18 @@ private fun CreateMoodDialog(
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
) )
} }
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
Text(
"Seed mix: ${seedPercent.roundToInt()}% seeds · ${100 - seedPercent.roundToInt()}% recommendations",
style = MaterialTheme.typography.bodySmall,
)
Slider(
value = seedPercent,
onValueChange = { seedPercent = it },
valueRange = 0f..100f,
steps = 9,
)
}
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Text("YT Music discovery", Modifier.weight(1f)) Text("YT Music discovery", Modifier.weight(1f))
Switch(checked = yt, onCheckedChange = { yt = it }) Switch(checked = yt, onCheckedChange = { yt = it })
@@ -260,6 +275,7 @@ private fun CreateMoodDialog(
target.toIntOrNull() ?: 30, target.toIntOrNull() ?: 30,
hours.toIntOrNull() ?: 24, hours.toIntOrNull() ?: 24,
yt, yt,
seedPercent.roundToInt(),
) )
}, },
) { Text("Create") } ) { Text("Create") }
@@ -24,6 +24,7 @@ import androidx.compose.material.icons.rounded.PlayArrow
import androidx.compose.material.icons.rounded.PlaylistAdd import androidx.compose.material.icons.rounded.PlaylistAdd
import androidx.compose.material.icons.rounded.QueueMusic import androidx.compose.material.icons.rounded.QueueMusic
import androidx.compose.material.icons.rounded.SkipNext import androidx.compose.material.icons.rounded.SkipNext
import androidx.compose.material.icons.rounded.SkipPrevious
import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.HorizontalDivider import androidx.compose.material3.HorizontalDivider
@@ -74,10 +75,14 @@ fun PlayerBar(state: AppState, store: AppStore) {
value = dragSeconds ?: progress, value = dragSeconds ?: progress,
onValueChange = { dragSeconds = it }, onValueChange = { dragSeconds = it },
onValueChangeFinished = { onValueChangeFinished = {
dragSeconds?.let { store.seekTo(it * state.durationSeconds) } dragSeconds?.let { fraction ->
if (state.durationSeconds > 0) {
store.seekTo(fraction * state.durationSeconds)
}
}
dragSeconds = null dragSeconds = null
}, },
enabled = store.canSeek(), enabled = store.canSeek() && state.durationSeconds > 0,
modifier = Modifier.weight(1f), modifier = Modifier.weight(1f),
) )
IconButton(onClick = { queueOpen = true }) { IconButton(onClick = { queueOpen = true }) {
@@ -165,6 +170,9 @@ fun PlayerBar(state: AppState, store: AppStore) {
IconButton(onClick = { picker = true }) { IconButton(onClick = { picker = true }) {
Icon(Icons.Rounded.PlaylistAdd, contentDescription = "Add to another mood") Icon(Icons.Rounded.PlaylistAdd, contentDescription = "Add to another mood")
} }
IconButton(onClick = { store.previous() }) {
Icon(Icons.Rounded.SkipPrevious, contentDescription = "Previous")
}
IconButton(onClick = { store.skip() }) { IconButton(onClick = { store.skip() }) {
Icon(Icons.Rounded.SkipNext, contentDescription = "Skip") Icon(Icons.Rounded.SkipNext, contentDescription = "Skip")
} }
@@ -244,6 +244,88 @@ class AppStoreTest {
store.release() store.release()
} }
@Test
fun `previous restarts after two seconds and restores the last track before it`() = runTest {
val storage = MemoryStorage()
val cache = LibraryCache(storage)
cache.save(CachedState(serverUrl = "http://music", songs = listOf(song("a"), song("b"), song("c"))))
val player = FakePlayer()
val store = store(storage, cache, player)
runCurrent()
store.playAll(shuffled = false)
runCurrent()
store.skip()
runCurrent()
assertEquals("b", store.state.value.current?.id)
assertEquals(listOf("a"), store.state.value.history.map { it.id })
store.seekTo(5.0)
store.previous()
assertEquals("b", store.state.value.current?.id)
assertEquals(0.0, store.state.value.positionSeconds)
assertEquals(0.0, player.seeks.last())
store.previous()
runCurrent()
assertEquals("a", store.state.value.current?.id)
assertEquals(listOf("b", "c"), store.state.value.queue.map { it.id })
assertTrue(store.state.value.history.isEmpty())
assertEquals("a", player.played.last())
assertTrue(cache.loadPlayback()?.history?.isEmpty() == true)
store.release()
}
@Test
fun `delta refresh keeps cached longform songs`() = runTest {
val storage = MemoryStorage()
val cache = LibraryCache(storage)
cache.saveSession(
SessionState(
"http://music",
"token",
User("alice", admin = false, musicFolders = listOf(1), longformFolders = listOf(10)),
),
)
cache.save(
CachedState(
serverUrl = "http://music",
revision = "r1",
songs = listOf(song("m1")),
longform = listOf(song("l1", artist = "Die drei ???", folderId = 10)),
),
)
val engine = MockEngine { request ->
when (request.url.encodedPath) {
"/api/v1/moods" -> respond("""{"moods":[]}""", HttpStatusCode.OK, jsonHeaders)
"/api/v1/library/songs" -> respond(
"""{"songs":[],"revision":"r2"}""",
HttpStatusCode.OK,
jsonHeaders,
)
"/api/v1/longform/progress" -> respond(
"""{"items":[]}""",
HttpStatusCode.OK,
jsonHeaders,
)
else -> respondError(HttpStatusCode.NotFound)
}
}
val store = AppStore(
scope = this,
api = ApiClient(configuredHttpClient(engine)),
storage = storage,
player = FakePlayer(),
cache = cache,
)
runCurrent()
awaitState { cache.load()?.revision == "r2" }
assertEquals(listOf("m1"), store.state.value.library.map { it.id })
assertEquals(listOf("l1"), store.state.value.longform.map { it.id })
store.release()
}
@Test @Test
fun `playing an artist queues only that longform artist`() = runTest { fun `playing an artist queues only that longform artist`() = runTest {
val storage = MemoryStorage() val storage = MemoryStorage()
@@ -362,14 +444,23 @@ class AppStoreTest {
private class FakePlayer : AudioPlayer { private class FakePlayer : AudioPlayer {
val played = mutableListOf<String>() val played = mutableListOf<String>()
val seeks = mutableListOf<Double>()
var paused = false var paused = false
private set private set
private var playing = false private var playing = false
private var position = 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?,
) {
played += song.id played += song.id
playing = true playing = true
paused = false paused = false
position = 0.0
} }
override fun pause() { override fun pause() {
@@ -388,10 +479,13 @@ class AppStoreTest {
override fun isPlaying(): Boolean = playing override fun isPlaying(): Boolean = playing
override fun isFinished(): Boolean = false override fun isFinished(): Boolean = false
override fun positionSeconds(): Double = 0.0 override fun positionSeconds(): Double = position
override fun durationSeconds(): Double = 0.0 override fun durationSeconds(): Double = 0.0
override val canSeek: Boolean = false override val canSeek: Boolean = true
override fun seekTo(seconds: Double) = Unit override fun seekTo(seconds: Double) {
position = seconds
seeks += seconds
}
override fun release() { override fun release() {
playing = false playing = false
} }
@@ -5,6 +5,7 @@ import win.taptap.musicmanager.client.data.AppStorage
/** In-memory [AppStorage] for store and cache tests. */ /** In-memory [AppStorage] for store and cache tests. */
internal class MemoryStorage : AppStorage { internal class MemoryStorage : AppStorage {
private val files = mutableMapOf<String, String>() private val files = mutableMapOf<String, String>()
private val artwork = mutableMapOf<String, ByteArray>()
override fun read(name: String): String? = files[name] override fun read(name: String): String? = files[name]
@@ -18,7 +19,14 @@ internal class MemoryStorage : AppStorage {
override fun saveDownload(songId: String, bytes: ByteArray): String? = null override fun saveDownload(songId: String, bytes: ByteArray): String? = null
override fun downloadPath(songId: String): String? = null override fun downloadPath(songId: String): String? = null
override fun saveArtwork(songId: String, bytes: ByteArray): String? {
artwork[songId] = bytes
return "memory://$songId.jpg"
}
override fun artworkBytes(songId: String): ByteArray? = artwork[songId]
override fun listDownloads(): Map<String, Long> = emptyMap() override fun listDownloads(): Map<String, Long> = emptyMap()
override fun deleteDownload(songId: String) = Unit override fun deleteDownload(songId: String) {
artwork.remove(songId)
}
override fun storageUsedBytes(): Long = 0 override fun storageUsedBytes(): Long = 0
} }
@@ -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 * 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 * 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 * 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 var worker: Thread? = null
private val lock = Object() 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 paused = false
@Volatile private var stopped = false @Volatile private var stopped = false
@Volatile private var finished = false @Volatile private var finished = false
@Volatile private var framesRead = 0L @Volatile private var framesRead = 0L
@Volatile private var baseSeconds = 0.0
private var frameSize = 4 private var frameSize = 4
private var frameRate = 44_100f private var frameRate = 44_100f
private var duration = 0.0 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() stop()
finished = false finished = false
paused = false paused = false
stopped = false stopped = false
framesRead = 0 framesRead = 0
baseSeconds = startAtSeconds.coerceAtLeast(0.0)
duration = song.duration.toDouble() 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) val buffered = BufferedInputStream(raw, 128 * 1024)
input = buffered input = buffered
val encoded = AudioSystem.getAudioInputStream(buffered) val encoded = AudioSystem.getAudioInputStream(buffered)
@@ -55,6 +85,13 @@ class DesktopAudioPlayer : AudioPlayer {
frameSize = audio.format.frameSize.takeIf { it > 0 } ?: 4 frameSize = audio.format.frameSize.takeIf { it > 0 } ?: 4
frameRate = audio.format.frameRate.takeIf { it > 0 } ?: 44_100f 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 info = javax.sound.sampled.DataLine.Info(SourceDataLine::class.java, audio.format)
val dataLine = AudioSystem.getLine(info) as SourceDataLine val dataLine = AudioSystem.getLine(info) as SourceDataLine
dataLine.open(audio.format) 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() { override fun pause() {
paused = true paused = true
} }
@@ -122,19 +176,32 @@ class DesktopAudioPlayer : AudioPlayer {
override fun isFinished(): Boolean = finished override fun isFinished(): Boolean = finished
override fun positionSeconds(): Double { 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) return seconds.coerceIn(0.0, duration.takeIf { it > 0 } ?: seconds)
} }
override fun durationSeconds(): Double = duration 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() { override fun release() {
stop() 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? = override fun downloadPath(songId: String): String? =
File(downloads, "${safeName(songId)}.mp3").takeIf { it.isFile }?.absolutePath 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> = override fun listDownloads(): Map<String, Long> =
downloads.listFiles() downloads.listFiles()
?.filter { it.isFile && it.extension == "mp3" } ?.filter { it.isFile && it.extension == "mp3" }
@@ -57,6 +68,7 @@ class DesktopStorage : AppStorage {
override fun deleteDownload(songId: String) { override fun deleteDownload(songId: String) {
File(downloads, "${safeName(songId)}.mp3").delete() File(downloads, "${safeName(songId)}.mp3").delete()
File(downloads, "${safeName(songId)}.jpg").delete()
} }
override fun storageUsedBytes(): Long = override fun storageUsedBytes(): Long =
@@ -29,6 +29,8 @@ class WebStorage : AppStorage {
override fun saveDownload(songId: String, bytes: ByteArray): String? = null override fun saveDownload(songId: String, bytes: ByteArray): String? = null
override fun downloadPath(songId: String): String? = null override fun downloadPath(songId: String): String? = null
override fun saveArtwork(songId: String, bytes: ByteArray): String? = null
override fun artworkBytes(songId: String): ByteArray? = null
override fun listDownloads(): Map<String, Long> = emptyMap() override fun listDownloads(): Map<String, Long> = emptyMap()
override fun deleteDownload(songId: String) = Unit override fun deleteDownload(songId: String) = Unit
override fun storageUsedBytes(): Long = 0L override fun storageUsedBytes(): Long = 0L
@@ -12,7 +12,13 @@ class WebAudioPlayer : AudioPlayer {
preload = "auto" preload = "auto"
} }
override fun play(source: String, song: Song, local: Boolean, artworkUri: String?) { override fun play(
source: String,
song: Song,
local: Boolean,
artworkUri: String?,
artworkBytes: ByteArray?,
) {
audio.src = source audio.src = source
audio.play() audio.play()
} }