Skip to content

Fix some dodgy math - #3271

Merged
copybara-service[bot] merged 2 commits into
androidx:mainfrom
nift4:oddmath
Aug 25, 2026
Merged

copybara-service[bot] merged 2 commits into
androidx:mainfrom
nift4:oddmath

Conversation

@nift4

@nift4 nift4 commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

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.
@microkatz microkatz self-assigned this Jun 12, 2026
@nift4

nift4 commented Jul 14, 2026

Copy link
Copy Markdown
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.
@microkatz

Copy link
Copy Markdown
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
microkatz force-pushed the oddmath branch 4 times, most recently from 965e24e to ab00283 Compare August 21, 2026 11:09
@copybara-service
copybara-service Bot merged commit db8db02 into androidx:main Aug 25, 2026
1 check passed
@nift4
nift4 deleted the oddmath branch August 25, 2026 16:10
@nift4

nift4 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

Thanks!

microkatz pushed a commit that referenced this pull request Sep 4, 2026
PiperOrigin-RevId: 970544044
(cherry picked from commit db8db02)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants