mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Non-Intrusive refactor of play_queued_audio_system()
(#10910)
# Objective - Improve readability. - Somewhat relates to #10896. ## Solution - Use early returns to minimize nesting. - Change `emitter_translation` to use `if let` instead of `map`.
This commit is contained in:
parent
24060213b5
commit
1523e8c409
1 changed files with 111 additions and 108 deletions
|
@ -122,116 +122,119 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
|
|||
};
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
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()
|
||||
});
|
||||
|
||||
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:?}");
|
||||
}
|
||||
}
|
||||
} 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:?}");
|
||||
}
|
||||
}
|
||||
// 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),
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
};
|
||||
} 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));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue