From 74e793d1e1482558730903ba0ce31e17aeeacc1a Mon Sep 17 00:00:00 2001 From: mgi388 <135186256+mgi388@users.noreply.github.com> Date: Tue, 17 Dec 2024 06:28:30 +1100 Subject: [PATCH] Move Volume and GlobalVolume to own file (#16838) # Objective - Prework for reviving #9582. ## Solution - Move the two types to volume.rs and made it compile. - Also `#[reflect(Debug)]` on `Volume` while I'm here. ## Testing - Ran example locally. - Rely on CI. --- crates/bevy_audio/src/audio.rs | 47 +------------------------------- crates/bevy_audio/src/lib.rs | 2 ++ crates/bevy_audio/src/volume.rs | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 crates/bevy_audio/src/volume.rs diff --git a/crates/bevy_audio/src/audio.rs b/crates/bevy_audio/src/audio.rs index be05582ab54de..25f2e07df95df 100644 --- a/crates/bevy_audio/src/audio.rs +++ b/crates/bevy_audio/src/audio.rs @@ -1,37 +1,11 @@ #![expect(deprecated)] -use crate::{AudioSource, Decodable}; +use crate::{AudioSource, Decodable, Volume}; use bevy_asset::{Asset, Handle}; -use bevy_derive::Deref; use bevy_ecs::prelude::*; use bevy_math::Vec3; use bevy_reflect::prelude::*; -/// A volume level equivalent to a non-negative float. -#[derive(Clone, Copy, Deref, Debug, Reflect)] -pub struct Volume(pub(crate) f32); - -impl Default for Volume { - fn default() -> Self { - Self(1.0) - } -} - -impl Volume { - /// Create a new volume level. - pub fn new(volume: f32) -> Self { - debug_assert!(volume >= 0.0); - Self(f32::max(volume, 0.)) - } - /// Get the value of the volume level. - pub fn get(&self) -> f32 { - self.0 - } - - /// Zero (silent) volume level - pub const ZERO: Self = Volume(0.0); -} - /// The way Bevy manages the sound playback. #[derive(Debug, Clone, Copy, Reflect)] pub enum PlaybackMode { @@ -197,25 +171,6 @@ impl SpatialListener { } } -/// Use this [`Resource`] to control the global volume of all audio. -/// -/// Note: changing this value will not affect already playing audio. -#[derive(Resource, Default, Clone, Copy, Reflect)] -#[reflect(Resource, Default)] -pub struct GlobalVolume { - /// The global volume of all audio. - pub volume: Volume, -} - -impl GlobalVolume { - /// Create a new [`GlobalVolume`] with the given volume. - pub fn new(volume: f32) -> Self { - Self { - volume: Volume::new(volume), - } - } -} - /// A scale factor applied to the positions of audio sources and listeners for /// spatial audio. /// diff --git a/crates/bevy_audio/src/lib.rs b/crates/bevy_audio/src/lib.rs index 1519987a4b155..a8de9393d15e1 100644 --- a/crates/bevy_audio/src/lib.rs +++ b/crates/bevy_audio/src/lib.rs @@ -34,6 +34,7 @@ mod audio_output; mod audio_source; mod pitch; mod sinks; +mod volume; /// The audio prelude. /// @@ -51,6 +52,7 @@ pub mod prelude { pub use audio::*; pub use audio_source::*; pub use pitch::*; +pub use volume::*; pub use rodio::{cpal::Sample as CpalSample, source::Source, Sample}; pub use sinks::*; diff --git a/crates/bevy_audio/src/volume.rs b/crates/bevy_audio/src/volume.rs new file mode 100644 index 0000000000000..f12fe0497fe3f --- /dev/null +++ b/crates/bevy_audio/src/volume.rs @@ -0,0 +1,48 @@ +use bevy_derive::Deref; +use bevy_ecs::prelude::*; +use bevy_reflect::prelude::*; + +/// Use this [`Resource`] to control the global volume of all audio. +/// +/// Note: changing this value will not affect already playing audio. +#[derive(Resource, Default, Clone, Copy, Reflect)] +#[reflect(Resource, Default)] +pub struct GlobalVolume { + /// The global volume of all audio. + pub volume: Volume, +} + +impl GlobalVolume { + /// Create a new [`GlobalVolume`] with the given volume. + pub fn new(volume: f32) -> Self { + Self { + volume: Volume::new(volume), + } + } +} + +/// A volume level equivalent to a non-negative float. +#[derive(Clone, Copy, Deref, Debug, Reflect)] +#[reflect(Debug)] +pub struct Volume(pub(crate) f32); + +impl Default for Volume { + fn default() -> Self { + Self(1.0) + } +} + +impl Volume { + /// Create a new volume level. + pub fn new(volume: f32) -> Self { + debug_assert!(volume >= 0.0); + Self(f32::max(volume, 0.)) + } + /// Get the value of the volume level. + pub fn get(&self) -> f32 { + self.0 + } + + /// Zero (silent) volume level + pub const ZERO: Self = Volume(0.0); +}