Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Play Spotify's DJ, with its voice between the songs

Lelonio34f022140a9b
diff --git a/README.md b/README.md
index 0817682..b9fe0f5 100644
--- a/README.md
+++ b/README.md
@@ -200,7 +200,7 @@ development on [Ko-fi](https://ko-fi.com/lelonio).
| [Phosphor Icons](https://phosphoricons.com) | MIT | Every icon in the app. |
| [ebur128](https://github.com/sdroege/ebur128) | MIT | Loudness of local files, so they play at the level of the streamed tracks. |
| librespot-java | Apache-2.0 | The listening-event format, re-implemented from its `EventService` rather than copied. |
-| [go-librespot](https://github.com/devgianlu/go-librespot) and [go-librespot-termux](https://github.com/SEKY443/go-librespot-termux) | GPL-3.0 | The remote sleep timer's command and state fields, and the check on truncated audio keys, re-implemented from their findings rather than copied. |
+| [go-librespot](https://github.com/devgianlu/go-librespot) and [go-librespot-termux](https://github.com/SEKY443/go-librespot-termux) | GPL-3.0 | The remote sleep timer's command and state fields, the check on truncated audio keys, and how Spotify's DJ is resolved and voiced (the session service, `client-tts`, the narration metadata), re-implemented from their findings rather than copied. |
## Building from source
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index d2096df..0f5bd63 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -112,6 +112,13 @@ class SquareApplication : Application(), ImageLoaderFactory {
}
}
+ // Spotify's DJ, offered or not; see SpotifyDj.enabled.
+ kotlinx.coroutines.CoroutineScope(
+ kotlinx.coroutines.SupervisorJob() + kotlinx.coroutines.Dispatchers.Default,
+ ).launch {
+ preferences.spotifyDj.collect { dev.lelonio.square.backend.spotify.SpotifyDj.enabled = it }
+ }
+
// Not a feature: a line in the log saying whether this install has been
// compiled ahead of time yet. See reportProfileStatus.
reportProfileStatus(
diff --git a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
index 10ff4b9..bd360c7 100644
--- a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
@@ -132,7 +132,9 @@ class SpotifyBackend(private val container: SquareApplication) : MusicBackend {
likedSongs().let { liked ->
listOfNotNull(liked) +
rootlist().filterNot {
- it.uri == liked?.uri || it.uri == Catalog.DJ_URI || it.uri.startsWith("spotify:station:")
+ it.uri == liked?.uri ||
+ (it.uri == Catalog.DJ_URI && !dev.lelonio.square.backend.spotify.SpotifyDj.enabled) ||
+ it.uri.startsWith("spotify:station:")
}
}
diff --git a/app/src/main/java/dev/lelonio/square/backend/spotify/SpotifyDj.kt b/app/src/main/java/dev/lelonio/square/backend/spotify/SpotifyDj.kt
new file mode 100644
index 0000000..25c4b1a
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/backend/spotify/SpotifyDj.kt
@@ -0,0 +1,191 @@
+package dev.lelonio.square.backend.spotify
+
+import android.content.Context
+import dev.lelonio.square.SquareApplication
+import dev.lelonio.square.data.Catalog
+import dev.lelonio.square.data.CatalogTrack
+import dev.lelonio.square.nativecore.NativeBridge
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
+import kotlinx.coroutines.withContext
+import org.json.JSONObject
+import java.nio.ByteBuffer
+import java.nio.ByteOrder
+
+/**
+ * Spotify's DJ: an endless session of tracks, with a voice between them.
+ *
+ * The DJ is listed among the account's playlists, and asked for as one it has
+ * nothing in it. What it plays comes a few tracks at a time from its own
+ * session service, each page naming the next, and some tracks come with a line
+ * to say before or after them, which Spotify synthesises on request. The engine
+ * does the asking, the synthesising and the saying (native/src/dj.rs); this is
+ * the app's side: starting a session, keeping the queue fed as it plays, and
+ * whether the listener wants any of it.
+ *
+ * Played as a queue of loose tracks filed under the DJ, so the account still
+ * records the listens against it and other devices name it.
+ */
+object SpotifyDj {
+
+ /** The setting, mirrored here so the pages that list playlists can ask. */
+ @Volatile
+ var enabled = true
+
+ fun isDj(contextUri: String?) = contextUri == Catalog.DJ_URI
+
+ /** The page after the last one read, where the session's next tracks are. */
+ @Volatile
+ private var nextPage = ""
+
+ private val _speaking = MutableStateFlow(false)
+
+ /**
+ * True while the DJ is talking, from the moment its voice reaches the
+ * output to the first packet of music after it. The player shows the DJ
+ * then instead of the song, which has not started.
+ */
+ val speaking: StateFlow<Boolean> = _speaking.asStateFlow()
+
+ private val _level = MutableStateFlow(0f)
+
+ /** How loud the DJ is right now, 0 to 1, for the picture that moves with it. */
+ val level: StateFlow<Float> = _level.asStateFlow()
+
+ /** Close to the DJ's own: a green, a blue, a violet. */
+ private val DEFAULT_COLORS = listOf(0xFF1ED760.toInt(), 0xFF2D46B9.toInt(), 0xFF8D67AB.toInt())
+
+ private val _colors = MutableStateFlow(DEFAULT_COLORS)
+
+ /** The DJ's own colours, top to bottom, as its session gives them. */
+ val colors: StateFlow<List<Int>> = _colors.asStateFlow()
+
+ /** Called by the output; see AudioOutput.writeVoice. */
+ fun setSpeaking(on: Boolean) {
+ _speaking.value = on
+ if (!on) _level.value = 0f
+ }
+
+ /**
+ * Measures a packet of the DJ's voice: 16-bit little-endian PCM, read
+ * without moving the buffer the output is about to write.
+ */
+ fun hear(data: ByteBuffer, sizeInBytes: Int) {
+ val pcm = data.duplicate().order(ByteOrder.LITTLE_ENDIAN)
+ val samples = sizeInBytes / 2
+ if (samples == 0) return
+ var sum = 0.0
+ var i = 0
+ while (i < samples) {
+ val value = pcm.getShort(i * 2) / 32768.0
+ sum += value * value
+ i += 2 // one channel is enough to see a voice move
+ }
+ val rms = kotlin.math.sqrt(sum / (samples / 2).coerceAtLeast(1))
+ _level.value = (rms * 4).toFloat().coerceIn(0f, 1f)
+ }
+
+ /**
+ * The session's newest tracks, ready to be played: the ones not heard yet.
+ *
+ * The session answers with everything it has played since it began and a
+ * few new tracks at the end, so starting from the top played the same
+ * songs every time the DJ was opened. Waits a moment for the line that
+ * opens the first, which otherwise could never be ready in time: a line
+ * takes seconds to synthesise and the track would start without it.
+ */
+ suspend fun start(context: Context): List<CatalogTrack> = withContext(Dispatchers.IO) {
+ val session = session(context)
+ val heard = heard(context)
+ // A session never met before is heard but for its newest part.
+ val unheard = if (heard.isEmpty() && session.size > SEGMENT) {
+ session.takeLast(SEGMENT)
+ } else {
+ session.filter { it.uri !in heard }.ifEmpty { session.takeLast(SEGMENT) }
+ }
+ prepare(context, unheard)
+ unheard.firstOrNull()?.let { NativeBridge.djAwaitOpening(it.uri, OPENING_WAIT_MS) }
+ unheard
+ }
+
+ /**
+ * The tracks the session has added since it was last asked: the ones not
+ * heard yet. The session answers with its whole history every time, and
+ * leaving that to the queue to sort out put the songs heard at the start
+ * back at the end of it. The caller hands them to [prepare].
+ */
+ suspend fun more(context: Context): List<CatalogTrack> = withContext(Dispatchers.IO) {
+ val heard = heard(context)
+ // The next page is what makes the session go on: asked for again,
+ // the session itself answers with the same tracks.
+ session(context, nextPage).filter { it.uri !in heard }
+ }
+
+ /** Has the lines of [tracks] made, and remembers them as heard. */
+ fun prepare(context: Context, tracks: List<CatalogTrack>) {
+ if (tracks.isEmpty()) return
+ NativeBridge.djPrepare(org.json.JSONArray(tracks.map { it.uri }).toString())
+ val store = context.getSharedPreferences(STORE, Context.MODE_PRIVATE)
+ val heard = (heard(context) + tracks.map { it.uri }).toList().takeLast(HEARD_CAP)
+ store.edit().putString(KEY_HEARD, org.json.JSONArray(heard).toString()).apply()
+ }
+
+ private fun heard(context: Context): Set<String> {
+ val raw = context.getSharedPreferences(STORE, Context.MODE_PRIVATE).getString(KEY_HEARD, null)
+ ?: return emptySet()
+ return runCatching {
+ val list = org.json.JSONArray(raw)
+ (0 until list.length()).map { list.getString(it) }.toCollection(LinkedHashSet())
+ }.getOrDefault(emptySet())
+ }
+
+ private suspend fun session(context: Context, page: String = ""): List<CatalogTrack> {
+ val app = context.applicationContext as SquareApplication
+ val answer = JSONObject(
+ NativeBridge.djSession(page, app.language.language(), app.pathfinderKeys.appVersion),
+ )
+ nextPage = answer.optString("next")
+ answer.optJSONObject("colors")?.let { colors ->
+ val parsed = listOf("top", "mid", "bottom").mapNotNull { parseColor(colors.optString(it)) }
+ if (parsed.size == 3) _colors.value = parsed
+ }
+ val uris = answer.optJSONArray("tracks")?.let { list ->
+ (0 until list.length()).map { list.getString(it) }
+ }.orEmpty()
+ if (uris.isEmpty()) return emptyList()
+ // In the session's order: the lines were written for it.
+ val byUri = Catalog.tracks(uris).associateBy { it.uri }
+ return uris.mapNotNull { byUri[it] }
+ }
+
+ /** `#RRGGBB`, `#AARRGGBB` or `#RRGGBBAA`, whichever the session uses. */
+ private fun parseColor(value: String): Int? {
+ val hex = value.trim().removePrefix("#")
+ return runCatching {
+ when (hex.length) {
+ 6 -> android.graphics.Color.parseColor("#$hex")
+ 8 -> {
+ // Read as RRGGBBAA when the last pair looks like an alpha
+ // and the first does not; Android's own reading otherwise.
+ val asCss = (hex.substring(6, 8).toInt(16) shl 24) or hex.substring(0, 6).toInt(16)
+ if (hex.startsWith("FF", ignoreCase = true)) android.graphics.Color.parseColor("#$hex") else asCss
+ }
+ else -> null
+ }
+ }.getOrNull()
+ }
+
+ private const val STORE = "spotify_dj"
+ private const val KEY_HEARD = "heard"
+
+ /** The tracks a session adds at a time. */
+ private const val SEGMENT = 5
+
+ /** How many heard tracks are remembered: far more than a session repeats. */
+ private const val HEARD_CAP = 500
+
+ /** How long a new session waits for its first line before playing without it. */
+ private const val OPENING_WAIT_MS = 8_000L
+}
diff --git a/app/src/main/java/dev/lelonio/square/data/Catalog.kt b/app/src/main/java/dev/lelonio/square/data/Catalog.kt
index d6077ca..ca5e0e7 100644
--- a/app/src/main/java/dev/lelonio/square/data/Catalog.kt
+++ b/app/src/main/java/dev/lelonio/square/data/Catalog.kt
@@ -266,8 +266,8 @@ object Catalog {
* Spotify's DJ, which is not a playlist.
*
* The access point lists it among them, but it is a generated stream: it has
- * no track list to open, and opening it is the one thing a row in a library
- * promises. The same id for every account.
+ * no track list to open, so it is played as a session rather than opened;
+ * see SpotifyDj. The same id for every account.
*/
const val DJ_URI = "spotify:playlist:37i9dQZF1EYkqdzj48dyYq"
@@ -279,7 +279,8 @@ object Catalog {
suspend fun playlists(): List<CatalogPlaylist> = withContext(Dispatchers.IO) {
json.decodeFromString<List<CatalogPlaylist>>(NativeBridge.rootlist())
- .filterNot { it.uri == DJ_URI }
+ // Kept out unless the DJ is on: played as a list, it has nothing in it.
+ .filterNot { it.uri == DJ_URI && !dev.lelonio.square.backend.spotify.SpotifyDj.enabled }
}
/**
diff --git a/app/src/main/java/dev/lelonio/square/data/PreferencesStore.kt b/app/src/main/java/dev/lelonio/square/data/PreferencesStore.kt
index 145201b..6a8d4ad 100644
--- a/app/src/main/java/dev/lelonio/square/data/PreferencesStore.kt
+++ b/app/src/main/java/dev/lelonio/square/data/PreferencesStore.kt
@@ -236,6 +236,19 @@ class PreferencesStore(context: Context) {
prefs.edit().putBoolean(KEY_AUTOPLAY_INFINITE, value).apply()
}
+ private val _spotifyDj = MutableStateFlow(prefs.getBoolean(KEY_SPOTIFY_DJ, true))
+
+ /**
+ * Whether Spotify's DJ is offered: shown among the playlists, and played
+ * with its lines when opened. Off, it is kept out of sight as before.
+ */
+ val spotifyDj: StateFlow<Boolean> = _spotifyDj.asStateFlow()
+
+ fun setSpotifyDj(value: Boolean) {
+ _spotifyDj.value = value
+ prefs.edit().putBoolean(KEY_SPOTIFY_DJ, value).apply()
+ }
+
private val _skipFade = MutableStateFlow(prefs.getBoolean(KEY_SKIP_FADE, true))
/**
@@ -377,6 +390,7 @@ class PreferencesStore(context: Context) {
const val KEY_KEEP_PLAYING = "keep_playing_on_close"
const val KEY_SPONSOR_BLOCK = "sponsor_block"
const val KEY_AUTOPLAY_INFINITE = "autoplay_infinite"
+ const val KEY_SPOTIFY_DJ = "spotify_dj"
const val KEY_TRIM_SILENCE = "trim_silence"
const val KEY_SKIP_FADE = "skip_fade"
const val KEY_PLAYER_OPEN = "player_open"
diff --git a/app/src/main/java/dev/lelonio/square/data/SpotifyBrowse.kt b/app/src/main/java/dev/lelonio/square/data/SpotifyBrowse.kt
index d3add06..d760f92 100644
--- a/app/src/main/java/dev/lelonio/square/data/SpotifyBrowse.kt
+++ b/app/src/main/java/dev/lelonio/square/data/SpotifyBrowse.kt
@@ -72,7 +72,8 @@ object SpotifyBrowse {
*/
private fun entry(item: JSONObject): CatalogPlaylist? {
val uri = item.optString("uri")
- if (uri.isEmpty() || uri == Catalog.DJ_URI) return null
+ if (uri.isEmpty()) return null
+ if (uri == Catalog.DJ_URI && !dev.lelonio.square.backend.spotify.SpotifyDj.enabled) return null
val data = item.optJSONObject("content")?.optJSONObject("data") ?: return null
return when (data.optString("__typename")) {
diff --git a/app/src/main/java/dev/lelonio/square/data/SpotifyHome.kt b/app/src/main/java/dev/lelonio/square/data/SpotifyHome.kt
index d4bb9a4..c93b689 100644
--- a/app/src/main/java/dev/lelonio/square/data/SpotifyHome.kt
+++ b/app/src/main/java/dev/lelonio/square/data/SpotifyHome.kt
@@ -98,9 +98,10 @@ object SpotifyHome {
val uri = item.optString("uri")
if (uri.isEmpty()) return null
// The DJ is a stream Spotify generates as you listen, not a list: it has
- // no tracks to resolve, so opening it here only ever showed a blank
- // page. It is kept out of the library for the same reason.
- if (uri == Catalog.DJ_URI) return null
+ // no tracks to resolve, so opening it as a page only ever showed a
+ // blank one. With the DJ on it is played as a session instead (see
+ // SpotifyDj); off, it is kept out as before.
+ if (uri == Catalog.DJ_URI && !dev.lelonio.square.backend.spotify.SpotifyDj.enabled) return null
val content = item.optJSONObject("content") ?: return null
val data = content.optJSONObject("data") ?: return null
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
index f556d3f..46a9bcf 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -642,4 +642,32 @@ object NativeBridge {
private external fun nativeDownloadState(trackUri: String): String
private external fun nativeRemoveDownload(trackUri: String)
private external fun nativeCancelDownload(trackUri: String)
+
+ /**
+ * One page of Spotify's DJ session, `{"tracks": [...], "next": "hm://..."}`;
+ * [page] is the `next` of the page before, empty for the first. The DJ's
+ * lines for the page start being synthesised before this returns. See
+ * native/src/dj.rs.
+ */
+ fun djSession(page: String, language: String, appVersion: String): String =
+ nativeDjSession(page, language, appVersion)
+
+ /** Has the DJ's lines for [urisJson], the tracks about to be queued, synthesised. */
+ fun djPrepare(urisJson: String) = runCatching { nativeDjPrepare(urisJson) }
+
+ /** Turns the DJ's lines on for the queue playing, or off with all it had fetched. */
+ fun djSetActive(on: Boolean) = runCatching { nativeDjSetActive(on) }
+
+ /** The listener went straight to [uri]: it is introduced with the DJ's jump line. */
+ fun djJumped(uri: String) = runCatching { nativeDjJumped(uri) }
+
+ /** Waits up to [timeoutMs] for the line that opens [uri]; true once it is ready. */
+ fun djAwaitOpening(uri: String, timeoutMs: Long): Boolean =
+ runCatching { nativeDjAwaitOpening(uri, timeoutMs) }.getOrDefault(false)
+
+ private external fun nativeDjSession(page: String, language: String, appVersion: String): String
+ private external fun nativeDjSetActive(on: Boolean)
+ private external fun nativeDjPrepare(urisJson: String)
+ private external fun nativeDjJumped(uri: String)
+ private external fun nativeDjAwaitOpening(uri: String, timeoutMs: Long): Boolean
}
diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
index 3187743..0f922b7 100644
--- a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
@@ -292,6 +292,10 @@ class AudioOutput {
fun stop() {
discarding = false
playing = false
+ if (voice) {
+ voice = false
+ dev.lelonio.square.backend.spotify.SpotifyDj.setSpeaking(false)
+ }
val output = synchronized(this) {
track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED }
}
@@ -517,6 +521,7 @@ class AudioOutput {
if (discarding) return
val output = synchronized(this) { ensureTrack(sampleRate, channels) } ?: return
+ if (voice) leaveVoice()
val currentSpeed = speed
val currentPitch = pitch
@@ -579,6 +584,56 @@ class AudioOutput {
}.onFailure { android.util.Log.w(TAG, "the platform refused $wantedSpeed x: ${it.message}") }
}
+ /**
+ * Writes one packet of Spotify's DJ talking: as it is, past every effect.
+ *
+ * Speed, pitch, karaoke and the room are the listener's choices about the
+ * music, and the DJ is not the music: slowed and reverbed it was a voice
+ * nobody chose to hear that way, and the karaoke took its words out. The
+ * effects come back with the next packet of music. See `speaking` in the
+ * vendored player for how the engine tells the two apart.
+ */
+ @Suppress("unused")
+ fun writeVoice(data: ByteBuffer, sizeInBytes: Int, sampleRate: Int, channels: Int) {
+ if (discarding) return
+ val output = synchronized(this) { ensureTrack(sampleRate, channels) } ?: return
+ if (!voice) enterVoice(output)
+ dev.lelonio.square.backend.spotify.SpotifyDj.hear(data, sizeInBytes)
+ writeAll(output, data, sizeInBytes)
+ }
+
+ /** Whether the packets being written are the DJ's voice; see [writeVoice]. */
+ @Volatile
+ private var voice = false
+
+ private fun enterVoice(output: AudioTrack) {
+ voice = true
+ synchronized(this) {
+ // What the stretcher holds is music, and would be heard after the
+ // line; the room would ring with the voice.
+ stretcher?.release()
+ stretcher = null
+ suspendReverb()
+ if (platformSpeed != 1f || platformPitch != 1f) {
+ runCatching {
+ output.playbackParams = output.playbackParams.setSpeed(1f).setPitch(1f)
+ platformSpeed = 1f
+ platformPitch = 1f
+ }
+ }
+ }
+ dev.lelonio.square.backend.spotify.SpotifyDj.setSpeaking(true)
+ }
+
+ private fun leaveVoice() {
+ voice = false
+ synchronized(this) {
+ if (reverbAmount > 0f) applyReverb()
+ if (lightEffects) applyPlatformRate()
+ }
+ dev.lelonio.square.backend.spotify.SpotifyDj.setSpeaking(false)
+ }
+
/** Takes the middle of the stage away; see [CentreExtractor]. */
private val vocals = CentreExtractor()
diff --git a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
index eb9e0f5..6d59667 100644
--- a/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/LibrespotPlayer.kt
@@ -97,6 +97,11 @@ class LibrespotPlayer(
handler.postDelayed(giveUpOnOwnQueue, OWN_QUEUE_MS)
val index = queue.currentIndex
val contextUri = queue.contextUri.orEmpty()
+ // The DJ's lines are said only over the DJ's own queue; see SpotifyDj.
+ NativeBridge.djSetActive(
+ dev.lelonio.square.backend.spotify.SpotifyDj.enabled &&
+ dev.lelonio.square.backend.spotify.SpotifyDj.isDj(contextUri),
+ )
// Never as a context, whatever it is.
//
// Handing the engine a playlist URI asks Spotify to resolve the list
diff --git a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt
index fccb7ca..7353911 100644
--- a/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/PlaybackService.kt
@@ -159,6 +159,7 @@ class PlaybackService : MediaLibraryService() {
player.pause()
return
}
+ noteDjJump(mediaItem, reason)
maybeTriggerAutoplay(mediaItem)
}
@@ -169,8 +170,79 @@ class PlaybackService : MediaLibraryService() {
}
}
+ /** Where the queue stood at the last change of song; see [noteDjJump]. */
+ private var lastItemIndex = -1
+
+ /**
+ * A song in the DJ's queue gone to directly, rather than reached by
+ * playing on or by a skip: the DJ has a line written for that. Told to the
+ * engine as soon as the queue moves, which is before the song has loaded
+ * and so before the line is chosen.
+ */
+ private fun noteDjJump(mediaItem: androidx.media3.common.MediaItem?, reason: Int) {
+ val index = player.currentMediaItemIndex
+ val previous = lastItemIndex
+ lastItemIndex = index
+ if (reason != androidx.media3.common.Player.MEDIA_ITEM_TRANSITION_REASON_SEEK) return
+ if (previous < 0 || kotlin.math.abs(index - previous) == 1) return
+ val item = mediaItem ?: return
+ if (!dev.lelonio.square.backend.spotify.SpotifyDj.isDj(contextOf(item))) return
+ NativeBridge.djJumped(item.mediaId)
+ }
+
+ private fun contextOf(item: androidx.media3.common.MediaItem?): String? =
+ item?.mediaMetadata?.extras?.getString(dev.lelonio.square.ui.EXTRA_CONTEXT_URI)
+
+ private var djInFlight = false
+
+ /**
+ * Keeps the DJ's queue fed: its next page when two songs are left, the
+ * way autoplay keeps any other queue going. The DJ never runs out.
+ */
+ private fun maybeContinueDj(current: androidx.media3.common.MediaItem): Boolean {
+ if (!dev.lelonio.square.backend.spotify.SpotifyDj.isDj(contextOf(current))) return false
+ if (!dev.lelonio.square.backend.spotify.SpotifyDj.enabled) return true
+ val remaining = player.mediaItemCount - player.currentMediaItemIndex - 1
+ if (remaining > 2 || djInFlight) return true
+ djInFlight = true
+ scope.launch(Dispatchers.IO) {
+ try {
+ val more = dev.lelonio.square.backend.spotify.SpotifyDj.more(this@PlaybackService)
+ withContext(Dispatchers.Main) {
+ val queued = (0 until player.mediaItemCount).map { player.getMediaItemAt(it).mediaId }.toSet()
+ val fresh = more.filter { it.uri !in queued }
+ dev.lelonio.square.backend.spotify.SpotifyDj.prepare(this@PlaybackService, fresh)
+ val items = fresh.map { track ->
+ androidx.media3.common.MediaItem.Builder()
+ .setMediaId(track.uri)
+ .setUri(track.uri)
+ .setMediaMetadata(
+ androidx.media3.common.MediaMetadata.Builder()
+ .setTitle(track.name)
+ .setArtist(track.artist)
+ .setAlbumTitle(track.album)
+ .setDurationMs(track.durationMs.takeIf { it > 0 })
+ .setArtworkUri(track.artworkUrl?.let(android.net.Uri::parse))
+ .build(),
+ )
+ .build()
+ }
+ android.util.Log.i(TAG, "DJ: appending ${items.size} tracks")
+ if (items.isNotEmpty()) player.addMediaItems(items)
+ }
+ } catch (t: Throwable) {
+ android.util.Log.w(TAG, "DJ: the next page failed", t)
+ } finally {
+ djInFlight = false
+ }
+ }
+ return true
+ }
+
private fun maybeTriggerAutoplay(mediaItem: androidx.media3.common.MediaItem?) {
if (!::container.isInitialized) return
+ // The DJ's queue is fed by the DJ, not by autoplay.
+ (mediaItem ?: player.currentMediaItem)?.let { if (maybeContinueDj(it)) return }
if (!container.preferences.autoplayInfinite.value) return
if (player.repeatMode != androidx.media3.common.Player.REPEAT_MODE_OFF) return
diff --git a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
index 5561365..b2902dc 100644
--- a/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/SquareApp.kt
@@ -727,11 +727,20 @@ fun SquareApp(
LaunchedEffect(spotifyVideoOn, playback.hasNext, playback.hasPrevious) {
if (!spotifyVideoOn) skipsBeforeVideo = playback.hasNext to playback.hasPrevious
}
+ // While the DJ talks, the DJ is what is playing: the song after it has not
+ // started, and its cover and title would be a promise made too early.
+ val djTalking by dev.lelonio.square.backend.spotify.SpotifyDj.speaking.collectAsStateWithLifecycle()
+ val djSpeaking = djTalking && dev.lelonio.square.backend.spotify.SpotifyDj.isDj(playback.contextUri)
+ val djTalkingLabel = stringResource(R.string.spotify_dj_talking)
val playerState = if (spotifyVideoOn) {
playback.copy(
hasNext = skipsBeforeVideo.first,
hasPrevious = skipsBeforeVideo.second,
)
+ } else if (djSpeaking) {
+ // The song's artists go too: the line under the title names them
+ // before the plain artist string.
+ playback.copy(title = DJ_LABEL, artist = djTalkingLabel, artists = emptyList(), artistUri = null)
} else {
playback
}
@@ -930,6 +939,32 @@ fun SquareApp(
}
val scope = rememberCoroutineScope()
+ // Spotify's DJ is played, not opened: as a list it has nothing in it. Its
+ // first line takes a few seconds to be synthesised, so the listener is
+ // told it is coming rather than left with a tap that did nothing.
+ val djFailed = stringResource(R.string.spotify_dj_failed)
+ // The DJ coming on, shown over everything while its session is fetched
+ // and its first line synthesised; see DjStarting.
+ var djStarting by remember { mutableStateOf(false) }
+ fun startDj() {
+ if (djStarting) return
+ djStarting = true
+ scope.launch {
+ val tracks = runCatching { dev.lelonio.square.backend.spotify.SpotifyDj.start(context) }
+ .onFailure { android.util.Log.w("SquareDj", "dj session failed: ${it.message}") }
+ .getOrDefault(emptyList())
+ if (tracks.isEmpty()) {
+ djStarting = false
+ android.widget.Toast.makeText(context, djFailed, android.widget.Toast.LENGTH_LONG).show()
+ return@launch
+ }
+ onPlay(tracks, 0, dev.lelonio.square.data.Catalog.DJ_URI, false, DJ_LABEL, 0L)
+ // Straight into the player, where the DJ is about to talk.
+ expand.animateTo(1f, expandSpec)
+ djStarting = false
+ }
+ }
+
// Remembered for the next launch. Written when the animation settles rather
// than on every frame of the drag.
LaunchedEffect(Unit) {
@@ -1340,6 +1375,10 @@ fun SquareApp(
) {
Box(Modifier.fillMaxSize()) {
val navController = rememberNavController()
+ val openList: (CatalogPlaylist) -> Unit = { playlist ->
+ if (playlist.uri == dev.lelonio.square.data.Catalog.DJ_URI) startDj()
+ else navController.openPlaylist(viewModel, playlist)
+ }
val currentEntry by navController.currentBackStackEntryAsState()
val route = currentEntry?.destination?.route
@@ -1608,7 +1647,7 @@ fun SquareApp(
onRetry = { viewModel.retryFailed() },
onGoOffline = viewModel::goOffline,
onLogOut = viewModel::logOut,
- onOpenPlaylist = { navController.openPlaylist(viewModel, it) },
+ onOpenPlaylist = openList,
playlistOrder = playlistOrder,
recent = recent,
onPlayRecent = { tracks, index ->
@@ -1675,7 +1714,7 @@ fun SquareApp(
onPlay = { tracks, index ->
onPlay(tracks, index, null, false, newLabel, 0L)
},
- onOpen = { navController.openPlaylist(viewModel, it) },
+ onOpen = openList,
)
return@composable
}
@@ -1722,7 +1761,7 @@ fun SquareApp(
onPlay = { tracks, index ->
onPlay(tracks, index, null, false, radioLabel, 0L)
},
- onOpen = { navController.openPlaylist(viewModel, it) },
+ onOpen = openList,
)
return@composable
}
@@ -1829,7 +1868,7 @@ fun SquareApp(
onRetry = { viewModel.retryFailed() },
onGoOffline = viewModel::goOffline,
onLogOut = viewModel::logOut,
- onOpenPlaylist = { navController.openPlaylist(viewModel, it) },
+ onOpenPlaylist = openList,
listeningEvents = listeningEvents,
listeningFromSpotify = backend == dev.lelonio.square.backend.BackendId.SPOTIFY,
playlistOrder = playlistOrder,
@@ -2196,7 +2235,8 @@ fun SquareApp(
) -> Unit)? = if (playback.hasItem) {
{ accessoryModifier, _ ->
FloatingMiniPlayer(
- state = playback,
+ state = if (djSpeaking) playerState else playback,
+ djSpeaking = djSpeaking,
positionMs = positionMs,
remoteLabel = remote?.takeIf { showConnectedDevice }?.let { remoteLabel },
modifier = accessoryModifier
@@ -2901,6 +2941,7 @@ fun SquareApp(
) {
PlayerScreen(
state = playerState,
+ djSpeaking = djSpeaking,
// The title is the way onto the record. The
// address travels with the queue where the
// source gave one; where it did not — a list
@@ -3239,6 +3280,18 @@ fun SquareApp(
}
}
+ // The DJ coming on: its lights, breathing, while the session
+ // is fetched and its first line made. In place of a toast,
+ // which said the same thing in a corner and went away before
+ // the music came.
+ androidx.compose.animation.AnimatedVisibility(
+ visible = djStarting,
+ enter = androidx.compose.animation.fadeIn(androidx.compose.animation.core.tween(320)),
+ exit = androidx.compose.animation.fadeOut(androidx.compose.animation.core.tween(420)),
+ ) {
+ dev.lelonio.square.ui.player.DjStarting()
+ }
+
}
// Above the tab bar and the now-playing bar, which is the whole
@@ -3964,6 +4017,9 @@ private fun RowScope.BottomItem(
}
}
+/** What the player names the DJ's queue after. */
+private const val DJ_LABEL = "DJ"
+
/** Loads the playlist and shows it, from whichever tab asked. */
private fun NavHostController.openPlaylist(viewModel: MainViewModel, playlist: CatalogPlaylist) {
viewModel.openPlaylist(playlist)
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/DjMark.kt b/app/src/main/java/dev/lelonio/square/ui/player/DjMark.kt
new file mode 100644
index 0000000..3d0ec8b
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/ui/player/DjMark.kt
@@ -0,0 +1,44 @@
+package dev.lelonio.square.ui.player
+
+import androidx.compose.foundation.background
+import androidx.compose.foundation.layout.padding
+import androidx.compose.foundation.shape.RoundedCornerShape
+import androidx.compose.material3.MaterialTheme
+import androidx.compose.material3.Text
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.graphics.Color
+import androidx.compose.ui.text.font.FontWeight
+import androidx.compose.ui.unit.TextUnit
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+
+/**
+ * Says that Spotify's DJ is the one choosing the songs, and talking between
+ * them: a small tag in the ink of whatever it sits on, beside the song in the
+ * mini player and beside the source at the top of the player.
+ */
+@Composable
+internal fun DjMark(
+ color: Color,
+ modifier: Modifier = Modifier,
+ fontSize: TextUnit = 10.sp,
+) {
+ Text(
+ "DJ",
+ style = MaterialTheme.typography.labelSmall.copy(
+ fontSize = fontSize,
+ fontWeight = FontWeight.Bold,
+ letterSpacing = 0.6.sp,
+ ),
+ color = color,
+ maxLines = 1,
+ modifier = modifier
+ .background(color.copy(alpha = 0.18f), RoundedCornerShape(5.dp))
+ .padding(horizontal = 5.dp, vertical = 1.dp),
+ )
+}
+
+/** Whether [contextUri] is the DJ's queue. */
+internal fun isDjQueue(contextUri: String?) =
+ dev.lelonio.square.backend.spotify.SpotifyDj.isDj(contextUri)
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/DjVoice.kt b/app/src/main/java/dev/lelonio/square/ui/player/DjVoice.kt
new file mode 100644
index 0000000..1d05233
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/ui/player/DjVoice.kt
@@ -0,0 +1,130 @@
+package dev.lelonio.square.ui.player
+
+import androidx.compose.animation.core.LinearEasing
+import androidx.compose.animation.core.Spring
+import androidx.compose.animation.core.animateFloat
+import androidx.compose.animation.core.animateFloatAsState
+import androidx.compose.animation.core.infiniteRepeatable
+import androidx.compose.animation.core.rememberInfiniteTransition
+import androidx.compose.animation.core.spring
+import androidx.compose.animation.core.tween
+import androidx.compose.foundation.Canvas
+import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.height
+import androidx.compose.ui.input.pointer.pointerInput
+import androidx.compose.ui.unit.dp
+import androidx.compose.ui.unit.sp
+import androidx.compose.runtime.Composable
+import androidx.compose.runtime.getValue
+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.lerp
+import androidx.lifecycle.compose.collectAsStateWithLifecycle
+import dev.lelonio.square.backend.spotify.SpotifyDj
+import kotlin.math.PI
+import kotlin.math.cos
+import kotlin.math.min
+import kotlin.math.sin
+
+/**
+ * Spotify's DJ, drawn while it talks: three soft lights in its own colours,
+ * drifting slowly and swelling with its voice, over the darkest of them.
+ *
+ * In place of the cover while a line is being said, since the song it would
+ * show has not started; and, still, while a session is being prepared.
+ * [idle] is that second case: the lights breathe on their own, with no voice
+ * to follow.
+ */
+@Composable
+internal fun DjVoice(modifier: Modifier = Modifier, idle: Boolean = false) {
+ val colors by SpotifyDj.colors.collectAsStateWithLifecycle()
+ val heard by SpotifyDj.level.collectAsStateWithLifecycle()
+ val transition = rememberInfiniteTransition(label = "djVoice")
+ val phase by transition.animateFloat(
+ initialValue = 0f,
+ targetValue = (2 * PI).toFloat(),
+ animationSpec = infiniteRepeatable(tween(9_000, easing = LinearEasing)),
+ label = "djDrift",
+ )
+ val breath by transition.animateFloat(
+ initialValue = 0f,
+ targetValue = (2 * PI).toFloat(),
+ animationSpec = infiniteRepeatable(tween(2_400, easing = LinearEasing)),
+ label = "djBreath",
+ )
+ // Quick to rise and slower to fall, as a voice's level is read.
+ val level by animateFloatAsState(
+ targetValue = if (idle) 0.25f + 0.2f * sin(breath) else heard,
+ animationSpec = spring(stiffness = Spring.StiffnessMediumLow),
+ label = "djLevel",
+ )
+
+ val lights = colors.map(::Color)
+ val ground = lights.fold(Color.Black) { dark, light -> lerp(dark, light, 0.12f) }
+
+ Canvas(modifier) {
+ drawRect(
+ Brush.verticalGradient(
+ listOf(lerp(ground, lights[0], 0.25f), ground, lerp(ground, lights[2], 0.2f)),
+ ),
+ )
+ val unit = min(size.width, size.height)
+ lights.forEachIndexed { index, light ->
+ val turn = phase + index * 2.1f
+ val center = Offset(
+ size.width / 2 + cos(turn) * size.width * 0.14f,
+ size.height * (0.3f + 0.2f * index) + sin(turn * 1.3f) * size.height * 0.04f,
+ )
+ val radius = unit * (0.42f + 0.06f * sin(turn)) * (0.9f + level * 0.55f)
+ drawCircle(
+ Brush.radialGradient(
+ listOf(light.copy(alpha = 0.85f), light.copy(alpha = 0.35f), Color.Transparent),
+ center = center,
+ radius = radius,
+ ),
+ radius = radius,
+ center = center,
+ )
+ }
+ }
+}
+
+/**
+ * The DJ coming on: its lights over the whole screen, breathing on their own,
+ * while the session is fetched and its first line made, which takes seconds.
+ * Takes every touch meanwhile, so nothing else is started under it.
+ */
+@Composable
+internal fun DjStarting() {
+ androidx.compose.foundation.layout.Box(
+ Modifier
+ .fillMaxSize()
+ .swallowTouches(),
+ contentAlignment = androidx.compose.ui.Alignment.Center,
+ ) {
+ DjVoice(Modifier.matchParentSize(), idle = true)
+ androidx.compose.foundation.layout.Column(
+ horizontalAlignment = androidx.compose.ui.Alignment.CenterHorizontally,
+ ) {
+ DjMark(color = Color.White, fontSize = 28.sp)
+ androidx.compose.foundation.layout.Spacer(Modifier.height(18.dp))
+ androidx.compose.material3.Text(
+ androidx.compose.ui.res.stringResource(dev.lelonio.square.R.string.spotify_dj_starting),
+ style = androidx.compose.material3.MaterialTheme.typography.titleMedium,
+ color = Color.White.copy(alpha = 0.85f),
+ )
+ }
+ }
+}
+
+/** Swallows every touch, so what is under it cannot be pressed. */
+private fun Modifier.swallowTouches(): Modifier =
+ pointerInput(Unit) {
+ awaitPointerEventScope {
+ while (true) {
+ awaitPointerEvent().changes.forEach { it.consume() }
+ }
+ }
+ }
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt b/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt
index 2417cbd..f51ddb2 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/FloatingMiniPlayer.kt
@@ -52,6 +52,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.LayoutDirection
+import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.adamglin.PhosphorIcons
@@ -94,6 +95,8 @@ fun FloatingMiniPlayer(
* less than which room, and the screen was saying nothing about it at all.
*/
remoteLabel: String? = null,
+ /** Spotify's DJ is talking: its lights in place of the cover; see DjVoice. */
+ djSpeaking: Boolean = false,
/** Folded: the strip has room for the song and one button, nothing else. */
inline: Boolean = false,
collapseProgress: Float? = null,
@@ -191,7 +194,13 @@ fun FloatingMiniPlayer(
vertical = androidx.compose.ui.unit.lerp(8.dp, 6.dp, fold),
),
) {
- Artwork(
+ if (djSpeaking) {
+ DjVoice(
+ Modifier
+ .size(artSize)
+ .clip(androidx.compose.foundation.shape.RoundedCornerShape(9.dp)),
+ )
+ } else Artwork(
url = state.artworkUrl,
title = state.title,
modifier = Modifier.size(artSize),
@@ -222,6 +231,14 @@ fun FloatingMiniPlayer(
)
}
Row(verticalAlignment = Alignment.CenterVertically) {
+ // The DJ choosing, and talking; see DjMark.
+ if (remoteLabel == null && isDjQueue(state.contextUri)) {
+ DjMark(
+ color = contentColor,
+ fontSize = 9.sp,
+ modifier = Modifier.padding(end = 5.dp),
+ )
+ }
if (remoteLabel != null) {
Icon(
PhosphorIcons.Fill.Devices,
diff --git a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
index 7f8849e..da7527c 100644
--- a/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/player/PlayerScreen.kt
@@ -299,6 +299,11 @@ private enum class Stage {
@Composable
fun PlayerScreen(
state: PlaybackState,
+ /**
+ * Spotify's DJ is talking: its lights take the place of the cover, since
+ * the song they would show has not started. See DjVoice.
+ */
+ djSpeaking: Boolean = false,
/** Read only inside the waveform, to keep position updates off the rest. */
positionMs: State<Long>,
onCollapse: () -> Unit,
@@ -633,6 +638,15 @@ fun PlayerScreen(
// buttons and the panels refract; see lightBackdrop.
Box(Modifier.fillMaxSize().layerBackdrop(lightBackdrop)) {
AmbientLight(ambient)
+ // In this layer so the glass refracts the DJ, not the cover under it.
+ val djShown by animateFloatAsState(
+ targetValue = if (djSpeaking) 1f else 0f,
+ animationSpec = tween(if (djSpeaking) 450 else 700),
+ label = "djShown",
+ )
+ if (djShown > 0f) {
+ DjVoice(Modifier.fillMaxSize().graphicsLayer { alpha = djShown })
+ }
}
Box(
Modifier
@@ -954,6 +968,17 @@ fun PlayerScreen(
.background(Color.Black.copy(alpha = panelDim)),
)
}
+
+ // The DJ over the cover it replaces while it talks: the cover is drawn
+ // in this layer, above the light one, and hid it there.
+ val djOver by animateFloatAsState(
+ targetValue = if (djSpeaking) 1f else 0f,
+ animationSpec = tween(if (djSpeaking) 450 else 700),
+ label = "djOver",
+ )
+ if (djOver > 0f) {
+ DjVoice(Modifier.fillMaxSize().graphicsLayer { alpha = djOver })
+ }
}
CompositionLocalProvider(LocalContentColor provides GlassInk) {
@@ -1005,6 +1030,7 @@ fun PlayerScreen(
onWatchVideo = onWatchVideo,
videoOn = videoOn,
modifier = Modifier.retreat(chrome),
+ dj = isDjQueue(state.contextUri),
)
// In its place while the lyrics are read alone: the song,
@@ -1921,6 +1947,8 @@ private fun TopBar(
onWatchVideo: (() -> Unit)?,
videoOn: Boolean,
modifier: Modifier = Modifier,
+ /** The DJ's queue: the source is marked as the DJ's; see DjMark. */
+ dj: Boolean = false,
) {
Row(
modifier
@@ -1966,7 +1994,13 @@ private fun TopBar(
// dims a stopped clip was then darkening the very thing those
// letters needed to be lighter than. A photograph has no side to
// be on: what works over one is light type with a shadow under it.
- Text(
+ if (dj && panel == PlayerPanel.NONE) {
+ // The DJ's queue: the mark alone says where the songs come
+ // from, and who is talking between them.
+ Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
+ DjMark(color = Color.White, fontSize = 13.sp)
+ }
+ } else Text(
title,
style = MaterialTheme.typography.titleMedium.copy(
shadow = Shadow(
diff --git a/app/src/main/java/dev/lelonio/square/ui/settings/SettingsScreen.kt b/app/src/main/java/dev/lelonio/square/ui/settings/SettingsScreen.kt
index 1b7f063..60143cb 100644
--- a/app/src/main/java/dev/lelonio/square/ui/settings/SettingsScreen.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/settings/SettingsScreen.kt
@@ -510,6 +510,20 @@ fun SettingsScreen(
AutoplaySection(backdrop)
}
+ // Spotify's DJ: offered and played with its voice, or kept out of sight.
+ if (shown == SettingsPage.Playback && showSpotify && "listening" in openGroups) groupItem("spotify-dj", Group.LISTENING) {
+ val enabled by app.preferences.spotifyDj.collectAsStateWithLifecycle()
+ Section(stringResource(R.string.spotify_dj)) {
+ DownloadSwitch(
+ label = stringResource(R.string.spotify_dj_title),
+ icon = PhosphorIcons.Regular.MicrophoneStage,
+ checked = enabled,
+ backdrop = backdrop,
+ onChange = app.preferences::setSpotifyDj,
+ )
+ }
+ }
+
if (shown == SettingsPage.Playback && showSpotify && "listening" in openGroups) groupItem("connected-device-label", Group.LISTENING) {
val enabled by app.preferences.showConnectedDevice.collectAsStateWithLifecycle()
Section(stringResource(R.string.connected_device_label)) {
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 407327c..f494237 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -598,4 +598,8 @@
<string name="lyrics_preferred_note">Es werden nur Songtexte der gewählten Quelle angezeigt: Hat sie keinen für den Song, wird keiner angezeigt. Automatisch wählt die beste verfügbare.</string>
<string name="youtube_account_fallback_name">Google-Konto</string>
<string name="youtube_sign_in_failed">Anmeldung bei YouTube Music fehlgeschlagen. Versuch es noch einmal; wenn es wieder passiert, speichere einen Bericht unter Einstellungen → Info.</string>
+ <string name="spotify_dj_title">Spotify-DJ, mit seiner Stimme</string>
+ <string name="spotify_dj_starting">Der DJ macht sich bereit…</string>
+ <string name="spotify_dj_failed">Der DJ ist gerade nicht verfügbar. Versuch es später noch einmal.</string>
+ <string name="spotify_dj_talking">spricht</string>
</resources>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index e3bae35..0825ae1 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -604,4 +604,8 @@
<string name="lyrics_preferred_note">Solo se muestran letras de la fuente elegida: si no tiene la de la canción, no se muestra ninguna. Automática elige la mejor disponible.</string>
<string name="youtube_account_fallback_name">Cuenta de Google</string>
<string name="youtube_sign_in_failed">No se pudo iniciar sesión en YouTube Music. Inténtalo de nuevo; si vuelve a pasar, guarda un informe en Ajustes → Información.</string>
+ <string name="spotify_dj_title">DJ de Spotify, con su voz</string>
+ <string name="spotify_dj_starting">El DJ se está preparando…</string>
+ <string name="spotify_dj_failed">El DJ no está disponible ahora. Inténtalo más tarde.</string>
+ <string name="spotify_dj_talking">está hablando</string>
</resources>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index 3af2ebc..baf48d3 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -598,4 +598,8 @@
<string name="lyrics_preferred_note">Seules les paroles de la source choisie sont affichées : si elle n’en a pas pour le morceau, rien n’est affiché. Automatique choisit la meilleure disponible.</string>
<string name="youtube_account_fallback_name">Compte Google</string>
<string name="youtube_sign_in_failed">Impossible de se connecter à YouTube Music. Réessayez ; si cela se reproduit, enregistrez un rapport dans Réglages → À propos.</string>
+ <string name="spotify_dj_title">DJ Spotify, avec sa voix</string>
+ <string name="spotify_dj_starting">Le DJ se prépare…</string>
+ <string name="spotify_dj_failed">Le DJ n\'est pas disponible pour le moment. Réessayez plus tard.</string>
+ <string name="spotify_dj_talking">parle</string>
</resources>
diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml
index 4d84b3f..fe15427 100644
--- a/app/src/main/res/values-hi/strings.xml
+++ b/app/src/main/res/values-hi/strings.xml
@@ -613,4 +613,8 @@
<string name="lyrics_preferred_note">सिर्फ़ चुने गए स्रोत के बोल दिखाए जाते हैं: अगर उसके पास गाने के बोल नहीं हैं, तो कुछ नहीं दिखेगा। स्वचालित सबसे अच्छा उपलब्ध स्रोत चुनता है।</string>
<string name="youtube_account_fallback_name">Google खाता</string>
<string name="youtube_sign_in_failed">YouTube Music में साइन इन नहीं हो सका। फिर से कोशिश करें; अगर फिर भी हो, तो सेटिंग → परिचय में रिपोर्ट सेव करें।</string>
+ <string name="spotify_dj_title">Spotify DJ, उसकी आवाज़ के साथ</string>
+ <string name="spotify_dj_starting">DJ तैयार हो रहा है…</string>
+ <string name="spotify_dj_failed">DJ अभी उपलब्ध नहीं है। बाद में फिर कोशिश करें।</string>
+ <string name="spotify_dj_talking">बोल रहा है</string>
</resources>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index acda56e..4cdbd96 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -635,4 +635,8 @@
<string name="lyrics_preferred_note">Vengono mostrati solo i testi della fonte scelta: se non ha il testo del brano, non viene mostrato nulla. Automatica sceglie la migliore disponibile.</string>
<string name="youtube_account_fallback_name">Account Google</string>
<string name="youtube_sign_in_failed">Accesso a YouTube Music non riuscito. Riprova; se succede ancora, salva un report in Impostazioni → Informazioni.</string>
+ <string name="spotify_dj_title">DJ di Spotify, con la sua voce</string>
+ <string name="spotify_dj_starting">Il DJ si sta preparando…</string>
+ <string name="spotify_dj_failed">Il DJ non è disponibile ora. Riprova più tardi.</string>
+ <string name="spotify_dj_talking">sta parlando</string>
</resources>
diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml
index bd95404..50cdcfb 100644
--- a/app/src/main/res/values-ja/strings.xml
+++ b/app/src/main/res/values-ja/strings.xml
@@ -654,5 +654,9 @@
<string name="lyrics_preferred_note">選んだソースの歌詞だけが表示されます。その曲の歌詞がなければ何も表示されません。自動では最適なソースが選ばれます。</string>
<string name="youtube_account_fallback_name">Google アカウント</string>
<string name="youtube_sign_in_failed">YouTube Music にログインできませんでした。もう一度お試しください。繰り返す場合は 設定 → アプリについて からレポートを保存してください。</string>
+ <string name="spotify_dj_title">Spotify DJ(音声つき)</string>
+ <string name="spotify_dj_starting">DJ を準備しています…</string>
+ <string name="spotify_dj_failed">DJ は現在利用できません。しばらくしてからもう一度お試しください。</string>
+ <string name="spotify_dj_talking">話しています</string>
</resources>
diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml
index 0511642..1a9a7a4 100644
--- a/app/src/main/res/values-ko/strings.xml
+++ b/app/src/main/res/values-ko/strings.xml
@@ -622,4 +622,8 @@
<string name="lyrics_preferred_note">선택한 출처의 가사만 표시됩니다. 그 출처에 노래 가사가 없으면 아무것도 표시되지 않습니다. 자동은 가장 좋은 출처를 고릅니다.</string>
<string name="youtube_account_fallback_name">Google 계정</string>
<string name="youtube_sign_in_failed">YouTube Music에 로그인하지 못했습니다. 다시 시도해 보고, 계속되면 설정 → 정보에서 보고서를 저장하세요.</string>
+ <string name="spotify_dj_title">Spotify DJ, 목소리와 함께</string>
+ <string name="spotify_dj_starting">DJ가 준비 중입니다…</string>
+ <string name="spotify_dj_failed">지금은 DJ를 사용할 수 없습니다. 나중에 다시 시도하세요.</string>
+ <string name="spotify_dj_talking">이야기 중</string>
</resources>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index 2356fdc..74f7cf2 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -598,4 +598,8 @@
<string name="lyrics_preferred_note">Só são mostradas letras da fonte escolhida: se ela não tiver a da música, nenhuma é mostrada. Automática escolhe a melhor disponível.</string>
<string name="youtube_account_fallback_name">Conta Google</string>
<string name="youtube_sign_in_failed">Não foi possível entrar no YouTube Music. Tente de novo; se continuar, salve um relatório em Configurações → Sobre.</string>
+ <string name="spotify_dj_title">DJ do Spotify, com a voz dele</string>
+ <string name="spotify_dj_starting">O DJ está se preparando…</string>
+ <string name="spotify_dj_failed">O DJ não está disponível agora. Tente mais tarde.</string>
+ <string name="spotify_dj_talking">está falando</string>
</resources>
diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml
index b96e32f..8770aec 100644
--- a/app/src/main/res/values-ru/strings.xml
+++ b/app/src/main/res/values-ru/strings.xml
@@ -627,4 +627,8 @@
<string name="lyrics_preferred_note">Показываются только тексты из выбранного источника: если у него нет текста песни, ничего не показывается. Автоматически выбирается лучший из доступных.</string>
<string name="youtube_account_fallback_name">Аккаунт Google</string>
<string name="youtube_sign_in_failed">Не удалось войти в YouTube Music. Попробуйте ещё раз; если повторится, сохраните отчёт в Настройки → О приложении.</string>
+ <string name="spotify_dj_title">DJ Spotify, с его голосом</string>
+ <string name="spotify_dj_starting">DJ готовится…</string>
+ <string name="spotify_dj_failed">DJ сейчас недоступен. Попробуйте позже.</string>
+ <string name="spotify_dj_talking">говорит</string>
</resources>
diff --git a/app/src/main/res/values-tr/strings.xml b/app/src/main/res/values-tr/strings.xml
index 2f7c5b3..38a1e01 100644
--- a/app/src/main/res/values-tr/strings.xml
+++ b/app/src/main/res/values-tr/strings.xml
@@ -611,4 +611,8 @@
<string name="lyrics_preferred_note">Yalnızca seçilen kaynağın sözleri gösterilir: şarkı için sözü yoksa hiçbir şey gösterilmez. Otomatik, mevcut en iyisini seçer.</string>
<string name="youtube_account_fallback_name">Google hesabı</string>
<string name="youtube_sign_in_failed">YouTube Music\'e giriş yapılamadı. Tekrar dene; yine olursa Ayarlar → Hakkında\'dan bir rapor kaydet.</string>
+ <string name="spotify_dj_title">Spotify DJ, sesiyle</string>
+ <string name="spotify_dj_starting">DJ hazırlanıyor…</string>
+ <string name="spotify_dj_failed">DJ şu anda kullanılamıyor. Daha sonra tekrar dene.</string>
+ <string name="spotify_dj_talking">konuşuyor</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 10663b7..c889987 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -662,4 +662,9 @@
<string name="lyrics_preferred_note">Only lyrics from the chosen source are shown: if it has none for the song, no lyrics are shown. Automatic picks the best available.</string>
<string name="youtube_account_fallback_name">Google account</string>
<string name="youtube_sign_in_failed">Couldn\'t sign in to YouTube Music. Try again, and if it keeps happening, save a report in Settings → About.</string>
+ <string name="spotify_dj" translatable="false">DJ</string>
+ <string name="spotify_dj_title">Spotify DJ, with its voice</string>
+ <string name="spotify_dj_starting">The DJ is getting ready…</string>
+ <string name="spotify_dj_failed">The DJ isn\'t available right now. Try again later.</string>
+ <string name="spotify_dj_talking">is talking</string>
</resources>
diff --git a/native/Cargo.lock b/native/Cargo.lock
index df59ae7..90178e1 100644
--- a/native/Cargo.lock
+++ b/native/Cargo.lock
@@ -2374,6 +2374,7 @@ dependencies = [
"once_cell",
"protobuf",
"serde_json",
+ "symphonia",
"tokio",
]
diff --git a/native/Cargo.toml b/native/Cargo.toml
index c5c1560..f6449d4 100644
--- a/native/Cargo.toml
+++ b/native/Cargo.toml
@@ -38,6 +38,9 @@ http-body-util = "0.1"
bytes = "1"
serde_json = "1"
futures-util = "0.3"
+# Decoding the DJ's lines, which arrive as MP3; see src/dj.rs. The same
+# version librespot-playback already builds, so nothing new is compiled.
+symphonia = { version = "0.5", default-features = false, features = ["mp3"] }
[target.'cfg(target_os = "android")'.dependencies]
ndk-context = "0.1"
diff --git a/native/src/dj.rs b/native/src/dj.rs
new file mode 100644
index 0000000..f58c2db
--- /dev/null
+++ b/native/src/dj.rs
@@ -0,0 +1,594 @@
+//! Spotify's DJ: a session of tracks, and the lines it says around them.
+//!
+//! The DJ is listed among the account's playlists, but asked for as one it has
+//! no tracks: what it plays comes from the DJ's own session service, a page at
+//! a time, each page naming the next. Some tracks carry a line to say before
+//! them and some a line to say after, written out as SSML in the track's
+//! metadata, with the voice and the synthesiser to read it with. Spotify reads
+//! it on request — a POST to `client-tts`, answered with a redirect to an MP3 —
+//! and the official client plays that around the track, one after the other.
+//!
+//! This is the same, worked out from go-librespot's DJ support and from what
+//! Spotify answered this client: the session is asked for here rather than
+//! handed over by another device, which Spotify allows. The lines are fetched
+//! and decoded ahead, as soon as a page arrives, because synthesising one takes
+//! seconds; the player asks for them at the moment a track starts and ends
+//! (see `NarrationMoment` in the vendored player), and a line that is not ready
+//! by then is not said. The music never waits for the DJ.
+//!
+//! The language the DJ speaks is the client's to say: without a platform and a
+//! version, Spotify answers in English whatever the account's language is.
+
+use crate::engine;
+use bytes::Bytes;
+use http::{header::HeaderValue, Method, Request};
+use librespot_core::Session;
+use librespot_playback::player::{self, NarrationMoment};
+use std::collections::{HashMap, HashSet};
+use std::sync::{Arc, Mutex, Once};
+use std::time::{Duration, Instant};
+
+/// The DJ, as the account's library lists it; the same for every account.
+const DJ_URI: &str = "spotify:playlist:37i9dQZF1EYkqdzj48dyYq";
+
+const SPCLIENT: &str = "https://spclient.wg.spotify.com";
+
+/// What the lines are synthesised at: the player's own rate, so nothing has to
+/// be resampled on the way.
+const LINE_SAMPLE_RATE: u32 = 44_100;
+
+/// The loudness every line is brought to, Spotify's normalisation target.
+const TARGET_LUFS: f64 = -14.0;
+
+/// How long one line may take to be synthesised and fetched.
+const LINE_TIMEOUT: Duration = Duration::from_secs(20);
+
+/// The lines the DJ has for a track, as its metadata gives them.
+#[derive(Clone)]
+struct Line {
+ ssml: String,
+ voice: i32,
+ provider: i32,
+ loudness: Option<f64>,
+ true_peak: Option<f64>,
+}
+
+#[derive(Clone)]
+enum Clip {
+ Pending,
+ Ready(Arc<Vec<f64>>),
+ Failed,
+}
+
+/// Lines by track and kind (`intro`, `jump`, `outro`), as they are fetched.
+static CLIPS: Mutex<Option<HashMap<(String, &'static str), Clip>>> = Mutex::new(None);
+
+/// Tracks the listener went straight to: introduced with the jump line, which
+/// is written for arriving out of order, rather than the intro.
+static JUMPED: Mutex<Option<HashSet<String>>> = Mutex::new(None);
+
+/// Every line this session has registered, so a page that repeats the
+/// session's history (they all do) does not have them synthesised again, or
+/// a line already said said again.
+static REGISTERED: Mutex<Option<HashSet<(String, &'static str)>>> = Mutex::new(None);
+
+/// The session's tracks in its order, for what follows a track; see `follows`.
+static ORDER: Mutex<Vec<String>> = Mutex::new(Vec::new());
+
+/// The lines each track of the session has, as its pages gave them, waiting
+/// for the app to say which tracks it will play; see [`prepare`].
+static LINES: Mutex<Option<HashMap<String, Vec<(&'static str, Line)>>>> = Mutex::new(None);
+
+/// The track the player last started, whose closing line, if still unsaid, is
+/// what a skip would otherwise lose; see [`install`].
+static LAST_STARTED: Mutex<Option<String>> = Mutex::new(None);
+
+static INSTALL: Once = Once::new();
+
+/// Gives the player where to find the lines. Once, the first time a session
+/// is turned on.
+fn install() {
+ INSTALL.call_once(|| {
+ player::set_narration_provider(Box::new(|uri, moment| match moment {
+ NarrationMoment::Before => {
+ let previous = LAST_STARTED
+ .lock()
+ .ok()
+ .and_then(|mut last| last.replace(uri.to_string()));
+ let jumped = JUMPED
+ .lock()
+ .ok()
+ .and_then(|mut set| set.as_mut().map(|set| set.remove(uri)))
+ .unwrap_or(false);
+ let own = if jumped {
+ take(uri, "jump").or_else(|| take(uri, "intro"))
+ } else {
+ take(uri, "intro")
+ };
+ // A track skipped before its end never says its closing line,
+ // and that line is the DJ talking between two songs: said
+ // here instead, before the one skipped to, unless that one has
+ // something of its own to say.
+ own.or_else(|| previous.filter(|previous| previous != uri).and_then(|previous| take(&previous, "outro")))
+ }
+ NarrationMoment::After => take(uri, "outro"),
+ }));
+ player::set_narration_follows(Box::new(follows));
+ });
+}
+
+/// Whether [uri] ends in a line: its own closing one, or the opening one of
+/// the track the session plays after it.
+fn follows(uri: &str) -> bool {
+ let next = ORDER.lock().ok().and_then(|order| {
+ order.iter().position(|it| it == uri).and_then(|at| order.get(at + 1).cloned())
+ });
+ let Ok(clips) = CLIPS.lock() else { return false };
+ let Some(clips) = clips.as_ref() else { return false };
+ let waiting = |uri: &str, kind: &'static str| {
+ matches!(clips.get(&(uri.to_string(), kind)), Some(Clip::Pending | Clip::Ready(_)))
+ };
+ waiting(uri, "outro") || next.is_some_and(|next| waiting(&next, "intro"))
+}
+
+/// A line ready to be said, taken so it is said once. One still being fetched
+/// is left: the music does not wait for it.
+fn take(uri: &str, kind: &'static str) -> Option<Arc<Vec<f64>>> {
+ let mut clips = CLIPS.lock().ok()?;
+ let clips = clips.as_mut()?;
+ match clips.get(&(uri.to_string(), kind)) {
+ Some(Clip::Ready(samples)) => {
+ let samples = samples.clone();
+ clips.remove(&(uri.to_string(), kind));
+ log::info!("dj: saying the {kind} of {uri}");
+ Some(samples)
+ }
+ Some(Clip::Pending) => {
+ log::info!("dj: the {kind} of {uri} is not ready, playing without it");
+ None
+ }
+ _ => None,
+ }
+}
+
+/// Turns a DJ session on, or off with everything it had fetched.
+pub fn set_active(on: bool) {
+ install();
+ player::set_narrating(on);
+ if !on {
+ if let Ok(mut clips) = CLIPS.lock() {
+ *clips = None;
+ }
+ if let Ok(mut jumped) = JUMPED.lock() {
+ *jumped = None;
+ }
+ if let Ok(mut last) = LAST_STARTED.lock() {
+ *last = None;
+ }
+ if let Ok(mut registered) = REGISTERED.lock() {
+ *registered = None;
+ }
+ if let Ok(mut lines) = LINES.lock() {
+ *lines = None;
+ }
+ if let Ok(mut order) = ORDER.lock() {
+ order.clear();
+ }
+ }
+}
+
+/// Says that [uri] is being gone to directly; see [`JUMPED`].
+pub fn jumped(uri: &str) {
+ if let Ok(mut jumped) = JUMPED.lock() {
+ jumped.get_or_insert_with(HashSet::new).insert(uri.to_string());
+ }
+}
+
+/// Waits for the line that opens [uri] to be ready, or to fail, up to
+/// [timeout_ms]. For the first track, which would otherwise always start
+/// before its introduction had been synthesised.
+pub fn await_opening(uri: &str, timeout_ms: u64) -> bool {
+ let deadline = Instant::now() + Duration::from_millis(timeout_ms);
+ loop {
+ let state = CLIPS.lock().ok().and_then(|clips| {
+ clips
+ .as_ref()
+ .and_then(|clips| clips.get(&(uri.to_string(), "intro")).cloned())
+ });
+ match state {
+ Some(Clip::Ready(_)) => return true,
+ Some(Clip::Pending) if Instant::now() < deadline => {
+ std::thread::sleep(Duration::from_millis(100))
+ }
+ _ => return false,
+ }
+ }
+}
+
+/// One page of the DJ's session: `{"tracks": [...], "next": "hm://..."}`.
+///
+/// [page] is the `next` of the page before, or empty for the session itself,
+/// which answers with its whole history and a few new tracks at the end. The
+/// lines are read, not fetched: the app says which tracks it will play, and
+/// [`prepare`] has theirs made.
+pub fn session(page: &str, language: &str, app_version: &str) -> engine::EngineResult<String> {
+ let handle = engine::runtime_handle()?;
+ let session = engine::with_session(|session| session.clone())?;
+ let path = if page.is_empty() {
+ format!("/lexicon-session-provider/context-resolve/v2/session?contextUri={DJ_URI}")
+ } else {
+ let rest = page
+ .strip_prefix("hm://")
+ .ok_or_else(|| format!("not a session page: {page}"))?;
+ format!("/{rest}")
+ };
+ let language = if language.is_empty() { "en" } else { language }.to_string();
+ let app_version = app_version.to_string();
+
+ let body = handle.block_on({
+ let session = session.clone();
+ async move {
+ let request = authorised(&session, Method::GET, &format!("{SPCLIENT}{path}"), Bytes::new(), &language, &app_version).await?;
+ session
+ .http_client()
+ .request_body(request)
+ .await
+ .map_err(|e| format!("dj session failed: {e}"))
+ }
+ })?;
+ let context: serde_json::Value =
+ serde_json::from_slice(&body).map_err(|e| format!("unreadable dj session: {e}"))?;
+
+ let mut tracks = Vec::new();
+ let mut next = String::new();
+ let mut lines = Vec::new();
+ // The session answers with a context of pages; a page asked for by its
+ // own address answers as that one page, its tracks at the top.
+ let pages: Vec<&serde_json::Value> = match context["pages"].as_array() {
+ Some(pages) => pages.iter().collect(),
+ None => vec![&context],
+ };
+ for page in pages {
+ if let Some(url) = page["next_page_url"].as_str().filter(|url| !url.is_empty()) {
+ next = url.to_string();
+ }
+ for track in page["tracks"].as_array().into_iter().flatten() {
+ let Some(uri) = track["uri"].as_str().filter(|uri| uri.starts_with("spotify:track:")) else {
+ continue;
+ };
+ tracks.push(uri.to_string());
+ let metadata = &track["metadata"];
+ for kind in ["intro", "jump", "outro"] {
+ if let Some(line) = line_in(metadata, kind) {
+ lines.push((uri.to_string(), kind, line));
+ }
+ }
+ }
+ }
+ log::info!(
+ "dj: session of {} tracks with {} lines, next page {}",
+ tracks.len(),
+ lines.len(),
+ if next.is_empty() { "none" } else { "named" }
+ );
+ if let Ok(mut order) = ORDER.lock() {
+ if page.is_empty() {
+ *order = tracks.clone();
+ } else {
+ order.extend(tracks.iter().cloned());
+ }
+ }
+ if let Ok(mut known) = LINES.lock() {
+ let known = known.get_or_insert_with(HashMap::new);
+ for (uri, kind, line) in lines {
+ let entry = known.entry(uri).or_default();
+ if !entry.iter().any(|(k, _)| *k == kind) {
+ entry.push((kind, line));
+ }
+ }
+ }
+
+ // The DJ's own colours, for what is shown while it talks.
+ let colours = &context["metadata"];
+ let colour = |key: &str| colours[key].as_str().unwrap_or_default().to_string();
+ Ok(serde_json::json!({
+ "tracks": tracks,
+ "next": next,
+ "colors": {
+ "top": colour("narration.jellyfish.top.color"),
+ "mid": colour("narration.jellyfish.mid.color"),
+ "bottom": colour("narration.jellyfish.bottom.color"),
+ "background": colour("narration.background.color"),
+ },
+ })
+ .to_string())
+}
+
+/// Has the lines of [uris] synthesised, in that order, each once a session:
+/// the tracks the app is about to queue. Every page repeats the session's
+/// whole history, and synthesising all of its lines each time cost seconds of
+/// the service's time for lines nobody would hear.
+pub fn prepare(uris: &[String]) -> engine::EngineResult<()> {
+ let handle = engine::runtime_handle()?;
+ let session = engine::with_session(|session| session.clone())?;
+ let mut lines = Vec::new();
+ {
+ let known = LINES.lock().map_err(|_| "dj lines unavailable".to_string())?;
+ let mut registered = REGISTERED.lock().map_err(|_| "dj lines unavailable".to_string())?;
+ let registered = registered.get_or_insert_with(HashSet::new);
+ for uri in uris {
+ for (kind, line) in known.as_ref().and_then(|known| known.get(uri)).into_iter().flatten() {
+ if registered.insert((uri.clone(), *kind)) {
+ lines.push((uri.clone(), *kind, line.clone()));
+ }
+ }
+ }
+ }
+ if lines.is_empty() {
+ return Ok(());
+ }
+ log::info!("dj: preparing {} lines", lines.len());
+ if let Ok(mut clips) = CLIPS.lock() {
+ let clips = clips.get_or_insert_with(HashMap::new);
+ for (uri, kind, _) in &lines {
+ clips.insert((uri.clone(), *kind), Clip::Pending);
+ }
+ }
+ // One after another, in the order they will be needed: they share one
+ // synthesiser, and the first track's opening is the one being waited for.
+ handle.spawn(async move {
+ for (uri, kind, line) in lines {
+ let fetched = tokio::time::timeout(LINE_TIMEOUT, fetch_line(&session, &line)).await;
+ let clip = match fetched {
+ Ok(Ok(samples)) => Clip::Ready(Arc::new(samples)),
+ Ok(Err(e)) => {
+ log::warn!("dj: the {kind} of {uri} could not be fetched: {e}");
+ Clip::Failed
+ }
+ Err(_) => {
+ log::warn!("dj: the {kind} of {uri} took too long");
+ Clip::Failed
+ }
+ };
+ if let Ok(mut clips) = CLIPS.lock() {
+ // Only into a session still on: one turned off meanwhile
+ // has no use for it.
+ if let Some(clips) = clips.as_mut() {
+ clips.insert((uri, kind), clip);
+ }
+ }
+ }
+ });
+ Ok(())
+}
+
+/// The line of [kind] in a track's metadata, if it has something to say.
+fn line_in(metadata: &serde_json::Value, kind: &str) -> Option<Line> {
+ let field = |name: &str| metadata[format!("narration.{kind}.{name}")].as_str();
+ let ssml = field("ssml").filter(|ssml| !ssml.is_empty())?.to_string();
+ Some(Line {
+ ssml,
+ voice: field("voice")
+ .and_then(|voice| voice.strip_prefix("VOICE"))
+ .and_then(|n| n.parse().ok())
+ .unwrap_or(1),
+ provider: field("tts_provider").map(provider_number).unwrap_or(6),
+ loudness: field("loudness").and_then(|v| v.parse().ok()),
+ true_peak: field("true_peak").and_then(|v| v.parse().ok()),
+ })
+}
+
+/// `spotify.narration.ResolveRequest.TtsProvider`, by name.
+fn provider_number(name: &str) -> i32 {
+ match name {
+ "CLOUD_TTS" => 1,
+ "READSPEAKER" => 2,
+ "POLLY" => 3,
+ "WELL_SAID" => 4,
+ "SONANTIC_DEPRECATED" => 5,
+ "SONANTIC_FAST" => 6,
+ "OPENAI" => 7,
+ "SONANTIC_LARGE" => 8,
+ "ELEVEN_LABS_DEPRECATED" => 9,
+ "ADS_PUBLIC" => 10,
+ _ => 6,
+ }
+}
+
+/// A request as the web player would make it, signed in.
+async fn authorised(
+ session: &Session,
+ method: Method,
+ url: &str,
+ body: Bytes,
+ language: &str,
+ app_version: &str,
+) -> engine::EngineResult<Request<Bytes>> {
+ let token = session
+ .login5()
+ .auth_token()
+ .await
+ .map_err(|e| format!("no access token: {e}"))?;
+ let mut request = Request::builder()
+ .method(method)
+ .uri(url)
+ .body(body)
+ .map_err(|e| format!("bad request: {e}"))?;
+ let headers = request.headers_mut();
+ if let Ok(value) = HeaderValue::from_str(&format!("{} {}", token.token_type, token.access_token)) {
+ headers.insert(http::header::AUTHORIZATION, value);
+ }
+ headers.insert(http::header::ACCEPT, HeaderValue::from_static("application/json"));
+ // What makes the DJ speak the account's language; see the note at the top.
+ headers.insert("app-platform", HeaderValue::from_static(crate::pathfinder::APP_PLATFORM));
+ let version = if app_version.is_empty() { crate::pathfinder::APP_VERSION } else { app_version };
+ if let Ok(value) = HeaderValue::from_str(version) {
+ headers.insert("spotify-app-version", value);
+ }
+ if let Ok(value) = HeaderValue::from_str(language) {
+ headers.insert(http::header::ACCEPT_LANGUAGE, value);
+ }
+ Ok(request)
+}
+
+/// Has [line] read out, fetches it and decodes it to the player's samples.
+async fn fetch_line(session: &Session, line: &Line) -> engine::EngineResult<Vec<f64>> {
+ let mut request = authorised(
+ session,
+ Method::POST,
+ &format!("{SPCLIENT}/client-tts/v1/fulfill"),
+ Bytes::from(tts_request(line)),
+ "",
+ "",
+ )
+ .await?;
+ request.headers_mut().insert(
+ http::header::CONTENT_TYPE,
+ HeaderValue::from_static("application/x-protobuf"),
+ );
+ // Not through `request`, which takes anything but a success for an error:
+ // the answer is a redirect, and the address is all that is wanted of it.
+ let response = session
+ .http_client()
+ .request_fut(request)
+ .map_err(|e| format!("tts request failed: {e}"))?
+ .await
+ .map_err(|e| format!("tts request failed: {e}"))?;
+ let status = response.status();
+ let location = response
+ .headers()
+ .get(http::header::LOCATION)
+ .and_then(|value| value.to_str().ok())
+ .map(str::to_string)
+ .ok_or_else(|| format!("tts answered {status} without an address"))?;
+
+ let audio = Request::builder()
+ .method(Method::GET)
+ .uri(&location)
+ .body(Bytes::new())
+ .map_err(|e| format!("bad clip address: {e}"))?;
+ let mp3 = session
+ .http_client()
+ .request_body(audio)
+ .await
+ .map_err(|e| format!("clip download failed: {e}"))?;
+
+ let samples = tokio::task::spawn_blocking(move || decode(mp3.to_vec()))
+ .await
+ .map_err(|e| format!("decoding stopped: {e}"))??;
+ Ok(level(samples, line.loudness, line.true_peak))
+}
+
+/// `spotify.narration.TtsRequest`, written by hand: ssml = 2, audio_format = 3
+/// (MP3 is 5), tts_voice = 5, tts_provider = 6, sample_rate_hz = 7.
+fn tts_request(line: &Line) -> Vec<u8> {
+ fn varint(out: &mut Vec<u8>, mut value: u64) {
+ while value >= 0x80 {
+ out.push((value as u8 & 0x7F) | 0x80);
+ value >>= 7;
+ }
+ out.push(value as u8);
+ }
+ let mut out = Vec::new();
+ let text = line.ssml.as_bytes();
+ varint(&mut out, (2 << 3) | 2);
+ varint(&mut out, text.len() as u64);
+ out.extend_from_slice(text);
+ for (field, value) in [(3u64, 5u64), (5, line.voice as u64), (6, line.provider as u64), (7, LINE_SAMPLE_RATE as u64)] {
+ varint(&mut out, field << 3);
+ varint(&mut out, value);
+ }
+ out
+}
+
+/// An MP3 as interleaved stereo at the player's rate.
+fn decode(mp3: Vec<u8>) -> engine::EngineResult<Vec<f64>> {
+ use symphonia::core::audio::SampleBuffer;
+ use symphonia::core::codecs::DecoderOptions;
+ use symphonia::core::formats::FormatOptions;
+ use symphonia::core::io::MediaSourceStream;
+ use symphonia::core::meta::MetadataOptions;
+ use symphonia::core::probe::Hint;
+
+ let stream = MediaSourceStream::new(Box::new(std::io::Cursor::new(mp3)), Default::default());
+ let mut hint = Hint::new();
+ hint.with_extension("mp3");
+ let probed = symphonia::default::get_probe()
+ .format(&hint, stream, &FormatOptions::default(), &MetadataOptions::default())
+ .map_err(|e| format!("not an mp3: {e}"))?;
+ let mut format = probed.format;
+ let track = format
+ .default_track()
+ .ok_or_else(|| "no audio in the clip".to_string())?;
+ let track_id = track.id;
+ let mut decoder = symphonia::default::get_codecs()
+ .make(&track.codec_params, &DecoderOptions::default())
+ .map_err(|e| format!("no decoder: {e}"))?;
+
+ let mut rate = LINE_SAMPLE_RATE;
+ let mut stereo = Vec::new();
+ loop {
+ let packet = match format.next_packet() {
+ Ok(packet) => packet,
+ Err(_) => break,
+ };
+ if packet.track_id() != track_id {
+ continue;
+ }
+ let decoded = match decoder.decode(&packet) {
+ Ok(decoded) => decoded,
+ Err(symphonia::core::errors::Error::DecodeError(_)) => continue,
+ Err(_) => break,
+ };
+ let spec = *decoded.spec();
+ rate = spec.rate;
+ let channels = spec.channels.count().max(1);
+ let mut buffer = SampleBuffer::<f32>::new(decoded.capacity() as u64, spec);
+ buffer.copy_interleaved_ref(decoded);
+ for frame in buffer.samples().chunks(channels) {
+ let left = frame[0] as f64;
+ let right = if channels > 1 { frame[1] as f64 } else { left };
+ stereo.push(left);
+ stereo.push(right);
+ }
+ }
+ if stereo.is_empty() {
+ return Err("the clip decoded to nothing".into());
+ }
+ Ok(if rate == LINE_SAMPLE_RATE { stereo } else { resample(&stereo, rate, LINE_SAMPLE_RATE) })
+}
+
+/// Linear resampling of interleaved stereo, for a clip that came back at
+/// another rate than the one asked for. Speech survives it.
+fn resample(stereo: &[f64], from: u32, to: u32) -> Vec<f64> {
+ let frames = stereo.len() / 2;
+ let out_frames = (frames as u64 * to as u64 / from as u64) as usize;
+ let mut out = Vec::with_capacity(out_frames * 2);
+ for i in 0..out_frames {
+ let at = i as f64 * from as f64 / to as f64;
+ let index = at as usize;
+ let next = (index + 1).min(frames - 1);
+ let t = at - index as f64;
+ for channel in 0..2 {
+ let a = stereo[index * 2 + channel];
+ let b = stereo[next * 2 + channel];
+ out.push(a + (b - a) * t);
+ }
+ }
+ out
+}
+
+/// Brings a line to the loudness music plays at, without letting its peaks
+/// past full scale. Levels the clip did not come with leave it as rendered.
+fn level(mut samples: Vec<f64>, loudness: Option<f64>, true_peak: Option<f64>) -> Vec<f64> {
+ let Some(loudness) = loudness else { return samples };
+ let mut gain_db = TARGET_LUFS - loudness;
+ if let Some(peak) = true_peak {
+ gain_db = gain_db.min(-1.0 - peak);
+ }
+ let gain = 10f64.powf(gain_db / 20.0);
+ for sample in samples.iter_mut() {
+ *sample = (*sample * gain).clamp(-1.0, 1.0);
+ }
+ samples
+}
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index 4250aca..a79bda4 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -4,7 +4,7 @@
//! `java.lang.IllegalStateException` with the engine's error message. Kotlin
//! therefore never has to check return codes.
-use crate::{catalog, downloads, engine, sink};
+use crate::{catalog, dj, downloads, engine, sink};
use jni::objects::{JClass, JObject, JString};
use jni::sys::{jboolean, jint, jlong, jstring, JNI_FALSE, JNI_TRUE};
use jni::JNIEnv;
@@ -953,3 +953,65 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeCan
let uri = read_string(&mut env, &track_uri);
guard(&mut env, "CancelDownload", || downloads::cancel(&uri?));
}
+
+/// One page of the DJ's session; see dj::session.
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeDjSession(
+ mut env: JNIEnv,
+ _class: JClass,
+ page: JString,
+ language: JString,
+ app_version: JString,
+) -> jstring {
+ let page = read_string(&mut env, &page).unwrap_or_default();
+ let language = read_string(&mut env, &language).unwrap_or_default();
+ let app_version = read_string(&mut env, &app_version).unwrap_or_default();
+ guard_string(&mut env, "DjSession", || dj::session(&page, &language, &app_version))
+}
+
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeDjSetActive(
+ mut env: JNIEnv,
+ _class: JClass,
+ on: jboolean,
+) {
+ guard(&mut env, "DjSetActive", || {
+ dj::set_active(on != 0);
+ Ok(())
+ });
+}
+
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeDjJumped(
+ mut env: JNIEnv,
+ _class: JClass,
+ uri: JString,
+) {
+ let Ok(uri) = read_string(&mut env, &uri) else { return };
+ guard(&mut env, "DjJumped", || {
+ dj::jumped(&uri);
+ Ok(())
+ });
+}
+
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeDjAwaitOpening(
+ mut env: JNIEnv,
+ _class: JClass,
+ uri: JString,
+ timeout_ms: jlong,
+) -> jboolean {
+ let Ok(uri) = read_string(&mut env, &uri) else { return JNI_FALSE };
+ if dj::await_opening(&uri, timeout_ms.max(0) as u64) { JNI_TRUE } else { JNI_FALSE }
+}
+
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeDjPrepare(
+ mut env: JNIEnv,
+ _class: JClass,
+ uris_json: JString,
+) {
+ let Ok(json) = read_string(&mut env, &uris_json) else { return };
+ let uris: Vec<String> = serde_json::from_str(&json).unwrap_or_default();
+ guard(&mut env, "DjPrepare", || dj::prepare(&uris));
+}
diff --git a/native/src/lib.rs b/native/src/lib.rs
index 40c3224..b76636e 100644
--- a/native/src/lib.rs
+++ b/native/src/lib.rs
@@ -6,6 +6,7 @@
//! ownership to reason about.
mod catalog;
+mod dj;
mod downloads;
mod engine;
mod events;
diff --git a/native/src/pathfinder.rs b/native/src/pathfinder.rs
index 1f17f2c..4d021f6 100644
--- a/native/src/pathfinder.rs
+++ b/native/src/pathfinder.rs
@@ -29,7 +29,7 @@ const ENDPOINT: &str = "https://api-partner.spotify.com/pathfinder/v2/query";
///
/// Sent because the gateway expects a client to say what it is; the values are
/// the web player's own, since that is the client these queries belong to.
-const APP_PLATFORM: &str = "WebPlayer";
+pub(crate) const APP_PLATFORM: &str = "WebPlayer";
/// The web client's own version string, as captured from it.
///
@@ -38,7 +38,7 @@ const APP_PLATFORM: &str = "WebPlayer";
/// caller it does not recognise rather than to one it refuses. Like the query
/// hashes, this ages, and like them the caller can pass a newer one; see
/// PathfinderKeys on the Kotlin side.
-const APP_VERSION: &str = "1.2.97.155.g5dd0dcaf-development";
+pub(crate) const APP_VERSION: &str = "1.2.97.155.g5dd0dcaf-development";
/// One persisted query, by name and hash. See the note at the top of the file.
struct Query {
diff --git a/native/src/sink.rs b/native/src/sink.rs
index 7475e6c..2b70992 100644
--- a/native/src/sink.rs
+++ b/native/src/sink.rs
@@ -193,9 +193,12 @@ impl Sink for AndroidSink {
.map_err(|e| SinkError::OnWrite(e.to_string()))?
};
+ // The DJ's voice goes to its own method, which keeps the music's
+ // effects off it; see `speaking` in the vendored player.
+ let method = if librespot_playback::player::speaking() { "writeVoice" } else { "write" };
env.call_method(
&output,
- "write",
+ method,
"(Ljava/nio/ByteBuffer;III)V",
&[
JValue::Object(&buffer),
diff --git a/native/vendor/README.md b/native/vendor/README.md
index db8a864..0ecc623 100644
--- a/native/vendor/README.md
+++ b/native/vendor/README.md
@@ -241,6 +241,30 @@ halves go on sharing one clock as before.
Zero, the default, leaves every one of these paths as upstream has it.
+### The patch: the DJ talks between the tracks
+
+Spotify's DJ gives some tracks a line to say before them and some a line after,
+which the app has synthesised by the time they play (see `native/src/dj.rs`).
+The player is the only place that knows where one track's sound ends and the
+next one's begins, so that is where the lines go.
+
+`set_narration_provider` installs where they come from, and `set_narrating`
+turns them on for a DJ queue. When a track starts playing near its beginning
+the player asks for its opening line; when its decoder runs out, before the end
+is announced, for its closing one. A line is written to the sink a twentieth of
+a second at a time, so commands are still heard between pieces: a seek drops an
+opening line, a load drops whatever was being said, and a closing line ends the
+track once it has been said. Lines and music are never mixed, as in the
+official client.
+
+While a line is written `speaking()` is true, and the app's sink hands those
+packets to the output separately, past the effects that are the listener's
+choices about the music. And `set_narration_follows` says which tracks end in a
+line: those are not dissolved into the next, since the crossfade would run the
+music under the DJ talking. Every other track of a DJ queue crossfades as usual.
+
+Nothing changes while `set_narrating` is off, which is the default.
+
### Maintenance
The markers are `LOCAL PATCH` in `src/player.rs` and `src/config.rs`.
diff --git a/native/vendor/librespot-playback/src/player.rs b/native/vendor/librespot-playback/src/player.rs
index 858d55b..393b9aa 100644
--- a/native/vendor/librespot-playback/src/player.rs
+++ b/native/vendor/librespot-playback/src/player.rs
@@ -42,6 +42,106 @@ use tokio::sync::{mpsc, oneshot};
use crate::{NUM_CHANNELS, SAMPLE_RATE, SAMPLES_PER_SECOND};
+/// LOCAL PATCH: Spotify's DJ, talking around the tracks it plays.
+///
+/// A DJ session gives some tracks a line before them and some a line after,
+/// which the app has synthesised into audio by the time they play. The player
+/// is the only place that knows where one track's sound ends and the next
+/// one's begins, so this is where the lines are put: written to the sink
+/// before the track's first packet and after its last, one after the other,
+/// never mixed, as the official client does. The app says what to say; the
+/// player only asks it, at those two moments, through [`set_narration_provider`].
+#[derive(Clone, Copy, Debug, PartialEq, Eq)]
+pub enum NarrationMoment {
+ /// Before the track's first packet.
+ Before,
+ /// After its last, before its end is announced.
+ After,
+}
+
+/// What the app answers with: interleaved stereo at [`SAMPLE_RATE`], already
+/// at the level it should be heard at, or nothing to say.
+pub type NarrationProvider =
+ dyn Fn(&str, NarrationMoment) -> Option<Arc<Vec<f64>>> + Send + Sync;
+
+static NARRATION_PROVIDER: std::sync::OnceLock<Box<NarrationProvider>> = std::sync::OnceLock::new();
+
+/// Whether a line comes at the end of a track: its own closing one, or the
+/// opening one of the track after it. Asked without taking either.
+pub type NarrationFollows = dyn Fn(&str) -> bool + Send + Sync;
+
+static NARRATION_FOLLOWS: std::sync::OnceLock<Box<NarrationFollows>> = std::sync::OnceLock::new();
+
+/// Installs the question above. Once, like the provider.
+pub fn set_narration_follows(follows: Box<NarrationFollows>) {
+ let _ = NARRATION_FOLLOWS.set(follows);
+}
+
+/// Whether [track_id] ends in a line, so it must not dissolve into the next
+/// track: a crossfade would run the music under the DJ talking.
+fn narration_follows(track_id: &SpotifyUri) -> bool {
+ narrating()
+ && track_id
+ .to_uri()
+ .ok()
+ .zip(NARRATION_FOLLOWS.get())
+ .is_some_and(|(uri, follows)| follows(&uri))
+}
+
+/// Whether a narrated session is playing: the player asks for lines only then,
+/// and does not dissolve a track into the next where a line comes between
+/// them; see [`narration_follows`].
+static NARRATING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// Installs where the lines come from. Once: the app has one.
+pub fn set_narration_provider(provider: Box<NarrationProvider>) {
+ let _ = NARRATION_PROVIDER.set(provider);
+}
+
+/// Turns the narrated session on or off; see [`NARRATING`].
+pub fn set_narrating(on: bool) {
+ NARRATING.store(on, Ordering::SeqCst);
+}
+
+fn narrating() -> bool {
+ NARRATING.load(Ordering::SeqCst)
+}
+
+/// True while the samples being written to the sink are a line rather than
+/// music: the sink hands them on separately, so the app's effects, made for
+/// the music, leave the voice alone.
+static SPEAKING: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
+
+/// Whether the packet the sink is being given is the DJ's voice.
+pub fn speaking() -> bool {
+ SPEAKING.load(Ordering::SeqCst)
+}
+
+fn narration_for(track_id: &SpotifyUri, moment: NarrationMoment) -> Option<Arc<Vec<f64>>> {
+ if !narrating() {
+ return None;
+ }
+ let uri = track_id.to_uri().ok()?;
+ NARRATION_PROVIDER.get()?(&uri, moment).filter(|samples| !samples.is_empty())
+}
+
+/// LOCAL PATCH: a line being spoken, and how far into it.
+struct Narration {
+ samples: Arc<Vec<f64>>,
+ at: usize,
+ /// A closing line: the track's end is announced once it has been said.
+ then_end: bool,
+}
+
+/// LOCAL PATCH: how much of a line is written at a time, in interleaved
+/// samples: about a twentieth of a second, so a pause or a skip is answered
+/// between two of them.
+const NARRATION_CHUNK: usize = 4096;
+
+/// LOCAL PATCH: a track that starts further in than this was resumed, not
+/// arrived at, and is not introduced.
+const NARRATION_START_WINDOW_MS: u32 = 1500;
+
/// LOCAL PATCH: how many times a failed load is tried again before the track is
/// called unavailable, and how long the first wait is (it doubles each time).
///
@@ -164,6 +264,12 @@ struct PlayerInternal {
/// branch of the loading state in `poll`.
load_attempt: u8,
load_position_ms: u32,
+ /// LOCAL PATCH: the DJ's line being spoken, if one is; see [`Narration`].
+ narration: Option<Narration>,
+ /// LOCAL PATCH: the play requests already asked for their opening and
+ /// closing lines, so each is asked once.
+ narrated_before: Option<u64>,
+ narrated_after: Option<u64>,
}
/// LOCAL PATCH: the outgoing half of a crossfade.
@@ -676,6 +782,9 @@ impl Player {
ended: None,
load_attempt: 0,
load_position_ms: 0,
+ narration: None,
+ narrated_before: None,
+ narrated_after: None,
normalisation_peaks: [0.0; 2],
normalisation_integrators: [0.0; 2],
normalisation_channel: 0,
@@ -2056,6 +2165,41 @@ impl Future for PlayerInternal {
if self.state.is_playing() {
self.ensure_sink_running();
+ // LOCAL PATCH: the DJ's opening line, before the track's
+ // first packet, asked for once per play request.
+ let opening = match self.state {
+ PlayerState::Playing {
+ ref track_id,
+ play_request_id,
+ stream_position_ms,
+ ..
+ } if self.narrated_before != Some(play_request_id) => {
+ Some((track_id.clone(), play_request_id, stream_position_ms))
+ }
+ _ => None,
+ };
+ if let Some((track_id, play_request_id, stream_position_ms)) = opening {
+ {
+ self.narrated_before = Some(play_request_id);
+ if stream_position_ms < NARRATION_START_WINDOW_MS {
+ if let Some(samples) = narration_for(&track_id, NarrationMoment::Before) {
+ self.narration = Some(Narration {
+ samples,
+ at: 0,
+ then_end: false,
+ });
+ }
+ }
+ }
+ }
+
+ // LOCAL PATCH: a line being spoken is written instead of the
+ // track, a piece per round, so commands are still heard.
+ if self.narration.is_some() {
+ self.pump_narration();
+ continue;
+ }
+
if let PlayerState::Playing {
ref track_id,
play_request_id,
@@ -2225,7 +2369,7 @@ impl Future for PlayerInternal {
let crossfade_ms = self.config.crossfade_duration_ms as i64;
let position = stream_position_ms as i64;
let duration = duration_ms as i64;
- if crossfade_ms > 0 && duration > crossfade_ms && !self.config.hold_end {
+ if crossfade_ms > 0 && duration > crossfade_ms && !self.config.hold_end && !narration_follows(&track_id) {
let silence_threshold_samples = (SAMPLE_RATE as usize * NUM_CHANNELS as usize * 350) / 1000;
let trailing_silence = self.config.trim_silence
&& self.consecutive_silent_samples >= silence_threshold_samples
@@ -2323,6 +2467,45 @@ impl PlayerInternal {
}
}
+ /// LOCAL PATCH: writes the next piece of the line being spoken, at the
+ /// player's volume, dissolving out of a track still fading as music does.
+ fn pump_narration(&mut self) {
+ let Some(narration) = self.narration.as_mut() else {
+ return;
+ };
+ let end = (narration.at + NARRATION_CHUNK).min(narration.samples.len());
+ let mut data = narration.samples[narration.at..end].to_vec();
+ narration.at = end;
+ let finished = narration.at >= narration.samples.len();
+ let then_end = narration.then_end;
+ if finished {
+ self.narration = None;
+ }
+
+ let volume = self.volume_getter.attenuation_factor();
+ if volume < 1.0 {
+ for sample in data.iter_mut() {
+ *sample *= volume;
+ }
+ }
+ if self.fade_out.is_some() {
+ self.apply_fade(&mut data);
+ }
+ SPEAKING.store(true, Ordering::SeqCst);
+ let written = self.sink.write(AudioPacket::Samples(data), &mut self.converter);
+ SPEAKING.store(false, Ordering::SeqCst);
+ if let Err(e) = written {
+ error!("{e}");
+ self.narration = None;
+ self.handle_pause();
+ return;
+ }
+
+ if finished && then_end {
+ self.finish_track();
+ }
+ }
+
/// LOCAL PATCH: how long a crossfade lasts, in interleaved samples.
fn crossfade_samples(&self) -> usize {
Self::fade_samples(self.config.crossfade_duration_ms)
@@ -2583,6 +2766,8 @@ impl PlayerInternal {
fn handle_player_stop(&mut self) {
// LOCAL PATCH: nothing is being faded into any more.
self.fade_out = None;
+ // LOCAL PATCH: nor said.
+ self.narration = None;
match self.state {
PlayerState::Playing {
ref track_id,
@@ -2863,23 +3048,54 @@ impl PlayerInternal {
return;
}
}
- self.state.playing_to_end_of_track();
- if let PlayerState::EndOfTrack {
- ref track_id,
- play_request_id,
- ..
- } = self.state
- {
- let track_id = track_id.clone();
- self.announce_end(track_id, play_request_id)
- } else {
- error!("PlayerInternal handle_packet: Invalid PlayerState");
- exit(1);
+ // LOCAL PATCH: the DJ's closing line, before the end is
+ // announced; the end follows once it has been said.
+ let closing = match self.state {
+ PlayerState::Playing {
+ ref track_id,
+ play_request_id,
+ ..
+ } if self.narrated_after != Some(play_request_id) => {
+ Some((track_id.clone(), play_request_id))
+ }
+ _ => None,
+ };
+ if let Some((track_id, play_request_id)) = closing {
+ {
+ self.narrated_after = Some(play_request_id);
+ if let Some(samples) = narration_for(&track_id, NarrationMoment::After) {
+ self.narration = Some(Narration {
+ samples,
+ at: 0,
+ then_end: true,
+ });
+ return;
+ }
+ }
}
+ self.finish_track();
}
}
}
+ /// LOCAL PATCH: the end of the track, said; split out of `handle_packet`
+ /// so a closing line can say it once it is over.
+ fn finish_track(&mut self) {
+ self.state.playing_to_end_of_track();
+ if let PlayerState::EndOfTrack {
+ ref track_id,
+ play_request_id,
+ ..
+ } = self.state
+ {
+ let track_id = track_id.clone();
+ self.announce_end(track_id, play_request_id)
+ } else {
+ error!("PlayerInternal finish_track: Invalid PlayerState");
+ exit(1);
+ }
+ }
+
fn start_playback(
&mut self,
track_id: SpotifyUri,
@@ -2973,6 +3189,9 @@ impl PlayerInternal {
let play_request_id =
play_request_id_option.unwrap_or(self.play_request_id_generator.get());
+ // LOCAL PATCH: a line about the track being left goes with it.
+ self.narration = None;
+
// LOCAL PATCH: whatever was playing is being replaced, so nothing more
// is going to be said about it.
//
@@ -3267,6 +3486,18 @@ impl PlayerInternal {
}
fn handle_command_seek(&mut self, position_ms: u32) -> PlayerResult {
+ // LOCAL PATCH: a listener who moves through the track wants the
+ // music, not the rest of its introduction. A closing line still ends
+ // the track: there is no music left to want.
+ // Paused, it is simply dropped: the decoder is at the end, and the
+ // end is reached again as soon as the track plays.
+ if let Some(narration) = self.narration.take() {
+ if narration.then_end && self.state.is_playing() {
+ self.finish_track();
+ return Ok(());
+ }
+ }
+
// When we are still loading, the user may immediately ask to
// seek to another position yet the decoder won't be ready for
// that. In this case just restart the loading process but