diff --git a/crates/horizon-client/src/audio.rs b/crates/horizon-client/src/audio.rs index 773bf064d..5d99c4858 100644 --- a/crates/horizon-client/src/audio.rs +++ b/crates/horizon-client/src/audio.rs @@ -552,3 +552,47 @@ impl Sounds for Audio { self.bound().update_category_volume(source, gain); } } + +#[cfg(test)] +mod tests { + use super::*; + + fn channel(samples: Vec, linear: Option, at: [f32; 3], looping: bool) -> Channel { + Channel { + samples: Some((Arc::new(samples), 48_000)), + position: 0.0, + state: State::Playing, + gain: 1.0, + pitch: 1.0, + linear, + looping, + at, + relative: false, + } + } + + #[test] + fn linear_attenuation_reaches_zero_at_the_max_distance() { + assert_eq!(distance_gain(None, 100.0), 1.0); + assert_eq!(distance_gain(Some(16.0), 0.0), 1.0); + assert_eq!(distance_gain(Some(16.0), 8.0), 0.5); + assert_eq!(distance_gain(Some(16.0), 16.0), 0.0); + assert_eq!(distance_gain(Some(16.0), 40.0), 0.0); + } + + #[test] + fn sources_play_attenuated_then_stop_or_loop() { + let mut m = Mixer::default(); + m.channels + .insert(1, channel(vec![1.0; 4], Some(16.0), [8.0, 0.0, 0.0], false)); + m.channels + .insert(2, channel(vec![1.0; 4], None, [0.0; 3], true)); + let mut data = [0.0f32; 16]; + mix(&mut m, &mut data, 2, 48_000); + assert_eq!(m.channels[&1].state, State::Stopped); + assert_eq!(m.channels[&2].state, State::Playing); + // Mono sum of the two at the first frame: the far one is half as loud. + let first = data[0] + data[1]; + assert!(first > 1.4 && first < 3.1, "{first}"); + } +} diff --git a/crates/horizon-client/src/game/recipe_book.rs b/crates/horizon-client/src/game/recipe_book.rs index 60fc4ec50..95470203d 100644 --- a/crates/horizon-client/src/game/recipe_book.rs +++ b/crates/horizon-client/src/game/recipe_book.rs @@ -490,7 +490,7 @@ impl Game<'_> { if !open { self.recipe_ui.overlay = None; } - self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0); + self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0); return true; } if !self.book_open(book_type) || self.spectator() { @@ -518,12 +518,12 @@ impl Game<'_> { if self.recipe_ui.page + 1 < pages && inside(cursor, xo + 93.0, yo + 137.0, 12.0, 17.0) { self.recipe_ui.page += 1; - self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0); + self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0); return true; } if self.recipe_ui.page > 0 && inside(cursor, xo + 38.0, yo + 137.0, 12.0, 17.0) { self.recipe_ui.page -= 1; - self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0); + self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0); return true; } } @@ -570,7 +570,7 @@ impl Game<'_> { let filtering = !self.book_filtering(book_type); self.level.recipe_book.filtering[book_type.index()] = filtering; self.recipe_ui.page = 0; - self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0); + self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0); return true; } // Tabs. @@ -581,7 +581,7 @@ impl Game<'_> { self.recipe_ui.tab = Some(tab); self.recipe_ui.page = 0; } - self.audio.play("minecraft:ui.button.click", None, 1.0, 1.0); + self.audio.play("minecraft:ui.button.click", None, 0.25, 1.0); return true; } } diff --git a/crates/horizon-client/src/sound_events.rs b/crates/horizon-client/src/sound_events.rs index 6a5e179c2..4f1af3670 100644 --- a/crates/horizon-client/src/sound_events.rs +++ b/crates/horizon-client/src/sound_events.rs @@ -20,7 +20,9 @@ pub enum Sampled { Product(Vec), } impl Sampled { - /// `SampledFloat.sample`: constants draw nothing. + /// `SampledFloat.sample`: constants draw nothing (sounds.json only has + /// constants, but the random stays part of the call as in vanilla). + #[allow(clippy::only_used_in_recursion)] pub fn sample(&self, random: &mut impl RandomSource) -> f32 { match self { Self::Constant(v) => *v,