Files
zarzet db27be3986 feat(lyrics): write translation tags as readable text
Saved translations and pronunciations were base64, so a .mlrc file or
a song's lyrics tag could not be read or fixed by hand. They are now
written as plain text under new tag names, still in ID tag form so
other players skip them:

  [x-romanized:12340:bokura wa]
  [x-translated-words:12340:<00:12.34>We <00:12.60>are<00:13.00>]
  [x-translated:12340:We are]

A value runs to the row's last ']', so only a line break (\n) and a
backslash (\\) are escaped, plus '<' (\<) inside word timing. Word
timing is enhanced LRC; a time with no word after it ends the word
before it, so gaps between words survive.

The old x-romaji and x-translation base64 tags are still read by both
parsers and are rewritten in the new form the next time the lyrics
are saved. Damaged old tags are dropped on that save, as the player
already ignored them. .mlrc files now start with [x-mlrc:2]; earlier
app versions read the header as a plain tag and show the lyrics, but
not the new translations.

The Rust download writer uses the same form. New shared parser
fixtures and usability rows keep the Dart and Rust readers in step.
2026-11-23 00:04:04 +07:00

388 lines
12 KiB
Dart

import 'package:mornye/utils/lyric_supplement_tags.dart';
import 'package:mornye/utils/lyrics_parser.dart' show LyricWord;
/// One line of a [LyricsDraft]: its time, its text, and the raw LRC that
/// rides along with it (word timing, a singer prefix, translations and
/// backing vocals) so saving an edit keeps what the editor does not show.
class LyricsDraftLine {
const LyricsDraftLine({
required this.time,
required this.text,
this.voice = '',
this.content,
this.contentTime,
this.supplements = const [],
this.background = const [],
});
/// An empty line to type into.
const LyricsDraftLine.blank()
: time = null,
text = '',
voice = '',
content = null,
contentTime = null,
supplements = const [],
background = const [];
/// When the line is sung, or null while it has no time yet.
final Duration? time;
final String text;
/// The singer prefix, such as `v2:`, written before the text.
final String voice;
/// The original LRC after the timestamp, with enhanced word timing, valid
/// while [text] is unchanged.
final String? content;
/// The line time [content]'s word timing was written for.
final Duration? contentTime;
/// Translation and pronunciation tags keyed to this line, as (kind,
/// payload) in the form [lyricSupplementRow] writes.
final List<(String, String)> supplements;
/// `[bg:...]` backing vocals sung with this line, as the raw content.
final List<String> background;
LyricsDraftLine copyWith({
Duration? time,
bool clearTime = false,
String? text,
}) => LyricsDraftLine(
time: clearTime ? null : time ?? this.time,
text: text ?? this.text,
voice: voice,
content: content,
contentTime: contentTime,
supplements: supplements,
background: background,
);
}
/// Editable lyrics read from LRC or plain text, written back as LRC.
///
/// Times are where each line is sung: an `[offset:]` tag is applied on
/// reading and dropped on writing. Other tags, word timing, translations
/// and backing vocals are kept; word timing moves with its line and is
/// dropped from a line whose text changes.
class LyricsDraft {
const LyricsDraft({
required this.lines,
this.header = const [],
this.footer = const [],
});
/// Tag lines such as `[ti:...]`, written first and unchanged.
final List<String> header;
final List<LyricsDraftLine> lines;
/// A trailing "Written by:" credit, written last and unchanged.
final List<String> footer;
static final _lineTime = RegExp(r'\[(\d{1,3}):(\d{1,2})(?:[.:](\d{1,3}))?\]');
static final _wordTime = RegExp(r'<(\d{1,3}):(\d{1,2})(?:[.:](\d{1,3}))?>');
static final _voice = RegExp(
r'^((?:<\d{1,3}:\d{1,2}(?:[.:]\d{1,3})?>\s*)*)(v[1-9]\d*:[ \t]*)',
caseSensitive: false,
);
static final _idTag = RegExp(
r'^\[(ti|ar|al|by|offset|length|re|ve|tool|au|la|encoder|instrumental|x-[a-z0-9_-]+):.*\]$',
caseSensitive: false,
);
static final _background = RegExp(r'^\[bg:(.*)\]$', caseSensitive: false);
static final _credit = RegExp(r'^Written\s+by\s*:', caseSensitive: false);
/// Whether [raw] is lyrics the editor can read: LRC or plain text, not
/// TTML.
static bool canEdit(String raw) {
// The player's TTML check.
final head = raw.trimLeft();
return !(head.startsWith('<?xml') ||
head.startsWith('<tt') ||
head.contains('<tt ') ||
head.contains('http://www.w3.org/ns/ttml'));
}
factory LyricsDraft.parse(String raw) {
final header = <String>[];
final footer = <String>[];
final lines = <LyricsDraftLine>[];
final supplements = <int, List<(String, String)>>{};
var offsetMs = 0;
for (final source in raw.split(RegExp(r'\r\n|\r|\n'))) {
final row = source.trim();
if (row.isEmpty) continue;
final background = _background.firstMatch(row);
if (background != null) {
if (lines.isEmpty) {
header.add(row);
} else {
final lead = lines.removeLast();
lines.add(
LyricsDraftLine(
time: lead.time,
text: lead.text,
voice: lead.voice,
content: lead.content,
contentTime: lead.contentTime,
background: [...lead.background, background.group(1)!.trim()],
),
);
}
continue;
}
final supplement = readLyricSupplementTag(row);
if (supplement != null) {
// Rewritten in the current form; damaged tags are dropped.
final (:kind, key: ms, :text, :words) = supplement;
final payload = words.isNotEmpty
? encodeLyricWordsTag(words)
: text != null && text.isNotEmpty
? encodeLyricTextTag(text)
: '';
if (ms != null && payload.isNotEmpty) {
(supplements[ms] ??= []).add((kind, payload));
}
continue;
}
final tag = _idTag.firstMatch(row);
if (tag != null) {
if (tag.group(1)!.toLowerCase() == 'offset') {
offsetMs =
int.tryParse(
row.substring(row.indexOf(':') + 1, row.length - 1).trim(),
) ??
0;
} else {
header.add(row);
}
continue;
}
final stamps = <Duration>[];
var rest = row;
while (true) {
final match = _lineTime.matchAsPrefix(rest);
if (match == null) break;
stamps.add(_duration(match));
rest = rest.substring(match.end);
}
// A timestamp later in the row is part of its text, as in the player.
rest = rest.trim();
final voiceMatch = _voice.firstMatch(rest);
final voice = voiceMatch?.group(2)?.trim() ?? '';
final body = voiceMatch == null
? rest
: rest.replaceRange(0, voiceMatch.end, voiceMatch.group(1)!);
final text = body.replaceAll(_wordTime, '').trim();
final hasWords = _wordTime.hasMatch(body);
if (stamps.isEmpty) {
lines.add(
LyricsDraftLine(
time: null,
text: text,
voice: voice,
content: hasWords ? body : null,
),
);
continue;
}
for (final stamp in stamps) {
lines.add(
LyricsDraftLine(
time: stamp,
text: text,
voice: voice,
content: hasWords ? body : null,
contentTime: stamp,
),
);
}
}
// Supplements are keyed to the written line time, before the offset.
// Older files mix centisecond lines with millisecond keys, so match the
// nearest line within 10ms as the player does.
final matched = List.generate(lines.length, (_) => <(String, String)>[]);
for (final entry in supplements.entries) {
int? nearest;
var distance = 11;
for (var i = 0; i < lines.length; i++) {
final time = lines[i].time;
if (time == null) continue;
final delta = (time.inMilliseconds - entry.key).abs();
if (delta < distance) {
nearest = i;
distance = delta;
}
}
if (nearest != null) matched[nearest].addAll(entry.value);
}
Duration? shifted(Duration? time) {
if (time == null || offsetMs == 0) return time;
final ms = time.inMilliseconds - offsetMs;
return Duration(milliseconds: ms < 0 ? 0 : ms);
}
final drafted = [
for (final (index, line) in lines.indexed)
LyricsDraftLine(
time: shifted(line.time),
text: line.text,
voice: line.voice,
content: line.content,
contentTime: shifted(line.contentTime),
supplements: List.unmodifiable(matched[index]),
background: List.unmodifiable(line.background),
),
];
// The player drops a final "Written by:" line from the lyrics and shows
// it as the credit; keep it out of the lines to time.
if (drafted.isNotEmpty &&
drafted.last.time == null &&
_credit.hasMatch(drafted.last.text)) {
footer.add(drafted.removeLast().text);
}
// Word timing written against the file's offset moves with its line.
final lead = offsetMs == 0
? drafted
: [
for (final line in drafted)
LyricsDraftLine(
time: line.time,
text: line.text,
voice: line.voice,
content: line.content == null
? null
: _shiftWords(line.content!, -offsetMs),
contentTime: line.contentTime,
supplements: _shiftSupplements(line.supplements, -offsetMs),
background: [
for (final row in line.background)
_shiftWords(row, -offsetMs),
],
),
];
return LyricsDraft(
header: List.unmodifiable(header),
lines: List.unmodifiable(lead),
footer: List.unmodifiable(footer),
);
}
/// Whether any line has a time, so the lyrics play in sync.
bool get synced => lines.any((line) => line.time != null);
/// The draft as LRC, or as plain text while no line has a time.
String toLrc() {
final out = <String>[];
final synced = this.synced;
if (synced) out.addAll(header);
for (final line in lines) {
final time = line.time;
final delta = time != null && line.contentTime != null
? (time - line.contentTime!).inMilliseconds
: 0;
final keepsWords =
line.content != null &&
time != null &&
line.contentTime != null &&
line.content!.replaceAll(_wordTime, '').trim() == line.text.trim();
final body = keepsWords
? _shiftWords(line.content!, delta)
: line.text.trim();
final voiced = line.voice.isEmpty
? body
: body.isEmpty
? line.voice
: '${line.voice} $body';
if (time == null) {
if (line.text.trim().isNotEmpty) out.add(voiced);
continue;
}
out.add('[${formatLyricsTime(time)}]$voiced');
final shifted = line.contentTime == null
? line.supplements
: _shiftSupplements(line.supplements, delta);
for (final (kind, payload) in shifted) {
out.add(lyricSupplementRow(kind, time.inMilliseconds, payload));
}
for (final row in line.background) {
out.add('[bg:${_shiftWords(row, delta)}]');
}
}
out.addAll(footer);
return out.join('\n');
}
static Duration _duration(Match match) {
final fraction = match.group(3) ?? '0';
final ms = switch (fraction.length) {
1 => int.parse(fraction) * 100,
2 => int.parse(fraction) * 10,
_ => int.parse(fraction),
};
return Duration(
minutes: int.parse(match.group(1)!),
seconds: int.parse(match.group(2)!),
milliseconds: ms,
);
}
static String _shiftWords(String content, int deltaMs) {
if (deltaMs == 0) return content;
return content.replaceAllMapped(_wordTime, (match) {
final ms = _duration(match).inMilliseconds + deltaMs;
return '<${formatLyricsTime(Duration(milliseconds: ms < 0 ? 0 : ms))}>';
});
}
static List<(String, String)> _shiftSupplements(
List<(String, String)> supplements,
int deltaMs,
) {
if (deltaMs == 0) return supplements;
return [
for (final (kind, payload) in supplements)
kind.endsWith('-words')
? (kind, _shiftSupplementWords(payload, deltaMs))
: (kind, payload),
];
}
/// Pronunciation and translation word timing is absolute; it moves with
/// its line.
static String _shiftSupplementWords(String payload, int deltaMs) {
Duration shift(Duration time) {
final ms = time.inMilliseconds + deltaMs;
return Duration(milliseconds: ms < 0 ? 0 : ms);
}
final shifted = encodeLyricWordsTag([
for (final word in decodeLyricWordsTag(payload))
LyricWord(
time: shift(word.time),
end: word.end == null ? null : shift(word.end!),
text: word.text,
),
]);
return shifted.isEmpty ? payload : shifted;
}
}
/// `mm:ss.xx` as LRC writes it, with milliseconds when centiseconds would
/// round them.
String formatLyricsTime(Duration time) {
final ms = time.inMilliseconds < 0 ? 0 : time.inMilliseconds;
final minutes = (ms ~/ 60000).toString().padLeft(2, '0');
final seconds = (ms ~/ 1000 % 60).toString().padLeft(2, '0');
final fraction = ms % 1000;
final tail = fraction % 10 == 0
? (fraction ~/ 10).toString().padLeft(2, '0')
: fraction.toString().padLeft(3, '0');
return '$minutes:$seconds.$tail';
}