Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Ability to set a Global Volume #7706

Merged
merged 19 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/bevy_audio/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub struct PlaybackSettings {
pub repeat: bool,
/// Volume to play at.
pub volume: f32,
/// Ignore global volume
pub absolue_volume: bool,
/// Speed to play at.
pub speed: f32,
}
Expand All @@ -143,13 +145,15 @@ impl PlaybackSettings {
pub const ONCE: PlaybackSettings = PlaybackSettings {
repeat: false,
volume: 1.0,
absolue_volume: false,
speed: 1.0,
};

/// Will play the associate audio source in a loop.
pub const LOOP: PlaybackSettings = PlaybackSettings {
repeat: true,
volume: 1.0,
absolue_volume: false,
speed: 1.0,
};

Expand All @@ -164,6 +168,12 @@ impl PlaybackSettings {
self.speed = speed;
self
}

/// Helper to set if the volume sould ignore the global volume.
pub const fn with_absolute_volume(mut self, absolue_volume: bool) -> Self {
self.absolue_volume = absolue_volume;
self
}
}

#[derive(Clone)]
Expand All @@ -188,3 +198,23 @@ where
.finish()
}
}

/// Use this [`Resource`] to control the global volume of all non-absolute audio.
#[derive(Resource)]
pub struct GlobalVolume {
/// The global volume of all audio.
pub volume: f32,
}

impl Default for GlobalVolume {
fn default() -> Self {
Self { volume: 1.0 }
}
}

impl GlobalVolume {
/// Create a new [`GlobalVolume`] with the given volume.
pub const fn new(volume: f32) -> Self {
Self { volume }
}
}
8 changes: 5 additions & 3 deletions crates/bevy_audio/src/audio_output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Audio, AudioSource, Decodable};
use crate::{Audio, AudioSource, Decodable, GlobalVolume};
use bevy_asset::{Asset, Assets};
use bevy_ecs::system::{Res, ResMut, Resource};
use bevy_reflect::TypeUuid;
Expand Down Expand Up @@ -70,6 +70,7 @@ where
audio_sources: &Assets<Source>,
audio: &mut Audio<Source>,
sinks: &mut Assets<AudioSink>,
global_volume: &GlobalVolume,
) {
let mut queue = audio.queue.write();
let len = queue.len();
Expand All @@ -79,7 +80,7 @@ where
if let Some(audio_source) = audio_sources.get(&config.source_handle) {
if let Some(sink) = self.play_source(audio_source, config.settings.repeat) {
sink.set_speed(config.settings.speed);
sink.set_volume(config.settings.volume);
sink.set_volume(config.settings.volume * global_volume.volume);

// don't keep the strong handle. there is no way to return it to the user here as it is async
let _ = sinks.set(config.sink_handle, AudioSink { sink: Some(sink) });
Expand All @@ -97,11 +98,12 @@ where
pub fn play_queued_audio_system<Source: Asset + Decodable>(
audio_output: Res<AudioOutput<Source>>,
audio_sources: Option<Res<Assets<Source>>>,
global_volume: Res<GlobalVolume>,
mut audio: ResMut<Audio<Source>>,
mut sinks: ResMut<Assets<AudioSink>>,
) {
if let Some(audio_sources) = audio_sources {
audio_output.try_play_queued(&*audio_sources, &mut *audio, &mut sinks);
audio_output.try_play_queued(&*audio_sources, &mut *audio, &mut sinks, &global_volume);
};
}

Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_audio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ mod audio_source;
#[allow(missing_docs)]
pub mod prelude {
#[doc(hidden)]
pub use crate::{Audio, AudioOutput, AudioSource, Decodable, PlaybackSettings};
pub use crate::{Audio, AudioOutput, AudioSource, Decodable, GlobalVolume, PlaybackSettings};
}

pub use audio::*;
Expand All @@ -56,6 +56,7 @@ impl Plugin for AudioPlugin {
.add_asset::<AudioSource>()
.add_asset::<AudioSink>()
.init_resource::<Audio<AudioSource>>()
.init_resource::<GlobalVolume>()
.add_system(play_queued_audio_system::<AudioSource>.in_base_set(CoreSet::PostUpdate));

#[cfg(any(feature = "mp3", feature = "flac", feature = "wav", feature = "vorbis"))]
Expand Down