Refactored song loading

~9% faster and a bit more stable
This commit is contained in:
Flamefire committed 2013-02-22 23:19:13 +01:00
1 parent 9c78bbe45f
commit 9761c115b2
4 files changed
+156 -154

No files matched your search

+4 -4
View File
@@ -1022,8 +1022,8 @@ namespace Vocaluxe.Base
CLog.StartBenchmark(2, "Read TXTs"); CLog.StartBenchmark(2, "Read TXTs");
foreach (string file in files) foreach (string file in files)
{ {
CSong Song = new CSong(); CSong Song = CSong.LoadSong(file);
if (Song.ReadTXTSong(file)) if (Song != null)
{ {
Song.ID = _Songs.Count; Song.ID = _Songs.Count;
_Songs.Add(Song); _Songs.Add(Song);
@@ -1039,7 +1039,7 @@ namespace Vocaluxe.Base
if (CConfig.Renderer != ERenderer.TR_CONFIG_SOFTWARE && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART) if (CConfig.Renderer != ERenderer.TR_CONFIG_SOFTWARE && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART)
{ {
CLog.StartBenchmark(2, "Load Cover"); CLog.StartBenchmark(2, "Load Covers/Notes");
for (int i = 0; i < _Songs.Count; i++) for (int i = 0; i < _Songs.Count; i++)
{ {
CSong song = _Songs[i]; CSong song = _Songs[i];
@@ -1058,7 +1058,7 @@ namespace Vocaluxe.Base
_CoverLoaded = true; _CoverLoaded = true;
CDataBase.CommitCovers(); CDataBase.CommitCovers();
CLog.StopBenchmark(2, "Load Cover"); CLog.StopBenchmark(2, "Load Covers/Notes");
} }
CLog.StopBenchmark(1, "Load Songs "); CLog.StopBenchmark(1, "Load Songs ");
GC.Collect(); GC.Collect();
+2 -2
View File
@@ -137,7 +137,7 @@ namespace Vocaluxe.Screens
} }
} }
CLog.StartBenchmark(3, "Load Cover"); CLog.StartBenchmark(0, "Load Songs Full");
_SongLoaderThread.IsBackground = true; _SongLoaderThread.IsBackground = true;
_SongLoaderThread.Start(); _SongLoaderThread.Start();
_timer.Start(); _timer.Start();
@@ -221,7 +221,7 @@ namespace Vocaluxe.Screens
CBackgroundMusic.CanSing = true; CBackgroundMusic.CanSing = true;
CLog.StopBenchmark(3, "Load Cover"); CLog.StopBenchmark(0, "Load Songs Full");
//Init Playlists //Init Playlists
CLog.StartBenchmark(0, "Init Playlists"); CLog.StartBenchmark(0, "Init Playlists");
+4 -3
View File
@@ -58,12 +58,13 @@ namespace Vocaluxe.Menu
try try
{ {
chr = (char)sr.Peek(); int tmp = sr.Peek();
while ((chr.CompareTo(' ') != 0) && ((int)chr != 19) && ((int)chr != 16) && ((int)chr != 13)) //Check for ' ', ?, ?, \n, \r
while (tmp != 32 && tmp != 19 && tmp != 16 && tmp != 13 && tmp != 10)
{ {
chr = (char)sr.Read(); chr = (char)sr.Read();
value += chr.ToString(); value += chr.ToString();
chr = (char)sr.Peek(); tmp = sr.Peek();
} }
} }
catch (Exception) catch (Exception)
+146 -145
View File
@@ -111,8 +111,9 @@ namespace Vocaluxe.Menu.SongMenu
{ {
if (!_CoverLoaded) if (!_CoverLoaded)
{ {
if (!_NotesLoaded) //Why in hell would you load the notes to get the cover???
this.ReadNotes(); //if (!_NotesLoaded)
// this.ReadNotes();
if (this.CoverFileName != String.Empty) if (this.CoverFileName != String.Empty)
{ {
@@ -193,10 +194,19 @@ namespace Vocaluxe.Menu.SongMenu
} }
} }
public CSong() //No point creating a song without a text file --> Use factory method LoadSong
private CSong()
{ {
} }
public static CSong LoadSong(string FilePath)
{
CSong Song = new CSong();
if (Song.ReadHeader(FilePath))
return Song;
return null;
}
public CSong(CSong song) public CSong(CSong song)
{ {
this._CoverTextureSmall = song._CoverTextureSmall; this._CoverTextureSmall = song._CoverTextureSmall;
@@ -286,18 +296,7 @@ namespace Vocaluxe.Menu.SongMenu
return Path.Combine(Folder, VideoFileName); return Path.Combine(Folder, VideoFileName);
} }
public bool ReadTXTSong(string FilePath) public bool ReadHeader(string FilePath)
{
if (!File.Exists(FilePath))
return false;
if (!ReadTXTHeader(FilePath))
return false;
return true;
}
public bool ReadTXTHeader(string FilePath)
{ {
if (!File.Exists(FilePath)) if (!File.Exists(FilePath))
return false; return false;
@@ -314,17 +313,11 @@ namespace Vocaluxe.Menu.SongMenu
{ {
this.FolderName = this.Folder.Substring(folder.Length + 1, this.Folder.Length - folder.Length - 1); this.FolderName = this.Folder.Substring(folder.Length + 1, this.Folder.Length - folder.Length - 1);
string str = this.FolderName; int pos = this.FolderName.IndexOf("\\");
try if (pos >= 0)
{ this.FolderName = this.FolderName.Substring(0, pos);
str = this.FolderName.Substring(0, this.FolderName.IndexOf("\\"));
}
catch (Exception)
{
}
this.FolderName = str;
} }
break;
} }
} }
@@ -337,16 +330,19 @@ namespace Vocaluxe.Menu.SongMenu
sr = new StreamReader(FilePath, Encoding.Default, true); sr = new StreamReader(FilePath, Encoding.Default, true);
string line = sr.ReadLine(); string line;
if (line.Length == 0)
return false;
int pos = -1; int pos = -1;
string Identifier = String.Empty; string Identifier = String.Empty;
string Value = String.Empty; string Value = String.Empty;
while ((line.Length == 0) || (line[0].ToString().Equals("#"))) while (!sr.EndOfStream)
{ {
line = sr.ReadLine();
if (line.Length == 0)
continue;
if (!line[0].ToString().Equals("#")) break;
pos = line.IndexOf(":"); pos = line.IndexOf(":");
if (pos > 0) if (pos > 0)
@@ -362,26 +358,22 @@ namespace Vocaluxe.Menu.SongMenu
this.Encoding = CEncoding.GetEncoding(Value); this.Encoding = CEncoding.GetEncoding(Value);
sr = new StreamReader(FilePath, this.Encoding); sr = new StreamReader(FilePath, this.Encoding);
Identifier = String.Empty; Identifier = String.Empty;
line = sr.ReadLine();
while ((line.Length == 0) || (line[0].ToString().Equals("#")) && (Identifier != "ENCODING")) //Scip everything till encoding
while (!sr.EndOfStream)
{ {
line = sr.ReadLine();
if (line.Length == 0)
continue;
if(!line[0].ToString().Equals("#")) break;
pos = line.IndexOf(":"); pos = line.IndexOf(":");
if (pos > 0) if (pos > 0)
{ {
Identifier = line.Substring(1, pos - 1).Trim().ToUpper(); Identifier = line.Substring(1, pos - 1).Trim().ToUpper();
Value = line.Substring(pos + 1, line.Length - pos - 1).Trim();
}
if (!sr.EndOfStream)
if (Identifier == "ENCODING") if (Identifier == "ENCODING")
break; break;
else
line = sr.ReadLine();
else
{
return false;
} }
} }
break; break;
@@ -482,7 +474,6 @@ namespace Vocaluxe.Menu.SongMenu
this.VideoFileName = Value; this.VideoFileName = Value;
else else
CBase.Base.Log.LogError("Can't find video file: " + Path.Combine(this.Folder, Value)); CBase.Base.Log.LogError("Can't find video file: " + Path.Combine(this.Folder, Value));
break; break;
case "VIDEOGAP": case "VIDEOGAP":
CHelper.TryParse(Value, out this.VideoGap); CHelper.TryParse(Value, out this.VideoGap);
@@ -520,21 +511,18 @@ namespace Vocaluxe.Menu.SongMenu
if (Value.ToUpper() == "YES" && Value.ToUpper() != "NO") if (Value.ToUpper() == "YES" && Value.ToUpper() != "NO")
this.Relative = EOffOn.TR_CONFIG_ON; this.Relative = EOffOn.TR_CONFIG_ON;
break; break;
default:
;
break;
} }
} }
} }
if (!sr.EndOfStream)
line = sr.ReadLine();
else
{
return false;
}
} //end of while } //end of while
if (sr.EndOfStream)
{
//No other data then header
CBase.Base.Log.LogError("Lyrics/Notes missing: " + FilePath);
return false;
}
if ((HeaderFlags & EHeaderFlags.Title) == 0) if ((HeaderFlags & EHeaderFlags.Title) == 0)
{ {
CBase.Base.Log.LogError("Title tag missing: " + FilePath); CBase.Base.Log.LogError("Title tag missing: " + FilePath);
@@ -645,119 +633,132 @@ namespace Vocaluxe.Menu.SongMenu
this.Notes.Reset(); this.Notes.Reset();
//Search for Note Beginning //Search for Note Beginning
do while(!sr.EndOfStream)
{ {
line = sr.ReadLine(); line = sr.ReadLine();
FileLineNo++; FileLineNo++;
if (sr.EndOfStream || (line.Length == 0)) TempC = (char)sr.Peek();
if ((TempC == '#') || (TempC == '\r') || (TempC == '\n'))
continue;
if ((TempC == ':') || (TempC == 'F') || (TempC == '*') || (TempC == 'P'))
{ {
CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". The file is empty or it starts with an empty line: " + FilePath); TempC = (char)sr.Read();
return false; break;
} }
}
TempC = (char)sr.Read(); if (sr.EndOfStream)
} while ((TempC.CompareTo(':') != 0) && (TempC.CompareTo('F') != 0) && (TempC.CompareTo('*') != 0) && (TempC.CompareTo('P') != 0));
while (!sr.EndOfStream && (TempC.CompareTo('E') != 0))
{ {
FileLineNo++; CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". No lyrics/notes found: " + FilePath);
if (TempC.CompareTo('P') == 0) return false;
}
do
{
char chr;
switch (TempC)
{ {
char chr; case 'P':
while ((chr = (char)sr.Read()) == ' '){ } while ((chr = (char)sr.Read()) == ' ') { }
int.TryParse(chr.ToString(), out Param1); int.TryParse(chr.ToString(), out Param1);
if (Param1 == 1) if (Param1 == 1)
Player = 0; Player = 0;
else if (Param1 == 2) else if (Param1 == 2)
Player = 1; Player = 1;
else if (Param1 == 3) else if (Param1 == 3)
Player = 2; Player = 2;
else else
{ {
CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". Wrong or missing number after \"P\": " + FilePath); CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". Wrong or missing number after \"P\": " + FilePath);
return false; return false;
} }
} sr.ReadLine();
break;
case ':':
case '*':
case 'F':
sr.Read();
Param1 = CHelper.TryReadInt(sr);
if ((TempC.CompareTo(':') == 0) || (TempC.CompareTo('*') == 0) || (TempC.CompareTo('F') == 0)) sr.Read();
{
char chr = (char)sr.Read();
Param1 = CHelper.TryReadInt(sr);
chr = (char)sr.Read();
Param2 = CHelper.TryReadInt(sr);
chr = (char)sr.Read();
Param3 = CHelper.TryReadInt(sr);
chr = (char)sr.Read();
ParamS = sr.ReadLine();
ENoteType NoteType;
if (TempC.CompareTo('*') == 0)
NoteType = ENoteType.Golden;
else if (TempC.CompareTo('F') == 0)
NoteType = ENoteType.Freestyle;
else
NoteType = ENoteType.Normal;
if (this.Relative == EOffOn.TR_CONFIG_ON)
Param1 += currentPos;
if (Player != 2)
// one singer
ParseNote(Player, NoteType, Param1, Param2, Param3, ParamS);
else
{
// both singer
ParseNote(0, NoteType, Param1, Param2, Param3, ParamS);
ParseNote(1, NoteType, Param1, Param2, Param3, ParamS);
}
isNewSentence = false;
}
if (TempC.CompareTo('-') == 0)
{
if (isNewSentence)
{
CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". Double sentence break: " + FilePath);
return false;
}
char chr = (char)sr.Read();
Param1 = CHelper.TryReadInt(sr);
if (this.Relative == EOffOn.TR_CONFIG_ON)
{
Param1 += currentPos;
chr = (char)sr.Read();
Param2 = CHelper.TryReadInt(sr); Param2 = CHelper.TryReadInt(sr);
currentPos += Param2;
}
if(Player != 2) sr.Read();
// one singer Param3 = CHelper.TryReadInt(sr);
NewSentence(Player, Param1);
else
{
// both singer
NewSentence(0, Param1);
NewSentence(1, Param1);
}
isNewSentence = true; sr.Read();
ParamS = sr.ReadLine();
ENoteType NoteType;
if (TempC.CompareTo('*') == 0)
NoteType = ENoteType.Golden;
else if (TempC.CompareTo('F') == 0)
NoteType = ENoteType.Freestyle;
else
NoteType = ENoteType.Normal;
if (this.Relative == EOffOn.TR_CONFIG_ON)
Param1 += currentPos;
if (Player != 2)
// one singer
ParseNote(Player, NoteType, Param1, Param2, Param3, ParamS);
else
{
// both singer
ParseNote(0, NoteType, Param1, Param2, Param3, ParamS);
ParseNote(1, NoteType, Param1, Param2, Param3, ParamS);
}
isNewSentence = false;
break;
case '-':
if (isNewSentence)
{
CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". Double sentence break: " + FilePath);
return false;
}
sr.Read();
Param1 = CHelper.TryReadInt(sr);
if (this.Relative == EOffOn.TR_CONFIG_ON)
{
Param1 += currentPos;
sr.Read();
Param2 = CHelper.TryReadInt(sr);
currentPos += Param2;
}
if (Player != 2)
// one singer
NewSentence(Player, Param1);
else
{
// both singer
NewSentence(0, Param1);
NewSentence(1, Param1);
}
isNewSentence = true;
sr.ReadLine();
break;
default:
CBase.Base.Log.LogError("Error loading song. Line No.: " + FileLineNo.ToString() + ". Unexpected or missing character (" + TempC + "): " + FilePath);
return false;
} }
int c = 0; int c = 0;
do do
{ {
c = sr.Read(); c = sr.Read();
TempC = (char)c; } while (!sr.EndOfStream && (c == 19 || c == 16 || c == 13 || c == 10));
} while ((TempC.CompareTo('E') != 0) && !sr.EndOfStream && (c == 19 || c == 16 || c == 13 || c==10)); TempC = (char)c;
} FileLineNo++;
}while(!sr.EndOfStream && (TempC != 'E'));
foreach(CLines lines in this.Notes.Lines) foreach(CLines lines in this.Notes.Lines)
{ {
lines.UpdateTimings(); lines.UpdateTimings();