Skip to content

Commit c29a972

Browse files
Remove the ability to ignore global volume (#11092)
# Objective The ability to ignore the global volume doesn't seem desirable and complicates the API. #7706 added global volume and the ability to ignore it, but there was no further discussion about whether that's useful. Feel free to discuss here :) ## Solution Replace the `Volume` type's functionality with the `VolumeLevel`. Remove `VolumeLevel`. I also removed `DerefMut` derive that effectively made the volume `pub` and actually ensured that the volume isn't set below `0` even in release builds. ## Migration Guide The option to ignore the global volume using `Volume::Absolute` has been removed and `Volume` now stores the volume level directly, removing the need for the `VolumeLevel` struct.
1 parent 8c6d9b8 commit c29a972

File tree

3 files changed

+19
-61
lines changed

3 files changed

+19
-61
lines changed

crates/bevy_audio/src/audio.rs

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,33 @@
11
use crate::{AudioSource, Decodable};
22
use bevy_asset::{Asset, Handle};
3-
use bevy_derive::{Deref, DerefMut};
3+
use bevy_derive::Deref;
44
use bevy_ecs::prelude::*;
55
use bevy_math::Vec3;
66
use bevy_reflect::prelude::*;
77

8-
/// Defines the volume to play an audio source at.
9-
#[derive(Clone, Copy, Debug, Reflect)]
10-
pub enum Volume {
11-
/// A volume level relative to the global volume.
12-
Relative(VolumeLevel),
13-
/// A volume level that ignores the global volume.
14-
Absolute(VolumeLevel),
15-
}
16-
17-
impl Default for Volume {
18-
fn default() -> Self {
19-
Self::Relative(VolumeLevel::default())
20-
}
21-
}
22-
23-
impl Volume {
24-
/// Create a new volume level relative to the global volume.
25-
pub fn new_relative(volume: f32) -> Self {
26-
Self::Relative(VolumeLevel::new(volume))
27-
}
28-
/// Create a new volume level that ignores the global volume.
29-
pub fn new_absolute(volume: f32) -> Self {
30-
Self::Absolute(VolumeLevel::new(volume))
31-
}
32-
}
33-
348
/// A volume level equivalent to a non-negative float.
35-
#[derive(Clone, Copy, Deref, DerefMut, Debug, Reflect)]
36-
pub struct VolumeLevel(pub(crate) f32);
9+
#[derive(Clone, Copy, Deref, Debug, Reflect)]
10+
pub struct Volume(pub(crate) f32);
3711

38-
impl Default for VolumeLevel {
12+
impl Default for Volume {
3913
fn default() -> Self {
4014
Self(1.0)
4115
}
4216
}
4317

44-
impl VolumeLevel {
18+
impl Volume {
4519
/// Create a new volume level.
4620
pub fn new(volume: f32) -> Self {
4721
debug_assert!(volume >= 0.0);
48-
Self(volume)
22+
Self(f32::max(volume, 0.))
4923
}
5024
/// Get the value of the volume level.
5125
pub fn get(&self) -> f32 {
5226
self.0
5327
}
5428

5529
/// Zero (silent) volume level
56-
pub const ZERO: Self = VolumeLevel(0.0);
30+
pub const ZERO: Self = Volume(0.0);
5731
}
5832

5933
/// The way Bevy manages the sound playback.
@@ -106,7 +80,7 @@ impl PlaybackSettings {
10680
/// Will play the associated audio source once.
10781
pub const ONCE: PlaybackSettings = PlaybackSettings {
10882
mode: PlaybackMode::Once,
109-
volume: Volume::Relative(VolumeLevel(1.0)),
83+
volume: Volume(1.0),
11084
speed: 1.0,
11185
paused: false,
11286
spatial: false,
@@ -115,28 +89,19 @@ impl PlaybackSettings {
11589
/// Will play the associated audio source in a loop.
11690
pub const LOOP: PlaybackSettings = PlaybackSettings {
11791
mode: PlaybackMode::Loop,
118-
volume: Volume::Relative(VolumeLevel(1.0)),
119-
speed: 1.0,
120-
paused: false,
121-
spatial: false,
92+
..PlaybackSettings::ONCE
12293
};
12394

12495
/// Will play the associated audio source once and despawn the entity afterwards.
12596
pub const DESPAWN: PlaybackSettings = PlaybackSettings {
12697
mode: PlaybackMode::Despawn,
127-
volume: Volume::Relative(VolumeLevel(1.0)),
128-
speed: 1.0,
129-
paused: false,
130-
spatial: false,
98+
..PlaybackSettings::ONCE
13199
};
132100

133101
/// Will play the associated audio source once and remove the audio components afterwards.
134102
pub const REMOVE: PlaybackSettings = PlaybackSettings {
135103
mode: PlaybackMode::Remove,
136-
volume: Volume::Relative(VolumeLevel(1.0)),
137-
speed: 1.0,
138-
paused: false,
139-
spatial: false,
104+
..PlaybackSettings::ONCE
140105
};
141106

142107
/// Helper to start in a paused state.
@@ -196,21 +161,21 @@ impl SpatialListener {
196161
}
197162
}
198163

199-
/// Use this [`Resource`] to control the global volume of all audio with a [`Volume::Relative`] volume.
164+
/// Use this [`Resource`] to control the global volume of all audio.
200165
///
201166
/// Note: changing this value will not affect already playing audio.
202167
#[derive(Resource, Default, Clone, Copy, Reflect)]
203168
#[reflect(Resource)]
204169
pub struct GlobalVolume {
205170
/// The global volume of all audio.
206-
pub volume: VolumeLevel,
171+
pub volume: Volume,
207172
}
208173

209174
impl GlobalVolume {
210175
/// Create a new [`GlobalVolume`] with the given volume.
211176
pub fn new(volume: f32) -> Self {
212177
Self {
213-
volume: VolumeLevel::new(volume),
178+
volume: Volume::new(volume),
214179
}
215180
}
216181
}

crates/bevy_audio/src/audio_output.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
AudioSourceBundle, Decodable, GlobalVolume, PlaybackMode, PlaybackSettings, SpatialAudioSink,
3-
SpatialListener, SpatialScale, Volume,
3+
SpatialListener, SpatialScale,
44
};
55
use bevy_asset::{Asset, Assets, Handle};
66
use bevy_ecs::{prelude::*, system::SystemParam};
@@ -159,10 +159,7 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
159159
};
160160

161161
sink.set_speed(settings.speed);
162-
match settings.volume {
163-
Volume::Relative(vol) => sink.set_volume(vol.0 * global_volume.volume.0),
164-
Volume::Absolute(vol) => sink.set_volume(vol.0),
165-
}
162+
sink.set_volume(settings.volume.0 * global_volume.volume.0);
166163

167164
if settings.paused {
168165
sink.pause();
@@ -202,10 +199,7 @@ pub(crate) fn play_queued_audio_system<Source: Asset + Decodable>(
202199
};
203200

204201
sink.set_speed(settings.speed);
205-
match settings.volume {
206-
Volume::Relative(vol) => sink.set_volume(vol.0 * global_volume.volume.0),
207-
Volume::Absolute(vol) => sink.set_volume(vol.0),
208-
}
202+
sink.set_volume(settings.volume.0 * global_volume.volume.0);
209203

210204
if settings.paused {
211205
sink.pause();

crates/bevy_audio/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct AudioPlaySet;
6363
/// Insert an [`AudioBundle`] onto your entities to play audio.
6464
#[derive(Default)]
6565
pub struct AudioPlugin {
66-
/// The global volume for all audio entities with a [`Volume::Relative`] volume.
66+
/// The global volume for all audio entities.
6767
pub global_volume: GlobalVolume,
6868
/// The scale factor applied to the positions of audio sources and listeners for
6969
/// spatial audio.
@@ -72,12 +72,11 @@ pub struct AudioPlugin {
7272

7373
impl Plugin for AudioPlugin {
7474
fn build(&self, app: &mut App) {
75-
app.register_type::<VolumeLevel>()
75+
app.register_type::<Volume>()
7676
.register_type::<GlobalVolume>()
7777
.register_type::<SpatialListener>()
7878
.register_type::<SpatialScale>()
7979
.register_type::<PlaybackMode>()
80-
.register_type::<Volume>()
8180
.register_type::<PlaybackSettings>()
8281
.insert_resource(self.global_volume)
8382
.insert_resource(self.spatial_scale)

0 commit comments

Comments
 (0)