3
\$\begingroup\$

So I made an XML database for my music player where I store songs data (title,artist,album,genre,year) the code for generating the database is working but slow as heck, the code works as following diagram.

                                       
                                       |Aritsts Database  >|Aritst ->(Name,Songs,Count)
Music Library -->For Loop> Xml Database|Albums  Database  >|Album  ->(Name,Songs,Count)
                                       |Genres  Database  >|Genre  ->(Name,Songs,Count)
                                       |Years   Database  >|Year   ->(Name,Songs,Count)

Example for the artist database (the rest for albums , years , genres will be the same) So what am doing is looping through the music library and storing the artist , album , genre , year in 4 separate List(Of String) Then I remove the duplicates from the list, then I make a List(Of String)(Song list) Then I loop on all the Artist XML database which is loaded in a XMLDocument If the Artist is already there it will add the songs to the current artist else It will make a new artist node and add the songs to it The only problem is It's too slow I think it's a problem with the code but I don't know how to fix it.Here is the code:

   Dim OldCount As Double = My.Settings.MusicLibrary.Count
                My.Settings.MusicLibrary.Clear()
                Dim filters As String = "*.mp3|*.wav|*.aiff|*.mp2|*.mp1|*.ogg|*.wma|*.flac|*.alac|*.webm"
                For Each path In My.Settings.librarypath
                    Dim files
                    If My.Settings.DirectoryTopOpen = True Then
                        files = filters.Split("|"c).SelectMany(Function(filter) System.IO.Directory.GetFiles(path, filter, IO.SearchOption.TopDirectoryOnly)).ToArray()
                    Else
                        files = filters.Split("|"c).SelectMany(Function(filter) System.IO.Directory.GetFiles(path, filter, IO.SearchOption.AllDirectories)).ToArray()
                    End If
                    For Each file In files
                        My.Settings.MusicLibrary.Add(file)
                    Next
                Next
                Dim ArtistList As New List(Of String)
                Dim AlbumList As New List(Of String)
                Dim Tagger = Nothing
                For Each track In My.Settings.MusicLibrary
                    Tagger = TagLib.File.Create(track)
                    ArtistList.Add(Tagger.Tag.JoinedPerformers)
                    AlbumList.Add(Tagger.Tag.Album)
                    Tagger = Nothing
                Next
                Dim NartistList = ArtistList.Distinct.ToList
                Dim NalbumList = AlbumList.Distinct.ToList
                My.Settings.ArtistLibrary.Clear()
                My.Settings.AlbumLibrary.Clear()
                For Each artist In NartistList
                    My.Settings.ArtistLibrary.Add(artist)
                Next
                For Each album In NalbumList
                    My.Settings.AlbumLibrary.Add(album)
                Next
                Dim _Tagger
                For Each song In My.Settings.MusicLibrary
                    _Tagger = TagLib.File.Create(song)
                    My.Forms.Form1.Library_Main.Items.Add(New ListViewItem({_Tagger.Tag.Title, _Tagger.Tag.JoinedPerformers, _Tagger.Tag.Album, _Tagger.Properties.Duration.TotalMinutes.ToString.Substring(0, 4).Replace(".", ":"), _Tagger.Tag.Year, _Tagger.Properties.AudioBitrate, _Tagger.Properties.AudioChannels, _Tagger.Properties.AudioSampleRate, _Tagger.Properties.BitsPerSample, _Tagger.Properties.MediaTypes.ToString, song}))
                Next
                settings_rebuildlib.Checked = False
                My.Forms.Form1.ShowNotification("Scanner", "Added " & My.Settings.MusicLibrary.Count - OldCount & " To the library", "Mr.Audio • Settings • Scanner", My.Resources.radar1, Color.DodgerBlue)
                UpdateLibStatistics()
  Dim WithEvents BG_LIB_UPDATOR As New BackgroundWorker With {.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
    Public Sub UpdateLibStatistics()
        Library_upd_overlay.Visible = True
        Library_upd_overlay.BringToFront
        BG_LIB_UPDATOR.RunWorkerAsync()
  Private Sub BG_LIB_UPDATOR_DoWork(sender As Object, e As DoWorkEventArgs) Handles BG_LIB_UPDATOR.DoWork
        BG_LIB_UPDATOR.ReportProgress(0)
        Dim ArtistList As New List(Of String)
        Dim AlbumList As New List(Of String)
        Dim YearList As New List(Of Integer)
        Dim GenreList As New List(Of String)
        Dim Tagger As TagLib.File
        BG_LIB_UPDATOR.ReportProgress(5)
        For Each track In My.Settings.MusicLibrary
            Tagger = TagLib.File.Create(track)
            ArtistList.Add(Tagger.Tag.JoinedPerformers)
            AlbumList.Add(Tagger.Tag.Album)
            YearList.Add(Tagger.Tag.Year)
            GenreList.Add(String.Join(";", Tagger.Tag.Genres))
            Tagger = Nothing
        Next
        BG_LIB_UPDATOR.ReportProgress(10)
        Dim NartistList = ArtistList.Distinct.ToList
        Dim NalbumList = AlbumList.Distinct.ToList
        Dim Nyearlist = YearList.Distinct.ToList
        Dim Ngenrelist = GenreList.Distinct.ToList
        My.Settings.ArtistLibrary.Clear()
        My.Settings.AlbumLibrary.Clear()
        My.Settings.YearLibrary.Clear()
        My.Settings.GenreLibrary.Clear()
        BG_LIB_UPDATOR.ReportProgress(15)
        For Each artist In NartistList
            My.Settings.ArtistLibrary.Add(artist)
        Next
        For Each album In NalbumList
            My.Settings.AlbumLibrary.Add(album)
        Next
        For Each _Year In Nyearlist
            My.Settings.YearLibrary.Add(_Year)
        Next
        For Each Genre In Ngenrelist
            My.Settings.GenreLibrary.Add(Genre)
        Next
        BG_LIB_UPDATOR.ReportProgress(20)
        'BEGIN OF LIB_ARTIST
        ' Create XmlWriterSettings.
        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.Indent = True
        Dim LIB_ARTISTtpath As String
        ' Create XmlWriter.
        If IO.File.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Artists.xml")) Then
            LIB_ARTISTtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Artists.xml")
        Else
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library"))
            IO.File.Create(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Artists.xml")).Close()
            LIB_ARTISTtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Artists.xml")
        End If
        If Not IO.Directory.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "ArtistsPics")) Then
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "ArtistsPics"))
        End If
        BG_LIB_UPDATOR.ReportProgress(23)
        Try
            Using writer As XmlWriter = XmlWriter.Create(LIB_ARTISTtpath, settings)
                ' Begin writing.
                writer.WriteStartDocument()
                writer.WriteStartElement("Mr.Audio") ' Root.
                writer.WriteStartElement("Artists") ' Root.
                For Each artist In NartistList
                    writer.WriteStartElement("Artist")
                    writer.WriteElementString("Name", artist)
                    writer.WriteStartElement("Songs")
                    Dim ArtistSongs As New List(Of String)
                    For Each song In My.Settings.MusicLibrary
                        If IO.File.Exists(song) Then
                            Tagger = TagLib.File.Create(song)
                            If CType(Tagger, TagLib.File).Tag.JoinedPerformers = artist Then
                                writer.WriteElementString("Song", song)
                                ArtistSongs.Add(song)
                            End If
                        End If
                    Next
                    writer.WriteEndElement()
                    writer.WriteElementString("Count", ArtistSongs.Count)
                    writer.WriteEndElement()
                    Tagger = TagLib.File.Create(ArtistSongs(0))
                    Image.FromStream(New MemoryStream(Tagger.Tag.Pictures(0).Data.Data)).GetThumbnailImage(100, 100, Nothing, IntPtr.Zero).Clone.Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "ArtistsPics", artist & ".png"))
                Next
                BG_LIB_UPDATOR.ReportProgress(24)
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteEndDocument()
            End Using
        Catch ex As Exception
            '' MessageBox.Show(Me, "There is somthing wrong :/" & vbCrLf & "Check console.", "Mr. Audio", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Console.WriteLine(TimeOfDay.ToShortTimeString & ": At LIB_UPDATE_ARTISTS //" & ex.Message & vbCrLf & ex.StackTrace)
        End Try
        'END OF LIB_ARTIST
        'BEGIN OF LIB_ALBUM
        BG_LIB_UPDATOR.ReportProgress(25)
        ' Create XmlWriterSettings.
        Dim settings2 As XmlWriterSettings = New XmlWriterSettings()
        settings2.Indent = True
        Dim LIB_ALBUMtpath As String
        ' Create XmlWriter.
        If IO.File.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Albums.xml")) Then
            LIB_ALBUMtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Albums.xml")
        Else
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library"))
            IO.File.Create(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Albums.xml")).Close()
            LIB_ALBUMtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Albums.xml")
        End If
        If Not IO.Directory.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "AlbumsPics")) Then
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "AlbumsPics"))
        End If
        Try
            BG_LIB_UPDATOR.ReportProgress(27)
            Using writer As XmlWriter = XmlWriter.Create(LIB_ALBUMtpath, settings2)
                ' Begin writing.
                writer.WriteStartDocument()
                writer.WriteStartElement("Mr.Audio") ' Root.
                writer.WriteStartElement("Albums") ' Root.
                For Each album In NalbumList
                    writer.WriteStartElement("Album")
                    writer.WriteElementString("Name", album)
                    writer.WriteStartElement("Songs")
                    Dim AlbumSongs As New List(Of String)
                    For Each song In My.Settings.MusicLibrary
                        If IO.File.Exists(song) Then
                            Tagger = TagLib.File.Create(song)
                            If CType(Tagger, TagLib.File).Tag.Album = album Then
                                writer.WriteElementString("Song", song)
                                AlbumSongs.Add(song)
                            End If
                        End If
                    Next
                    writer.WriteEndElement()
                    writer.WriteElementString("Count", AlbumSongs.Count)
                    writer.WriteEndElement()
                    Tagger = TagLib.File.Create(AlbumSongs(0))
                    Image.FromStream(New MemoryStream(Tagger.Tag.Pictures(0).Data.Data)).GetThumbnailImage(100, 100, Nothing, IntPtr.Zero).Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "AlbumsPics", album & ".png"))
                Next
                BG_LIB_UPDATOR.ReportProgress(29)
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteEndDocument()
            End Using
        Catch ex As Exception
            '' MessageBox.Show(Me, "There is somthing wrong :/" & vbCrLf & "Check console.", "Mr. Audio", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Console.WriteLine(TimeOfDay.ToShortTimeString & ": At LIB_UPDATE_ALBUMS //" & ex.Message & vbCrLf & ex.StackTrace)
        End Try
        'END OF LIB_ALBUM
        'BEGIN OF LIB_YEAR
        BG_LIB_UPDATOR.ReportProgress(30)
        ' Create XmlWriterSettings.
        Dim settings3 As XmlWriterSettings = New XmlWriterSettings()
        settings3.Indent = True
        Dim LIB_YEARtpath As String
        ' Create XmlWriter.
        If IO.File.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Years.xml")) Then
            LIB_YEARtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Years.xml")
        Else
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library"))
            IO.File.Create(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Years.xml")).Close()
            LIB_YEARtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Years.xml")
        End If
        If Not IO.Directory.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "YearsPics")) Then
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "YearsPics"))
        End If
        Try
            BG_LIB_UPDATOR.ReportProgress(37)
            Using writer As XmlWriter = XmlWriter.Create(LIB_YEARtpath, settings3)
                ' Begin writing.
                writer.WriteStartDocument()
                writer.WriteStartElement("Mr.Audio") ' Root.
                writer.WriteStartElement("Years") ' Root.
                For Each _Year In Nyearlist
                    writer.WriteStartElement("Year")
                    writer.WriteElementString("Name", _Year)
                    writer.WriteStartElement("Songs")
                    Dim YearSongs As New List(Of String)
                    For Each song In My.Settings.MusicLibrary
                        If IO.File.Exists(song) Then
                            Tagger = TagLib.File.Create(song)
                            If CType(Tagger, TagLib.File).Tag.Year = _Year Then
                                writer.WriteElementString("Song", song)
                                YearSongs.Add(song)
                            End If
                        End If
                    Next
                    writer.WriteEndElement()
                    writer.WriteElementString("Count", YearSongs.Count)
                    writer.WriteEndElement()
                    GenerateImage(_Year).Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "YearsPics", _Year & ".png"))
                Next
                BG_LIB_UPDATOR.ReportProgress(40)
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteEndDocument()
            End Using
        Catch ex As Exception
            '' MessageBox.Show(Me, "There is somthing wrong :/" & vbCrLf & "Check console.", "Mr. Audio", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Console.WriteLine(TimeOfDay.ToShortTimeString & ": At LIB_UPDATE_YEARS //" & ex.Message & vbCrLf & ex.StackTrace)
        End Try
        'END OF LIB_YEAR
        'BEGIN OF LIB_GENRE
        BG_LIB_UPDATOR.ReportProgress(50)
        ' Create XmlWriterSettings.
        Dim settings4 As XmlWriterSettings = New XmlWriterSettings()
        settings4.Indent = True
        Dim LIB_GENREtpath As String
        ' Create XmlWriter.
        If IO.File.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Genres.xml")) Then
            LIB_GENREtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Genres.xml")
        Else
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library"))
            IO.File.Create(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Genres.xml")).Close()
            LIB_GENREtpath = IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "Genres.xml")
        End If
        If Not IO.Directory.Exists(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "GenresPics")) Then
            IO.Directory.CreateDirectory(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "GenresPics"))
        End If
        Try
            BG_LIB_UPDATOR.ReportProgress(60)
            Using writer As XmlWriter = XmlWriter.Create(LIB_GENREtpath, settings4)
                ' Begin writing.
                writer.WriteStartDocument()
                writer.WriteStartElement("Mr.Audio") ' Root.
                writer.WriteStartElement("Genres") ' Root.
                For Each _Genre In Ngenrelist
                    writer.WriteStartElement("Genre")
                    writer.WriteElementString("Name", _Genre)
                    writer.WriteStartElement("Songs")
                    Dim GenreSongs As New List(Of String)
                    For Each song In My.Settings.MusicLibrary
                        If IO.File.Exists(song) Then
                            Tagger = TagLib.File.Create(song)
                            If String.Join(";", CType(Tagger, TagLib.File).Tag.Genres) = _Genre Then
                                writer.WriteElementString("Song", song)
                                GenreSongs.Add(song)
                            End If
                        End If
                    Next
                    writer.WriteEndElement()
                    writer.WriteElementString("Count", GenreSongs.Count)
                    writer.WriteEndElement()
                    GenerateImage(_Genre).Save(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MrAudio", "Library", "GenresPics", _Genre & ".png"))
                Next
                BG_LIB_UPDATOR.ReportProgress(70)
                writer.WriteEndElement()
                writer.WriteEndElement()
                writer.WriteEndDocument()
            End Using
        Catch ex As Exception
            '' MessageBox.Show(Me, "There is somthing wrong :/" & vbCrLf & "Check console.", "Mr. Audio", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Console.WriteLine(TimeOfDay.ToShortTimeString & ": At LIB_UPDATE_GENRES //" & ex.Message & vbCrLf & ex.StackTrace)
        End Try
        'END OF LIB_GENRE
        BG_LIB_UPDATOR.ReportProgress(100)
    End Sub

Thanks in advance.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.