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
Local files play at the level of the streamed tracks, through every fade
Lelonio1b305435abe7
diff --git a/native/Cargo.lock b/native/Cargo.lockindex 06e06dd..df59ae7 100644--- a/native/Cargo.lock+++ b/native/Cargo.lock@@ -328,6 +328,21 @@ dependencies = ["syn 2.0.119",]+[[package]]+name = "dasp_frame"+version = "0.11.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b2a3937f5fe2135702897535c8d4a5553f8b116f76c1529088797f2eee7c5cd6"+dependencies = [+ "dasp_sample",+]++[[package]]+name = "dasp_sample"+version = "0.11.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"+[[package]]name = "data-encoding"version = "2.11.0"@@ -405,6 +420,18 @@ dependencies = ["syn 3.0.3",]+[[package]]+name = "ebur128"+version = "0.1.10"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e227cc62d64d6fe01abbef48134b9c1f17d470cef1e7a56337ad05b1f81df7f9"+dependencies = [+ "bitflags 1.3.2",+ "dasp_frame",+ "dasp_sample",+ "smallvec",+]+[[package]]name = "either"version = "1.17.0"@@ -1238,6 +1265,7 @@ dependencies = [name = "librespot-playback"version = "0.8.0"dependencies = [+ "ebur128","form_urlencoded","futures-util","librespot-audio",diff --git a/native/vendor/librespot-playback/Cargo.toml b/native/vendor/librespot-playback/Cargo.tomlindex f24bd49..9dd462e 100644--- a/native/vendor/librespot-playback/Cargo.toml+++ b/native/vendor/librespot-playback/Cargo.toml@@ -162,6 +162,11 @@ optional = true[dependencies.shell-words]version = "1.1"+# LOCAL PATCH: loudness of local files, which carry no Spotify normalisation+# data; see local_file.rs.+[dependencies.ebur128]+version = "0.1.10"+[dependencies.symphonia]version = "0.5"features = [diff --git a/native/vendor/librespot-playback/src/decoder/mod.rs b/native/vendor/librespot-playback/src/decoder/mod.rsindex e77d6e9..7e4bf66 100644--- a/native/vendor/librespot-playback/src/decoder/mod.rs+++ b/native/vendor/librespot-playback/src/decoder/mod.rs@@ -7,6 +7,8 @@ mod passthrough_decoder;#[cfg(feature = "passthrough-decoder")]pub use passthrough_decoder::PassthroughDecoder;+/// LOCAL PATCH: see the module.+mod resample;mod symphonia_decoder;pub use symphonia_decoder::SymphoniaDecoder;diff --git a/native/vendor/librespot-playback/src/decoder/resample.rs b/native/vendor/librespot-playback/src/decoder/resample.rsnew file mode 100644index 0000000..e204d13--- /dev/null+++ b/native/vendor/librespot-playback/src/decoder/resample.rs@@ -0,0 +1,140 @@+//! LOCAL PATCH: local files at other rates and channel counts.+//!+//! The player runs at 44.1 kHz stereo throughout, which is everything Spotify+//! streams. A file on a phone is as often 48 kHz, and sometimes mono, and the+//! decoder used to refuse both. This brings such a file to the player's format:+//! a windowed-sinc resampler, cut off below the lower of the two Nyquist+//! frequencies so a downsample does not alias, and a mono channel played on+//! both sides.++use std::f64::consts::PI;++/// Taps on each side of the point being interpolated.+const HALF_TAPS: usize = 16;++pub struct Resampler {+ /// Input samples per output sample.+ step: f64,+ /// Below 1 when downsampling: the kernel is widened by its inverse.+ cutoff: f64,+ channels: usize,+ /// Input not yet consumed, per output channel, after [HALF_TAPS] of lead.+ history: [Vec<f64>; 2],+ /// Where the next output sample falls, in input samples into [history].+ position: f64,+}++impl Resampler {+ pub fn new(input_rate: u32, output_rate: u32, input_channels: usize) -> Self {+ let step = input_rate as f64 / output_rate as f64;+ let mut resampler = Self {+ step,+ cutoff: (1.0 / step).min(1.0) * 0.97,+ channels: input_channels,+ history: [Vec::new(), Vec::new()],+ position: 0.0,+ };+ resampler.reset();+ resampler+ }++ /// After a seek: nothing before the new position belongs to it.+ pub fn reset(&mut self) {+ for channel in &mut self.history {+ channel.clear();+ channel.resize(HALF_TAPS, 0.0);+ }+ self.position = HALF_TAPS as f64;+ }++ /// Interleaved input in, interleaved 44.1 kHz stereo out.+ pub fn process(&mut self, input: &[f64]) -> Vec<f64> {+ let channels = self.channels.max(1);+ for frame in input.chunks_exact(channels) {+ let left = frame[0];+ let right = if channels > 1 { frame[1] } else { left };+ self.history[0].push(left);+ self.history[1].push(right);+ }++ let available = self.history[0].len();+ let mut output = Vec::with_capacity(((input.len() / channels) as f64 / self.step) as usize * 2 + 4);+ while self.position + (HALF_TAPS as f64) < available as f64 {+ let centre = self.position.floor() as usize;+ for channel in &self.history {+ let mut sum = 0.0;+ for k in centre + 1 - HALF_TAPS..=centre + HALF_TAPS {+ sum += channel[k] * self.kernel(self.position - k as f64);+ }+ output.push(sum);+ }+ self.position += self.step;+ }++ // Keep only what the next call can still need.+ let drop = (self.position.floor() as usize).saturating_sub(HALF_TAPS);+ if drop > 0 {+ for channel in &mut self.history {+ channel.drain(..drop.min(channel.len()));+ }+ self.position -= drop as f64;+ }+ output+ }++ fn kernel(&self, distance: f64) -> f64 {+ let reach = HALF_TAPS as f64;+ if distance.abs() >= reach {+ return 0.0;+ }+ let x = distance * self.cutoff;+ let sinc = if x.abs() < 1e-9 { 1.0 } else { (PI * x).sin() / (PI * x) };+ // Blackman, across the kernel's width.+ let w = 0.5 + 0.5 * distance / reach;+ let window = 0.42 - 0.5 * (2.0 * PI * w).cos() + 0.08 * (4.0 * PI * w).cos();+ self.cutoff * sinc * window+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ /// A 1 kHz tone at 48 kHz comes out as a 1 kHz tone at 44.1 kHz, at the+ /// same level, in the number of samples the rates call for.+ #[test]+ fn resamples_a_tone() {+ let input: Vec<f64> = (0..48_000)+ .flat_map(|n| {+ let v = (2.0 * PI * 1000.0 * n as f64 / 48_000.0).sin() * 0.5;+ [v, v]+ })+ .collect();+ let mut resampler = Resampler::new(48_000, 44_100, 2);+ let mut output = Vec::new();+ for chunk in input.chunks(2 * 1152) {+ output.extend(resampler.process(chunk));+ }+ let frames = output.len() / 2;+ assert!((frames as i64 - 44_100).abs() < 64, "{frames} frames");++ // Compare against the ideal tone away from the edges.+ let worst = (2_000..40_000)+ .map(|n| {+ // Output n is the input at n / 44 100 s: no delay, the lead+ // of zeros is consumed before the first sample.+ let ideal = (2.0 * PI * 1000.0 * n as f64 / 44_100.0).sin() * 0.5;+ (output[n * 2] - ideal).abs()+ })+ .fold(0.0, f64::max);+ assert!(worst < 0.01, "worst error {worst}");+ }++ /// A mono file is played on both sides.+ #[test]+ fn mono_to_stereo() {+ let mut resampler = Resampler::new(44_100, 44_100, 1);+ let output = resampler.process(&vec![0.25; 4_096]);+ assert!(output.chunks_exact(2).skip(64).all(|f| (f[0] - f[1]).abs() < 1e-12));+ }+}diff --git a/native/vendor/librespot-playback/src/decoder/symphonia_decoder.rs b/native/vendor/librespot-playback/src/decoder/symphonia_decoder.rsindex 56561bb..3a19460 100644--- a/native/vendor/librespot-playback/src/decoder/symphonia_decoder.rs+++ b/native/vendor/librespot-playback/src/decoder/symphonia_decoder.rs@@ -10,7 +10,7 @@ use symphonia::core::{probe::{Hint, ProbeResult},};-use super::{AudioDecoder, AudioPacket, AudioPacketPosition, DecoderError, DecoderResult};+use super::{AudioDecoder, AudioPacket, AudioPacketPosition, DecoderError, DecoderResult, resample::Resampler};use crate::{NUM_CHANNELS, PAGES_PER_MS, SAMPLE_RATE, player::NormalisationData, symphonia_util};@@ -18,6 +18,8 @@ pub struct SymphoniaDecoder {probe_result: ProbeResult,decoder: Box<dyn Decoder>,sample_buffer: Option<SampleBuffer<f64>>,+ /// LOCAL PATCH: for a file that is not 44.1 kHz stereo; see resample.rs.+ resampler: Option<Resampler>,}#[derive(Default)]@@ -64,31 +66,29 @@ impl SymphoniaDecoder {DecoderError::SymphoniaDecoder("Could not retrieve sample rate".into())})?;- // TODO: The official client supports local files with sample rates other than 44,100 kHz.- // To play these accurately, we need to either resample the input audio, or introduce a way- // to change the player's current sample rate (likely by closing and re-opening the sink- // with new parameters).- if rate != SAMPLE_RATE {- return Err(DecoderError::SymphoniaDecoder(format!(- "Unsupported sample rate: {rate}"- )));- }-let channels = decoder.codec_params().channels.ok_or_else(|| {DecoderError::SymphoniaDecoder("Could not retrieve channel configuration".into())})?;- if channels.count() != NUM_CHANNELS as usize {+ if channels.count() == 0 || channels.count() > NUM_CHANNELS as usize {return Err(DecoderError::SymphoniaDecoder(format!("Unsupported number of channels: {channels}")));}+ // LOCAL PATCH: upstream refused anything but 44.1 kHz stereo, which is+ // all Spotify streams and much less than what a phone holds. Other+ // rates are resampled and a mono file is played on both sides; a+ // 44.1 kHz stereo stream goes through untouched.+ let resampler = (rate != SAMPLE_RATE || channels.count() != NUM_CHANNELS as usize)+ .then(|| Resampler::new(rate, SAMPLE_RATE, channels.count()));+Ok(Self {probe_result,decoder,// We set the sample buffer when decoding the first full packet,// whose duration is also the ideal sample buffer size.sample_buffer: None,+ resampler,})}@@ -213,6 +213,9 @@ impl AudioDecoder for SymphoniaDecoder {// Seeking is a `FormatReader` operation, so the decoder cannot reliably// know when a seek took place. Reset it to avoid audio glitches.self.decoder.reset();+ if let Some(resampler) = self.resampler.as_mut() {+ resampler.reset();+ }Ok(self.ts_to_ms(seeked_to_ts.actual_ts))}@@ -253,7 +256,14 @@ impl AudioDecoder for SymphoniaDecoder {};sample_buffer.copy_interleaved_ref(decoded);- let samples = AudioPacket::Samples(sample_buffer.samples().to_vec());+ let samples = match self.resampler.as_mut() {+ // A packet can be too short to finish an output sample.+ Some(resampler) => match resampler.process(sample_buffer.samples()) {+ resampled if resampled.is_empty() => continue,+ resampled => AudioPacket::Samples(resampled),+ },+ None => AudioPacket::Samples(sample_buffer.samples().to_vec()),+ };return Ok(Some((packet_position, samples)));}diff --git a/native/vendor/librespot-playback/src/local_file.rs b/native/vendor/librespot-playback/src/local_file.rsindex 92544dc..2696fe3 100644--- a/native/vendor/librespot-playback/src/local_file.rs+++ b/native/vendor/librespot-playback/src/local_file.rs@@ -1,4 +1,9 @@-use crate::symphonia_util;+use crate::{+ NUM_CHANNELS, SAMPLE_RATE,+ decoder::{AudioDecoder, AudioPacket, SymphoniaDecoder},+ player::NormalisationData,+ symphonia_util,+};use librespot_core::{Error, SpotifyUri};use std::{collections::HashMap,@@ -50,10 +55,93 @@ static APP_FILES: std::sync::RwLock<Option<HashMap<SpotifyUri, PathBuf>>> =std::sync::RwLock::new(None);/// LOCAL PATCH: replaces the app's matches; see [APP_FILES].+///+/// Files not measured yet are measured in the background, so they are at the+/// level of the streamed tracks by the time they play; see [loudness].pub fn set_app_files(files: HashMap<SpotifyUri, PathBuf>) {+ let pending: Vec<PathBuf> = files+ .values()+ .filter(|path| measured(path).is_none())+ .cloned()+ .collect();if let Ok(mut current) = APP_FILES.write() {*current = Some(files);}+ if !pending.is_empty() {+ let _ = std::thread::Builder::new()+ .name("local-loudness".into())+ .spawn(move || {+ for path in pending {+ let _ = loudness(&path);+ }+ });+ }+}++/// LOCAL PATCH: how loud each local file is, once measured.+static LOUDNESS: std::sync::RwLock<Option<HashMap<PathBuf, NormalisationData>>> =+ std::sync::RwLock::new(None);++/// The loudness Spotify normalises its own tracks to, in LUFS: a file is given+/// the gain that brings it there, as a Spotify track is by the gain it carries.+const TARGET_LUFS: f64 = -14.0;++fn measured(path: &Path) -> Option<NormalisationData> {+ LOUDNESS.read().ok()?.as_ref()?.get(path).copied()+}++/// LOCAL PATCH: normalisation data for a local file that has no ReplayGain+/// tags of its own: the gain to [TARGET_LUFS] from its integrated loudness+/// (EBU R128), and its sample peak. Measured once, by decoding the whole file,+/// then remembered.+///+/// Without it a local file played at whatever level it was mastered at, next+/// to streamed tracks that are all brought to one level, and the difference+/// was plain to hear.+pub fn loudness(path: &Path) -> Option<NormalisationData> {+ if let Some(known) = measured(path) {+ return Some(known);+ }+ let mut hint = Hint::new();+ if let Some(extension) = path.extension().and_then(|e| e.to_str()) {+ hint.with_extension(extension);+ }+ let mut decoder = SymphoniaDecoder::new(File::open(path).ok()?, hint).ok()?;+ // Everything the decoder hands out is 44.1 kHz stereo; see resample.rs.+ let mut meter = ebur128::EbuR128::new(+ NUM_CHANNELS as u32,+ SAMPLE_RATE,+ ebur128::Mode::I | ebur128::Mode::SAMPLE_PEAK,+ )+ .ok()?;+ while let Ok(Some((_, packet))) = decoder.next_packet() {+ if let AudioPacket::Samples(samples) = packet {+ meter.add_frames_f64(&samples).ok()?;+ }+ }+ let lufs = meter.loudness_global().ok()?;+ if !lufs.is_finite() {+ return None;+ }+ let peak = (0..NUM_CHANNELS as u32)+ .filter_map(|channel| meter.sample_peak(channel).ok())+ .fold(0.0, f64::max)+ .max(f64::MIN_POSITIVE);+ let data = NormalisationData {+ track_gain_db: TARGET_LUFS - lufs,+ track_peak: peak,+ album_gain_db: TARGET_LUFS - lufs,+ album_peak: peak,+ };+ info!(+ "local file {} measured at {lufs:.1} LUFS, gain {:.1} dB",+ path.display(),+ data.track_gain_db+ );+ if let Ok(mut known) = LOUDNESS.write() {+ known.get_or_insert_with(HashMap::new).insert(path.to_path_buf(), data);+ }+ Some(data)}pub fn create_local_file_lookup(directories: &[PathBuf]) -> LocalFileLookup {diff --git a/native/vendor/librespot-playback/src/player.rs b/native/vendor/librespot-playback/src/player.rsindex a064fd4..858d55b 100644--- a/native/vendor/librespot-playback/src/player.rs+++ b/native/vendor/librespot-playback/src/player.rs@@ -197,6 +197,66 @@ struct FadeOut {/// incoming song then came in most of the way up already, which is a cut/// with a fade in front of it.rising: Option<usize>,+ /// LOCAL PATCH: the limiter the outgoing track was being played through,+ /// carried on for its tail; see [`TailLimiter`].+ limiter: TailLimiter,+}++/// LOCAL PATCH: the dynamic limiter, for the tail of a crossfade.+///+/// A track plays through the limiter after its normalisation gain, and the+/// tail of one being faded out used to be given the gain alone. Where the gain+/// is above unity — a quiet local file brought up to the level of the streamed+/// tracks, plus the pregain — the tail came out far louder than the song had+/// been a second before. This is the same limiter, picking up from where the+/// main one was when the fade began, so the tail stays at its own level.+#[derive(Clone, Copy)]+struct TailLimiter {+ integrators: [f64; 2],+ peaks: [f64; 2],+ channel: usize,+}++/// LOCAL PATCH: what [`TailLimiter`] needs from the configuration, copied out+/// so it can be read while the fade is borrowed.+#[derive(Clone, Copy)]+struct LimiterParams {+ dynamic: bool,+ threshold_db: f64,+ knee_db: f64,+ knee_factor: f64,+ attack_cf: f64,+ release_cf: f64,+}++impl TailLimiter {+ /// The main limiter's step, on one sample already scaled by its gain.+ fn limit(&mut self, sample: f64, params: &LimiterParams) -> f64 {+ if !params.dynamic {+ return sample;+ }+ let sample = sample + f64::MIN_POSITIVE;+ let bias_db = ratio_to_db(sample.abs()) - params.threshold_db;+ let knee_boundary_db = bias_db * 2.0;+ let limiter_db = if knee_boundary_db < -params.knee_db {+ 0.0+ } else if knee_boundary_db.abs() <= params.knee_db {+ let term = knee_boundary_db + params.knee_db;+ term * term * params.knee_factor+ } else {+ bias_db+ };+ let channel = self.channel;+ self.channel ^= 1;+ let integrator = &mut self.integrators[channel];+ *integrator = f64::max(+ limiter_db,+ params.release_cf * *integrator + (1.0 - params.release_cf) * limiter_db,+ );+ let peak = &mut self.peaks[channel];+ *peak = params.attack_cf * *peak + (1.0 - params.attack_cf) * *integrator;+ sample * db_to_ratio(-f64::max(self.peaks[0], self.peaks[1]))+ }}static PLAYER_COUNTER: AtomicUsize = AtomicUsize::new(0);@@ -1740,10 +1800,22 @@ impl PlayerTrackLoader {};let mut decoder = Box::new(decoder);- let normalisation_data = decoder.normalisation_data().unwrap_or_else(|| {- warn!("Unable to get normalisation data, continuing with defaults.");- NormalisationData::default()- });+ // LOCAL PATCH: a file with no ReplayGain tags is measured instead, so+ // it plays at the level of the streamed tracks; see local_file.rs.+ let normalisation_data = match decoder.normalisation_data() {+ Some(data) => data,+ None => {+ let measured_path = path.clone();+ tokio::task::spawn_blocking(move || crate::local_file::loudness(&measured_path))+ .await+ .ok()+ .flatten()+ .unwrap_or_else(|| {+ warn!("Unable to get normalisation data, continuing with defaults.");+ NormalisationData::default()+ })+ }+ };let local_file_metadata = decoder.local_file_metadata().unwrap_or_default();@@ -2316,6 +2388,11 @@ impl PlayerInternal {spare: std::collections::VecDeque::new(),drained: false,rising,+ limiter: TailLimiter {+ integrators: self.normalisation_integrators,+ peaks: self.normalisation_peaks,+ channel: self.normalisation_channel,+ },});}// Nothing was playing: put the state back and leave the caller to@@ -2336,8 +2413,22 @@ impl PlayerInternal {/// The result is clamped just under full scale. Two tracks played at once/// can exceed it between them, and what comes out of an overflow is a click/// rather than a loud moment.+ /// LOCAL PATCH: see [`LimiterParams`].+ fn limiter_params(&self) -> LimiterParams {+ LimiterParams {+ dynamic: self.config.normalisation+ && self.config.normalisation_method == NormalisationMethod::Dynamic,+ threshold_db: self.config.normalisation_threshold_dbfs,+ knee_db: self.config.normalisation_knee_db,+ knee_factor: self.normalisation_knee_factor,+ attack_cf: self.config.normalisation_attack_cf,+ release_cf: self.config.normalisation_release_cf,+ }+ }+fn apply_fade(&mut self, data: &mut [f64]) {let volume = self.volume_getter.attenuation_factor();+ let params = self.limiter_params();let channels = NUM_CHANNELS as usize;let Some(fade) = self.fade_out.as_mut() else {@@ -2391,8 +2482,9 @@ impl PlayerInternal {} else {0.0};- let mixed = *sample * gain_in- + tail * gain_out * fade.normalisation_factor * volume;+ // Levelled as it was while it played alone, then faded.+ let tail = fade.limiter.limit(tail * fade.normalisation_factor, ¶ms);+ let mixed = *sample * gain_in + tail * gain_out * volume;*sample = soft_limit(mixed);}@@ -2422,6 +2514,7 @@ impl PlayerInternal {/// caller knows to come round again rather than wait to be woken.fn pump_fade_out(&mut self) -> bool {let volume = self.volume_getter.attenuation_factor();+ let params = self.limiter_params();let (samples, drained, rising) = {let Some(fade) = self.fade_out.as_mut() else {@@ -2450,11 +2543,10 @@ impl PlayerInternal {}let x = fade.done as f64 / fade.total as f64;- let gain = (x * std::f64::consts::FRAC_PI_2).cos()- * fade.normalisation_factor- * volume;+ let gain = (x * std::f64::consts::FRAC_PI_2).cos() * volume;for sample in samples.iter_mut() {- *sample = soft_limit(*sample * gain);+ let levelled = fade.limiter.limit(*sample * fade.normalisation_factor, ¶ms);+ *sample = soft_limit(levelled * gain);}// Counted here as well, or the curve would stand still through the