public static async Task <List <string> > FetchLyrics(Mediafile file) { ILyricAPI[] Sources = new ILyricAPI[] { new NeteaseClient(), new XiamiClient(), new BaiduClient() }; try { var mediaFile = new Mediafile(); mediaFile.Title = TagParser.ParseTitle(file.Title); mediaFile.LeadArtist = TagParser.ParseArtists(TagParser.ParseTitle(file.LeadArtist))[0]; List <string> Lyrics = new List <string>(); for (int i = 0; i < Sources.Length; i++) { var lyrics = await Sources[i].FetchLyrics(mediaFile).ConfigureAwait(false); if (LrcParser.IsLrc(lyrics)) { Lyrics.Add(lyrics); break; } } return(Lyrics); } catch { return(null); } }
protected override void ParseStreamInto(LineBufferedReader stream, Beatmap output) { // Clear all hitobjects output.HitObjects.Clear(); var lyricText = stream.ReadToEnd(); var result = new LrcParser().Decode(lyricText); // Convert line for (int i = 0; i < result.Lines.Length; i++) { // Empty line should not be imported var line = result.Lines[i]; if (string.IsNullOrEmpty(line.Text)) { continue; } try { // todo : check list ls sorted by time. var lrcTimeTag = line.TimeTags; var timeTags = line.TimeTags.Where(x => x.Check).ToDictionary(k => { var index = (int)Math.Ceiling((double)(Array.IndexOf(lrcTimeTag, k) - 1) / 2); var state = (Array.IndexOf(lrcTimeTag, k) - 1) % 2 == 0 ? TextIndex.IndexState.Start : TextIndex.IndexState.End; return(new TextIndex(index, state)); }, v => (double)v.Time); var startTime = timeTags.FirstOrDefault(x => x.Value > 0).Value; var duration = timeTags.LastOrDefault(x => x.Value > 0).Value - startTime; var lyric = new Lyric { Order = output.HitObjects.Count + 1, // should create default order. Text = line.Text, // Start time and end time should be re-assigned StartTime = startTime, Duration = duration, TimeTags = TimeTagsUtils.ToTimeTagList(timeTags), RubyTags = result.QueryRubies(line.Text).Select(ruby => new RubyTag { Text = ruby.Ruby.Ruby, StartIndex = ruby.StartIndex, EndIndex = ruby.EndIndex }).ToArray() }; lyric.InitialWorkingTime(); output.HitObjects.Add(lyric); } catch (Exception ex) { var message = $"Parsing lyric '{line.Text}' got error in line:{i}" + "Please check time tag should be ordered and not duplicated." + "Then re-import again."; throw new FormatException(message, ex); } } }
public static async Task <string> FetchLyrics(Mediafile file, string lyricSource) { var mediaFile = new Mediafile(); mediaFile.Title = TagParser.ParseTitle(file.Title.ToString()); if (mediaFile.Title == null) { return(null); } var cleanedArtist = TagParser.ParseTitle(file.LeadArtist.ToString()); List <string> parsedArtists = TagParser.ParseArtists(cleanedArtist); if (string.IsNullOrEmpty(cleanedArtist)) { cleanedArtist = file.Title; parsedArtists = TagParser.ParseArtists(cleanedArtist); parsedArtists.RemoveAt(0); } mediaFile.LeadArtist = !parsedArtists.Any() ? file.LeadArtist : TagParser.ParseTitle(parsedArtists[0]); if (mediaFile.LeadArtist == null || mediaFile.LeadArtist.Equals("Unknown Artist", System.StringComparison.CurrentCultureIgnoreCase)) { return(null); } string Lyrics = ""; switch (lyricSource) { case "Auto": for (int i = 0; i < Sources.Length; i++) { var lrc = await Sources[i].FetchLyrics(mediaFile).ConfigureAwait(false); if (LrcParser.IsLrc(lrc)) { Lyrics = lrc; break; } } break; case "Musixmatch": Lyrics = await Sources[0].FetchLyrics(mediaFile).ConfigureAwait(false); break; case "Netease": Lyrics = await Sources[1].FetchLyrics(mediaFile).ConfigureAwait(false); break; case "Baidu": Lyrics = await Sources[3].FetchLyrics(mediaFile).ConfigureAwait(false); break; case "Xiami": Lyrics = await Sources[2].FetchLyrics(mediaFile).ConfigureAwait(false); break; } return(Lyrics); }
public string Encode(Beatmap output) { var lyric = new Lyric { Lines = output.HitObjects.OfType <Objects.LyricLine>().Select(encodeLyric).ToArray(), }; var encodeResult = new LrcParser().Encode(lyric); return(encodeResult); }
public static async Task <string> FetchLyrics(Mediafile file, string lyricSource) { try { var mediaFile = new Mediafile(); mediaFile.Title = TagParser.ParseTitle(file.Title.ToString()); var cleanedArtist = TagParser.ParseTitle(file.LeadArtist.ToString()); List <string> parsedArtists = TagParser.ParseArtists(cleanedArtist); if (string.IsNullOrEmpty(cleanedArtist)) { cleanedArtist = file.Title; parsedArtists = TagParser.ParseArtists(cleanedArtist); parsedArtists.RemoveAt(0); } mediaFile.LeadArtist = !parsedArtists.Any() ? file.LeadArtist : TagParser.ParseTitle(parsedArtists[0]); string Lyrics = ""; switch (lyricSource) { case "Auto": for (int i = 0; i < Sources.Length; i++) { var lrc = await Sources[i].FetchLyrics(mediaFile).ConfigureAwait(false); if (LrcParser.IsLrc(lrc)) { Lyrics = lrc; break; } } break; case "Netease": Lyrics = await Sources[0].FetchLyrics(mediaFile).ConfigureAwait(false); break; case "Baidu": Lyrics = await Sources[2].FetchLyrics(mediaFile).ConfigureAwait(false); break; case "Xiami": Lyrics = await Sources[1].FetchLyrics(mediaFile).ConfigureAwait(false); break; } return(Lyrics); } catch { return(null); } }
public void TestDecodeEncodedBeatmap(string fileName) { using (var stream = TestResources.OpenLrcResource(fileName)) using (var sr = new StreamReader(stream)) { var text = sr.ReadToEnd(); // Parser song var parser = new LrcParser(); var song = parser.Decode(text); // Encode to lyric var newLyricText = parser.Encode(song); // Encode and decode result should be same IsLyricEqual(newLyricText, text); } }
protected static Lyric GenerateLyric(string lyric) { var parser = new LrcParser(); return(parser.Decode(lyric)); }