예제 #1
0
        internal MetadataToIlstAtomAdapter([NotNull] AudioMetadata metadata, bool compressCoverArt)
        {
            if (!string.IsNullOrEmpty(metadata.Title))
            {
                _atoms.Add(new TextAtom("©nam", metadata.Title));
            }
            if (!string.IsNullOrEmpty(metadata.Artist))
            {
                _atoms.Add(new TextAtom("©ART", metadata.Artist));
            }
            if (!string.IsNullOrEmpty(metadata.Album))
            {
                _atoms.Add(new TextAtom("©alb", metadata.Album));
            }
            if (!string.IsNullOrEmpty(metadata.AlbumArtist))
            {
                _atoms.Add(new TextAtom("aART", metadata.AlbumArtist));
            }
            if (!string.IsNullOrEmpty(metadata.Composer))
            {
                _atoms.Add(new TextAtom("©wrt", metadata.Composer));
            }
            if (!string.IsNullOrEmpty(metadata.Genre))
            {
                _atoms.Add(new TextAtom("©gen", metadata.Genre));
            }
            if (!string.IsNullOrEmpty(metadata.Comment))
            {
                _atoms.Add(new TextAtom("©cmt", metadata.Comment));
            }

            if (!string.IsNullOrEmpty(metadata.Day) &&
                !string.IsNullOrEmpty(metadata.Month) &&
                !string.IsNullOrEmpty(metadata.Year))
            {
                _atoms.Add(new TextAtom("©day",
                                        $"{metadata.Year}-{metadata.Month}-{metadata.Day}"));
            }
            else if (!string.IsNullOrEmpty(metadata.Year))
            {
                _atoms.Add(new TextAtom("©day", metadata.Year));
            }

            if (!string.IsNullOrEmpty(metadata.TrackNumber))
            {
                _atoms.Add(new TrackNumberAtom(metadata.TrackNumber, metadata.TrackCount));
            }

            if (metadata.CoverArt != null)
            {
                _atoms.Add(new CoverAtom(compressCoverArt
                    ? CoverArtFactory.ConvertToLossy(metadata.CoverArt)
                    : metadata.CoverArt));
            }
        }
        internal MetadataToTagModelAdapter([NotNull] AudioMetadata metadata, [NotNull] string encoding)
        {
            AddTextFrame("TIT2", metadata.Title, encoding);
            AddTextFrame("TPE1", metadata.Artist, encoding);
            AddTextFrame("TALB", metadata.Album, encoding);
            AddTextFrame("TPE2", metadata.AlbumArtist, encoding);
            AddTextFrame("TCOM", metadata.Composer, encoding);
            AddTextFrame("TCON", metadata.Genre, encoding);
            AddFullTextFrame("COMM", metadata.Comment, "eng", encoding);
            AddTextFrame("TDAT", GetDateText(metadata), encoding);
            AddTextFrame("TYER", metadata.Year, encoding);
            AddTextFrame("TRCK", GetTrackText(metadata), encoding);

            // ReplayGain fields are always in Latin-1, encoding as per specification
            AddUserDefinedFrame("REPLAYGAIN_TRACK_PEAK", metadata.TrackPeak, "Latin1", true);
            AddUserDefinedFrame("REPLAYGAIN_ALBUM_PEAK", metadata.AlbumPeak, "Latin1", true);
            if (!string.IsNullOrEmpty(metadata.TrackGain))
            {
                AddUserDefinedFrame("REPLAYGAIN_TRACK_GAIN", $"{metadata.TrackGain} dB", "Latin1", true);
            }
            if (!string.IsNullOrEmpty(metadata.AlbumGain))
            {
                AddUserDefinedFrame("REPLAYGAIN_ALBUM_GAIN", $"{metadata.AlbumGain} dB", "Latin1", true);
            }

            if (metadata.CoverArt == null)
            {
                return;
            }

            // Always store images in JPEG format since MP3 is also lossy
            var lossyCoverArt = CoverArtFactory.ConvertToLossy(metadata.CoverArt);

            Add(new FramePicture("APIC")
            {
                PictureType = PictureTypeCode.CoverFront,
                Mime        = lossyCoverArt.MimeType,
                PictureData = lossyCoverArt.Data.ToArray()
            });
        }
        internal unsafe MetadataToOpusCommentAdapter([NotNull] AudioMetadata metadata)
        {
            Handle = SafeNativeMethods.OpusEncoderCommentsCreate();

            if (!string.IsNullOrEmpty(metadata.Title))
            {
                AddTag("TITLE", metadata.Title);
            }
            if (!string.IsNullOrEmpty(metadata.Artist))
            {
                AddTag("ARTIST", metadata.Artist);
            }
            if (!string.IsNullOrEmpty(metadata.Album))
            {
                AddTag("ALBUM", metadata.Album);
            }
            if (!string.IsNullOrEmpty(metadata.AlbumArtist))
            {
                AddTag("ALBUMARTIST", metadata.AlbumArtist);
            }
            if (!string.IsNullOrEmpty(metadata.Composer))
            {
                AddTag("COMPOSER", metadata.Composer);
            }
            if (!string.IsNullOrEmpty(metadata.Genre))
            {
                AddTag("GENRE", metadata.Genre);
            }
            if (!string.IsNullOrEmpty(metadata.Comment))
            {
                AddTag("DESCRIPTION", metadata.Comment);
            }

            if (!string.IsNullOrEmpty(metadata.Day) &&
                !string.IsNullOrEmpty(metadata.Month) &&
                !string.IsNullOrEmpty(metadata.Year))
            {
                AddTag("DATE", $"{metadata.Year}-{metadata.Month}-{metadata.Day}");
            }
            else if (!string.IsNullOrEmpty(metadata.Year))
            {
                AddTag("YEAR", metadata.Year);
            }

            if (!string.IsNullOrEmpty(metadata.TrackNumber))
            {
                AddTag("TRACKNUMBER", !string.IsNullOrEmpty(metadata.TrackCount)
                    ? $"{metadata.TrackNumber}/{metadata.TrackCount}"
                    : metadata.TrackNumber);
            }

            if (!string.IsNullOrEmpty(metadata.TrackGain))
            {
                AddTag("R128_TRACK_GAIN", ConvertGain(metadata.TrackGain));
            }
            if (!string.IsNullOrEmpty(metadata.AlbumGain))
            {
                AddTag("R128_ALBUM_GAIN", ConvertGain(metadata.AlbumGain));
            }

            if (metadata.CoverArt == null)
            {
                return;
            }

            // Always store images in JPEG format since Vorbis is also lossy
            var coverArt = CoverArtFactory.ConvertToLossy(metadata.CoverArt);

            fixed(byte *coverArtAddress = coverArt.Data)
            {
                var error = SafeNativeMethods.OpusEncoderCommentsAddPictureFromMemory(
                    Handle,
                    coverArtAddress,
                    new IntPtr(coverArt.Data.Length),
                    -1,
                    IntPtr.Zero);

                if (error != 0)
                {
                    throw new AudioEncodingException($"Opus encountered error {error} writing the cover art.");
                }
            }
        }
예제 #4
0
        internal MetadataToTagModelAdapter(AudioMetadata metadata, int version, string encoding)
        {
            Header.Version = (byte)version;

            var textType = encoding switch
            {
                "UTF16" => TextType.Utf16,
                "UTF8" => TextType.Utf8,
                _ => TextType.Ascii
            };

            AddTextFrame("TIT2", metadata.Title, textType);
            AddTextFrame("TPE1", metadata.Artist, textType);
            AddTextFrame("TALB", metadata.Album, textType);
            AddTextFrame("TPE2", metadata.AlbumArtist, textType);
            AddTextFrame("TCOM", metadata.Composer, textType);
            AddTextFrame("TCON", metadata.Genre, textType);
            AddTextFrame("COMM", metadata.Comment, textType);

            if (version == 3)
            {
                AddTextFrame("TDAT", GetDateText(metadata), textType);
                AddTextFrame("TYER", metadata.Year, textType);
            }
            else
            {
                AddTextFrame("TDRC", GetTimeStamp(metadata), textType);
            }

            AddTextFrame("TRCK", GetTrackText(metadata), textType);

            AddReplayGainFrame(metadata.TrackPeak, "REPLAYGAIN_TRACK_PEAK");
            AddReplayGainFrame(metadata.AlbumPeak, "REPLAYGAIN_ALBUM_PEAK");
            AddReplayGainFormattedFrame(metadata.TrackGain, "REPLAYGAIN_TRACK_GAIN");
            AddReplayGainFormattedFrame(metadata.AlbumGain, "REPLAYGAIN_ALBUM_GAIN");

            if (metadata.CoverArt == null)
            {
                return;
            }

            // Always store images in JPEG format, since MP3 is also lossy
            var lossyCoverArt = CoverArtFactory.ConvertToLossy(metadata.CoverArt);

            Frames.Add(new FramePicture
            {
                PictureType = PictureType.CoverFront,
                Mime        = lossyCoverArt.MimeType,
                PictureData = lossyCoverArt.Data.ToArray()
            });
        }

        void AddReplayGainFormattedFrame(string value, string description)
        {
            if (!string.IsNullOrEmpty(value))
            {
                AddReplayGainFrame($"{value} dB", description);
            }
        }

        void AddReplayGainFrame(string value, string description) => AddTextFrame("TXXX", value, TextType.Ascii, description, true);

        void AddTextFrame(string frameId, string value, TextType textType = TextType.Ascii, string?description = null, bool fileAlter = false)
        {
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            var frame = (FrameText)FrameFactory.Build(frameId);

            frame.Text      = value;
            frame.TextType  = textType;
            frame.FileAlter = fileAlter;

            if (description != null)
            {
                if (frame is IFrameDescription frameDescription)
                {
                    frameDescription.Description = description;
                }
            }

            Frames.Add(frame);
        }
        internal MetadataToVorbisCommentAdapter([NotNull] AudioMetadata metadata)
        {
            SafeNativeMethods.VorbisCommentInit(out _comment);

            if (!string.IsNullOrEmpty(metadata.Title))
            {
                AddTag("TITLE", metadata.Title);
            }
            if (!string.IsNullOrEmpty(metadata.Artist))
            {
                AddTag("ARTIST", metadata.Artist);
            }
            if (!string.IsNullOrEmpty(metadata.Album))
            {
                AddTag("ALBUM", metadata.Album);
            }
            if (!string.IsNullOrEmpty(metadata.AlbumArtist))
            {
                AddTag("ALBUMARTIST", metadata.AlbumArtist);
            }
            if (!string.IsNullOrEmpty(metadata.Composer))
            {
                AddTag("COMPOSER", metadata.Composer);
            }
            if (!string.IsNullOrEmpty(metadata.Genre))
            {
                AddTag("GENRE", metadata.Genre);
            }
            if (!string.IsNullOrEmpty(metadata.Comment))
            {
                AddTag("DESCRIPTION", metadata.Comment);
            }

            if (!string.IsNullOrEmpty(metadata.Day) &&
                !string.IsNullOrEmpty(metadata.Month) &&
                !string.IsNullOrEmpty(metadata.Year))
            {
                AddTag("DATE", $"{metadata.Year}-{metadata.Month}-{metadata.Day}");
            }
            else if (!string.IsNullOrEmpty(metadata.Year))
            {
                AddTag("YEAR", metadata.Year);
            }

            if (!string.IsNullOrEmpty(metadata.TrackNumber))
            {
                AddTag("TRACKNUMBER", !string.IsNullOrEmpty(metadata.TrackCount)
                    ? $"{metadata.TrackNumber}/{metadata.TrackCount}"
                    : metadata.TrackNumber);
            }

            if (!string.IsNullOrEmpty(metadata.TrackPeak))
            {
                AddTag("REPLAYGAIN_TRACK_PEAK", metadata.TrackPeak);
            }
            if (!string.IsNullOrEmpty(metadata.AlbumPeak))
            {
                AddTag("REPLAYGAIN_ALBUM_PEAK", metadata.AlbumPeak);
            }
            if (!string.IsNullOrEmpty(metadata.TrackGain))
            {
                AddTag("REPLAYGAIN_TRACK_GAIN", $"{metadata.TrackGain} dB");
            }
            if (!string.IsNullOrEmpty(metadata.AlbumGain))
            {
                AddTag("REPLAYGAIN_ALBUM_GAIN", $"{metadata.AlbumGain} dB");
            }

            // Always store images in JPEG format since Vorbis is also lossy
            if (metadata.CoverArt != null)
            {
                AddTag("METADATA_BLOCK_PICTURE", CoverArtAdapter.ToBase64(
                           CoverArtFactory.ConvertToLossy(metadata.CoverArt)));
            }
        }