internal static VorbisMode Init(VorbisStreamDecoder vorbis, DataPacket packet) { var mode = new VorbisMode(vorbis); mode.BlockFlag = packet.ReadBit(); mode.WindowType = (int)packet.ReadBits(16); mode.TransformType = (int)packet.ReadBits(16); var mapping = (int)packet.ReadBits(8); if (mode.WindowType != 0 || mode.TransformType != 0 || mapping >= vorbis.Maps.Length) { throw new InvalidOperationException(); } mode.Mapping = vorbis.Maps[mapping]; mode.BlockSize = mode.BlockFlag ? vorbis.Block1Size : vorbis.Block0Size; // now pre-calc the window(s)... if (mode.BlockFlag) { // long block mode._windows = new float[4][]; mode._windows[0] = new float[vorbis.Block1Size]; mode._windows[1] = new float[vorbis.Block1Size]; mode._windows[2] = new float[vorbis.Block1Size]; mode._windows[3] = new float[vorbis.Block1Size]; } else { // short block mode._windows = new float[1][]; mode._windows[0] = new float[vorbis.Block0Size]; } mode.CalcWindows(); return(mode); }
bool UnpackPacket(DataPacket packet) { // make sure we're on an audio packet if (packet.ReadBit()) { // we really can't do anything... count the bits as waste return(false); } // get mode and prev/next flags var modeBits = _modeFieldBits; _mode = Modes[(int)packet.ReadBits(_modeFieldBits)]; if (_mode.BlockFlag) { _prevFlag = packet.ReadBit(); _nextFlag = packet.ReadBit(); modeBits += 2; } else { _prevFlag = _nextFlag = false; } if (packet.IsShort) { return(false); } var startBits = packet.BitsRead; var halfBlockSize = _mode.BlockSize / 2; // read the noise floor data (but don't decode yet) for (int i = 0; i < _channels; i++) { _floorData[i] = _mode.Mapping.ChannelSubmap[i].Floor.UnpackPacket(packet, _mode.BlockSize, i); _noExecuteChannel[i] = !_floorData[i].ExecuteChannel; // go ahead and clear the residue buffers Array.Clear(_residue[i], 0, halfBlockSize); } // make sure we handle no-energy channels correctly given the couplings... foreach (var step in _mode.Mapping.CouplingSteps) { if (_floorData[step.Angle].ExecuteChannel || _floorData[step.Magnitude].ExecuteChannel) { _floorData[step.Angle].ForceEnergy = true; _floorData[step.Magnitude].ForceEnergy = true; } } var floorBits = packet.BitsRead - startBits; startBits = packet.BitsRead; foreach (var subMap in _mode.Mapping.Submaps) { for (int j = 0; j < _channels; j++) { if (_mode.Mapping.ChannelSubmap[j] != subMap) { _floorData[j].ForceNoEnergy = true; } } var rTemp = subMap.Residue.Decode(packet, _noExecuteChannel, _channels, _mode.BlockSize); for (int c = 0; c < _channels; c++) { var r = _residue[c]; var rt = rTemp[c]; for (int i = 0; i < halfBlockSize; i++) { r[i] += rt[i]; } } } _glueBits += 1; _modeBits += modeBits; _floorBits += floorBits; _resBits += packet.BitsRead - startBits; _wasteBits += 8 * packet.Length - packet.BitsRead; _packetCount += 1; return(true); }
bool LoadBooks(DataPacket packet) { if (!packet.ReadBytes(7).SequenceEqual(new byte[] { 0x05, 0x76, 0x6f, 0x72, 0x62, 0x69, 0x73 })) { 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 InvalidOperationException(); } ++_glueBits; _wasteHdrBits += 8 * packet.Length - packet.BitsRead; _modeFieldBits = Utils.ilog(Modes.Length - 1); return(true); }