Example #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);

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

                if (outputStream != null)
                {
                    inputSteam.Dispose();
                }
            }
        }
Example #2
0
        public void Write(Id3V1Tag tag, Stream input, Stream output)
        {
            //
            //  Validate the parameter.
            //
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }

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

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

            if (!input.CanSeek)
            {
                throw new ID3TagException("Cannot write ID3V1 tag because the source does not support seek.");
            }

            if (!output.CanWrite)
            {
                throw new ID3TagException("Cannot write ID3V1 tag because the output does not support writing.");
            }

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

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

                var tagBytes = ConvertToByte(tag);
                output.Write(tagBytes, 0, tagBytes.Length);
            }
            catch (Exception ex)
            {
                throw new ID3IOException("Cannot write ID3v1 tag", ex);
            }

        }
Example #3
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.IsID3V1_1Compliant, tag2.IsID3V1_1Compliant);
            Assert.AreEqual(tag1.Title, tag2.Title);
            Assert.AreEqual(tag1.Year, tag2.Year);

            if (tag1.IsID3V1_1Compliant)
            {
                Assert.AreEqual(tag1.TrackNr, tag2.TrackNr);
            }
            else
            {
                Assert.IsNotNull(tag1);
                Assert.IsNotNull(tag2);
            }
        }
Example #4
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 static 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));
		}
Example #5
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.IsID3V1_1Compliant)
			{
				commentBytes = GetField(tag.Comment, 28, codePage);
				Array.Copy(commentBytes, 0, tagBytes, 97, 28);

				string trackNr = tag.TrackNr;
				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;
		}
Example #6
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);
		}
Example #7
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 static void WriteV1Tag(string sourcePath, string targetPath, Id3V1Tag tag)
		{
			WriteV1Tag(sourcePath, targetPath, tag, 0);
		}
Example #8
0
		/// <summary>
		/// Updates ID3 v1 tag in the same file.
		/// </summary>
		/// <param name="path">The path.</param>
		/// <param name="tag">The tag.</param>
		public static void WriteV1Tag(string path, Id3V1Tag tag)
		{
			WriteV1Tag(path, tag, 0);
		}
        public void WriteId3V1Tag (Id3V1Tag tag, string sourceFile, string targetFile)
        {
            FileStream inputStream = null;
            FileStream outputStream = null;
            try
            {
                var id3V1Controller = Id3TagFactory.CreateId3V1Controller();

                // Write the tag.
                inputStream = File.Open(sourceFile, FileMode.Open);
                outputStream = File.OpenWrite(targetFile);

                id3V1Controller.Write(tag, inputStream, outputStream);
            }
            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);
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Close();
                    inputStream.Dispose();
                }

                if (outputStream != null)
                {
                    outputStream.Close();
                    outputStream.Dispose();
                }
            }
        }
Example #10
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'),
                                  IsID3V1_1Compliant = true,
                                  TrackNr = "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'),
                                    IsID3V1_1Compliant = true,
                                    TrackNr = "6",
                                    GenreIdentifier = 12
                                };

            var id3Tag2 = ReadFromStream(output);
            CheckID3Tag(refId3Tag, id3Tag2);
        }
Example #11
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'),
                                  IsID3V1_1Compliant = true,
                                  TrackNr = "6",
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, id3Tag1);

            var id3Tag2 = ReadFromStream(output);
            CheckID3Tag(id3Tag1, id3Tag2);
        }
Example #12
0
        public void WriteID3V1Tag()
        {
            var audioData = new byte[] {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19};
            var output = new byte[138];

            // Create a byte array...
            var id3Tag1 = new Id3V1Tag
                              {
                                  Title = "1",
                                  Artist = "2",
                                  Album = "3",
                                  Year = "4",
                                  Comment = "5",
                                  IsID3V1_1Compliant = false,
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, id3Tag1);
            var id3Tag2 = ReadFromStream(output);
            CheckID3Tag(id3Tag1, id3Tag2);
        }
Example #13
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",
                                  IsID3V1_1Compliant = false,
                                  GenreIdentifier = 12
                              };

            WriteToStream(audioData, output, id3Tag1);

            for (byte i = 0; i < 150; i++)
            {
                Assert.AreEqual(output[i], i);
            }
        }
Example #14
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,
			            		IsID3V1_1Compliant = id3v1_1Support,
			            		TrackNr = trackNr
			            	};

			return id3v1;
		}
Example #15
0
        private byte[] ConvertToByte(Id3V1Tag tag)
        {
            var tagBytes = new byte[128];
            // Write the tag ID ( TAG)
            tagBytes[0] = 0x54;
            tagBytes[1] = 0x41;
            tagBytes[2] = 0x47;

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

            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.IsID3V1_1Compliant)
            {
                commentBytes = GetField(tag.Comment, 28);
                Array.Copy(commentBytes,0,tagBytes,97,28);

                var trackNr = tag.TrackNr;
                tagBytes[125] = 0x00;
                tagBytes[126] = Convert.ToByte(trackNr);
            }
            else
            {
                commentBytes = GetField(tag.Comment, 30);
                Array.Copy(commentBytes,0,tagBytes,97,30);
            }

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

            return tagBytes;
        }
Example #16
0
        public void WriteId3V1Tag(Id3V1Tag tag, string sourceFile, string targetFile)
        {
            try
            {
				Id3TagManager.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);
            }
        }
Example #17
0
        public ID3V1Dialog()
        {
            InitializeComponent();

            TagData = new Id3V1Tag();
        }