Example #1
0
        public bool readKaraokeLyric(string input)
        {
            string   realString  = "";
            string   timestr     = "";
            TimeSpan beginTime   = TimeSpan.FromSeconds(0);
            TimeSpan tmptime     = TimeSpan.FromSeconds(0);
            int      state       = 0; //0:read string, 1: read time
            int      lastLinePos = 0;

            if (input[input.Length - 1] != '\n')
            {
                input += "\n";
            }
            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] == '\n')
                {
                    //split line
                    realString  = input.Substring(lastLinePos, i - lastLinePos).Trim();
                    lastLinePos = i + 1;
                    if (realString != "")
                    {
                        if (i > 0 && input[i - 1] != ']')
                        {
                            tmptime    += TimeSpan.FromSeconds(1);
                            realString += string.Format("[{0}]", tmptime.ToString(@"hh\:mm\:ss\.fff"));
                        }
                        LineKaraoke tmp = new LineKaraoke();
                        tmp.BeginTime = beginTime.TotalSeconds;
                        tmp.EndTime   = tmptime.TotalSeconds;
                        tmp.LyricLRC  = realString;
                        allLyricByLine.Add(tmp);
                        Console.WriteLine(realString);
                        state      = 0;
                        beginTime  = TimeSpan.FromSeconds(0);
                        tmptime    = TimeSpan.FromSeconds(0);
                        realString = "";
                        timestr    = "";
                    }
                }
                if (state == 0)
                {
                    if (input[i] == '[')
                    {
                        state = 1;
                    }
                    else
                    {
                        realString += input[i];
                    }
                }
                else
                {
                    if (input[i] == ']')
                    {
                        state = 0;
                        if (tmptime != TimeSpan.FromSeconds(0))
                        {
                            try
                            {
                                tmptime = TimeSpan.ParseExact(timestr, "c", null);;
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                                return(false);
                            }
                        }
                        else
                        {
                            try
                            {
                                tmptime   = TimeSpan.ParseExact(timestr, "c", null);
                                beginTime = tmptime;
                            }
                            catch (Exception ex)
                            {
                                Debug.WriteLine(ex.Message);
                                return(false);
                            }
                        }
                        timestr = "";
                    }
                    else
                    {
                        timestr += input[i];
                    }
                }
            }
            return(true);
        }
Example #2
0
 public bool readKaraokeLyric(string input)
 {
     string realString = "";
     string timestr = "";
     TimeSpan beginTime = TimeSpan.FromSeconds(0);
     TimeSpan tmptime = TimeSpan.FromSeconds(0);
     int state = 0; //0:read string, 1: read time
     int lastLinePos = 0;
     if (input[input.Length - 1] != '\n')
     {
         input += "\n";
     }
     for (int i = 0; i < input.Length; i++)
     {
         if (input[i] == '\n')
         {
             //split line
             realString = input.Substring(lastLinePos, i - lastLinePos).Trim();
             lastLinePos = i + 1;
             if (realString != "")
             {
                 if (i > 0 && input[i - 1] != ']')
                 {
                     tmptime += TimeSpan.FromSeconds(1);
                     realString += string.Format("[{0}]", tmptime.ToString(@"hh\:mm\:ss\.fff"));
                 }
                 LineKaraoke tmp = new LineKaraoke();
                 tmp.BeginTime = beginTime.TotalSeconds;
                 tmp.EndTime = tmptime.TotalSeconds;
                 tmp.LyricLRC = realString;
                 allLyricByLine.Add(tmp);
                 Console.WriteLine(realString);
                 state = 0;
                 beginTime = TimeSpan.FromSeconds(0);
                 tmptime = TimeSpan.FromSeconds(0);
                 realString = "";
                 timestr = "";
             }
         }
         if (state == 0)
         {
             if (input[i] == '[')
             {
                 state = 1;
             }
             else
             {
                 realString += input[i];
             }
         }
         else
         {
             if (input[i] == ']')
             {
                 state = 0;
                 if (tmptime != TimeSpan.FromSeconds(0))
                 {
                     try
                     {
                         tmptime = TimeSpan.ParseExact(timestr, "c", null); ;
                     }
                     catch (Exception ex)
                     {
                         Debug.WriteLine(ex.Message);
                         return false;
                     }
                 }
                 else
                 {
                     try
                     {
                         tmptime = TimeSpan.ParseExact(timestr, "c", null);
                         beginTime = tmptime;
                     }
                     catch (Exception ex)
                     {
                         Debug.WriteLine(ex.Message);
                         return false;
                     }
                 }
                 timestr = "";
             }
             else
             {
                 timestr += input[i];
             }
         }
     }
     return true;
 }