Header Search

Jan 31, 2011 at 1:07 PM

Hi,

I'm not much of a multimedia programming guy, but with lots of googling i managed to write the following code to search for the MP3 header.

 

 bool isHeaderFound = false;
            byte firstByte;
            byte secondByte;
            byte[] mp3HeaderData = new byte[_mp3FrameHeaderLength];
            while (!isHeaderFound)
            {
                firstByte = (byte)r.ReadByte();
                secondByte = (byte)r.ReadByte();
                string bitValue = byte2bit(firstByte) + byte2bit(secondByte);
                string first11bits = bitValue.Substring(0, 11);
                if (first11bits == "11111111111")
                {
                    Debug.WriteLine(bitValue);
                    isHeaderFound = true;
                    mp3HeaderData[0] = firstByte;
                    mp3HeaderData[1] = secondByte;
                }
            }
for (int i = 2; i < 4; i++, _byteCount++) //two byte already read, read the remaining 2
                mp3HeaderData[i] = (byte)r.ReadByte();


 private string byte2bit(Int32 intNumber)
        {
            Int32 intBinary;
            string strReturn = "";
            while (intNumber > 0)
            {
                intBinary = intNumber % 2; //get remainder
                strReturn += intBinary;
                intNumber = intNumber / 2;  //k
            }

            char[] arrBinary;
            arrBinary = strReturn.ToCharArray();
            Array.Reverse(arrBinary);
            strReturn = new string(arrBinary);
            if (strReturn.Length < 8)
            {
                strReturn = strReturn.PadLeft(8, '0');
            }
            return strReturn;
        }
The first time it is able to find the header correctly.If i try to find the header while the bytes are getting downloaded ,then also it recognizes header bytes but then i'm not able to hear anything. If i don't search for header while bytes are downloaded, i'm able to hear sound but with hiccups. Any idea where i am going wrong. Thanks.

Coordinator
Mar 3, 2011 at 7:26 PM
Edited Mar 3, 2011 at 8:03 PM

Thanks for the reply, and sorry for the lack of updates ( exams, start of internship etc. )

I'm looking at the hiccups now, and I'm sure it's related to the handling of the mp3header. I'll try and inject your code instead of the current handler, as it seems to be the right approach. Thank you :)

Edit: Sadly, it didn't quite seem to fix it, but I added some exception handling to prevent the program from crashing. I'm guessing it's the right approach, as we cannot guarantee that a sequence of 11 high bits is the start of a header ( even though the probability is really low ). I'm starting to think that the download algorithm needs a complete rewrite though, as there's still a good list of weird bugs.. sometimes the music plays at half speed, and there's still a good amount of hicupps during playback. I'm guessing the thread downloading the data is being downprioritized somehow.