From 0cb9694a20b8c95e040650f78dfd60399f07673b Mon Sep 17 00:00:00 2001 From: sevenc-nanashi Date: Wed, 15 Nov 2023 20:41:52 +0900 Subject: [PATCH] =?UTF-8?q?Add:=20Android=E5=81=B4=E3=82=92=E5=AE=9F?= =?UTF-8?q?=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/app/build.gradle | 6 +- .../java/jp/hiroshiba/voicevox/CorePlugin.kt | 55 +++++++++++++++---- src/mobile/engine/dict.ts | 31 ++++++++--- src/mobile/engine/index.ts | 8 ++- src/mobile/engine/manifestAssets/base.json | 2 +- src/mobile/plugin.ts | 2 +- 6 files changed, 79 insertions(+), 25 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index cc6d810b5e..7088395020 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -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" diff --git a/android/app/src/main/java/jp/hiroshiba/voicevox/CorePlugin.kt b/android/app/src/main/java/jp/hiroshiba/voicevox/CorePlugin.kt index 54184b1fad..4a3d111044 100644 --- a/android/app/src/main/java/jp/hiroshiba/voicevox/CorePlugin.kt +++ b/android/app/src/main/java/jp/hiroshiba/voicevox/CorePlugin.kt @@ -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 @@ -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) } @@ -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から書き込めなくなった。そのため、 + // filesのディレクトリ内に一時フォルダを用意してそこから書き込むように設定する。 + val tempDir = File(activity.filesDir.absolutePath + "/.tmp") + tempDir.mkdirs() + Os.setenv("TMPDIR", tempDir.absolutePath, true) + call.resolve() - } catch (e: VoicevoxException) { + } catch (e: Exception) { call.reject(e.message) } } @@ -112,7 +124,7 @@ class CorePlugin : Plugin() { } synthesizer.loadVoiceModel(model) call.resolve() - } catch (e: VoicevoxException) { + } catch (e: Exception) { call.reject(e.message) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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) } } @@ -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::class.java).asList() + val userDict = UserDict() + words.forEach { word -> + userDict.addWord(word) + } + openJtalk.useUserDict(userDict) + call.resolve() + } catch (e: Exception) { call.reject(e.message) } } diff --git a/src/mobile/engine/dict.ts b/src/mobile/engine/dict.ts index 2bcc06ecb6..56f2b208bf 100644 --- a/src/mobile/engine/dict.ts +++ b/src/mobile/engine/dict.ts @@ -1,20 +1,34 @@ import { Preferences } from "@capacitor/preferences"; import { v4 as uuidv4 } from "uuid"; +import { VoicevoxCorePlugin } from "../plugin"; import { ApiProvider } from "."; import { UserDictWord } from "@/openapi"; type InternalUserDict = Record; const preferenceKey = "userDict"; -const getUserDictWords = async () => { +export const getUserDictWords = async () => { const userDictJson = await Preferences.get({ key: preferenceKey }); const dict: InternalUserDict = userDictJson.value ? JSON.parse(userDictJson.value) : {}; return dict; }; -const setUserDictWords = async (dict: InternalUserDict) => { - await Preferences.set({ key: preferenceKey, value: JSON.stringify(dict) }); +export const useUserDictWords = async ( + corePlugin: VoicevoxCorePlugin, + dict: InternalUserDict +) => { + await corePlugin.useUserDict({ + wordsJson: JSON.stringify( + Object.values(dict).map((word) => ({ + surface: word.surface, + pronunciation: word.pronunciation, + accent_type: word.accentType, + priority: word.priority, + word_type: word.wordType, + })) + ), + }); }; type InternalWordType = @@ -90,7 +104,11 @@ const apiWordToInternalWord = (word: UserDictWord): InternalDictWord => { }; }; -const dictProvider: ApiProvider = () => { +const dictProvider: ApiProvider = ({ corePlugin }) => { + const setUserDictWords = async (dict: InternalUserDict) => { + await Preferences.set({ key: preferenceKey, value: JSON.stringify(dict) }); + await useUserDictWords(corePlugin, dict); + }; return { async getUserDictWordsUserDictGet() { const dict = await getUserDictWords(); @@ -112,9 +130,6 @@ const dictProvider: ApiProvider = () => { ); }, async addUserDictWordUserDictWordPost(word) { - if (!word.wordType) { - throw new Error("wordType is required"); - } const uuid = uuidv4(); const dict = await getUserDictWords(); dict[uuid] = { @@ -122,7 +137,7 @@ const dictProvider: ApiProvider = () => { pronunciation: word.pronunciation, accentType: word.accentType, priority: word.priority ?? 5, - wordType: word.wordType, + wordType: word.wordType ?? "COMMON_NOUN", }; await setUserDictWords(dict); return uuid; diff --git a/src/mobile/engine/index.ts b/src/mobile/engine/index.ts index d6af74fef8..965bc5abb6 100644 --- a/src/mobile/engine/index.ts +++ b/src/mobile/engine/index.ts @@ -2,7 +2,7 @@ import { VoicevoxCorePlugin } from "../plugin"; import queryProvider from "./query"; import infoProvider from "./info"; import speakerProvider from "./speaker"; -import dictProvider from "./dict"; +import dictProvider, { getUserDictWords, useUserDictWords } from "./dict"; import { DefaultApi, DefaultApiInterface } from "@/openapi"; let api: DefaultApi | undefined; @@ -16,9 +16,11 @@ const loadApi = () => { const corePlugin = window.plugins?.voicevoxCore; if (!corePlugin) throw new Error("assert: corePlugin != null"); let isCoreInitialized = false; - corePlugin.initialize().then(() => { + (async () => { + await corePlugin.initialize(); + await useUserDictWords(corePlugin, await getUserDictWords()); isCoreInitialized = true; - }); + })(); // コアベースのOpenAPI Connectorライクなオブジェクト。 // - コアベースの実装がある場合は呼び出し、 diff --git a/src/mobile/engine/manifestAssets/base.json b/src/mobile/engine/manifestAssets/base.json index 98ec9b9194..cf3df651bc 100644 --- a/src/mobile/engine/manifestAssets/base.json +++ b/src/mobile/engine/manifestAssets/base.json @@ -17,7 +17,7 @@ "adjust_intonation_scale": true, "adjust_volume_scale": true, "interrogative_upspeak": true, - "synthesis_morphing": true, + "synthesis_morphing": false, "manage_library": false } } diff --git a/src/mobile/plugin.ts b/src/mobile/plugin.ts index a3b01e6bd6..826f8ce14e 100644 --- a/src/mobile/plugin.ts +++ b/src/mobile/plugin.ts @@ -38,7 +38,7 @@ export type VoicevoxCorePlugin = { enableInterrogativeUpspeak: boolean; }) => Promise<{ value: string }>; - userDictLoad: (obj: { dictJson: string }) => Promise; + useUserDict: (obj: { wordsJson: string }) => Promise; }; const loadPlugin = () => {