Accompanist released a group of artifacts, including:
-
lyrics-core- Parsing lyrics file, holding data and exporting to other formats. -
lyrics-ui- Standard lyrics interface built on Jetpack Compose
This repository hosts the lyrics-core code.
- π€ Smart Auto-Detection: Automatically detects and parses various lyrics formats out of the box.
- π€ Karaoke-Ready: Provides syllable-level timing for precise karaoke-style highlighting.
- π Translation Support: Natively handles dual-language or translated lyric lines.
- π§© Highly Extensible: Easily add support for new or custom formats.
- π·οΈ Metadata Extraction: Reads standard tags like artist, album, title, and offset.
- π Pure Kotlin/JVM: No Android dependencies, suitable for any Kotlin project.
- LRC: Standard and dual-language
.lrcfiles. - Enhanced LRC: Syllable-level timing, voice separation, and accompaniment tags.
- TTML (Apple Syllable): The format used by Apple Music.
- Lyricify Syllable: Custom format from the Lyricify App.
Add the dependency to your build.gradle.kts:
dependencies {
implementation("com.mocharealm.accompanist:lyrics-core:VERSION")
}Replace VERSION with the latest version from Maven Central.
For most use cases, AutoParser is the easiest way to parse lyrics without needing to know the format beforehand.
import com.mocharealm.accompanist.lyrics.core.parser.AutoParser
// 1. Get your lyrics content from a file or network
val lyricsContent: String = fetchLyrics()
// 2. Create a default AutoParser instance using its builder
val autoParser = AutoParser.Builder().build()
// 3. Parse the content
val lyrics = autoParser.parse(lyricsContent)
// Now you have a unified SyncedLyrics object!
println(lyrics.metadata.title)
println(lyrics.lines.first().text)If you know the exact format, you can use a specific parser directly.
import com.mocharealm.accompanist.lyrics.core.parser.LrcParser
val lrcLines = listOf(
"[00:39.96]I lean in and you move away",
"[00:39.96]ζι ε¨ιι’οΌδ½ 就离εΌ"
)
val lyrics = LrcParser.parse(lrcLines)
println(lyrics.lines)You can also use EnhancedLrcParser, TTMLParser, or LyricifySyllableParser.
Accompanist Lyrics is designed to be extensible. You can add support for any custom format by implementing the ILyricsParser interface and registering it with the AutoParser.
Create a class that implements the parsing logic for your custom format.
import com.mocharealm.accompanist.lyrics.core.model.SyncedLyrics
import com.mocharealm.accompanist.lyrics.core.parser.ILyricsParser
class MyCustomParser : ILyricsParser {
override fun parse(lines: List<String>): SyncedLyrics {
// Your parsing logic here...
}
override fun parse(content: String): SyncedLyrics {
// Your parsing logic here...
}
}Create a LyricsFormat that contains a detector function. This function returns true if the given content matches your custom format.
import com.mocharealm.accompanist.lyrics.core.utils.LyricsFormatGuesser
val myCustomFormat = LyricsFormatGuesser.LyricsFormat(
name = "MY_CUSTOM_FORMAT",
detector = { content ->
// Example: check for a unique tag
content.startsWith("##MY_COOL_LYRICS##")
}
)Use the withFormat method on the builder to register your new format and its parser. Custom formats are checked first, ensuring they are prioritized over built-in ones.
import com.mocharealm.accompanist.lyrics.core.parser.AutoParser
// Build an AutoParser instance with your custom format
val autoParser = AutoParser.Builder()
.withFormat(myCustomFormat, MyCustomParser())
.build()
// This parser now understands both built-in and your custom format!
val lyrics = autoParser.parse(myCustomLyricsContent)Join the community, ask questions, and share your projects!
- Telegram: mocha_pot
- GitHub Issues: Create an Issue
Contributions are welcome! Please feel free to submit a pull request or open an issue to discuss your ideas. For major changes, please open an issue first.
This project is licensed under the Apache License 2.0. See the LICENSE file for details.