Example #1
0
        public Album(DirectoryInfo sourceDirectory, IProgress progress, CancellationToken cancellationToken, Session session, Context context)
            : this(session, context)
        {
            var discDirectories = new List<DirectoryInfo>();
            discDirectories.Add(sourceDirectory);
            discDirectories.AddRange(sourceDirectory.GetDirectories());

            progress.Begin(discDirectories.Sum(directory => directory.GetFiles("*.mp3").Length));

            foreach (var directory in discDirectories)
            {
                var disc = new Disc(this, _session, _context);

                foreach (var file in directory.GetFiles("*.mp3"))
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    progress.Advance(file.Name);

                    var track = new Track(file, disc, _session, _context);
                    disc.Tracks.Add(track);
                }

                if (disc.Tracks.Count > 0)
                    Discs.Add(disc);
            }

            SourceDirectory = sourceDirectory;
        }
Example #2
0
 public IEnumerable<IValidator> GetTrackValidators(Track track)
 {
     yield return new TrackNameValidator(track, GetTextValidators());
 }
Example #3
0
        public void SaveTo(DirectoryInfo discDirectory, IProgress progress, CancellationToken cancellationToken)
        {
            var sourceFilePath = SourceFile.FullName;
            var mpegDataOffset = _mpegDataOffset;
            var mpegDataSize = _mpegDataSize;
            string reEncodedFilePath = null;

            if (NeedsReEncoding() && _session.ReEncodeFiles)
            {
                var encoder = _context.GetEncoder();
                reEncodedFilePath = Path.GetTempFileName();

                var minBirtate = Disc.MostCommonBitrate != null ? Disc.MostCommonBitrate.Value : MpegFileInfo.MinBitrate;
                var maxBirtate = Disc.MostCommonBitrate != null ? Disc.MostCommonBitrate.Value : MpegFileInfo.MaxBitrate;
                var sampleRate = Disc.MostCommonSampleRate.Value;
                var channelMode = Disc.MostCommonChannelMode.Value;

                encoder.Encode(sourceFilePath, reEncodedFilePath, minBirtate, maxBirtate, sampleRate, channelMode);

                var tempTrack = new Track(new FileInfo(reEncodedFilePath), null, _session, _context);
                mpegDataOffset = tempTrack._mpegDataOffset;
                mpegDataSize = tempTrack._mpegDataSize;

                sourceFilePath = reEncodedFilePath;
            }

            using (var sourceStream = File.Open(sourceFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                using (var destinationStream = File.Open(Path.Combine(discDirectory.FullName, FileName), FileMode.CreateNew, FileAccess.Write, FileShare.None))
                {
                    var id3Tagv2 = new Id3Tagv2();
                    id3Tagv2.Version = new Version(3, 0);
                    id3Tagv2.Frames.Add(new Tpe1Frame(Disc.Album.ArtistName));
                    id3Tagv2.Frames.Add(new TyerFrame(Disc.Album.ReleaseYear));
                    id3Tagv2.Frames.Add(new TalbFrame(Disc.Album.FullName));
                    id3Tagv2.Frames.Add(new TposFrame(string.Format("{0}/{1}", Disc.Index, Disc.Album.Discs.Count)));
                    id3Tagv2.Frames.Add(new TrckFrame(string.Format("{0}/{1}", Index, Disc.Tracks.Count)));
                    id3Tagv2.Frames.Add(new Tit2Frame(Name));
                    id3Tagv2.Frames.Add(new PrivFrame("RHAPSODY", Encoding.ASCII.GetBytes(string.Format("RHAPSODY/{0}", VersionHelper.GetAppVersion()))));

                    if (Disc.Album.Cover != null)
                        id3Tagv2.Frames.Add(new ApicFrame(Disc.Album.Cover, PictureType.FrontCover));

                    if (!string.IsNullOrEmpty(Disc.Name))
                        id3Tagv2.Frames.Add(new TsstFrame(Disc.Name));

                    id3Tagv2.WriteTo(destinationStream);

                    StreamHelper.Copy(sourceStream, destinationStream, mpegDataOffset, mpegDataSize);
                }
            }

            if (reEncodedFilePath != null)
                File.Delete(reEncodedFilePath);
        }