Fix for SongWriter

This commit is contained in:
Flamefire committed 2013-10-12 18:08:46 +02:00
1 parent cc6d921f03
commit 87ba525811
4 files changed
+84 -74

No files matched your search

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