-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b41f2e1
commit 3d46f07
Showing
12 changed files
with
226 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
interface LRCTag {} | ||
interface LRCLine { | ||
time: string; | ||
timeInMs: number; | ||
duration: number; | ||
text: string; | ||
} | ||
|
||
interface LRC { | ||
tags: LRCTag[]; | ||
lines: LRCLine[]; | ||
} | ||
|
||
const tagRegex = /^\[(?<tag>\w+):\s*(?<value>.+?)\s*\]$/; | ||
// prettier-ignore | ||
const lyricRegex = /^\[(?<minutes>\d+):(?<seconds>\d+)\.(?<milliseconds>\d+)\](?<text>.+)$/; | ||
|
||
export const LRC = { | ||
parse: (text: string): LRC => { | ||
let offset = 0; | ||
const lrc: LRC = { | ||
tags: [], | ||
lines: [], | ||
}; | ||
|
||
let previousLine: LRCLine | null = null; | ||
for (const line of text.split('\n')) { | ||
if (!line.trim().startsWith('[')) continue; | ||
|
||
const lyric = line.match(lyricRegex)?.groups; | ||
if (!lyric) { | ||
const tag = line.match(tagRegex)?.groups; | ||
if (tag) { | ||
if (tag.tag === 'offset') { | ||
offset = parseInt(tag.value); | ||
continue; | ||
} | ||
|
||
lrc.tags.push({ | ||
tag: tag.tag, | ||
value: tag.value, | ||
}); | ||
} | ||
continue; | ||
} | ||
|
||
const { minutes, seconds, milliseconds, text } = lyric; | ||
const timeInMs = | ||
parseInt(minutes) * 60 * 1000 + | ||
parseInt(seconds) * 1000 + | ||
parseInt(milliseconds); | ||
|
||
const currentLine: LRCLine = { | ||
time: `${minutes}:${seconds}:${milliseconds}`, | ||
timeInMs, | ||
text, | ||
duration: Infinity, | ||
}; | ||
if (previousLine) { | ||
previousLine.duration = timeInMs - previousLine.timeInMs; | ||
} | ||
|
||
previousLine = currentLine; | ||
lrc.lines.push(currentLine); | ||
} | ||
|
||
const first = lrc.lines.at(0); | ||
if (first && first.timeInMs > 300) { | ||
lrc.lines.unshift({ | ||
time: '0:0:0', | ||
timeInMs: 0, | ||
duration: first.timeInMs, | ||
text: '', | ||
}); | ||
} | ||
|
||
for (const line of lrc.lines) { | ||
line.timeInMs += offset; | ||
} | ||
|
||
return lrc; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.