muting audio
This commit is contained in:
parent
a19a9e9cc3
commit
ecbb7c6756
@ -12,34 +12,59 @@ fn source_from_path<P: AsRef<std::path::Path>>(path: P) -> Decoder<BufReader<Fil
|
|||||||
|
|
||||||
fn play_audio<P: AsRef<std::path::Path>>(
|
fn play_audio<P: AsRef<std::path::Path>>(
|
||||||
stream_handle: &OutputStreamHandle,
|
stream_handle: &OutputStreamHandle,
|
||||||
sink: &mut Sink,
|
sink: &mut Option<Sink>,
|
||||||
path: P,
|
path: P,
|
||||||
volume: f32,
|
volume: f32,
|
||||||
) {
|
) {
|
||||||
let source = source_from_path(path);
|
let source = source_from_path(path);
|
||||||
*sink = Sink::try_new(&stream_handle).unwrap();
|
*sink = Sink::try_new(&stream_handle).ok();
|
||||||
sink.set_volume(volume);
|
if let Some(sink) = sink {
|
||||||
sink.append(source);
|
sink.set_volume(volume);
|
||||||
|
sink.append(source);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn audio_thread() -> mpsc::Sender<SoundEffect> {
|
pub enum Command {
|
||||||
let (sender, receiver) = mpsc::channel();
|
ToggleMuted,
|
||||||
|
PlayEffect(SoundEffect),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn audio_thread() -> mpsc::Sender<Command> {
|
||||||
|
let (sender, receiver) = mpsc::channel::<Command>();
|
||||||
|
|
||||||
let _ = std::thread::spawn(move || {
|
let _ = std::thread::spawn(move || {
|
||||||
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
|
let (_stream, stream_handle) = OutputStream::try_default().unwrap();
|
||||||
let music_sink = Sink::try_new(&stream_handle).unwrap();
|
let music_sink = Sink::try_new(&stream_handle).unwrap();
|
||||||
let mut hard_drop_sink = Sink::try_new(&stream_handle).unwrap();
|
let mut hard_drop_sink = None;
|
||||||
let mut line_clear_sink = Sink::try_new(&stream_handle).unwrap();
|
let mut line_clear_sink = None;
|
||||||
let mut move_sink = Sink::try_new(&stream_handle).unwrap();
|
let mut move_sink = None;
|
||||||
let mut rotation_sink = Sink::try_new(&stream_handle).unwrap();
|
let mut rotation_sink = None;
|
||||||
|
let mut muted = false;
|
||||||
|
|
||||||
music_sink.append(source_from_path("resources/music.ogg").repeat_infinite());
|
music_sink.append(source_from_path("resources/music.ogg").repeat_infinite());
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let Ok(effect) = receiver.recv() else {
|
let Ok(cmd) = receiver.recv() else {
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let effect = match cmd {
|
||||||
|
Command::ToggleMuted => {
|
||||||
|
muted = !muted;
|
||||||
|
if muted {
|
||||||
|
music_sink.pause();
|
||||||
|
} else {
|
||||||
|
music_sink.play();
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
Command::PlayEffect(effect) => effect,
|
||||||
|
};
|
||||||
|
|
||||||
|
if muted {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
let base_volume = 0.5;
|
let base_volume = 0.5;
|
||||||
match effect {
|
match effect {
|
||||||
SoundEffect::HardDrop => play_audio(
|
SoundEffect::HardDrop => play_audio(
|
||||||
|
@ -191,6 +191,11 @@ pub fn start_game() -> Result<(), String> {
|
|||||||
game = Game::new();
|
game = Game::new();
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
Keycode::M => {
|
||||||
|
audio_thread.send(audio::Command::ToggleMuted).unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
Keycode::P => {
|
Keycode::P => {
|
||||||
paused = !paused;
|
paused = !paused;
|
||||||
continue;
|
continue;
|
||||||
@ -242,9 +247,11 @@ pub fn start_game() -> Result<(), String> {
|
|||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
let effects = game.step(&actions);
|
let effects = game.step(&actions);
|
||||||
effects
|
effects.into_iter().for_each(|effect| {
|
||||||
.into_iter()
|
audio_thread
|
||||||
.for_each(|effect| audio_thread.send(effect).unwrap());
|
.send(audio::Command::PlayEffect(effect))
|
||||||
|
.unwrap()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
canvas.present();
|
canvas.present();
|
||||||
|
Loading…
Reference in New Issue
Block a user