private void ProcessLyrics(string cmd, LyricsPhrase selectedLP) { var newLP = new LyricsPhrase() { Text = "(Lyrics Phrase)" }; switch (cmd) { case "Before": newLP.BeginTime = selectedLP.BeginTime; newLP.EndTime = selectedLP.BeginTime; this.SentencePhrases.Insert(this.list_LyricsSentence.SelectedIndex, newLP); break; case "Behind": newLP.BeginTime = selectedLP.EndTime; newLP.EndTime = selectedLP.EndTime; this.SentencePhrases.Insert(this.list_LyricsSentence.SelectedIndex + 1, newLP); break; case "Delete": this.SentencePhrases.RemoveAt(this.list_LyricsSentence.SelectedIndex); break; } }
private static int CompareListSmall(LyricsPhrase a, LyricsPhrase b)//由大到小 { double _temp = a.Begin - b.Begin; if (_temp < 0) { return(-1); } if (_temp > 0) { return(1); } return(0); }
public Lyrics(string lrcFilePath, bool sortByBeginTime) { this.NeedSort = sortByBeginTime; this.Phrases = new List <LyricsPhrase>(); this.InformationDict = new Dictionary <string, string>(); this.Name = Path.GetFileNameWithoutExtension(lrcFilePath); lines = File.ReadAllLines(lrcFilePath, this.Encoding); foreach (var line in lines) { Regex regex = new Regex("\\[[a-z]+?:.+?\\]"); var formated = HttpUtility.HtmlEncode(line.Trim()); Match match = regex.Match(formated); if (match.Success == true) { var array = line.Trim().Split(new char[] { '[', ']', ':' }, StringSplitOptions.RemoveEmptyEntries); if (array.Length == 2) { var key = array[0].Trim().ToLower(); if (key.Trim() == "offset") { if (array[1].Length > 0) { offset = double.Parse(array[1]) / 1000; } } else if (key.Length == 2 && key.ToLower() != key.ToUpper()) { if (InformationDict.ContainsKey(key) == false) { InformationDict.Add(key, array[1]); } } } } else { regex = new Regex("\\[\\d\\d:\\d\\d\\.\\d\\d\\]+", RegexOptions.Singleline); var matches = regex.Matches(line); var count = matches.Count; if (count > 0) { for (int i = 0; i < count; i++) { string time = matches[i].Value.Trim('[', ']').Trim(); var begin = stringToInterval(time) - this.offset; if (begin <= 0) { continue; } string text = line.Substring(count * 10); var phrase = new LyricsPhrase() { Text = text, Begin = begin }; this.Phrases.Add(phrase); } } } } if (this.NeedSort == true) { this.Phrases.Sort(CompareListSmall); } for (int i = 0; i < this.Phrases.Count; i++) { var phrase = this.Phrases[i]; phrase.Index = i; if (i < this.Phrases.Count - 1) { phrase.End = this.Phrases[i + 1].Begin; } } this.Phrases[this.Phrases.Count - 1].End = 60 * 60 * 4; File.WriteAllText(lrcFilePath, this.ToString(), this.Encoding); }