Skip to content

Commit

Permalink
Fade music in & out (yes you can spam the button to abuse it)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCOfficer committed Nov 14, 2023
1 parent d300a71 commit 2e15902
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ fn play(rx: &Receiver<MusicCommand>, 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)
}
}
}
Expand All @@ -69,3 +69,21 @@ fn play(rx: &Receiver<MusicCommand>, initial_state: MusicState) -> Result<()> {
thread::sleep(Duration::from_millis(100));
}
}

fn fade(sink: &Sink, out: bool) {
let mut range: Vec<i32> = (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()
}
}

0 comments on commit 2e15902

Please sign in to comment.