From dff071c2a86c4acce5223d26f1fefea3f9d20dbd Mon Sep 17 00:00:00 2001 From: Liam Gallagher Date: Tue, 11 Apr 2023 02:08:43 +1200 Subject: [PATCH] Ability to set a Global Volume (#7706) # Objective Adds a new resource to control a global volume. Fixes #7690 --- ## Solution Added a new resource to control global volume, this is then multiplied with an audio sources volume to get the output volume, individual audio sources can opt out of this my enabling the `absolute_volume` field in `PlaybackSettings`. --- ## Changelog ### Added - `GlobalVolume` a resource to control global volume (in prelude). - `global_volume` field to `AudioPlugin` or setting the initial value of `GlobalVolume`. - `Volume` enum that can be `Relative` or `Absolute`. - `VolumeLevel` struct for defining a volume level. --------- Co-authored-by: Alice Cecile --- crates/bevy_audio/Cargo.toml | 1 + crates/bevy_audio/src/audio.rs | 83 ++++++++++++++++++++++++--- crates/bevy_audio/src/audio_output.rs | 27 +++++++-- crates/bevy_audio/src/lib.rs | 12 ++-- examples/audio/decodable.rs | 12 ++-- 5 files changed, 114 insertions(+), 21 deletions(-) diff --git a/crates/bevy_audio/Cargo.toml b/crates/bevy_audio/Cargo.toml index ea1998bb44166..1c7e918640c0e 100644 --- a/crates/bevy_audio/Cargo.toml +++ b/crates/bevy_audio/Cargo.toml @@ -16,6 +16,7 @@ bevy_ecs = { path = "../bevy_ecs", version = "0.11.0-dev" } bevy_math = { path = "../bevy_math", version = "0.11.0-dev" } bevy_reflect = { path = "../bevy_reflect", version = "0.11.0-dev", features = ["bevy"] } bevy_transform = { path = "../bevy_transform", version = "0.11.0-dev" } +bevy_derive = { path = "../bevy_derive", version = "0.11.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.11.0-dev" } # other diff --git a/crates/bevy_audio/src/audio.rs b/crates/bevy_audio/src/audio.rs index fa08641c73fe0..0e17168f9b6a6 100644 --- a/crates/bevy_audio/src/audio.rs +++ b/crates/bevy_audio/src/audio.rs @@ -1,5 +1,6 @@ use crate::{AudioSink, AudioSource, Decodable, SpatialAudioSink}; use bevy_asset::{Asset, Handle, HandleId}; +use bevy_derive::{Deref, DerefMut}; use bevy_ecs::system::Resource; use bevy_math::Vec3; use bevy_transform::prelude::Transform; @@ -97,12 +98,12 @@ where /// ``` /// # use bevy_ecs::system::Res; /// # use bevy_asset::AssetServer; - /// # use bevy_audio::Audio; + /// # use bevy_audio::{Audio, Volume}; /// # use bevy_audio::PlaybackSettings; /// fn play_audio_system(asset_server: Res, audio: Res