Sylloge
A C# helper library
code/Audio/Metadata/ID3/Frames/TextFrame.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 
00006 namespace Sylloge.Audio.Metadata.ID3.Frames
00007 {
00008     public class TextFrame : ID3.Frame
00009     {
00013         public byte Encoding { get; protected set; }
00017         public string Description { get; protected set; }
00021         public string Value { get; protected set; }
00022 
00023         public TextFrame() : base(ID3.Frame.Keys.UserDefinedString) { }
00024 
00025         public TextFrame(string id) : base(id) { }
00026 
00027         public TextFrame(Frame other)
00028             : base(other)
00029         {
00030             this.Encoding = 0x00;
00031             this.Description = string.Empty;
00032             this.Value = ID3.Frames.TextFrame.GetTextFromFrame(other.Data);
00033         }
00034 
00035         public TextFrame(TextFrame other)
00036             : base(other)
00037         {
00038             this.Initialize(other.Encoding, other.Description, other.Value);
00039         }
00040 
00045         public override byte[] GenerateTag()
00046         {
00047             return this.GenerateTagData();
00048         }
00049 
00050         public void SetDescription(string desc)
00051         {
00052             this.Description = desc;
00053             this.GenerateTag();
00054         }
00055 
00056         public void SetEncoding(System.Text.Encoding e)
00057         {
00058             if (e == null) { throw new System.ArgumentNullException("e"); }
00059             this.Encoding = ((e.CodePage == System.Text.Encoding.Unicode.CodePage) ? (byte)0x01 : (byte)0x00);
00060             this.GenerateTag();
00061         }
00062 
00063         public void SetValue(string value)
00064         {
00065             this.Value = value;
00066             this.GenerateTag();
00067         }
00068 
00073         protected byte[] GenerateTagData()
00074         {
00075             System.Text.Encoding enc = ID3.Helper.GetEncoding(this.Encoding);
00076             int dlen = enc.GetByteCount(this.Description) + 1; // upto 0x00 + 1 byte (hence + 2)
00077             int vlen = enc.GetByteCount(this.Value);
00078             this.Data = new byte[(1 + dlen + vlen)];
00079             this.Data[0] = this.Encoding;
00080             System.Buffer.BlockCopy(enc.GetBytes(this.Description), 0, this.Data, 1, (dlen - 1));
00081             System.Buffer.BlockCopy(enc.GetBytes(this.Value), 0, this.Data, dlen, vlen);
00082             return this.FullFrame;
00083         }
00084 
00089         protected byte[] GenerateTagData(byte[] pre_data)
00090         {
00091             System.Text.Encoding enc = ID3.Helper.GetEncoding(this.Encoding);
00092             int dlen = enc.GetByteCount(this.Description) + 1;
00093             int vlen = enc.GetByteCount(this.Value);
00094             int plen = pre_data.Length;
00095             this.Data = new byte[(1 + plen + dlen + vlen)];
00096             this.Data[0] = this.Encoding;
00097             System.Buffer.BlockCopy(pre_data, 0, this.Data, 1, plen);
00098             System.Buffer.BlockCopy(enc.GetBytes(this.Description), 0, this.Data, (plen + 1), (dlen - 1));
00099             System.Buffer.BlockCopy(enc.GetBytes(this.Value), 0, this.Data, (plen + 1 + dlen), vlen);
00100             return this.FullFrame;
00101         }
00102 
00103         protected void Initialize(byte enc, string desc, string val)
00104         {
00105             this.Encoding = enc;
00106             this.Description = desc;
00107             this.Value = val;
00108         }
00109 
00116         public static byte[] GenerateTagData(string id, string data)
00117         {
00118             return ID3.Frames.TextFrame.GenerateTagData(id, data, false);
00119         }
00120 
00128         public static byte[] GenerateTagData(string id, string data, bool isAnsi)
00129         {
00130             System.Text.Encoding e = (isAnsi ? ID3.Helper.ISO : ID3.Helper.UTF16);
00131             byte[] b = new byte[(e.GetByteCount(data) + 1)];
00132             b[0] = (isAnsi ? (byte)0x00 : (byte)0x01);
00133             System.Buffer.BlockCopy(e.GetBytes(data), 0, b, 1, (b.Length - 1));
00134             return ID3.Frame.GetFullTagData(id, b);
00135         }
00136 
00142         public static string GetTextFromFrame(byte[] data)
00143         {
00144             /* <Header for 'Text information frame', ID: "T000" - "TZZZ", excluding "TXXX" described in 4.2.2.> 
00145             Text encoding    $xx
00146             Information    <text string according to encoding>*/
00147             return ID3.Helper.GetEncoding(data[0]).GetString(data, 1, data.Length - 1);
00148         }
00149     }
00150 }
 All Classes Namespaces Files Functions Variables Enumerations Properties Events