Skip non-speech symbols in reader TTS
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
Build & Test / Build & Test App (push) Has been cancelled
Release / Extract tag name (push) Has been cancelled
Update website / update_website (release) Has been cancelled
Release / Build (push) Has been cancelled
Release / Build (FOSS) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
This commit is contained in:
@@ -293,10 +293,17 @@ class ReaderTtsService : Service() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
currentUtteranceBaseOffset = speechOffset
|
currentUtteranceBaseOffset = speechOffset
|
||||||
|
val speechText = sanitizeSpeechText(segment.text.substring(speechOffset))
|
||||||
|
if (speechText.isBlank()) {
|
||||||
|
segmentSpeechOffset = 0
|
||||||
|
advanceSegment()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
val utteranceId = "$ttsGeneration:$pagePosition:$segmentPosition:$speechOffset"
|
val utteranceId = "$ttsGeneration:$pagePosition:$segmentPosition:$speechOffset"
|
||||||
currentUtteranceId = utteranceId
|
currentUtteranceId = utteranceId
|
||||||
val result = tts?.speak(
|
val result = tts?.speak(
|
||||||
segment.text.substring(speechOffset),
|
speechText,
|
||||||
TextToSpeech.QUEUE_FLUSH,
|
TextToSpeech.QUEUE_FLUSH,
|
||||||
null,
|
null,
|
||||||
utteranceId,
|
utteranceId,
|
||||||
@@ -680,6 +687,77 @@ class ReaderTtsService : Service() {
|
|||||||
|
|
||||||
override fun onBind(intent: Intent?): IBinder? = null
|
override fun onBind(intent: Intent?): IBinder? = null
|
||||||
|
|
||||||
|
private fun sanitizeSpeechText(text: String): String {
|
||||||
|
if (text.isBlank()) return text
|
||||||
|
|
||||||
|
var hasReadableContent = false
|
||||||
|
var scan = 0
|
||||||
|
while (scan < text.length) {
|
||||||
|
val codePoint = text.codePointAt(scan)
|
||||||
|
if (Character.isLetterOrDigit(codePoint)) {
|
||||||
|
hasReadableContent = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
scan += Character.charCount(codePoint)
|
||||||
|
}
|
||||||
|
if (!hasReadableContent) return " ".repeat(text.length)
|
||||||
|
|
||||||
|
val chars = text.toCharArray()
|
||||||
|
var index = 0
|
||||||
|
while (index < text.length) {
|
||||||
|
val codePoint = text.codePointAt(index)
|
||||||
|
if (!isPunctuationOrSymbol(codePoint)) {
|
||||||
|
index += Character.charCount(codePoint)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val runStart = index
|
||||||
|
var runEnd = index
|
||||||
|
var containsSymbol = false
|
||||||
|
while (runEnd < text.length) {
|
||||||
|
val current = text.codePointAt(runEnd)
|
||||||
|
if (!isPunctuationOrSymbol(current)) break
|
||||||
|
containsSymbol = containsSymbol || isSymbol(current)
|
||||||
|
runEnd += Character.charCount(current)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (containsSymbol || runEnd - runStart >= 2) {
|
||||||
|
for (offset in runStart until runEnd) chars[offset] = ' '
|
||||||
|
}
|
||||||
|
index = runEnd
|
||||||
|
}
|
||||||
|
return chars.concatToString()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isPunctuationOrSymbol(codePoint: Int): Boolean {
|
||||||
|
return when (Character.getType(codePoint)) {
|
||||||
|
Character.CONNECTOR_PUNCTUATION.toInt(),
|
||||||
|
Character.DASH_PUNCTUATION.toInt(),
|
||||||
|
Character.START_PUNCTUATION.toInt(),
|
||||||
|
Character.END_PUNCTUATION.toInt(),
|
||||||
|
Character.INITIAL_QUOTE_PUNCTUATION.toInt(),
|
||||||
|
Character.FINAL_QUOTE_PUNCTUATION.toInt(),
|
||||||
|
Character.OTHER_PUNCTUATION.toInt(),
|
||||||
|
Character.MATH_SYMBOL.toInt(),
|
||||||
|
Character.CURRENCY_SYMBOL.toInt(),
|
||||||
|
Character.MODIFIER_SYMBOL.toInt(),
|
||||||
|
Character.OTHER_SYMBOL.toInt(),
|
||||||
|
-> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun isSymbol(codePoint: Int): Boolean {
|
||||||
|
return when (Character.getType(codePoint)) {
|
||||||
|
Character.MATH_SYMBOL.toInt(),
|
||||||
|
Character.CURRENCY_SYMBOL.toInt(),
|
||||||
|
Character.MODIFIER_SYMBOL.toInt(),
|
||||||
|
Character.OTHER_SYMBOL.toInt(),
|
||||||
|
-> true
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun segmentText(text: String): List<Segment> {
|
private fun segmentText(text: String): List<Segment> {
|
||||||
if (text.isBlank()) return emptyList()
|
if (text.isBlank()) return emptyList()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user