From 2e15902c8f9862af3cd780e1805a974d224d5ac7 Mon Sep 17 00:00:00 2001 From: MCOfficer Date: Tue, 14 Nov 2023 15:06:40 +0100 Subject: [PATCH] Fade music in & out (yes you can spam the button to abuse it) --- src/music.rs | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/music.rs b/src/music.rs index 78ced7b..7b734d0 100644 --- a/src/music.rs +++ b/src/music.rs @@ -46,16 +46,16 @@ fn play(rx: &Receiver, initial_state: MusicState) -> Result<()> { match cmd { MusicCommand::Pause => { state = MusicState::Paused; - sink.pause() + fade(&sink, true) } MusicCommand::Play => { state = MusicState::Playing; - sink.play() + fade(&sink, false) } - MusicCommand::WeakPause => sink.pause(), + MusicCommand::WeakPause => fade(&sink, true), MusicCommand::WeakPlay => { if let MusicState::Playing = state { - sink.play() + fade(&sink, false) } } } @@ -69,3 +69,21 @@ fn play(rx: &Receiver, initial_state: MusicState) -> Result<()> { thread::sleep(Duration::from_millis(100)); } } + +fn fade(sink: &Sink, out: bool) { + let mut range: Vec = (1..20).collect(); + if out { + range.reverse(); + } else { + sink.play(); + } + + for i in range { + sink.set_volume(i as f32 / 20.); + thread::sleep(Duration::from_millis(20)); + } + + if out { + sink.pause() + } +}