fix: handle mid-stream sample rate/format changes in decode_audio - #1472
Open
g0rdonL wants to merge 1 commit into
Open
fix: handle mid-stream sample rate/format changes in decode_audio#1472g0rdonL wants to merge 1 commit into
g0rdonL wants to merge 1 commit into
Conversation
_group_frames wrote every decoded frame into a single AudioFifo, but
AudioFifo locks onto the format/layout/rate of the first frame it
receives and raises ValueError on any later frame that differs. Files
whose audio stream changes sample rate or sample format partway
through (e.g. a spliced/transcoded recording) crashed decode_audio
with:
ValueError: Frame does not match AudioFifo parameters.
AudioResampler has the same locking behavior and can hit the
equivalent error in _resample_frames once frames from different
source parameter groups reach it.
Catch the ValueError in both places and start a fresh
FIFO/resampler (flushing the previous one first) for the frame with
the new parameters. The downstream AudioResampler still normalizes
everything to the requested output format/layout/rate, so results
are unaffected for the common case; verified decode_audio output is
byte-identical before/after this change on all existing fixtures in
tests/data.
Fixes SYSTRAN#1451
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.
Bug
decode_audiocrashes on audio files whose stream changes sample rate or sample format partway through._group_frameswrites every decoded frame into a singleAudioFifo, butAudioFifolocks onto the format/layout/rate of the first frame it receives and raisesValueErroron any later frame that differs:AudioResamplerhas the same locking behavior and can hit the equivalent error in_resample_framesonce frames from different source-parameter groups reach it (this surfaces after fixing the FIFO issue alone).Fix
Catch the
ValueErrorin both_group_framesand_resample_frames, flush what's already buffered, and start a fresh FIFO/resampler for the frame with the new parameters. The finalAudioResamplerstep still normalizes everything to the requested output format/layout/rate, so decoded output is unaffected in the common (single sample-rate) case.Verification
whisper-french-mixed-samplerate.mp3) attached to decode_audio crashes with "Frame does not match AudioFifo parameters" on files whose sample rate/format changes mid-stream #1451 — the file's stream switches 44100 Hz → 48000 Hz partway through (confirmed viaffprobe).decode_audiosucceeds on that file (17.08s of mono 16kHz float32 audio).decode_audiooutput is byte-identical before/after this change on all 5 existing fixtures intests/data/(jfk.flac,multilingual.mp3,physicsworks.wav,stereo_diarization.wav,hotwords.mp3), includingsplit_stereo=True— this fix is a no-op on the happy path.tests/test_audio.pywith a regression test using the reporter's sample file (added astests/data/mixed_samplerate.mp3).pytest tests/→ 20 passed (19 existing + 1 new).black,isort,flake8all clean per CONTRIBUTING.md.Fixes #1451
Drafted and verified with AI assistance (repro, fix, and test run locally); reviewed before submission.