-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Closed
Labels
A-AudioSounds playback and modificationSounds playback and modificationC-BugAn unexpected or incorrect behaviorAn unexpected or incorrect behaviorD-TrivialNice and easy! A great choice to get started with BevyNice and easy! A great choice to get started with BevyS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!This issue is ready for an implementation PR. Go for it!
Description
Bevy version
0.16.0
What you did
use bevy::audio::Volume;
fn main() {
let linear_a = Volume::Linear(0.5);
let linear_b = Volume::Linear(1.6);
let linear_c = linear_a - linear_b;
println!("{}", linear_c.to_linear());
}
What went wrong
It prints 1.1 and not 0
Solution
Per @SolarLiner, maybe we should remove this API entirely
Metadata
Metadata
Assignees
Labels
A-AudioSounds playback and modificationSounds playback and modificationC-BugAn unexpected or incorrect behaviorAn unexpected or incorrect behaviorD-TrivialNice and easy! A great choice to get started with BevyNice and easy! A great choice to get started with BevyS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!This issue is ready for an implementation PR. Go for it!
Type
Projects
Status
Done
Milestone
Relationships
Development
Select code repository
Activity
SolarLiner commentedon May 29, 2025
Adding and subtracting volumes shouldn't be allowed in the first place; you should only be able to multiply (and maybe divide by abuse of notation) them, precisely because it keeps the meaning intuitive: if you have a 0.5 volume and want to decrease it by 0.5, you want 0.25 at the end instead. It's the same reason why you can't add/subtract percentages either.
janhohenheim commentedon May 29, 2025
In the context of an audio slider, I certainly can subtract percentages: I start at 100% and go down in 10 steps of 10%
Though you will rightfully point out that using linear instead of dB here is misleading anyways, as hearing is logarithmic
SolarLiner commentedon May 31, 2025
Adding and subtracting percentages is not a well-defined operation. If you're at 0 % and subtract another 10 %, should it give you -10 % ? Depending on the meaning of the percentage it might not make sense at all.
My point is that much like adding percentages is almost never the correct thing, so it is with volume. With volume specifically, adding and subtracting only applies to specifically linear values (with a bunch of caveats as we have seen) but in the general case you can only multiply and divide.
You can work with "10 steps of 10 %", and do whatever you want to your
f32
, but once you wrap it into aVolume::Linear
, it then loses the ability to be added to.The drawbacks of manipulating linear volume values are besides this point (though additional motivation to prevent footguns). This is more fundamental.
SolarLiner commentedon May 31, 2025
And for the implementation of an audio fader specifically, this is something that was discussed recently for the new official 2D template: you can find a code snippet here: TheBevyFlock/bevy_new_2d#385 (comment)
janhohenheim commentedon Jun 1, 2025
Fair enough, I think you've convinced me :) Renaming the issue.
The template is second party, not official, btw
[-]Linear volume subtractions use `abs()` instead of clamping at `0`[/-][+]Volume subtractions and additions are malformed[/+]fix: Ensure linear volume subtraction does not go below zero (#19423)
3 remaining items