예제 #1
0
        /// <summary>
        /// The number of channels is read from the flac header here.
        /// This can be extracted from bits 165-167 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 number of channels.</returns>
        public static Fin <byte> ReadNumChannels(Stream stream)
        {
            long position = stream.Seek(ChannelOffset, SeekOrigin.Begin);

            Debug.Assert(position == 20, $"Expected stream.Seek position to return 20, instead returned {position}");

            Span <byte> buffer    = stackalloc byte[1];
            int         bytesRead = stream.Read(buffer);

            if (bytesRead != buffer.Length)
            {
                return(FileTooShort);
            }

            return((byte)(BinaryHelpers.Read3BitUnsignedBigEndianIgnoringFirstFourAndLastBit(buffer) + 1));
        }