From 24ff874d34d2af5c79990b9a60a42a9b7f22a892 Mon Sep 17 00:00:00 2001 From: Yuriy Panov Date: Thu, 15 Oct 2026 18:33:04 +0600 Subject: [PATCH] feat: lossless (FLAC) streaming Adds a Lossless streaming quality that asks /get-file-info for FLAC and decrypts its encraw stream in memory. FLAC and FLAC-in-MP4 decode through symphonia. When Yandex grants no FLAC, its lossy reply is played as is, except HE-AAC, which falls back to the MP3 path. The player bar badge shows the codec actually served and turns accent-coloured for FLAC. Adds a read-only stream_probe example. --- README.md | 10 ++++ crates/musyx/examples/stream_probe.rs | 47 +++++++++++++++++ crates/musyx/src/app.rs | 15 +++--- crates/musyx/src/player/mod.rs | 4 +- crates/musyx/src/store.rs | 16 ++++-- crates/musyx/src/widgets/player_bar.rs | 5 +- crates/ym-api/src/client.rs | 29 ++++++++-- crates/ym-api/src/models.rs | 73 ++++++++++++++++++++++++-- 8 files changed, 175 insertions(+), 24 deletions(-) create mode 100644 crates/musyx/examples/stream_probe.rs diff --git a/README.md b/README.md index 58ff105..1c630c8 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,12 @@ Settings (streaming quality, appearance, volume) are kept in `~/Library/Application Support/Musyx/settings.json`. Covers are cached under `~/Library/Caches/Musyx/covers` and can be cleared from Settings. +Streaming quality **Lossless** asks Yandex for FLAC (through the signed `/get-file-info` +endpoint, decrypting its AES-CTR stream in memory). Yandex decides what to grant by +subscription, region and track. When there's no FLAC, it answers with the best lossy file +it allows, and Musyx plays that. The badge in the player bar shows what was actually served, +and turns accent-coloured for FLAC. + ## Check ```sh @@ -82,6 +88,10 @@ to stderr, followed by what's playing and the next three tracks. `cargo run -p musyx --example audio_probe` lists the output devices and checks that each one opens. Use it when the player bar says no sound output was found. +`cargo run -p musyx --example stream_probe -- "artist title"` asks for the lossless stream +of the first search hit with the stored account (read-only). It downloads and decrypts the +file, and prints the codec Yandex granted plus the decoded sample rate. + ## Licences Musyx is MIT. Onest and Unbounded are OFL (`crates/musyx/assets/fonts`), and Lucide icons diff --git a/crates/musyx/examples/stream_probe.rs b/crates/musyx/examples/stream_probe.rs new file mode 100644 index 0000000..9e00e88 --- /dev/null +++ b/crates/musyx/examples/stream_probe.rs @@ -0,0 +1,47 @@ +//! Read-only check of lossless streaming with the signed-in account: +//! `cargo run -p musyx --example stream_probe [query]`. Asks for FLAC, downloads +//! and decrypts it, and decodes the start to show the format. +use std::io::Cursor; +use std::time::Instant; + +use rodio::Source; +use ym_api::auth::Credentials; +use ym_api::{SearchKind, YmClient}; + +#[tokio::main(flavor = "current_thread")] +async fn main() { + let raw = keyring::Entry::new("musyx", "yandex-music") + .and_then(|e| e.get_password()) + .expect("sign in with Musyx first"); + let credentials: Credentials = serde_json::from_str(&raw).expect("stored credentials"); + let (client, account) = YmClient::connect(&credentials).await.expect("connect"); + println!("account: {} (plus: {})", account.login, account.has_plus); + + let query = std::env::args().nth(1).unwrap_or_else(|| "земфира хочешь".into()); + let found = client.search(&query, SearchKind::Tracks, 0).await.expect("search"); + let track = found.tracks.first().expect("no tracks found"); + println!("track: {} — {} ({})", track.artist_line(), track.title, track.id); + + let started = Instant::now(); + let stream = client.lossless_stream(&track.id).await.expect("stream"); + println!("stream: {stream:?} label={} in {:?}", stream.label(), started.elapsed()); + let started = Instant::now(); + let bytes = client.fetch(&stream).await.expect("fetch"); + println!("fetched {:.1} MB in {:?}, starts with {:?}", bytes.len() as f64 / 1e6, started.elapsed(), String::from_utf8_lossy(&bytes[..12])); + + let decoder = rodio::Decoder::builder() + .with_data(Cursor::new(bytes.to_vec())) + .with_byte_len(bytes.len() as u64) + .with_hint(stream.container()) + .with_seekable(true) + .build() + .expect("decode"); + println!( + "decoded: {} Hz, {} ch, duration {:?}", + decoder.sample_rate(), + decoder.channels(), + decoder.total_duration() + ); + let samples = decoder.take(48_000).filter(|s| s.abs() > 0.0).count(); + println!("non-silent samples in the first 48k: {samples}"); +} diff --git a/crates/musyx/src/app.rs b/crates/musyx/src/app.rs index 8cbbede..49c5137 100644 --- a/crates/musyx/src/app.rs +++ b/crates/musyx/src/app.rs @@ -962,15 +962,15 @@ impl App { Task::batch(effects.into_iter().map(|effect| match effect { Effect::Fetch { generation, track_id } => { let client = client.clone(); - let cap = self.settings.quality.max_kbps(); - Task::future(async move { fetch_audio(&client, &track_id, cap).await }) + let quality = self.settings.quality; + Task::future(async move { fetch_audio(&client, &track_id, quality).await }) .map(move |result| Message::Player(player::Msg::Fetched { generation, result })) } Effect::Prefetch { track_id } => { let client = client.clone(); - let cap = self.settings.quality.max_kbps(); + let quality = self.settings.quality; Task::future(async move { - let result = fetch_audio(&client, &track_id, cap).await; + let result = fetch_audio(&client, &track_id, quality).await; (track_id, result) }) .map(|(track_id, result)| Message::Player(player::Msg::Prefetched { track_id, result })) @@ -1024,8 +1024,11 @@ impl App { } } -async fn fetch_audio(client: &YmClient, track_id: &str, max_kbps: Option) -> Result { - let stream = client.stream(track_id, max_kbps).await?; +async fn fetch_audio(client: &YmClient, track_id: &str, quality: Quality) -> Result { + let stream = match quality { + Quality::Lossless => client.lossless_stream(track_id).await?, + _ => client.stream(track_id, quality.max_kbps()).await?, + }; let bytes = client.fetch(&stream).await?; Ok(Audio { stream, bytes }) } diff --git a/crates/musyx/src/player/mod.rs b/crates/musyx/src/player/mod.rs index eca5112..e60ac57 100644 --- a/crates/musyx/src/player/mod.rs +++ b/crates/musyx/src/player/mod.rs @@ -600,7 +600,7 @@ impl Player { self.send(Command::Load { generation: self.generation, bytes: audio.bytes.clone(), - hint: audio.stream.codec.clone(), + hint: audio.stream.container().to_owned(), play, }); self.current = Some(audio); @@ -644,7 +644,7 @@ mod tests { fn audio() -> Audio { Audio { - stream: Stream { url: "u".into(), codec: "mp3".into(), bitrate_kbps: 320, preview: false }, + stream: Stream { urls: vec!["u".into()], codec: "mp3".into(), bitrate_kbps: 320, preview: false, key: None }, bytes: Arc::from(vec![0u8; 4]), } } diff --git a/crates/musyx/src/store.rs b/crates/musyx/src/store.rs index 31746c7..602874d 100644 --- a/crates/musyx/src/store.rs +++ b/crates/musyx/src/store.rs @@ -9,7 +9,9 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] #[serde(rename_all = "kebab-case")] pub enum Quality { - /// The best Yandex offers (usually MP3 320). + /// FLAC where Yandex grants it, else the best lossy file it allows. + Lossless, + /// The best MP3 Yandex offers. #[default] Best, /// Up to 192 kbps. @@ -19,10 +21,11 @@ pub enum Quality { } impl Quality { - pub const ALL: [Quality; 3] = [Quality::Best, Quality::Balanced, Quality::DataSaver]; + pub const ALL: [Quality; 4] = [Quality::Lossless, Quality::Best, Quality::Balanced, Quality::DataSaver]; pub fn label(self) -> &'static str { match self { + Quality::Lossless => "Lossless", Quality::Best => "Best", Quality::Balanced => "Balanced", Quality::DataSaver => "Data saver", @@ -31,7 +34,11 @@ impl Quality { pub fn detail(self) -> &'static str { match self { - Quality::Best => "Highest bitrate available, usually MP3 320 kbps.", + Quality::Lossless => { + "FLAC when Yandex offers it for your subscription and region, otherwise the best it allows. \ + About 3–5 times the data of MP3, and tracks take longer to start." + } + Quality::Best => "Highest MP3 bitrate Yandex offers, up to 320 kbps.", Quality::Balanced => "Up to 192 kbps. About 40% less data.", Quality::DataSaver => "Up to 128 kbps, for slow or metered connections.", } @@ -40,7 +47,7 @@ impl Quality { /// The bitrate cap passed to the API; `None` means no cap. pub fn max_kbps(self) -> Option { match self { - Quality::Best => None, + Quality::Lossless | Quality::Best => None, Quality::Balanced => Some(192), Quality::DataSaver => Some(128), } @@ -117,6 +124,7 @@ mod tests { let s = Settings { quality: Quality::DataSaver, appearance: Appearance::Dark, volume: 0.25 }; let json = serde_json::to_string(&s).unwrap(); assert!(json.contains("\"data-saver\""), "{json}"); + assert_eq!(serde_json::to_string(&Quality::Lossless).unwrap(), "\"lossless\""); assert_eq!(serde_json::from_str::(&json).unwrap(), s); let partial: Settings = serde_json::from_str(r#"{"appearance":"light"}"#).unwrap(); assert_eq!(partial, Settings { appearance: Appearance::Light, ..Settings::default() }); diff --git a/crates/musyx/src/widgets/player_bar.rs b/crates/musyx/src/widgets/player_bar.rs index e204df6..97231cd 100644 --- a/crates/musyx/src/widgets/player_bar.rs +++ b/crates/musyx/src/widgets/player_bar.rs @@ -181,9 +181,8 @@ fn extras<'a>(t: &'static Tokens, p: &'a Player, drawer: Option) -> Eleme let mut items = row![].spacing(4).align_y(Alignment::Center); if let Some(stream) = p.stream() { let label = if stream.preview { "Preview".to_owned() } else { stream.label() }; - items = items.push( - container(line(label, theme::MEDIUM, 11.5, t.muted)).padding([2, 6]).style(theme::badge), - ); + let ink = if stream.lossless() { t.accent } else { t.muted }; + items = items.push(container(line(label, theme::MEDIUM, 11.5, ink)).padding([2, 6]).style(theme::badge)); } let volume = if p.muted { 0.0 } else { p.volume }; diff --git a/crates/ym-api/src/client.rs b/crates/ym-api/src/client.rs index a9fb205..22605c3 100644 --- a/crates/ym-api/src/client.rs +++ b/crates/ym-api/src/client.rs @@ -7,7 +7,7 @@ use crate::library::{ }; use crate::edit::{LikedIds, Lyrics, QueueContext}; use crate::discover::{ChartEntry, Genre, Home, SearchKind, SearchResults}; -use crate::models::{Stream, Track, best_variant}; +use crate::models::{Stream, Track, best_variant, file_info_stream}; use crate::wave::{PlayReport, Station, WaveBatch, WaveEvent, WaveSettings, iso_millis, numeric_id}; use crate::{Error, Result}; @@ -145,16 +145,37 @@ impl YmClient { .ok_or_else(|| Error::Api(format!("no playable variant for track {track_id}")))?; let url = variant.direct_link(self.inner.transport()).await?; Ok(Stream { - url, + urls: vec![url], codec: variant.codec.clone().unwrap_or_default(), bitrate_kbps: variant.bitrate_in_kbps.unwrap_or(0).max(0) as u32, preview: variant.preview.unwrap_or(false), + key: None, }) } - /// Downloads a whole stream into memory (a 320 kbps track is about 10 MB). + /// FLAC when Yandex grants it for this track and account. Otherwise Yandex + /// answers with the best lossy file it allows (often AAC 192), which is played + /// as is unless it's HE-AAC, which the decoder can't do justice; then the MP3 + /// path, as [`stream`](Self::stream) with no cap. + pub async fn lossless_stream(&self, track_id: &str) -> Result { + match self.inner.track_lossless_info(track_id, "lossless").await.map_err(Error::from) { + Ok(info) => { + if let Some(stream) = file_info_stream(info) { + return Ok(stream); + } + } + Err(e @ (Error::Unauthorized(_) | Error::Network(_))) => return Err(e), + // An API refusal (region, rights) still leaves the MP3 path. + Err(_) => {} + } + self.stream(track_id, None).await + } + + /// Downloads a whole stream into memory, decrypting it when it is encrypted + /// (MP3 320 is about 10 MB a track, FLAC 30–60 MB). pub async fn fetch(&self, stream: &Stream) -> Result> { - let bytes = yamuse::download::Downloader::new(self.inner.transport(), [stream.url.clone()]) + let bytes = yamuse::download::Downloader::new(self.inner.transport(), stream.urls.iter().cloned()) + .maybe_encraw_key(stream.key.clone()) .to_bytes() .await?; Ok(bytes.into()) diff --git a/crates/ym-api/src/models.rs b/crates/ym-api/src/models.rs index 8b8f390..dee3e94 100644 --- a/crates/ym-api/src/models.rs +++ b/crates/ym-api/src/models.rs @@ -122,21 +122,64 @@ pub(crate) fn hues_for(seed: u64) -> (f32, f32) { } /// Where a track's audio comes from. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq)] pub struct Stream { - pub url: String, - /// "mp3" or "aac". + /// Mirrors of the same file; any one works. + pub urls: Vec, + /// "mp3", "aac", "flac" or "flac-mp4". pub codec: String, + /// 0 when Yandex doesn't say (lossless). pub bitrate_kbps: u32, /// True when only a 30-second preview is available (no Plus). pub preview: bool, + /// Hex AES-CTR key for an encrypted (`encraw`) lossless file. + pub key: Option, } impl Stream { - /// "MP3 320", shown in the player bar. + /// "MP3 320" or "FLAC", shown in the player bar. pub fn label(&self) -> String { - format!("{} {}", self.codec.to_ascii_uppercase(), self.bitrate_kbps) + let name = self.codec.trim_end_matches("-mp4").to_ascii_uppercase(); + if self.lossless() || self.bitrate_kbps == 0 { name } else { format!("{name} {}", self.bitrate_kbps) } } + + pub fn lossless(&self) -> bool { + matches!(self.codec.as_str(), "flac" | "flac-mp4") + } + + /// The file format, for the decoder: "flac-mp4" is FLAC inside an MP4 container. + pub fn container(&self) -> &str { + if self.codec.ends_with("-mp4") { "mp4" } else { &self.codec } + } +} + +/// Signed URLs and the key stay out of logs. +impl std::fmt::Debug for Stream { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Stream") + .field("codec", &self.codec) + .field("bitrate_kbps", &self.bitrate_kbps) + .field("preview", &self.preview) + .field("mirrors", &self.urls.len()) + .field("encrypted", &self.key.is_some()) + .finish() + } +} + +/// Builds a stream from a `/get-file-info` reply, when it's a codec we decode well. +pub(crate) fn file_info_stream(info: yamuse::models::LosslessDownloadInfo) -> Option { + let urls = info.links(); + let playable = matches!(info.codec.as_deref(), Some("flac" | "flac-mp4" | "mp3" | "aac" | "aac-mp4")); + if !playable || urls.is_empty() { + return None; + } + Some(Stream { + urls, + codec: info.codec.unwrap_or_default(), + bitrate_kbps: info.bitrate.unwrap_or(0).max(0) as u32, + preview: false, + key: info.key.filter(|k| !k.is_empty()), + }) } /// Picks the variant to play: full tracks over previews, then the highest bitrate @@ -224,4 +267,24 @@ mod tests { assert!(best_variant(&previews, None).unwrap().preview.unwrap()); assert!(best_variant(&[], None).is_none()); } + + #[test] + fn file_info_replies_become_streams_except_he_aac() { + let info = |json: &str| serde_json::from_str::(json).unwrap(); + let flac = file_info_stream(info( + r#"{"codec":"flac-mp4","bitrate":0,"transport":"encraw","key":"00ff","urls":["https://a","https://b"]}"#, + )) + .unwrap(); + assert_eq!(flac.urls, ["https://a", "https://b"]); + assert_eq!((flac.label().as_str(), flac.container()), ("FLAC", "mp4")); + assert_eq!(flac.key.as_deref(), Some("00ff")); + assert!(!format!("{flac:?}").contains("00ff"), "key leaked into Debug"); + assert!(flac.lossless()); + let aac = file_info_stream(info(r#"{"codec":"aac-mp4","quality":"nq","bitrate":192,"key":"00ff","urls":["https://a"]}"#)).unwrap(); + assert_eq!((aac.label().as_str(), aac.container(), aac.lossless()), ("AAC 192", "mp4", false)); + assert!(file_info_stream(info(r#"{"codec":"he-aac-mp4","bitrate":64,"urls":["https://a"]}"#)).is_none()); + assert!(file_info_stream(info(r#"{"codec":"flac","urls":[]}"#)).is_none()); + let mp3 = Stream { urls: vec![], codec: "mp3".into(), bitrate_kbps: 320, preview: false, key: None }; + assert_eq!((mp3.label().as_str(), mp3.container()), ("MP3 320", "mp3")); + } }