Skip to content

Commit

Permalink
feat: ヘッダー行のみの場合、特殊読み上げを行う機能を追加 (#182)
Browse files Browse the repository at this point in the history
* feat: ヘッダー行のみの場合、特殊読み上げを行う機能を追加

* fix: removed debug print

* fix: あたらしいテキストの処理を修正

* chore: prepare for adding H1,2,3

* feat: leveled heading

---------

Co-authored-by: yuua <identity@yuua.dev>
  • Loading branch information
book000 and yuuahp authored Jul 14, 2024
1 parent 0c5e3e9 commit ccc2782
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class VoiceCommand : Extension() {
if (it == "none") null else Emotion.valueOf(it)
}

val newVoice = oldVoice.overwrite(
val newVoice = oldVoice.copyNotNull(
speaker = arguments.speaker?.let { Speaker.valueOf(it) },
emotion = emotion,
emotionLevel = if (emotion != null) arguments.emotionLevel else null,
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/jaoafa/vcspeaker/stores/GuildStore.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.jaoafa.vcspeaker.stores

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.tts.api.Speaker
import com.jaoafa.vcspeaker.tts.Voice
import com.jaoafa.vcspeaker.tts.api.Speaker
import dev.kord.common.entity.Snowflake
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

@Serializable
Expand Down
3 changes: 1 addition & 2 deletions src/main/kotlin/com/jaoafa/vcspeaker/stores/VoiceStore.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.jaoafa.vcspeaker.stores

import com.jaoafa.vcspeaker.VCSpeaker
import com.jaoafa.vcspeaker.tts.api.Speaker
import com.jaoafa.vcspeaker.tts.Voice
import com.jaoafa.vcspeaker.tts.api.Speaker
import dev.kord.common.entity.Snowflake
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

@Serializable
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/jaoafa/vcspeaker/tts/Voice.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class Voice(
) {
fun toJson() = Json.encodeToString(serializer(), this)

fun overwrite(
fun copyNotNull(
speaker: Speaker? = null,
emotion: Emotion? = null,
emotionLevel: Int? = null,
Expand Down
16 changes: 11 additions & 5 deletions src/main/kotlin/com/jaoafa/vcspeaker/tts/markdown/Line.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ package com.jaoafa.vcspeaker.tts.markdown
data class Line(val inlines: List<Inline>, val effects: Set<LineEffect>) {
companion object {
fun from(paragraph: String): Line {
val inlines = Inline.from(paragraph)
val plainText = inlines.joinToString("") { it.text }
val prefixCandidates = plainText.split(" ").filter { it.isNotEmpty() }
val prefixCandidates = paragraph.split(" ").filter { it.isNotEmpty() }

val effects = mutableSetOf<LineEffect>()
var skipped = false

for (prefixCandidate in prefixCandidates) {
if (skipped) break

// null if this is not a prefix
val prefix = LineEffect.entries.firstOrNull { it.regex.matches(prefixCandidate) }

if (prefix != null && !skipped) effects.add(prefix)
if (prefix != null) effects.add(prefix)
else skipped = true
}

val text = prefixCandidates.drop(effects.size).joinToString(" ")

val inlines = Inline.from(text)

return Line(inlines, effects)
}
}
Expand All @@ -30,7 +34,9 @@ data class Line(val inlines: List<Inline>, val effects: Set<LineEffect>) {
}

enum class LineEffect(val regex: Regex) {
Header(Regex("^#{1,3}$")),
Heading1(Regex("^#$")),
Heading2(Regex("^##$")),
Heading3(Regex("^###$")),
Quote(Regex("^>$")),
BulletList(Regex("^[*-]$")),
NumberedList(Regex("^\\d+\\.$"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InlineVoiceProcessor : BaseProcessor() {
key to value
}.toMap()

val newVoice = voice.overwrite(
val newVoice = voice.copyNotNull(
speaker = parameterMap["speaker"]?.let { Speaker.valueOf(it.capitalizeWords()) },
emotion = parameterMap["emotion"]?.let { Emotion.valueOf(it.capitalizeWords()) },
emotionLevel = parameterMap["emotion_level"]?.toIntOrNull(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package com.jaoafa.vcspeaker.tts.processors

import com.jaoafa.vcspeaker.tts.Voice
import com.jaoafa.vcspeaker.tts.markdown.LineEffect
import com.jaoafa.vcspeaker.tts.markdown.toMarkdown
import dev.kord.core.entity.Message

class MarkdownFormatProcessor : BaseProcessor() {
override val priority = 60
override val priority = 80

override suspend fun process(message: Message?, content: String, voice: Voice): Pair<String, Voice> {
val markdown = content.toMarkdown().joinToString("") { it.toReadable() }
val markdown = content.toMarkdown()

return markdown to voice
val heading = markdown.first().effects.firstOrNull { it.name.startsWith("Heading") }

val newVoice = if (markdown.size == 1 && heading != null) {
when (heading) {
LineEffect.Heading1 -> voice.copy(speed = 200)
LineEffect.Heading2 -> voice.copy(speed = 175)
LineEffect.Heading3 -> voice.copy(speed = 150)
else -> voice
}
} else voice

val readableMarkdown = markdown.joinToString(" ") { it.toReadable() }

return readableMarkdown to newVoice
}
}
Loading

0 comments on commit ccc2782

Please sign in to comment.