Skip to content

Commit

Permalink
Add: ユーザー辞書を追加 (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenc-nanashi authored Nov 15, 2023
1 parent 0e872e1 commit 571fd07
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 31 deletions.
6 changes: 5 additions & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ dependencies {
implementation project(':capacitor-cordova-android-plugins')

// TODO: ちゃんと公開されたらそれに置き換える
implementation urlZipFile("voicevoxcore-android", "jp/hiroshiba/voicevoxcore/voicevoxcore-android/0.15.0-preview.13/voicevoxcore-android-0.15.0-preview.13.aar", "https://github.com/VOICEVOX/voicevox_core/releases/download/0.15.0-preview.13/java_packages.zip")
implementation urlZipFile(
"voicevoxcore-android_0.15.0-preview.15",
"jp/hiroshiba/voicevoxcore/voicevoxcore-android/0.15.0-preview.15/voicevoxcore-android-0.15.0-preview.15.aar",
"https://github.com/VOICEVOX/voicevox_core/releases/download/0.15.0-preview.15/java_packages.zip"
)

// https://mvnrepository.com/artifact/com.google.code.gson/gson
implementation group: 'com.google.code.gson', name: 'gson', version: "2.10.1"
Expand Down
1 change: 1 addition & 0 deletions android/app/capacitor.build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ android {

apply from: "../capacitor-cordova-android-plugins/cordova.variables.gradle"
dependencies {
implementation project(':capacitor-preferences')
implementation project(':capacitor-splash-screen')

}
Expand Down
55 changes: 44 additions & 11 deletions android/app/src/main/java/jp/hiroshiba/voicevox/CorePlugin.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jp.hiroshiba.voicevox

import android.app.Activity
import android.system.Os
import android.util.Log
import com.getcapacitor.JSObject
import com.getcapacitor.Plugin
Expand Down Expand Up @@ -34,8 +35,9 @@ class CorePlugin : Plugin() {
@PluginMethod
fun getSupportedDevicesJson(call: PluginCall) {
val ret = JSObject()
// TODO: ハードコードをやめてちゃんと取得する
ret.put("value", "{\"cpu\": true, \"cuda\": false, \"dml\": false}")
val supportedDevices = GlobalInfo.getSupportedDevices()
val supportedDevicesJson = gson.toJson(supportedDevices)
ret.put("value", supportedDevicesJson)
call.resolve(ret)
}

Expand Down Expand Up @@ -86,12 +88,22 @@ class CorePlugin : Plugin() {
Log.e("CorePlugin", "Couldn't get vvms")
return
}
vvms.sortWith(compareBy {
it.name.split(".")[0].length
})
voiceModels = vvms.map {
VoiceModel(it.absolutePath)
}

// Rustのtempfileクレートのための設定。
// /data/local/tmp はAndroid 10から書き込めなくなった。そのため、
// キャッシュディレクトリ内に一時フォルダを用意してそこから書き込むように設定する。
val tempDir = File(activity.cacheDir.absolutePath + "/.tmp")
tempDir.mkdirs()
Os.setenv("TMPDIR", tempDir.absolutePath, true)

call.resolve()
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -112,7 +124,7 @@ class CorePlugin : Plugin() {
}
synthesizer.loadVoiceModel(model)
call.resolve()
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -135,7 +147,7 @@ class CorePlugin : Plugin() {
val ret = JSObject()
ret.put("value", result)
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -154,7 +166,7 @@ class CorePlugin : Plugin() {
val ret = JSObject()
ret.put("value", gson.toJson(audioQuery))
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -173,7 +185,7 @@ class CorePlugin : Plugin() {
val ret = JSObject()
ret.put("value", gson.toJson(accentPhrases))
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -194,7 +206,7 @@ class CorePlugin : Plugin() {
val ret = JSObject()
ret.put("value", gson.toJson(newAccentPhrases))
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -215,7 +227,7 @@ class CorePlugin : Plugin() {
val ret = JSObject()
ret.put("value", gson.toJson(newAccentPhrases))
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -236,7 +248,7 @@ class CorePlugin : Plugin() {
val ret = JSObject()
ret.put("value", gson.toJson(newAccentPhrases))
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand All @@ -260,7 +272,28 @@ class CorePlugin : Plugin() {
val encodedResult = Base64.getEncoder().encodeToString(result)
ret.put("value", encodedResult)
call.resolve(ret)
} catch (e: VoicevoxException) {
} catch (e: Exception) {
call.reject(e.message)
}
}

@PluginMethod
fun useUserDict(call: PluginCall) {
val wordsJson = call.getString("wordsJson")
if (wordsJson == null) {
call.reject("Type mismatch")
return
}

try {
val words = gson.fromJson(wordsJson, Array<UserDict.Word>::class.java).asList()
val userDict = UserDict()
words.forEach { word ->
userDict.addWord(word)
}
openJtalk.useUserDict(userDict)
call.resolve()
} catch (e: Exception) {
call.reject(e.message)
}
}
Expand Down
3 changes: 3 additions & 0 deletions android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')

include ':capacitor-preferences'
project(':capacitor-preferences').projectDir = new File('../node_modules/@capacitor/preferences/android')

include ':capacitor-splash-screen'
project(':capacitor-splash-screen').projectDir = new File('../node_modules/@capacitor/splash-screen/android')
15 changes: 15 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"@capacitor/android": "5.5.1",
"@capacitor/core": "5.5.1",
"@capacitor/preferences": "5.0.6",
"@capacitor/splash-screen": "5.0.2",
"@gtm-support/vue-gtm": "1.2.3",
"@quasar/extras": "1.10.10",
Expand Down
Loading

0 comments on commit 571fd07

Please sign in to comment.