Example #1
0
        /*
         * TODO: Better Implementation of bitrate determination.
         * Somehow first audio stream makes the day. Then Streamnumber must
         * be stored, to read the right info out of an optional stream bitrate properties
         * chunk. Or if that comes first, store all the data and assign it on occurence of the
         * fist audio stream.
         * Where is the info about VBR
         */
        public EncodingInfo Read(Stream stream)
        {
            EncodingInfo result      = new EncodingInfo();
            GUID         header_guid = GUID.ReadGUID(stream);

            if (!GUID.GUID_HEADER.Equals(header_guid))
            {
                return(result);
            }

            BinaryReader reader = new BinaryReader(stream);

            // Skip length of header
            stream.Seek(8, SeekOrigin.Current);

            // Read the number of chunks.
            uint chunk_count = reader.ReadUInt32();

            // Skip unknown bytes
            stream.Seek(2, SeekOrigin.Current);

            // Two flags, When both are set, all Information needed has ben read
            bool is_file_header_parsed  = false;
            bool is_stream_chunk_parsed = false;

            // Now read the chunks
            for (int i = 0; i < chunk_count && !(is_file_header_parsed && is_stream_chunk_parsed); i++)
            {
                long  chunk_start  = stream.Position;
                GUID  current_guid = GUID.ReadGUID(stream);
                ulong chunk_len    = reader.ReadUInt64();

                if (GUID.GUID_FILE.Equals(current_guid))
                {
                    stream.Seek(48, SeekOrigin.Current);
                    result.Duration       = new TimeSpan((long)reader.ReadUInt64());
                    is_file_header_parsed = true;
                }
                else if (GUID.GUID_STREAM.Equals(current_guid))
                {
                    GUID streamTypeGUID = GUID.ReadGUID(stream);
                    if (GUID.GUID_AUDIOSTREAM.Equals(streamTypeGUID))
                    {
                        // Jump over ignored values.
                        stream.Seek(38, SeekOrigin.Current);
                        result.EncodingType    = GetFormatDescription(reader.ReadUInt16());
                        result.ChannelNumber   = reader.ReadUInt16();
                        result.SamplingRate    = (int)reader.ReadUInt32();
                        result.Bitrate         = (int)(reader.ReadUInt32() * 8 / 1000);
                        is_stream_chunk_parsed = true;
                    }
                }

                stream.Seek((long)((ulong)chunk_start + chunk_len - (ulong)stream.Position), SeekOrigin.Current);
            }

            return(result);
        }
Example #2
0
        public Tag Read(Stream stream)
        {
            /* Assuming we are at the start of the ASF header GUID */
            GUID header_guid = GUID.ReadGUID(stream);

            if (!GUID.GUID_HEADER.Equals(header_guid))
            {
                return(null);
            }

            Tag          tag    = new Tag();
            BinaryReader reader = new BinaryReader(stream);

            // Skip length of header
            stream.Seek(8, SeekOrigin.Current);

            // Read the number of chunks
            uint chunk_count = reader.ReadUInt32();

            // Skip unknown bytes
            stream.Seek(2, SeekOrigin.Current);

            // Two flags, When both are set, all Information needed has ben read
            bool is_content_parsed  = false;
            bool is_extended_parsed = false;

            // Now read the chunks
            for (int i = 0; i < chunk_count && !(is_content_parsed && is_extended_parsed); i++)
            {
                long  chunk_start  = stream.Position;
                GUID  current_guid = GUID.ReadGUID(stream);
                ulong chunk_len    = reader.ReadUInt64();

                if (GUID.GUID_CONTENTDESCRIPTION.Equals(current_guid))
                {
                    ReadContentDescription(stream, tag);
                    is_content_parsed = true;
                }
                else if (GUID.GUID_EXTENDED_CONTENT_DESCRIPTION.Equals(current_guid))
                {
                    ReadExtendedDescription(stream, tag);
                    is_extended_parsed = true;
                }

                stream.Seek((long)((ulong)chunk_start + chunk_len - (ulong)stream.Position), SeekOrigin.Current);
            }

            return(tag);
        }