public StreamHeader(File file, long streamLength) { if (file == null) { throw new ArgumentNullException("file"); } stream_length = streamLength; version = 7; header_data = 0; frames = 0; sample_rate = 0; channels = 2; framecount = 0; file.Seek(0); ByteVector magic = file.ReadBlock(4); if (magic.StartsWith(FileIdentifierSv7)) { ReadSv7Properties(magic + file.ReadBlock((int)SizeSV7 - 4)); } else if (magic.StartsWith(FileIdentifierSv8)) { ReadSv8Properties(file); } else { throw new CorruptFileException("Data does not begin with identifier."); } }
private void ReadSv8Properties(File file) { bool foundSH = false; while (!foundSH) { ByteVector packetType = file.ReadBlock(2); uint packetSizeLength = 0; bool eof = false; ulong packetSize = ReadSize(file, ref packetSizeLength, ref eof); if (eof) { break; } ulong payloadSize = packetSize - 2 - packetSizeLength; ByteVector data = file.ReadBlock((int)payloadSize); if (packetType == "SH") { foundSH = true; if (payloadSize <= 5) { break; } int pos = 4; version = data[pos]; pos += 1; frames = (uint)ReadSize(data, ref pos); if (pos > (uint)payloadSize - 3) { break; } ulong beginSilence = ReadSize(data, ref pos); if (pos > (uint)payloadSize - 2) { break; } ushort flags = data.Mid(pos, 1).ToUShort(true); sample_rate = sftable[(flags >> 13) & 0x07]; channels = ((flags >> 4) & 0x0F) + 1; framecount = frames - beginSilence; } else if (packetType == "SE") { break; } else { file.Seek((int)payloadSize, SeekOrigin.Current); } } }
/// <summary> /// Constructs and initializes a new instance of <see /// cref="StreamHeader" /> for a specified header block and /// stream length. /// </summary> /// <param name="data"> /// A <see cref="ByteVector" /> object containing the stream /// header data. /// </param> /// <param name="streamLength"> /// A <see cref="long" /> value containing the length of the /// MusePAck stream in bytes. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="data" /> is <see langword="null" />. /// </exception> /// <exception cref="CorruptFileException"> /// <paramref name="data" /> does not begin with <see /// cref="FileIdentifierSv7" /> or with <see /// cref="FileIdentifierSv8" /> or is less than /// <see cref="Size" /> bytes long. /// </exception> public StreamHeader(File file, long streamLength) { if (file == null) { throw new ArgumentNullException(nameof(file)); } // Assign default values, to be able to call methods // in the constructor stream_length = streamLength; version = 7; header_data = 0; frames = 0; sample_rate = 0; channels = 2; framecount = 0; file.Seek(0); ByteVector magic = file.ReadBlock(4); if (magic.StartsWith(FileIdentifierSv7)) { // SV7 Format has a fixed Header size ReadSv7Properties(magic + file.ReadBlock((int)SizeSV7 - 4)); } else if (magic.StartsWith(FileIdentifierSv8)) { // for SV8 the properties need to be read from // packet information inside the file ReadSv8Properties(file); } else { throw new CorruptFileException( "Data does not begin with identifier."); } }