//! 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, ym_api::ContentLanguage::English).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}"); }