Skip to content

Commit c1b9eae

Browse files
committed
Support Yrc lyrics
1 parent 73192df commit c1b9eae

File tree

5 files changed

+321
-55
lines changed

5 files changed

+321
-55
lines changed

Sources/LyricsService/JSONModel/NetEase/NetEaseResponseSingleLyrics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct NetEaseResponseSingleLyrics: Decodable {
1414
let klyric: Lyric?
1515
let tlyric: Lyric?
1616
let lyricUser: User?
17-
17+
let yrc: Lyric?
1818
/*
1919
let sgc: Bool
2020
let sfy: Bool

Sources/LyricsService/Parser/NetEaseKLyricParser.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,47 @@ extension Lyrics {
5555

5656
self.init(lines: lines, idTags: idTags)
5757
}
58+
59+
convenience init?(netEaseYrcContent content: String) {
60+
var idTags: [IDTagKey: String] = [:]
61+
id3TagRegex.matches(in: content).forEach { match in
62+
if let key = match[1]?.content.trimmingCharacters(in: .whitespaces),
63+
let value = match[2]?.content.trimmingCharacters(in: .whitespaces),
64+
!key.isEmpty,
65+
!value.isEmpty {
66+
idTags[.init(key)] = value
67+
}
68+
}
69+
70+
let lines: [LyricsLine] = krcLineRegex.matches(in: content).map { match in
71+
let timeTagStr = match[1]!.content
72+
let timeTag = TimeInterval(timeTagStr)! / 1000
73+
74+
let durationStr = match[2]!.content
75+
let duration = TimeInterval(durationStr)! / 1000
76+
77+
var lineContent = ""
78+
var attachment = LyricsLine.Attachments.InlineTimeTag(tags: [.init(index: 0, time: 0)], duration: duration)
79+
var dt = 0.0
80+
netEaseYrcInlineTagRegex.matches(in: content, range: match[3]!.range).forEach { m in
81+
let t1 = Int(m[1]!.content)! - Int(timeTagStr)!
82+
let t2 = Int(m[2]!.content)!
83+
let t = TimeInterval(t1 + t2) / 1000
84+
let fragment = m[3]!.content
85+
let prevCount = lineContent.count
86+
lineContent += fragment
87+
if lineContent.count > prevCount {
88+
attachment.tags.append(.init(index: lineContent.count, time: t))
89+
}
90+
}
91+
92+
let att = LyricsLine.Attachments(attachments: [.timetag: attachment])
93+
return LyricsLine(content: lineContent, position: timeTag, attachments: att)
94+
}
95+
guard !lines.isEmpty else {
96+
return nil
97+
}
98+
99+
self.init(lines: lines, idTags: idTags)
100+
}
58101
}

0 commit comments

Comments
 (0)