Skip to content

Commit

Permalink
Add master and SFX volume config
Browse files Browse the repository at this point in the history
  • Loading branch information
Zakru committed Aug 3, 2023
1 parent 752bd8d commit d0eb5ae
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
7 changes: 3 additions & 4 deletions crates/audio/src/music.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bevy::{asset::LoadState, prelude::*};
use bevy_kira_audio::{
prelude::{Audio as KAudio, AudioSource as KAudioSource, Volume},
AudioControl,
prelude::{Audio, AudioSource, Volume}, AudioControl,
};
use de_conf::Configuration;
use de_core::state::AppState;
Expand All @@ -21,7 +20,7 @@ impl Plugin for MusicPlugin {
}

#[derive(Resource)]
struct Tracks(Handle<KAudioSource>);
struct Tracks(Handle<AudioSource>);

fn setup(mut commands: Commands, server: Res<AssetServer>) {
commands.insert_resource(Tracks(server.load("audio/music/menu_loop.mp3")));
Expand All @@ -35,7 +34,7 @@ fn load(server: Res<AssetServer>, tracks: Res<Tracks>) -> Progress {
}
}

fn start(audio: Res<KAudio>, tracks: Res<Tracks>, config: Res<Configuration>) {
fn start(audio: Res<Audio>, tracks: Res<Tracks>, config: Res<Configuration>) {
if !config.audio().music_enabled() {
return;
}
Expand Down
21 changes: 15 additions & 6 deletions crates/audio/src/spatial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::f32::consts::PI;

use bevy::{asset::LoadState, prelude::*};
use bevy_kira_audio::{
prelude::{Audio as KAudio, AudioSource as KAudioSource},
AudioControl, AudioInstance,
prelude::{AudioSource, Volume},
AudioApp, AudioChannel, AudioControl, AudioInstance, Audio,
};
use de_camera::CameraFocus;
use de_conf::Configuration;
use de_core::{gamestate::GameState, state::AppState};
use enum_map::{enum_map, Enum, EnumMap};
use iyes_progress::{Progress, ProgressSystem};
Expand Down Expand Up @@ -58,7 +59,7 @@ impl PlaySpatialAudioEvent {
}

#[derive(Resource)]
struct Sounds(EnumMap<Sound, Handle<KAudioSource>>);
struct Sounds(EnumMap<Sound, Handle<AudioSource>>);

#[derive(Component, Default)]
struct SpatialSound;
Expand Down Expand Up @@ -136,17 +137,23 @@ fn play(
mut commands: Commands,
camera: Query<&GlobalTransform, With<Camera>>,
focus: Res<CameraFocus>,
audio: Res<KAudio>,
audio: Res<Audio>,
sounds: Res<Sounds>,
config: Res<Configuration>,
mut play_events: EventReader<PlaySpatialAudioEvent>,
) {
if !config.audio().sound_enabled() {
play_events.clear();
}

let camera = camera.single();
let sound_volume = config.audio().sound_volume() as f64;

for PlaySpatialAudioEvent { sound, position } in &mut play_events {
let (volume, pan) = calculate_volume_and_pan(camera, &focus, *position);
let handle = audio
.play(sounds.0[*sound].clone())
.with_volume(volume)
.with_volume(volume * sound_volume)
.with_panning(pan)
.handle();

Expand All @@ -165,9 +172,11 @@ fn update_spatial(
spatial_audios: Query<InitializedSound, With<SpatialSound>>,
camera: Query<&GlobalTransform, With<Camera>>,
focus: Res<CameraFocus>,
config: Res<Configuration>,
mut audio_instances: ResMut<Assets<AudioInstance>>,
) {
let camera = camera.single();
let sound_volume = config.audio().sound_volume() as f64;

for (entity, audio, transform) in &spatial_audios {
let Some(audio_instance) = audio_instances.get_mut(audio) else {
Expand All @@ -177,7 +186,7 @@ fn update_spatial(

let (volume, pan) = calculate_volume_and_pan(camera, &focus, transform.translation());

audio_instance.set_volume(volume, default());
audio_instance.set_volume(volume * sound_volume, default());
audio_instance.set_panning(pan, default());
}
}
40 changes: 36 additions & 4 deletions crates/conf/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ pub struct Camera {

#[derive(Deserialize, Serialize, Config, Debug, Clone)]
pub struct AudioConf {
#[is_finite]
#[ensure(*music_volume >= 0., "`master_volume` must be greater than or equal to 0.0.")]
#[ensure(*music_volume <= 1., "`master_volume` must be smaller or equal to 1.0.")]
master_volume: f32,

#[is_finite]
#[ensure(*music_volume >= 0., "`sound_volume` must be greater than or equal to 0.0.")]
#[ensure(*music_volume <= 1., "`sound_volume` must be smaller or equal to 1.0.")]
sound_volume: f32,

#[is_finite]
#[ensure(*music_volume >= 0., "`music_volume` must be greater than or equal to 0.0.")]
#[ensure(*music_volume <= 1., "`music_volume` must be smaller or equal to 1.0.")]
Expand Down Expand Up @@ -85,7 +95,11 @@ impl Default for Camera {

impl Default for AudioConf {
fn default() -> Self {
Self { music_volume: 1. }
Self {
master_volume: 1.,
sound_volume: 1.,
music_volume: 1.,
}
}
}

Expand Down Expand Up @@ -178,13 +192,31 @@ impl MultiplayerConf {
}

impl AudioConf {
// Whether music is enabled (volume is zero).
/// Whether audio is enabled (master volume is above zero).
pub fn audio_enabled(&self) -> bool {
self.master_volume > 0.
}

pub fn master_volume(&self) -> f32 {
self.master_volume
}

/// Whether SFX are enabled (sound and master volume are above zero).
pub fn sound_enabled(&self) -> bool {
self.audio_enabled() && self.sound_volume > 0.
}

pub fn sound_volume(&self) -> f32 {
self.master_volume * self.sound_volume
}

/// Whether music is enabled (music and master volume are above zero).
pub fn music_enabled(&self) -> bool {
self.music_volume > 0.
self.audio_enabled() && self.music_volume > 0.
}

pub fn music_volume(&self) -> f32 {
self.music_volume
self.master_volume * self.music_volume
}
}

Expand Down
6 changes: 6 additions & 0 deletions docs/src/conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ est as if all properties are missing.
* `scroll_inverted` (bool; default: `false`) – if `true`, mouse wheel and
touchpad scrolling is inverted.
* `audio` (object) – audio configuration.
* `master_volume` (f32; default: `1.0`) – sets the master volume of all audio. It
must be a finite number between `0.0` and `1.0`. If set to 0 no audio will play.
* `sound_volume` (f32; default: `1.0`) – sets the SFX volume. It must be a finite
number between `0.0` and `1.0`. If set to 0 sound effects will not play.
* `music_volume` (f32; default: `1.0`) – sets the music volume. It must be a finite
number between `0.0` and `1.0`. If set to 0 music will not play.

Expand All @@ -64,5 +68,7 @@ camera:
rotation_sensitivity: 0.01
scroll_inverted: false
audio:
master_volume: 1.0
sound_volume: 1.0
music_volume: 1.0
```

0 comments on commit d0eb5ae

Please sign in to comment.