示例#1
0
        private void SeekHeader()
        {
            byte[] buffer = new byte[4];
            do
            {
                // read 4 bytes (possible header)
                int read = source.Read(buffer, 0, 4);

                // less than 4 bytes read -> EOF
                if(read < 4)
                {
                    break;
                }

                // check for typical header strucure
                if(SyncCheck(buffer))
                {
                    // load the header bytes into the header class
                    Header head = new Header(buffer, (source.Position-4));

                    // extract header information and do another check for invalid values
                    if(head.Init())
                    {
                        // if frame contains CRC bytes, copy them to the header
                        if(head.crc)
                        {
                            source.Read(head.crcbytes, 0, 2);
                            source.Seek(-2, SeekOrigin.Current);
                        }

                        // check if the frame length matches the header data
                        byte[] syncbytes = new byte[4];
                        long currentpos = source.Position;
                        source.Seek(head.NextFrame(), SeekOrigin.Begin);
                        source.Read(syncbytes, 0, 4);
                        source.Seek(-1, SeekOrigin.Current);
                        // do we have another header?
                        if(SyncCheck(syncbytes))
                        {
                            // first header?
                            if(headerstream.Count <= 0)
                            {
                                if(SyncCompare(buffer, syncbytes)) // check if MPEG Version / Layer matches those of the next header (to reduce chance to catch noise)
                                {
                                    comparebuffer = (byte[])buffer.Clone();

                                    // we have found a frame!
                                    headerstream.Add(head);
                                    Console.WriteLine(head.ToString());
                                    //break;
                                }
                                else
                                {
                                    // resume search where we left
                                    source.Seek(currentpos, SeekOrigin.Begin);
                                }

                            }
                            else  // not the first header, so we don't do more checks to save some time
                            {
                                // we have found a frame!
                                headerstream.Add(head);
                                //Console.WriteLine(head.ToString());
                            }
                        }

                    }
                }

                // move 1 byte per cycle (instead of 4)
                source.Seek(-3, SeekOrigin.Current);

            } while (true);

            Console.WriteLine("Frames: " + headerstream.Count.ToString());

            if(headerstream.Count <= 0)
            {
                throw new Exception("Could not find MPEG headers");
            }
        }
示例#2
0
        public Frame(Header frameheader, Stream sourceStream)
        {
            scfsi[0] = new bool[4];
            scfsi[1] = new bool[4];
            part_23_length[0] = new int[2];
            part_23_length[1] = new int[2];
            big_values[0] = new int[2];
            big_values[1] = new int[2];
            global_gains[0] = new int[2];
            global_gains[1] = new int[2];
            sf_compress[0] = new int[2];
            sf_compress[1] = new int[2];

            header = frameheader;
            source = sourceStream;

            // let's get our frame's data
            byte[] data = new byte[header.framelength];
            long org_pos = source.Position;
            int headlen = (header.crc) ? 6 : 4;
            source.Seek(header.position+headlen, SeekOrigin.Begin);
            source.Read(data, 0, header.framelength-headlen);
            source.Seek(org_pos, SeekOrigin.Begin);

            // split data

            // select sidedata length
            int sidedata_len = 0;
            if(header.mpegversion == Header.MPEG_V1)
            {
                if(header.channelmode == Header.MONO)
                {
                    sidedata_len = 17;
                }
                else
                {
                    sidedata_len = 32;
                }
            }
            else
            {
                if(header.channelmode == Header.MONO)
                {
                    sidedata_len = 9;
                }
                else
                {
                    sidedata_len = 17;
                }
            }

            // init buffers
            sidedata = new byte[sidedata_len];
            byte[] mdata = new byte[data.Length - sidedata_len];

            // write the data into the buffers
            MemoryStream audiodata = new MemoryStream(data);
            audiodata.Read(sidedata, 0, (int)sidedata.Length);
            audiodata.Read(mdata, 0, (int)mdata.Length);

            // create stream
            maindata = new MemoryStream(mdata);

            Parse_sidedata();
        }