Skip to content

Commit

Permalink
tts: add logging (:tts-lib), fix GetResourceListeners (:app-tts)
Browse files Browse the repository at this point in the history
  • Loading branch information
sszuev committed Sep 12, 2024
1 parent 2a7dc0d commit fb35ecd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app-tts/src/main/kotlin/GetResourceListeners.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fun onGetResource(commands: RedisCommands<String, String>) = try {
var res2 = commands.incr("words.count.daily")
val date = commands.get("words.date")?.let { Instant.parse(it) }
val now = Instant.now()
if (date != null && Duration.between(date, now).seconds > 24 * 60 * 60) {
if (date == null || Duration.between(date, now).seconds > 24 * 60 * 60) {
// new day
res2 = commands.del("words.count.daily")
commands.set("words.date", now.toString())
Expand Down
26 changes: 17 additions & 9 deletions tts-lib/src/main/kotlin/impl/CombinedTextToSpeechService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import com.gitlab.sszuev.flashcards.speaker.ResourceCache
import com.gitlab.sszuev.flashcards.speaker.TTSConfig
import com.gitlab.sszuev.flashcards.speaker.TextToSpeechService
import com.gitlab.sszuev.flashcards.speaker.toResourcePath
import org.slf4j.LoggerFactory

private val logger = LoggerFactory.getLogger(CombinedTextToSpeechService::class.java)

class CombinedTextToSpeechService(
resourceIdMapper: (String) -> Pair<String, String>? = { toResourcePath(it) },
Expand All @@ -17,18 +20,23 @@ class CombinedTextToSpeechService(
private val espeakNgTestToSpeechService =
EspeakNgTestToSpeechService(resourceIdMapper = resourceIdMapper, config = config)

override suspend fun getResource(id: String, vararg args: String): ByteArray? {
override suspend fun getResource(id: String, vararg args: String): ByteArray? = try {
var res = cache.get(id)
if (res != null) {
return res
}
res = voicerssTextToSpeechService.getResource(id, *args)
if (res != null) {
onGetResource()
cache.put(id, res)
return res
res
} else {
res = voicerssTextToSpeechService.getResource(id, *args)
if (res != null) {
onGetResource()
cache.put(id, res)
res
} else {
espeakNgTestToSpeechService.getResource(id, *args)
}
}
return espeakNgTestToSpeechService.getResource(id, *args)
} catch (ex: Exception) {
logger.error(ex.message, ex)
null
}

override suspend fun containsResource(id: String): Boolean {
Expand Down
6 changes: 4 additions & 2 deletions tts-lib/src/main/kotlin/impl/EspeakNgTestToSpeechService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ class EspeakNgTestToSpeechService(
override suspend fun getResource(id: String, vararg args: String): ByteArray? {
val langToWord = resourceIdMapper(id) ?: return null
val lang = languageByTag(langToWord.first) ?: return null
val word = langToWord.second
logger.info("::[ESPEAK-NG]$lang:::'$word'")
val processBuilder =
ProcessBuilder("/bin/bash", "-c", """espeak-ng -v $lang '${langToWord.second}' --stdout""")
ProcessBuilder("/bin/bash", "-c", """espeak-ng -v $lang '$word' --stdout""")
return try {
withTimeout(config.getResourceTimeoutMs) {
val s = System.currentTimeMillis()
Expand All @@ -79,7 +81,7 @@ class EspeakNgTestToSpeechService(
res
}
} catch (ex: Exception) {
logger.error("::[ESPEAK-NG] Can't get resource for [${lang}:${langToWord.second}]")
logger.error("::[ESPEAK-NG] Can't get resource for [${lang}:$word]")
throw ex
}
}
Expand Down

0 comments on commit fb35ecd

Please sign in to comment.