Skip to content

fix: Ensure linear volume subtraction does not go below zero #19423

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

foxzool
Copy link
Contributor

@foxzool foxzool commented May 29, 2025

fix: Ensure linear volume subtraction does not go below zero

Solution

  • Clamp the result of linear volume subtraction to a minimum of 0.0
  • Add a new test case to verify behavior when subtracting beyond zero

## Solution
- Clamp the result of linear volume subtraction to a minimum of 0.0
- Add a new test case to verify behavior when subtracting beyond zero
@foxzool foxzool changed the title fix: Ensure linear volume subtraction does not go below zero (#19417) fix: Ensure linear volume subtraction does not go below zero May 29, 2025
Comment on lines 192 to 196
match (self, rhs) {
(Linear(a), Linear(b)) => Linear(a - b),
(Linear(a), Linear(b)) => Linear((a - b).max(0.0)),
(Decibels(a), Decibels(b)) => Decibels(linear_to_decibels(
decibels_to_linear(a) - decibels_to_linear(b),
)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only correct solution here is to simply not impl Add/Sub on Volume. Linear volumes are like percentages, and it does not make sense to add or subtract percentages. It's similar here with "linear volume": if the volume is at 0.5 and you want to reduce it by another 0.5, you should end up with 0.25 instead of 0.

Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth to discuss whether to keep these impls or not, but while we have them, we should at least not use abs

@janhohenheim janhohenheim added D-Trivial Nice and easy! A great choice to get started with Bevy A-Audio Sounds playback and modification S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels May 29, 2025
@SolarLiner
Copy link
Contributor

I think it's worth to discuss whether to keep these impls or not, but while we have them, we should at least not use abs

My recommendation would be for this PR to remove the impls, and mark this a breaking change in case people have accidentally/intentionally relied on this impl existing.

@SolarLiner SolarLiner moved this to In Progress in Audio May 31, 2025
@janhohenheim janhohenheim added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 1, 2025
Copy link
Member

@janhohenheim janhohenheim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you convinced me.

@github-project-automation github-project-automation bot moved this from In Progress to Needs Update in Audio Jun 1, 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 D-Trivial Nice and easy! A great choice to get started with Bevy S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
Status: Needs Update
Development

Successfully merging this pull request may close these issues.

Volume subtractions and additions are malformed
3 participants