Exemplo n.º 1
0
        public async Task <TrackStatistic> GetTrackStatisticAsync(string path)
        {
            TrackStatistic trackStatistic = null;

            await Task.Run(() =>
            {
                try
                {
                    using (var conn = this.factory.GetConnection())
                    {
                        try
                        {
                            trackStatistic = conn.Query <TrackStatistic>("SELECT * FROM TrackStatistic WHERE SafePath=?", path.ToSafePath()).FirstOrDefault();
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not get TrackStatistic for path='{0}'. Exception: {1}", path, ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message);
                }
            });

            return(trackStatistic);
        }
Exemplo n.º 2
0
        private async void PlaybackService_TrackStatisticsChanged(IList <TrackStatistic> trackStatistics)
        {
            if (this.Tracks == null)
            {
                return;
            }

            if (trackStatistics == null)
            {
                return;
            }

            if (trackStatistics.Count == 0)
            {
                return;
            }

            await Task.Run(() =>
            {
                foreach (TrackViewModel vm in this.Tracks)
                {
                    if (trackStatistics.Select(t => t.SafePath).Contains(vm.Track.SafePath))
                    {
                        // The UI is only updated if PropertyChanged is fired on the UI thread
                        TrackStatistic statistic = trackStatistics.Where(c => c.SafePath.Equals(vm.Track.SafePath)).FirstOrDefault();
                        Application.Current.Dispatcher.Invoke(() => vm.UpdateVisibleCounters(statistic));
                    }
                }
            });
        }
Exemplo n.º 3
0
        public async Task UpdateCountersAsync(string path, int playCount, int skipCount, long dateLastPlayed)
        {
            await Task.Run(() =>
            {
                try
                {
                    using (var conn = this.factory.GetConnection())
                    {
                        try
                        {
                            TrackStatistic existingTrackStatistic = conn.Query <TrackStatistic>("SELECT * FROM TrackStatistic WHERE SafePath=?", path.ToSafePath()).FirstOrDefault();

                            if (existingTrackStatistic != null)
                            {
                                if (existingTrackStatistic.PlayCount.HasValue)
                                {
                                    existingTrackStatistic.PlayCount += playCount;
                                }
                                else
                                {
                                    existingTrackStatistic.PlayCount = playCount;
                                }

                                if (existingTrackStatistic.SkipCount.HasValue)
                                {
                                    existingTrackStatistic.SkipCount += skipCount;
                                }
                                else
                                {
                                    existingTrackStatistic.SkipCount = skipCount;
                                }

                                existingTrackStatistic.DateLastPlayed = dateLastPlayed;
                                conn.Update(existingTrackStatistic);
                            }
                            else
                            {
                                var newTrackStatistic            = new TrackStatistic();
                                newTrackStatistic.Path           = path;
                                newTrackStatistic.SafePath       = path.ToSafePath();
                                newTrackStatistic.PlayCount      = playCount;
                                newTrackStatistic.SkipCount      = skipCount;
                                newTrackStatistic.DateLastPlayed = dateLastPlayed;
                                conn.Insert(newTrackStatistic);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not update counters for path='{0}'. Exception: {1}", path, ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message);
                }
            });
        }
Exemplo n.º 4
0
 public void UpdateVisibleCounters(TrackStatistic statistic)
 {
     this.Track.PlayCount      = statistic.PlayCount;
     this.Track.SkipCount      = statistic.SkipCount;
     this.track.DateLastPlayed = statistic.DateLastPlayed;
     RaisePropertyChanged(nameof(this.PlayCount));
     RaisePropertyChanged(nameof(this.SkipCount));
     RaisePropertyChanged(nameof(this.DateLastPlayed));
     RaisePropertyChanged(nameof(this.SortDateLastPlayed));
 }
Exemplo n.º 5
0
 public void UpdateVisibleCounters(TrackStatistic statistic)
 {
     this.Track.PlayCount      = statistic.PlayCount;
     this.Track.SkipCount      = statistic.SkipCount;
     this.track.DateLastPlayed = statistic.DateLastPlayed;
     OnPropertyChanged(() => this.PlayCount);
     OnPropertyChanged(() => this.SkipCount);
     OnPropertyChanged(() => this.DateLastPlayed);
     OnPropertyChanged(() => this.SortDateLastPlayed);
 }
Exemplo n.º 6
0
        public bool HasCachedTrackStatistic(TrackStatistic trackStatistic)
        {
            if (this.cachedTrackStatistics.Contains(trackStatistic.SafePath))
            {
                return(true);
            }

            this.cachedTrackStatistics.Add(trackStatistic.SafePath);

            return(false);
        }
Exemplo n.º 7
0
        public static async Task <PlayableTrack> Path2TrackAsync(string path, TrackStatistic savedTrackStatistic)
        {
            var returnTrack = new PlayableTrack();

            await Task.Run(() =>
            {
                var track          = new Track();
                var trackStatistic = new TrackStatistic();
                var album          = new Album();
                var artist         = new Artist();
                var genre          = new Genre();

                MetadataUtils.SplitMetadata(path, ref track, ref trackStatistic, ref album, ref artist, ref genre);

                returnTrack.Path        = track.Path;
                returnTrack.SafePath    = track.Path.ToSafePath();
                returnTrack.FileName    = track.FileName;
                returnTrack.MimeType    = track.MimeType;
                returnTrack.FileSize    = track.FileSize;
                returnTrack.BitRate     = track.BitRate;
                returnTrack.SampleRate  = track.SampleRate;
                returnTrack.TrackTitle  = track.TrackTitle;
                returnTrack.TrackNumber = track.TrackNumber;
                returnTrack.TrackCount  = track.TrackCount;
                returnTrack.DiscNumber  = track.DiscNumber;
                returnTrack.DiscCount   = track.DiscCount;
                returnTrack.Duration    = track.Duration;
                returnTrack.Year        = track.Year;
                returnTrack.HasLyrics   = track.HasLyrics;

                returnTrack.ArtistName = artist.ArtistName;

                returnTrack.GenreName = genre.GenreName;

                returnTrack.AlbumTitle  = album.AlbumTitle;
                returnTrack.AlbumArtist = album.AlbumArtist;
                returnTrack.AlbumYear   = album.Year;

                // If there is a saved TrackStatistic, used that one. Otherwise the
                // TrackStatistic from the file is used. That only contains rating.
                if (savedTrackStatistic != null)
                {
                    trackStatistic = savedTrackStatistic;
                }

                returnTrack.Rating         = trackStatistic.Rating;
                returnTrack.Love           = trackStatistic.Love;
                returnTrack.PlayCount      = trackStatistic.PlayCount;
                returnTrack.SkipCount      = trackStatistic.SkipCount;
                returnTrack.DateLastPlayed = trackStatistic.DateLastPlayed;
            });

            return(returnTrack);
        }
Exemplo n.º 8
0
        public static ITrackStatistic GetTrackStatistics()
        {
            var tracks = Logic.Factory.GetAllTracks();

            TrackStatistic trackStatistic = new TrackStatistic();

            trackStatistic.MaxTime = tracks.Max(x => x.Milliseconds / 1000);

            trackStatistic.MinTime = tracks.Min(x => x.Milliseconds / 1000);

            trackStatistic.AverageTime = tracks.Average(x => x.Milliseconds / 1000);

            return(trackStatistic);
        }
Exemplo n.º 9
0
        public async Task UpdateLoveAsync(string path, bool love)
        {
            await Task.Run(() =>
            {
                try
                {
                    using (var conn = this.factory.GetConnection())
                    {
                        try
                        {
                            TrackStatistic existingTrackStatistic = conn.Query <TrackStatistic>("SELECT * FROM TrackStatistic WHERE SafePath=?", path.ToSafePath()).FirstOrDefault();

                            if (existingTrackStatistic != null)
                            {
                                existingTrackStatistic.Love = love ? 1 : 0;
                                conn.Update(existingTrackStatistic);
                            }
                            else
                            {
                                var newTrackStatistic      = new TrackStatistic();
                                newTrackStatistic.Path     = path;
                                newTrackStatistic.SafePath = path.ToSafePath();
                                newTrackStatistic.Love     = love ? 1 : 0;
                                conn.Insert(newTrackStatistic);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not update love for path='{0}'. Exception: {1}", path, ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message);
                }
            });
        }
Exemplo n.º 10
0
        public async Task UpdateTrackStatisticAsync(TrackStatistic trackStatistic)
        {
            await Task.Run(() =>
            {
                try
                {
                    using (var conn = this.factory.GetConnection())
                    {
                        try
                        {
                            TrackStatistic existingTrackStatistic = conn.Query <TrackStatistic>("SELECT * FROM TrackStatistic WHERE SafePath=?", trackStatistic.Path.ToSafePath()).FirstOrDefault();

                            if (existingTrackStatistic != null)
                            {
                                existingTrackStatistic.PlayCount      = trackStatistic.PlayCount;
                                existingTrackStatistic.SkipCount      = trackStatistic.SkipCount;
                                existingTrackStatistic.DateLastPlayed = trackStatistic.DateLastPlayed;
                                conn.Update(existingTrackStatistic);
                            }
                            else
                            {
                                conn.Insert(trackStatistic);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogClient.Error("Could not update statistics for path='{0}'. Exception: {1}", trackStatistic.Path, ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogClient.Error("Could not connect to the database. Exception: {0}", ex.Message);
                }
            });
        }
Exemplo n.º 11
0
        private bool ProcessTrack(Track track, SQLiteConnection conn)
        {
            bool processingSuccessful = false;

            var newTrackStatistic = new TrackStatistic();
            var newAlbum          = new Album();
            var newArtist         = new Artist();
            var newGenre          = new Genre();

            try
            {
                MetadataUtils.SplitMetadata(track.Path, ref track, ref newTrackStatistic, ref newAlbum, ref newArtist, ref newGenre);
                processingSuccessful = true;
            }
            catch (Exception ex)
            {
                processingSuccessful = false;
                LogClient.Error("Error while retrieving tag information for file {0}. File not added to the database. Exception: {1}", track.Path, ex.Message);
            }

            if (processingSuccessful)
            {
                // Check if such TrackStatistic already exists in the database
                if (!this.cache.HasCachedTrackStatistic(newTrackStatistic))
                {
                    // If not, add it.
                    conn.Insert(newTrackStatistic);
                }

                // Check if such Artist already exists in the database
                if (!this.cache.HasCachedArtist(ref newArtist))
                {
                    // If not, add it.
                    conn.Insert(newArtist);
                }

                // Check if such Genre already exists in the database
                if (!this.cache.HasCachedGenre(ref newGenre))
                {
                    // If not, add it.
                    conn.Insert(newGenre);
                }

                // Check if such Album already exists in the database
                if (!this.cache.HasCachedAlbum(ref newAlbum))
                {
                    // If Not, add it.
                    conn.Insert(newAlbum);
                }
                else
                {
                    // Make sure the Year of the existing album is updated
                    Album dbAlbum = conn.Table <Album>().Where((a) => a.AlbumID.Equals(newAlbum.AlbumID)).FirstOrDefault();

                    if (dbAlbum != null)
                    {
                        dbAlbum.Year = newAlbum.Year;
                        conn.Update(dbAlbum);
                    }
                }

                track.AlbumID  = newAlbum.AlbumID;
                track.ArtistID = newArtist.ArtistID;
                track.GenreID  = newGenre.GenreID;
            }

            return(processingSuccessful);
        }
Exemplo n.º 12
0
        public static void SplitMetadata(string path, ref Track track, ref TrackStatistic trackStatistic, ref Album album, ref Artist artist, ref Genre genre)
        {
            if (!string.IsNullOrEmpty(path))
            {
                var fmd = new FileMetadata(path);

                // Track information
                track.Path          = path;
                track.SafePath      = path.ToSafePath();
                track.FileName      = FileUtils.NameWithoutExtension(path);
                track.Duration      = Convert.ToInt64(fmd.Duration.TotalMilliseconds);
                track.MimeType      = fmd.MimeType;
                track.BitRate       = fmd.BitRate;
                track.SampleRate    = fmd.SampleRate;
                track.TrackTitle    = MetadataUtils.SanitizeTag(fmd.Title.Value);
                track.TrackNumber   = MetadataUtils.SafeConvertToLong(fmd.TrackNumber.Value);
                track.TrackCount    = MetadataUtils.SafeConvertToLong(fmd.TrackCount.Value);
                track.DiscNumber    = MetadataUtils.SafeConvertToLong(fmd.DiscNumber.Value);
                track.DiscCount     = MetadataUtils.SafeConvertToLong(fmd.DiscCount.Value);
                track.Year          = MetadataUtils.SafeConvertToLong(fmd.Year.Value);
                track.HasLyrics     = string.IsNullOrWhiteSpace(fmd.Lyrics.Value) ? 0 : 1;
                track.NeedsIndexing = 0;

                // TrackStatistic information
                trackStatistic.Path     = path;
                trackStatistic.SafePath = path.ToSafePath();
                trackStatistic.Rating   = fmd.Rating.Value;

                // Before proceeding, get the available artists
                string albumArtist = GetFirstAlbumArtist(fmd);
                string trackArtist = GetFirstArtist(fmd); // will be used for the album if no album artist is found

                // Album information
                album.AlbumTitle  = string.IsNullOrWhiteSpace(fmd.Album.Value) ? Defaults.UnknownAlbumString : MetadataUtils.SanitizeTag(fmd.Album.Value);
                album.AlbumArtist = (albumArtist == Defaults.UnknownAlbumArtistString ? trackArtist : albumArtist);
                album.DateAdded   = FileUtils.DateCreatedTicks(path);

                UpdateAlbumYear(album, MetadataUtils.SafeConvertToLong(fmd.Year.Value));

                // Artist information
                artist.ArtistName = trackArtist;

                // Genre information
                genre.GenreName = GetFirstGenre(fmd);

                // Metadata hash
                System.Text.StringBuilder sb = new System.Text.StringBuilder();

                sb.Append(album.AlbumTitle);
                sb.Append(artist.ArtistName);
                sb.Append(genre.GenreName);
                sb.Append(track.TrackTitle);
                sb.Append(track.TrackNumber);
                sb.Append(track.Year);
                track.MetaDataHash = CryptographyUtils.MD5Hash(sb.ToString());

                // File information
                track.FileSize         = FileUtils.SizeInBytes(path);
                track.DateFileModified = FileUtils.DateModifiedTicks(path);
                track.DateLastSynced   = DateTime.Now.Ticks;
            }
        }
Exemplo n.º 13
0
        private void ProcessTrack(Track track, SQLiteConnection conn)
        {
            var newTrackStatistic = new TrackStatistic();
            var newAlbum          = new Album()
            {
                NeedsIndexing = 1
            };                                                // Make sure album art gets indexed for this album
            var newArtist = new Artist();
            var newGenre  = new Genre();

            try
            {
                MetadataUtils.SplitMetadata(this.fileMetadataFactory.Create(track.Path), ref track, ref newTrackStatistic, ref newAlbum, ref newArtist, ref newGenre);

                // Check if such TrackStatistic already exists in the database
                if (!this.cache.HasCachedTrackStatistic(newTrackStatistic))
                {
                    // If not, add it.
                    conn.Insert(newTrackStatistic);
                }

                // Check if such Artist already exists in the database
                if (!this.cache.HasCachedArtist(ref newArtist))
                {
                    // If not, add it.
                    conn.Insert(newArtist);
                }

                // Check if such Genre already exists in the database
                if (!this.cache.HasCachedGenre(ref newGenre))
                {
                    // If not, add it.
                    conn.Insert(newGenre);
                }

                // Check if such Album already exists in the database
                if (!this.cache.HasCachedAlbum(ref newAlbum))
                {
                    // If Not, add it.
                    conn.Insert(newAlbum);
                }
                else
                {
                    // Make sure the Year of the existing album is updated
                    Album dbAlbum = conn.Table <Album>().Where((a) => a.AlbumID.Equals(newAlbum.AlbumID)).FirstOrDefault();

                    if (dbAlbum != null)
                    {
                        dbAlbum.Year = newAlbum.Year;

                        // A track from this album has changed, so make sure album art gets re-indexed.
                        dbAlbum.NeedsIndexing = 1;
                        conn.Update(dbAlbum);
                    }
                }

                track.IndexingSuccess = 1;
                track.AlbumID         = newAlbum.AlbumID;
                track.ArtistID        = newArtist.ArtistID;
                track.GenreID         = newGenre.GenreID;
            }
            catch (Exception ex)
            {
                // When updating tracks: for tracks that were indexed successfully in the past and had IndexingSuccess = 1
                track.IndexingSuccess       = 0;
                track.AlbumID               = 0;
                track.ArtistID              = 0;
                track.GenreID               = 0;
                track.IndexingFailureReason = ex.Message;

                LogClient.Error("Error while retrieving tag information for file {0}. Exception: {1}", track.Path, ex.Message);
            }

            return;
        }