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() + } +}