diff --git a/crates/bevy_audio/src/audio_output.rs b/crates/bevy_audio/src/audio_output.rs index 7f7e4c581b518..ecbf805bc1c34 100644 --- a/crates/bevy_audio/src/audio_output.rs +++ b/crates/bevy_audio/src/audio_output.rs @@ -122,116 +122,119 @@ pub(crate) fn play_queued_audio_system( }; for (entity, source_handle, settings, maybe_emitter_transform) in &query_nonplaying { - if let Some(audio_source) = audio_sources.get(source_handle) { - // audio data is available (has loaded), begin playback and insert sink component - if settings.spatial { - let (left_ear, right_ear) = ear_positions.get(); + let Some(audio_source) = audio_sources.get(source_handle) else { + continue; + }; + // audio data is available (has loaded), begin playback and insert sink component + if settings.spatial { + let (left_ear, right_ear) = ear_positions.get(); - // We can only use one `SpatialListener`. If there are more than that, then - // the user may have made a mistake. - if ear_positions.multiple_listeners() { - warn!( - "Multiple SpatialListeners found. Using {:?}.", - ear_positions.query.iter().next().unwrap().0 - ); + // We can only use one `SpatialListener`. If there are more than that, then + // the user may have made a mistake. + if ear_positions.multiple_listeners() { + warn!( + "Multiple SpatialListeners found. Using {:?}.", + ear_positions.query.iter().next().unwrap().0 + ); + } + + let emitter_translation = if let Some(emitter_transform) = maybe_emitter_transform { + (emitter_transform.translation() * ear_positions.scale.0).into() + } else { + warn!("Spatial AudioBundle with no GlobalTransform component. Using zero."); + Vec3::ZERO.into() + }; + + let sink = match SpatialSink::try_new( + stream_handle, + emitter_translation, + left_ear.into(), + right_ear.into(), + ) { + Ok(sink) => sink, + Err(err) => { + warn!("Error creating spatial sink: {err:?}"); + continue; } + }; + + sink.set_speed(settings.speed); + match settings.volume { + Volume::Relative(vol) => sink.set_volume(vol.0 * global_volume.volume.0), + Volume::Absolute(vol) => sink.set_volume(vol.0), + } - let emitter_translation = maybe_emitter_transform - .map(|t| (t.translation() * ear_positions.scale.0).into()) - .unwrap_or_else(|| { - warn!("Spatial AudioBundle with no GlobalTransform component. Using zero."); - Vec3::ZERO.into() - }); + if settings.paused { + sink.pause(); + } - match SpatialSink::try_new( - stream_handle, - emitter_translation, - left_ear.into(), - right_ear.into(), - ) { - Ok(sink) => { - sink.set_speed(settings.speed); - match settings.volume { - Volume::Relative(vol) => { - sink.set_volume(vol.0 * global_volume.volume.0); - } - Volume::Absolute(vol) => sink.set_volume(vol.0), - } - if settings.paused { - sink.pause(); - } - match settings.mode { - PlaybackMode::Loop => { - sink.append(audio_source.decoder().repeat_infinite()); - commands.entity(entity).insert(SpatialAudioSink { sink }); - } - PlaybackMode::Once => { - sink.append(audio_source.decoder()); - commands.entity(entity).insert(SpatialAudioSink { sink }); - } - PlaybackMode::Despawn => { - sink.append(audio_source.decoder()); - commands - .entity(entity) - // PERF: insert as bundle to reduce archetype moves - .insert((SpatialAudioSink { sink }, PlaybackDespawnMarker)); - } - PlaybackMode::Remove => { - sink.append(audio_source.decoder()); - commands - .entity(entity) - // PERF: insert as bundle to reduce archetype moves - .insert((SpatialAudioSink { sink }, PlaybackRemoveMarker)); - } - }; - } - Err(err) => { - warn!("Error playing spatial sound: {err:?}"); - } + match settings.mode { + PlaybackMode::Loop => { + sink.append(audio_source.decoder().repeat_infinite()); + commands.entity(entity).insert(SpatialAudioSink { sink }); } - } else { - match Sink::try_new(stream_handle) { - Ok(sink) => { - sink.set_speed(settings.speed); - match settings.volume { - Volume::Relative(vol) => { - sink.set_volume(vol.0 * global_volume.volume.0); - } - Volume::Absolute(vol) => sink.set_volume(vol.0), - } - if settings.paused { - sink.pause(); - } - match settings.mode { - PlaybackMode::Loop => { - sink.append(audio_source.decoder().repeat_infinite()); - commands.entity(entity).insert(AudioSink { sink }); - } - PlaybackMode::Once => { - sink.append(audio_source.decoder()); - commands.entity(entity).insert(AudioSink { sink }); - } - PlaybackMode::Despawn => { - sink.append(audio_source.decoder()); - commands - .entity(entity) - // PERF: insert as bundle to reduce archetype moves - .insert((AudioSink { sink }, PlaybackDespawnMarker)); - } - PlaybackMode::Remove => { - sink.append(audio_source.decoder()); - commands - .entity(entity) - // PERF: insert as bundle to reduce archetype moves - .insert((AudioSink { sink }, PlaybackRemoveMarker)); - } - }; - } - Err(err) => { - warn!("Error playing sound: {err:?}"); - } + PlaybackMode::Once => { + sink.append(audio_source.decoder()); + commands.entity(entity).insert(SpatialAudioSink { sink }); + } + PlaybackMode::Despawn => { + sink.append(audio_source.decoder()); + commands + .entity(entity) + // PERF: insert as bundle to reduce archetype moves + .insert((SpatialAudioSink { sink }, PlaybackDespawnMarker)); + } + PlaybackMode::Remove => { + sink.append(audio_source.decoder()); + commands + .entity(entity) + // PERF: insert as bundle to reduce archetype moves + .insert((SpatialAudioSink { sink }, PlaybackRemoveMarker)); } + }; + } else { + let sink = match Sink::try_new(stream_handle) { + Ok(sink) => sink, + Err(err) => { + warn!("Error creating sink: {err:?}"); + continue; + } + }; + + sink.set_speed(settings.speed); + match settings.volume { + Volume::Relative(vol) => sink.set_volume(vol.0 * global_volume.volume.0), + Volume::Absolute(vol) => sink.set_volume(vol.0), + } + + if settings.paused { + sink.pause(); } + + match settings.mode { + PlaybackMode::Loop => { + sink.append(audio_source.decoder().repeat_infinite()); + commands.entity(entity).insert(AudioSink { sink }); + } + PlaybackMode::Once => { + sink.append(audio_source.decoder()); + commands.entity(entity).insert(AudioSink { sink }); + } + PlaybackMode::Despawn => { + sink.append(audio_source.decoder()); + commands + .entity(entity) + // PERF: insert as bundle to reduce archetype moves + .insert((AudioSink { sink }, PlaybackDespawnMarker)); + } + PlaybackMode::Remove => { + sink.append(audio_source.decoder()); + commands + .entity(entity) + // PERF: insert as bundle to reduce archetype moves + .insert((AudioSink { sink }, PlaybackRemoveMarker)); + } + }; } } }