From 74cb4807ad4bef60e1ab13fb48224727a490df84 Mon Sep 17 00:00:00 2001 From: Brian Wo <45139213+brainwo@users.noreply.github.com> Date: Sun, 11 Aug 2024 22:25:45 +0800 Subject: [PATCH] Fix KaraTime parsing Signed-off-by: Brian Wo <45139213+brainwo@users.noreply.github.com> --- lib/kara.dart | 41 +++++++++++++++++++++++++++-------------- test/kara_test.dart | 2 +- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/lib/kara.dart b/lib/kara.dart index 885937c..cfa52fd 100644 --- a/lib/kara.dart +++ b/lib/kara.dart @@ -80,7 +80,7 @@ KaraTime? _parseTimestamp(String line) { return null; } - final lyricSplit = split.last.split("."); + final lyricSplit = split.last.trim().split(" "); late final Duration end; if (_parseDuration(lyricSplit.first) case Duration parsedDuration) { @@ -124,7 +124,7 @@ Kara? parse(String raw) { List time = []; for (final line in raw.split('\n').skip(1)) { - if (line.startsWith("#")) continue; + if (line.startsWith("#") || line.isEmpty) continue; // Section // e.g `[Singers]`, `[Intro]`, `[Chorus]` @@ -142,7 +142,7 @@ Kara? parse(String raw) { "Post-Bridge" => KaraSection.postBridge, _ => KaraSection.header, }; - + continue; if (currentSection.isSongStructure && time.isNotEmpty) {} } @@ -188,7 +188,10 @@ Kara? parse(String raw) { parsedTimestamp.start < currentStart) { currentStart = parsedTimestamp.start; currentEnd = parsedTimestamp.end; - continue; + } + + if (parsedTimestamp.end > currentEnd) { + currentEnd = parsedTimestamp.end; } time.add(parsedTimestamp); @@ -209,23 +212,33 @@ Kara? parse(String raw) { end: currentEnd, time: time, )); + currentLyric = null; + currentStart = null; + currentEnd = null; time = []; } final parsed = _parseKeyValue(line); if (parsed != null) { - final tempSingers = List.generate( - singers.length, - (index) => false, - growable: false, - ); - parsed.value - .split(",") - .map((e) => int.tryParse(e.trim())) - .forEach((element) { - // TODO: convert number to [true,true,false] + currentSingers = + parsed.value.split(",").map((e) => int.tryParse(e.trim())).fold( + List.generate( + singers.length, + (index) => false, + growable: false, + ), (singers, parsedSingerIndex) { + if (parsedSingerIndex == null) { + return singers; + } + singers?[parsedSingerIndex] = true; + return singers; }); } + if (currentLyric != null) { + // TODO: translations + continue; + } + currentLyric = line; } } diff --git a/test/kara_test.dart b/test/kara_test.dart index 727e5d4..83349aa 100644 --- a/test/kara_test.dart +++ b/test/kara_test.dart @@ -13,7 +13,7 @@ void main() { parse( File.fromUri( Uri.file( - 'test/example.kara', + 'test/test_file/example.kara', ), ).readAsStringSync(), ).toString(),