From 17bda94bfc94e618dcde9283be1bf7500e5a399e Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 15 Nov 2026 17:07:11 +0000 Subject: [PATCH] feat: previous/scrub controls, mood mix and artwork for downloads 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. --- README.md | 8 +- .../client/MediaControllerAudioPlayer.kt | 19 +++- .../musicmanager/client/Platform.android.kt | 12 +++ .../musicmanager/client/api/ApiClient.kt | 13 ++- .../taptap/musicmanager/client/api/Models.kt | 4 + .../musicmanager/client/data/AppStorage.kt | 4 + .../musicmanager/client/data/LibraryCache.kt | 1 + .../client/playback/AudioPlayer.kt | 8 +- .../musicmanager/client/store/AppStore.kt | 75 ++++++++++++- .../client/ui/MoodDetailScreen.kt | 26 +++++ .../musicmanager/client/ui/MoodsScreen.kt | 22 +++- .../musicmanager/client/ui/PlayerBar.kt | 12 ++- .../musicmanager/client/AppStoreTest.kt | 102 +++++++++++++++++- .../musicmanager/client/MemoryStorage.kt | 10 +- .../musicmanager/client/DesktopAudioPlayer.kt | 79 ++++++++++++-- .../musicmanager/client/Platform.desktop.kt | 12 +++ .../musicmanager/client/Platform.wasm.kt | 2 + .../musicmanager/client/WebAudioPlayer.kt | 8 +- 18 files changed, 390 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 17b9ab9..c6f3411 100644 --- a/README.md +++ b/README.md @@ -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. - 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. +- **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. - **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. @@ -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. - **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. -- **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. - **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. - 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. -- Accepted recommendations are downloaded by the server into `/AudioManager Imports`, scanned and added as seeds. The server needs outbound network access for this. +- Accepted recommendations are downloaded by the server into `/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 (`.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. - No iOS target yet. The shared module is ready for one. diff --git a/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/MediaControllerAudioPlayer.kt b/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/MediaControllerAudioPlayer.kt index e761a80..27e5eb0 100644 --- a/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/MediaControllerAudioPlayer.kt +++ b/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/MediaControllerAudioPlayer.kt @@ -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 withController { controller -> 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) .setArtist(song.artist) .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() val item = MediaItem.Builder() .setUri(uri) diff --git a/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/Platform.android.kt b/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/Platform.android.kt index cb184fb..57dd1d6 100644 --- a/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/Platform.android.kt +++ b/composeApp/src/androidMain/kotlin/win/taptap/musicmanager/client/Platform.android.kt @@ -49,6 +49,17 @@ class AndroidStorage(context: Context) : 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 = downloads.listFiles() ?.filter { it.isFile && it.extension == "mp3" } @@ -57,6 +68,7 @@ class AndroidStorage(context: Context) : AppStorage { override fun deleteDownload(songId: String) { File(downloads, "${safeName(songId)}.mp3").delete() + File(downloads, "${safeName(songId)}.jpg").delete() } override fun storageUsedBytes(): Long = diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt index 0925539..950a297 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt @@ -255,11 +255,16 @@ class ApiClient(private val http: HttpClient) { suspend fun cover(albumId: String, size: Int = 320): ByteArray? { val key = "$albumId:$size" if (coverCache.containsKey(key)) return coverCache[key] - val bytes = runCatching { - http.get("$baseUrl/api/v1/covers/$albumId?size=$size&token=${token.orEmpty().encodeURLParameter()}") - .bodyAsBytes() + val result = runCatching { + val response = http.get( + "$baseUrl/api/v1/covers/$albumId?size=$size&token=${token.orEmpty().encodeURLParameter()}", + ) + if (response.status.isSuccess()) { + response.bodyAsBytes().takeIf { it.isNotEmpty() } + } else { + null + } }.getOrNull() - val result = bytes?.takeIf { it.isNotEmpty() } if (coverCache.size > MAX_CACHED_IMAGES) coverCache.clear() coverCache[key] = result return result diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt index ff21df5..2a4913c 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/Models.kt @@ -62,6 +62,8 @@ data class Mood( val query: String? = null, val genres: List = emptyList(), val targetSize: Int, + val seedPercent: Int = 50, + val recommendationPercent: Int = 50, val refreshHours: Int, val ytEnabled: Boolean, val lastDiscoveryAt: String? = null, @@ -217,6 +219,7 @@ data class CreateMoodRequest( val refreshHours: Int = 24, val ytEnabled: Boolean = true, val seedSongIds: List = emptyList(), + val seedPercent: Int = 50, ) @Serializable @@ -230,6 +233,7 @@ data class UpdateMoodRequest( val targetSize: Int? = null, val refreshHours: Int? = null, val ytEnabled: Boolean? = null, + val seedPercent: Int? = null, ) @Serializable diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/AppStorage.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/AppStorage.kt index 8beb2dd..42e1ff9 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/AppStorage.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/AppStorage.kt @@ -9,6 +9,10 @@ interface AppStorage { /** Returns a platform-specific handle for playback, or null when unsupported. */ fun saveDownload(songId: String, bytes: ByteArray): 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 fun deleteDownload(songId: String) fun storageUsedBytes(): Long diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/LibraryCache.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/LibraryCache.kt index b95fc3a..2530165 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/LibraryCache.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/data/LibraryCache.kt @@ -33,6 +33,7 @@ data class PlaybackSnapshot( val session: Session? = null, val queue: List = emptyList(), val current: Song? = null, + val history: List = emptyList(), ) class LibraryCache(private val storage: AppStorage) { diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/playback/AudioPlayer.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/playback/AudioPlayer.kt index eb2d4be..75e74bf 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/playback/AudioPlayer.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/playback/AudioPlayer.kt @@ -9,7 +9,13 @@ import win.taptap.musicmanager.client.api.Song * store polls [isFinished] because some backends have no callback support. */ 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 resume() fun stop() diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt index 51f077c..3441599 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt @@ -49,6 +49,7 @@ data class AppState( val session: Session? = null, val queue: List = emptyList(), val current: Song? = null, + val history: List = emptyList(), val playing: Boolean = false, val positionSeconds: Double = 0.0, val durationSeconds: Double = 0.0, @@ -114,6 +115,7 @@ class AppStore( session = playback?.session, queue = playback?.queue.orEmpty(), current = playback?.current, + history = playback?.history.orEmpty(), durationSeconds = playback?.current?.duration?.toDouble() ?: 0.0, ) } @@ -184,7 +186,11 @@ class AppStore( try { val moods = api.moods() 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 if (revision == null) { @@ -309,6 +315,7 @@ class AppStore( targetSize: Int, refreshHours: Int, ytEnabled: Boolean, + seedPercent: Int = 50, ) { scope.launch { _state.update { it.copy(saving = true) } @@ -322,6 +329,7 @@ class AppStore( targetSize = targetSize, refreshHours = refreshHours, ytEnabled = ytEnabled, + seedPercent = seedPercent, ), ) cacheDetail(detail) @@ -351,6 +359,7 @@ class AppStore( targetSize: Int, refreshHours: Int, ytEnabled: Boolean, + seedPercent: Int = 50, ) { scope.launch { _state.update { it.copy(saving = true) } @@ -367,6 +376,7 @@ class AppStore( targetSize = targetSize, refreshHours = refreshHours, ytEnabled = ytEnabled, + seedPercent = seedPercent, ), ) cacheDetail(updated) @@ -526,6 +536,7 @@ class AppStore( session = null, queue = emptyList(), current = song, + history = pushHistory(it.history, it.current), playing = true, positionSeconds = 0.0, durationSeconds = song.duration.toDouble(), @@ -592,6 +603,7 @@ class AppStore( session = null, queue = emptyList(), current = song, + history = pushHistory(it.history, it.current), playing = true, positionSeconds = startAtSeconds, durationSeconds = song.duration.toDouble(), @@ -760,6 +772,52 @@ class AppStore( 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) { if (!player.canSeek) return player.seekTo(seconds) @@ -840,6 +898,12 @@ class AppStore( try { val bytes = api.download(song.id) 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) { _state.update { it.copy( @@ -953,6 +1017,7 @@ class AppStore( it.copy( queue = it.queue.drop(1), current = song, + history = pushHistory(it.history, it.current), playing = true, positionSeconds = 0.0, durationSeconds = song.duration.toDouble(), @@ -990,6 +1055,7 @@ class AppStore( song = song, local = localPath != null, artworkUri = api.coverUrl(song.albumId), + artworkBytes = storage.artworkBytes(song.id), ) if (startAtSeconds > 0 && player.canSeek) { player.seekTo(startAtSeconds) @@ -1073,6 +1139,7 @@ class AppStore( _state.update { it.copy( current = null, + history = emptyList(), playing = false, queue = emptyList(), session = null, @@ -1105,6 +1172,7 @@ class AppStore( session = state.session, queue = state.queue, 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, current: Song?): List = + if (current == null) history else (history + current).takeLast(MAX_HISTORY) + private fun longformFolders(): Set = _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 DOWNLOAD_ATTEMPTS = 3 private const val PROGRESS_POLL_TICKS = 12 +private const val MAX_HISTORY = 50 private fun LongformProgressItem.toProgress(): LongformProgress = LongformProgress( songId = song.id, diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt index cd5301e..2e5efc4 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt @@ -29,6 +29,7 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedButton import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Slider import androidx.compose.material3.Surface import androidx.compose.material3.Switch 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.style.TextOverflow import androidx.compose.ui.unit.dp +import kotlin.math.roundToInt import win.taptap.musicmanager.client.api.Recommendation import win.taptap.musicmanager.client.api.Song 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 hours by remember(mood.id, mood.changedAt) { mutableStateOf(mood.refreshHours.toString()) } 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 { 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("·", color = MaterialTheme.colorScheme.onSurfaceVariant) 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)) Button(onClick = { store.playMood(mood.id) }) { 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), ) } + 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) { Text("YT Music discovery", Modifier.weight(1f)) Switch(checked = yt, onCheckedChange = { yt = it }) @@ -312,6 +337,7 @@ fun MoodDetailScreen(state: AppState, store: AppStore) { targetSize = target.toIntOrNull() ?: mood.targetSize, refreshHours = hours.toIntOrNull() ?: mood.refreshHours, ytEnabled = yt, + seedPercent = seedPercent.roundToInt(), ) }, modifier = Modifier.fillMaxWidth(), diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt index bf356a6..fcb2c54 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodsScreen.kt @@ -31,6 +31,7 @@ import androidx.compose.material3.FilledTonalButton import androidx.compose.material3.Icon import androidx.compose.material3.MaterialTheme import androidx.compose.material3.OutlinedTextField +import androidx.compose.material3.Slider import androidx.compose.material3.Switch import androidx.compose.material3.Text 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.style.TextOverflow import androidx.compose.ui.unit.dp +import kotlin.math.roundToInt import win.taptap.musicmanager.client.api.Mood import win.taptap.musicmanager.client.store.AppState import win.taptap.musicmanager.client.store.AppStore @@ -114,9 +116,9 @@ fun MoodsScreen(state: AppState, store: AppStore) { CreateMoodDialog( saving = state.saving, onDismiss = { creating = false }, - onCreate = { name, description, genres, query, target, hours, yt -> + onCreate = { name, description, genres, query, target, hours, yt, seedPercent -> 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( saving: Boolean, onDismiss: () -> Unit, - onCreate: (String, String?, List, String?, Int, Int, Boolean) -> Unit, + onCreate: (String, String?, List, String?, Int, Int, Boolean, Int) -> Unit, ) { var name by remember { mutableStateOf("") } var description by remember { mutableStateOf("") } @@ -193,6 +195,7 @@ private fun CreateMoodDialog( var target by remember { mutableStateOf("30") } var hours by remember { mutableStateOf("24") } var yt by remember { mutableStateOf(true) } + var seedPercent by remember { mutableStateOf(50f) } AlertDialog( onDismissRequest = onDismiss, @@ -242,6 +245,18 @@ private fun CreateMoodDialog( 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) { Text("YT Music discovery", Modifier.weight(1f)) Switch(checked = yt, onCheckedChange = { yt = it }) @@ -260,6 +275,7 @@ private fun CreateMoodDialog( target.toIntOrNull() ?: 30, hours.toIntOrNull() ?: 24, yt, + seedPercent.roundToInt(), ) }, ) { Text("Create") } diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/PlayerBar.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/PlayerBar.kt index d8cbcc5..27b5c67 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/PlayerBar.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/PlayerBar.kt @@ -24,6 +24,7 @@ import androidx.compose.material.icons.rounded.PlayArrow import androidx.compose.material.icons.rounded.PlaylistAdd import androidx.compose.material.icons.rounded.QueueMusic import androidx.compose.material.icons.rounded.SkipNext +import androidx.compose.material.icons.rounded.SkipPrevious import androidx.compose.material3.DropdownMenu import androidx.compose.material3.DropdownMenuItem import androidx.compose.material3.HorizontalDivider @@ -74,10 +75,14 @@ fun PlayerBar(state: AppState, store: AppStore) { value = dragSeconds ?: progress, onValueChange = { dragSeconds = it }, onValueChangeFinished = { - dragSeconds?.let { store.seekTo(it * state.durationSeconds) } + dragSeconds?.let { fraction -> + if (state.durationSeconds > 0) { + store.seekTo(fraction * state.durationSeconds) + } + } dragSeconds = null }, - enabled = store.canSeek(), + enabled = store.canSeek() && state.durationSeconds > 0, modifier = Modifier.weight(1f), ) IconButton(onClick = { queueOpen = true }) { @@ -165,6 +170,9 @@ fun PlayerBar(state: AppState, store: AppStore) { IconButton(onClick = { picker = true }) { Icon(Icons.Rounded.PlaylistAdd, contentDescription = "Add to another mood") } + IconButton(onClick = { store.previous() }) { + Icon(Icons.Rounded.SkipPrevious, contentDescription = "Previous") + } IconButton(onClick = { store.skip() }) { Icon(Icons.Rounded.SkipNext, contentDescription = "Skip") } diff --git a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt index 473b42c..d7a7d4e 100644 --- a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt +++ b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/AppStoreTest.kt @@ -244,6 +244,88 @@ class AppStoreTest { 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 fun `playing an artist queues only that longform artist`() = runTest { val storage = MemoryStorage() @@ -362,14 +444,23 @@ class AppStoreTest { private class FakePlayer : AudioPlayer { val played = mutableListOf() + val seeks = mutableListOf() var paused = false private set 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 playing = true paused = false + position = 0.0 } override fun pause() { @@ -388,10 +479,13 @@ class AppStoreTest { override fun isPlaying(): Boolean = playing override fun isFinished(): Boolean = false - override fun positionSeconds(): Double = 0.0 + override fun positionSeconds(): Double = position override fun durationSeconds(): Double = 0.0 - override val canSeek: Boolean = false - override fun seekTo(seconds: Double) = Unit + override val canSeek: Boolean = true + override fun seekTo(seconds: Double) { + position = seconds + seeks += seconds + } override fun release() { playing = false } diff --git a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/MemoryStorage.kt b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/MemoryStorage.kt index 13796df..465d075 100644 --- a/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/MemoryStorage.kt +++ b/composeApp/src/commonTest/kotlin/win/taptap/musicmanager/client/MemoryStorage.kt @@ -5,6 +5,7 @@ import win.taptap.musicmanager.client.data.AppStorage /** In-memory [AppStorage] for store and cache tests. */ internal class MemoryStorage : AppStorage { private val files = mutableMapOf() + private val artwork = mutableMapOf() 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 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 = emptyMap() - override fun deleteDownload(songId: String) = Unit + override fun deleteDownload(songId: String) { + artwork.remove(songId) + } override fun storageUsedBytes(): Long = 0 } diff --git a/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt b/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt index f3e7b6b..9cd6764 100644 --- a/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt +++ b/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/DesktopAudioPlayer.kt @@ -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()}" + } } /** diff --git a/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/Platform.desktop.kt b/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/Platform.desktop.kt index 97faffe..7c9b129 100644 --- a/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/Platform.desktop.kt +++ b/composeApp/src/desktopMain/kotlin/win/taptap/musicmanager/client/Platform.desktop.kt @@ -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 = 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 = diff --git a/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/Platform.wasm.kt b/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/Platform.wasm.kt index 67b1d1d..a3c411c 100644 --- a/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/Platform.wasm.kt +++ b/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/Platform.wasm.kt @@ -29,6 +29,8 @@ class WebStorage : AppStorage { override fun saveDownload(songId: String, bytes: ByteArray): 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 = emptyMap() override fun deleteDownload(songId: String) = Unit override fun storageUsedBytes(): Long = 0L diff --git a/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/WebAudioPlayer.kt b/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/WebAudioPlayer.kt index d1209f0..9184353 100644 --- a/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/WebAudioPlayer.kt +++ b/composeApp/src/wasmJsMain/kotlin/win/taptap/musicmanager/client/WebAudioPlayer.kt @@ -12,7 +12,13 @@ class WebAudioPlayer : AudioPlayer { 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.play() }