Skip to content

Volume subtractions and additions are malformed #19417

@janhohenheim

Description

@janhohenheim
Member

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

Activity

added
C-BugAn unexpected or incorrect behavior
S-Needs-TriageThis issue needs to be labelled
D-TrivialNice and easy! A great choice to get started with Bevy
A-AudioSounds playback and modification
S-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!
on May 28, 2025
SolarLiner

SolarLiner commented on May 29, 2025

@SolarLiner
Contributor

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

janhohenheim commented on May 29, 2025

@janhohenheim
MemberAuthor

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

SolarLiner commented on May 31, 2025

@SolarLiner
Contributor

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 a Volume::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

SolarLiner commented on May 31, 2025

@SolarLiner
Contributor

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

janhohenheim commented on Jun 1, 2025

@janhohenheim
MemberAuthor

Fair enough, I think you've convinced me :) Renaming the issue.
The template is second party, not official, btw

changed the title [-]Linear volume subtractions use `abs()` instead of clamping at `0`[/-] [+]Volume subtractions and additions are malformed[/+] on Jun 1, 2025

3 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-AudioSounds playback and modificationC-BugAn unexpected or incorrect behaviorD-TrivialNice and easy! A great choice to get started with BevyS-Ready-For-ImplementationThis issue is ready for an implementation PR. Go for it!

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @SolarLiner@janhohenheim

      Issue actions

        Volume subtractions and additions are malformed · Issue #19417 · bevyengine/bevy