From 0a65d16d440d7bd75abfb5ab0fc4a7e293c57c52 Mon Sep 17 00:00:00 2001 From: Nolan Darilek Date: Tue, 13 Jul 2021 10:40:50 -0500 Subject: [PATCH] More aggressively clean up sounds when possible. --- src/lib.rs | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b429b5f..47d9562 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -252,9 +252,11 @@ fn source_update( max_distance, rolloff_factor, bypass_global_effects, + state, .. } = *sound; - match &sound.state { + let mut clear = false; + match state { SoundState::Stopped => { if let Some(source) = sound.source.as_mut() { let mut source = source.lock().unwrap(); @@ -265,21 +267,25 @@ fn source_update( SoundState::Playing => { if let Some(source) = sound.source.as_mut() { let mut source = source.lock().unwrap(); - sync_source_and_components( - &mut source, - transform, - global_transform, - gain, - pitch, - looping, - reference_distance, - max_distance, - rolloff_factor, - bypass_global_effects, - &mut **global_effects, - ); - if ![SourceState::Playing, SourceState::Stopped].contains(&source.state()) { - source.play(); + if !vec![SourceState::Playing, SourceState::Paused].contains(&source.state()) { + clear = true; + } else { + sync_source_and_components( + &mut source, + transform, + global_transform, + gain, + pitch, + looping, + reference_distance, + max_distance, + rolloff_factor, + bypass_global_effects, + &mut **global_effects, + ); + if source.state() != SourceState::Playing { + source.play(); + } } } else { if let Ok(mut source) = context.new_static_source() { @@ -348,6 +354,10 @@ fn source_update( } } } + if clear { + sound.source = None; + sound.state = SoundState::Stopped; + } if let Some(source) = sound.source.clone() { let source = source.lock().unwrap(); sound.state = match &source.state() {