Skip to content

Commit

Permalink
Lyric: Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lightsummer233 committed Dec 13, 2024
1 parent abe8351 commit 43a440c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 99 deletions.
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

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.

61 changes: 45 additions & 16 deletions app/src/main/java/org/akanework/gramophone/logic/utils/LrcUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ object LrcUtils {
}

@OptIn(UnstableApi::class)
fun extractAndParseLyrics(metadata: Metadata, trim: Boolean): MutableList<MediaStoreUtils.Lyric>? {
for (i in 0..< metadata.length()) {
fun extractAndParseLyrics(
metadata: Metadata,
trim: Boolean
): MutableList<MediaStoreUtils.Lyric>? {
for (i in 0..<metadata.length()) {
val meta = metadata.get(i)
val data =
if (meta is VorbisComment && meta.key == "LYRICS") // ogg / flac
Expand Down Expand Up @@ -134,15 +137,23 @@ object LrcUtils {
var lastWordTimestamp = currentTimeStamp
val wordTimestamps = words.mapIndexedNotNull { index, _ ->
wordMatches.elementAtOrNull(index)?.let { match ->
val wordTimestamp = parseTime(match.groupValues[1] + match.groupValues[2])
Triple(words.take(index + 1).sumOf { it.length }, lastWordTimestamp, wordTimestamp).also {
val wordTimestamp =
parseTime(match.groupValues[1] + match.groupValues[2])
Triple(
words.take(index + 1).sumOf { it.length },
lastWordTimestamp,
wordTimestamp
).also {
lastWordTimestamp = wordTimestamp
}
}
}.toMutableList().apply {
// Remove word timestamps whose content is empty
removeIf { it.first == 0 }
}
list.add(
MediaStoreUtils.Lyric(
timeStamp = currentTimeStamp,
startTimestamp = currentTimeStamp,
content = lyricLine.replace(wordTimeMarksRegex, ""),
wordTimestamps = wordTimestamps,
label = currentLabel
Expand All @@ -151,7 +162,7 @@ object LrcUtils {
} else {
list.add(
MediaStoreUtils.Lyric(
timeStamp = currentTimeStamp,
startTimestamp = currentTimeStamp,
content = lyricLine,
label = currentLabel
)
Expand All @@ -173,15 +184,23 @@ object LrcUtils {
var lastWordTimestamp = currentTimeStamp
val wordTimestamps = words.mapIndexedNotNull { index, _ ->
wordMatches.elementAtOrNull(index)?.let { match ->
val wordTimestamp = parseTime(match.groupValues[1] + match.groupValues[2])
Triple(words.take(index + 1).sumOf { it.length }, lastWordTimestamp, wordTimestamp).also {
val wordTimestamp =
parseTime(match.groupValues[1] + match.groupValues[2])
Triple(
words.take(index + 1).sumOf { it.length },
lastWordTimestamp,
wordTimestamp
).also {
lastWordTimestamp = wordTimestamp
}
}
}.toMutableList().apply {
// Remove word timestamps whose content is empty
removeIf { it.first == 0 }
}
list.add(
MediaStoreUtils.Lyric(
timeStamp = currentTimeStamp + 1,
startTimestamp = currentTimeStamp + 1,
content = lyricLine.replace(wordTimeMarksRegex, ""),
wordTimestamps = wordTimestamps,
label = currentLabel
Expand All @@ -190,7 +209,7 @@ object LrcUtils {
} else {
list.add(
MediaStoreUtils.Lyric(
timeStamp = currentTimeStamp,
startTimestamp = currentTimeStamp,
content = lyricLine,
label = currentLabel
)
Expand All @@ -201,35 +220,45 @@ object LrcUtils {
}

// Sort and mark as translations all found duplicated timestamps (usually one)
list.sortBy { it.timeStamp }
list.sortBy { it.startTimestamp }
var previousTs = -1L
var translationItems = intArrayOf()
list.forEach {
// Merge lyric and translation
if (it.timeStamp == previousTs && it.label != SpeakerLabel.Background) {
if (it.startTimestamp == previousTs && it.label != SpeakerLabel.Background) {
list[list.indexOf(it) - 1].translationContent = it.content
translationItems += list.indexOf(it)
}
previousTs = it.timeStamp!!
previousTs = it.startTimestamp!!
}
// Remove translation items
translationItems.reversed().forEach { list.removeAt(it) }

// Add end timestamp to each item
list.forEachIndexed { index, it ->
if (it.wordTimestamps.isNotEmpty()) {
it.endTimestamp = it.wordTimestamps.last().third
} else {
it.endTimestamp = list.getOrNull(index + 1)?.startTimestamp ?: Long.MAX_VALUE
}
}

// Add absolute position to each item
list.takeWhile { it.content.isEmpty() }.forEach { _ -> list.removeAt(0) }
var absolutePosition = 0
list.forEachIndexed { index, it ->
if (it.content.isNotEmpty() && it.label != SpeakerLabel.Background) {
it.absolutePosition = absolutePosition
absolutePosition ++
absolutePosition++
} else {
it.absolutePosition = list[index - 1].absolutePosition
}
}
if (list.isEmpty() && lrcContent.isNotEmpty()) {
list.add(MediaStoreUtils.Lyric(null, lrcContent))
list.add(MediaStoreUtils.Lyric(content = lrcContent))
} else if (!foundNonNull) {
list.clear()
list.add(MediaStoreUtils.Lyric(null, lyricsText!!.toString()))
list.add(MediaStoreUtils.Lyric(content = lyricsText!!.toString()))
}
return list
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ object MediaStoreUtils {

@Parcelize
data class Lyric(
val timeStamp: Long? = null,
val startTimestamp: Long? = null,
var endTimestamp: Long? = null,
val content: String = "",
var translationContent: String = "",
var absolutePosition: Int? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class CustomTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
val colors: IntArray = intArrayOf(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, 0x24FFFFFF)
val colors: IntArray = intArrayOf(Color.WHITE),
val durationStart: Long = -1L,
val durationEnd: Long = -1L,
val contentHash: Int = 10721
) : AppCompatTextView(context, attrs, defStyleAttr) {

var gradient: LinearGradient? = null
Expand All @@ -27,9 +30,7 @@ class CustomTextView @JvmOverloads constructor(
currentProgress = percent
localMatrix.setTranslate(percent * width, height.toFloat())
gradient?.setLocalMatrix(localMatrix)
if (invalidate == true) {
invalidate()
}
if (invalidate) invalidate()
}

fun setDefaultGradient() = updateGradient(colors)
Expand Down
Loading

0 comments on commit 43a440c

Please sign in to comment.