Overview
TheLyricsManager class handles synchronized lyrics display over the cover art with timestamp-based synchronization. It supports both real-time audio playback and virtual time-based synchronization for streaming scenarios.
Features
- Adaptive streaming delay for iOS/Safari and Desktop/Android devices
- Virtual time synchronization for live streams
- LRC format parsing support
- Automatic visibility handling on tab changes
- Smooth fade animations
Constructor
Properties
Array of lyric objects with
time and text properties, sorted by timeIndex of the currently displayed lyric line
Reference to the audio element for real-time synchronization
Whether the lyrics system is currently active
Initial time offset in seconds for the song (elapsed time when loaded)
Whether to use virtual time based on Azura timestamp instead of audio element time
Custom delay in seconds (null = use LYRICS_CONFIG.STREAM_DELAY)
Methods
init()
Initializes the lyrics manager with an audio element reference.The HTML5 audio element to synchronize with
loadLyrics()
Loads lyrics from an array and starts synchronization.Array of objects with format:
[{time: seconds, text: "lyric text"}]Initial elapsed time in seconds (used for live streams)
Custom delay in seconds to compensate for stream latency (null = use default adaptive delay)
- Automatically sorts lyrics by timestamp
- Shows the overlay if lyrics are present
- Enables virtual time mode if
startOffset > 0 - Starts update interval for virtual time synchronization
loadFromLRC()
Parses and loads lyrics from LRC format text.Lyrics in LRC format with timestamps like
[mm:ss.xx] or [mm:ss][mm:ss]- Minutes and seconds[mm:ss.xx]- Minutes, seconds, and centiseconds[mm:ss.xxx]- Minutes, seconds, and milliseconds
getCurrentTime()
Calculates the current playback time, accounting for virtual time or audio element time.Number - Current time in seconds
Behavior:
- In virtual time mode: Returns
timeOffset + elapsed - streamDelay - In audio mode: Returns
audioElement.currentTime - Uses
customDelayif set, otherwise usesLYRICS_CONFIG.STREAM_DELAY
updateLyrics()
Updates the displayed lyrics based on current playback time.- Audio
timeupdateevent (non-virtual mode) - Update interval (virtual mode, every 100ms)
- Finds the appropriate lyric line for current time
- Updates display only when line changes
- Calls
displayLyric()with new index
displayLyric()
Displays the lyric at the specified index with animation.Index of the lyric line to display
- Updates current lyric with fade-in animation
- Shows previous lyric with reduced opacity
- Logs displayed lyric in development mode
show()
Shows the lyrics overlay with smooth fade-in animation.- Sets display to flex
- Adds
activeclass after 50ms for CSS transition - Sets
isActiveto true
hide()
Hides the lyrics overlay with smooth fade-out animation.- Removes
activeclass for fade-out - Sets display to none after 1200ms (transition duration)
- Only deactivates if no lyrics are loaded
clear()
Clears current lyrics and resets state.If true, keeps the system active for quick reload; if false, fully deactivates
- Clears lyrics array and resets indices
- Stops virtual time updates
- Removes custom delay setting
- Optionally hides overlay completely
startVirtualTimeUpdate()
Starts the interval-based update loop for virtual time mode.- Clears any existing interval
- Creates new interval at
LYRICS_CONFIG.UPDATE_INTERVAL(100ms) - Updates only when active, has lyrics, in virtual mode, and tab is visible
stopVirtualTimeUpdate()
Stops the virtual time update interval.Static Methods
getDemoLyrics()
Returns sample lyrics for testing and demonstration.Array - Array of 10 sample lyric objects
Example:
Configuration
The module uses an adaptive configuration based on device detection:Device-Specific Delays
Device-Specific Delays
- iOS (iPhone/iPad): 4.5 seconds delay due to Safari’s aggressive buffering
- Desktop/Android: 1.5 seconds delay for standard streaming latency
- Custom Delay: Can be overridden using the
customDelayparameter inloadLyrics()
Usage Example
DOM Requirements
The LyricsManager expects these elements in the DOM:#lyricsOverlay- Main overlay container#lyricsBackdrop- Backdrop element for visual effects#lyricsPrevious- Container for previous lyric line#lyricsCurrent- Container for current lyric line
Event Handling
Visibility Change
Visibility Change
When the tab becomes visible again after being hidden:
- Reconnects the lyrics system
- Restarts virtual time updates if needed
- Updates lyrics to current position
- Shows the overlay if lyrics exist
