示例#1
0
        public static UserUrlLinkFrame Get(Tag tag, string description, StringType type, bool create)
        {
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }
            if (description.Length == 0)
            {
                throw new ArgumentException("Description must not be empty.", "description");
            }
            foreach (UserUrlLinkFrame frame in tag.GetFrames <UserUrlLinkFrame>(FrameType.WXXX))
            {
                if (description.Equals(frame.Description))
                {
                    return(frame);
                }
            }
            if (!create)
            {
                return(null);
            }
            UserUrlLinkFrame new_frame = new UserUrlLinkFrame(description, type);

            tag.AddFrame(new_frame);
            return(new_frame);
        }
        public static GeneralEncapsulatedObjectFrame Get(Tag tag,
                                                         string description,
                                                         bool create)
        {
            GeneralEncapsulatedObjectFrame geob;

            foreach (Frame frame in tag.GetFrames(FrameType.GEOB))
            {
                geob = frame as GeneralEncapsulatedObjectFrame;

                if (geob == null)
                {
                    continue;
                }

                if (geob.Description != description)
                {
                    continue;
                }

                return(geob);
            }

            if (!create)
            {
                return(null);
            }

            geob             = new GeneralEncapsulatedObjectFrame();
            geob.Description = description;
            tag.AddFrame(geob);
            return(geob);
        }
示例#3
0
        public static UrlLinkFrame Get(Tag tag, ByteVector ident, bool create)
        {
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }
            if (ident == null)
            {
                throw new ArgumentNullException("ident");
            }
            if (ident.Count != 4)
            {
                throw new ArgumentException("Identifier must be four bytes long.", "ident");
            }
            foreach (UrlLinkFrame frame in tag.GetFrames <UrlLinkFrame>(ident))
            {
                return(frame);
            }
            if (!create)
            {
                return(null);
            }
            UrlLinkFrame new_frame = new UrlLinkFrame(ident);

            tag.AddFrame(new_frame);
            return(new_frame);
        }
示例#4
0
        public static TextInformationFrame Get(Tag tag, ByteVector ident, StringType encoding, bool create)
        {
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }
            if (ident == null)
            {
                throw new ArgumentNullException("ident");
            }
            if (ident.Count != 4)
            {
                throw new ArgumentException("Identifier must be four bytes long.", "ident");
            }
            foreach (TextInformationFrame frame in tag.GetFrames <TextInformationFrame>(ident))
            {
                return(frame);
            }
            if (!create)
            {
                return(null);
            }
            TextInformationFrame new_frame = new TextInformationFrame(ident, encoding);

            tag.AddFrame(new_frame);
            return(new_frame);
        }
示例#5
0
        public static UnsynchronisedLyricsFrame Get(Tag tag, string description, string language, bool create)
        {
            UnsynchronisedLyricsFrame uslt;

            foreach (Frame frame in tag.GetFrames(FrameType.USLT))
            {
                uslt = frame as UnsynchronisedLyricsFrame;
                if (uslt == null)
                {
                    continue;
                }
                if (uslt.Description != description)
                {
                    continue;
                }
                if (language != null && language != uslt.Language)
                {
                    continue;
                }
                return(uslt);
            }
            if (!create)
            {
                return(null);
            }
            uslt = new UnsynchronisedLyricsFrame(description, language);
            tag.AddFrame(uslt);
            return(uslt);
        }
        /// <summary>
        ///    Gets a specified lyrics frame from the specified tag,
        ///    optionally creating it if it does not exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="description">
        ///    A <see cref="string" /> object specifying the description
        ///    to match.
        /// </param>
        /// <param name="language">
        ///    A <see cref="string" /> object specifying the ISO-639-2
        ///    language code to match.
        /// </param>
        /// <param name="type">
        ///    A <see cref="SynchedTextType" /> value specifying the
        ///    text type to match.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="SynchronisedLyricsFrame" /> object
        ///    containing the matching frame, or <see langword="null" />
        ///    if a match wasn't found and <paramref name="create" /> is
        ///    <see langword="false" />.
        /// </returns>
        public static SynchronisedLyricsFrame Get(Tag tag, string description, string language, SynchedTextType type, bool create)
        {
            foreach (Frame f in tag)
            {
                if (!(f is SynchronisedLyricsFrame lyr))
                {
                    continue;
                }

                if (lyr.Description == description &&
                    (language == null ||
                     language == lyr.Language) &&
                    type == lyr.Type)
                {
                    return(lyr);
                }
            }

            if (!create)
            {
                return(null);
            }

            var frame = new SynchronisedLyricsFrame(description, language, type);

            tag.AddFrame(frame);
            return(frame);
        }
示例#7
0
        public static CommentsFrame Get(Tag tag, string description, string language, bool create)
        {
            CommentsFrame comm;

            foreach (Frame frame in tag.GetFrames(FrameType.COMM))
            {
                comm = frame as CommentsFrame;
                if (comm == null)
                {
                    continue;
                }
                if (comm.Description != description)
                {
                    continue;
                }
                if (language != null && language != comm.Language)
                {
                    continue;
                }
                return(comm);
            }
            if (!create)
            {
                return(null);
            }
            comm = new CommentsFrame(description, language);
            tag.AddFrame(comm);
            return(comm);
        }
示例#8
0
        public static AttachedPictureFrame Get(Tag tag, string description, PictureType type, bool create)
        {
            AttachedPictureFrame apic;

            foreach (Frame frame in tag.GetFrames(FrameType.APIC))
            {
                apic = frame as AttachedPictureFrame;
                if (apic == null)
                {
                    continue;
                }
                if (description != null && apic.Description != description)
                {
                    continue;
                }
                if (type != PictureType.Other && apic.Type != type)
                {
                    continue;
                }
                return(apic);
            }
            if (!create)
            {
                return(null);
            }
            apic             = new AttachedPictureFrame();
            apic.Description = description;
            apic.Type        = type;
            tag.AddFrame(apic);
            return(apic);
        }
        /// <summary>
        ///    Gets a specified volume adjustment frame from the
        ///    specified tag, optionally creating it if it does not
        ///    exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="identification">
        ///    A <see cref="string" /> specifying the identification to
        ///    match.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="RelativeVolumeFrame" /> object containing
        ///    the matching frame, or <see langword="null" /> if a match
        ///    wasn't found and <paramref name="create" /> is <see
        ///    langword="false" />.
        /// </returns>
        public static RelativeVolumeFrame Get(Tag tag,
                                              string identification,
                                              bool create)
        {
            RelativeVolumeFrame rva2;

            foreach (Frame frame in tag.GetFrames(FrameType.RVA2))
            {
                rva2 = frame as RelativeVolumeFrame;

                if (rva2 == null)
                {
                    continue;
                }

                if (rva2.Identification != identification)
                {
                    continue;
                }

                return(rva2);
            }

            if (!create)
            {
                return(null);
            }

            rva2 = new RelativeVolumeFrame(identification);
            tag.AddFrame(rva2);
            return(rva2);
        }
示例#10
0
        /// <summary>
        ///    Gets a specified unique file identifer frame from the
        ///    specified tag, optionally creating it if it does not
        ///    exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="owner">
        ///    A <see cref="string" /> specifying the owner to match.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="UserTextInformationFrame" /> object
        ///    containing the matching frame, or <see langword="null" />
        ///    if a match wasn't found and <paramref name="create" /> is
        ///    <see langword="false" />.
        /// </returns>
        public static UniqueFileIdentifierFrame Get(Tag tag,
                                                    string owner,
                                                    bool create)
        {
            UniqueFileIdentifierFrame ufid;

            foreach (Frame frame in tag.GetFrames(FrameType.UFID))
            {
                ufid = frame as UniqueFileIdentifierFrame;

                if (ufid == null)
                {
                    continue;
                }

                if (ufid.Owner == owner)
                {
                    return(ufid);
                }
            }

            if (!create)
            {
                return(null);
            }

            ufid = new UniqueFileIdentifierFrame(owner, null);
            tag.AddFrame(ufid);
            return(ufid);
        }
示例#11
0
        public static UserTextInformationFrame Get(Tag tag, string description, StringType type, bool create, bool caseSensitive)
        {
            if (tag == null)
            {
                throw new ArgumentNullException("tag");
            }
            if (description == null)
            {
                throw new ArgumentNullException("description");
            }
            if (description.Length == 0)
            {
                throw new ArgumentException("Description must not be empty.", "description");
            }
            StringComparison stringComparison = caseSensitive?StringComparison.InvariantCulture:StringComparison.InvariantCultureIgnoreCase;

            foreach (UserTextInformationFrame frame in tag.GetFrames <UserTextInformationFrame>(FrameType.TXXX))
            {
                if (description.Equals(frame.Description, stringComparison))
                {
                    return(frame);
                }
            }
            if (!create)
            {
                return(null);
            }
            UserTextInformationFrame new_frame = new UserTextInformationFrame(description, type);

            tag.AddFrame(new_frame);
            return(new_frame);
        }
示例#12
0
        private void SetTagMainKey()
        {
            if (trackNameModel.MainKey == null)
            {
                return;
            }
            var tagKeyFrame = TextInformationFrame.Get(tag, "TKEY", true);

            tagKeyFrame.Text = new[] { trackNameModel.MainKey.ToString(KeyNotation.Sharp_M) };
            tag.AddFrame(tagKeyFrame);
        }
示例#13
0
        /// <summary>
        /// set text frame from id
        /// </summary>
        /// <param name="text"></param>
        public void SetText(string code, string text)
        {
            if (text != null)
            {
                if (code.StartsWith("T"))
                {
                    TextInformationFrame frame = GetTextFrame(code, true);

                    // add frame
                    if (frame == null)
                    {
                        frame = new TextInformationFrame(code);
                        tag.AddFrame(frame);
                    }

                    if (frame.Text.Length < 2) // one or zero
                    {
                        frame.Text = new string[1] {
                            text
                        };
                    }
                    else  // more than one
                    {
                        // add to front
                        string[] strs = new string[frame.Text.Length];
                        strs[0] = text;
                        Array.Copy(frame.Text, 1, strs, 1, frame.Text.Length - 1);
                    }
                }
                else if (code.StartsWith("W"))
                {
                    UnknownFrame frame = GetUnknownFrame(code);
                    if (frame != null)
                    {
                        byte[] byts = UTF8Encoding.UTF8.GetBytes(text);
                        frame.Data = new TagLib.ByteVector(byts);
                        frame.Data.Add((byte)0);
                    }
                }
            }
        }
示例#14
0
        public static TermsOfUseFrame Get(Tag tag, string language, bool create)
        {
            foreach (Frame f in tag.GetFrames(FrameType.USER))
            {
                TermsOfUseFrame cf = f as TermsOfUseFrame;
                if (cf != null && (language == null || language == cf.Language))
                {
                    return(cf);
                }
            }
            if (!create)
            {
                return(null);
            }
            TermsOfUseFrame frame = new TermsOfUseFrame(language);

            tag.AddFrame(frame);
            return(frame);
        }
示例#15
0
        /// <summary>
        ///    Gets a specified attachment frame from the specified tag,
        ///    optionally creating it if it does not exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="description">
        ///    A <see cref="string" /> specifying the description to
        ///    match.
        /// </param>
        /// <param name="type">
        ///    A <see cref="PictureType" /> specifying the picture type
        ///    to match.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="AttachmentFrame" /> object containing
        ///    the matching frame, or <see langword="null" /> if a match
        ///    wasn't found and <paramref name="create" /> is <see
        ///    langword="false" />.
        /// </returns>
        /// <example>
        ///    <para>Sets a cover image with a description. Because <see
        ///    cref="Get(Tag,string,PictureType,bool)" /> is used, if
        ///    the program is called again with the same audio file and
        ///    desciption, the picture will be overwritten with the new
        ///    one.</para>
        ///    <code lang="C#">
        /// using TagLib;
        /// using TagLib.Id3v2;
        ///
        /// public static class SetId3v2Cover
        /// {
        ///     public static void Main (string [] args)
        ///     {
        ///         if (args.Length != 3)
        ///             throw new ApplicationException (
        ///                 "USAGE: SetId3v2Cover.exe AUDIO_FILE PICTURE_FILE DESCRIPTION");
        ///
        ///         // Create the file. Can throw file to TagLib# exceptions.
        ///         File file = File.Create (args [0]);
        ///
        ///         // Get or create the ID3v2 tag.
        ///         TagLib.Id3v2.Tag tag = file.GetTag (TagTypes.Id3v2, true) as TagLib.Id3v2.Tag;
        ///         if (tag == null)
        ///             throw new ApplicationException ("File does not support ID3v2 tags.");
        ///
        ///         // Create a picture. Can throw file related exceptions.
        ///		TagLib.Picture picture = TagLib.Picture.CreateFromPath (args [1]);
        ///
        ///         // Get or create the picture frame.
        ///         AttachedPictureFrame frame = AttachedPictureFrame.Get (
        ///             tag, args [2], PictureType.FrontCover, true);
        ///
        ///         // Set the data from the picture.
        ///         frame.MimeType = picture.MimeType;
        ///         frame.Data     = picture.data;
        ///
        ///         // Save the file.
        ///         file.Save ();
        ///     }
        /// }
        ///    </code>
        /// </example>
        public static AttachmentFrame Get(Tag tag,
                                          string description,
                                          PictureType type,
                                          bool create)
        {
            AttachmentFrame att;

            foreach (Frame frame in tag.GetFrames <AttachmentFrame>())
            {
                att = frame as AttachmentFrame;

                if (att == null)
                {
                    continue;
                }

                if (description != null && att.Description != description)
                {
                    continue;
                }

                if (type != PictureType.Other && att.Type != type)
                {
                    continue;
                }

                return(att);
            }

            if (!create)
            {
                return(null);
            }

            att             = new AttachmentFrame();
            att.Description = description;
            att.Type        = type;

            tag.AddFrame(att);

            return(att);
        }
示例#16
0
        public static PopularimeterFrame Get(Tag tag, string user, bool create)
        {
            PopularimeterFrame popm;

            foreach (Frame frame in tag)
            {
                popm = frame as PopularimeterFrame;
                if (popm != null && popm.user.Equals(user))
                {
                    return(popm);
                }
            }
            if (!create)
            {
                return(null);
            }
            popm = new PopularimeterFrame(user);
            tag.AddFrame(popm);
            return(popm);
        }
示例#17
0
        public static PlayCountFrame Get(Tag tag, bool create)
        {
            PlayCountFrame pcnt;

            foreach (Frame frame in tag)
            {
                pcnt = frame as PlayCountFrame;
                if (pcnt != null)
                {
                    return(pcnt);
                }
            }
            if (!create)
            {
                return(null);
            }
            pcnt = new PlayCountFrame();
            tag.AddFrame(pcnt);
            return(pcnt);
        }
示例#18
0
        public static MusicCdIdentifierFrame Get(Tag tag, bool create)
        {
            MusicCdIdentifierFrame mcdi;

            foreach (Frame frame in tag)
            {
                mcdi = frame as MusicCdIdentifierFrame;
                if (mcdi != null)
                {
                    return(mcdi);
                }
            }
            if (!create)
            {
                return(null);
            }
            mcdi = new MusicCdIdentifierFrame();
            tag.AddFrame(mcdi);
            return(mcdi);
        }
        public static UniqueFileIdentifierFrame Get(Tag tag, string owner, bool create)
        {
            foreach (Frame f in tag.GetFrames(FrameType.UFID))
            {
                if (f is UniqueFileIdentifierFrame && (f as UniqueFileIdentifierFrame).Owner == owner)
                {
                    return(f as UniqueFileIdentifierFrame);
                }
            }

            if (!create)
            {
                return(null);
            }

            UniqueFileIdentifierFrame frame = new UniqueFileIdentifierFrame(owner, null);

            tag.AddFrame(frame);
            return(frame);
        }
示例#20
0
        /// <summary>
        ///    Gets a specified private frame from the specified tag,
        ///    optionally creating it if it does not exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="owner">
        ///    A <see cref="string" /> specifying the owner to match.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="PrivateFrame" /> object containing the
        ///    matching frame, or <see langword="null" /> if a match
        ///    wasn't found and <paramref name="create" /> is <see
        ///    langword="false" />.
        /// </returns>
        public static PrivateFrame Get(Tag tag, string owner, bool create)
        {
            PrivateFrame priv;

            foreach (Frame frame in tag.GetFrames(FrameType.PRIV))
            {
                priv = frame as PrivateFrame;
                if (priv != null && priv.Owner == owner)
                {
                    return(priv);
                }
            }

            if (!create)
            {
                return(null);
            }

            priv = new PrivateFrame(owner);
            tag.AddFrame(priv);
            return(priv);
        }
        public static UnsynchronisedLyricsFrame Get(Tag tag, string description, string language, bool create)
        {
            foreach (Frame f in tag.GetFrames(FrameType.USLT))
            {
                UnsynchronisedLyricsFrame cf = f as UnsynchronisedLyricsFrame;

                if (cf != null && cf.Description == description && (language == null || language == cf.Language))
                {
                    return(cf);
                }
            }

            if (!create)
            {
                return(null);
            }

            UnsynchronisedLyricsFrame frame = new UnsynchronisedLyricsFrame(description, language);

            tag.AddFrame(frame);
            return(frame);
        }
示例#22
0
        public void TestMp3Uid()
        {
            var mp3File = string.Format("{0}\\{1:N}.mp3", Path.GetTempPath(), Guid.NewGuid());

            System.IO.File.Copy(@"TestData\test.mp3", mp3File, true);

            var file = File.Create(CreateAbstraction(mp3File));

            Tag id3V2Tag = (Tag)file.GetTag(TagTypes.Id3v2, true);
            var userTextInformationFrames  = id3V2Tag.GetFrames <UserTextInformationFrame>();
            UserTextInformationFrame frame = userTextInformationFrames.First(a => a.Description == "UID");

            frame.Text.First().Should().Be("SomewhereOverTheRainbow");
            frame.Text = new[] { "Hei" };
            var userTextInformationFrame = new UserTextInformationFrame("WhateverUID")
            {
                Text = new[] { Guid.NewGuid().ToString("N") }
            };

            id3V2Tag.AddFrame(userTextInformationFrame);
            file.Save();
            System.IO.File.Delete(mp3File);
        }
示例#23
0
        /// <summary>
        ///    Gets a play count frame from a specified tag, optionally
        ///    creating it if it does not exist.
        /// </summary>
        /// <param name="tag">
        ///    A <see cref="Tag" /> object to search in.
        /// </param>
        /// <param name="create">
        ///    A <see cref="bool" /> specifying whether or not to create
        ///    and add a new frame to the tag if a match is not found.
        /// </param>
        /// <returns>
        ///    A <see cref="EventTimeCodesFrame" /> object containing the
        ///    matching frame, or <see langword="null" /> if a match
        ///    wasn't found and <paramref name="create" /> is <see
        ///    langword="false" />.
        /// </returns>
        public static EventTimeCodesFrame Get(Tag tag, bool create)
        {
            EventTimeCodesFrame etco;

            foreach (Frame frame in tag)
            {
                etco = frame as EventTimeCodesFrame;

                if (etco != null)
                {
                    return(etco);
                }
            }

            if (!create)
            {
                return(null);
            }

            etco = new EventTimeCodesFrame();
            tag.AddFrame(etco);
            return(etco);
        }
示例#24
0
文件: MyTag.cs 项目: mkitby/MyTag
        // TODO: expection handling
        private void bt_Save_Click(object sender, EventArgs e)
        {
            TreeNodeCollection              nodes = tagTreeView.Nodes;
            UserTextInformationFrame        tagframe;
            List <UserTextInformationFrame> save_frame_list = new List <UserTextInformationFrame>();
            List <string> tag_desc_list = new List <string>();

            try
            {
                // RemoveFrame() is invalid, workaround: save other "TXXX" first, then remove all "TXXX", add selected tags and recovery other "TXXX" at last
                foreach (TreeNode n in nodes)
                {
                    tag_desc_list.Add(n.Text);
                }

                foreach (UserTextInformationFrame fm in audioTag.GetFrames("TXXX"))
                {
                    if (!tag_desc_list.Contains(fm.Description))
                    {
                        save_frame_list.Add(fm);
                    }
                }

                // Remove all "TXXX"
                audioTag.RemoveFrames("TXXX");

                // Pre-handle comment tag for link to comment
                if (cb_LinkToComment.Checked == true)
                {
                    if (cb_AppendMode.Checked == true)
                    {
                        if (string.IsNullOrWhiteSpace(audioTag.Comment))
                        {
                            audioTag.Comment = ""; // clear comment
                        }
                        else
                        {
                            audioTag.Comment += "||"; // add a link character "||"
                        }
                    }
                    else
                    {
                        audioTag.Comment = ""; // clear comment
                    }
                }

                // Add selected tags
                foreach (TreeNode n in nodes)
                {
                    string valmixed = "";

                    foreach (TreeNode tn in n.Nodes)
                    {
                        if (tn.Checked == true)
                        {
                            valmixed += tn.Text + ";";

                            // Handle link to comment tag
                            if (cb_LinkToComment.Checked == true)
                            {
                                audioTag.Comment += tn.Text + ";";
                            }
                        }
                    }

                    tagframe      = new UserTextInformationFrame(n.Text);
                    tagframe.Text = new string[] { valmixed };
                    audioTag.AddFrame(tagframe);
                }

                // Recovery other "TXXX"
                if (save_frame_list.Count != 0)
                {
                    foreach (UserTextInformationFrame fm in save_frame_list)
                    {
                        audioTag.AddFrame(fm);
                    }
                }

                // Save file
                audioFile.Save();

                showInStatusBar("Save OK");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ExceptionInfo.ShowExceptionInfo(ex), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }