Пример #1
0
        public void LoadFile(String path)
        {
            m_ID3v1 = ID3v1Helper.CreateID3v1(path);

            txtFilename.Text       = Path.GetFileName(path);
            txtArtist.Text         = m_ID3v1.Artist;
            txtTitle.Text          = m_ID3v1.Title;
            txtAlbum.Text          = m_ID3v1.Album;
            cmbGenre.SelectedIndex = cmbGenre.Items.IndexOf(GenreHelper.GenreByIndex[m_ID3v1.GenreIndex]);
            txtYear.Text           = m_ID3v1.Year;
            txtComment.Text        = m_ID3v1.Comment;
            if (m_ID3v1.TrackNumber > 0)
            {
                txtTrackNumber.Text = m_ID3v1.TrackNumber.ToString();
            }
            else
            {
                txtTrackNumber.Text = String.Empty;
            }

            switch (m_ID3v1.TagVersion)
            {
            case ID3v1TagVersion.ID3v10:
                cmbID3v1.SelectedIndex = cmbID3v1.Items.IndexOf("ID3v1.0");
                break;

            case ID3v1TagVersion.ID3v11:
                cmbID3v1.SelectedIndex = cmbID3v1.Items.IndexOf("ID3v1.1");
                break;
            }
        }
Пример #2
0
 public MpegAudio(string path)
 {
     this.ResetData();
     using (BinaryReader reader1 = new BinaryReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
     {
         int num1 = ID3v2Helper.GetTagSize(reader1.BaseStream);
         this.m_FileLength = reader1.BaseStream.Length;
         reader1.BaseStream.Seek((long)num1, SeekOrigin.Begin);
         byte[] buffer1 = reader1.ReadBytes(0xd82);
         this.FindFrame(buffer1, ref this.m_VBR);
         this.m_VendorID = this.FindVendorID(buffer1);
         if (!this.m_Frame.Found)
         {
             reader1.BaseStream.Seek((this.m_FileLength - num1) / ((long)2), SeekOrigin.Begin);
             buffer1 = reader1.ReadBytes(0xd82);
             this.FindFrame(buffer1, ref this.m_VBR);
         }
         if (this.m_Frame.Found && string.IsNullOrEmpty(this.m_VendorID))
         {
             reader1.BaseStream.Seek((long)-(buffer1.Length + ID3v1Helper.GetTagSize(reader1.BaseStream)), SeekOrigin.End);
             buffer1 = reader1.ReadBytes(0xd82);
             this.FindFrame(buffer1, ref this.m_VBR);
             this.m_VendorID = this.FindVendorID(buffer1);
         }
     }
     if (!this.m_Frame.Found)
     {
         this.ResetData();
     }
 }
Пример #3
0
        private void btnRemoveID3v1_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(m_Filename))
            {
                MessageBox.Show("No file loaded", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                DialogResult result = MessageBox.Show(String.Format("Remove ID3v1 tag from '{0}'?", Path.GetFileName(m_Filename)), "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    Boolean success = ID3v1Helper.RemoveTag(m_Filename);
                    if (success)
                    {
                        MessageBox.Show("ID3v1 tag successfully removed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("ID3v1 tag not found", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }

            btnRemoveID3v1.Enabled = ID3v1Helper.DoesTagExist(m_Filename);
            ucID3v1.LoadFile(m_Filename);
        }
Пример #4
0
        private void LoadFile(String path)
        {
            m_Filename = path;

            ucID3v2.LoadFile(m_Filename);
            ucID3v1.LoadFile(m_Filename);

            btnSave.Enabled        = true;
            btnRemoveID3v2.Enabled = ID3v2Helper.DoesTagExist(m_Filename);
            btnRemoveID3v1.Enabled = ID3v1Helper.DoesTagExist(m_Filename);
        }
Пример #5
0
        /// <summary>
        /// This method sets various audio file properties into a Dictionary
        /// </summary>
        /// <param name="source">source indicates the path to the file whose properties are to be extracted</param>
        /// <returns>Returns a Dictionary object where key => property name and value => property value</returns>
        public override StringDictionary GetProperties(string source)
        {
            if (IO.File.Exists(source))
            {
                try
                {
                    TagLib.File file = TagLib.File.Create(source);
                    TagLib.Tag  tags = file.Tag;

                    fileProperties["title"]   = tags.Title;
                    fileProperties["album"]   = tags.Album;
                    fileProperties["artist"]  = string.Join(",", tags.AlbumArtists);
                    fileProperties["year"]    = tags.Year.ToString();
                    fileProperties["track"]   = tags.Track.ToString();
                    fileProperties["genres"]  = string.Join(",", tags.Genres);
                    fileProperties["comment"] = tags.Comment;
                    fileProperties["length"]  = TimeSpan.Parse(file.Properties.Duration.Hours + ":" + file.Properties.Duration.Minutes + ":" + file.Properties.Duration.Seconds).ToString();
                }
                catch (Exception ex) //CorruptFileException
                {
                    if (ID3v1Helper.DoesTagExist(source))
                    {
                        IID3v1 id3v1 = ID3v1Helper.CreateID3v1(source);
                        fileProperties["title"]   = id3v1.Title;
                        fileProperties["album"]   = id3v1.Album;
                        fileProperties["artist"]  = id3v1.Artist;
                        fileProperties["year"]    = id3v1.Year;
                        fileProperties["track"]   = id3v1.TrackNumber.ToString();
                        fileProperties["comment"] = id3v1.Comment;
                    }
                    if (ID3v2Helper.DoesTagExist(source))
                    {
                        IID3v2   id3v2 = ID3v2Helper.CreateID3v2(source);
                        TimeSpan ts    = TimeSpan.FromMilliseconds(id3v2.LengthMilliseconds.Value);
                        fileProperties["length"] = TimeSpan.Parse(ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds).ToString();
                        fileProperties["genres"] = id3v2.Genre;
                    }
                    if (!(ID3v1Helper.DoesTagExist(source) && ID3v2Helper.DoesTagExist(source)))
                    {
                        RemoveFileSpecificKeys();
                    }
                }
                return(base.GetProperties(source));
            }
            else if (Win32Helper.PathExist(source))
            {
                RemoveFileSpecificKeys(); return(base.GetProperties(source));
            }
            return(null);
        }
Пример #6
0
        public void SyncTagsToFiles()
        {
            using (var dataContext = new MusicEntities1())
            {
                IQueryable <SONG> songs     = dataContext.SONG.OrderByDescending(s => s.UPDATED).Take(5000);
                SONG[]            songArray = songs.ToArray();

                foreach (SONG song in songArray)
                {
                    try
                    {
                        if (Regex.IsMatch(song.LOCATION, ".mp3", RegexOptions.IgnoreCase))
                        {
                            FileInfo fileInfo      = new FileInfo(song.LOCATION);
                            string   directoryRoot = Directory.GetDirectoryRoot(song.LOCATION);
                            string   filename      = song.LOCATION;
                            if (directoryRoot.ToLower().IndexOf("e:\\") == -1)
                            {
                                filename = filename.Substring(1);
                                filename = "e" + filename;
                            }

                            string genre = null;

                            IID3v2 iD3v2 = ID3v2Helper.CreateID3v2(filename);
                            genre = iD3v2.Genre;

                            UltraID3 ultraID3 = new UltraID3();
                            ultraID3.Read(filename);
                            if (string.IsNullOrWhiteSpace(genre))
                            {
                                genre = ultraID3.Genre;
                            }
                            else if (ultraID3.Genre != genre)
                            {
//                                ultraID3.Genre = genre;
                            }

                            IID3v1 iD3v1 = ID3v1Helper.CreateID3v1(filename);
                            if (Regex.IsMatch(genre, "("))
                            {
                                genre = GenreHelper.GenreByIndex[iD3v1.GenreIndex];
                            }

//                            ID3v2Tag id3v2Tag = ultraID3.ID3v2Tag;


                            if ((genre != song.GENRE) &&
                                (!string.IsNullOrWhiteSpace(song.GENRE))
                                //&& (song.GENRE.ToLower().IndexOf("blues")==-1)
                                //&& (song.GENRE.ToLower().IndexOf("other") == -1)
                                //&& (song.GENRE.ToLower().IndexOf("unknown") == -1)
                                )
                            {
                                testContextInstance.WriteLine("{0}, DB genre: {1}, Disk genre: {2}", filename, song.GENRE, genre);

                                try
                                {
                                    ultraID3.Genre = song.GENRE;
                                    iD3v2.Genre    = song.GENRE;
                                }
                                catch (Exception e1)
                                {
                                    testContextInstance.WriteLine("Exception, song: {0}, {1}, {2}", song.LOCATION, e1.Message, e1.StackTrace);
                                }

                                iD3v2.Save(filename);
                                ultraID3.Write();
                            }
                        }
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        testContextInstance.WriteLine("Exception, file not found, song: {0}", song.LOCATION);
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        testContextInstance.WriteLine("Exception, file not found, song: {0}", song.LOCATION);
                    }
                    catch (Exception ex)
                    {
                        testContextInstance.WriteLine("Exception, song: {0}, {1}, {2}", song.LOCATION, ex.Message, ex.StackTrace);
                    }
                }
            }
        }
Пример #7
0
        public void SyncTagsToDb()
        {
            using (var dataContext = new MusicEntities1())
            {
                IQueryable <SONG> songs = from s in dataContext.SONG
                                          orderby s.ID
                                          select s;

                foreach (SONG song in songs)
                {
                    try
                    {
                        if (Regex.IsMatch(song.LOCATION, ".mp3", RegexOptions.IgnoreCase))
                        {
                            FileInfo fileInfo      = new FileInfo(song.LOCATION);
                            string   directoryRoot = Directory.GetDirectoryRoot(song.LOCATION);
                            string   filename      = song.LOCATION;
                            if (directoryRoot.ToLower().IndexOf("e:\\") == -1)
                            {
                                filename = filename.Substring(1);
                                filename = "e" + filename;
                            }

                            string genre = null;

                            UltraID3 ultraID3 = new UltraID3();
                            ultraID3.Read(filename);

                            genre = ultraID3.Genre;

                            IID3v2 iD3v2 = ID3v2Helper.CreateID3v2(filename);

                            if (string.IsNullOrWhiteSpace(genre))
                            {
                                genre = iD3v2.Genre;
                            }

                            IID3v1 iD3v1 = ID3v1Helper.CreateID3v1(filename);
                            if (string.IsNullOrWhiteSpace(genre))
                            {
                                genre = GenreHelper.GenreByIndex[iD3v1.GenreIndex];
                            }

                            ID3v2Tag id3v2Tag = ultraID3.ID3v2Tag;

                            //if ((!string.IsNullOrEmpty(ultraID3.Genre)) &&
                            //    (!string.IsNullOrEmpty(id3v2Tag.Genre)) &&
                            //    (ultraID3.Genre != id3v2Tag.Genre)
                            //    )
                            //{
                            //    testContextInstance.WriteLine("{0}, {1}", ultraID3.Genre, id3v2Tag.Genre);
                            //}

                            //genre = id3v2Tag.Genre;

                            //if (string.IsNullOrWhiteSpace(genre))
                            //{
                            //    genre = ultraID3.Genre;
                            //}

                            //if (!string.IsNullOrWhiteSpace(ultraID3.Genre))
                            //{
                            //    genre = ultraID3.Genre;
                            //}

                            //if (string.IsNullOrWhiteSpace(genre))
                            //{
                            //    genre = id3v2Tag.Genre;
                            //}


                            if ((genre != song.GENRE) &&
                                (!string.IsNullOrWhiteSpace(song.GENRE))
                                //&& (song.GENRE.ToLower().IndexOf("blues")==-1)
                                //&& (song.GENRE.ToLower().IndexOf("other") == -1)
                                //&& (song.GENRE.ToLower().IndexOf("unknown") == -1)
                                )
                            {
                                testContextInstance.WriteLine("{0}, DB genre: {1}, Disk genre: {2}", filename, song.GENRE, genre);

                                try
                                {
                                    if ((song.UPDATED > fileInfo.LastWriteTime) &&
                                        (song.GENRE != "Other"))
                                    {
                                        id3v2Tag.Genre = song.GENRE;
                                        ultraID3.Write();
                                    }
                                    else if (genre != "Other")
                                    {
                                        song.GENRE = genre;
                                    }
                                }
                                catch (Exception e1)
                                {
                                    testContextInstance.WriteLine("Exception, song: {0}, {1}, {2}", song.LOCATION, e1.Message, e1.StackTrace);
                                }
                            }
                        }
                    }
                    catch (System.IO.DirectoryNotFoundException)
                    {
                        testContextInstance.WriteLine("Exception, file not found, song: {0}", song.LOCATION);
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        testContextInstance.WriteLine("Exception, file not found, song: {0}", song.LOCATION);
                    }
                    catch (Exception ex)
                    {
                        testContextInstance.WriteLine("Exception, song: {0}, {1}, {2}", song.LOCATION, ex.Message, ex.StackTrace);
                    }
                }
                dataContext.SaveChanges();
            }
        }
Пример #8
0
        public static SONG Populate(this SONG song)
        {
            string title, artist, album, genre;

            title  = null;
            artist = null;
            album  = null;
            genre  = null;
            string trackNumber = null;

            try
            {
                try
                {
                    IID3v2 iD3v2 = ID3v2Helper.CreateID3v2(song.LOCATION);
                    artist      = iD3v2.Artist;
                    title       = iD3v2.Title;
                    album       = iD3v2.Album;
                    genre       = iD3v2.Genre;
                    trackNumber = iD3v2.TrackNumber;
                    if ((!string.IsNullOrWhiteSpace(genre)) && (genre.Contains("(")))
                    {
                        genre = genre.Replace('(', ' ');
                        genre = genre.Replace(')', ' ');
                        genre = genre.Trim();
                        int genreIndex = Convert.ToInt16(genre);
                        genre = GenreHelper.GenreByIndex[genreIndex];
                    }
                }
                catch (Exception)
                {
                    Debug.WriteLine(String.Format("Warning: Unable to extract ID3v2 tags from {0}", song.LOCATION));
                }

                try
                {
                    IID3v1 iD3v1 = ID3v1Helper.CreateID3v1(song.LOCATION);
                    if ((string.IsNullOrWhiteSpace(genre)) || (genre.Contains("(")))
                    {
                        genre = GenreHelper.GenreByIndex[iD3v1.GenreIndex];
                    }
                    if (string.IsNullOrWhiteSpace(artist))
                    {
                        artist = iD3v1.Artist;
                    }
                    if (string.IsNullOrWhiteSpace(title))
                    {
                        title = iD3v1.Title;
                    }
                    if (string.IsNullOrWhiteSpace(album))
                    {
                        album = iD3v1.Album;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format("Warning: Unable to extract ID3v1 tags from {0} {1}", song.LOCATION, ex.Message));
                }

                if (String.IsNullOrWhiteSpace(title))
                {
                    String[] s = song.LOCATION.Split(new char[] { '\\', '.' });
                    title = s[s.Length - 2];
                }

                if (string.IsNullOrEmpty(artist))
                {
                    int loc = title.IndexOf("by");
                    if ((loc > -1) && (title.Length > 3))
                    {
                        artist = title.Substring(loc + 3);
                        if (loc > 1)
                        {
                            title = title.Substring(0, loc - 1);
                        }

                        loc = artist.IndexOf("from");
                        if ((loc > -1) && (artist.Length > 5))
                        {
                            album  = artist.Substring(loc + 5);
                            artist = artist.Substring(0, loc - 1);
                        }
                    }
                }

                song.TITLE       = title;
                song.ARTIST      = artist;
                song.GENRE       = genre;
                song.ALBUM       = album;
                song.TrackNumber = trackNumber;

                song.RATING = 1;

                try
                {
                    song.HASH = MusicDao.GenerateHashCode(song.LOCATION);
                }
                catch (Exception)
                {
                    Debug.WriteLine(String.Format("Warning: Unable to generate hash for {0}", song.LOCATION));
                }

                try
                {
                    FileInfo fileInfo = new FileInfo(song.LOCATION);
                    song.FILESIZE = fileInfo.Length;
                }
                catch (Exception)
                {
                    Debug.WriteLine(String.Format("Warning: Unable to get file length {0}", song.LOCATION));
                }
            }
            catch (Exception e1)
            {
                Debug.WriteLine(e1.Message);
            }

            return(song);
        }
Пример #9
0
        public void UpdateTags(string connectionString, string musicDrive)
        {
            int songId    = 0;
            int batchSize = 100;

            while (true)
            {
                bool songTaken = false;
                using (var dataContext = new MusicEntities1())
                {
                    IQueryable <SONG> songs = dataContext.SONG.Where(s => s.ID > songId && s.LOCATION.ToLower().Contains(".mp3")).OrderBy(s => s.ID).Take(batchSize);

                    foreach (SONG song in songs)
                    {
                        songTaken = true;
                        try
                        {
                            FileInfo fileInfo      = new FileInfo(song.LOCATION);
                            string   directoryRoot = Directory.GetDirectoryRoot(song.LOCATION);
                            string   filename      = song.LOCATION;
                            if (directoryRoot.ToLower().IndexOf(musicDrive) != 0)
                            {
                                filename = filename.Substring(1);
                                filename = musicDrive + filename;
                            }
                            string genre = null;

                            UltraID3 ultraID3 = new UltraID3();
                            ultraID3.Read(filename);

                            genre = ultraID3.Genre;

                            IID3v2 iD3v2 = ID3v2Helper.CreateID3v2(filename);

                            if (string.IsNullOrWhiteSpace(genre))
                            {
                                genre = iD3v2.Genre;
                            }

                            IID3v1 iD3v1 = ID3v1Helper.CreateID3v1(filename);
                            if (string.IsNullOrWhiteSpace(genre))
                            {
                                genre = GenreHelper.GenreByIndex[iD3v1.GenreIndex];
                            }

                            ID3v2Tag id3v2Tag = ultraID3.ID3v2Tag;

                            if ((genre != song.GENRE) &&
                                (!string.IsNullOrWhiteSpace(song.GENRE)) &&
                                (song.GENRE.ToLower().IndexOf("blues") == -1) &&
                                (song.GENRE.ToLower().IndexOf("other") == -1) &&
                                (song.GENRE.ToLower().IndexOf("unknown") == -1)
                                )
                            {
                                Console.WriteLine(String.Format("{0}, DB genre: {1}, Disk genre: {2}", filename, song.GENRE, genre));

                                try
                                {
                                    ultraID3.Genre = song.GENRE;
                                }
                                catch (Exception e1)
                                {
                                    Console.WriteLine(String.Format("Exception, song: {0}, {1}, {2}", song.LOCATION, e1.Message, e1.StackTrace));
                                }

                                id3v2Tag.Genre = song.GENRE;
                                ultraID3.Write();
                            }
                        }
                        catch (System.IO.DirectoryNotFoundException)
                        {
                            Console.WriteLine(String.Format("Exception, file not found, song: {0}", song.LOCATION));
                        }
                        catch (System.IO.FileNotFoundException)
                        {
                            Console.WriteLine(String.Format("Exception, file not found, song: {0}", song.LOCATION));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(String.Format("Exception, song: {0}, {1}, {2}", song.LOCATION, ex.Message, ex.StackTrace));
                        }
                        songId = song.ID;
                    }
                }
                if (!songTaken)
                {
                    break;
                }
            }
        }