Skip to content

Commit 4865c53

Browse files
authored
SpatialListener now requires a Transform (#19357)
I noticed that the `SpatialListener` asks to have a `Transform` attached. It seemed weird that we didnt just use a require macro, so i went ahead and did that I also tweaked the system that plays audio to use a `&GlobalTransform` instead of an `Option<&GlobalTransform>`
1 parent 3ad7a75 commit 4865c53

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

crates/bevy_audio/src/audio.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ use bevy_asset::{Asset, Handle};
33
use bevy_ecs::prelude::*;
44
use bevy_math::Vec3;
55
use bevy_reflect::prelude::*;
6+
use bevy_transform::components::Transform;
67

78
/// The way Bevy manages the sound playback.
89
#[derive(Debug, Clone, Copy, Reflect)]
910
#[reflect(Clone)]
1011
pub enum PlaybackMode {
1112
/// Play the sound once. Do nothing when it ends.
1213
///
13-
/// Note: It is not possible to reuse an `AudioPlayer` after it has finished playing and
14-
/// the underlying `AudioSink` or `SpatialAudioSink` has been drained.
14+
/// Note: It is not possible to reuse an [`AudioPlayer`] after it has finished playing and
15+
/// the underlying [`AudioSink`](crate::AudioSink) or [`SpatialAudioSink`](crate::SpatialAudioSink) has been drained.
1516
///
16-
/// To replay a sound, the audio components provided by `AudioPlayer` must be removed and
17+
/// To replay a sound, the audio components provided by [`AudioPlayer`] must be removed and
1718
/// added again.
1819
Once,
1920
/// Repeat the sound forever.
@@ -27,7 +28,7 @@ pub enum PlaybackMode {
2728
/// Initial settings to be used when audio starts playing.
2829
///
2930
/// If you would like to control the audio while it is playing, query for the
30-
/// [`AudioSink`][crate::AudioSink] or [`SpatialAudioSink`][crate::SpatialAudioSink]
31+
/// [`AudioSink`](crate::AudioSink) or [`SpatialAudioSink`](crate::SpatialAudioSink)
3132
/// components. Changes to this component will *not* be applied to already-playing audio.
3233
#[derive(Component, Clone, Copy, Debug, Reflect)]
3334
#[reflect(Clone, Default, Component, Debug)]
@@ -78,10 +79,10 @@ impl Default for PlaybackSettings {
7879
impl PlaybackSettings {
7980
/// Will play the associated audio source once.
8081
///
81-
/// Note: It is not possible to reuse an `AudioPlayer` after it has finished playing and
82-
/// the underlying `AudioSink` or `SpatialAudioSink` has been drained.
82+
/// Note: It is not possible to reuse an [`AudioPlayer`] after it has finished playing and
83+
/// the underlying [`AudioSink`](crate::AudioSink) or [`SpatialAudioSink`](crate::SpatialAudioSink) has been drained.
8384
///
84-
/// To replay a sound, the audio components provided by `AudioPlayer` must be removed and
85+
/// To replay a sound, the audio components provided by [`AudioPlayer`] must be removed and
8586
/// added again.
8687
pub const ONCE: PlaybackSettings = PlaybackSettings {
8788
mode: PlaybackMode::Once,
@@ -164,14 +165,15 @@ impl PlaybackSettings {
164165

165166
/// Settings for the listener for spatial audio sources.
166167
///
167-
/// This must be accompanied by `Transform` and `GlobalTransform`.
168-
/// Only one entity with a `SpatialListener` should be present at any given time.
168+
/// This is accompanied by [`Transform`] and [`GlobalTransform`](bevy_transform::prelude::GlobalTransform).
169+
/// Only one entity with a [`SpatialListener`] should be present at any given time.
169170
#[derive(Component, Clone, Debug, Reflect)]
171+
#[require(Transform)]
170172
#[reflect(Clone, Default, Component, Debug)]
171173
pub struct SpatialListener {
172-
/// Left ear position relative to the `GlobalTransform`.
174+
/// Left ear position relative to the [`GlobalTransform`](bevy_transform::prelude::GlobalTransform).
173175
pub left_ear_offset: Vec3,
174-
/// Right ear position relative to the `GlobalTransform`.
176+
/// Right ear position relative to the [`GlobalTransform`](bevy_transform::prelude::GlobalTransform).
175177
pub right_ear_offset: Vec3,
176178
}
177179

@@ -182,7 +184,7 @@ impl Default for SpatialListener {
182184
}
183185

184186
impl SpatialListener {
185-
/// Creates a new `SpatialListener` component.
187+
/// Creates a new [`SpatialListener`] component.
186188
///
187189
/// `gap` is the distance between the left and right "ears" of the listener. Ears are
188190
/// positioned on the x axis.
@@ -203,12 +205,12 @@ impl SpatialListener {
203205
pub struct SpatialScale(pub Vec3);
204206

205207
impl SpatialScale {
206-
/// Create a new `SpatialScale` with the same value for all 3 dimensions.
208+
/// Create a new [`SpatialScale`] with the same value for all 3 dimensions.
207209
pub const fn new(scale: f32) -> Self {
208210
Self(Vec3::splat(scale))
209211
}
210212

211-
/// Create a new `SpatialScale` with the same value for `x` and `y`, and `0.0`
213+
/// Create a new [`SpatialScale`] with the same value for `x` and `y`, and `0.0`
212214
/// for `z`.
213215
pub const fn new_2d(scale: f32) -> Self {
214216
Self(Vec3::new(scale, scale, 0.0))
@@ -238,11 +240,11 @@ pub struct DefaultSpatialScale(pub SpatialScale);
238240
/// If the handle refers to an unavailable asset (such as if it has not finished loading yet),
239241
/// the audio will not begin playing immediately. The audio will play when the asset is ready.
240242
///
241-
/// When Bevy begins the audio playback, an [`AudioSink`][crate::AudioSink] component will be
243+
/// When Bevy begins the audio playback, an [`AudioSink`](crate::AudioSink) component will be
242244
/// added to the entity. You can use that component to control the audio settings during playback.
243245
///
244246
/// Playback can be configured using the [`PlaybackSettings`] component. Note that changes to the
245-
/// `PlaybackSettings` component will *not* affect already-playing audio.
247+
/// [`PlaybackSettings`] component will *not* affect already-playing audio.
246248
#[derive(Component, Reflect)]
247249
#[reflect(Component, Clone)]
248250
#[require(PlaybackSettings)]

crates/bevy_audio/src/audio_output.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
103103
Entity,
104104
&AudioPlayer<Source>,
105105
&PlaybackSettings,
106-
Option<&GlobalTransform>,
106+
&GlobalTransform,
107107
),
108108
(Without<AudioSink>, Without<SpatialAudioSink>),
109109
>,
@@ -118,7 +118,7 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
118118
return;
119119
};
120120

121-
for (entity, source_handle, settings, maybe_emitter_transform) in &query_nonplaying {
121+
for (entity, source_handle, settings, emitter_transform) in &query_nonplaying {
122122
let Some(audio_source) = audio_sources.get(&source_handle.0) else {
123123
continue;
124124
};
@@ -136,14 +136,7 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
136136
}
137137

138138
let scale = settings.spatial_scale.unwrap_or(default_spatial_scale.0).0;
139-
140-
let emitter_translation = if let Some(emitter_transform) = maybe_emitter_transform {
141-
(emitter_transform.translation() * scale).into()
142-
} else {
143-
warn!("Spatial AudioPlayer with no GlobalTransform component. Using zero.");
144-
Vec3::ZERO.into()
145-
};
146-
139+
let emitter_translation = (emitter_transform.translation() * scale).into();
147140
let sink = match SpatialSink::try_new(
148141
stream_handle,
149142
emitter_translation,

0 commit comments

Comments
 (0)