Esempio n. 1
0
        private static string GetTagText(FrameBase tag)
        {
            var frameText = tag as FrameText;
            if (frameText != null)
                return frameText.Text;

            var frameFullText = tag as FrameFullText;
            if (frameFullText != null)
                return frameFullText.Text;

            return null;
        }
Esempio n. 2
0
 private void SetFlags(FrameBase frame, ushort flags)
 {
     frame.TagAlter = GetTagAlter(flags);
     frame.FileAlter = GetFileAlter(flags);
     frame.ReadOnly = GetReadOnly(flags);
     frame.Compression = GetCompression(flags);
     frame.Encryption = GetEncryption(flags);
     frame.Unsynchronisation = GetUnsynchronisation(flags);
     frame.DataLength = GetDataLength(flags);
 }
Esempio n. 3
0
 private ushort GetFlags(FrameBase frame)
 {
     ushort flags = 0;
     SetTagAlter(frame.TagAlter, ref flags);
     SetFileAlter(frame.FileAlter, ref flags);
     SetReadOnly(frame.ReadOnly, ref flags);
     SetGrouping(frame.Group.HasValue, ref flags);
     SetCompression(frame.Compression, ref flags);
     SetEncryption(frame.Encryption, ref flags);
     SetUnsynchronisation(frame.Unsynchronisation, ref flags);
     SetDataLength(frame.DataLength, ref flags);
     return flags;
 }
Esempio n. 4
0
        /// <summary>
        /// Build a binary data frame form the frame object.
        /// </summary>
        /// <param name="frame">ID3 Frame</param>
        /// <returns>binary frame representation</returns>
        public byte[] Make(FrameBase frame, out ushort flags)
        {
            flags = GetFlags(frame);
            var buffer = frame.Make();

            var memoryStream = new MemoryStream();
            var writer = new BinaryWriter(memoryStream);

            if (frame.Group.HasValue == true)
                writer.Write((byte)frame.Group);

            if (frame.Compression == true)
            {
                switch (Version)
                {
                    case 3:
                        {
                            writer.Write(Swap.Int32(buffer.Length));
                            break;
                        }
                    case 4:
                        {
                            writer.Write(Sync.UnsafeBigEndian(Swap.UInt32((uint)buffer.Length)));
                            break;
                        }
                    default:
                        {
                            throw new NotImplementedException("ID3v2 Version " + Version + " is not supported.");
                        }
                }
                var buf = new byte[2048];
                var deflater = new Deflater(Deflater.BEST_COMPRESSION);
                deflater.SetInput(buffer, 0, buffer.Length);
                deflater.Finish();
                while (!deflater.IsNeedingInput)
                {
                    int len = deflater.Deflate(buf, 0, buf.Length);
                    if (len <= 0)
                    {
                        break;
                    }
                    memoryStream.Write(buf, 0, len);
                }

                if (!deflater.IsNeedingInput)
                {
                    //TODO: Skip and remove invalid frames.
                    throw new InvalidFrameException("Can't decompress frame '" + frame.FrameId + "' missing data");
                }
            }
            else
            {
                memoryStream.Write(buffer, 0, buffer.Length);
            }

            if (frame.Encryption == true)
            {
                //TODO: Encryption
                throw new NotImplementedException("Encryption is not implemented, consequently it is not supported.");
            }

            if (frame.Unsynchronisation == true)
            {
                MemoryStream synchStream = new MemoryStream();
                Sync.Unsafe(memoryStream, synchStream, (uint)memoryStream.Position);
                memoryStream = synchStream;
            }
            return memoryStream.ToArray();
        }