ReadByte() public method

Reads the next byte from the packet and advances the position counter.
public ReadByte ( ) : byte
return byte
Beispiel #1
0
        bool ProcessStreamHeader(DataPacket packet)
        {
            bool equal = true;

            byte[] sequence = new byte[] { 0x01, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 };
            byte[] packets  = packet.ReadBytes(7);

            for (int i = 0; i < 7; i++)
            {
                if (packets[i] != sequence[i])
                {
                    equal = false;
                    break;
                }
            }

            if (!equal)
            {
                // don't mark the packet as done... it might be used elsewhere
                _glueBits += packet.Length * 8;
                return(false);
            }

            if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber)))
            {
                _pagesSeen.Add(_lastPageSeen);
            }

            _glueBits += 56;

            var startPos = packet.BitsRead;

            if (packet.ReadInt32() != 0)
            {
                throw new InvalidDataException("Only Vorbis stream version 0 is supported.");
            }

            _channels       = packet.ReadByte();
            _sampleRate     = packet.ReadInt32();
            _upperBitrate   = packet.ReadInt32();
            _nominalBitrate = packet.ReadInt32();
            _lowerBitrate   = packet.ReadInt32();

            Block0Size = 1 << (int)packet.ReadBits(4);
            Block1Size = 1 << (int)packet.ReadBits(4);

            if (_nominalBitrate == 0)
            {
                if (_upperBitrate > 0 && _lowerBitrate > 0)
                {
                    _nominalBitrate = (_upperBitrate + _lowerBitrate) / 2;
                }
            }

            _metaBits += packet.BitsRead - startPos + 8;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            return(true);
        }
Beispiel #2
0
        void ProcessStreamHeader(DataPacket packet)
        {
            _pagesSeen.Add(packet.PageSequenceNumber);

            var startPos = packet.BitsRead;

            if (packet.ReadInt32() != 0)
            {
                throw new InvalidDataException("Only Vorbis stream version 0 is supported.");
            }

            _channels       = packet.ReadByte();
            _sampleRate     = packet.ReadInt32();
            _upperBitrate   = packet.ReadInt32();
            _nominalBitrate = packet.ReadInt32();
            _lowerBitrate   = packet.ReadInt32();

            Block0Size = 1 << (int)packet.ReadBits(4);
            Block1Size = 1 << (int)packet.ReadBits(4);

            if (_nominalBitrate == 0)
            {
                if (_upperBitrate > 0 && _lowerBitrate > 0)
                {
                    _nominalBitrate = (_upperBitrate + _lowerBitrate) / 2;
                }
            }

            _metaBits += packet.BitsRead - startPos + 8;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;
        }
Beispiel #3
0
 static bool ValidateHeader(DataPacket packet, byte[] expected)
 {
     for (var i = 0; i < expected.Length; i++)
     {
         if (expected[i] != packet.ReadByte())
         {
             return(false);
         }
     }
     return(true);
 }
        private bool CheckForHeader(DataPacket packet, byte[] header)
        {
            bool equal = true;

            for (int i = 0; i < header.Length; i++)
            {
                byte value = packet.ReadByte();
                if (value != header[i])
                {
                    equal = false;
                }
            }
            return(equal);
        }
        bool ProcessStreamHeader(DataPacket packet)
        {
            if (!CheckForHeader(packet, streamHeader))
            {
                // don't mark the packet as done... it might be used elsewhere
                _glueBits += packet.Length * 8;
                return(false);
            }

            if (!_pagesSeen.Contains(_lastPageSeen = packet.PageSequenceNumber))
            {
                _pagesSeen.Add(_lastPageSeen);
            }

            _glueBits += 56;

            long startPos = packet.BitsRead;

            if (packet.ReadInt32() != 0)
            {
                throw new InvalidDataException("Only Vorbis stream version 0 is supported.");
            }

            _channels       = packet.ReadByte();
            _sampleRate     = packet.ReadInt32();
            _upperBitrate   = packet.ReadInt32();
            _nominalBitrate = packet.ReadInt32();
            _lowerBitrate   = packet.ReadInt32();

            Block0Size = 1 << (int)packet.ReadUBits(4);
            Block1Size = 1 << (int)packet.ReadUBits(4);

            if (_nominalBitrate == 0)
            {
                if (_upperBitrate > 0 && _lowerBitrate > 0)
                {
                    _nominalBitrate = (_upperBitrate + _lowerBitrate) / 2;
                }
            }

            _metaBits     += packet.BitsRead - startPos + 8;
            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            return(true);
        }
        public bool ProcessStreamHeader(DataPacket packet)
        {
            if (!packet.ReadBytes(7).SequenceEqual(new byte[7]
            {
                1,
                118,
                111,
                114,
                98,
                105,
                115
            }))
            {
                _glueBits += packet.Length * 8;
                return(false);
            }
            if (!_pagesSeen.Contains(_lastPageSeen = packet.PageSequenceNumber))
            {
                _pagesSeen.Add(_lastPageSeen);
            }
            _glueBits += 56L;
            long bitsRead = packet.BitsRead;

            if (packet.ReadInt32() != 0)
            {
                throw new InvalidDataException("Only Vorbis stream version 0 is supported.");
            }
            _channels       = packet.ReadByte();
            _sampleRate     = packet.ReadInt32();
            _upperBitrate   = packet.ReadInt32();
            _nominalBitrate = packet.ReadInt32();
            _lowerBitrate   = packet.ReadInt32();
            Block0Size      = 1 << (int)packet.ReadBits(4);
            Block1Size      = 1 << (int)packet.ReadBits(4);
            if (_nominalBitrate == 0 && _upperBitrate > 0 && _lowerBitrate > 0)
            {
                _nominalBitrate = (_upperBitrate + _lowerBitrate) / 2;
            }
            _metaBits     += packet.BitsRead - bitsRead + 8;
            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;
            return(true);
        }
Beispiel #7
0
        private void ProcessStreamHeader(DataPacket packet)
        {
            this._pagesSeen.Add(packet.PageSequenceNumber);
            long bitsRead = packet.BitsRead;

            if (packet.ReadInt32() != 0)
            {
                throw new InvalidDataException("Only Vorbis stream version 0 is supported.");
            }
            this._channels       = (int)packet.ReadByte();
            this._sampleRate     = packet.ReadInt32();
            this._upperBitrate   = packet.ReadInt32();
            this._nominalBitrate = packet.ReadInt32();
            this._lowerBitrate   = packet.ReadInt32();
            this.Block0Size      = 1 << (int)packet.ReadBits(4);
            this.Block1Size      = 1 << (int)packet.ReadBits(4);
            if (this._nominalBitrate == 0 && this._upperBitrate > 0 && this._lowerBitrate > 0)
            {
                this._nominalBitrate = (this._upperBitrate + this._lowerBitrate) / 2;
            }
            this._metaBits     += packet.BitsRead - bitsRead + 8L;
            this._wasteHdrBits += (long)(8 * packet.Length) - packet.BitsRead;
        }
Beispiel #8
0
        void LoadBooks(DataPacket packet)
        {
            packet.SkipBits(8);
            if (!packet.ReadBytes(6).SequenceEqual(new byte[] { 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 }))
            {
                throw new InvalidDataException("Corrupted book header!");
            }

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits       = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits          = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits           = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);
        }
Beispiel #9
0
 private void LoadBooks(DataPacket packet)
 {
   packet.SkipBits(8);
   if (!Enumerable.SequenceEqual<byte>((IEnumerable<byte>) packet.ReadBytes(6), (IEnumerable<byte>) new byte[6]
   {
     (byte) 118,
     (byte) 111,
     (byte) 114,
     (byte) 98,
     (byte) 105,
     (byte) 115
   }))
     throw new InvalidDataException("Corrupted book header!");
   long bitsRead1 = packet.BitsRead;
   this._glueBits += packet.BitsRead;
   this.Books = new VorbisCodebook[(int) packet.ReadByte() + 1];
   for (int number = 0; number < this.Books.Length; ++number)
     this.Books[number] = VorbisCodebook.Init(this, packet, number);
   this._bookBits += packet.BitsRead - bitsRead1;
   long bitsRead2 = packet.BitsRead;
   this.Times = new VorbisTime[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Times.Length; ++index)
     this.Times[index] = VorbisTime.Init(this, packet);
   this._timeHdrBits += packet.BitsRead - bitsRead2;
   long bitsRead3 = packet.BitsRead;
   this.Floors = new VorbisFloor[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Floors.Length; ++index)
     this.Floors[index] = VorbisFloor.Init(this, packet);
   this._floorHdrBits += packet.BitsRead - bitsRead3;
   long bitsRead4 = packet.BitsRead;
   this.Residues = new VorbisResidue[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Residues.Length; ++index)
     this.Residues[index] = VorbisResidue.Init(this, packet);
   this._resHdrBits += packet.BitsRead - bitsRead4;
   long bitsRead5 = packet.BitsRead;
   this.Maps = new VorbisMapping[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Maps.Length; ++index)
     this.Maps[index] = VorbisMapping.Init(this, packet);
   this._mapHdrBits += packet.BitsRead - bitsRead5;
   long bitsRead6 = packet.BitsRead;
   this.Modes = new VorbisMode[(int) packet.ReadBits(6) + 1];
   for (int index = 0; index < this.Modes.Length; ++index)
     this.Modes[index] = VorbisMode.Init(this, packet);
   this._modeHdrBits += packet.BitsRead - bitsRead6;
   if (!packet.ReadBit())
     throw new InvalidDataException();
   ++this._glueBits;
   this._wasteHdrBits += (long) (8 * packet.Length) - packet.BitsRead;
   this._modeFieldBits = Utils.ilog(this.Modes.Length - 1);
 }
Beispiel #10
0
 private void ProcessStreamHeader(DataPacket packet)
 {
   this._pagesSeen.Add(packet.PageSequenceNumber);
   long bitsRead = packet.BitsRead;
   if (packet.ReadInt32() != 0)
     throw new InvalidDataException("Only Vorbis stream version 0 is supported.");
   this._channels = (int) packet.ReadByte();
   this._sampleRate = packet.ReadInt32();
   this._upperBitrate = packet.ReadInt32();
   this._nominalBitrate = packet.ReadInt32();
   this._lowerBitrate = packet.ReadInt32();
   this.Block0Size = 1 << (int) packet.ReadBits(4);
   this.Block1Size = 1 << (int) packet.ReadBits(4);
   if (this._nominalBitrate == 0 && this._upperBitrate > 0 && this._lowerBitrate > 0)
     this._nominalBitrate = (this._upperBitrate + this._lowerBitrate) / 2;
   this._metaBits += packet.BitsRead - bitsRead + 8L;
   this._wasteHdrBits += (long) (8 * packet.Length) - packet.BitsRead;
 }
Beispiel #11
0
        void ProcessStreamHeader(DataPacket packet)
        {
            _pagesSeen.Add(packet.PageSequenceNumber);

            var startPos = packet.BitsRead;

            if (packet.ReadInt32() != 0) throw new ArgumentException("Only Vorbis stream version 0 is supported.");

            _channels = packet.ReadByte();
            _sampleRate = packet.ReadInt32();
            _upperBitrate = packet.ReadInt32();
            _nominalBitrate = packet.ReadInt32();
            _lowerBitrate = packet.ReadInt32();

            Block0Size = 1 << (int)packet.ReadBits(4);
            Block1Size = 1 << (int)packet.ReadBits(4);

            if (_nominalBitrate == 0)
            {
                if (_upperBitrate > 0 && _lowerBitrate > 0)
                {
                    _nominalBitrate = (_upperBitrate + _lowerBitrate) / 2;
                }
            }

            _metaBits += packet.BitsRead - startPos + 8;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;
        }
Beispiel #12
0
        void LoadBooks(DataPacket packet)
        {
            packet.SkipBits(8);
            if ( !packet.ReadBytes ( 6 ).SequenceEqual ( new byte [] { 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 } ) ) throw new ArgumentException ( "Corrupted book header!" );

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if ( !packet.ReadBit () ) throw new ArgumentException ();

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);
        }
        public bool LoadBooks(DataPacket packet)
        {
            if (!packet.ReadBytes(7).SequenceEqual(new byte[7]
            {
                5,
                118,
                111,
                114,
                98,
                105,
                115
            }))
            {
                return(false);
            }
            if (!_pagesSeen.Contains(_lastPageSeen = packet.PageSequenceNumber))
            {
                _pagesSeen.Add(_lastPageSeen);
            }
            long bitsRead = packet.BitsRead;

            _glueBits += packet.BitsRead;
            Books      = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }
            _bookBits += packet.BitsRead - bitsRead;
            bitsRead   = packet.BitsRead;
            Times      = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int j = 0; j < Times.Length; j++)
            {
                Times[j] = VorbisTime.Init(this, packet);
            }
            _timeHdrBits += packet.BitsRead - bitsRead;
            bitsRead      = packet.BitsRead;
            Floors        = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int k = 0; k < Floors.Length; k++)
            {
                Floors[k] = VorbisFloor.Init(this, packet);
            }
            _floorHdrBits += packet.BitsRead - bitsRead;
            bitsRead       = packet.BitsRead;
            Residues       = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int l = 0; l < Residues.Length; l++)
            {
                Residues[l] = VorbisResidue.Init(this, packet);
            }
            _resHdrBits += packet.BitsRead - bitsRead;
            bitsRead     = packet.BitsRead;
            Maps         = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int m = 0; m < Maps.Length; m++)
            {
                Maps[m] = VorbisMapping.Init(this, packet);
            }
            _mapHdrBits += packet.BitsRead - bitsRead;
            bitsRead     = packet.BitsRead;
            Modes        = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int n = 0; n < Modes.Length; n++)
            {
                Modes[n] = VorbisMode.Init(this, packet);
            }
            _modeHdrBits += packet.BitsRead - bitsRead;
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }
            _glueBits++;
            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;
            _modeFieldBits = Utils.ilog(Modes.Length - 1);
            return(true);
        }
        bool LoadBooks(DataPacket packet)
        {
            bool equal = true;
            byte[] sequence = new byte[] { 0x05, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 };
            byte[] packets  = packet.ReadBytes(7);

            for (int i = 0; i < 7; i++)
            {
                if (packets[i] != sequence[i])
                {
                    equal = false;
                    break;
                }
            }

            if (!equal)
            {
                return false;
            }

            if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber))) _pagesSeen.Add(_lastPageSeen);

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit()) throw new InvalidDataException();

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);

            return true;
        }
        bool LoadBooks(DataPacket packet)
        {
            if (!CheckForHeader(packet, bookHeader))
            {
                return(false);
            }

            if (!_pagesSeen.Contains(_lastPageSeen = packet.PageSequenceNumber))
            {
                _pagesSeen.Add(_lastPageSeen);
            }

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Create(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits       = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits          = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Create(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits           = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Create(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadUBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ILog(Modes.Length - 1);

            return(true);
        }
Beispiel #16
0
        private void LoadBooks(DataPacket packet)
        {
            packet.SkipBits(8);
            if (!Enumerable.SequenceEqual <byte>((IEnumerable <byte>)packet.ReadBytes(6), (IEnumerable <byte>) new byte[6]
            {
                (byte)118,
                (byte)111,
                (byte)114,
                (byte)98,
                (byte)105,
                (byte)115
            }))
            {
                throw new InvalidDataException("Corrupted book header!");
            }
            long bitsRead1 = packet.BitsRead;

            this._glueBits += packet.BitsRead;
            this.Books      = new VorbisCodebook[(int)packet.ReadByte() + 1];
            for (int number = 0; number < this.Books.Length; ++number)
            {
                this.Books[number] = VorbisCodebook.Init(this, packet, number);
            }
            this._bookBits += packet.BitsRead - bitsRead1;
            long bitsRead2 = packet.BitsRead;

            this.Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Times.Length; ++index)
            {
                this.Times[index] = VorbisTime.Init(this, packet);
            }
            this._timeHdrBits += packet.BitsRead - bitsRead2;
            long bitsRead3 = packet.BitsRead;

            this.Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Floors.Length; ++index)
            {
                this.Floors[index] = VorbisFloor.Init(this, packet);
            }
            this._floorHdrBits += packet.BitsRead - bitsRead3;
            long bitsRead4 = packet.BitsRead;

            this.Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Residues.Length; ++index)
            {
                this.Residues[index] = VorbisResidue.Init(this, packet);
            }
            this._resHdrBits += packet.BitsRead - bitsRead4;
            long bitsRead5 = packet.BitsRead;

            this.Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Maps.Length; ++index)
            {
                this.Maps[index] = VorbisMapping.Init(this, packet);
            }
            this._mapHdrBits += packet.BitsRead - bitsRead5;
            long bitsRead6 = packet.BitsRead;

            this.Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int index = 0; index < this.Modes.Length; ++index)
            {
                this.Modes[index] = VorbisMode.Init(this, packet);
            }
            this._modeHdrBits += packet.BitsRead - bitsRead6;
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }
            ++this._glueBits;
            this._wasteHdrBits += (long)(8 * packet.Length) - packet.BitsRead;
            this._modeFieldBits = Utils.ilog(this.Modes.Length - 1);
        }
Beispiel #17
0
        bool LoadBooks(DataPacket packet)
        {
            bool equal = true;

            byte[] sequence = new byte[] { 0x05, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 };
            byte[] packets  = packet.ReadBytes(7);

            for (int i = 0; i < 7; i++)
            {
                if (packets[i] != sequence[i])
                {
                    equal = false;
                    break;
                }
            }

            if (!equal)
            {
                return(false);
            }

            if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber)))
            {
                _pagesSeen.Add(_lastPageSeen);
            }

            var bits = packet.BitsRead;

            _glueBits += packet.BitsRead;

            // get books
            Books = new VorbisCodebook[packet.ReadByte() + 1];
            for (int i = 0; i < Books.Length; i++)
            {
                Books[i] = VorbisCodebook.Init(this, packet, i);
            }

            _bookBits += packet.BitsRead - bits;
            bits       = packet.BitsRead;

            // get times
            Times = new VorbisTime[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Times.Length; i++)
            {
                Times[i] = VorbisTime.Init(this, packet);
            }

            _timeHdrBits += packet.BitsRead - bits;
            bits          = packet.BitsRead;

            // get floor
            Floors = new VorbisFloor[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Floors.Length; i++)
            {
                Floors[i] = VorbisFloor.Init(this, packet);
            }

            _floorHdrBits += packet.BitsRead - bits;
            bits           = packet.BitsRead;

            // get residue
            Residues = new VorbisResidue[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Residues.Length; i++)
            {
                Residues[i] = VorbisResidue.Init(this, packet);
            }

            _resHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get map
            Maps = new VorbisMapping[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Maps.Length; i++)
            {
                Maps[i] = VorbisMapping.Init(this, packet);
            }

            _mapHdrBits += packet.BitsRead - bits;
            bits         = packet.BitsRead;

            // get mode settings
            Modes = new VorbisMode[(int)packet.ReadBits(6) + 1];
            for (int i = 0; i < Modes.Length; i++)
            {
                Modes[i] = VorbisMode.Init(this, packet);
            }

            _modeHdrBits += packet.BitsRead - bits;

            // check the framing bit
            if (!packet.ReadBit())
            {
                throw new InvalidDataException();
            }

            ++_glueBits;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            _modeFieldBits = Utils.ilog(Modes.Length - 1);

            return(true);
        }
        bool ProcessStreamHeader(DataPacket packet)
        {
            bool equal = true;
            byte[] sequence = new byte[] { 0x01, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 };
            byte[] packets  = packet.ReadBytes(7);

            for (int i = 0; i < 7; i++)
            {
                if (packets[i] != sequence[i])
                {
                    equal = false;
                    break;
                }
            }

            if (!equal)
            {
                // don't mark the packet as done... it might be used elsewhere
                _glueBits += packet.Length * 8;
                return false;
            }

            if (!_pagesSeen.Contains((_lastPageSeen = packet.PageSequenceNumber))) _pagesSeen.Add(_lastPageSeen);

            _glueBits += 56;

            var startPos = packet.BitsRead;

            if (packet.ReadInt32() != 0) throw new InvalidDataException("Only Vorbis stream version 0 is supported.");

            _channels = packet.ReadByte();
            _sampleRate = packet.ReadInt32();
            _upperBitrate = packet.ReadInt32();
            _nominalBitrate = packet.ReadInt32();
            _lowerBitrate = packet.ReadInt32();

            Block0Size = 1 << (int)packet.ReadBits(4);
            Block1Size = 1 << (int)packet.ReadBits(4);

            if (_nominalBitrate == 0)
            {
                if (_upperBitrate > 0 && _lowerBitrate > 0)
                {
                    _nominalBitrate = (_upperBitrate + _lowerBitrate) / 2;
                }
            }

            _metaBits += packet.BitsRead - startPos + 8;

            _wasteHdrBits += 8 * packet.Length - packet.BitsRead;

            return true;
        }