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.

Keep the effects where the listener left them

Lelonio3194d4cfd506
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index cb2bc82..8a93ffc 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -21,6 +21,15 @@ import dev.lelonio.square.data.SpotifyApi
*/
class SquareApplication : Application() {
+ override fun onCreate() {
+ super.onCreate()
+ // Before anything can read or write them. The service used to do this on
+ // creation, which is late: the player screen and the media session both
+ // exist by then and either can announce a default that would be saved
+ // over what the listener had set.
+ dev.lelonio.square.playback.AudioEffects.load(this)
+ }
+
val tokenStore: TokenStore by lazy { TokenStore(this) }
val recentStore: RecentStore by lazy { RecentStore(this) }
@@ -38,6 +47,11 @@ class SquareApplication : Application() {
dev.lelonio.square.data.PathfinderKeys(this)
}
+ /** Which stretcher works out speed and pitch. */
+ val effectQuality: dev.lelonio.square.data.EffectQualityStore by lazy {
+ dev.lelonio.square.data.EffectQualityStore(this)
+ }
+
/** How long one track dissolves into the next. */
val crossfade: dev.lelonio.square.data.CrossfadeStore by lazy {
dev.lelonio.square.data.CrossfadeStore(this)
diff --git a/app/src/main/java/dev/lelonio/square/data/EffectQualityStore.kt b/app/src/main/java/dev/lelonio/square/data/EffectQualityStore.kt
new file mode 100644
index 0000000..0c086a5
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/data/EffectQualityStore.kt
@@ -0,0 +1,53 @@
+package dev.lelonio.square.data
+
+import android.content.Context
+import androidx.annotation.StringRes
+import dev.lelonio.square.R
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.asStateFlow
+
+/**
+ * How speed and pitch are worked out.
+ *
+ * The two ways of doing it are genuinely different bargains rather than more or
+ * less of one thing.
+ *
+ * [High] is Bungee, a phase vocoder: it analyses and resynthesises every grain
+ * of audio, holds transients together and stays musical well past the range a
+ * listener is likely to ask for. It also does that work for every packet, on a
+ * phone, next to a decoder and a user interface.
+ *
+ * [Light] is the platform's own stretcher, the one behind `setPlaybackParams`.
+ * It is nearly free, because the system does it below the app, and it is built
+ * for speech: on music it smears attacks and adds a metallic edge that grows
+ * with the amount. On an older phone that is the difference between altered
+ * playback and stuttering playback.
+ */
+enum class EffectQuality(val key: String, @StringRes val label: Int, @StringRes val note: Int) {
+ High("high", R.string.effect_quality_high, R.string.effect_quality_high_note),
+ Light("light", R.string.effect_quality_light, R.string.effect_quality_light_note),
+}
+
+class EffectQualityStore(context: Context) {
+
+ private val prefs = context.applicationContext
+ .getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
+
+ private val _quality = MutableStateFlow(
+ EffectQuality.entries.firstOrNull { it.key == prefs.getString(KEY_QUALITY, null) }
+ ?: EffectQuality.High,
+ )
+ val quality: StateFlow<EffectQuality> = _quality.asStateFlow()
+
+ fun set(quality: EffectQuality) {
+ if (quality == _quality.value) return
+ _quality.value = quality
+ prefs.edit().putString(KEY_QUALITY, quality.key).commit()
+ }
+
+ private companion object {
+ const val FILE_NAME = "square_effect_quality"
+ const val KEY_QUALITY = "quality"
+ }
+}
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 bad41d9..eb04cec 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -156,6 +156,17 @@ object NativeBridge {
*/
val spircLost: Boolean get() = nativeSpircLost()
+ /**
+ * Rebuilds the player with a new bitrate and crossfade.
+ *
+ * Both belong to a configuration the player owns for its whole life, so a
+ * change means another player. This replaces the session and the player and
+ * leaves the runtime and the audio output where they are: tearing those
+ * down from here is what used to abort the process. The queue is the
+ * caller's to put back. Blocking.
+ */
+ fun setQuality(bitrateKbps: Int, crossfadeMs: Int) = nativeSetQuality(bitrateKbps, crossfadeMs)
+
/**
* Builds a new session, player and Connect device, keeping everything else.
*
@@ -341,6 +352,7 @@ object NativeBridge {
private external fun nativeVolume(): Int
private external fun nativeIsConnected(): Boolean
private external fun nativeSpircLost(): Boolean
+ private external fun nativeSetQuality(bitrateKbps: Int, crossfadeMs: Int)
private external fun nativeReconnect()
private external fun nativePlaybackElsewhere(): Boolean
private external fun nativePublishContext(positionMs: Int): Boolean
diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt b/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt
index be2020e..fcc8a94 100644
--- a/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/AudioEffects.kt
@@ -21,8 +21,11 @@ import kotlinx.coroutines.flow.asStateFlow
* mood and losing it on the next launch — or on the next playlist — is the more
* annoying of the two, and the effects panel makes the current state plain.
*
- * [load] has to be called once before the values mean anything; the service does
- * it on creation.
+ * [load] has to be called once before the values mean anything, and it is called
+ * from the application object rather than from the service. Nothing may write
+ * before it: a value set while the file has not been read is a value written
+ * over what was saved, and the whole point of persisting was not to lose the
+ * mood the listener set.
*/
object AudioEffects {
@@ -55,21 +58,43 @@ object AudioEffects {
_reverb.value = store.getFloat(KEY_REVERB, 0f)
_speed.value = store.getFloat(KEY_SPEED, 1f)
_pitch.value = store.getFloat(KEY_PITCH, 1f)
+ android.util.Log.i(
+ "SpotAudio",
+ "effects restored: reverb=${_reverb.value} speed=${_speed.value} pitch=${_pitch.value}",
+ )
}
fun setReverb(amount: Float) {
- _reverb.value = amount.coerceIn(0f, 1f)
- prefs?.edit()?.putFloat(KEY_REVERB, _reverb.value)?.apply()
+ val wanted = amount.coerceIn(0f, 1f)
+ if (wanted == _reverb.value) return
+ _reverb.value = wanted
+ val store = prefs ?: return
+ // `commit`, not `apply`, and deliberately.
+ //
+ // These are set by hand a moment before the app is put away, and an
+ // asynchronous write does not always survive the process being killed
+ // straight afterwards, which is exactly how a setting is "sometimes"
+ // lost. One synchronous write per deliberate change is nothing.
+ store.edit().putFloat(KEY_REVERB, wanted).commit()
}
- /** Records what was last sent to the player, so a restart can restore it. */
+ /**
+ * Records what was last sent to the player, so a restart can restore it.
+ *
+ * Ignored until the file has been read. Media3 announces the playback
+ * parameters of a player that has just been built, which are 1.0 and 1.0,
+ * and taking that for an instruction wrote the default over a saved speed
+ * before anyone had touched anything.
+ */
fun rememberSpeedAndPitch(speed: Float, pitch: Float) {
+ val store = prefs ?: return
+ if (speed == _speed.value && pitch == _pitch.value) return
_speed.value = speed
_pitch.value = pitch
- prefs?.edit()
- ?.putFloat(KEY_SPEED, speed)
- ?.putFloat(KEY_PITCH, pitch)
- ?.apply()
+ store.edit()
+ .putFloat(KEY_SPEED, speed)
+ .putFloat(KEY_PITCH, pitch)
+ .commit()
}
private const val FILE_NAME = "spot_audio_effects"
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 f43ea02..e6a9659 100644
--- a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
+++ b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt
@@ -45,6 +45,15 @@ class AudioOutput {
/** Logged once rather than per packet, which would be several a second. */
private var stretchFailureReported = false
+ /**
+ * Which stretcher does the work; see EffectQuality.
+ *
+ * Read on the audio thread, set from the settings, so it is volatile like
+ * the rest of the values that cross that line.
+ */
+ @Volatile
+ private var lightEffects = false
+
/** Reverb amount, 0 (off) to 1. */
@Volatile
private var reverbAmount = 0f
@@ -101,6 +110,24 @@ class AudioOutput {
this.pitch = pitch.coerceIn(MIN_RATE, MAX_RATE)
}
+ /**
+ * Chooses between the phase vocoder and the platform's own stretcher.
+ *
+ * Switching throws away whichever one was running: the Bungee stretcher
+ * holds a window of audio that would otherwise be resynthesised over the
+ * first moments of the new path, and the platform's has to be told to stop
+ * altering a stream this app is about to alter itself.
+ */
+ fun setLightEffects(light: Boolean) {
+ if (light == lightEffects) return
+ lightEffects = light
+ synchronized(this) {
+ stretcher?.release()
+ stretcher = null
+ applyPlatformRate()
+ }
+ }
+
fun setReverbAmount(amount: Float) {
reverbAmount = amount.coerceIn(0f, 1f)
synchronized(this) { applyReverb() }
@@ -144,6 +171,7 @@ class AudioOutput {
return
}
+ val fresh = reverb == null
val effect = reverb ?: runCatching {
// Priority 0: no reason to outbid anything else on the mix.
EnvironmentalReverb(0, 0)
@@ -181,6 +209,7 @@ class AudioOutput {
// where the audible change happens, and a straight mapping leaves
// most of the slider doing almost nothing.
output.setAuxEffectSendLevel(amount * amount * 0.85f + amount * 0.15f)
+ if (fresh) android.util.Log.i(TAG, "reverb on at $amount, id ${effect.id}")
}.onFailure { android.util.Log.w(TAG, "reverb not applied: ${it.message}") }
}
@@ -281,6 +310,7 @@ class AudioOutput {
* definition.
*/
private fun dropReverb() {
+ reverb?.let { android.util.Log.i(TAG, "reverb off, id ${it.id}") }
reverb?.runCatching {
enabled = false
release()
@@ -422,6 +452,14 @@ class AudioOutput {
val currentSpeed = speed
val currentPitch = pitch
+ // The platform does it below this app when the light path is chosen, so
+ // the packets go out untouched and the rate is set on the track itself.
+ if (lightEffects) {
+ synchronized(this) { applyPlatformRate() }
+ writeAll(output, data, sizeInBytes)
+ return
+ }
+
// Only pay for the stretcher when it would actually change something.
// At 1.0/1.0 Bungee is not a no-op — it still analyses and resynthesises
// every grain — so bypassing it keeps normal playback exactly as it was.
@@ -442,6 +480,32 @@ class AudioOutput {
writeAll(output, data, sizeInBytes)
}
+ /**
+ * Puts the current speed and pitch on the AudioTrack itself.
+ *
+ * Only for the light path, and set rather than left alone even at 1.0: a
+ * track keeps whatever rate it was given, so returning to normal speed has
+ * to say so. Failures are ignored on purpose, since a device that refuses a
+ * rate should still play the music.
+ */
+ private fun applyPlatformRate() {
+ val output = track?.takeIf { it.state == AudioTrack.STATE_INITIALIZED } ?: return
+ val wantedSpeed = if (lightEffects) speed else 1f
+ val wantedPitch = if (lightEffects) pitch else 1f
+ if (wantedSpeed == platformSpeed && wantedPitch == platformPitch) return
+ runCatching {
+ output.playbackParams = output.playbackParams
+ .setSpeed(wantedSpeed)
+ .setPitch(wantedPitch)
+ platformSpeed = wantedSpeed
+ platformPitch = wantedPitch
+ }.onFailure { android.util.Log.w(TAG, "the platform refused $wantedSpeed x: ${it.message}") }
+ }
+
+ /** What the track was last told, so it is not told again every packet. */
+ private var platformSpeed = 1f
+ private var platformPitch = 1f
+
private fun writeAll(output: AudioTrack, buffer: ByteBuffer, sizeInBytes: Int) {
var written = 0
while (written < sizeInBytes) {
@@ -578,6 +642,10 @@ class AudioOutput {
track = created
configuredSampleRate = sampleRate
configuredChannels = channels
+ // A new track knows nothing of the rate the old one was given.
+ platformSpeed = 1f
+ platformPitch = 1f
+ applyPlatformRate()
// The old track took its effect and its playback params with it, so both
// have to be put back or a format change silently resets the sound.
applyReverb()
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 8137608..0772f76 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
@@ -56,6 +56,7 @@ import dev.lelonio.square.R
import dev.lelonio.square.data.AppLanguages
import dev.lelonio.square.backend.BackendId
import dev.lelonio.square.data.CrossfadeSteps
+import dev.lelonio.square.data.EffectQuality
import dev.lelonio.square.data.Quality
import dev.lelonio.square.ui.MainViewModel
import dev.lelonio.square.ui.components.Artwork
@@ -258,6 +259,11 @@ fun SettingsScreen(
CrossfadeSection()
}
+ // The effects run on our own output, so this one holds for both backends.
+ if (open == SettingsPage.Playback) item("effect-quality") {
+ EffectQualitySection()
+ }
+
if (open == SettingsPage.App) item("language") {
Section(stringResource(R.string.language)) {
AppLanguages.forEachIndexed { index, (tag, name) ->
@@ -510,6 +516,32 @@ private fun YouTubeAccountSection(onSignIn: () -> Unit) {
}
}
+@Composable
+private fun EffectQualitySection() {
+ val context = LocalContext.current
+ val store = remember(context) {
+ (context.applicationContext as dev.lelonio.square.SquareApplication).effectQuality
+ }
+ val chosen by store.quality.collectAsStateWithLifecycle()
+
+ Section(stringResource(R.string.effect_quality)) {
+ EffectQuality.entries.forEachIndexed { index, quality ->
+ if (index > 0) RowDivider()
+ ChoiceRow(
+ label = stringResource(quality.label),
+ selected = quality == chosen,
+ ) { store.set(quality) }
+ }
+ RowDivider()
+ Text(
+ stringResource(chosen.note),
+ style = MaterialTheme.typography.bodySmall,
+ color = InkDim,
+ modifier = Modifier.padding(horizontal = 18.dp, vertical = 14.dp),
+ )
+ }
+}
+
@Composable
private fun CrossfadeSection() {
val context = LocalContext.current
diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml
index 82bc900..fa55652 100644
--- a/app/src/main/res/values-de/strings.xml
+++ b/app/src/main/res/values-de/strings.xml
@@ -225,4 +225,9 @@
<string name="playing_on">Wiedergabe auf %1$s</string>
<string name="switching">Wechsel…</string>
<string name="in_a_playlist">Schon in einer Playlist</string>
+ <string name="effect_quality">Effektqualität</string>
+ <string name="effect_quality_high">Originalgetreu</string>
+ <string name="effect_quality_high_note">Am besten für Musik und am anspruchsvollsten für den Akku.</string>
+ <string name="effect_quality_light">Leicht</string>
+ <string name="effect_quality_light_note">Das Telefon übernimmt. Schonender für ältere Geräte, rauer bei Anschlägen.</string>
</resources>
diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml
index 00711dd..cf6b362 100644
--- a/app/src/main/res/values-es/strings.xml
+++ b/app/src/main/res/values-es/strings.xml
@@ -225,4 +225,9 @@
<string name="playing_on">Reproduciendo en %1$s</string>
<string name="switching">Cambiando…</string>
<string name="in_a_playlist">Ya está en una lista</string>
+ <string name="effect_quality">Calidad de los efectos</string>
+ <string name="effect_quality_high">Fiel</string>
+ <string name="effect_quality_high_note">La mejor con música y la que más batería consume.</string>
+ <string name="effect_quality_light">Ligera</string>
+ <string name="effect_quality_light_note">Lo hace el teléfono. Más amable con los equipos antiguos, más áspera en los ataques.</string>
</resources>
diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml
index e25acd7..6fc3193 100644
--- a/app/src/main/res/values-fr/strings.xml
+++ b/app/src/main/res/values-fr/strings.xml
@@ -225,4 +225,9 @@
<string name="playing_on">Lecture sur %1$s</string>
<string name="switching">Transfert…</string>
<string name="in_a_playlist">Déjà dans une playlist</string>
+ <string name="effect_quality">Qualité des effets</string>
+ <string name="effect_quality_high">Fidèle</string>
+ <string name="effect_quality_high_note">La meilleure sur la musique, et la plus gourmande en batterie.</string>
+ <string name="effect_quality_light">Légère</string>
+ <string name="effect_quality_light_note">Le téléphone s’en charge. Plus douce pour les appareils anciens, plus rêche sur les attaques.</string>
</resources>
diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml
index 70091e2..bd11e91 100644
--- a/app/src/main/res/values-it/strings.xml
+++ b/app/src/main/res/values-it/strings.xml
@@ -226,4 +226,9 @@
<string name="playing_on">In riprod. su %1$s</string>
<string name="switching">Passaggio in corso…</string>
<string name="in_a_playlist">Già in una playlist</string>
+ <string name="effect_quality">Qualità degli effetti</string>
+ <string name="effect_quality_high">Fedele</string>
+ <string name="effect_quality_high_note">La migliore sulla musica, e la più esigente per la batteria.</string>
+ <string name="effect_quality_light">Leggera</string>
+ <string name="effect_quality_light_note">Se ne occupa il telefono. Più gentile con i dispositivi vecchi, più ruvida sugli attacchi.</string>
</resources>
diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml
index a9b9633..fe07d5b 100644
--- a/app/src/main/res/values-pt/strings.xml
+++ b/app/src/main/res/values-pt/strings.xml
@@ -225,4 +225,9 @@
<string name="playing_on">Reproduzindo em %1$s</string>
<string name="switching">Trocando…</string>
<string name="in_a_playlist">Já está numa playlist</string>
+ <string name="effect_quality">Qualidade dos efeitos</string>
+ <string name="effect_quality_high">Fiel</string>
+ <string name="effect_quality_high_note">A melhor com música e a mais exigente para a bateria.</string>
+ <string name="effect_quality_light">Leve</string>
+ <string name="effect_quality_light_note">O telefone é que trata disso. Melhor para aparelhos antigos, mais áspera nos ataques.</string>
</resources>
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index 00d0624..030b380 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -246,4 +246,9 @@
<string name="playing_on">Playing on %1$s</string>
<string name="switching">Switching…</string>
<string name="in_a_playlist">Already in a playlist</string>
+ <string name="effect_quality">Effect quality</string>
+ <string name="effect_quality_high">Faithful</string>
+ <string name="effect_quality_high_note">Best on music, and the heaviest on the battery.</string>
+ <string name="effect_quality_light">Light</string>
+ <string name="effect_quality_light_note">The phone does the work. Kinder to older devices, rougher on attacks.</string>
</resources>
diff --git a/native/src/engine.rs b/native/src/engine.rs
index 84f0cce..e39a49f 100644
--- a/native/src/engine.rs
+++ b/native/src/engine.rs
@@ -659,6 +659,30 @@ fn spawn_event_forwarder(
});
}
+/// Changes what the player is built with, and builds it again.
+///
+/// The bitrate and the crossfade belong to the player's configuration, which is
+/// fixed for the life of a player, so a new value means a new player. That used
+/// to mean tearing the whole engine down from the Kotlin side, and that is the
+/// call that aborted the process on a destroyed mutex: the runtime cannot be
+/// dropped from a JNI thread while librespot's own threads are still inside it.
+///
+/// Replacing the bundle does the same job without touching the runtime or the
+/// audio output. The queue is not restored here; the caller owns that.
+pub fn set_quality(bitrate_kbps: i32, crossfade_ms: i32) -> EngineResult<()> {
+ {
+ let mut guard = ENGINE.lock().map_err(|_| "engine mutex poisoned")?;
+ let engine = guard.as_mut().ok_or("engine not started")?;
+ engine.recipe.player_config.bitrate = match bitrate_kbps {
+ 96 => Bitrate::Bitrate96,
+ 160 => Bitrate::Bitrate160,
+ _ => Bitrate::Bitrate320,
+ };
+ engine.recipe.player_config.crossfade_duration_ms = crossfade_ms.max(0) as u32;
+ }
+ reconnect()
+}
+
/// Throws away a bundle and builds another one, leaving the runtime and the
/// audio output alone.
///
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index 97465a9..6c9f37a 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -437,6 +437,19 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSpi
engine::spirc_lost() as jboolean
}
+/// Rebuilds the player with a new bitrate and crossfade.
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeSetQuality(
+ mut env: JNIEnv,
+ _class: JClass,
+ bitrate_kbps: jint,
+ crossfade_ms: jint,
+) {
+ guard(&mut env, "SetQuality", || {
+ engine::set_quality(bitrate_kbps, crossfade_ms)
+ });
+}
+
#[no_mangle]
pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeReconnect(
mut env: JNIEnv,
diff --git a/native/src/sink.rs b/native/src/sink.rs
index 7298ffe..4019cf6 100644
--- a/native/src/sink.rs
+++ b/native/src/sink.rs
@@ -111,9 +111,16 @@ impl AndroidSink {
impl Sink for AndroidSink {
fn start(&mut self) -> SinkResult<()> {
- if self.stale() {
- return Ok(());
- }
+ // Not gated, and this matters more than it looks.
+ //
+ // The gate is about audio: packets from a session that has been
+ // replaced, or from before the track the listener asked for. This is
+ // not audio, it is the sink saying that playback has begun, and the
+ // other side raises the output on hearing it. Swallowed, the track was
+ // built silent while the reverb, whose send does not pass through that
+ // volume, went on at full level: the song arrived as reverb alone until
+ // something else started it properly. AudioOutput has the same note
+ // against `playing` for the same failure from another direction.
self.invoke("start")
}