Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Expose both linear and logarithmic volume settings #9507

Closed
alice-i-cecile opened this issue Aug 20, 2023 · 4 comments · Fixed by #17605
Closed

Expose both linear and logarithmic volume settings #9507

alice-i-cecile opened this issue Aug 20, 2023 · 4 comments · Fixed by #17605
Labels
A-Audio Sounds playback and modification C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Aug 20, 2023

I am wondering if we should have it accept something like

enum Volume {
    Logarithmic(f32),
    Linear(u8), // 255 is the 100%, 128 is 50% of the volume, etc.
}

instead of just f32.

Originally posted by @harudagondi in #9480 (review)

@alice-i-cecile alice-i-cecile added D-Trivial Nice and easy! A great choice to get started with Bevy A-Audio Sounds playback and modification C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Aug 20, 2023
@alice-i-cecile
Copy link
Member Author

@harudagondi I really like this idea, but it's definitely better to spin this out rather than adding it to a docs PR.

@GitGhillie
Copy link
Contributor

Maybe Percentage(f32) instead of Linear(u8)?

  1. Name is closer to the intention
  2. Finer control with f32
  3. Ability to go beyond 100%, in case you have a quiet audio file you want to boost a bit
  4. Less complicated conversion for user

@GitGhillie
Copy link
Contributor

GitGhillie commented Aug 20, 2023

Some additional thoughts:
Logarithmic(f32) might be a bit ambiguous. I think we can split it up in two ways. First the technically correct way:

Amplitude(f32) to control the amplitude, as set_volume currently does.
Volume(f32) to set the volume in dB. so Volume(0.0) would be amplitude 1.0 or percentage 100%. Maybe call it Volume_db(f32) to be extra clear.

Or:
Volume(f32) to control the amplitude, as set_volume currently does.
Volume_db(f32) to set the volume in dB. so Volume(0.0) would be amplitude 1.0.

edit: Volume::Volume(x) might look a bit silly, maybe something like Volume::dB(x) could work in that case.

@basilefff
Copy link
Contributor

If I might add, Kira has a Volume struct that looks very much like what you propose:

pub enum Volume {
    Amplitude(f64),
    Decibels(f64),
}

@SolarLiner SolarLiner added this to Audio Mar 12, 2024
@SolarLiner SolarLiner moved this to Open in Audio Mar 15, 2024
github-merge-queue bot pushed a commit that referenced this issue Feb 10, 2025
# Objective

- Allow users to configure volume using decibels by changing the
`Volume` type from newtyping an `f32` to an enum with `Linear` and
`Decibels` variants.
- Fixes #9507.
- Alternative reworked version of closed #9582.

## Solution

Compared to #9582, this PR has
the following main differences:

1. It uses the term "linear scale" instead of "amplitude" per
https://github.com/bevyengine/bevy/pull/9582/files#r1513529491.
2. Supports `ops` for doing `Volume` arithmetic. Can add two volumes,
e.g. to increase/decrease the current volume. Can multiply two volumes,
e.g. to get the “effective” volume of an audio source considering global
volume.

[requested and blessed on Discord]:
https://discord.com/channels/691052431525675048/749430447326625812/1318272597003341867

## Testing

- Ran `cargo run --example soundtrack`.
- Ran `cargo run --example audio_control`.
- Ran `cargo run --example spatial_audio_2d`.
- Ran `cargo run --example spatial_audio_3d`.
- Ran `cargo run --example pitch`.
- Ran `cargo run --example decodable`.
- Ran `cargo run --example audio`.

---

## Migration Guide

Audio volume can now be configured using decibel values, as well as
using linear scale values. To enable this, some types and functions in
`bevy_audio` have changed.

- `Volume` is now an enum with `Linear` and `Decibels` variants.

Before:

```rust
let v = Volume(1.0);
```

After:

```rust
let volume = Volume::Linear(1.0);
let volume = Volume::Decibels(0.0); // or now you can deal with decibels if you prefer
```

- `Volume::ZERO` has been renamed to the more semantically correct
`Volume::SILENT` because `Volume` now supports decibels and "zero
volume" in decibels actually means "normal volume".
- The `AudioSinkPlayback` trait's volume-related methods now deal with
`Volume` types rather than `f32`s. `AudioSinkPlayback::volume()` now
returns a `Volume` rather than an `f32`. `AudioSinkPlayback::set_volume`
now receives a `Volume` rather than an `f32`. This affects the
`AudioSink` and `SpatialAudioSink` implementations of the trait. The
previous `f32` values are equivalent to the volume converted to linear
scale so the `Volume:: Linear` variant should be used to migrate between
`f32`s and `Volume`.
- The `GlobalVolume::new` function now receives a `Volume` instead of an
`f32`.

---------

Co-authored-by: Zachary Harrold <[email protected]>
@github-project-automation github-project-automation bot moved this from Open to Done in Audio Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Audio Sounds playback and modification C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
Status: Done
3 participants