Down-mix stereo MP3 to mono in readMp3 - #4544
Open
Daksha1611 wants to merge 1 commit into
Open
Conversation
readMp3() is documented in audio_utils.hpp as returning mono float32 PCM samples, and readWav() down-mixes stereo accordingly. readMp3() did not: it appended framesRead * mp3.channels floats per chunk, so a 2-channel file came back as an interleaved L/R stream twice as long as the frame count. Downstream that buffer is consumed as a mono waveform - the chat completions input_audio path copies it straight into a 1-D f32 tensor - so stereo MP3 input produced garbage audio at twice the real duration. The resampling path was affected too: outputLength was computed from the interleaved sample count and resample_audio() interpolated between adjacent L and R samples. Down-mix per decode chunk so the existing size guard keeps measuring the final buffer, and pass 1 channel to the metadata-based size validation to match readWav(). Dropping the drmp3_uninit() call before the overflow throw also removes a double-uninit: the enclosing catch(...) already calls it before rethrowing. Tests: the two existing max-file-size tests used a joint-stereo frame and encoded the interleaved count (2304); they now expect the mono count (1152). Adds mp3StereoIsDownmixedToMono covering the contract directly.
Daksha1611
marked this pull request as ready for review
September 12, 2026 07:09
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.
🛠 Summary
Fixes #4543.
readMp3()is documented inaudio_utils.hppas returning mono float32 PCM samples, andreadWav()down-mixes stereo to match.readMp3()did not — it appendedframesRead * mp3.channelsfloats per chunk, so a 2-channel file came back interleaved and twice as long as the frame count. The chat completionsinput_audiopath copies that straight into a 1-D f32 tensor, so stereo MP3 reached the model as garbage audio at double the real duration. The resampling path was affected too, sinceoutputLengthwas derived from the interleaved sample count.Down-mixes per decode chunk, so the existing size guard still measures the final buffer. The metadata size check now passes 1 channel, matching the
readWavcall. Dropping thedrmp3_uninit()before the overflow throw also removes a double-uninit — the enclosingcatch (...)already calls it.The two existing max-file-size tests build a joint-stereo frame and expected the interleaved count of 2304; they now expect the mono count of 1152. Adds
mp3StereoIsDownmixedToMono.I have no OVMS build container available, so this is not compiled against the full tree and CI will need to confirm the build. I did check the rewritten loop in isolation, including across chunk boundaries where the write-offset arithmetic matters. Draft for that reason.
🧪 Checklist