smooth out engine volume, fixing audio glitches

This commit is contained in:
yuni 2024-04-15 21:55:46 +02:00
parent 65f57cbccb
commit a1c4fbb821
3 changed files with 37 additions and 20 deletions

View file

@ -152,7 +152,7 @@ pub struct Engine {
pub reaction_wheels: f32,
pub engine_type: EngineType,
pub warmup_seconds: f32,
pub current_warmup: f32,
pub current_warmup: f32, // between 0.0 and 1.0
}
impl Default for Engine {
fn default() -> Self {

View file

@ -79,6 +79,7 @@ pub fn setup(
source: asset_server.load(ASSET_THRUSTER),
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::new(0.0),
paused: true,
..default()
},
@ -90,6 +91,7 @@ pub fn setup(
source: asset_server.load(ASSET_ROCKET),
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::new(0.0),
paused: true,
..default()
},
@ -101,6 +103,7 @@ pub fn setup(
source: asset_server.load(ASSET_ION),
settings: PlaybackSettings {
mode: PlaybackMode::Loop,
volume: Volume::new(0.0),
paused: true,
..default()
},

View file

@ -350,25 +350,39 @@ pub fn apply_input_to_player(
}
}
}
if let Ok(sink) = thruster_sound_controller.get_single() {
if play_thruster_sound && !settings.mute_sfx && engine.engine_type == actor::EngineType::Monopropellant {
sink.play()
let sinks = vec![
(1.2, actor::EngineType::Monopropellant, thruster_sound_controller.get_single()),
(1.0, actor::EngineType::Rocket, rocket_sound_controller.get_single()),
(1.4, actor::EngineType::Ion, ion_sound_controller.get_single()),
];
let seconds_to_max_vol = 0.05;
let seconds_to_min_vol = 0.05;
for sink_data in sinks {
if let (vol_boost, engine_type, Ok(sink)) = sink_data {
if settings.mute_sfx {
sink.pause();
}
else {
let volume = sink.volume();
if engine.engine_type == engine_type {
if play_thruster_sound {
sink.play();
if volume < 1.0 {
let maxvol = vol_boost;
//let maxvol = engine.current_warmup * vol_boost;
sink.set_volume((volume + dt / seconds_to_max_vol).clamp(0.0, maxvol));
}
} else {
sink.pause()
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, 1.0));
}
}
if let Ok(sink) = rocket_sound_controller.get_single() {
if play_thruster_sound && !settings.mute_sfx && engine.engine_type == actor::EngineType::Rocket {
sink.play()
} else {
sink.pause()
else if volume > 0.0 {
sink.set_volume((volume - dt / seconds_to_min_vol).clamp(0.0, 1.0));
}
if volume < 0.0001 {
sink.pause();
}
}
if let Ok(sink) = ion_sound_controller.get_single() {
if play_thruster_sound && !settings.mute_sfx && engine.engine_type == actor::EngineType::Ion {
sink.play()
} else {
sink.pause()
}
}
}