예제 #1
0
        private static void WriteToStream(byte[] audioData, byte[] output, Id3V1Tag Id3Tag)
        {
            MemoryStream inputSteam = null;
            MemoryStream outputStream = null;
            try
            {
                inputSteam = new MemoryStream(audioData, false);
                outputStream = new MemoryStream(output, true);

                IId3V1Controller id3Controller = Id3TagFactory.CreateId3V1Controller();
                id3Controller.Write(Id3Tag, inputSteam, outputStream, 0);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                if (inputSteam != null)
                {
                    inputSteam.Dispose();
                }

                if (outputStream != null)
                {
                    inputSteam.Dispose();
                }
            }
        }
예제 #2
0
        private static void CheckId3Tag(Id3V1Tag tag1, Id3V1Tag tag2)
        {
            Assert.AreEqual(tag1.Album, tag2.Album);
            Assert.AreEqual(tag1.Artist, tag2.Artist);
            Assert.AreEqual(tag1.Comment, tag2.Comment);
            Assert.AreEqual(tag1.Genre, tag2.Genre);
            Assert.AreEqual(tag1.GenreIdentifier, tag2.GenreIdentifier);
            Assert.AreEqual(tag1.IsId3V1Dot1Compliant, tag2.IsId3V1Dot1Compliant);
            Assert.AreEqual(tag1.Title, tag2.Title);
            Assert.AreEqual(tag1.Year, tag2.Year);

            if (tag1.IsId3V1Dot1Compliant)
            {
                Assert.AreEqual(tag1.TrackNumber, tag2.TrackNumber);
            }
            else
            {
                Assert.IsNotNull(tag1);
                Assert.IsNotNull(tag2);
            }
        }
예제 #3
0
        /// <summary>
        /// Updates ID3 v1 tag in the same file.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <param name="tag">The tag.</param>
        /// <param name="codePage">The code page.</param>
        public void WriteV1Tag(string path, Id3V1Tag tag, int codePage)
        {
            #region Params Check

            if (String.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException("path");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("File does not exist.", path);
            }

            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

            #endregion

            IId3V1Controller controller = Id3TagFactory.CreateId3V1Controller();
            WriteTag(path, (input, output) => controller.Write(tag, input, output, codePage));
        }
예제 #4
0
 public void WriteId3V1Tag(Id3V1Tag tag, string sourceFile, string targetFile)
 {
     try
     {
         Id3TagFactory.CreateId3TagManager().WriteV1Tag(sourceFile, targetFile, tag);
     }
     catch (Id3IOException ioException)
     {
         MessageBox.Show("IO Exception caught : " + ioException.Message);
     }
     catch (Id3TagException tagException)
     {
         MessageBox.Show("Id3TagException caught : " + tagException.Message);
     }
     catch (Exception ex)
     {
         MessageBox.Show("Unknown exception caught : " + ex.Message);
     }
 }
예제 #5
0
        public ID3V1Dialog()
        {
            InitializeComponent();

            TagData = new Id3V1Tag();
        }
예제 #6
0
 /// <summary>
 /// Saves ID3 v1 tag to new file.
 /// </summary>
 /// <param name="sourcePath">The path.</param>
 /// <param name="targetPath">The target sourcePath.</param>
 /// <param name="tag">The tag.</param>
 public void WriteV1Tag(string sourcePath, string targetPath, Id3V1Tag tag)
 {
     WriteV1Tag(sourcePath, targetPath, tag, 0);
 }
예제 #7
0
 /// <summary>
 /// Updates ID3 v1 tag in the same file.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="tag">The tag.</param>
 public void WriteV1Tag(string path, Id3V1Tag tag)
 {
     WriteV1Tag(path, tag, 0);
 }
예제 #8
0
        public void WriteWithOverMaxFieldSize_ID3v11()
        {
            var audioData = new byte[] {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
            var output = new byte[138];

            var Id3Tag1 = new Id3V1Tag
                              {
                                  Title = CreateFieldText(31, '1'),
                                  Artist = CreateFieldText(31, '2'),
                                  Album = CreateFieldText(31, '3'),
                                  Year = CreateFieldText(5, '4'),
                                  Comment = CreateFieldText(29, '5'),
                                  IsId3V1Dot1Compliant = true,
                                  TrackNumber = "6",
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, Id3Tag1);

            //
            //  The coding if the fiels is over the limit. The I/O Controller cuts the string.
            //
            var refId3Tag = new Id3V1Tag
                                {
                                    Title = CreateFieldText(30, '1'),
                                    Artist = CreateFieldText(30, '2'),
                                    Album = CreateFieldText(30, '3'),
                                    Year = CreateFieldText(4, '4'),
                                    Comment = CreateFieldText(28, '5'),
                                    IsId3V1Dot1Compliant = true,
                                    TrackNumber = "6",
                                    GenreIdentifier = 12
                                };

            Id3V1Tag Id3Tag2 = ReadFromStream(output);
            CheckId3Tag(refId3Tag, Id3Tag2);
        }
예제 #9
0
        public void WriteWithMaxFieldSize()
        {
            var audioData = new byte[] {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
            var output = new byte[138];

            var Id3Tag1 = new Id3V1Tag
                              {
                                  Title = CreateFieldText(30, '1'),
                                  Artist = CreateFieldText(30, '2'),
                                  Album = CreateFieldText(30, '3'),
                                  Year = CreateFieldText(4, '4'),
                                  Comment = CreateFieldText(28, '5'),
                                  IsId3V1Dot1Compliant = true,
                                  TrackNumber = "6",
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, Id3Tag1);

            Id3V1Tag Id3Tag2 = ReadFromStream(output);
            CheckId3Tag(Id3Tag1, Id3Tag2);
        }
예제 #10
0
        public void WriteID3V1_1Tag()
        {
            var audioData = new byte[] {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
            var output = new byte[138];

            var Id3Tag1 = new Id3V1Tag
                              {
                                  Title = "1",
                                  Artist = "2",
                                  Album = "3",
                                  Year = "4",
                                  Comment = "5",
                                  IsId3V1Dot1Compliant = true,
                                  TrackNumber = "6",
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, Id3Tag1);

            Id3V1Tag Id3Tag2 = ReadFromStream(output);
            CheckId3Tag(Id3Tag1, Id3Tag2);
        }
예제 #11
0
        public void WriteAudioContent2()
        {
            var audioData = new byte[150];
            var output = new byte[audioData.Length + 128];

            //
            // Create dummy audio content..
            //
            for (byte i = 0; i < audioData.Length; i++)
            {
                audioData[i] = i;
            }

            // Create a new tag description.
            var Id3Tag1 = new Id3V1Tag
                              {
                                  Title = "1",
                                  Artist = "2",
                                  Album = "3",
                                  Year = "4",
                                  Comment = "5",
                                  IsId3V1Dot1Compliant = false,
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, Id3Tag1);

            for (byte i = 0; i < 150; i++)
            {
                Assert.AreEqual(output[i], i);
            }
        }
예제 #12
0
        private static Id3V1Tag ExtractTag(byte[] tagBytes, int codePage)
        {
            // Read the tag

            var titleBytes = new byte[30];
            var artistBytes = new byte[30];
            var albumBytes = new byte[30];
            var yearBytes = new byte[4];
            var commentBytes = new byte[30];

            Array.Copy(tagBytes, 3, titleBytes, 0, 30);
            Array.Copy(tagBytes, 33, artistBytes, 0, 30);
            Array.Copy(tagBytes, 63, albumBytes, 0, 30);
            Array.Copy(tagBytes, 93, yearBytes, 0, 4);
            Array.Copy(tagBytes, 97, commentBytes, 0, 30);
            byte genreByte = tagBytes[127];

            string title = GetString(titleBytes, codePage);
            string artits = GetString(artistBytes, codePage);
            string album = GetString(albumBytes, codePage);
            string year = GetString(yearBytes, codePage);

            bool id3v1_1Support = ((commentBytes[28] == 0) && (commentBytes[29] != 0));
            string trackNr = String.Empty;
            string comment = String.Empty;

            if (id3v1_1Support)
            {
                byte trackNrValue = commentBytes[29];
                trackNr = Convert.ToString(trackNrValue, CultureInfo.InvariantCulture);

                var newComments = new byte[28];
                Array.Copy(commentBytes, 0, newComments, 0, newComments.Length);

                comment = GetString(newComments, codePage);
            }
            else
            {
                comment = GetString(commentBytes, codePage);
            }

            var id3v1 = new Id3V1Tag
                            {
                                Title = title,
                                Artist = artits,
                                Album = album,
                                Year = year,
                                Comment = comment,
                                GenreIdentifier = genreByte,
                                IsId3V1Dot1Compliant = id3v1_1Support,
                                TrackNumber = trackNr
                            };

            return id3v1;
        }
예제 #13
0
        private static byte[] ConvertToByte(Id3V1Tag tag, int codePage)
        {
            var tagBytes = new byte[128];
            // Write the tag ID ( TAG)
            tagBytes[0] = 0x54;
            tagBytes[1] = 0x41;
            tagBytes[2] = 0x47;

            // Write the fields...
            byte[] titleBytes = GetField(tag.Title, 30, codePage);
            byte[] artistBytes = GetField(tag.Artist, 30, codePage);
            byte[] albumBytes = GetField(tag.Album, 30, codePage);
            byte[] year = GetField(tag.Year, 4, codePage);

            Array.Copy(titleBytes, 0, tagBytes, 3, 30);
            Array.Copy(artistBytes, 0, tagBytes, 33, 30);
            Array.Copy(albumBytes, 0, tagBytes, 63, 30);
            Array.Copy(year, 0, tagBytes, 93, 4);

            byte[] commentBytes;
            if (tag.IsId3V1Dot1Compliant)
            {
                commentBytes = GetField(tag.Comment, 28, codePage);
                Array.Copy(commentBytes, 0, tagBytes, 97, 28);

                string trackNr = tag.TrackNumber;
                tagBytes[125] = 0x00;
                tagBytes[126] = Convert.ToByte(trackNr, CultureInfo.InvariantCulture);
            }
            else
            {
                commentBytes = GetField(tag.Comment, 30, codePage);
                Array.Copy(commentBytes, 0, tagBytes, 97, 30);
            }

            // Add genre
            tagBytes[127] = Convert.ToByte(tag.GenreIdentifier);

            return tagBytes;
        }
예제 #14
0
        /// <summary>
        /// Writes a new ID3v1 tag using specified code page for text encoding.
        /// </summary>
        /// <param name="tag">the tag.</param>
        /// <param name="input">the audio input stream.</param>
        /// <param name="output">the target stream.</param>
        /// <param name="codePage">The code page for text encoding.</param>
        public void Write(Id3V1Tag tag, Stream input, Stream output, int codePage)
        {
            //
            //  Validate the parameter.
            //
            if (tag == null)
            {
                var ex = new ArgumentNullException("tag");
                Logger.LogError(ex);

                throw ex;
            }

            CheckStreams(input, output);

            try
            {
                //
                //  Read the last 128 Bytes from the stream (ID3v1 Position)
                //
                long audioBytesCount = GetAudioBytesCount(input);

                //
                //  Write the audio data and tag
                //
                input.Seek(0, SeekOrigin.Begin);
                Utils.WriteAudioStream(output, input, audioBytesCount);

                byte[] tagBytes = ConvertToByte(tag, codePage);
                output.Write(tagBytes, 0, tagBytes.Length);
            }
            catch (Exception ex)
            {
                var ioEx = new Id3IOException("Cannot write ID3v1 tag", ex);
                Logger.LogError(ioEx);

                throw ioEx;
            }
        }
예제 #15
0
 /// <summary>
 /// Writes a new ID3v1 tag using default code page for text encoding.
 /// </summary>
 /// <param name="tag">the tag.</param>
 /// <param name="input">the audio input stream.</param>
 /// <param name="output">the target stream.</param>
 public void Write(Id3V1Tag tag, Stream input, Stream output)
 {
     Write(tag, input, output, 1252);
 }