/// <summary> /// The bit depth is read from the flac header here. /// This can be extracted from bits 168-172 of the stream. /// More information: https://xiph.org/flac/format.html#metadata_block_streaminfo. /// </summary> /// <param name="stream">The flac file stream.</param> /// <returns>The bit depth.</returns> public static Fin <byte> ReadBitDepth(Stream stream) { long position = stream.Seek(ChannelOffset, SeekOrigin.Begin); Debug.Assert(position == 20, $"Expected stream.Seek position to return 20, instead returned {position}"); stream.Seek(ChannelOffset, SeekOrigin.Begin); Span <byte> buffer = stackalloc byte[2]; int bytesRead = stream.Read(buffer); if (bytesRead != buffer.Length) { return(FileTooShort); } return((byte)(BinaryHelpers.Read5BitUnsignedBigEndianIgnoringFirstSevenAndLastFourBits(buffer) + 1)); }