geesawra.industries/hydraulophone
An unofficial YouTube Music app for iPhone, iPad, and Mac.
0
0
import Foundation
/// A lyrics source is optional because some tracks have no published lyrics.
public protocol LyricsProviding: Sendable {
func lyrics(for song: Song) async throws -> SongLyrics?
}
/// Source-provided word or syllable timing. Text retains its original spacing
/// so a line can render naturally without inventing word boundaries.
public struct LyricsWord: Equatable, Sendable {
public let text: String
public let startTime: TimeInterval
public let endTime: TimeInterval
public init(text: String, startTime: TimeInterval, endTime: TimeInterval) {
self.text = text
self.startTime = startTime
self.endTime = endTime
}
}
public struct LyricsLine: Equatable, Identifiable, Sendable {
public let id: Int
public let text: String
/// Position in the song, in seconds. Nil for manually scrolled lyrics.
public let startTime: TimeInterval?
public let endTime: TimeInterval?
public let words: [LyricsWord]
public init(
id: Int, text: String, startTime: TimeInterval? = nil, endTime: TimeInterval? = nil,
words: [LyricsWord] = []
) {
self.id = id
self.text = text
self.startTime = startTime
self.endTime = endTime
// A partial or invalid word timeline must not drop text from the line.
self.words =
words.map(\.text).joined() == text
&& words.allSatisfy {
$0.startTime.isFinite && $0.endTime.isFinite
&& $0.startTime >= 0 && $0.endTime > $0.startTime
} ? words : []
}
public func activeWordIndex(at position: TimeInterval) -> Int? {
guard position.isFinite, position >= 0 else { return nil }
return words.lastIndex { $0.startTime <= position && position < $0.endTime }
}
}
public struct SongLyrics: Equatable, Sendable {
public let lines: [LyricsLine]
public let source: String?
public let isSynchronized: Bool
public init(lines: [LyricsLine], source: String? = nil, isSynchronized: Bool? = nil) {
self.source = source
let hasCompleteTimeline =
!lines.isEmpty
&& lines.allSatisfy {
guard let start = $0.startTime else { return false }
guard start.isFinite && start >= 0 else { return false }
guard let end = $0.endTime else { return true }
return end.isFinite && end > start
}
let synchronized = (isSynchronized ?? hasCompleteTimeline) && hasCompleteTimeline
self.isSynchronized = synchronized
self.lines =
synchronized
? lines.enumerated().sorted {
let left = $0.element.startTime ?? 0
let right = $1.element.startTime ?? 0
return left == right ? $0.offset < $1.offset : left < right
}.map(\.element)
: lines
}
/// Returns the line whose cue contains the current playback position.
public func activeLineIndex(at position: TimeInterval) -> Int? {
guard isSynchronized, position.isFinite, position >= 0 else { return nil }
for index in lines.indices.reversed() {
guard let start = lines[index].startTime, start <= position else { continue }
let end = lines[index].endTime ?? (index + 1 < lines.count ? lines[index + 1].startTime : nil)
if let end, position >= end { continue }
return index
}
return nil
}
}