Skip to content

Commit

Permalink
fix a lyric bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Jan 29, 2025
1 parent 0f7633d commit ed36be9
Showing 1 changed file with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import android.text.Spanned
import android.text.StaticLayout
import android.text.TextPaint
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.animation.PathInterpolator
Expand Down Expand Up @@ -39,9 +40,11 @@ import org.akanework.gramophone.logic.utils.SemanticLyrics
import org.akanework.gramophone.logic.utils.SpeakerEntity
import org.akanework.gramophone.ui.MainActivity

private const val TAG = "NewLyricsView"

class NewLyricsView(context: Context, attrs: AttributeSet) : View(context, attrs) {

private val useBaseTs = true
private val useBaseTs = true // TODO
private val smallSizeFactor = 0.97f
private val lyricAnimTime = 650f
private var currentScrollTarget: Int? = null
Expand Down Expand Up @@ -271,7 +274,7 @@ class NewLyricsView(context: Context, attrs: AttributeSet) : View(context, attrs
else (instance?.currentPosition?.toULong() ?: 0uL)
if (useBaseTs && (cPos.toLong() - (instance?.currentPosition ?: 0L)).absoluteValue > 500L)
timebase = (instance?.currentPosition?.toULong() ?: 0uL) to System.currentTimeMillis()
if (cPos != posForRender)
if (cPos != posForRender) // if not playing, might stay same
invalidate()
}

Expand Down Expand Up @@ -310,9 +313,12 @@ class NewLyricsView(context: Context, attrs: AttributeSet) : View(context, attrs
// manually count seconds from last known position and only rely on player pos when it
// ends up being very different. Shouldn't cause lyric delays because if a file is played at
// 1x speed then X seconds of playback will have passed if you wait X seconds.
posForRender = if (useBaseTs && instance?.isPlaying == true) timebase.first +
posForRender = (if (useBaseTs && instance?.isPlaying == true) timebase.first +
((System.currentTimeMillis() - timebase.second).toULong())
else (instance?.currentPosition?.toULong() ?: 0uL)
else (instance?.currentPosition?.toULong() ?: 0uL)).also {
if (posForRender > it && posForRender - it < 1000uL)
Log.w(TAG, "regressing position by ${posForRender - it}ms from $posForRender to $it!")
}
if (spForRender == null) {
requestLayout()
return
Expand Down Expand Up @@ -682,6 +688,8 @@ class NewLyricsView(context: Context, attrs: AttributeSet) : View(context, attrs
}, (width * smallSizeFactor).toInt()
).setAlignment(align).build()
val paragraphRtl = layout.getParagraphDirection(0) == Layout.DIR_RIGHT_TO_LEFT
val alignmentNormal = if (paragraphRtl) align == Layout.Alignment.ALIGN_OPPOSITE
else align == Layout.Alignment.ALIGN_NORMAL
val lineOffsets = syncedLines?.get(i)?.words?.map {
val ia = mutableListOf<Int>()
val firstLine = layout.getLineForOffset(it.charRange.first)
Expand Down Expand Up @@ -746,7 +754,7 @@ class NewLyricsView(context: Context, attrs: AttributeSet) : View(context, attrs
(0..<layout.lineCount).map { line ->
LrcUtils.findBidirectionalBarriers(layout.text.subSequence(
layout.getLineStart(line), layout.getLineEnd(line))).flatMap {
if (it.second != paragraphRtl)
if (it.second == alignmentNormal)
listOf(line, line)
else
listOf(line)
Expand Down

0 comments on commit ed36be9

Please sign in to comment.