Skip to content

Commit c32a576

Browse files
committed
Try to be smart about TTML sides
1 parent 7a99833 commit c32a576

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

app/src/main/java/org/akanework/gramophone/logic/utils/Flags.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.akanework.gramophone.logic.utils
22

33
object Flags {
44
const val TEST_RG_OFFLOAD = false // test only
5+
const val TTML_AGENT_SMART_SIDES = true
56

67
// Before turning it on in prod we need i18n.
78
const val FORMAT_INFO_DIALOG = true // TODO(ASAP)

app/src/main/java/org/akanework/gramophone/logic/utils/SemanticLyrics.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.akanework.gramophone.logic.utils.SemanticLyrics.UnsyncedLyrics
1919
import org.akanework.gramophone.logic.utils.SemanticLyrics.Word
2020
import org.xmlpull.v1.XmlPullParser
2121
import org.xmlpull.v1.XmlPullParserException
22+
import uk.akane.libphonograph.putIfAbsentSupport
2223
import java.io.StringReader
2324
import java.nio.charset.Charset
2425
import java.util.concurrent.atomic.AtomicReference
@@ -1521,6 +1522,19 @@ fun parseTtml(audioMimeType: String?, lyricText: String): SemanticLyrics? {
15211522
} while (cur != -1)
15221523
out
15231524
}
1525+
// start left if person is first, and right if sample is first.
1526+
val agentToSide = hashMapOf<String?, Boolean /* true = right */>()
1527+
if (Flags.TTML_AGENT_SMART_SIDES) {
1528+
var agent: String? = null
1529+
var side = state.paragraphs.firstOrNull()?.let { peopleToType[it.agent] == "other" } == true
1530+
state.paragraphs.forEach {
1531+
if (agent != it.agent) {
1532+
agent = it.agent
1533+
agentToSide.putIfAbsentSupport(it.agent, side)
1534+
side = agentToSide[it.agent]!!
1535+
}
1536+
}
1537+
}
15241538
val hasAtLeastTwoPeople = people["person"]?.let { it.size > 1 } == true
15251539
if (paragraphs.find { it.time != null } == null) {
15261540
return UnsyncedLyrics(paragraphs.map {
@@ -1530,10 +1544,10 @@ fun parseTtml(audioMimeType: String?, lyricText: String): SemanticLyrics? {
15301544
val isOther = peopleToType[it.agent] == "other"
15311545
// first person goes left, second right, third left, fourth right, and so on.
15321546
// and the same goes for "other" except that we start on the right here.
1533-
val isVoice2 =
1534-
it.agent != null && (people[peopleToType[it.agent]] ?: throw NullPointerException(
1547+
val isVoice2 = agentToSide[it.agent] ?:
1548+
(it.agent != null && (people[peopleToType[it.agent]] ?: throw NullPointerException(
15351549
"expected to find ${it.agent} (${peopleToType[it.agent]}) in $people"
1536-
)).indexOf(it.agent) % 2 == (if (isOther) 0 else 1)
1550+
)).indexOf(it.agent) % 2 == (if (isOther) 0 else 1))
15371551
val speaker = when {
15381552
isGroup && isBg -> SpeakerEntity.GroupBackground
15391553
isGroup -> SpeakerEntity.Group

0 commit comments

Comments
 (0)