Repository navigation
Trim the MP3 info header frame and encoder delay - #101
Merged
Merged
Conversation
- go-mp3 decodes the Xing/Info header frame as a frame of silence and leaves in the encoder delay and end padding that the header declares, so a LAME-encoded sound started about 47 ms late at 48 kHz and ran long - decodeMP3 now reads the header: the header frame is always dropped, and the delay and padding are dropped when the encoder name is LAME, Lavc or Lavf - The skipped frames are decoded and discarded rather than seeked past, so the rest of the stream is what a straight decode gives - A header that would trim the whole stream is ignored - Tests decode two LAME fixtures (CBR mono with an Info tag, VBR stereo with a Xing tag behind an ID3v2 tag) and compare them to the source sine, sample-aligned; a header from an unknown encoder loses only the header frame - Files with no info header decode exactly as before
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.
What
MP3s that carry an info header now start and end where their source did.
LAME,LavcorLavf), the encoder delay and end padding it declares are trimmed too.Why
go-mp3 (and Beep's wrapper around it) decodes every frame as audio, including the header frame, and does nothing with the delay fields. Measured through
decodeSoundon a 0.2 s tone encoded with LAME at 48 kHz:The extra 3072 frames were 1152 of header frame, 1105 of encoder plus decoder delay in front of the sound (47 ms late in total), and the rest padding after it.
Notes for review
internal/audio/native/mp3.go.mp3InfoTrimparses the header from the bytesdecodeMP3already holds;windowdrops the leading frames by decoding and discarding them, not by seeking, so the remaining samples are identical to a straight decode.testdata/(2.1 kB and 3.0 kB) were made with ffmpeg's libmp3lame fromaevalsrcsines. ffmpeg's own decoder returns 9600 and 8820 frames for them, the same lengths the tests expect.TestCharacterizeMP3still passes unchanged: its fixture has no info header.Testing
go test ./...passes;golangci-lint run ./...(v2.14.0) reports 0 issues.