Esempio n. 1
0
        private static byte[] EncodePicture(Id3Frame id3Frame)
        {
            var frame = (PictureFrame)id3Frame;

            var bytes = new List <byte> {
                (byte)frame.EncodingType
            };

            Encoding defaultEncoding = TextEncodingHelper.GetDefaultEncoding();

            bytes.AddRange(!string.IsNullOrEmpty(frame.MimeType)
                ? defaultEncoding.GetBytes(frame.MimeType)
                : defaultEncoding.GetBytes("image/"));

            bytes.Add(0);
            bytes.Add((byte)frame.PictureType);

            Encoding descriptionEncoding = TextEncodingHelper.GetEncoding(frame.EncodingType);

            bytes.AddRange(descriptionEncoding.GetPreamble());
            if (!string.IsNullOrEmpty(frame.Description))
            {
                bytes.AddRange(descriptionEncoding.GetBytes(frame.Description));
            }
            bytes.AddRange(TextEncodingHelper.GetSplitterBytes(frame.EncodingType));

            if (frame.PictureData != null && frame.PictureData.Length > 0)
            {
                bytes.AddRange(frame.PictureData);
            }

            return(bytes.ToArray());
        }
Esempio n. 2
0
        private static byte[] EncodeComment(Id3Frame id3Frame)
        {
            var frame = (CommentFrame)id3Frame;

            var bytes = new List <byte> {
                (byte)frame.EncodingType
            };

            bytes.AddRange(TextEncodingHelper.GetDefaultEncoding().GetBytes(frame.Language.ToString()));

            Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType);

            bytes.AddRange(encoding.GetPreamble());
            if (!string.IsNullOrEmpty(frame.Description))
            {
                bytes.AddRange(encoding.GetBytes(frame.Description));
            }
            bytes.AddRange(TextEncodingHelper.GetSplitterBytes(frame.EncodingType));
            bytes.AddRange(encoding.GetPreamble());
            if (!string.IsNullOrEmpty(frame.Comment))
            {
                bytes.AddRange(encoding.GetBytes(frame.Comment));
            }

            return(bytes.ToArray());
        }
Esempio n. 3
0
        private static byte[] EncodeUrlLink <TFrame>(Id3Frame id3Frame)
            where TFrame : UrlLinkFrame
        {
            var frame = (TFrame)id3Frame;

            return(frame.Url != null?TextEncodingHelper.GetDefaultEncoding().GetBytes(frame.Url) : new byte[0]);
        }
Esempio n. 4
0
        private static byte[] EncodePrivate(Id3Frame id3Frame)
        {
            var frame = (PrivateFrame)id3Frame;

            var bytes = new List <byte>();

            bytes.AddRange(TextEncodingHelper.GetEncoding(Id3TextEncoding.Iso8859_1).GetBytes(frame.OwnerId));
            bytes.AddRange(TextEncodingHelper.GetSplitterBytes(Id3TextEncoding.Iso8859_1));
            bytes.AddRange(frame.Data ?? new byte[0]);
            return(bytes.ToArray());
        }
Esempio n. 5
0
        private string FireResolveMissingDataEvent(Id3Tag tag, Id3Frame frame, string sourceName)
        {
            EventHandler <ResolveMissingDataEventArgs> resolveMissingData = ResolveMissingData;

            if (resolveMissingData != null)
            {
                var args = new ResolveMissingDataEventArgs(tag, frame, sourceName);
                resolveMissingData(this, args);
                return(args.Value);
            }
            return(null);
        }
        /// <summary>
        ///     Retrieves the frame ID from the specified frame instance.
        /// </summary>
        /// <param name="frame">The frame instance.</param>
        /// <returns>The frame ID, or null if there is no frame handler for the specified frame instance.</returns>
        internal string GetFrameIdFromFrame(Id3Frame frame)
        {
            if (frame is UnknownFrame unknownFrame)
            {
                return(unknownFrame.Id);
            }

            Type frameType = frame.GetType();

            FrameHandler handler = FrameHandlers[frameType];

            return(handler?.FrameId);
        }
Esempio n. 7
0
        protected void AssertValidResult(Id3Tag[] result)
        {
            Assert.NotEmpty(result);

            Id3Tag checkTag = result[0];

            Assert.NotEmpty(checkTag);
            foreach (Type frameType in Provider.Properties.AvailableOutputs)
            {
                Id3Frame frame = checkTag.FirstOrDefault(f => f.GetType() == frameType && f.IsAssigned);
                Assert.True(frame != null);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Gets (parses) the discog identifier from an <see cref="Id3.Id3Tag"/>
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <returns></returns>
        private Discog GetDiscogId(Id3.Id3Tag tag)
        {
            string _discogsId = "0";

            Id3Frame discogsFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGS_RELEASE_ID\0")).FirstOrDefault();
            Discog   discog       = new Discog();

            if (discogsFrame != null)
            {
                _discogsId = discogsFrame.ToString().ToUpper().Replace("DISCOGS_RELEASE_ID\0", "");
            }
            else
            {
                //Try get release Id other search
                discogsFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGSID\0")).FirstOrDefault();
                if (discogsFrame != null)
                {
                    _discogsId = discogsFrame.ToString().ToUpper().Replace("DISCOGSID\0", "");
                }
                else
                {
                    discogsFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGS-ID\0")).FirstOrDefault();

                    if (discogsFrame == null)
                    {
                        return(discog);
                    }

                    _discogsId = discogsFrame.ToString().ToUpper().Replace("DISCOGS-ID\0", "");
                }
            }

            if (!string.IsNullOrEmpty(_discogsId))
            {
                int i;
                int.TryParse(_discogsId, out i);

                if (i != 0)
                {
                    discog.ReleaseId = i;
                }
                else
                {
                    discog.ReleaseId = 0;
                }
            }

            return(discog);
        }
Esempio n. 9
0
        private static byte[] EncodeText <TFrame>(Id3Frame id3Frame)
            where TFrame : TextFrameBase
        {
            var      frame    = (TFrame)id3Frame;
            Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType);

            byte[] preamble  = encoding.GetPreamble();
            byte[] textBytes = encoding.GetBytes(frame.TextValue);
            var    data      = new byte[1 + preamble.Length + textBytes.Length];

            data[0] = (byte)frame.EncodingType;
            preamble.CopyTo(data, 1);
            textBytes.CopyTo(data, preamble.Length + 1);
            return(data);
        }
Esempio n. 10
0
        internal string GetFrameIdFromFrame(Id3Frame frame)
        {
            var unknownFrame = frame as UnknownFrame;

            if (unknownFrame != null)
            {
                return(unknownFrame.Id);
            }

            Type frameType = frame.GetType();

            FrameHandler handler = FrameHandlers[frameType];

            return(handler != null ? handler.FrameId : null);
        }
Esempio n. 11
0
 public override bool Equals(Id3Frame other)
 {
     if (base.Equals(other))
     {
         return(true);
     }
     if (!(other is UnknownFrame unknownFrame))
     {
         return(false);
     }
     if (Id != unknownFrame.Id)
     {
         return(false);
     }
     return(ByteArrayHelper.AreEqual(Data, unknownFrame.Data));
 }
Esempio n. 12
0
        void ISerializationSurrogate.GetObjectData(object obj, SerializationInfo info, StreamingContext context)
        {
            var tag = (Id3Tag)obj;

            info.AddValue("MajorVersion", tag.MajorVersion);
            info.AddValue("MinorVersion", tag.MinorVersion);

            int frameCount = tag.Frames.Count;

            info.AddValue("FrameCount", frameCount);
            for (int i = 0; i < frameCount; i++)
            {
                Id3Frame frame = tag.Frames[i];
                info.AddValue("Frame" + i + "Type", frame.GetType().AssemblyQualifiedName);
                info.AddValue("Frame" + i, frame);
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Gets the country name from the file tag
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <returns></returns>
        private string GetCountry(Id3.Id3Tag tag)
        {
            Id3Frame countryFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("DISCOGS_COUNTRY\0")).FirstOrDefault();

            if (countryFrame != null)
            {
                return(countryFrame.ToString().ToUpper().Replace("DISCOGS_COUNTRY\0", ""));
            }
            else
            {
                countryFrame = tag.Frames.Where(x => x.ToString().ToUpper().Contains("COUNTRY\0")).FirstOrDefault();
                if (countryFrame != null)
                {
                    return(countryFrame.ToString().ToUpper().Replace("COUNTRY\0", ""));
                }
            }

            return(null);
        }
Esempio n. 14
0
        private static byte[] EncodeCustomUrlLink(Id3Frame id3Frame)
        {
            var frame = (CustomUrlLinkFrame)id3Frame;

            var bytes = new List <byte> {
                (byte)frame.EncodingType
            };

            Encoding encoding = TextEncodingHelper.GetEncoding(frame.EncodingType);

            bytes.AddRange(encoding.GetPreamble());
            if (!string.IsNullOrEmpty(frame.Description))
            {
                bytes.AddRange(encoding.GetBytes(frame.Description));
            }
            bytes.AddRange(TextEncodingHelper.GetSplitterBytes(frame.EncodingType));
            if (frame.Url != null)
            {
                bytes.AddRange(TextEncodingHelper.GetDefaultEncoding().GetBytes(frame.Url));
            }

            return(bytes.ToArray());
        }
Esempio n. 15
0
 private static byte[] EncodeBeatsPerMinute(Id3Frame frame)
 {
     return(EncodeText <BeatsPerMinuteFrame>(frame));
 }
 internal ResolveMissingDataEventArgs(Id3Tag tag, Id3Frame frame, string sourceName)
 {
     Tag        = tag;
     Frame      = frame;
     SourceName = sourceName;
 }
Esempio n. 17
0
 private static byte[] EncodeArtistUrl(Id3Frame frame)
 {
     return(EncodeUrlLink <ArtistUrlFrame>(frame));
 }
Esempio n. 18
0
 private static byte[] EncodeAudioSourceUrl(Id3Frame frame)
 {
     return(EncodeUrlLink <AudioSourceUrlFrame>(frame));
 }
Esempio n. 19
0
 private static byte[] EncodeCommercialUrl(Id3Frame frame)
 {
     return(EncodeUrlLink <CommercialUrlFrame>(frame));
 }
Esempio n. 20
0
 private static byte[] EncodeLyrics(Id3Frame id3Frame)
 {
     throw new NotImplementedException();
 }
Esempio n. 21
0
 private static byte[] EncodeCopyrightUrl(Id3Frame frame)
 {
     return(EncodeUrlLink <CopyrightUrlFrame>(frame));
 }
Esempio n. 22
0
 private static byte[] EncodeContentGroupDescription(Id3Frame frame)
 {
     return(EncodeText <ContentGroupDescriptionFrame>(frame));
 }
Esempio n. 23
0
 private static byte[] EncodePaymentUrl(Id3Frame frame)
 {
     return(EncodeUrlLink <PaymentUrlFrame>(frame));
 }
Esempio n. 24
0
 private static byte[] EncodeAlbum(Id3Frame frame)
 {
     return(EncodeText <AlbumFrame>(frame));
 }
Esempio n. 25
0
 private static byte[] EncodeBand(Id3Frame frame)
 {
     return(EncodeText <BandFrame>(frame));
 }
Esempio n. 26
0
 private static byte[] EncodeArtists(Id3Frame frame)
 {
     return(EncodeText <ArtistsFrame>(frame));
 }
Esempio n. 27
0
        internal override Id3Tag ReadTag(Stream stream, out object additionalData)
        {
            if (!HasTag(stream))
            {
                additionalData = null;
                return(null);
            }

            Id3Tag tag = CreateTag();

            stream.Seek(4, SeekOrigin.Begin);
            var headerBytes = new byte[6];

            stream.Read(headerBytes, 0, 6);

            var headerContainer = new Id3V2Header();

            additionalData = headerContainer;

            byte flags  = headerBytes[1];
            var  header = new Id3V2StandardHeader {
                Revision         = headerBytes[0],
                Unsyncronization = (flags & 0x80) > 0,
                ExtendedHeader   = (flags & 0x40) > 0,
                Experimental     = (flags & 0x20) > 0
            };

            headerContainer.Header = header;

            int tagSize = SyncSafeNumber.DecodeSafe(headerBytes, 2, 4);
            var tagData = new byte[tagSize];

            stream.Read(tagData, 0, tagSize);

            var currentPos = 0;

            if (header.ExtendedHeader)
            {
                SyncSafeNumber.DecodeSafe(tagData, currentPos, 4);
                currentPos += 4;

                var extendedHeader = new Id3V2ExtendedHeader {
                    PaddingSize = SyncSafeNumber.DecodeNormal(tagData, currentPos + 2, 4)
                };

                if ((tagData[currentPos] & 0x80) > 0)
                {
                    extendedHeader.Crc32 = SyncSafeNumber.DecodeNormal(tagData, currentPos + 6, 4);
                    currentPos          += 10;
                }
                else
                {
                    currentPos += 6;
                }

                headerContainer.ExtendedHeader = extendedHeader;
            }

            while (currentPos < tagSize && tagData[currentPos] != 0x00)
            {
                string frameId = Encoding.ASCII.GetString(tagData, currentPos, 4);
                currentPos += 4;

                int frameSize = SyncSafeNumber.DecodeNormal(tagData, currentPos, 4);
                currentPos += 4;

                var frameFlags = (ushort)((tagData[currentPos] << 0x08) + tagData[currentPos + 1]);
                currentPos += 2;

                var frameData = new byte[frameSize];
                Array.Copy(tagData, currentPos, frameData, 0, frameSize);

                FrameHandler mapping = FrameHandlers[frameId];
                if (mapping != null)
                {
                    Id3Frame frame = mapping.Decoder(frameData);
                    tag.AddUntypedFrame(frame);
                }

                currentPos += frameSize;
            }

            return(tag);
        }
Esempio n. 28
0
 private static byte[] EncodeComposers(Id3Frame frame)
 {
     return(EncodeText <ComposersFrame>(frame));
 }
Esempio n. 29
0
        internal override Id3Tag ReadTag(Stream stream)
        {
            if (!HasTag(stream))
            {
                return(null);
            }

            var tag = new Id3Tag {
                MajorVersion = 2,
                MinorVersion = 3,
                Family       = Id3TagFamily.Version2x,
                IsSupported  = true,
            };

            stream.Seek(4, SeekOrigin.Begin);
            var headerBytes = new byte[6];

            stream.Read(headerBytes, 0, 6);

            var headerContainer = new Id3v2Header();

            tag.AdditionalData = headerContainer;

            byte flags  = headerBytes[1];
            var  header = new Id3v2StandardHeader {
                Revision         = headerBytes[0],
                Unsyncronization = (flags & 0x80) > 0,
                ExtendedHeader   = (flags & 0x40) > 0,
                Experimental     = (flags & 0x20) > 0
            };

            headerContainer.Header = header;

            int tagSize = SyncSafeNumber.DecodeSafe(headerBytes, 2, 4);
            var tagData = new byte[tagSize];

            stream.Read(tagData, 0, tagSize);

            int currentPos = 0;

            if (header.ExtendedHeader)
            {
                SyncSafeNumber.DecodeSafe(tagData, currentPos, 4);
                currentPos += 4;

                var extendedHeader = new Id3v2ExtendedHeader {
                    PaddingSize = SyncSafeNumber.DecodeNormal(tagData, currentPos + 2, 4)
                };

                if ((tagData[currentPos] & 0x80) > 0)
                {
                    extendedHeader.Crc32 = SyncSafeNumber.DecodeNormal(tagData, currentPos + 6, 4);
                    currentPos          += 10;
                }
                else
                {
                    currentPos += 6;
                }

                headerContainer.ExtendedHeader = extendedHeader;
            }

            while (currentPos < tagSize && tagData[currentPos] != 0x00)
            {
                string frameId = AsciiEncoding.GetString(tagData, currentPos, 4);
                currentPos += 4;

                int frameSize = SyncSafeNumber.DecodeNormal(tagData, currentPos, 4);
                currentPos += 4;

                var frameFlags = (ushort)((tagData[currentPos] << 0x08) + tagData[currentPos + 1]);
                currentPos += 2;

                var frameData = new byte[frameSize];
                Array.Copy(tagData, currentPos, frameData, 0, frameSize);

                FrameHandler mapping = FrameHandlers[frameId];
                if (mapping != null && mapping.Decoder != null)
                {
                    Id3Frame frame = mapping.Decoder(frameData);
                    tag.Frames.Add(frame);
                }

                currentPos += frameSize;
            }

            return(tag);
        }
Esempio n. 30
0
 private static byte[] EncodeConductor(Id3Frame frame)
 {
     return(EncodeText <ConductorFrame>(frame));
 }