Skip to content

Commit

Permalink
feat: mozhi tts support (closes #375)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Dec 6, 2023
1 parent 17e9dbd commit c8c2a54
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 20 deletions.
17 changes: 5 additions & 12 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions app/src/main/java/com/bnyro/translate/api/lv/LVEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,9 @@ class LVEngine : TranslationEngine(
override suspend fun getAudioFile(lang: String, query: String): File? {
val byteArray = api.getAudio(lang, query).toByteArray()
if (byteArray.isEmpty()) return null
return withContext(Dispatchers.IO) {
File.createTempFile("audio", ".mp3").apply {
writeBytes(byteArray)
}

return File.createTempFile("audio", ".mp3").apply {
writeBytes(byteArray)
}
}
}
12 changes: 11 additions & 1 deletion app/src/main/java/com/bnyro/translate/api/mh/MhEngine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.util.RetrofitHelper
import com.bnyro.translate.util.TranslationEngine
import java.io.File

class MhEngine : TranslationEngine(
name = "Mozhi",
Expand All @@ -38,7 +39,8 @@ class MhEngine : TranslationEngine(
"mymemory",
"watson",
"yandex"
)
),
supportsAudio = true
) {
lateinit var api: Mozhi

Expand All @@ -64,4 +66,12 @@ class MhEngine : TranslationEngine(
detectedLanguage = response.sourceLanguage
)
}

override suspend fun getAudioFile(lang: String, query: String): File? {
val audioBytes = api.getAudioFile(lang = lang, text = query).body()?.bytes() ?: return null

return File.createTempFile("audio", ".mp3").apply {
writeBytes(audioBytes)
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/java/com/bnyro/translate/api/mh/Mozhi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.bnyro.translate.api.mh

import com.bnyro.translate.api.mh.obj.MhLanguage
import com.bnyro.translate.api.st.obj.STTranslationResponse
import okhttp3.ResponseBody
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Query

Expand All @@ -35,4 +37,11 @@ interface Mozhi {
suspend fun getLanguages(
@Query("engine") engine: String?
): List<MhLanguage>

@GET("api/tts")
suspend fun getAudioFile(
@Query("engine") engine: String? = "google",
@Query("lang") lang: String,
@Query("text") text: String
): Response<ResponseBody>
}
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class TranslationModel : ViewModel() {

fun playAudio() {
releaseMediaPlayer()

viewModelScope.launch(Dispatchers.IO) {
audioFile = runCatching {
engine.getAudioFile(targetLanguage.code, translation.translatedText)
Expand Down

0 comments on commit c8c2a54

Please sign in to comment.