![]() |
Sylloge
A C# helper library
|
00001 00002 00003 00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 using System; 00018 using System.Collections.Generic; 00019 using System.Text; 00020 00021 namespace Sylloge.Audio 00022 { 00029 public class MP3 00030 { 00031 #region Comments and Dev Notes 00032 00033 // DEV_NOTE: This class provides BASIC tag information for ID3v1 and ID3v2. Extended header information, 00034 // compressed frames, extended frame header information, things of this nature are ignored or 00035 // considered invalid data. If you wish to extend that ability, feel free to do so in any one of the 00036 // methods that gathers the tag information (see http://www.id3.org for more info on tags). 00037 00038 // DEV_NOTE: If a tag has the 'Text Encoding' flag specified, 0x00 = ANSI, 0x01 = Unicode. 00039 00040 #endregion 00041 00042 #region Local Attributes 00043 00044 public Metadata.ID3.V1 ID3v1 { get; set; } 00045 public Metadata.ID3.V2 ID3v2 { get; set; } 00046 public Metadata.APE.V2 APEv2 { get; set; } 00047 public string File { get; set; } 00048 00049 #endregion 00050 00051 #region Methods 00052 00056 public MP3() 00057 { 00058 this.Initialize(string.Empty); 00059 } 00060 00066 public MP3(string fileName) 00067 { 00068 this.Initialize(fileName); 00069 } 00070 00074 public void Clear() 00075 { 00076 if (this.ID3v1 != null) { this.ID3v1.Clear(); } 00077 if (this.ID3v2 != null) { this.ID3v2.Clear(); } 00078 if (this.APEv2 != null) { this.APEv2.Clear(); } 00079 } 00080 00084 public long FileSize { get; private set; } 00085 00089 public void GetTagInformation() 00090 { 00091 this.Initialize(this.File); 00092 } 00093 00097 public bool HasId3v1Tag 00098 { 00099 get { return (this.ID3v1 != null && this.ID3v1.IsValid); } 00100 } 00101 00105 public bool HasId3v2Tag 00106 { 00107 get { return (this.ID3v2 != null && this.ID3v2.IsValid); } 00108 } 00109 00113 public bool HasApeTag 00114 { 00115 get { return (this.APEv2 != null && this.APEv2.IsValid); } 00116 } 00117 00122 public void Initialize(string fileName) 00123 { 00124 this.File = fileName; 00125 if (System.IO.File.Exists(this.File)) { 00126 this.FileSize = ((new System.IO.FileInfo(this.File)).Length); 00127 } 00128 this.ID3v1 = Metadata.ID3.V1.Parse(this.File); 00129 this.ID3v2 = Metadata.ID3.V2.Parse(this.File); 00130 this.APEv2 = Metadata.APE.V2.Parse(this.File); 00131 } 00132 00136 public void SaveTagInfo() 00137 { 00138 if (this.HasId3v1Tag && !this.ID3v1.Save()) { 00139 // TODO: Change Debug.RaiseEvent("Failed to save the ID3v1 tag"); 00140 } 00141 if (this.HasId3v2Tag && !this.ID3v2.Save()) { 00142 // TODO: Change Debug.RaiseEvent("Failed to save the ID3v2 tag"); 00143 } 00144 } 00145 00150 public long SongDataSize 00151 { 00152 get 00153 { 00154 long fs = this.FileSize; 00155 if (this.HasId3v1Tag) { 00156 fs -= 128; 00157 if (this.ID3v1.Extended.IsValid) { fs -= 227; } 00158 } 00159 if (this.HasId3v2Tag) { fs -= this.ID3v2.CalculateHeaderSize(); } 00160 if (this.HasApeTag) { fs -= this.APEv2.Size - 32; } 00161 return fs; 00162 } 00163 } 00164 00169 public override string ToString() 00170 { 00171 if (this.HasId3v1Tag || this.HasId3v2Tag) { 00172 if (this.HasId3v1Tag) { 00173 return this.ID3v1.ToString(); 00174 } else { 00175 if (this.HasId3v2Tag) { 00176 return this.ID3v2.ToString(); 00177 } 00178 } 00179 } 00180 return " - "; 00181 } 00182 00183 #endregion 00184 00185 #region Static Methods 00186 00193 public static long GetHeaderPosition(string file, long start) 00194 { 00195 if (!System.IO.File.Exists(file)) { return 0; } 00196 long ret = 0; 00197 try { 00198 System.IO.FileStream fin = new System.IO.FileStream(file, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite); 00199 fin.Seek(start, System.IO.SeekOrigin.Begin); 00200 byte[] data = new byte[(fin.Length - start)]; 00201 fin.Read(data, 0, data.Length); 00202 fin.Close(); 00203 for (int i = 0; i < data.Length - 1; i++) { 00204 if (data[i] == 0xFF && data[i + 1] == 0xFB) { 00205 ret = i + start; 00206 break; 00207 } 00208 } 00209 } catch (Exception ex) { 00210 // TODO: Change Debug.RaiseEvent("Error getting the MP3 header position: " + ex.Message); 00211 Console.WriteLine(ex.Message); 00212 } 00213 return ret; 00214 } 00215 00216 #endregion 00217 } 00218 }
1.7.4