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 reaction_wheels: f32,
pub engine_type: EngineType, pub engine_type: EngineType,
pub warmup_seconds: f32, pub warmup_seconds: f32,
pub current_warmup: f32, pub current_warmup: f32, // between 0.0 and 1.0
} }
impl Default for Engine { impl Default for Engine {
fn default() -> Self { fn default() -> Self {

View file

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

View file

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