Skip to content

Commit 74cb480

Browse files
committed
Fix KaraTime parsing
Signed-off-by: Brian Wo <45139213+brainwo@users.noreply.github.com>
1 parent 9cf600c commit 74cb480

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

lib/kara.dart

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ KaraTime? _parseTimestamp(String line) {
8080
return null;
8181
}
8282

83-
final lyricSplit = split.last.split(".");
83+
final lyricSplit = split.last.trim().split(" ");
8484

8585
late final Duration end;
8686
if (_parseDuration(lyricSplit.first) case Duration parsedDuration) {
@@ -124,7 +124,7 @@ Kara? parse(String raw) {
124124
List<KaraTime> time = [];
125125

126126
for (final line in raw.split('\n').skip(1)) {
127-
if (line.startsWith("#")) continue;
127+
if (line.startsWith("#") || line.isEmpty) continue;
128128

129129
// Section
130130
// e.g `[Singers]`, `[Intro]`, `[Chorus]`
@@ -142,7 +142,7 @@ Kara? parse(String raw) {
142142
"Post-Bridge" => KaraSection.postBridge,
143143
_ => KaraSection.header,
144144
};
145-
145+
continue;
146146
if (currentSection.isSongStructure && time.isNotEmpty) {}
147147
}
148148

@@ -188,7 +188,10 @@ Kara? parse(String raw) {
188188
parsedTimestamp.start < currentStart) {
189189
currentStart = parsedTimestamp.start;
190190
currentEnd = parsedTimestamp.end;
191-
continue;
191+
}
192+
193+
if (parsedTimestamp.end > currentEnd) {
194+
currentEnd = parsedTimestamp.end;
192195
}
193196

194197
time.add(parsedTimestamp);
@@ -209,23 +212,33 @@ Kara? parse(String raw) {
209212
end: currentEnd,
210213
time: time,
211214
));
215+
currentLyric = null;
216+
currentStart = null;
217+
currentEnd = null;
212218
time = [];
213219
}
214220

215221
final parsed = _parseKeyValue(line);
216222
if (parsed != null) {
217-
final tempSingers = List<bool>.generate(
218-
singers.length,
219-
(index) => false,
220-
growable: false,
221-
);
222-
parsed.value
223-
.split(",")
224-
.map((e) => int.tryParse(e.trim()))
225-
.forEach((element) {
226-
// TODO: convert number to [true,true,false]
223+
currentSingers =
224+
parsed.value.split(",").map((e) => int.tryParse(e.trim())).fold(
225+
List<bool>.generate(
226+
singers.length,
227+
(index) => false,
228+
growable: false,
229+
), (singers, parsedSingerIndex) {
230+
if (parsedSingerIndex == null) {
231+
return singers;
232+
}
233+
singers?[parsedSingerIndex] = true;
234+
return singers;
227235
});
228236
}
237+
if (currentLyric != null) {
238+
// TODO: translations
239+
continue;
240+
}
241+
currentLyric = line;
229242
}
230243
}
231244

test/kara_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void main() {
1313
parse(
1414
File.fromUri(
1515
Uri.file(
16-
'test/example.kara',
16+
'test/test_file/example.kara',
1717
),
1818
).readAsStringSync(),
1919
).toString(),

0 commit comments

Comments
 (0)