diff --git a/app/src/main/java/it/panov/musyx/tv/api/Lyrics.kt b/app/src/main/java/it/panov/musyx/tv/api/Lyrics.kt index 0c484a1..53c459c 100644 --- a/app/src/main/java/it/panov/musyx/tv/api/Lyrics.kt +++ b/app/src/main/java/it/panov/musyx/tv/api/Lyrics.kt @@ -19,6 +19,26 @@ data class Lyrics( return lines.indexOfLast { it.atMs != null && it.atMs <= positionMs }.takeIf { it >= 0 } } + /** + * The few lines a glance from the sofa shows at `positionMs`: synced, the line being sung + * between its neighbours (before the first, just the first, to come); plain, the line as far + * through the words as the song is through its `durationMs`. Null when there's nothing to show. + */ + fun glance(positionMs: Long, durationMs: Long): Glance? { + if (synced) { + if (lines.isEmpty()) return null + val i = current(positionMs) ?: return Glance(null, null, lines.first().text) + return Glance(lines.getOrNull(i - 1)?.text, lines[i].text, lines.getOrNull(i + 1)?.text) + } + val words = lines.map { it.text }.filter { it.isNotBlank() } + if (words.isEmpty() || durationMs <= 0) return null + val i = (positionMs.coerceIn(0, durationMs) * words.size / durationMs).toInt().coerceAtMost(words.size - 1) + return Glance(words.getOrNull(i - 1), words[i], words.getOrNull(i + 1)) + } + + /** Three lines around now; an empty line is a break in the singing. */ + data class Glance(val previous: String?, val current: String?, val next: String?) + companion object { /** Plain text: one line per line, blank lines kept as paragraph breaks. */ fun fromText(text: String, writers: List, source: LyricsSource = LyricsSource.Yandex) = diff --git a/app/src/main/java/it/panov/musyx/tv/data/SettingsStore.kt b/app/src/main/java/it/panov/musyx/tv/data/SettingsStore.kt index f8ad890..d17e0e2 100644 --- a/app/src/main/java/it/panov/musyx/tv/data/SettingsStore.kt +++ b/app/src/main/java/it/panov/musyx/tv/data/SettingsStore.kt @@ -43,6 +43,8 @@ data class Settings( val appearance: Appearance = Appearance.System, val fontSize: FontSize = FontSize.Medium, val language: Locale = Locale.System, + /** Whether the screen saver shows the playing track's lyrics over the photos. */ + val saverLyrics: Boolean = false, ) class SettingsStore(context: Context) { @@ -56,6 +58,7 @@ class SettingsStore(context: Context) { appearance = Appearance.entries.firstOrNull { it.key == prefs.getString("appearance", null) } ?: d.appearance, fontSize = FontSize.entries.firstOrNull { it.key == prefs.getString("font_size", null) } ?: d.fontSize, language = Locale.entries.firstOrNull { it.key == prefs.getString("language", null) } ?: d.language, + saverLyrics = prefs.getBoolean("saver_lyrics", d.saverLyrics), ) } @@ -65,6 +68,7 @@ class SettingsStore(context: Context) { putString("appearance", settings.appearance.key) putString("font_size", settings.fontSize.key) putString("language", settings.language.key) + putBoolean("saver_lyrics", settings.saverLyrics) } } diff --git a/app/src/main/java/it/panov/musyx/tv/state/AppState.kt b/app/src/main/java/it/panov/musyx/tv/state/AppState.kt index 326a0de..818de0e 100644 --- a/app/src/main/java/it/panov/musyx/tv/state/AppState.kt +++ b/app/src/main/java/it/panov/musyx/tv/state/AppState.kt @@ -584,6 +584,8 @@ class AppState( fun setFontSize(fontSize: FontSize) = updateSettings(settings.copy(fontSize = fontSize)) + fun setSaverLyrics(on: Boolean) = updateSettings(settings.copy(saverLyrics = on)) + /** The activity recreates itself in the new language; Yandex's own titles follow. */ fun setLanguage(language: Locale) { val before = contentLanguage diff --git a/app/src/main/java/it/panov/musyx/tv/ui/screens/ScreenSaver.kt b/app/src/main/java/it/panov/musyx/tv/ui/screens/ScreenSaver.kt index 1ecc214..f55aa02 100644 --- a/app/src/main/java/it/panov/musyx/tv/ui/screens/ScreenSaver.kt +++ b/app/src/main/java/it/panov/musyx/tv/ui/screens/ScreenSaver.kt @@ -1,19 +1,23 @@ // The app's own screen saver. While music plays the screen stays on, so the system's saver // never starts; after the system's screen saver delay without input, photos of the artist and -// the cover drift across the screen with what's playing. Paused for a while, or with nothing -// queued, the app lets go and the TV's own saver and sleep take over again. +// the cover drift across the screen with what's playing, and the lyrics when Settings asks for +// them. Paused for a while, or with nothing queued, the app lets go and the TV's own saver and +// sleep take over again. package it.panov.musyx.tv.ui.screens import android.content.Context import android.os.SystemClock import android.provider.Settings +import androidx.compose.animation.AnimatedContent import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.Crossfade +import androidx.compose.animation.SizeTransform import androidx.compose.animation.core.Animatable import androidx.compose.animation.core.LinearEasing import androidx.compose.animation.core.tween import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut +import androidx.compose.animation.togetherWith import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box @@ -35,8 +39,10 @@ import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Brush import androidx.compose.ui.graphics.Color +import androidx.compose.ui.graphics.Shadow import androidx.compose.ui.graphics.TransformOrigin import androidx.compose.ui.graphics.graphicsLayer import androidx.compose.ui.layout.ContentScale @@ -49,8 +55,10 @@ import androidx.tv.material3.Text import coil3.SingletonImageLoader import coil3.compose.AsyncImage import coil3.request.ImageRequest +import it.panov.musyx.tv.api.Lyrics import it.panov.musyx.tv.api.Track import it.panov.musyx.tv.state.AppState +import it.panov.musyx.tv.state.Loadable import it.panov.musyx.tv.player.Status import it.panov.musyx.tv.ui.artistsOrUnknown import it.panov.musyx.tv.ui.theme.CoverPlaceholder @@ -79,6 +87,13 @@ private const val PHOTO_SIZE = 1000 /** Artists whose galleries are fetched, from the start of the track's artist list. */ private const val GALLERY_ARTISTS = 2 +/** The crossfade from one lyric line to the next. */ +private const val LYRIC_FADE_MS = 400 + +/** The caption's width, and the wider column it shares with the lyrics. */ +private val CAPTION_WIDTH = 560.dp +private val LYRICS_WIDTH = 620.dp + /** When the remote was last touched, and whether the screen saver covers the app. */ @Stable class ScreenSaverState { @@ -181,6 +196,10 @@ private fun ScreenSaver(state: AppState, track: Track, galleries: MutableMap Photo(s, track.hues) @@ -191,9 +210,34 @@ private fun ScreenSaver(state: AppState, track: Track, galleries: MutableMap - Box(Modifier.fillMaxSize().padding(horizontal = MusyxTheme.space.safeX, vertical = MusyxTheme.space.safeY)) { - Caption(state, track, left, Modifier.align(if (left) Alignment.BottomStart else Alignment.BottomEnd)) + Box(Modifier.fillMaxSize()) { + if (lyrics != null) { + val shade = Color.Black.copy(alpha = 0.6f) + Box( + Modifier.fillMaxSize().background( + if (left) { + Brush.horizontalGradient(0f to shade, 0.6f to Color.Transparent) + } else { + Brush.horizontalGradient(0.4f to Color.Transparent, 1f to shade) + }, + ), + ) + } + Box(Modifier.fillMaxSize().padding(horizontal = MusyxTheme.space.safeX, vertical = MusyxTheme.space.safeY)) { + Column( + Modifier + .align(if (left) Alignment.BottomStart else Alignment.BottomEnd) + .width(if (lyrics != null) LYRICS_WIDTH else CAPTION_WIDTH), + horizontalAlignment = if (left) Alignment.Start else Alignment.End, + ) { + if (lyrics != null) { + SaverLyrics(state, lyrics, left, Modifier.fillMaxWidth().padding(bottom = MusyxTheme.space.xl)) + } + Caption(state, track, left, Modifier.fillMaxWidth()) + } + } } } } @@ -235,7 +279,7 @@ private fun Caption(state: AppState, track: Track, left: Boolean, modifier: Modi val p = state.playback.state val fraction = if (p.durationMs > 0) state.playback.positionMs.toFloat() / p.durationMs else 0f Column( - modifier.width(560.dp), + modifier, horizontalAlignment = if (left) Alignment.Start else Alignment.End, verticalArrangement = Arrangement.spacedBy(6.dp), ) { @@ -257,3 +301,39 @@ private fun Caption(state: AppState, track: Track, left: Boolean, modifier: Modi } } } + +/** + * The line being sung, large, between the one before and the one to come. Plain lyrics can't + * know the line, so all three are the same size and only roughly where the song is. The block + * is anchored at its foot and the next line's slot is always kept, so the lines sit at the same + * distance above the song name whatever they say. + */ +@Composable +private fun SaverLyrics(state: AppState, lyrics: Lyrics, left: Boolean, modifier: Modifier) { + val glance = lyrics.glance(state.playback.positionMs, state.playback.state.durationMs) ?: return + val type = MusyxTheme.type + val align = if (left) TextAlign.Start else TextAlign.End + val shadow = Shadow(Color.Black.copy(alpha = 0.7f), Offset(0f, 2f), blurRadius = 12f) + val side = type.section.copy(shadow = shadow) + val now = if (lyrics.synced) type.displayM.copy(shadow = shadow) else side + AnimatedContent( + glance, + modifier, + transitionSpec = { + fadeIn(tween(LYRIC_FADE_MS)) togetherWith fadeOut(tween(LYRIC_FADE_MS)) using SizeTransform(clip = false) + }, + contentAlignment = if (left) Alignment.BottomStart else Alignment.BottomEnd, + label = "lyric", + ) { g -> + Column( + Modifier.fillMaxWidth(), + horizontalAlignment = if (left) Alignment.Start else Alignment.End, + verticalArrangement = Arrangement.spacedBy(14.dp), + ) { + g.previous?.let { Text(it.ifBlank { "♪" }, style = side, color = Color.White.copy(alpha = 0.45f), textAlign = align, maxLines = 1, overflow = TextOverflow.Ellipsis) } + g.current?.let { Text(it.ifBlank { "♪" }, style = now, color = Color.White, textAlign = align, maxLines = 2, overflow = TextOverflow.Ellipsis) } + val alpha = if (g.current == null || !lyrics.synced) 0.85f else 0.6f + Text(g.next?.ifBlank { "♪" }.orEmpty(), style = side, color = Color.White.copy(alpha = alpha), textAlign = align, maxLines = 1, overflow = TextOverflow.Ellipsis) + } + } +} diff --git a/app/src/main/java/it/panov/musyx/tv/ui/screens/Settings.kt b/app/src/main/java/it/panov/musyx/tv/ui/screens/Settings.kt index dacfd3c..3ab8850 100644 --- a/app/src/main/java/it/panov/musyx/tv/ui/screens/Settings.kt +++ b/app/src/main/java/it/panov/musyx/tv/ui/screens/Settings.kt @@ -1,4 +1,4 @@ -// Settings: account, streaming quality, appearance, font size, language, storage. +// Settings: account, streaming quality, appearance, font size, language, screen saver, storage. package it.panov.musyx.tv.ui.screens import androidx.compose.foundation.background @@ -108,6 +108,19 @@ fun SettingsScreen(state: AppState, entry: FocusEntry) { } } } + item(key = "screen-saver") { + Section(stringResource(R.string.settings_screen_saver)) { + ChoiceRow { + Chip(stringResource(R.string.saver_photos), !settings.saverLyrics, { state.setSaverLyrics(false) }) + Chip(stringResource(R.string.saver_photos_lyrics), settings.saverLyrics, { state.setSaverLyrics(true) }) + } + Text( + stringResource(if (settings.saverLyrics) R.string.saver_photos_lyrics_detail else R.string.saver_photos_detail), + style = MusyxTheme.type.meta, + color = MusyxTheme.colors.muted, + ) + } + } item(key = "storage") { Section(stringResource(R.string.settings_storage)) { Text(stringResource(R.string.settings_storage_detail), style = MusyxTheme.type.meta, color = MusyxTheme.colors.muted) diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index d7b8c4a..3144524 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -191,6 +191,11 @@ Средний Крупный Язык + Заставка + Фото + Фото и текст + Пока играет музыка и пульт не трогают, по экрану плывут фото исполнителя и название трека. + Вместе с ними — текст песни: строка, которая звучит сейчас, если текст синхронизирован, иначе примерно то место, до которого дошла песня. Хранилище Обложки хранятся на диске, чтобы страницы открывались мгновенно. Очистка освободит место, а обложки снова загрузятся по мере просмотра. Musyx %1$s · неофициальный клиент Яндекс Музыки diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8040661..5ec41a7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -204,6 +204,11 @@ Medium Large Language + Screen saver + Photos + Photos and lyrics + While music plays and the remote rests, photos of the artist drift by with what\'s playing. + The lyrics come along too: the line being sung when they\'re synced, else the words about as far in as the song is. Storage Covers are kept on disk so pages open instantly next time. Clearing them frees the space; they download again as you browse. Musyx %1$s · unofficial Yandex Music client diff --git a/app/src/test/java/it/panov/musyx/tv/api/ApiTest.kt b/app/src/test/java/it/panov/musyx/tv/api/ApiTest.kt index c14d798..efaceb1 100644 --- a/app/src/test/java/it/panov/musyx/tv/api/ApiTest.kt +++ b/app/src/test/java/it/panov/musyx/tv/api/ApiTest.kt @@ -367,6 +367,28 @@ class ApiTest { assertNull(Lyrics.parseStamp("00:01.1234")) } + @Test + fun syncedGlanceIsTheSungLineBetweenItsNeighbours() { + val l = Lyrics.fromLrc("[00:10.00]one\n[00:20.00]\n[00:30.00]three", emptyList()) + assertEquals(Lyrics.Glance(null, null, "one"), l.glance(5_000, 60_000)) + assertEquals(Lyrics.Glance(null, "one", ""), l.glance(10_000, 60_000)) + assertEquals(Lyrics.Glance("one", "", "three"), l.glance(25_000, 60_000)) + assertEquals(Lyrics.Glance("", "three", null), l.glance(59_000, 60_000)) + assertNull(Lyrics(emptyList(), synced = true, writers = emptyList()).glance(0, 60_000)) + } + + @Test + fun plainGlanceFollowsTheSongsShareOfTheWords() { + val l = Lyrics.fromText("a\n\nb\nc\nd", emptyList()) + assertEquals(Lyrics.Glance(null, "a", "b"), l.glance(0, 100_000)) + assertEquals(Lyrics.Glance("a", "b", "c"), l.glance(25_000, 100_000)) + assertEquals(Lyrics.Glance("c", "d", null), l.glance(99_000, 100_000)) + // Past the end, or with the length unknown, it doesn't guess. + assertEquals(Lyrics.Glance("c", "d", null), l.glance(150_000, 100_000)) + assertNull(l.glance(10_000, 0)) + assertNull(Lyrics.fromText(" \n", emptyList()).glance(0, 100_000)) + } + // ---- LRCLIB ---- private fun record(duration: Double, synced: String? = null, plain: String? = null, instrumental: Boolean = false) =