Skip to content

Fix some dodgy math#3271

Open
nift4 wants to merge 1 commit into
androidx:mainfrom
nift4:oddmath
Open

Fix some dodgy math#3271
nift4 wants to merge 1 commit 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.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants