Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.
Kotlin alternativeandroidandroid-applicationclientglassmorphisminnertubelibrespotliquid-glassspotifyspotify-connectspotifyapiyoutube-musicyoutube-playerytdlnis
Give every player the same speed, pitch and room
Lelonio8206bff948bb
diff --git a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.ktindex e6a9659..279c906 100644--- a/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt+++ b/app/src/main/java/dev/lelonio/square/playback/AudioOutput.kt@@ -181,34 +181,13 @@ class AudioOutput {reverb = effectrunCatching {- // The room grows with the amount: a longer tail and a louder- // reflection, from a small live room up to a hall.- effect.decayTime = (MIN_DECAY_MS + amount * (MAX_DECAY_MS - MIN_DECAY_MS)).toInt()- effect.reverbLevel = (MIN_LEVEL_MB + amount * (MAX_LEVEL_MB - MIN_LEVEL_MB)).toInt().toShort()- // This is the one that made the effect nearly inaudible. An- // EnvironmentalReverb starts with roomLevel at -9000 mB — ninety- // decibels down, which is silence — and it is a master attenuation- // sitting in front of everything else, so raising reverbLevel and- // the send alone changed almost nothing.- effect.roomLevel = (MIN_ROOM_MB + amount * (MAX_ROOM_MB - MIN_ROOM_MB)).toInt().toShort()- // Keeps the tail from being all low end, which is what a room with- // its highs rolled off sounds like: muddy rather than spacious.- effect.roomHFLevel = (-400).toShort()- // Held steady: these shape the character of the room rather than its- // size, and moving them with the slider makes the low end swell as- // well as the tail, which reads as the track getting muddier rather- // than more distant.- effect.decayHFRatio = 700- effect.density = 1000- effect.diffusion = 1000+ // The room itself is described in one place, since the players that+ // are not this one put a listener in the same one; see ReverbTuning.+ ReverbTuning.tune(effect, amount)effect.enabled = trueoutput.attachAuxEffect(effect.id)- // The dry path is untouched, so this is purely how much goes to the- // tail. Curved rather than linear: the first part of the range is- // 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)+ output.setAuxEffectSendLevel(ReverbTuning.sendLevel(amount))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}") }}diff --git a/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.kt b/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.ktnew file mode 100644index 0000000..130dcff--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/playback/BungeeAudioProcessor.kt@@ -0,0 +1,174 @@+package dev.lelonio.square.playback++import androidx.annotation.OptIn+import androidx.media3.common.C+import androidx.media3.common.PlaybackParameters+import androidx.media3.common.audio.AudioProcessor+import androidx.media3.common.audio.BaseAudioProcessor+import androidx.media3.common.util.UnstableApi+import androidx.media3.common.util.Util+import androidx.media3.exoplayer.audio.DefaultAudioSink+import java.nio.ByteBuffer++/**+ * Speed and pitch for the players that are not the Spotify engine.+ *+ * Spotify's audio goes through the app's own sink, where a phase vocoder does+ * this work; a file on the phone goes through ExoPlayer, which has its own+ * stretcher built for speech. On music that one smears every attack and adds a+ * metallic edge, which is exactly what the same track sounds like on the two+ * paths at the same setting. This puts the same vocoder in ExoPlayer's chain,+ * so a song is altered the same way whatever it was read from.+ *+ * Sixteen-bit PCM only. Everything the decoders here hand over is that, and a+ * format this cannot take is passed through rather than refused: an untouched+ * song is a much better answer than silence.+ */+@OptIn(UnstableApi::class)+class BungeeAudioProcessor : BaseAudioProcessor() {++ private var stretcher: Stretcher? = null+ private var speed = 1f+ private var pitch = 1f++ /** Logged once rather than per packet, which would be several a second. */+ private var failureReported = false++ /**+ * Set from the sink's chain when the listener moves either slider.+ *+ * The stretcher takes them per packet, so nothing has to be rebuilt: what+ * is playing changes on the next few milliseconds of audio.+ */+ fun setSpeedAndPitch(speed: Float, pitch: Float) {+ this.speed = speed+ this.pitch = pitch+ }++ val isAltering: Boolean get() = speed != 1f || pitch != 1f++ override fun onConfigure(+ inputAudioFormat: AudioProcessor.AudioFormat,+ ): AudioProcessor.AudioFormat {+ if (inputAudioFormat.encoding != C.ENCODING_PCM_16BIT) {+ throw AudioProcessor.UnhandledAudioFormatException(inputAudioFormat)+ }+ return inputAudioFormat+ }++ override fun onFlush() {+ // Kept, deliberately.+ //+ // The sink flushes its processors whenever the parameters change, and+ // throwing the vocoder away here meant the next packet arrived at a+ // stretcher with an empty window: it has to fill one before it can+ // resynthesise anything, and that silence is the hole heard when the+ // speed slider moves. What it carries over instead is a few tens of+ // milliseconds of the audio before the change, which after a seek is+ // the only cost — and one nobody can hear.+ //+ // The format is a different matter: a stretcher is built for a sample+ // rate and a channel count, and a track with either different needs a+ // new one. That is what [stretcher] checks.+ }++ override fun onReset() {+ stretcher?.release()+ stretcher = null+ failureReported = false+ }++ override fun queueInput(inputBuffer: ByteBuffer) {+ val size = inputBuffer.remaining()+ if (size == 0) return++ // Nothing to do at rest, and doing nothing is worth the check: this+ // runs on the audio thread for every packet of every track.+ if (!isAltering) {+ replaceOutputBuffer(size).put(inputBuffer).flip()+ return+ }++ val frames = size / (inputAudioFormat.channelCount * BYTES_PER_SAMPLE)+ val stretcher = stretcher(frames)+ val produced = stretcher?.process(inputBuffer, size, speed, pitch)+ if (produced == null) {+ if (!failureReported) {+ failureReported = true+ android.util.Log.w(TAG, "stretcher unavailable, playing untouched")+ }+ // Position is already past what was read on the failed attempt for+ // a partial read; take what is left rather than dropping the packet.+ val remaining = inputBuffer.remaining()+ if (remaining > 0) replaceOutputBuffer(remaining).put(inputBuffer).flip()+ return+ }++ replaceOutputBuffer(produced.remaining()).put(produced).flip()+ }++ /** One stretcher per format, sized for the largest packet seen so far. */+ private fun stretcher(frames: Int): Stretcher? {+ val rate = inputAudioFormat.sampleRate+ val channels = inputAudioFormat.channelCount+ val existing = stretcher+ if (existing != null && frames <= maxFrames && rate == builtForRate &&+ channels == builtForChannels+ ) {+ return existing+ }++ existing?.release()+ maxFrames = maxOf(frames, maxFrames, MIN_PACKET_FRAMES)+ builtForRate = rate+ builtForChannels = channels+ return Stretcher.create(rate, channels, maxFrames).also { stretcher = it }+ }++ private var maxFrames = 0+ private var builtForRate = 0+ private var builtForChannels = 0++ private companion object {+ const val TAG = "SquareStretch"+ const val BYTES_PER_SAMPLE = 2++ /** A decoder's packet, with room to spare, so the first one fits. */+ const val MIN_PACKET_FRAMES = 8192+ }+}++/**+ * The sink's chain, with the vocoder in it instead of the platform's stretcher.+ *+ * ExoPlayer asks the chain to apply speed and pitch, and asks it afterwards how+ * long the audio it has played was in media time. Both answers have to come+ * from whoever is doing the stretching, or the position on screen runs at a+ * different rate from the sound.+ */+@OptIn(UnstableApi::class)+class BungeeProcessorChain(+ private val bungee: BungeeAudioProcessor = BungeeAudioProcessor(),+) : DefaultAudioSink.AudioProcessorChain {++ private var parameters = PlaybackParameters.DEFAULT++ override fun getAudioProcessors(): Array<AudioProcessor> = arrayOf(bungee)++ override fun applyPlaybackParameters(+ playbackParameters: PlaybackParameters,+ ): PlaybackParameters {+ parameters = playbackParameters+ bungee.setSpeedAndPitch(playbackParameters.speed, playbackParameters.pitch)+ return playbackParameters+ }++ override fun applySkipSilenceEnabled(skipSilenceEnabled: Boolean): Boolean = false++ /** How much media a stretch of playing covers, which is the speed itself. */+ override fun getMediaDuration(playoutDuration: Long): Long =+ Util.getMediaDurationForPlayoutDuration(playoutDuration, parameters.speed)++ /** Nothing is dropped here; silence is skipped by nobody in this chain. */+ override fun getSkippedOutputFrameCount(): Long = 0+}diff --git a/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.kt b/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.ktnew file mode 100644index 0000000..fd2a518--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/playback/PlayerReverb.kt@@ -0,0 +1,79 @@+package dev.lelonio.square.playback++import android.media.audiofx.EnvironmentalReverb+import androidx.media3.common.AuxEffectInfo+import androidx.media3.exoplayer.ExoPlayer++/**+ * The same room, for the players that are not the Spotify engine.+ *+ * The reverb on the Spotify path is not a filter this app wrote: it is+ * Android's own [EnvironmentalReverb], attached to the output as an auxiliary+ * effect with a send level. ExoPlayer offers the same attachment through+ * [ExoPlayer.setAuxEffectInfo], so a file on the phone can be put in exactly the+ * same room rather than in a second one that sounds nearly like it.+ *+ * The tuning is shared with [AudioOutput] on purpose; see [ReverbTuning]. Two+ * copies of these numbers would drift apart the first time either was touched.+ */+class PlayerReverb {++ private var effect: EnvironmentalReverb? = null++ /**+ * Puts [amount] of room on [player], or takes it away at zero.+ *+ * Safe to call for a player that has just been built and for one that is+ * about to be released: the effect follows the player rather than the+ * track, so nothing has to be timed against playback starting.+ */+ fun apply(player: ExoPlayer, amount: Float) {+ val level = amount.coerceIn(0f, 1f)+ if (level <= 0f) {+ detach(player)+ return+ }++ val existing = effect+ val reverb = existing ?: runCatching {+ // Priority 0 and the output mix, as on the other path.+ EnvironmentalReverb(0, 0)+ }+ .onFailure { android.util.Log.w(TAG, "reverb unavailable: ${it.message}") }+ .getOrNull()+ ?: return+ effect = reverb++ runCatching {+ ReverbTuning.tune(reverb, level)+ reverb.enabled = true+ player.setAuxEffectInfo(AuxEffectInfo(reverb.id, ReverbTuning.sendLevel(level)))+ if (existing == null) android.util.Log.i(TAG, "reverb on at $level, id ${reverb.id}")+ }.onFailure { android.util.Log.w(TAG, "reverb not applied: ${it.message}") }+ }++ /** Takes the room away and gives the effect back to the system. */+ fun release(player: ExoPlayer?) {+ player?.let(::detach)+ effect?.runCatching {+ enabled = false+ release()+ }+ effect = null+ }++ private fun detach(player: ExoPlayer) {+ runCatching {+ player.setAuxEffectInfo(AuxEffectInfo(AuxEffectInfo.NO_AUX_EFFECT_ID, 0f))+ }+ effect?.runCatching {+ enabled = false+ release()+ }+ effect = null+ }++ private companion object {+ const val TAG = "SquareReverb"+ }+}diff --git a/app/src/main/java/dev/lelonio/square/playback/ReverbTuning.kt b/app/src/main/java/dev/lelonio/square/playback/ReverbTuning.ktnew file mode 100644index 0000000..881b891--- /dev/null+++ b/app/src/main/java/dev/lelonio/square/playback/ReverbTuning.kt@@ -0,0 +1,73 @@+package dev.lelonio.square.playback++import android.media.audiofx.EnvironmentalReverb++/**+ * What one number on a slider means as a room.+ *+ * Kept in one place because two players use it: the Spotify path attaches the+ * effect to its own AudioTrack ([AudioOutput]), and everything else attaches it+ * through ExoPlayer ([PlayerReverb]). The same setting has to be the same room+ * whichever is playing, and a second copy of these numbers would drift from the+ * first the moment either was adjusted.+ */+object ReverbTuning {++ /**+ * Sets the room up for [amount], 0 to 1.+ *+ * The room grows with it: a longer tail and a louder reflection, from a+ * small live room up to a hall.+ */+ fun tune(effect: EnvironmentalReverb, amount: Float) {+ val level = amount.coerceIn(0f, 1f)+ effect.decayTime = (MIN_DECAY_MS + level * (MAX_DECAY_MS - MIN_DECAY_MS)).toInt()+ effect.reverbLevel =+ (MIN_LEVEL_MB + level * (MAX_LEVEL_MB - MIN_LEVEL_MB)).toInt().toShort()+ // The one that decides whether any of this is audible at all. An+ // EnvironmentalReverb starts with roomLevel at -9000 mB — ninety+ // decibels down, which is silence — and it is a master attenuation+ // sitting in front of everything else.+ effect.roomLevel = (MIN_ROOM_MB + level * (MAX_ROOM_MB - MIN_ROOM_MB)).toInt().toShort()+ // Keeps the tail from being all low end, which is what a room with its+ // highs rolled off sounds like: muddy rather than spacious.+ effect.roomHFLevel = (-400).toShort()+ // Held steady: these shape the character of the room rather than its+ // size, and moving them with the slider makes the low end swell as well+ // as the tail, which reads as the track getting muddier rather than+ // more distant.+ effect.decayHFRatio = 700+ effect.density = 1000+ effect.diffusion = 1000+ }++ /**+ * How much of the signal goes to the tail, for [amount].+ *+ * The dry path is untouched, so this is purely the send. Curved rather than+ * linear: the first part of the range is where the audible change happens,+ * and a straight mapping leaves most of the slider doing almost nothing.+ */+ fun sendLevel(amount: Float): Float {+ val level = amount.coerceIn(0f, 1f)+ return level * level * 0.85f + level * 0.15f+ }++ /** Decay range, in milliseconds: a live room up to a large hall. */+ private const val MIN_DECAY_MS = 500f+ private const val MAX_DECAY_MS = 4500f++ /**+ * Reverb level in millibels. The API allows down to -9000, which is+ * inaudible, so the useful part of the range starts much higher.+ */+ private const val MIN_LEVEL_MB = -2600f+ private const val MAX_LEVEL_MB = -200f++ /**+ * Master room level, in millibels. 0 is unattenuated; the API default of+ * -9000 is inaudible.+ */+ private const val MIN_ROOM_MB = -2000f+ private const val MAX_ROOM_MB = 0f+}