Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a2 lyrics #268

Draft
wants to merge 3 commits into
base: beta
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,22 @@ object LrcUtils {
if (lrcContent.isBlank()) return null

val timeMarksRegex = "\\[(\\d{2}:\\d{2})([.:]\\d+)?]".toRegex()
val wordTimeMarksRegex = "<(\\d{2}:\\d{2})([.:]\\d+)?>".toRegex()
val labelRegex = "(v\\d+|bg):\\s?".toRegex()
val lyricsList = mutableListOf<MediaStoreUtils.Lyric>()
var foundNonNull = false
var lyricsText: StringBuilder? = StringBuilder()
var currentLabel = ""

lrcContent.lines().forEach { line ->
val matches = timeMarksRegex.findAll(line).toList()
if (matches.isEmpty()) return@forEach

val lyricContent = line.substring(matches.last().range.last + 1).let { if (trim) it.trim() else it }
currentLabel = labelRegex.find(line)?.value ?: currentLabel

val lyricContent = line.substring(matches.last().range.last + 1)
.let { if (trim) it.trim() else it }
.replace(labelRegex, "")

matches.forEach { match ->
val timeString = match.groupValues[1] + match.groupValues[2]
Expand All @@ -123,7 +130,35 @@ object LrcUtils {
}

lyricsText?.append("$lyricLine\n")
lyricsList.add(MediaStoreUtils.Lyric(timestamp, lyricLine))

if (wordTimeMarksRegex.containsMatchIn(lyricLine)) {
val wordMatches = wordTimeMarksRegex.findAll(lyricLine)
val words = lyricLine.split(wordTimeMarksRegex)
val wordTimestamps = words.mapIndexedNotNull { index, word ->
wordMatches.elementAtOrNull(index)?.let { match ->
val wordTimestamp =
parseTime(match.groupValues[1] + match.groupValues[2])
Pair(words.take(index).sumOf { it.length }, wordTimestamp)
}
}
lyricsList.add(
MediaStoreUtils.Lyric(
timestamp,
lyricLine.replace(wordTimeMarksRegex, ""),
false,
wordTimestamps,
currentLabel
)
)
} else {
lyricsList.add(
MediaStoreUtils.Lyric(
timestamp,
lyricLine,
label = currentLabel
)
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ object MediaStoreUtils {
data class Lyric(
val timeStamp: Long? = null,
val content: String = "",
var isTranslation: Boolean = false
var isTranslation: Boolean = false,
val wordTimestamps: List<Pair<Int, Long>> = emptyList(),
val label: String = ""
) : Parcelable

/**
Expand Down
94 changes: 94 additions & 0 deletions app/src/test/kotlin/org/akanework/gramophone/LrcUtilsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Ignore
import org.junit.Test

Expand Down Expand Up @@ -50,6 +51,99 @@ class LrcUtilsTest {
return str.toString()
}

@Test
fun testA2Format() {
val a2Lrc = "[00:12.00]This is <00:12.10>a <00:12.20>test <00:12.30>of <00:12.40>A2 <00:12.50>format"
val parsed = parse(a2Lrc)
assertNotNull(parsed)
assertEquals(1, parsed?.size)
val lyric = parsed?.get(0)
assertEquals(12000L, lyric?.timeStamp)
assertEquals("This is a test of A2 format", lyric?.content)
assertEquals(
listOf(
Pair(0, 12100L),
Pair(8, 12200L),
Pair(10, 12300L),
Pair(15, 12400L),
Pair(18, 12500L)
),
lyric?.wordTimestamps
)
}

@Test
fun testA2FormatMixedWithStandard() {
val mixedLrc = """
[00:10.00]This is a standard line
[00:12.00]This <00:12.10>is <00:12.20>an <00:12.30>A2 <00:12.40>line
[00:14.00]Back to standard
""".trimIndent()
val parsed = parse(mixedLrc)
assertNotNull(parsed)
assertEquals(3, parsed?.size)

assertEquals(10000L, parsed?.get(0)?.timeStamp)
assertEquals("This is a standard line", parsed?.get(0)?.content)
assertTrue(parsed?.get(0)?.wordTimestamps?.isEmpty() ?: false)

assertEquals(12000L, parsed?.get(1)?.timeStamp)
assertEquals("This is an A2 line", parsed?.get(1)?.content)
assertEquals(
listOf(
Pair(0, 12100L),
Pair(5, 12200L),
Pair(8, 12300L),
Pair(11, 12400L)
),
parsed?.get(1)?.wordTimestamps
)

assertEquals(14000L, parsed?.get(2)?.timeStamp)
assertEquals("Back to standard", parsed?.get(2)?.content)
assertTrue(parsed?.get(2)?.wordTimestamps?.isEmpty() ?: false)
}

@Test
fun testA2FormatWithPartialWords() {
val partialA2Lrc = "[00:12.00]This is <00:12.10>a test <00:12.30>of A2 <00:12.50>format"
val parsed = parse(partialA2Lrc)
assertNotNull(parsed)
assertEquals(1, parsed?.size)
val lyric = parsed?.get(0)
assertEquals(12000L, lyric?.timeStamp)
assertEquals("This is a test of A2 format", lyric?.content)
assertEquals(
listOf(
Pair(0, 12100L),
Pair(8, 12300L),
Pair(15, 12500L)
),
lyric?.wordTimestamps
)
}

@Test
fun testA2FormatWithEmptyWords() {
val emptyWordsA2Lrc = "[00:12.00]<00:12.10><00:12.20>Test<00:12.30><00:12.40>A2<00:12.50>"
val parsed = parse(emptyWordsA2Lrc)
assertNotNull(parsed)
assertEquals(1, parsed?.size)
val lyric = parsed?.get(0)
assertEquals(12000L, lyric?.timeStamp)
assertEquals("TestA2", lyric?.content)
assertEquals(
listOf(
Pair(0, 12100L),
Pair(0, 12200L),
Pair(0, 12300L),
Pair(4, 12400L),
Pair(4, 12500L)
),
lyric?.wordTimestamps
)
}

@Test
fun emptyInEmptyOut() {
val emptyLrc = parse("")
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
val agpVersion = "8.6.0-rc01"
val agpVersion = "8.5.0"
id("com.android.application") version agpVersion apply false
id("com.android.library") version agpVersion apply false
id("com.android.test") version agpVersion apply false
Expand Down