示例#1
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="stream">Stream to access in-memory data to be parsed</param>
        /// <param name="mimeType">Mime-type of the stream to process</param>
        /// <param name="readEmbeddedPictures">Embedded pictures will be read if true; ignored if false</param>
        /// <param name="readAllMetaFrames">All metadata frames (including unmapped ones) will be read if true; ignored if false</param>
        /// <param name="writeProgress">Object to use to signal writing progress (optional)</param>
        public AudioFileIO(Stream stream, String mimeType, bool readEmbeddedPictures, bool readAllMetaFrames = false, IProgress <float> writeProgress = null)
        {
            byte alternate = 0;
            bool found     = false;

            audioData = AudioDataIOFactory.GetInstance().GetFromMimeType(mimeType, "In-memory", alternate);

            audioManager       = new AudioDataManager(audioData, stream, writeProgress);
            this.writeProgress = writeProgress;
            found = audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames);

            while (!found && alternate < AudioDataIOFactory.MAX_ALTERNATES)
            {
                alternate++;
                audioData    = AudioDataIOFactory.GetInstance().GetFromMimeType(mimeType, "In-memory", alternate);
                audioManager = new AudioDataManager(audioData, stream, writeProgress);
                found        = audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames);
            }

            metaData = MetaDataIOFactory.GetInstance().GetMetaReader(audioManager);

            if (metaData is DummyTag && (0 == audioManager.getAvailableMetas().Count))
            {
                LogDelegator.GetLogDelegate()(Log.LV_WARNING, "Could not find any metadata");
            }
        }
示例#2
0
        public bool RemoveTagFromFile(int tagType)
        {
            bool result = false;

            LogDelegator.GetLocateDelegate()(fileName);

            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, bufferSize, fileOptions))
                    using (BinaryReader reader = new BinaryReader(fs))
                    {
                        result = read(reader, false, false, true);

                        IMetaDataIO metaIO = getMeta(tagType);
                        if (metaIO.Exists)
                        {
                            using (BinaryWriter writer = new BinaryWriter(fs))
                            {
                                metaIO.Remove(writer);
                            }
                        }
                    }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                System.Console.WriteLine(e.StackTrace);
                LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
                result = false;
            }

            return(result);
        }
示例#3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="stream">Stream to access in-memory data to be parsed</param>
        /// <param name="mimeType">the file's mime type.</param>
        /// <param name="readEmbeddedPictures">Embedded pictures will be read if true; ignored if false</param>
        /// <param name="readAllMetaFrames">All metadata frames (including unmapped ones) will be read if true; ignored if false</param>
        public AudioFileIo(Stream stream, String mimeType, Boolean readEmbeddedPictures, Boolean readAllMetaFrames = false)
        {
            Byte alternate = 0;
            var  found     = false;

            _audioData = AudioDataIoFactory.GetInstance().GetFromMimeType(mimeType, "In-memory", alternate);

            _audioManager = new AudioDataManager(_audioData, stream);
            found         = _audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames);

            while (!found && alternate < AudioDataIoFactory.MaxAlternates)
            {
                alternate++;
                _audioData    = AudioDataIoFactory.GetInstance().GetFromMimeType(mimeType, "In-memory", alternate);
                _audioManager = new AudioDataManager(_audioData, stream);
                found         = _audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames);
            }

            _metaData = MetaDataIOFactory.GetInstance().GetMetaReader(_audioManager);

            if (_metaData is DummyTag && (0 == _audioManager.getAvailableMetas().Count))
            {
                LogDelegator.GetLogDelegate()(Log.LV_WARNING, "Could not find any metadata");
            }
        }
示例#4
0
        public void TagIO_R_ID3v2_WXXX_ManualUpdate()
        {
            string           testFileLocation = TestUtils.CopyAsTempTestFile("MP3/id3v2.4_UTF8.mp3");
            AudioDataManager theFile          = new AudioDataManager(ATL.AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            TagData theTag = new TagData();

            theTag.AdditionalFields = new List <MetaFieldInfo>();
            MetaFieldInfo info = new MetaFieldInfo(MetaDataIOFactory.TAG_ID3V2, "WXXX", "http://justtheurl.com");

            theTag.AdditionalFields.Add(info);

            Assert.IsTrue(theFile.UpdateTagInFile(theTag, tagType));

            Assert.IsTrue(theFile.ReadFromFile(false, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            IMetaDataIO meta = theFile.getMeta(tagType);

            Assert.IsTrue(meta.Exists);

            Assert.IsTrue(meta.AdditionalFields.ContainsKey("WXXX"));
            Assert.AreEqual("http://justtheurl.com", meta.AdditionalFields["WXXX"].Split(Settings.InternalValueSeparator)[1]);

            File.Delete(testFileLocation);
        }
示例#5
0
        // ------------------------------------------------------------------------------------------

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="path">Path of the file to be parsed</param>
        public AudioFileIO(string path, bool readEmbeddedPictures, bool readAllMetaFrames = false)
        {
            byte alternate = 0;
            bool found     = false;

            thePath = path;

            audioData    = AudioDataIOFactory.GetInstance().GetDataReader(path, alternate);
            audioManager = new AudioDataManager(audioData);
            found        = audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames);

            while (!found && alternate < AudioDataIOFactory.MAX_ALTERNATES)
            {
                alternate++;
                audioData    = AudioDataIOFactory.GetInstance().GetDataReader(path, alternate);
                audioManager = new AudioDataManager(audioData);
                found        = audioManager.ReadFromFile(readEmbeddedPictures, readAllMetaFrames);
            }

            metaData = MetaDataIOFactory.GetInstance().GetMetaReader(audioManager);

            if (metaData is DummyTag && (0 == audioManager.getAvailableMetas().Count))
            {
                LogDelegator.GetLogDelegate()(Log.LV_WARNING, "Could not find any metadata");
            }
        }
示例#6
0
        /// <summary>
        /// Gets the appropriate metadata reader for a given file / physical data reader
        /// </summary>
        /// <param name="theDataManager">AudioDataReader produced for this file</param>
        /// <param name="forceTagType">Forces a certain tag type to be read regardless of the current "cross reading" settings</param>
        /// <returns>Metadata reader able to give metadata info for this file (or the dummy reader if the format is unknown)</returns>
        public IMetaDataIO GetMetaReader(AudioDataManager theDataManager, int forceTagType = -1)
        {
            IMetaDataIO theMetaReader = null;

            int tagCount = 0;

            if (theDataManager.HasNativeMeta())
            {
                tagCount++;
            }
            if (theDataManager.ID3v1.Exists)
            {
                tagCount++;
            }
            if (theDataManager.ID3v2.Exists)
            {
                tagCount++;
            }
            if (theDataManager.APEtag.Exists)
            {
                tagCount++;
            }

            if (m_enableCrossReading && (tagCount > 1) && (-1 == forceTagType))
            {
                theMetaReader = new CrossMetadataReader(theDataManager, tagPriority);
            }
            else
            {
                for (int i = 0; i < TAG_TYPE_COUNT; i++)
                {
                    if (((TAG_NATIVE == tagPriority[i] && -1 == forceTagType) || (TAG_NATIVE == forceTagType)) && (theDataManager.HasNativeMeta()))
                    {
                        theMetaReader = theDataManager.NativeTag; break;
                    }
                    if (((TAG_ID3V1 == tagPriority[i] && -1 == forceTagType) || (TAG_ID3V1 == forceTagType)) && (theDataManager.ID3v1.Exists))
                    {
                        theMetaReader = theDataManager.ID3v1; break;
                    }
                    if (((TAG_ID3V2 == tagPriority[i] && -1 == forceTagType) || (TAG_ID3V2 == forceTagType)) && (theDataManager.ID3v2.Exists))
                    {
                        theMetaReader = theDataManager.ID3v2; break;
                    }
                    if (((TAG_APE == tagPriority[i] && -1 == forceTagType) || (TAG_APE == forceTagType)) && (theDataManager.APEtag.Exists))
                    {
                        theMetaReader = theDataManager.APEtag; break;
                    }
                }
            }

            if (null == theMetaReader)
            {
                theMetaReader = new IO.DummyTag();
            }

            return(theMetaReader);
        }
示例#7
0
        public void TagIO_R_VorbisFLAC_multipleArtists()
        {
            new ConsoleLogger();

            string           fileName         = "FLAC/multiple_artists.flac";
            string           location         = TestUtils.GetResourceLocationRoot() + fileName;
            string           testFileLocation = TestUtils.CopyAsTempTestFile(fileName);
            AudioDataManager theFile          = new AudioDataManager(AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            Assert.IsTrue(theFile.ReadFromFile(true, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            IMetaDataIO meta = theFile.getMeta(tagType);

            Assert.IsTrue(meta.Exists);

            // Read
            Assert.AreEqual("lovesick (feat. Punipuni Denki)", meta.Title);
            Assert.AreEqual("Kamome Sano" + ATL.Settings.InternalValueSeparator + "Punipuni Denki", meta.Artist);

            // Write same data and keep initial format
            TagData theTag = new TagData();

            theTag.Artist = "Kamome Sano" + ATL.Settings.DisplayValueSeparator + "Punipuni Denki";
            Assert.IsTrue(theFile.UpdateTagInFile(theTag, MetaDataIOFactory.TAG_NATIVE));

            // Check that the resulting file (working copy that has been tagged, then untagged) remains identical to the original file (i.e. no byte lost nor added)
            FileInfo originalFileInfo = new FileInfo(location);
            FileInfo testFileInfo     = new FileInfo(testFileLocation);

            Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);

            // Can't test with MD5 here because of field order

            // Write and modify
            theTag        = new TagData();
            theTag.Artist = "aaa" + ATL.Settings.DisplayValueSeparator + "bbb" + ATL.Settings.DisplayValueSeparator + "ccc";
            Assert.IsTrue(theFile.UpdateTagInFile(theTag, MetaDataIOFactory.TAG_NATIVE));

            // Read again
            Assert.IsTrue(theFile.ReadFromFile(true, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            meta = theFile.getMeta(tagType);
            Assert.IsTrue(meta.Exists);

            Assert.AreEqual("aaa" + ATL.Settings.InternalValueSeparator + "bbb" + ATL.Settings.InternalValueSeparator + "ccc", meta.Artist);

            // Get rid of the working copy
            if (Settings.DeleteAfterSuccess)
            {
                File.Delete(testFileLocation);
            }
        }
示例#8
0
        protected void assumeRatingInFile(string file, double rating, int tagType)
        {
            string           location = TestUtils.GetResourceLocationRoot() + file;
            AudioDataManager theFile  = new AudioDataManager(ATL.AudioData.AudioDataIOFactory.GetInstance().GetFromPath(location));

            Assert.IsTrue(theFile.ReadFromFile());

            IMetaDataIO meta = theFile.getMeta(tagType);

            Assert.IsNotNull(meta);
            Assert.IsTrue(meta.Exists);

            //Assert.IsTrue(Math.Abs(rating - meta.Popularity) < 0.01);
            Assert.AreEqual((float)rating, meta.Popularity);
        }
示例#9
0
        public bool RemoveTagFromFile(int tagType)
        {
            bool result = false;

            LogDelegator.GetLocateDelegate()(fileName);

            try
            {
                Stream       s      = (null == stream) ? new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, bufferSize, fileOptions) : stream;
                BinaryReader reader = new BinaryReader(s);
                BinaryWriter writer = null;
                try
                {
                    result = read(reader, false, false, true);

                    IMetaDataIO metaIO = getMeta(tagType);
                    if (metaIO.Exists)
                    {
                        writer = new BinaryWriter(s);
                        metaIO.Remove(writer);
                    }
                } finally
                {
                    if (null == stream)
                    {
                        reader.Close();
                        if (writer != null)
                        {
                            writer.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                System.Console.WriteLine(e.StackTrace);
                LogDelegator.GetLogDelegate()(Log.LV_ERROR, e.Message);
                result = false;
            }

            return(result);
        }
示例#10
0
 public void setMeta(IMetaDataIO meta)
 {
     if (meta is ID3v1)
     {
         iD3v1 = meta;
         sizeInfo.SetSize(MetaDataIOFactory.TAG_ID3V1, iD3v1.Size);
     }
     else if (meta is ID3v2)
     {
         iD3v2 = meta;
         sizeInfo.SetSize(MetaDataIOFactory.TAG_ID3V2, iD3v2.Size);
     }
     else if (meta is APEtag)
     {
         aPEtag = meta;
         sizeInfo.SetSize(MetaDataIOFactory.TAG_APE, aPEtag.Size);
     }
     else
     {
         nativeTag = meta;
         sizeInfo.SetSize(MetaDataIOFactory.TAG_NATIVE, nativeTag.Size);
     }
 }
示例#11
0
        protected void test_RW_Cohabitation(int tagType1, int tagType2, bool canMeta1NotExist = true)
        {
            ConsoleLogger log = new ConsoleLogger();

            // Source : empty file
            string           location         = TestUtils.GetResourceLocationRoot() + emptyFile;
            string           testFileLocation = TestUtils.CopyAsTempTestFile(emptyFile);
            AudioDataManager theFile          = new AudioDataManager(ATL.AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            // Check that it is indeed tag-free
            Assert.IsTrue(theFile.ReadFromFile());

            IMetaDataIO meta1 = theFile.getMeta(tagType1);
            IMetaDataIO meta2 = theFile.getMeta(tagType2);

            Assert.IsNotNull(meta1);
            if (canMeta1NotExist)
            {
                Assert.IsFalse(meta1.Exists);
            }
            Assert.IsNotNull(meta2);
            Assert.IsFalse(meta2.Exists);

            long initialPaddingSize1 = meta1.PaddingSize;


            // Construct a new tag with the most basic options (no un supported fields, no pictures)
            TagData theTag1 = new TagData();

            theTag1.Title = "Test1";
            theTag1.Album = "Album1";

            TagData theTag2 = new TagData();

            theTag2.Title = "Test2";
            theTag2.Album = "Album2";

            // Add the new tag and check that it has been indeed added with all the correct information
            Assert.IsTrue(theFile.UpdateTagInFile(theTag1, tagType1));
            Assert.IsTrue(theFile.UpdateTagInFile(theTag2, tagType2));

            // This also tests if physical data can still be read (e.g. native tag has not been scrambled by the apparition of a non-native tag)
            Assert.IsTrue(theFile.ReadFromFile());

            meta1 = theFile.getMeta(tagType1);
            meta2 = theFile.getMeta(tagType2);

            Assert.IsNotNull(meta1);
            Assert.IsTrue(meta1.Exists);

            Assert.IsNotNull(meta2);
            Assert.IsTrue(meta2.Exists);

            Assert.AreEqual("Test1", meta1.Title);
            Assert.AreEqual("Album1", meta1.Album);

            Assert.AreEqual("Test2", meta2.Title);
            Assert.AreEqual("Album2", meta2.Album);

            Assert.IsTrue(theFile.RemoveTagFromFile(tagType1));
            Assert.IsTrue(theFile.ReadFromFile());

            meta1 = theFile.getMeta(tagType1);
            meta2 = theFile.getMeta(tagType2);

            Assert.IsNotNull(meta1);
            if (canMeta1NotExist)
            {
                Assert.IsFalse(meta1.Exists);
            }

            Assert.IsNotNull(meta2);
            Assert.IsTrue(meta2.Exists);

            Assert.AreEqual("Test2", meta2.Title);
            Assert.AreEqual("Album2", meta2.Album);

            Assert.IsTrue(theFile.RemoveTagFromFile(tagType2));
            Assert.IsTrue(theFile.ReadFromFile());

            meta1 = theFile.getMeta(tagType1);
            meta2 = theFile.getMeta(tagType2);

            Assert.IsNotNull(meta1);
            if (canMeta1NotExist)
            {
                Assert.IsFalse(meta1.Exists);
            }

            Assert.IsNotNull(meta2);
            Assert.IsFalse(meta2.Exists);

            // Restore initial padding
            TagData theTagFinal = new TagData();

            theTagFinal.PaddingSize = initialPaddingSize1;
            Assert.IsTrue(theFile.UpdateTagInFile(theTagFinal, tagType1));

            // Check that the resulting file (working copy that has been tagged, then untagged) remains identical to the original file (i.e. no byte lost nor added)
            FileInfo originalFileInfo = new FileInfo(location);
            FileInfo testFileInfo     = new FileInfo(testFileLocation);

            Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);

            string originalMD5 = TestUtils.GetFileMD5Hash(location);
            string testMD5     = TestUtils.GetFileMD5Hash(testFileLocation);

            Assert.AreEqual(originalMD5, testMD5);

            // Get rid of the working copy
            File.Delete(testFileLocation);
        }
示例#12
0
        protected void readExistingTagsOnFile(AudioDataManager theFile, int nbPictures = 2)
        {
            Assert.IsTrue(theFile.ReadFromFile(true, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            IMetaDataIO meta = theFile.getMeta(tagType);

            Assert.IsTrue(meta.Exists);

            // Supported fields
            if (testData.Title != null)
            {
                Assert.AreEqual(testData.Title, meta.Title);
            }
            if (testData.Album != null)
            {
                Assert.AreEqual(testData.Album, meta.Album);
            }
            if (testData.Artist != null)
            {
                Assert.AreEqual(testData.Artist, meta.Artist);
            }
            if (testData.AlbumArtist != null)
            {
                Assert.AreEqual(testData.AlbumArtist, meta.AlbumArtist);
            }
            if (testData.Comment != null)
            {
                Assert.AreEqual(testData.Comment, meta.Comment);
            }
            if (!supportsDateOrYear)
            {
                if (testData.RecordingYear != null)
                {
                    Assert.AreEqual(testData.RecordingYear, meta.Year);
                }
                if (testData.RecordingDate != null)
                {
                    DateTime date;
                    Assert.IsTrue(DateTime.TryParse(testData.RecordingDate, out date));
                    Assert.AreEqual(date, meta.Date);
                }
                if (testData.PublishingDate != null)
                {
                    DateTime date;
                    Assert.IsTrue(DateTime.TryParse(testData.PublishingDate, out date));
                    Assert.AreEqual(date, meta.PublishingDate);
                }
            }
            else
            {
                Assert.IsTrue(meta.Year != null || (meta.Date != null && meta.Date > DateTime.MinValue));
                if (meta.Year != null && testData.RecordingYear != null)
                {
                    Assert.AreEqual(testData.RecordingYear, meta.Year);
                }
                if (meta.Date != null && meta.Date > DateTime.MinValue && testData.RecordingDate != null)
                {
                    DateTime date;
                    Assert.IsTrue(DateTime.TryParse(testData.RecordingDate, out date));
                    Assert.AreEqual(date, meta.Date);
                }
            }
            if (testData.Genre != null)
            {
                Assert.AreEqual(testData.Genre, meta.Genre);
            }
            if (testData.Rating != null)
            {
                if (Utils.IsNumeric(testData.Rating))
                {
                    float f = float.Parse(testData.Rating);
                    Assert.AreEqual((f / 5.0).ToString(), meta.Popularity.ToString());
                }
                else if (0 == testData.Rating.Length)
                {
                    Assert.AreEqual("0", meta.Popularity.ToString());
                }
                else
                {
                    Assert.AreEqual(testData.Rating, meta.Popularity.ToString());
                }
            }
            if (testData.TrackNumber != null)
            {
                Assert.AreEqual(ushort.Parse(testData.TrackNumber), meta.Track);
            }
            if (testData.TrackTotal != null)
            {
                Assert.AreEqual(ushort.Parse(testData.TrackTotal), meta.TrackTotal);
            }
            if (testData.Composer != null)
            {
                Assert.AreEqual(testData.Composer, meta.Composer);
            }
            if (testData.DiscNumber != null)
            {
                Assert.AreEqual(ushort.Parse(testData.DiscNumber), meta.Disc);
            }
            if (testData.DiscTotal != null)
            {
                Assert.AreEqual(ushort.Parse(testData.DiscTotal), meta.DiscTotal);
            }
            if (testData.Conductor != null)
            {
                Assert.AreEqual(testData.Conductor, meta.Conductor);
            }
            if (testData.Publisher != null)
            {
                Assert.AreEqual(testData.Publisher, meta.Publisher);
            }
            if (testData.Copyright != null)
            {
                Assert.AreEqual(testData.Copyright, meta.Copyright);
            }
            if (testData.GeneralDescription != null)
            {
                Assert.AreEqual(testData.GeneralDescription, meta.GeneralDescription);
            }

            // Unsupported field
            if (testData.AdditionalFields != null && testData.AdditionalFields.Count > 0)
            {
                foreach (MetaFieldInfo field in testData.AdditionalFields)
                {
                    Assert.IsTrue(meta.AdditionalFields.Keys.Contains(field.NativeFieldCode));
                    Assert.AreEqual(field.Value, meta.AdditionalFields[field.NativeFieldCode]);
                }
            }

            // Pictures
            if (testData.Pictures != null && testData.Pictures.Count > 0)
            {
                Assert.AreEqual(nbPictures, meta.EmbeddedPictures.Count);

                byte nbFound = 0;
                foreach (PictureInfo pic in meta.EmbeddedPictures)
                {
                    foreach (PictureInfo testPicInfo in testData.Pictures)
                    {
                        if ((pic.NativePicCode > -1 && pic.NativePicCode.Equals(testPicInfo.NativePicCode)) ||
                            (pic.NativePicCodeStr != null && pic.NativePicCodeStr.Equals(testPicInfo.NativePicCodeStr, System.StringComparison.OrdinalIgnoreCase))
                            )
                        {
                            nbFound++;
                            pic.ComputePicHash();
                            Assert.AreEqual(testPicInfo.PictureHash, pic.PictureHash);
                            break;
                        }
                    }
                }
                Assert.AreEqual(testData.Pictures.Count, nbFound);
            }
        }
示例#13
0
        public void test_RW_Unsupported_Empty(string fileName, bool deleteTempFile = true)
        {
            ConsoleLogger log = new ConsoleLogger();

            // Source : totally metadata-free file
            string           location         = TestUtils.GetResourceLocationRoot() + fileName;
            string           testFileLocation = TestUtils.CopyAsTempTestFile(fileName);
            AudioDataManager theFile          = new AudioDataManager(ATL.AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            // Check that it is indeed tag-free
            Assert.IsTrue(theFile.ReadFromFile());

            Assert.IsNotNull(theFile.getMeta(tagType));
            IMetaDataIO meta = theFile.getMeta(tagType);

            if (canMetaNotExist)
            {
                Assert.IsFalse(meta.Exists);
            }


            bool handleUnsupportedFields   = (testData.AdditionalFields != null && testData.AdditionalFields.Count > 0);
            bool handleUnsupportedPictures = (testData.Pictures != null && testData.Pictures.Count > 0);
            char internationalChar         = supportsInternationalChars ? '父' : '!';

            // Add new unsupported fields
            TagData theTag = new TagData();

            if (handleUnsupportedFields)
            {
                theTag.AdditionalFields.Add(new MetaFieldInfo(tagType, "TEST", "This is a test " + internationalChar));
                theTag.AdditionalFields.Add(new MetaFieldInfo(tagType, "TES2", "This is another test " + internationalChar));
            }

            // Add new unsupported pictures
            PictureInfo picInfo = null;
            byte        found   = 0;

            object pictureCode1, pictureCode2;

            if (tagType.Equals(MetaDataIOFactory.TAG_APE))
            {
                pictureCode1 = "pic1";
                pictureCode2 = "pic2";
            }
            else
            {
                pictureCode1 = 23;
                pictureCode2 = 24;
            }

            if (handleUnsupportedPictures)
            {
                byte[] data = File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.jpg");
                picInfo = PictureInfo.fromBinaryData(data, PIC_TYPE.Unsupported, tagType, pictureCode1);
                theTag.Pictures.Add(picInfo);

                data    = File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic2.jpg");
                picInfo = PictureInfo.fromBinaryData(data, PIC_TYPE.Unsupported, tagType, pictureCode2);
                theTag.Pictures.Add(picInfo);
            }

            theFile.UpdateTagInFile(theTag, tagType);

            Assert.IsTrue(theFile.ReadFromFile(true, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            meta = theFile.getMeta(tagType);
            Assert.IsTrue(meta.Exists);

            if (handleUnsupportedFields)
            {
                Assert.AreEqual(2, meta.AdditionalFields.Count);

                Assert.IsTrue(meta.AdditionalFields.Keys.Contains("TEST"));
                Assert.AreEqual("This is a test " + internationalChar, meta.AdditionalFields["TEST"]);

                Assert.IsTrue(meta.AdditionalFields.Keys.Contains("TES2"));
                Assert.AreEqual("This is another test " + internationalChar, meta.AdditionalFields["TES2"]);
            }

            if (handleUnsupportedPictures)
            {
                Assert.AreEqual(2, meta.EmbeddedPictures.Count);
                found = 0;

                foreach (PictureInfo pic in meta.EmbeddedPictures)
                {
                    if (pic.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) &&
                        (pic.NativePicCode.Equals(pictureCode1) || (pic.NativePicCodeStr != null && pic.NativePicCodeStr.Equals(pictureCode1)))
                        )
                    {
                        using (Image picture = Image.FromStream(new MemoryStream(pic.PictureData)))
                        {
                            Assert.AreEqual(picture.RawFormat, System.Drawing.Imaging.ImageFormat.Jpeg);
                            Assert.AreEqual(600, picture.Height);
                            Assert.AreEqual(900, picture.Width);
                        }
                        found++;
                    }
                    else if (pic.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) &&
                             (pic.NativePicCode.Equals(pictureCode2) || (pic.NativePicCodeStr != null && pic.NativePicCodeStr.Equals(pictureCode2)))
                             )
                    {
                        using (Image picture = Image.FromStream(new MemoryStream(pic.PictureData)))
                        {
                            Assert.AreEqual(picture.RawFormat, System.Drawing.Imaging.ImageFormat.Jpeg);
                            Assert.AreEqual(290, picture.Height);
                            Assert.AreEqual(900, picture.Width);
                        }
                        found++;
                    }
                }

                Assert.AreEqual(2, found);
            }

            // Remove the additional unsupported field
            if (handleUnsupportedFields)
            {
                theTag = new TagData();
                MetaFieldInfo fieldInfo = new MetaFieldInfo(tagType, "TEST");
                fieldInfo.MarkedForDeletion = true;
                theTag.AdditionalFields.Add(fieldInfo);
            }

            // Remove additional picture
            if (handleUnsupportedPictures)
            {
                picInfo = new PictureInfo(tagType, pictureCode1);
                picInfo.MarkedForDeletion = true;
                theTag.Pictures.Add(picInfo);
            }

            // Add the new tag and check that it has been indeed added with all the correct information
            Assert.IsTrue(theFile.UpdateTagInFile(theTag, tagType));

            Assert.IsTrue(theFile.ReadFromFile(true, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            meta = theFile.getMeta(tagType);
            Assert.IsTrue(meta.Exists);

            if (handleUnsupportedFields)
            {
                // Additional removed field
                Assert.AreEqual(1, meta.AdditionalFields.Count);
                Assert.IsTrue(meta.AdditionalFields.Keys.Contains("TES2"));
                Assert.AreEqual("This is another test " + internationalChar, meta.AdditionalFields["TES2"]);
            }

            // Pictures
            if (handleUnsupportedPictures)
            {
                Assert.AreEqual(1, meta.EmbeddedPictures.Count);

                found = 0;

                foreach (PictureInfo pic in meta.EmbeddedPictures)
                {
                    if (pic.PicType.Equals(PictureInfo.PIC_TYPE.Unsupported) &&
                        (pic.NativePicCode.Equals(pictureCode2) || (pic.NativePicCodeStr != null && pic.NativePicCodeStr.Equals(pictureCode2)))
                        )
                    {
                        using (Image picture = Image.FromStream(new MemoryStream(pic.PictureData)))
                        {
                            Assert.AreEqual(picture.RawFormat, System.Drawing.Imaging.ImageFormat.Jpeg);
                            Assert.AreEqual(290, picture.Height);
                            Assert.AreEqual(900, picture.Width);
                        }
                        found++;
                    }
                }

                Assert.AreEqual(1, found);
            }

            // Get rid of the working copy
            if (deleteTempFile)
            {
                File.Delete(testFileLocation);
            }
        }
示例#14
0
        public void test_RW_Empty(string fileName, bool deleteTempFile = true, bool sameSizeAfterEdit = false, bool sameBitsAfterEdit = false)
        {
            ConsoleLogger log = new ConsoleLogger();

            // Source : totally metadata-free file
            string           location         = TestUtils.GetResourceLocationRoot() + fileName;
            string           testFileLocation = TestUtils.CopyAsTempTestFile(fileName);
            AudioDataManager theFile          = new AudioDataManager(ATL.AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));


            // Check that it is indeed metadata-free
            Assert.IsTrue(theFile.ReadFromFile());

            Assert.IsNotNull(theFile.getMeta(tagType));
            if (canMetaNotExist)
            {
                Assert.IsFalse(theFile.getMeta(tagType).Exists);
            }

            long initialPaddingSize = theFile.getMeta(tagType).PaddingSize;


            char internationalChar = supportsInternationalChars ? '父' : '!';

            // Construct a new tag
            TagData theTag = new TagData();

            if (testData.Title != null)
            {
                theTag.Title = "Test !!";
            }
            if (testData.Album != null)
            {
                theTag.Album = "Album";
            }
            if (testData.Artist != null)
            {
                theTag.Artist = "Artist";
            }
            if (testData.AlbumArtist != null)
            {
                theTag.AlbumArtist = "Mike";
            }
            if (testData.Comment != null)
            {
                theTag.Comment = "This is a test";
            }
            if (testData.RecordingYear != null)
            {
                theTag.RecordingYear = "2008";
            }
            if (testData.RecordingDate != null)
            {
                theTag.RecordingDate = "2008/01/01";
            }
            if (testData.PublishingDate != null)
            {
                theTag.PublishingDate = "2007/02/02";
            }
            if (testData.Genre != null)
            {
                theTag.Genre = "Merengue";
            }
            if (testData.Rating != null)
            {
                theTag.Rating = 2.5.ToString();
            }
            if (testData.TrackNumber != null)
            {
                theTag.TrackNumber = "01";
            }
            if (testData.TrackTotal != null)
            {
                theTag.TrackTotal = "02";
            }
            if (testData.DiscNumber != null)
            {
                theTag.DiscNumber = "03";
            }
            if (testData.DiscTotal != null)
            {
                theTag.DiscTotal = "04";
            }
            if (testData.Composer != null)
            {
                theTag.Composer = "Me";
            }
            if (testData.Copyright != null)
            {
                theTag.Copyright = "a" + internationalChar + "a";
            }
            if (testData.Conductor != null)
            {
                theTag.Conductor = "John Johnson Jr.";
            }
            if (testData.Publisher != null)
            {
                theTag.Publisher = "Z Corp.";
            }
            if (testData.GeneralDescription != null)
            {
                theTag.GeneralDescription = "Description";
            }

            if (testData.AdditionalFields != null && testData.AdditionalFields.Count > 0)
            {
                theTag.AdditionalFields = new List <MetaFieldInfo>();
                foreach (MetaFieldInfo info in testData.AdditionalFields)
                {
                    theTag.AdditionalFields.Add(info);
                }
            }

            // Add the new tag and check that it has been indeed added with all the correct information
            Assert.IsTrue(theFile.UpdateTagInFile(theTag, tagType));

            Assert.IsTrue(theFile.ReadFromFile(false, true));

            Assert.IsNotNull(theFile.getMeta(tagType));
            IMetaDataIO meta = theFile.getMeta(tagType);

            Assert.IsTrue(meta.Exists);

            if (testData.Title != null)
            {
                Assert.AreEqual("Test !!", meta.Title);
            }
            if (testData.Album != null)
            {
                Assert.AreEqual("Album", meta.Album);
            }
            if (testData.Artist != null)
            {
                Assert.AreEqual("Artist", meta.Artist);
            }
            if (testData.AlbumArtist != null)
            {
                Assert.AreEqual("Mike", meta.AlbumArtist);
            }
            if (testData.Comment != null)
            {
                Assert.AreEqual("This is a test", meta.Comment);
            }
            if (!supportsDateOrYear)
            {
                if (testData.RecordingYear != null)
                {
                    Assert.AreEqual("2008", meta.Year);
                }
                if (testData.RecordingDate != null)
                {
                    DateTime date;
                    Assert.IsTrue(DateTime.TryParse("2008/01/01", out date));
                    Assert.AreEqual(date, meta.Date);
                }
                if (testData.PublishingDate != null)
                {
                    DateTime date;
                    Assert.IsTrue(DateTime.TryParse("2007/02/02", out date));
                    Assert.AreEqual(date, meta.PublishingDate);
                }
            }
            else
            {
                Assert.IsTrue(meta.Year != null || (meta.Date != null && meta.Date > DateTime.MinValue));
                if (meta.Year != null && testData.RecordingYear != null)
                {
                    Assert.AreEqual("2008", meta.Year);
                }
                if (meta.Date != null && meta.Date > DateTime.MinValue && testData.RecordingDate != null)
                {
                    DateTime date;
                    Assert.IsTrue(DateTime.TryParse("2008/01/01", out date));
                    Assert.AreEqual(date, meta.Date);
                }
            }
            if (testData.Genre != null)
            {
                Assert.AreEqual("Merengue", meta.Genre);
            }
            if (testData.Rating != null)
            {
                Assert.AreEqual((float)(2.5 / 5), meta.Popularity);
            }
            if (testData.TrackNumber != null)
            {
                Assert.AreEqual(1, meta.Track);
            }
            if (testData.TrackTotal != null)
            {
                Assert.AreEqual(2, meta.TrackTotal);
            }
            if (testData.DiscNumber != null)
            {
                Assert.AreEqual(3, meta.Disc);
            }
            if (testData.DiscTotal != null)
            {
                Assert.AreEqual(4, meta.DiscTotal);
            }
            if (testData.Composer != null)
            {
                Assert.AreEqual("Me", meta.Composer);
            }
            if (testData.Copyright != null)
            {
                Assert.AreEqual("a" + internationalChar + "a", meta.Copyright);
            }
            if (testData.Conductor != null)
            {
                Assert.AreEqual("John Johnson Jr.", meta.Conductor);
            }
            if (testData.Publisher != null)
            {
                Assert.AreEqual("Z Corp.", meta.Publisher);
            }
            if (testData.GeneralDescription != null)
            {
                Assert.AreEqual("Description", meta.GeneralDescription);
            }

            if (testData.AdditionalFields != null && testData.AdditionalFields.Count > 0)
            {
                foreach (MetaFieldInfo info in testData.AdditionalFields)
                {
                    Assert.IsTrue(meta.AdditionalFields.ContainsKey(info.NativeFieldCode));
                    Assert.AreEqual(info.Value, meta.AdditionalFields[info.NativeFieldCode]);
                }
            }

            // Remove the tag and check that it has been indeed removed
            Assert.IsTrue(theFile.RemoveTagFromFile(tagType));

            if (initialPaddingSize > 0)
            {
                TagData paddingRestore = new TagData();
                paddingRestore.PaddingSize = initialPaddingSize;
                Assert.IsTrue(theFile.UpdateTagInFile(paddingRestore, tagType));
            }

            Assert.IsTrue(theFile.ReadFromFile());

            Assert.IsNotNull(theFile.getMeta(tagType));
            if (canMetaNotExist)
            {
                Assert.IsFalse(theFile.getMeta(tagType).Exists);
            }


            // Check that the resulting file (working copy that has been tagged, then untagged) remains identical to the original file (i.e. no byte lost nor added)
            if (sameSizeAfterEdit || sameBitsAfterEdit)
            {
                FileInfo originalFileInfo = new FileInfo(location);
                FileInfo testFileInfo     = new FileInfo(testFileLocation);

                if (sameSizeAfterEdit)
                {
                    Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);
                }

                if (sameBitsAfterEdit)
                {
                    string originalMD5 = TestUtils.GetFileMD5Hash(location);
                    string testMD5     = TestUtils.GetFileMD5Hash(testFileLocation);

                    Assert.AreEqual(originalMD5, testMD5);
                }
            }

            // Get rid of the working copy
            if (deleteTempFile)
            {
                File.Delete(testFileLocation);
            }
        }
示例#15
0
        protected void test_RW_Existing(string fileName, int initialNbPictures, bool deleteTempFile = true, bool sameSizeAfterEdit = false, bool sameBitsAfterEdit = false)
        {
            ConsoleLogger log = new ConsoleLogger();

            // Source : file with existing tag incl. unsupported picture (Conductor); unsupported field (MOOD)
            string           location         = TestUtils.GetResourceLocationRoot() + fileName;
            string           testFileLocation = TestUtils.CopyAsTempTestFile(fileName);
            AudioDataManager theFile          = new AudioDataManager(ATL.AudioData.AudioDataIOFactory.GetInstance().GetFromPath(testFileLocation));

            // Add a new supported field and a new supported picture
            Assert.IsTrue(theFile.ReadFromFile());

            TagData theTag = new TagData();

            char internationalChar = supportsInternationalChars ? '父' : '!';

            TagData initialTestData = new TagData(testData);

            // These two cases cover all tag capabilities
            if (testData.Title != null)
            {
                theTag.Title = "Test !!" + internationalChar;
            }
            else if (testData.GeneralDescription != null)
            {
                theTag.GeneralDescription = "Description" + internationalChar;
            }

            if (testData.AdditionalFields != null && testData.AdditionalFields.Count > 0)
            {
                theTag.AdditionalFields = new List <MetaFieldInfo>();
                foreach (MetaFieldInfo info in testData.AdditionalFields)
                {
                    theTag.AdditionalFields.Add(info);
                    break; // 1 is enough
                }
            }
            testData = new TagData(theTag);

            PictureInfo picInfo = PictureInfo.fromBinaryData(File.ReadAllBytes(TestUtils.GetResourceLocationRoot() + "_Images/pic1.jpg"), PictureInfo.PIC_TYPE.CD);

            theTag.Pictures.Add(picInfo);


            // Add the new tag and check that it has been indeed added with all the correct information
            Assert.IsTrue(theFile.UpdateTagInFile(theTag, tagType));

            readExistingTagsOnFile(theFile, initialNbPictures + 1);
            Assert.IsNotNull(theFile.getMeta(tagType));
            IMetaDataIO meta = theFile.getMeta(tagType);

            Assert.IsTrue(meta.Exists);

            if (testData.Pictures != null && testData.Pictures.Count > 0)
            {
                int nbFound = 0;
                foreach (PictureInfo pic in meta.EmbeddedPictures)
                {
                    if (pic.PicType.Equals(PictureInfo.PIC_TYPE.CD))
                    {
                        if (tagType.Equals(MetaDataIOFactory.TAG_APE))
                        {
                            Assert.AreEqual("Cover Art (Media)", pic.NativePicCodeStr);
                        }
                        else // ID3v2 convention
                        {
                            Assert.AreEqual(0x06, pic.NativePicCode);
                        }
                        using (Image picture = Image.FromStream(new MemoryStream(pic.PictureData)))
                        {
                            Assert.AreEqual(picture.RawFormat, System.Drawing.Imaging.ImageFormat.Jpeg);
                            Assert.AreEqual(600, picture.Height);
                            Assert.AreEqual(900, picture.Width);
                        }
                        nbFound++;
                        break;
                    }
                }

                Assert.AreEqual(1, nbFound);
            }

            // Remove the additional supported field
            theTag   = new TagData(initialTestData);
            testData = new TagData(initialTestData);

            // Remove additional picture
            picInfo = new PictureInfo(PictureInfo.PIC_TYPE.CD);
            picInfo.MarkedForDeletion = true;
            theTag.Pictures.Add(picInfo);

            // Add the new tag and check that it has been indeed added with all the correct information
            Assert.IsTrue(theFile.UpdateTagInFile(theTag, tagType));

            readExistingTagsOnFile(theFile, initialNbPictures);

            // Additional removed field
            Assert.AreEqual("", theFile.getMeta(tagType).Conductor);


            // Check that the resulting file (working copy that has been tagged, then untagged) remains identical to the original file (i.e. no byte lost nor added)
            if (sameSizeAfterEdit || sameBitsAfterEdit)
            {
                FileInfo originalFileInfo = new FileInfo(location);
                FileInfo testFileInfo     = new FileInfo(testFileLocation);

                if (sameSizeAfterEdit)
                {
                    Assert.AreEqual(originalFileInfo.Length, testFileInfo.Length);
                }

                if (sameBitsAfterEdit)
                {
                    string originalMD5 = TestUtils.GetFileMD5Hash(location);
                    string testMD5     = TestUtils.GetFileMD5Hash(testFileLocation);

                    Assert.AreEqual(originalMD5, testMD5);
                }
            }

            // Get rid of the working copy
            if (deleteTempFile)
            {
                File.Delete(testFileLocation);
            }
        }
示例#16
0
        private bool read(BinaryReader source, MetaDataIO.ReadTagParams readTagParams)
        {
            bool result = false;

            if (audioDataIO.IsMetaSupported(MetaDataIOFactory.TAG_ID3V1))
            {
                if (iD3v1.Read(source, readTagParams))
                {
                    sizeInfo.TagSizes.Add(MetaDataIOFactory.TAG_ID3V1, iD3v1.Size);
                }
            }
            if (audioDataIO.IsMetaSupported(MetaDataIOFactory.TAG_ID3V2))
            {
                if (!(audioDataIO is IMetaDataEmbedder)) // No embedded ID3v2 tag => supported tag is the standard version of ID3v2
                {
                    if (iD3v2.Read(source, readTagParams))
                    {
                        sizeInfo.TagSizes.Add(MetaDataIOFactory.TAG_ID3V2, iD3v2.Size);
                    }
                }
            }
            if (audioDataIO.IsMetaSupported(MetaDataIOFactory.TAG_APE))
            {
                if (aPEtag.Read(source, readTagParams))
                {
                    sizeInfo.TagSizes.Add(MetaDataIOFactory.TAG_APE, aPEtag.Size);
                }
            }

            if (audioDataIO.IsMetaSupported(MetaDataIOFactory.TAG_NATIVE) && audioDataIO is IMetaDataIO)
            {
                IMetaDataIO nativeTag = (IMetaDataIO)audioDataIO;
                this.nativeTag = nativeTag;
                result         = audioDataIO.Read(source, sizeInfo, readTagParams);

                if (result)
                {
                    sizeInfo.TagSizes.Add(MetaDataIOFactory.TAG_NATIVE, nativeTag.Size);
                }
            }
            else
            {
                readTagParams.ReadTag = false;
                result = audioDataIO.Read(source, sizeInfo, readTagParams);
            }

            if (audioDataIO is IMetaDataEmbedder) // Embedded ID3v2 tag detected while reading
            {
                if (((IMetaDataEmbedder)audioDataIO).HasEmbeddedID3v2 > 0)
                {
                    readTagParams.offset = ((IMetaDataEmbedder)audioDataIO).HasEmbeddedID3v2;
                    if (iD3v2.Read(source, readTagParams))
                    {
                        sizeInfo.TagSizes.Add(MetaDataIOFactory.TAG_ID3V2, iD3v2.Size);
                    }
                }
                else
                {
                    iD3v2.Clear();
                }
            }

            return(result);
        }