Beispiel #1
0
		public OggTag Read( Stream oggStream )
		{
			OggTag tag = new OggTag();
			
			byte[] b = new byte[4];
			oggStream.Read( b , 0,  b .Length);
			int vendorstringLength = Utils.GetNumber( b, 0, 3);
			b = new byte[vendorstringLength];
			oggStream.Read( b , 0,  b .Length);
			tag.Add("vendor", new string(Encoding.UTF8.GetChars(b)));
			
			b = new byte[4];
			oggStream.Read( b , 0,  b .Length);
			int userComments = Utils.GetNumber( b, 0, 3);

			for ( int i = 0; i < userComments; i++ ) {
				b = new byte[4];
				oggStream.Read( b , 0,  b .Length);
				int commentLength = Utils.GetNumber( b, 0, 3);

				b = new byte[commentLength];
				oggStream.Read( b , 0,  b .Length);
				tag.AddOggField(b);
			}
			
			return tag;
		}
Beispiel #2
0
        public OggTag Read(Stream oggStream)
        {
            OggTag tag = new OggTag();

            byte[] b = new byte[4];
            oggStream.Read(b, 0, b.Length);
            int vendorstringLength = Utils.GetNumber(b, 0, 3);

            b = new byte[vendorstringLength];
            oggStream.Read(b, 0, b.Length);
            tag.Add("vendor", new string(Encoding.UTF8.GetChars(b)));

            b = new byte[4];
            oggStream.Read(b, 0, b.Length);
            int userComments = Utils.GetNumber(b, 0, 3);

            for (int i = 0; i < userComments; i++)
            {
                b = new byte[4];
                oggStream.Read(b, 0, b.Length);
                int commentLength = Utils.GetNumber(b, 0, 3);

                b = new byte[commentLength];
                oggStream.Read(b, 0, b.Length);
                tag.AddOggField(b);
            }

            return(tag);
        }
Beispiel #3
0
        public OggTag Read(Stream flacStream)
        {
            //Begins tag parsing-------------------------------------
            if (flacStream.Length == 0)
            {
                //Empty File
                throw new CannotReadException("Error: File empty");
            }
            flacStream.Seek(0, SeekOrigin.Begin);

            //FLAC Header string
            byte[] b = new byte[4];
            flacStream.Read(b, 0, b.Length);
            string flac = new string(System.Text.Encoding.ASCII.GetChars(b));

            if (flac != "fLaC")
            {
                throw new CannotReadException("fLaC Header not found, not a flac file");
            }

            OggTag tag = null;

            //Seems like we hava a valid stream
            bool isLastBlock = false;

            while (!isLastBlock)
            {
                b = new byte[4];
                flacStream.Read(b, 0, b.Length);
                MetadataBlockHeader mbh = new MetadataBlockHeader(b);

                switch (mbh.BlockType)
                {
                //We got a vorbis comment block, parse it
                case MetadataBlockHeader.BlockTypes.VorbisComment:
                    tag = HandleVorbisComment(mbh, flacStream);
                    mbh = null;
                    return(tag);                            //We have it, so no need to go further

                //This is not a vorbis comment block, we skip to next block
                default:
                    flacStream.Seek(flacStream.Position + mbh.DataLength, SeekOrigin.Begin);
                    break;
                }

                isLastBlock = mbh.IsLastBlock;
                mbh         = null;
            }
            //FLAC not found...
            throw new CannotReadException("FLAC Tag could not be found or read..");
        }
Beispiel #4
0
        private OggTag HandleVorbisComment(MetadataBlockHeader mbh, Stream flacStream)
        {
            long oldPos = flacStream.Position;

            OggTag tag = oggTagReader.Read(flacStream);

            long newPos = flacStream.Position;

            if (newPos - oldPos != mbh.DataLength)
            {
                throw new CannotReadException("Tag length do not match with flac comment data length");
            }

            return(tag);
        }
Beispiel #5
0
        public void Delete(Stream raf, Stream tempRaf)
        {
            OggTag tag = null;

            try {
                tag = (OggTag)reader.Read(raf);
            } catch (CannotReadException e) {
                Write(new OggTag(), raf, tempRaf);
                return;
            }

            OggTag emptyTag = new OggTag();

            if (tag.HasField("vendor"))
            {
                emptyTag.Set("vendor", tag.Get("vendor")[0] as string);
            }

            Write(emptyTag, raf, tempRaf);
        }
Beispiel #6
0
        public Tag Read(Stream raf)
        {
            long oldPos = 0;

            //----------------------------------------------------------

            //Check wheter we have an ogg stream---------------
            raf.Seek(0, SeekOrigin.Begin);
            byte[] b = new byte[4];
            raf.Read(b, 0, b.Length);

            string ogg = new string(System.Text.Encoding.ASCII.GetChars(b));

            if (ogg != "OggS")
            {
                throw new CannotReadException("OggS Header could not be found, not an ogg stream");
            }
            //--------------------------------------------------

            //Parse the tag ------------------------------------
            raf.Seek(0, SeekOrigin.Begin);

            //Supposing 1st page = codec infos
            //			2nd page = comment+decode info
            //...Extracting 2nd page

            //1st page to get the length
            b      = new byte[4];
            oldPos = raf.Position;
            raf.Seek(26, SeekOrigin.Begin);
            int pageSegments = raf.ReadByte() & 0xFF;           //unsigned

            raf.Seek(oldPos, SeekOrigin.Begin);

            b = new byte[27 + pageSegments];
            raf.Read(b, 0, b.Length);

            OggPageHeader pageHeader = new OggPageHeader(b);

            raf.Seek(raf.Position + pageHeader.PageLength, SeekOrigin.Begin);

            //2nd page extraction
            oldPos = raf.Position;
            raf.Seek(raf.Position + 26, SeekOrigin.Begin);
            pageSegments = raf.ReadByte() & 0xFF;           //unsigned
            raf.Seek(oldPos, SeekOrigin.Begin);

            b = new byte[27 + pageSegments];
            raf.Read(b, 0, b.Length);
            pageHeader = new OggPageHeader(b);

            b = new byte[7];
            raf.Read(b, 0, b.Length);

            string vorbis = new string(System.Text.Encoding.ASCII.GetChars(b, 1, 6));

            if (b[0] != 3 || vorbis != "vorbis")
            {
                throw new CannotReadException("Cannot find comment block (no vorbis header)");
            }

            //Begin tag reading
            OggTag tag = oggTagReader.Read(raf);

            byte isValid = (byte)raf.ReadByte();

            if (isValid == 0)
            {
                throw new CannotReadException("Error: The OGG Stream isn't valid, could not extract the tag");
            }

            return(tag);
        }