-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
base: main
Are you sure you want to change the base?
Conversation
## 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
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), | ||
)), |
There was a problem hiding this comment.
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.
There was a problem hiding this 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
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. |
There was a problem hiding this 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.
fix: Ensure linear volume subtraction does not go below zero
Solution