Author: bnickel Date: 2007-05-15 01:25:31 -0400 (Tue, 15 May 2007) New Revision: 77388
Added: trunk/taglib-sharp/src/TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs Modified: trunk/taglib-sharp/ChangeLog trunk/taglib-sharp/src/TagLib/File.cs trunk/taglib-sharp/src/TagLib/Id3v2/FrameFactory.cs trunk/taglib-sharp/src/TagLib/Id3v2/Frames/CommentsFrame.cs trunk/taglib-sharp/src/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs trunk/taglib-sharp/src/TagLib/TagLib.sources trunk/taglib-sharp/src/taglib-sharp.mdp trunk/taglib-sharp/taglib-sharp.csproj Log: 2007-05-14 Brian Nickel <[EMAIL PROTECTED]> Feature Request: http://forum.taglib-sharp.com/viewtopic.php?f=4&t=12 * src/TagLib/File.cs: Support pushing and poping file abstractors. * src/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs: * src/TagLib/Id3v2/Frames/CommentsFrame.cs: * src/TagLib/Id3v2/Tag.cs: Be a little bit smarter when storing lyrics and comments. GetPreferred shouldn't be used for storing, because we want to store in exactly what we want, not a best guess. * src/TagLib/Id3v2/FrameFactory.cs: * src/TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs: Support synchronized lyrics. * src/TagLib/TagLib.sources: * src/taglib-sharp.mdp: * taglib-sharp.csproj: Added file. Modified: trunk/taglib-sharp/ChangeLog =================================================================== --- trunk/taglib-sharp/ChangeLog 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/ChangeLog 2007-05-15 05:25:31 UTC (rev 77388) @@ -1,3 +1,20 @@ +2007-05-14 Brian Nickel <[EMAIL PROTECTED]> + +Feature Request: http://forum.taglib-sharp.com/viewtopic.php?f=4&t=12 + + * src/TagLib/File.cs: Support pushing and poping file abstractors. + * src/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs: + * src/TagLib/Id3v2/Frames/CommentsFrame.cs: + * src/TagLib/Id3v2/Tag.cs: Be a little bit smarter when storing lyrics + and comments. GetPreferred shouldn't be used for storing, because we + want to store in exactly what we want, not a best guess. + * src/TagLib/Id3v2/FrameFactory.cs: + * src/TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs: Support + synchronized lyrics. + * src/TagLib/TagLib.sources: + * src/taglib-sharp.mdp: + * taglib-sharp.csproj: Added file. + 2007-05-10 Brian Nickel <[EMAIL PROTECTED]> * src/TagLib/Mpeg/Properties.cs: Modified: trunk/taglib-sharp/src/TagLib/File.cs =================================================================== --- trunk/taglib-sharp/src/TagLib/File.cs 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/File.cs 2007-05-15 05:25:31 UTC (rev 77388) @@ -70,7 +70,7 @@ private static uint buffer_size = 1024; private static List<FileTypeResolver> file_type_resolvers = new List<FileTypeResolver> (); - private static FileAbstractionCreator file_abstraction_creator = new FileAbstractionCreator (LocalFileAbstraction.CreateFile); + private static List<FileAbstractionCreator> file_abstraction_creator_list = new List<FileAbstractionCreator> (); ////////////////////////////////////////////////////////////////////////// // public members @@ -79,7 +79,7 @@ public File (string file) { file_stream = null; - file_abstraction = file_abstraction_creator (file); + file_abstraction = GetFileAbstractionCreator () (file); } public string Name {get {return file_abstraction.Name;}} @@ -543,17 +543,40 @@ file_type_resolvers.Insert (0, resolver); } + [Obsolete("Use PushFileAbstractionCreator")] public static void SetFileAbstractionCreator (FileAbstractionCreator creator) { if (creator != null) - file_abstraction_creator = creator; + { + if (file_abstraction_creator_list.Count > 0) + file_abstraction_creator_list [0] = creator; + else + file_abstraction_creator_list.Add (creator); + } } - - internal static FileAbstractionCreator GetFileAbstractionCreator() + + public static void PushFileAbstractionCreator (FileAbstractionCreator creator) { - return file_abstraction_creator; + if (creator != null) + file_abstraction_creator_list.Insert (0, creator); } + public static FileAbstractionCreator PopFileAbstractionCreator () + { + if (file_abstraction_creator_list.Count == 0) + return null; + + FileAbstractionCreator creator = file_abstraction_creator_list [0]; + file_abstraction_creator_list.RemoveAt (0); + return creator; + } + + public static FileAbstractionCreator GetFileAbstractionCreator () + { + return file_abstraction_creator_list.Count > 0 ? + file_abstraction_creator_list [0] : LocalFileAbstraction.CreateFile; + } + ////////////////////////////////////////////////////////////////////////// // protected members ////////////////////////////////////////////////////////////////////////// Modified: trunk/taglib-sharp/src/TagLib/Id3v2/FrameFactory.cs =================================================================== --- trunk/taglib-sharp/src/TagLib/Id3v2/FrameFactory.cs 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/Id3v2/FrameFactory.cs 2007-05-15 05:25:31 UTC (rev 77388) @@ -111,6 +111,11 @@ if (frame_id == "USLT") return new UnsynchronisedLyricsFrame (data, offset, header, version); + // Synchronized Lyrics (frames 4.9) + + if (frame_id == "SYLT") + return new SynchronisedLyricsFrame (data, offset, header, version); + // Comments (frames 4.10) if (frame_id == "COMM") Modified: trunk/taglib-sharp/src/TagLib/Id3v2/Frames/CommentsFrame.cs =================================================================== --- trunk/taglib-sharp/src/TagLib/Id3v2/Frames/CommentsFrame.cs 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/Id3v2/Frames/CommentsFrame.cs 2007-05-15 05:25:31 UTC (rev 77388) @@ -91,7 +91,7 @@ return frame; } - public static CommentsFrame GetPreferred (Tag tag, string description, ByteVector language, bool create) + public static CommentsFrame GetPreferred (Tag tag, string description, ByteVector language) { // This is weird, so bear with me. The best thing we can have is // something straightforward and in our own language. If it has a @@ -123,11 +123,6 @@ best_frame = cf; } - if (best_frame == null && create) - { - best_frame = new CommentsFrame (description, language); - tag.AddFrame (best_frame); - } return best_frame; } Added: trunk/taglib-sharp/src/TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs =================================================================== --- trunk/taglib-sharp/src/TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs 2007-05-15 05:25:31 UTC (rev 77388) @@ -0,0 +1,243 @@ +using System; +using System.Collections.Generic; +using System.Text; +using TagLib.Id3v2; + +namespace TagLib.Id3v2 +{ + public enum TimestampFormat + { + Unknown = 0x00, + AbsoluteMpegFrames = 0x01, + AbsoluteMilliseconds = 0x02 + } + + public enum LyricsType + { + Other = 0x00, + Lyrics = 0x01, + TextTranscription = 0x02, + Movement = 0x03, + Events = 0x04, + Chord = 0x05, + Trivia = 0x06, + WebpageUrls = 0x07, + ImageUrls = 0x08 + } + + public class SynchronisedLyricsFrame : Frame + { + #region Private Properties + StringType text_encoding = StringType.UTF8; + ByteVector language = null; + string description = null; + TimestampFormat timestamp_format = TimestampFormat.Unknown; + LyricsType lyrics_type = LyricsType.Other; + SynchedText [] text = new SynchedText [0]; + #endregion + + #region Constructors + public SynchronisedLyricsFrame (string description, ByteVector language, StringType encoding) : base ("SYLT", 4) + { + this.text_encoding = encoding; + this.language = language; + this.description = description; + } + + public SynchronisedLyricsFrame (string description, ByteVector language) : this (description, language, TagLib.Id3v2.Tag.DefaultEncoding) + {} + + public SynchronisedLyricsFrame (string description) : this (description, null) + {} + + public SynchronisedLyricsFrame (ByteVector data, uint version) : base(data, version) + { + SetData (data, 0, version); + } + + public static SynchronisedLyricsFrame Get (Tag tag, string description, ByteVector language, bool create) + { + foreach (Frame f in tag) + { + if (!(f is SynchronisedLyricsFrame)) + continue; + + SynchronisedLyricsFrame lyr = f as SynchronisedLyricsFrame; + + if (lyr.Description == description && (language == null || language == lyr.Language)) + return lyr; + } + + if (!create) + return null; + + SynchronisedLyricsFrame frame = new SynchronisedLyricsFrame (description, language); + tag.AddFrame (frame); + return frame; + } + #endregion + + #region Public Properties + public StringType TextEncoding + { + get {return text_encoding;} + set {text_encoding = value;} + } + + public ByteVector Language + { + get {return language != null ? language : "XXX";} + set {language = value != null ? value.Mid (0, 3) : "XXX";} + } + + public string Description + { + get {return description;} + set {description = value;} + } + + public TimestampFormat Format + { + get {return timestamp_format;} + set {timestamp_format = value;} + } + + public LyricsType Type + { + get {return lyrics_type;} + set {lyrics_type = value;} + } + + public SynchedText [] Text + { + get {return text;} + set {text = value == null ? new SynchedText [0] : value;} + } + #endregion + + + public static SynchronisedLyricsFrame GetPreferred (Tag tag, string description, ByteVector language) + { + // This is weird, so bear with me. The best thing we can have is + // something straightforward and in our own language. If it has a + // description, then it is probably used for something other than + // an actual comment. If that doesn't work, we'd still rather have + // something in our language than something in another. After that + // all we have left are things in other languages, so we'd rather + // have one with actual content, so we try to get one with no + // description first. + + int best_value = -1; + SynchronisedLyricsFrame best_frame = null; + + foreach (Frame f in tag) + { + if (!(f is SynchronisedLyricsFrame)) + continue; + + SynchronisedLyricsFrame cf = f as SynchronisedLyricsFrame; + + bool same_name = cf.Description == description; + bool same_lang = cf.Language == language; + + if (same_name && same_lang) return cf; + + int value = same_lang ? 2 : same_name ? 1 : 0; + + if (value <= best_value) + continue; + + best_value = value; + best_frame = cf; + } + + return best_frame; + } + + + protected override void ParseFields (ByteVector data, uint version) + { + if (data.Count < 6) + throw new CorruptFileException ("Not enough bytes in field."); + + text_encoding = (StringType) data [0]; + language = data.Mid (1, 3); + timestamp_format = (TimestampFormat) data [4]; + lyrics_type = (LyricsType) data [5]; + + ByteVector delim = TextDelimiter (text_encoding); + int delim_index = data.Find (delim, 6); + + if (delim_index < 0) + throw new CorruptFileException ("Text delimiter expected."); + + description = data.Mid (6, delim_index - 6).ToString (text_encoding); + + int offset = delim_index + delim.Count; + List<SynchedText> l = new List<SynchedText> (); + + while (offset + delim.Count + 4 < data.Count) + { + delim_index = data.Find (delim, offset); + if (delim_index < offset) + throw new CorruptFileException ("Text delimiter expected."); + + string text = data.Mid (offset, delim_index - offset).ToString (text_encoding); + offset = delim_index + delim.Count; + + if (offset + 4 > data.Count) + break; + + l.Add (new SynchedText (data.Mid (offset, 4).ToUInt (), text)); + offset += 4; + } + + this.text = l.ToArray (); + } + + protected override ByteVector RenderFields (uint version) + { + StringType encoding = CorrectEncoding(TextEncoding, version); + ByteVector delim = TextDelimiter (encoding); + ByteVector v = new ByteVector (); + + v.Add ((byte)encoding); + v.Add (Language); + v.Add ((byte)timestamp_format); + v.Add ((byte)lyrics_type); + v.Add (ByteVector.FromString (description, encoding)); + v.Add (delim); + + foreach (SynchedText t in text) + { + v.Add (ByteVector.FromString (t.Text, encoding)); + v.Add (delim); + v.Add (ByteVector.FromUInt (t.Time)); + System.Console.WriteLine (t.Text); + } + + return v; + } + + protected internal SynchronisedLyricsFrame(ByteVector data, int offset, FrameHeader h, uint version) + : base(h) + { + ParseFields (FieldData (data, offset, version), version); + } + } + + public struct SynchedText + { + private uint time; + private string text; + + public SynchedText (uint time, string text) + { + this.time = time; + this.text = text; + } + + public uint Time {get {return time;} set {time = value;}} + public string Text {get {return text;} set {text = value;}} + } +} \ No newline at end of file Modified: trunk/taglib-sharp/src/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs =================================================================== --- trunk/taglib-sharp/src/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs 2007-05-15 05:25:31 UTC (rev 77388) @@ -72,7 +72,7 @@ return frame; } - public static UnsynchronisedLyricsFrame GetPreferred (Tag tag, string description, ByteVector language, bool create) + public static UnsynchronisedLyricsFrame GetPreferred (Tag tag, string description, ByteVector language) { // This is weird, so bear with me. The best thing we can have is // something straightforward and in our own language. If it has a @@ -105,11 +105,6 @@ best_frame = cf; } - if (best_frame == null && create) - { - best_frame = new UnsynchronisedLyricsFrame (description, language); - tag.AddFrame (best_frame); - } return best_frame; } Modified: trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs =================================================================== --- trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/Id3v2/Tag.cs 2007-05-15 05:25:31 UTC (rev 77388) @@ -20,12 +20,13 @@ * USA * ***************************************************************************/ +using System.Collections; using System.Collections.Generic; using System; namespace TagLib.Id3v2 { - public class Tag : TagLib.Tag + public class Tag : TagLib.Tag, IEnumerable { ////////////////////////////////////////////////////////////////////////// // private properties @@ -63,6 +64,11 @@ Read (file); } + public IEnumerator GetEnumerator() + { + return frame_list.GetEnumerator(); + } + public IEnumerable<Frame> GetFrames () { return frame_list; @@ -232,12 +238,12 @@ { get { - CommentsFrame f = CommentsFrame.GetPreferred (this, String.Empty, Language, false); + CommentsFrame f = CommentsFrame.GetPreferred (this, String.Empty, Language); return f != null ? f.ToString () : null; } set { - CommentsFrame f = CommentsFrame.GetPreferred (this, String.Empty, Language, true); + CommentsFrame f = CommentsFrame.Get (this, String.Empty, Language, true); f.SetText (value); } } @@ -423,12 +429,12 @@ { get { - UnsynchronisedLyricsFrame f = UnsynchronisedLyricsFrame.GetPreferred (this, String.Empty, Language, false); + UnsynchronisedLyricsFrame f = UnsynchronisedLyricsFrame.GetPreferred (this, String.Empty, Language); return f != null ? f.ToString () : null; } set { - UnsynchronisedLyricsFrame f = UnsynchronisedLyricsFrame.GetPreferred (this, String.Empty, Language, true); + UnsynchronisedLyricsFrame f = UnsynchronisedLyricsFrame.Get (this, String.Empty, Language, true); f.SetText (value); } } Modified: trunk/taglib-sharp/src/TagLib/TagLib.sources =================================================================== --- trunk/taglib-sharp/src/TagLib/TagLib.sources 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/TagLib/TagLib.sources 2007-05-15 05:25:31 UTC (rev 77388) @@ -51,6 +51,7 @@ $(srcdir)/TagLib/Id3v2/Frames/AttachedPictureFrame.cs \ $(srcdir)/TagLib/Id3v2/Frames/GeneralEncapsulatedObjectFrame.cs \ $(srcdir)/TagLib/Id3v2/Frames/UnsynchronisedLyricsFrame.cs \ + $(srcdir)/TagLib/Id3v2/Frames/SynchronisedLyricsFrame.cs \ $(srcdir)/TagLib/Id3v2/FrameFactory.cs \ $(srcdir)/TagLib/Id3v2/Frame.cs \ $(srcdir)/TagLib/Id3v2/Tag.cs \ Modified: trunk/taglib-sharp/src/taglib-sharp.mdp =================================================================== --- trunk/taglib-sharp/src/taglib-sharp.mdp 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/src/taglib-sharp.mdp 2007-05-15 05:25:31 UTC (rev 77388) @@ -134,6 +134,7 @@ <File name="./TagLib/Riff/AviStream.cs" subtype="Code" buildaction="Compile" /> <File name="./TagLib/Riff/WaveFormatEx.cs" subtype="Code" buildaction="Compile" /> <File name="./TagLib/Riff/BitmapInfoHeader.cs" subtype="Code" buildaction="Compile" /> + <File name="./TagLib/Id3v2/Frames/SynchronizedLyricsFrame.cs" subtype="Code" buildaction="Compile" /> </Contents> <References> <ProjectReference type="Gac" localcopy="True" refto="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> Modified: trunk/taglib-sharp/taglib-sharp.csproj =================================================================== --- trunk/taglib-sharp/taglib-sharp.csproj 2007-05-15 03:51:35 UTC (rev 77387) +++ trunk/taglib-sharp/taglib-sharp.csproj 2007-05-15 05:25:31 UTC (rev 77388) @@ -112,6 +112,7 @@ <Compile Include="src\TagLib\Id3v2\Frames\TextIdentificationFrame.cs" /> <Compile Include="src\TagLib\Id3v2\Frames\UniqueFileIdentifierFrame.cs" /> <Compile Include="src\TagLib\Id3v2\Frames\UnsynchronisedLyricsFrame.cs" /> + <Compile Include="src\TagLib\Id3v2\Frames\SynchronisedLyricsFrame.cs" /> <Compile Include="src\TagLib\Id3v1\StringHandler.cs" /> <Compile Include="src\TagLib\Id3v1\Tag.cs" /> <Compile Include="src\TagLib\Flac\Block.cs" /> _______________________________________________ Mono-patches maillist - [email protected] http://lists.ximian.com/mailman/listinfo/mono-patches
