Example #1
0
        //Used for debugging.
        // public static void ReadBytes(string Mp3Path, Encoding FileEncoding){
        //     Console.OutputEncoding = FileEncoding; //set the console output.
        //     byte[] MyMp3 = System.IO.File.ReadAllBytes(Mp3Path);
        //     string ByteFile = Hex.Dump(MyMp3); //Store Hex the Hex data from the Mp3 file.
        //     System.IO.File.WriteAllText("./Printouts/Mp3HexPrintout.txt", ByteFile); //Create a document containing Mp3 ByteFile (debugging).
        // }

        ///Method ensures the mp3 file is encapsulated with the appropriate data.
        ///Does not alter existing meta data.
        ///Ensures the existence of an ApeTag item with the key: CloudCoinStack.
        ///Correct Apetag has a CloudCoinStack and StackName container.
        public static TagLib.Ape.Tag CheckApeTag(TagLib.File Mp3File)
        {
            TagLib.Ape.Tag ApeTag;
            bool           hasCCS       = false;
            bool           hasStackName = false;

            // Pass a true parameter to the GetTag function in order to add one if the Mp3File doesn't already have a Mp3Tag.
            // By passing a the parameter 'TagTypes.Ape' we ensure the type is of Ape.

            try{
                ApeTag       = (TagLib.Ape.Tag)Mp3File.GetTag(TagLib.TagTypes.Ape, true);
                hasCCS       = ApeTag.HasItem("CloudCoinStack");
                hasStackName = ApeTag.HasItem("StackName");
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("The process failed: {0}", e.ToString());
                ApeTag = (TagLib.Ape.Tag)Mp3File.GetTag(TagLib.TagTypes.Ape, false);
            }
            if (!hasCCS)
            {
                TagLib.Ape.Item item = new TagLib.Ape.Item("CloudCoinStack", "");
                ApeTag.SetItem(item);
            }
            if (!hasStackName)
            {
                TagLib.Ape.Item itemName = new TagLib.Ape.Item("StackName", "");
                ApeTag.SetItem(itemName);
            }
            return(ApeTag);
        }
        public Tag Read(Stream apeStream)
        {
            ApeTag tag = new ApeTag();

            //Check wether the file contains an APE tag--------------------------------
            apeStream.Seek( apeStream.Length - 32 , SeekOrigin.Begin);

            byte[] b = new byte[8];
            apeStream.Read(b, 0, b.Length);

            string tagS = new string( System.Text.Encoding.ASCII.GetChars(b) );
            if(tagS != "APETAGEX" ){
                throw new CannotReadException("There is no APE Tag in this file");
            }
            //Parse the tag -)------------------------------------------------
            //Version
            b = new byte[4];
            apeStream.Read( b , 0,  b .Length);
            int version = Utils.GetNumber(b, 0,3);
            if(version != 2000) {
                throw new CannotReadException("APE Tag other than version 2.0 are not supported");
            }

            //Size
            b = new byte[4];
            apeStream.Read( b , 0,  b .Length);
            long tagSize = Utils.GetLongNumber(b, 0,3);

            //Number of items
            b = new byte[4];
            apeStream.Read( b , 0,  b .Length);
            int itemNumber = Utils.GetNumber(b, 0,3);

            //Tag Flags
            b = new byte[4];
            apeStream.Read( b , 0,  b .Length);
            //TODO handle these

            apeStream.Seek(apeStream.Length - tagSize, SeekOrigin.Begin);

            for(int i = 0; i<itemNumber; i++) {
                //Content length
                b = new byte[4];
                apeStream.Read( b , 0,  b .Length);
                int contentLength = Utils.GetNumber(b, 0,3);
                if(contentLength > 500000)
                    throw new CannotReadException("Item size is much too large: "+contentLength+" bytes");

                //Item flags
                b = new byte[4];
                apeStream.Read( b , 0,  b .Length);
                //TODO handle these
                bool binary = ((b[0]&0x06) >> 1) == 1;

                int j = 0;
                while(apeStream.ReadByte() != 0)
                    j++;
                apeStream.Seek(apeStream.Position - j -1, SeekOrigin.Begin);
                int fieldSize = j;

                //Read Item key
                b = new byte[fieldSize];
                apeStream.Read( b , 0,  b .Length);
                apeStream.Seek(1, SeekOrigin.Current);
                string field = new string(System.Text.Encoding.GetEncoding("ISO-8859-1").GetChars(b));

                //Read Item content
                b = new byte[contentLength];
                apeStream.Read( b , 0,  b .Length);
                if(!binary)
                    tag.Add(new ApeTagTextField(field, new string(System.Text.Encoding.UTF8.GetChars(b))));
                else
                    tag.Add(new ApeTagBinaryField(field, b));
            }

            return tag;
        }