From 87ba525811500cddbdea2398aa9d6b0964ae4915 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Sat, 12 Oct 2013 18:08:46 +0200 Subject: [PATCH] Fix for SongWriter --- VocaluxeLib/CHelper.cs | 4 +- VocaluxeLib/Songs/CSong.cs | 21 +++--- VocaluxeLib/Songs/CSongLoader.cs | 25 +++---- VocaluxeLib/Songs/CSongWriter.cs | 108 +++++++++++++++++-------------- 4 files changed, 84 insertions(+), 74 deletions(-) diff --git a/VocaluxeLib/CHelper.cs b/VocaluxeLib/CHelper.cs index 97192ae1..7b441caa 100644 --- a/VocaluxeLib/CHelper.cs +++ b/VocaluxeLib/CHelper.cs @@ -199,7 +199,7 @@ namespace VocaluxeLib static class CEncoding { - public static Encoding GetEncoding(string encodingName) + public static Encoding GetEncoding(this string encodingName) { switch (encodingName) { @@ -218,7 +218,7 @@ namespace VocaluxeLib } } - public static string GetEncodingName(Encoding enc) + public static string GetEncodingName(this Encoding enc) { string result = "UTF8"; diff --git a/VocaluxeLib/Songs/CSong.cs b/VocaluxeLib/Songs/CSong.cs index 688d2d28..ac528889 100644 --- a/VocaluxeLib/Songs/CSong.cs +++ b/VocaluxeLib/Songs/CSong.cs @@ -78,6 +78,7 @@ namespace VocaluxeLib.Songs public int ShortEnd; public Encoding Encoding = Encoding.Default; + public bool ManualEncoding = false; public string Folder = String.Empty; public string FolderName = String.Empty; public string FileName = String.Empty; @@ -187,14 +188,7 @@ namespace VocaluxeLib.Songs _CoverTextureSmall = song._CoverTextureSmall; _CoverTextureBig = song._CoverTextureBig; - Medley = new SMedley - { - Source = song.Medley.Source, - StartBeat = song.Medley.StartBeat, - EndBeat = song.Medley.EndBeat, - FadeInTime = song.Medley.FadeInTime, - FadeOutTime = song.Medley.FadeOutTime - }; + Medley = song.Medley; _CalculateMedley = song._CalculateMedley; PreviewStart = song.PreviewStart; @@ -202,6 +196,7 @@ namespace VocaluxeLib.Songs ShortEnd = song.ShortEnd; Encoding = song.Encoding; + ManualEncoding = song.ManualEncoding; Folder = song.Folder; FolderName = song.FolderName; FileName = song.FileName; @@ -217,6 +212,13 @@ namespace VocaluxeLib.Songs Artist = song.Artist; Title = song.Title; + ArtistSorting = song.ArtistSorting; + TitleSorting = song.TitleSorting; + + Creator = song.Creator; + Version = song.Version; + Source = song.Source; + UnknownTags = new List(song.UnknownTags); Start = song.Start; Finish = song.Finish; @@ -233,9 +235,8 @@ namespace VocaluxeLib.Songs _Selected = song._Selected; Editions = new List(song.Editions); - Genres = new List(song.Genres); - + Album = song.Album; Year = song.Year; Languages = new List(song.Languages); diff --git a/VocaluxeLib/Songs/CSongLoader.cs b/VocaluxeLib/Songs/CSongLoader.cs index 98cc839b..0720be1a 100644 --- a/VocaluxeLib/Songs/CSongLoader.cs +++ b/VocaluxeLib/Songs/CSongLoader.cs @@ -77,7 +77,7 @@ namespace VocaluxeLib.Songs _LogMsg(msg, false, withLineNr); } - public bool ReadHeader(Encoding encoding = null) + public bool ReadHeader(bool ignoreSetEncoding = false) { string filePath = Path.Combine(_Song.Folder, _Song.FileName); @@ -88,13 +88,14 @@ namespace VocaluxeLib.Songs _Song.Genres.Clear(); _Song.UnknownTags.Clear(); _Song._Comment = ""; + _Song.ManualEncoding = false; var headerFlags = new EHeaderFlags(); StreamReader sr = null; _LineNr = 0; try { - sr = new StreamReader(filePath, Encoding.Default, true); + sr = new StreamReader(filePath, _Song.Encoding, true); while (!sr.EndOfStream) { string line = sr.ReadLine(); @@ -129,17 +130,19 @@ namespace VocaluxeLib.Songs switch (identifier) { case "ENCODING": - if (encoding != null) - { - _LogWarning("Duplicate encoding ignored"); - continue; - } - Encoding newEncoding = CEncoding.GetEncoding(value); + Encoding newEncoding = value.GetEncoding(); + _Song.ManualEncoding = true; if (!newEncoding.Equals(sr.CurrentEncoding)) { + if (ignoreSetEncoding) + { + _LogWarning("Duplicate encoding ignored"); + continue; + } sr.Dispose(); sr = null; - return ReadHeader(_Song.Encoding); + _Song.Encoding = newEncoding; + return ReadHeader(true); } break; case "TITLE": @@ -300,7 +303,7 @@ namespace VocaluxeLib.Songs if (value.ToUpper() == "YES") _Song.Relative = true; break; - case "RESOULTION": + case "RESOLUTION": _LogWarning("#RESOLUTION tag is outdated and ignored. Test the song to see if note timing is still correct!"); break; default: @@ -383,8 +386,6 @@ namespace VocaluxeLib.Songs _Song.Medley.FadeOutTime = CBase.Settings.GetDefaultMedleyFadeOutTime(); } #endregion check medley tags - - _Song.Encoding = sr.CurrentEncoding; } catch (Exception e) { diff --git a/VocaluxeLib/Songs/CSongWriter.cs b/VocaluxeLib/Songs/CSongWriter.cs index a108c685..73c2ecc5 100644 --- a/VocaluxeLib/Songs/CSongWriter.cs +++ b/VocaluxeLib/Songs/CSongWriter.cs @@ -9,91 +9,99 @@ namespace VocaluxeLib.Songs private class CSongWriter { private readonly CSong _Song; + private TextWriter tw; public CSongWriter(CSong song) { _Song = song; } - private void _WriteHeaderEntry(TextWriter sw, string id, T value) + private void _WriteHeaderEntry(string id, string value) { - if (!value.Equals(default(T))) - sw.WriteLine("#" + id.ToUpper() + ":" + value); + if (!String.IsNullOrEmpty(value)) + tw.WriteLine("#" + id.ToUpper() + ":" + value); } - private void _WriteHeaderEntry(TextWriter sw, string id, bool value) + private void _WriteHeaderEntry(string id, bool value) { if (value) - _WriteHeaderEntry(sw, id, "YES"); + _WriteHeaderEntry(id, "YES"); } - private void _WriteHeaderEntrys(TextWriter sw, string id, IList value) + private void _WriteHeaderEntry(string id, float value, float def = 0f) { - if (value != null && value.Count > 0) - { - foreach (T val in value) - _WriteHeaderEntry(sw, id, val); - } + if (Math.Abs(value - def) > 0.0001) + _WriteHeaderEntry(id, value.ToString()); } - private void _WriteHeader(TextWriter sw) + private void _WriteHeaderEntrys(string id, ICollection value) { - _WriteHeaderEntry(sw, "ENCODING", _Song.Encoding); - _WriteHeaderEntry(sw, "CREATOR", _Song.Creator); - _WriteHeaderEntry(sw, "VERSION", _Song.Version); - _WriteHeaderEntry(sw, "SOURCE", _Song.Source); + if (value == null || value.Count <= 0) + return; + foreach (String val in value) + _WriteHeaderEntry(id, val); + } + + private void _WriteHeader() + { + if (_Song.ManualEncoding) + _WriteHeaderEntry("ENCODING", _Song.Encoding.GetEncodingName()); + _WriteHeaderEntry("CREATOR", _Song.Creator); + _WriteHeaderEntry("VERSION", _Song.Version); + _WriteHeaderEntry("SOURCE", _Song.Source); if (!String.IsNullOrEmpty(_Song._Comment)) { string comment = _Song._Comment.Replace("\r\n", "\n").Replace('\r', '\n'); char[] splitChar = {'\n'}; - _WriteHeaderEntrys(sw, "COMMENT", comment.Split(splitChar)); + _WriteHeaderEntrys("COMMENT", comment.Split(splitChar)); } - _WriteHeaderEntry(sw, "TITLE", _Song.Title); - _WriteHeaderEntry(sw, "ARTIST", _Song.Artist); + _WriteHeaderEntry("TITLE", _Song.Title); + _WriteHeaderEntry("ARTIST", _Song.Artist); if (!_Song.Title.Equals(_Song.TitleSorting)) - _WriteHeaderEntry(sw, "TITLE-ON-SORTING", _Song.TitleSorting); + _WriteHeaderEntry("TITLE-ON-SORTING", _Song.TitleSorting); if (!_Song.Artist.Equals(_Song.ArtistSorting)) - _WriteHeaderEntry(sw, "ARTIST-ON-SORTING", _Song.ArtistSorting); - _WriteHeaderEntrys(sw, "EDITION", _Song.Editions); - _WriteHeaderEntrys(sw, "GENRE", _Song.Genres); - _WriteHeaderEntrys(sw, "LANGUAGE", _Song.Languages); - _WriteHeaderEntry(sw, "ALBUM", _Song.Album); - _WriteHeaderEntry(sw, "YEAR", _Song.Year); - _WriteHeaderEntry(sw, "MP3", _Song.MP3FileName); - _WriteHeaderEntry(sw, "COVER", _Song.CoverFileName); - _WriteHeaderEntry(sw, "BACKGROUND", _Song.BackgroundFileName); - _WriteHeaderEntry(sw, "VIDEO", _Song.VideoFileName); - _WriteHeaderEntry(sw, "VIDEOGAP", _Song.VideoGap); - _WriteHeaderEntry(sw, "VIDEOASPECT", _Song.VideoAspect); - _WriteHeaderEntry(sw, "RELATIVE", _Song.Relative); - _WriteHeaderEntry(sw, "BPM", _Song.BPM); - _WriteHeaderEntry(sw, "GAP", _Song.Gap); - _WriteHeaderEntry(sw, "PREVIEWSTART", _Song.PreviewStart); - _WriteHeaderEntry(sw, "START", _Song.Start); - _WriteHeaderEntry(sw, "END", _Song.Finish); + _WriteHeaderEntry("ARTIST-ON-SORTING", _Song.ArtistSorting); + _WriteHeaderEntrys("EDITION", _Song.Editions); + _WriteHeaderEntrys("GENRE", _Song.Genres); + _WriteHeaderEntrys("LANGUAGE", _Song.Languages); + _WriteHeaderEntry("ALBUM", _Song.Album); + _WriteHeaderEntry("YEAR", _Song.Year); + _WriteHeaderEntry("MP3", _Song.MP3FileName); + _WriteHeaderEntry("COVER", _Song.CoverFileName); + _WriteHeaderEntry("BACKGROUND", _Song.BackgroundFileName); + _WriteHeaderEntry("VIDEO", _Song.VideoFileName); + _WriteHeaderEntry("VIDEOGAP", _Song.VideoGap); + if (_Song.VideoAspect != EAspect.Crop) + _WriteHeaderEntry("VIDEOASPECT", _Song.VideoAspect.ToString()); + _WriteHeaderEntry("RELATIVE", _Song.Relative); + _WriteHeaderEntry("BPM", _Song.BPM / _BPMFactor); + _WriteHeaderEntry("GAP", _Song.Gap * 1000f); + _WriteHeaderEntry("PREVIEWSTART", _Song.PreviewStart); + _WriteHeaderEntry("START", _Song.Start); + _WriteHeaderEntry("END", _Song.Finish * 1000f); if (!_Song._CalculateMedley) - _WriteHeaderEntry(sw, "CALCMEDLEY", "OFF"); + _WriteHeaderEntry("CALCMEDLEY", "OFF"); if (_Song.Medley.Source == EMedleySource.Tag) { - _WriteHeaderEntry(sw, "MEDLEYSTARTBEAT", _Song.Medley.StartBeat); - _WriteHeaderEntry(sw, "MEDLEYENDBEAT", _Song.Medley.EndBeat); + _WriteHeaderEntry("MEDLEYSTARTBEAT", _Song.Medley.StartBeat); + _WriteHeaderEntry("MEDLEYENDBEAT", _Song.Medley.EndBeat); } for (int i = 0; i < _Song.Notes.VoiceCount; i++) { if (_Song.Notes.VoiceNames.IsSet(i)) - _WriteHeaderEntry(sw, "P" + i, _Song.Notes.VoiceNames[i]); + _WriteHeaderEntry("P" + i, _Song.Notes.VoiceNames[i]); } foreach (string addLine in _Song.UnknownTags) - sw.WriteLine(addLine); + tw.WriteLine(addLine); } - private void _WriteNotes(TextWriter sw) + private void _WriteNotes() { for (int i = 0; i < _Song.Notes.VoiceCount; i++) { CVoice voice = _Song.Notes.GetVoice(i); if (_Song.Notes.VoiceCount > 1) - sw.WriteLine("P" + Math.Pow(2, i)); + tw.WriteLine("P" + Math.Pow(2, i)); bool firstLine = true; int currentBeat = 0; foreach (CSongLine line in voice.Lines) @@ -106,7 +114,7 @@ namespace VocaluxeLib.Songs lineTxt += " " + (line.FirstNoteBeat - currentBeat); currentBeat = line.FirstNoteBeat; } - sw.WriteLine(lineTxt); + tw.WriteLine(lineTxt); } else firstLine = false; @@ -127,7 +135,7 @@ namespace VocaluxeLib.Songs default: throw new NotImplementedException("Note type " + note.Type); } - sw.WriteLine(tag + " " + (note.StartBeat - currentBeat) + " " + note.Duration + " " + note.Tone + " " + note.Text); + tw.WriteLine(tag + " " + (note.StartBeat - currentBeat) + " " + note.Duration + " " + note.Tone + " " + note.Text); } } } @@ -137,9 +145,9 @@ namespace VocaluxeLib.Songs { try { - TextWriter sw = new StreamWriter(filePath, false, _Song.Encoding); - _WriteHeader(sw); - _WriteNotes(sw); + tw = new StreamWriter(filePath, false, _Song.Encoding); + _WriteHeader(); + _WriteNotes(); } catch (Exception e) {