Fix some dodgy math - #3271
Merged
Merged
Fix some dodgy math#3271
Conversation
Contributor
Author
|
Hi @microkatz, please can you check this PR? Thanks! |
I was expanding SilenceSkippingAudioProcessor to cover different sample formats and noticed a couple instances of dodgy math. - SilenceSkippingAudioProcessor was not considering `bytesPerFrame` when calculating `maybeSilenceBufferSize` which in practice led to `minimumSilenceDurationUs` being divided by 4 (channel count 2 and sample format 2). The test `skipInNoisySignalWithShortSilences_skipsNothing` should've caught this but didn't, due to a bug in the test: - In `Pcm16BitAudioBuilder`, `appendFrames` was only generating half of the supposed millisecond amounts (but due to the loop in the caller, this wasn't noticed as the output was still correct size, just with wrong amount of silence/noise between each change). This is because the loop was accepting a frame count but did `i += channelCount`, that is, treating it as sample count. Thus, `skipInNoisySignalWithShortSilences_skipsNothing` only generated 15ms of silence and noise, which is below the threshold of 25ms (which is the intended default 100ms but divided by 4 due to aforementioned bug), and thus passed. - In `modifyVolume`, the volume percentage was calculated from the byte index, which could lead to the left and right channel having a different percentage or prevent reaching 100% of volume in edge cases. - In PcmAudioUtil, 8-bit PCM is treated as signed, but Android defines 8-bit PCM to be unsigned.
Contributor
|
I'm going to send this for internal review now. You may see some more commits being added as I make changes in response to review feedback. Please refrain from pushing any more substantive changes as it will complicate the internal review - thanks! |
microkatz
force-pushed
the
oddmath
branch
4 times, most recently
from
August 21, 2026 11:09
965e24e to
ab00283
Compare
Contributor
Author
|
Thanks! |
microkatz
pushed a commit
that referenced
this pull request
Sep 4, 2026
PiperOrigin-RevId: 970544044 (cherry picked from commit db8db02)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
I was expanding SilenceSkippingAudioProcessor to cover different sample formats and noticed a couple instances of dodgy math.
bytesPerFramewhen calculatingmaybeSilenceBufferSizewhich in practice led tominimumSilenceDurationUsbeing divided by 4 (channel count 2 and sample format 2). The testskipInNoisySignalWithShortSilences_skipsNothingshould've caught this but didn't, due to a bug in the test:Pcm16BitAudioBuilder,appendFrameswas only generating half of the supposed millisecond amounts (but due to the loop in the caller, this wasn't noticed as the output was still correct size, just with wrong amount of silence/noise between each change). This is because the loop was accepting a frame count but didi += channelCount, that is, treating it as sample count. Thus,skipInNoisySignalWithShortSilences_skipsNothingonly generated 15ms of silence and noise, which is below the threshold of 25ms (which is the intended default 100ms but divided by 4 due to aforementioned bug), and thus passed.modifyVolume, the volume percentage was calculated from the byte index, which could lead to the left and right channel having a different percentage or prevent reaching 100% of volume in edge cases.