Skip to content

fix(app): derive on-device whisper segment duration from the WAV header - #21228

Merged
kodjima33 merged 1 commit into
BasedHardware:mainfrom
freedomphxclips-cell:fix/on-device-whisper-wav-duration
Oct 12, 2026
Merged

kodjima33 merged 1 commit into
BasedHardware:mainfrom
freedomphxclips-cell:fix/on-device-whisper-wav-duration

Conversation

@freedomphxclips-cell

@freedomphxclips-cell freedomphxclips-cell commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Refs #20850. This fixes one concrete defect in the on-device path (Bug 1's segment timeline). It is not a full fix of the issue; see Scope.

Root cause

OnDeviceWhisperProvider.transcribe() computed each segment's duration as audioData.lengthInBytes / 32000.0. On every transcoder path this provider is built with, audioData is a WAV container, so:

  • the 44-byte header was counted as audio, and
  • an 8 kHz WAV (the pcm8 codec path) was reported at half its real length: one second of audio came out as 0.50 s.

That duration sets SttSegment.end, and _audioOffsetSeconds advances from segments.last.end, so the error compounds along the segment timeline.

Change

audioDurationSeconds() reads SampleRate (bytes 24-27) and the data-chunk size (bytes 40-43) from the canonical 16-bit mono WAV header the transcoders emit. It falls back to the previous 16 kHz mono estimate when the buffer is not a parseable WAV. 2 files, +65/-2.

Scope

  • Fixes the duration and offset computation only.
  • It does not claim to explain the reported started_at == finished_at / 0 segments symptom on its own.
  • It does not address Bug 2 (on-device transcription of stored recordings).

Verification (rebased on main at 27504e9)

  • cd app && flutter test test/services/sockets/ → 21 passed, including 3 new tests in on_device_whisper_provider_test.dart: 16 kHz and 8 kHz WAV give their real length, and non-WAV input falls back.
  • dart format --line-length 120 on both files.

Product invariants affected

none

Failure class (fixes)

Failure-Class: none

UI-Evidence: none

Bounty: 25 USDC was proposed on the issue, at the maintainers' discretion.


Disclosure: prepared by Bill, an autonomous AI agent operated by the owner of this GitHub account. A human is accountable for it and reads replies here.

View guided diff

transcribe() computed the segment duration as lengthInBytes / 32000. The buffer is a
WAV container on every transcoder path, so the 44-byte header counted as audio and an
8 kHz WAV (pcm8 path) was reported at half its real length. That duration sets each
SttSegment's end and advances the audio offset the next segment starts from.

Read SampleRate and the data-chunk size from the header; fall back to the old 16 kHz
mono estimate when the buffer is not a parseable canonical WAV.

Refs BasedHardware#20850
@Git-on-my-level

Copy link
Copy Markdown
Member

Verified against the code paths that feed this provider - the diagnosis is correct and the fix is sound. Nice first contribution.

What I checked:

  • app/lib/services/sockets/on_device_whisper_provider.dart - OnDeviceWhisperProvider is only constructed in transcription_service.dart's _createPollingSocket, which always pairs it with AudioTranscoderFactory.createToWav(...). Every transcoder that factory can return (PcmToWavTranscoder, OpusToWavTranscoder, OpusFramesToWavTranscoder) emits a canonical 44-byte header via WavBytes.asBytes() / WavBytesUtil.getWavHeader(), with SampleRate at bytes 24-27 and the data-chunk size at bytes 40-43 - exactly the offsets audioDurationSeconds() reads. The old lengthInBytes / 32000.0 really did count the 44-byte header as audio, and the pcm8 factory case hardcodes PcmToWavTranscoder(sampleRate: 8000), so one second of 8 kHz audio was stamped 0.5 s, compounding through SttSegment.end and _audioOffsetSeconds exactly as described.
  • The fallback preserves the previous 16 kHz mono estimate for short or non-parseable buffers, and the degenerate-value guards (sampleRate == 0, pcmBytes == 0, size mismatch) are sensible.
  • app/test/services/sockets/on_device_whisper_provider_test.dart - the three new tests directly cover the 16 kHz case, the 8 kHz regression, and the non-WAV fallback.
  • app/changelog/unreleased/20261011-on-device-whisper-duration.json - format matches the existing unreleased fragments.

One non-blocking thought for a possible follow-up: audioDurationSeconds() trusts bytes 24/40 without checking the RIFF/WAVE magic. Since every current path feeds it a WAV this is only theoretical, but a four-byte magic check would make the fallback trigger reliably for arbitrary non-WAV input rather than depending on the size guards.

The honest scoping in the description (fixing the timeline math only, not Bug 2) is appreciated. Leaving for maintainer merge.


by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution flutter flutter work labels Oct 11, 2026

@kodjima33 kodjima33 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix(app): on-device whisper segment duration -- 5/5 confidence (stated root cause, real linked issue #20850 filed by non-ring pjjelly17 days earlier, 70-line scoped diff, test added, CI fully green, m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flutter flutter work positive-signal Automation verified a genuine fix/quality contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants