コード例 #1
0
        /// <summary>
        /// The sample rate is read from the flac header here.
        /// This can be extracted from bits 145-164 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 sample rate.</returns>
        public static Fin <uint> ReadSampleRate(Stream stream)
        {
            long position = stream.Seek(SampleRateOffset, SeekOrigin.Begin);

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

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

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

            return(BinaryHelpers.Read20BitUnsignedBigEndianIgnoringLastNibble(buffer));
        }