From d04766160d02a49cbe45e075656be615be869cde Mon Sep 17 00:00:00 2001 From: Henrik Friedrichsen Date: Thu, 14 May 2020 22:11:35 +0200 Subject: [PATCH] implement audio_cache setting fixes #196 --- README.md | 2 ++ src/config.rs | 1 + src/spotify.rs | 32 +++++++++++++++----------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 2c47e84..5d91cbf 100644 --- a/README.md +++ b/README.md @@ -122,6 +122,8 @@ values are: * `use_nerdfont`: Turn nerdfont glyphs on/off * `theme`: Set a custom color palette (see below) +* `audio_cache`: Enable or disable caching of audio files, on by default + * `volnorm`: Enable or disable volume normalization, off by default * `volnorm_pregain`: Normalization pregain to apply (if enabled) diff --git a/src/config.rs b/src/config.rs index c5c341f..28cb627 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub struct Config { pub theme: Option, pub use_nerdfont: Option, pub saved_state: Option, + pub audio_cache: Option, pub volnorm: Option, pub volnorm_pregain: Option, } diff --git a/src/spotify.rs b/src/spotify.rs index 0e13673..d70634f 100644 --- a/src/spotify.rs +++ b/src/spotify.rs @@ -223,11 +223,6 @@ impl futures::Future for Worker { impl Spotify { pub fn new(events: EventManager, credentials: Credentials, cfg: &config::Config) -> Spotify { - let player_config = PlayerConfig { - bitrate: Bitrate::Bitrate320, - normalisation: cfg.volnorm.unwrap_or(false), - normalisation_pregain: cfg.volnorm_pregain.unwrap_or(0.0), - }; let (user_tx, user_rx) = oneshot::channel(); let volume = match &cfg.saved_state { Some(state) => match state.volume { @@ -257,15 +252,9 @@ impl Spotify { let (tx, rx) = mpsc::unbounded(); { + let cfg = cfg.clone(); thread::spawn(move || { - Self::worker( - events, - Box::pin(rx), - player_config, - credentials, - user_tx, - volume, - ) + Self::worker(events, Box::pin(rx), cfg, credentials, user_tx, volume) }); } @@ -314,9 +303,12 @@ impl Spotify { th.join().is_ok() } - fn create_session(core: &mut Core, credentials: Credentials) -> Session { + fn create_session(core: &mut Core, cfg: &config::Config, credentials: Credentials) -> Session { let session_config = Self::session_config(); - let cache = Cache::new(config::cache_path("librespot"), true); + let cache = Cache::new( + config::cache_path("librespot"), + cfg.audio_cache.unwrap_or(true), + ); let handle = core.handle(); debug!("opening spotify session"); core.run(Session::connect( @@ -358,14 +350,20 @@ impl Spotify { fn worker( events: EventManager, commands: Pin>>, - player_config: PlayerConfig, + cfg: config::Config, credentials: Credentials, user_tx: oneshot::Sender, volume: u16, ) { + let player_config = PlayerConfig { + bitrate: Bitrate::Bitrate320, + normalisation: cfg.volnorm.unwrap_or(false), + normalisation_pregain: cfg.volnorm_pregain.unwrap_or(0.0), + }; + let mut core = Core::new().unwrap(); - let session = Self::create_session(&mut core, credentials); + let session = Self::create_session(&mut core, &cfg, credentials); user_tx .send(session.username()) .expect("could not pass username back to Spotify::new");