Time to get roll’n. First a standard disclaimer. The code is meant to be readable and written quickly. I’ll go back later and optimize. Also I make mistakes, but once discovered I’ll correct them as fast as possible. whew…
MP3s are composed of a series of frames. And each frame starts with a sync word. The sync word is 12 1’s (0xFFF). If reading an MP3 stream, one would wait for the sync word and then try to decode the rest of the header and maybe wait for the next frame to make sure that the syncword that was found was real and not just a random occurrence.
|Bit start bit pos||bit length||Description|
|0||12||Frame sync word 0xFFF|
|12||1||MPEG version, 0=v2 1=v1|
|13||2||Layer, 00=reserved 01=layer III 10=layer II 11=layer I|
|15||1||Protection bit, 0=has 16bit CRC after header 1=no protection|
|20||2||Sampling rate 00=44100 01=48000 10=32000 11=reserved|
|22||1||Padding bit 0=no pad 1=padded|
|24||2||Channel mode 00=stereo 01=joint stereo 10=dual channel 11= single|
|26||2||Mode extention (joint stereo)|
|30||2||Emphasis 00=none 01=50/15ms 10=reserved 11=CCIT J.17|
I whipped up some code to find the sync and parse the header from a standard MP3 and created a new bitbucket repo.