-
Notifications
You must be signed in to change notification settings - Fork 246
Fix Input Boost wrapping around instead of clipping #3834
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
mcfnord
wants to merge
1
commit into
jamulussoftware:main
Choose a base branch
from
mcfnord:fix-input-boost-overflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2
β2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1429,8 +1429,8 @@ void CClient::ProcessAudioDataIntern ( CVector<int16_t>& vecsStereoSndCrd ) | |
| // apply a general gain boost to all audio input: | ||
| for ( i = 0, j = 0; i < iMonoBlockSizeSam; i++, j += 2 ) | ||
| { | ||
| vecsStereoSndCrd[j + 1] = static_cast<int16_t> ( iInputBoost * vecsStereoSndCrd[j + 1] ); | ||
| vecsStereoSndCrd[j] = static_cast<int16_t> ( iInputBoost * vecsStereoSndCrd[j] ); | ||
| vecsStereoSndCrd[j + 1] = Float2Short ( static_cast<float> ( iInputBoost ) * vecsStereoSndCrd[j + 1] ); | ||
| vecsStereoSndCrd[j] = Float2Short ( static_cast<float> ( iInputBoost ) * vecsStereoSndCrd[j] ); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fix does look sensible to me definitely. I think we do clip on the server with basically this code also. |
||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
This is:
(e.g.
1.0 * 1) Is that going to work or is* <short>going to coerce<float>to<short>anyway?Then it says:
if I'm right on the first bit.
I'm assuming -- given the naming convention
vecs....is astd::vector<short>(or QVector).Uh oh!
There was an error while loading. Please reload this page.
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.
MY LLM WROTE:
The promotion goes the other way.
float * shortnever coerces thefloatdown; C++'s usual arithmetic conversions take the wider type, so theshortis integer-promoted tointand then converted tofloat. The expression's static type isfloat, andFloat2Short()receives a real float, not an already-narrowed short.Your assumption about the vector is right, by the way:
ProcessAudioDataIntern ( CVector<short>& vecsStereoSndCrd )(client.h:322), andCVector<short>derives fromstd::vector<short>, sovecsStereoSndCrd[j]is ashortlvalue.Confirmed rather than asserted:
compiles, and the whole loop is clean under
-Wall -Wconversion.The values, boost = 2:
static_cast<int16_t> ( int * short )Float2Short ( float * short )So the old form is where the narrowing happened β the
intproduct was truncated to 16 bits, which is the wraparound this PR fixes. The new form keeps full range untilFloat2Short()saturates it.On precision: the largest magnitude reachable is 10 Γ 32768 = 327 680, far inside float's exactly-representable integer range (2^24), so nothing is lost to rounding before the clamp.
For what it's worth, this is the same idiom the pan path a few lines below already uses, unchanged from before this PR:
One thing you might legitimately want changed: the
static_cast<float>is not load-bearing.iInputBoost * vecsStereoSndCrd[j]isint * intβint, which cannot overflow at the maximum boost of 10, and it would convert tofloatat the call toFloat2Short()anyway. I kept the cast because it makes "this multiply is done in float" visible at the point it happens rather than at the callee's signature. Happy to drop it if you prefer the shorter line.