コード例 #1
0
        /// <summary>
        /// The total samples in the stream are read from the flac header here.
        /// This can be extracted from bits 173-208 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 total samples.</returns>
        public static Fin <ulong> ReadTotalSamples(Stream stream)
        {
            long position = stream.Seek(FlacSamplesOffset, SeekOrigin.Begin);

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

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

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

            return(BinaryHelpers.Read36BitUnsignedBigEndianIgnoringFirstNibble(buffer));
        }