Exemple #1
0
        private async Task ResampleAsync(MusicDb db)
        {
            db.ChangeTracker.AutoDetectChangesEnabled = false;
            taskItem = await db.TaskItems.FindAsync(taskId);

            var sources        = new MusicSources(musicOptions, true);
            var vbrDestination = sources.FirstOrDefault(x => x.IsGenerated);

            if (vbrDestination != null)
            {
                var work = await db.Works.SingleAsync(x => x.UID.ToString() == taskItem.TaskString);

                foreach (var track in work.Tracks)
                {
                    var mf = track.MusicFiles.First(f => f.Encoding == EncodingType.flac);
                    var destinationFile = mf.File.Replace(mf.DiskRoot, vbrDestination.DiskRoot, StringComparison.CurrentCultureIgnoreCase);
                    destinationFile = destinationFile.Replace(".flac", ".mp3", StringComparison.CurrentCultureIgnoreCase);
                    var srcFi = new FileInfo(mf.File);
                    var vbrFi = new FileInfo(destinationFile);
                    if (!vbrFi.Exists || srcFi.LastWriteTimeUtc > vbrFi.LastWriteTimeUtc)
                    {
                        var existingVbr = track.MusicFiles.FirstOrDefault(f => f.IsGenerated);
                        if (existingVbr != null)
                        {
                            track.MusicFiles.Remove(existingVbr);
                            db.MusicFiles.Remove(existingVbr);
                        }
                        await Resample(srcFi.FullName, vbrFi.FullName);

                        vbrFi.Refresh();
                        AddToTrack(vbrDestination, track, mf, vbrFi);
                    }
                }
            }
            else
            {
                log.Warning($"{taskItem} Resampling failed as no source for generated files is available");
            }
            taskItem.FinishedAt = DateTimeOffset.Now;
            taskItem.Status     = Music.Core.TaskStatus.Finished;
            await db.SaveChangesAsync();
        }
 public SongItemDownloader(MusicSources musicSources, string target, MergedSong song)
 {
     this.musicSources = musicSources;
     this.target       = target;
     this.song         = song;
 }
Exemple #3
0
        private async Task ResampleAsync(long taskId)
        {
            using (var db = new MusicDb(connectionString))
            {
                TaskItem taskItem = null;
                db.ChangeTracker.AutoDetectChangesEnabled = false;
                taskItem = await db.TaskItems.FindAsync(taskId);

                var sources        = new MusicSources(musicOptions, true);
                var vbrDestination = sources.FirstOrDefault(x => x.IsGenerated);
                if (vbrDestination != null)
                {
                    var work = await db.Works.SingleOrDefaultAsync(x => x.UID.ToString() == taskItem.TaskString);

                    if (work != null)
                    {
                        //log.Information($"Resampling {work.Artist.Name}, {work.Name}");
                        int resampledCount = 0;
                        foreach (var track in work.Tracks)
                        {
                            var mf = track.MusicFiles.First(f => f.Encoding == EncodingType.flac);
                            var destinationFile = mf.File.Replace(mf.DiskRoot, vbrDestination.DiskRoot, StringComparison.CurrentCultureIgnoreCase);
                            destinationFile = destinationFile.Replace(".flac", ".mp3", StringComparison.CurrentCultureIgnoreCase);
                            var srcFi = new FileInfo(mf.File);
                            var vbrFi = new FileInfo(destinationFile);
                            if (!vbrFi.Exists || srcFi.LastWriteTimeUtc > vbrFi.LastWriteTimeUtc)
                            {
                                await Resample(taskItem, srcFi.FullName, vbrFi.FullName);

                                vbrFi.Refresh();
                                resampledCount++;
                            }
                            else
                            {
                                log.Debug($"{taskItem} {vbrFi.Name} is up to date");
                            }
                            var existingVbr = track.MusicFiles.FirstOrDefault(f => f.IsGenerated);
                            if (existingVbr != null)
                            {
                                track.MusicFiles.Remove(existingVbr);
                                db.MusicFiles.Remove(existingVbr);
                            }
                            AddToTrack(vbrDestination, track, mf, vbrFi);
                            await db.SaveChangesAsync();
                        }
                        log.Information($"{taskItem} resampling completed {work.Artist.Name}, {work.Name}, {resampledCount}/{work.Tracks.Count()} files");
                    }
                    else
                    {
                        log.Warning($"{taskItem} work with uid {taskItem.TaskString} not found");
                        taskItem.FinishedAt = DateTimeOffset.Now;
                        taskItem.Status     = Music.Core.TaskStatus.Failed;
                        await db.SaveChangesAsync();

                        return;
                    }
                }
                else
                {
                    log.Warning($"{taskItem} Resampling failed as no source for generated files is available");
                }
                taskItem.FinishedAt = DateTimeOffset.Now;
                taskItem.Status     = Music.Core.TaskStatus.Finished;
                await db.SaveChangesAsync();
            }
        }
 public SongDownloader(MusicSources musicSources, string target)
 {
     this.musicSources = musicSources;
     this.target       = target;
 }
        /// <summary>
        /// Gets Pathdata instance for the given disk path. The path is parsed to establish source, style, artist and, if present, opus
        /// and the result is checked for existence otherwise returns null
        ///
        /// </summary>
        /// <param name="musicOptions"></param>
        /// <param name="diskPath"></param>
        /// <returns></returns>
        public static PathData GetPathData(MusicOptions musicOptions, string diskPath, bool allowDeleted = false)
        {
            PathData    pd      = null;
            var         sources = new MusicSources(musicOptions);
            var         matches = sources.Where(s => diskPath.StartsWith(s.DiskRoot, StringComparison.CurrentCultureIgnoreCase));
            MusicSource source  = null;

            switch (matches.Count())
            {
            case int count when count == 1:
                source = matches.First();
                break;

            case int count when count > 1:
                source = matches.OrderByDescending(x => x.DiskRoot.Length).First();
                break;

            default:
                break;
            }
            if (source != null)
            {
                var relativeDiskPath = Path.GetRelativePath(source.DiskRoot, diskPath);
                var styleInfo        = musicOptions.Styles.Where(s => s.Enabled).SingleOrDefault(s => s.Settings.Select(t => t.Path).Any(p => relativeDiskPath.StartsWith(p, StringComparison.CurrentCultureIgnoreCase)));
                if (styleInfo != null)
                {
                    var ss             = styleInfo.Settings.Single(s => relativeDiskPath.StartsWith(s.Path, StringComparison.CurrentCultureIgnoreCase));
                    var remainingParts = Path.GetRelativePath(ss.Path, relativeDiskPath).Split(Path.DirectorySeparatorChar);
                    if (remainingParts.Length == 1 && remainingParts[0] == ".")
                    {
                        pd = new PathData
                        {
                            DiskRoot    = source.DiskRoot,
                            MusicStyle  = styleInfo.Style,
                            StylePath   = ss.Path,
                            IsGenerated = source.IsGenerated
                        };
                    }
                    else
                    {
                        var artistName = remainingParts.First();
                        if (!artistName.StartsWith("$"))
                        {
                            if (!styleInfo.Filter || styleInfo.IncludeArtists.Any(x => x.IsEqualIgnoreAccentsAndCase(artistName)))
                            {
                                switch (remainingParts.Length)
                                {
                                case int l when l == 1:
                                    var t1 = Path.Combine(source.DiskRoot, ss.Path, artistName);
                                    if (allowDeleted == true || (Directory.Exists(t1) || Directory.Exists(t1.RemoveDiacritics())))
                                    {
                                        pd = new PathData
                                        {
                                            DiskRoot      = source.DiskRoot,
                                            MusicStyle    = styleInfo.Style,
                                            StylePath     = ss.Path,
                                            ArtistPath    = artistName,
                                            IsGenerated   = source.IsGenerated,
                                            IsCollections = string.Compare(artistName, "collections", true) == 0
                                        };
                                    }
                                    break;

                                case int l when l > 1:
                                    var t2 = Path.Combine(source.DiskRoot, ss.Path, artistName, remainingParts.Skip(1).First());
                                    if (allowDeleted == true || (Directory.Exists(t2) || Directory.Exists(t2.RemoveDiacritics())))
                                    {
                                        pd = new PathData
                                        {
                                            DiskRoot      = source.DiskRoot,
                                            MusicStyle    = styleInfo.Style,
                                            StylePath     = ss.Path,
                                            ArtistPath    = artistName,
                                            OpusPath      = remainingParts.Skip(1).First(),
                                            IsGenerated   = source.IsGenerated,
                                            IsCollections = string.Compare(artistName, "collections", true) == 0
                                        };
                                    }
                                    break;
                                }
                            }
                        }
                        else
                        {
                            // for the moment only $portraits
                            pd = new PathData
                            {
                                DiskRoot    = source.DiskRoot,
                                MusicStyle  = styleInfo.Style,
                                StylePath   = ss.Path,
                                IsGenerated = source.IsGenerated,
                                IsPortraits = true
                            };
                        }
                    }
                }
            }
            return(pd);
        }