bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Ability to set a Global Volume

Open LiamGallagher737 opened this issue 2 years ago • 9 comments

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.

LiamGallagher737 avatar Feb 16 '23 07:02 LiamGallagher737

This also opens the opportunity to fix the issue in the decodable audio example being very loud by adding .insert_resource(GlobalVolume::new(0.2)) to it. Tested it and it works.

LiamGallagher737 avatar Feb 16 '23 09:02 LiamGallagher737

Changing the global volume should change the sounds already playing that have been set relative to it.

Agreed, but this may not be trivial. Docs would be fine for me as a start.

I think I would prefer an enum for the volume instead of having two fields, but I'm not completely sure on that.

This is nice :)

alice-i-cecile avatar Feb 16 '23 22:02 alice-i-cecile

Enum for volume makes a lot of sense, I'll have a go at changing the volume of already playing sounds. Would something like this work

pub enum Volume {
    Absolute(f32),
    Relative(f32),
}

LiamGallagher737 avatar Feb 16 '23 22:02 LiamGallagher737

That looks perfect. Ideally we could document what the f32 means, or use a more sensible numeric type too. Negative volumes don't exactly make sense to me.

alice-i-cecile avatar Feb 16 '23 23:02 alice-i-cecile

Is there a way in rust to have positive only floats?

LiamGallagher737 avatar Feb 17 '23 00:02 LiamGallagher737

Is there a way in rust to have positive only floats?

I would newtype here and use a constructor method.

alice-i-cecile avatar Feb 17 '23 00:02 alice-i-cecile

I've added the volume enum but using the newtype for the volume level feels too verbose imo, some thoughts on this would be great. I also added the global volume as a field in the audio struct so you can set it using the .set() method on the plugin group.

LiamGallagher737 avatar Feb 17 '23 03:02 LiamGallagher737

Changing the global volume should change the sounds already playing that have been set relative to it.

Updating the volume for AudioSinks that have not been detached is easy, but I am unsure of how to update ones which have been detached (have no strong handle) so for now I will add message mentioning that only new sounds will be impacted.

LiamGallagher737 avatar Feb 17 '23 06:02 LiamGallagher737

I'm glad to see this added. Merging now, we can refine later as needed.

alice-i-cecile avatar Mar 13 '23 15:03 alice-i-cecile