Skip to content

Commit

Permalink
Fix KaraTime parsing
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Wo <[email protected]>
  • Loading branch information
brainwo committed Aug 11, 2024
1 parent 9cf600c commit 74cb480
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
41 changes: 27 additions & 14 deletions lib/kara.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -124,7 +124,7 @@ Kara? parse(String raw) {
List<KaraTime> 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]`
Expand All @@ -142,7 +142,7 @@ Kara? parse(String raw) {
"Post-Bridge" => KaraSection.postBridge,
_ => KaraSection.header,
};

continue;
if (currentSection.isSongStructure && time.isNotEmpty) {}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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<bool>.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<bool>.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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/kara_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void main() {
parse(
File.fromUri(
Uri.file(
'test/example.kara',
'test/test_file/example.kara',
),
).readAsStringSync(),
).toString(),
Expand Down

0 comments on commit 74cb480

Please sign in to comment.