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.
This commit is contained in:
Yuriy Panov committed 2026-10-15 18:33:04 +06:00
1 parent c85f5785fd
commit 24ff874d34
8 files changed
+175 -24

No files matched your search

+10
View File
@@ -48,6 +48,12 @@ Settings (streaming quality, appearance, volume) are kept in
`~/Library/Application Support/Musyx/settings.json`. Covers are cached under `~/Library/Application Support/Musyx/settings.json`. Covers are cached under
`~/Library/Caches/Musyx/covers` and can be cleared from Settings. `~/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 ## Check
```sh ```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 `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. 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 ## Licences
Musyx is MIT. Onest and Unbounded are OFL (`crates/musyx/assets/fonts`), and Lucide icons Musyx is MIT. Onest and Unbounded are OFL (`crates/musyx/assets/fonts`), and Lucide icons
+47
View File
@@ -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}");
}
+9 -6
View File
@@ -962,15 +962,15 @@ impl App {
Task::batch(effects.into_iter().map(|effect| match effect { Task::batch(effects.into_iter().map(|effect| match effect {
Effect::Fetch { generation, track_id } => { Effect::Fetch { generation, track_id } => {
let client = client.clone(); let client = client.clone();
let cap = self.settings.quality.max_kbps(); let quality = self.settings.quality;
Task::future(async move { fetch_audio(&client, &track_id, cap).await }) Task::future(async move { fetch_audio(&client, &track_id, quality).await })
.map(move |result| Message::Player(player::Msg::Fetched { generation, result })) .map(move |result| Message::Player(player::Msg::Fetched { generation, result }))
} }
Effect::Prefetch { track_id } => { Effect::Prefetch { track_id } => {
let client = client.clone(); let client = client.clone();
let cap = self.settings.quality.max_kbps(); let quality = self.settings.quality;
Task::future(async move { 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) (track_id, result)
}) })
.map(|(track_id, result)| Message::Player(player::Msg::Prefetched { 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<u32>) -> Result<Audio, Error> { async fn fetch_audio(client: &YmClient, track_id: &str, quality: Quality) -> Result<Audio, Error> {
let stream = client.stream(track_id, max_kbps).await?; 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?; let bytes = client.fetch(&stream).await?;
Ok(Audio { stream, bytes }) Ok(Audio { stream, bytes })
} }
+2 -2
View File
@@ -600,7 +600,7 @@ impl Player {
self.send(Command::Load { self.send(Command::Load {
generation: self.generation, generation: self.generation,
bytes: audio.bytes.clone(), bytes: audio.bytes.clone(),
hint: audio.stream.codec.clone(), hint: audio.stream.container().to_owned(),
play, play,
}); });
self.current = Some(audio); self.current = Some(audio);
@@ -644,7 +644,7 @@ mod tests {
fn audio() -> Audio { fn audio() -> 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]), bytes: Arc::from(vec![0u8; 4]),
} }
} }
+12 -4
View File
@@ -9,7 +9,9 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub enum Quality { 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] #[default]
Best, Best,
/// Up to 192 kbps. /// Up to 192 kbps.
@@ -19,10 +21,11 @@ pub enum Quality {
} }
impl 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 { pub fn label(self) -> &'static str {
match self { match self {
Quality::Lossless => "Lossless",
Quality::Best => "Best", Quality::Best => "Best",
Quality::Balanced => "Balanced", Quality::Balanced => "Balanced",
Quality::DataSaver => "Data saver", Quality::DataSaver => "Data saver",
@@ -31,7 +34,11 @@ impl Quality {
pub fn detail(self) -> &'static str { pub fn detail(self) -> &'static str {
match self { 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::Balanced => "Up to 192 kbps. About 40% less data.",
Quality::DataSaver => "Up to 128 kbps, for slow or metered connections.", 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. /// The bitrate cap passed to the API; `None` means no cap.
pub fn max_kbps(self) -> Option<u32> { pub fn max_kbps(self) -> Option<u32> {
match self { match self {
Quality::Best => None, Quality::Lossless | Quality::Best => None,
Quality::Balanced => Some(192), Quality::Balanced => Some(192),
Quality::DataSaver => Some(128), Quality::DataSaver => Some(128),
} }
@@ -117,6 +124,7 @@ mod tests {
let s = Settings { quality: Quality::DataSaver, appearance: Appearance::Dark, volume: 0.25 }; let s = Settings { quality: Quality::DataSaver, appearance: Appearance::Dark, volume: 0.25 };
let json = serde_json::to_string(&s).unwrap(); let json = serde_json::to_string(&s).unwrap();
assert!(json.contains("\"data-saver\""), "{json}"); assert!(json.contains("\"data-saver\""), "{json}");
assert_eq!(serde_json::to_string(&Quality::Lossless).unwrap(), "\"lossless\"");
assert_eq!(serde_json::from_str::<Settings>(&json).unwrap(), s); assert_eq!(serde_json::from_str::<Settings>(&json).unwrap(), s);
let partial: Settings = serde_json::from_str(r#"{"appearance":"light"}"#).unwrap(); let partial: Settings = serde_json::from_str(r#"{"appearance":"light"}"#).unwrap();
assert_eq!(partial, Settings { appearance: Appearance::Light, ..Settings::default() }); assert_eq!(partial, Settings { appearance: Appearance::Light, ..Settings::default() });
+2 -3
View File
@@ -181,9 +181,8 @@ fn extras<'a>(t: &'static Tokens, p: &'a Player, drawer: Option<Panel>) -> Eleme
let mut items = row![].spacing(4).align_y(Alignment::Center); let mut items = row![].spacing(4).align_y(Alignment::Center);
if let Some(stream) = p.stream() { if let Some(stream) = p.stream() {
let label = if stream.preview { "Preview".to_owned() } else { stream.label() }; let label = if stream.preview { "Preview".to_owned() } else { stream.label() };
items = items.push( let ink = if stream.lossless() { t.accent } else { t.muted };
container(line(label, theme::MEDIUM, 11.5, t.muted)).padding([2, 6]).style(theme::badge), 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 }; let volume = if p.muted { 0.0 } else { p.volume };
+25 -4
View File
@@ -7,7 +7,7 @@ use crate::library::{
}; };
use crate::edit::{LikedIds, Lyrics, QueueContext}; use crate::edit::{LikedIds, Lyrics, QueueContext};
use crate::discover::{ChartEntry, Genre, Home, SearchKind, SearchResults}; 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::wave::{PlayReport, Station, WaveBatch, WaveEvent, WaveSettings, iso_millis, numeric_id};
use crate::{Error, Result}; use crate::{Error, Result};
@@ -145,16 +145,37 @@ impl YmClient {
.ok_or_else(|| Error::Api(format!("no playable variant for track {track_id}")))?; .ok_or_else(|| Error::Api(format!("no playable variant for track {track_id}")))?;
let url = variant.direct_link(self.inner.transport()).await?; let url = variant.direct_link(self.inner.transport()).await?;
Ok(Stream { Ok(Stream {
url, urls: vec![url],
codec: variant.codec.clone().unwrap_or_default(), codec: variant.codec.clone().unwrap_or_default(),
bitrate_kbps: variant.bitrate_in_kbps.unwrap_or(0).max(0) as u32, bitrate_kbps: variant.bitrate_in_kbps.unwrap_or(0).max(0) as u32,
preview: variant.preview.unwrap_or(false), 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<Stream> {
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<Arc<[u8]>> { pub async fn fetch(&self, stream: &Stream) -> Result<Arc<[u8]>> {
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() .to_bytes()
.await?; .await?;
Ok(bytes.into()) Ok(bytes.into())
+68 -5
View File
@@ -122,21 +122,64 @@ pub(crate) fn hues_for(seed: u64) -> (f32, f32) {
} }
/// Where a track's audio comes from. /// Where a track's audio comes from.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Clone, PartialEq, Eq)]
pub struct Stream { pub struct Stream {
pub url: String, /// Mirrors of the same file; any one works.
/// "mp3" or "aac". pub urls: Vec<String>,
/// "mp3", "aac", "flac" or "flac-mp4".
pub codec: String, pub codec: String,
/// 0 when Yandex doesn't say (lossless).
pub bitrate_kbps: u32, pub bitrate_kbps: u32,
/// True when only a 30-second preview is available (no Plus). /// True when only a 30-second preview is available (no Plus).
pub preview: bool, pub preview: bool,
/// Hex AES-CTR key for an encrypted (`encraw`) lossless file.
pub key: Option<String>,
} }
impl Stream { impl Stream {
/// "MP3 320", shown in the player bar. /// "MP3 320" or "FLAC", shown in the player bar.
pub fn label(&self) -> String { 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<Stream> {
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 /// 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(&previews, None).unwrap().preview.unwrap());
assert!(best_variant(&[], None).is_none()); assert!(best_variant(&[], None).is_none());
} }
#[test]
fn file_info_replies_become_streams_except_he_aac() {
let info = |json: &str| serde_json::from_str::<yamuse::models::LosslessDownloadInfo>(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"));
}
} }