diff --git a/Cargo.lock b/Cargo.lock index 8e03f58e1..2c770847b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -116,7 +116,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c494134f746c14dc653a35a4ea5aca24ac368529da5370ecf41fe0341c35772f" dependencies = [ "android_log-sys", - "env_logger 0.10.2", + "env_logger", "log", "once_cell", ] @@ -475,15 +475,13 @@ checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" [[package]] name = "bindgen" -version = "0.60.1" +version = "0.62.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "062dddbc1ba4aca46de6338e2bf87771414c335f7b2f2036e8f3e9befebf88e6" +checksum = "c6720a8b7b2d39dd533285ed438d458f65b31b5c257e6ac7bb3d7e82844dd722" dependencies = [ "bitflags 1.3.2", "cexpr", "clang-sys", - "clap 3.2.25", - "env_logger 0.9.3", "lazy_static", "lazycell", "log", @@ -493,6 +491,7 @@ dependencies = [ "regex", "rustc-hash", "shlex", + "syn 1.0.109", "which", ] @@ -1267,29 +1266,6 @@ dependencies = [ "syn 2.0.52", ] -[[package]] -name = "env_filter" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea" -dependencies = [ - "log", - "regex", -] - -[[package]] -name = "env_logger" -version = "0.9.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "env_logger" version = "0.10.2" @@ -1300,19 +1276,6 @@ dependencies = [ "regex", ] -[[package]] -name = "env_logger" -version = "0.11.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9" -dependencies = [ - "anstream 0.6.13", - "anstyle", - "env_filter", - "humantime", - "log", -] - [[package]] name = "equivalent" version = "1.0.1" @@ -1844,12 +1807,6 @@ dependencies = [ "libm", ] -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hyper" version = "0.14.28" @@ -2510,8 +2467,9 @@ checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" [[package]] name = "open_jtalk" version = "0.1.25" -source = "git+https://github.com/White-Green/open_jtalk-rs.git?branch=support_wasm32-unknown-emscripten#edc964bebff2ffdd00f0bf6248cf2426bd26d710" +source = "git+https://github.com/sevenc-nanashi/open_jtalk-rs.git?branch=add/wasm#a650313b5592cfebb2b873952f80bf4930ec72d9" dependencies = [ + "camino", "open_jtalk-sys", "thiserror", ] @@ -2519,7 +2477,7 @@ dependencies = [ [[package]] name = "open_jtalk-sys" version = "0.16.111" -source = "git+https://github.com/White-Green/open_jtalk-rs.git?branch=support_wasm32-unknown-emscripten#edc964bebff2ffdd00f0bf6248cf2426bd26d710" +source = "git+https://github.com/sevenc-nanashi/open_jtalk-rs.git?branch=add/wasm#a650313b5592cfebb2b873952f80bf4930ec72d9" dependencies = [ "bindgen", "cmake", @@ -4583,23 +4541,6 @@ dependencies = [ "voicevox_core", ] -[[package]] -name = "voicevox_core_wasm_api" -version = "0.0.0" -dependencies = [ - "base64 0.21.7", - "chrono", - "env_logger 0.11.3", - "log", - "serde", - "serde_json", - "strum", - "tempfile", - "tracing-subscriber", - "voicevox_core", - "zip", -] - [[package]] name = "vswhom" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 573bbae18..612a9acd6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -94,9 +94,8 @@ rev = "ebb9dcb9b26ee681889b52b6db3b4f642b04a250" # git = "https://github.com/VOICEVOX/open_jtalk-rs.git" # rev = "70c76bb54522830e92803038191bf533ba68ce85" -# https://github.com/White-Green/open_jtalk-rs/tree/support_wasm32-unknown-emscripten -git = "https://github.com/White-Green/open_jtalk-rs.git" -branch = "support_wasm32-unknown-emscripten" +git = "https://github.com/sevenc-nanashi/open_jtalk-rs.git" +branch = "add/wasm" # FIXME: iOS対応のpull request(https://github.com/wesleywiser/process_path/pull/16)がマージされる見込みが無いため [workspace.dependencies.process_path] diff --git a/crates/voicevox_core_c_api/Cargo.toml b/crates/voicevox_core_c_api/Cargo.toml index 3d950f0e8..ed18dc5a5 100644 --- a/crates/voicevox_core_c_api/Cargo.toml +++ b/crates/voicevox_core_c_api/Cargo.toml @@ -27,7 +27,6 @@ futures.workspace = true itertools.workspace = true libc.workspace = true once_cell.workspace = true -process_path.workspace = true serde_json = { workspace = true, features = ["preserve_order"] } thiserror.workspace = true tracing.workspace = true @@ -35,6 +34,9 @@ tracing-subscriber = { workspace = true, features = ["env-filter"] } uuid.workspace = true voicevox_core.workspace = true +[target."cfg(not(target_family = \"wasm\"))".dependencies] +process_path.workspace = true + [dev-dependencies] anyhow.workspace = true assert_cmd = { workspace = true, features = ["color-auto"] } @@ -55,6 +57,9 @@ test_util.workspace = true toml.workspace = true typetag.workspace = true +[target."cfg(target_family = \"wasm\")".build-dependencies] +regex.workspace = true + [lints.rust] unsafe_code = "allow" # C APIのための操作 rust_2018_idioms = "warn" diff --git a/crates/voicevox_core_c_api/build.rs b/crates/voicevox_core_c_api/build.rs index 535e73676..ecac0cd16 100644 --- a/crates/voicevox_core_c_api/build.rs +++ b/crates/voicevox_core_c_api/build.rs @@ -7,4 +7,52 @@ fn main() { println!("cargo:rustc-link-arg=-Wl,-rpath,@loader_path/"); println!("cargo:rustc-link-arg=-Wl,-install_name,@rpath/libvoicevox_core.dylib"); } + + if std::env::var("TARGET").unwrap() == "wasm32-unknown-emscripten" { + // use regex::Regex; + // use std::fs; + + // let lib_rs = fs::read_to_string("src/lib.rs").unwrap(); + // let re = Regex::new(r#"pub (?:unsafe )?extern "C" fn (\w+)"#).unwrap(); + // let mut functions = vec![]; + // for cap in re.captures_iter(&lib_rs) { + // functions.push(cap[1].to_string()); + // } + // println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0"); + // // TODO: WARNにしたいけど、これにするとemccがクラッシュする(どうして...) + // // println!("cargo:rustc-link-arg=-sWARN_ON_UNDEFINED_SYMBOLS=1"); + // println!( + // "cargo:rustc-link-arg=-sEXPORTED_FUNCTIONS=['{}']", + // functions.join("','") + // ); + // println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['ccall']"); + // println!("cargo:rustc-link-arg=-sEXPORT_NAME=\"RawVoicevoxCore\""); + // println!("cargo:rustc-link-arg=-sMODULARIZE=1"); + // println!("cargo:rustc-link-arg=-sALLOW_MEMORY_GROWTH=1"); + // println!("cargo:rustc-link-arg=-sTOTAL_STACK=128MB"); + // println!("cargo:rustc-link-arg=-sINITIAL_MEMORY=256MB"); + println!( + "cargo:rustc-link-arg=--js-library={}", + std::env::var("CARGO_MANIFEST_DIR").unwrap() + "/wasm_library.js" + ); + + println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['ccall', 'FS']"); + // println!("cargo:rustc-link-arg=--no-entry"); + let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); + let target_dir = out_dir + .parent() + .unwrap() + .parent() + .unwrap() + .parent() + .unwrap(); + println!( + "cargo:rustc-link-arg=-o{}/voicevox_core_wasm_api.mjs", + target_dir.display() + ); + println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0"); + println!("cargo:rustc-link-arg=-sEXPORT_NAME=VoicevoxCore"); + println!("cargo:rustc-link-arg=-DEMSCRIPTEN_STANDALONE_WASM"); + println!("cargo:rustc-link-arg=--no-entry"); + } } diff --git a/crates/voicevox_core_c_api/src/compatible_engine.rs b/crates/voicevox_core_c_api/src/compatible_engine.rs index 6755910f5..131a439dd 100644 --- a/crates/voicevox_core_c_api/src/compatible_engine.rs +++ b/crates/voicevox_core_c_api/src/compatible_engine.rs @@ -2,6 +2,7 @@ use std::{ collections::BTreeMap, env, ffi::{c_char, CString}, + path::PathBuf, sync::{Mutex, MutexGuard}, }; @@ -64,15 +65,22 @@ static VOICE_MODEL_SET: Lazy = Lazy::new(|| { /// /// 失敗したらパニックする fn get_all_models() -> Vec { - let root_dir = if let Some(root_dir) = env::var_os(ROOT_DIR_ENV_NAME) { + let root_dir: PathBuf = if let Some(root_dir) = env::var_os(ROOT_DIR_ENV_NAME) { root_dir.into() } else { - process_path::get_dylib_path() - .or_else(process_path::get_executable_path) - .unwrap() - .parent() - .unwrap_or_else(|| "".as_ref()) - .join("model") + #[cfg(target_family = "wasm")] + { + unimplemented!() + } + #[cfg(not(target_family = "wasm"))] + { + process_path::get_dylib_path() + .or_else(process_path::get_executable_path) + .unwrap() + .parent() + .unwrap_or_else(|| "".as_ref()) + .join("model") + } }; root_dir diff --git a/crates/voicevox_core_wasm_api/library.js b/crates/voicevox_core_c_api/wasm_library.js similarity index 100% rename from crates/voicevox_core_wasm_api/library.js rename to crates/voicevox_core_c_api/wasm_library.js diff --git a/crates/voicevox_core_wasm_api/Cargo.toml b/crates/voicevox_core_wasm_api/Cargo.toml deleted file mode 100644 index f9bcc7121..000000000 --- a/crates/voicevox_core_wasm_api/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "voicevox_core_wasm_api" -version.workspace = true -edition.workspace = true -publish.workspace = true -build = "build.rs" - -[lib] -crate-type = ["cdylib"] - -[dependencies] -voicevox_core.workspace = true -serde = { workspace = true, features = ["derive"] } -serde_json.workspace = true -base64 = "0.21.5" -tempfile.workspace = true -tracing-subscriber = { workspace = true, features = ["env-filter"] } -strum = { workspace = true, features = ["derive"] } -zip.workspace = true -chrono = { workspace = true, default-features = false, features = ["clock"] } -log.workspace = true -env_logger = "0.11.1" diff --git a/crates/voicevox_core_wasm_api/README.md b/crates/voicevox_core_wasm_api/README.md deleted file mode 100644 index 3ca604cea..000000000 --- a/crates/voicevox_core_wasm_api/README.md +++ /dev/null @@ -1,29 +0,0 @@ -# voicevox core wasm api - -- `EXPORTED_FUNCTIONS`足すのが面倒なので、`message`という関数を呼び出すようにしてる -- `message`は JSON を取って文字列を返す。 -- JSON の構造は web/src/voicevoxCore.ts を参照 - -## Rust 側準備 - -- onnxruntime の wasm ビルドが必要 - - https://onnxruntime.ai/docs/build/web.html - - `--build_wasm_static_lib`をつけてビルドする -- OpenJtalk 辞書を zip にしたやつが必要 - - src/openjtalk_dict.zip に配置 - -## Vite 側準備 - -- pnpm を使う(個人の好み) -- public/下に色々入れる - - sample.vvm - - voicevox_core_wasm_api.js - - voicevox_core_wasm_api.wasm - -## ビルド - -``` -export ORT_LIB_LOCATION=/home/sevenc7c/voicevox/wasm/onnxruntime/build/Linux/Release -cargo build --target wasm32-unknown-emscripten -p voicevox_core_wasm_api --release $@ -cp ./target/wasm32-unknown-emscripten/release/voicevox_core_wasm_api.{js,wasm} ./crates/voicevox_core_wasm_api/web/public -``` diff --git a/crates/voicevox_core_wasm_api/build.rs b/crates/voicevox_core_wasm_api/build.rs deleted file mode 100644 index 215ac9bc8..000000000 --- a/crates/voicevox_core_wasm_api/build.rs +++ /dev/null @@ -1,28 +0,0 @@ -fn main() { - println!("cargo:rustc-link-arg=--no-entry"); - println!("cargo:rustc-link-arg=-sERROR_ON_UNDEFINED_SYMBOLS=0"); - // TODO: WARNにしたいけど、これにするとemccがクラッシュする(どうして...) - // println!("cargo:rustc-link-arg=-sWARN_ON_UNDEFINED_SYMBOLS=1"); - println!("cargo:rustc-link-arg=-sEXPORTED_FUNCTIONS=['_message']"); - println!("cargo:rustc-link-arg=-sEXPORTED_RUNTIME_METHODS=['ccall']"); - println!("cargo:rustc-link-arg=-sEXPORT_NAME=\"RawVoicevoxCore\""); - println!("cargo:rustc-link-arg=-sMODULARIZE=1"); - println!("cargo:rustc-link-arg=-sALLOW_MEMORY_GROWTH=1"); - println!("cargo:rustc-link-arg=-sTOTAL_STACK=128MB"); - println!("cargo:rustc-link-arg=-sINITIAL_MEMORY=256MB"); - println!("cargo:rustc-link-arg=--js-library={}", std::env::var("CARGO_MANIFEST_DIR").unwrap() + "/library.js"); - let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()); - let target_dir = out_dir - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap(); - println!( - "cargo:rustc-link-arg=-o{}/voicevox_core_wasm_api.js", - target_dir.display() - ); - - println!("cargo:rustc-link-arg=-DEMSCRIPTEN_STANDALONE_WASM"); -} diff --git a/crates/voicevox_core_wasm_api/src/commands/mod.rs b/crates/voicevox_core_wasm_api/src/commands/mod.rs deleted file mode 100644 index 4de771286..000000000 --- a/crates/voicevox_core_wasm_api/src/commands/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub mod model; -pub mod synthesizer; -pub mod system; diff --git a/crates/voicevox_core_wasm_api/src/commands/model.rs b/crates/voicevox_core_wasm_api/src/commands/model.rs deleted file mode 100644 index 57f8b829b..000000000 --- a/crates/voicevox_core_wasm_api/src/commands/model.rs +++ /dev/null @@ -1,20 +0,0 @@ -use log::info; -use std::io::Write; -use voicevox_core::blocking as core; - -pub fn load(model: &[u8]) -> String { - let model_path = std::env::temp_dir().join(format!("model_{:x}.vvm", model.as_ptr() as usize)); - let mut file = std::fs::File::create(&model_path).unwrap(); - file.write_all(model).unwrap(); - let model = core::VoiceModel::from_path(model_path).unwrap(); - - let id = model.id().to_string(); - - let model_box = Box::new(model); - - let ptr = Box::into_raw(model_box) as usize; - - info!("Loaded model: {}@{:?}", id, ptr); - - ptr.to_string() -} diff --git a/crates/voicevox_core_wasm_api/src/commands/synthesizer.rs b/crates/voicevox_core_wasm_api/src/commands/synthesizer.rs deleted file mode 100644 index 90ef179b3..000000000 --- a/crates/voicevox_core_wasm_api/src/commands/synthesizer.rs +++ /dev/null @@ -1,40 +0,0 @@ -use base64::{engine::general_purpose, Engine as _}; -use log::info; -use voicevox_core::blocking as core; - -use crate::constants::get_dict_path; - -pub fn create() -> String { - let open_jtalk = core::OpenJtalk::new(get_dict_path()).unwrap(); - let synthesizer = core::Synthesizer::new(open_jtalk, &Default::default()).unwrap(); - - let synthesizer_box = Box::new(synthesizer); - - let ptr = Box::into_raw(synthesizer_box) as usize; - info!("Created Synthesizer: {:?}", ptr); - - ptr.to_string() -} - -pub fn load_model(synthesizer_ptr: usize, model_ptr: usize) -> String { - let synthesizer = unsafe { &mut *(synthesizer_ptr as *mut core::Synthesizer) }; - let model = unsafe { &*(model_ptr as *mut core::VoiceModel) }; - - synthesizer.load_voice_model(model).unwrap(); - - info!("Loaded model: {:?}", &model.id()); - - "".to_string() -} - -pub fn tts(synthesizer_ptr: usize, text: &str) -> String { - let synthesizer = unsafe { &mut *(synthesizer_ptr as *mut core::Synthesizer) }; - let metas = synthesizer.metas(); - let model_id = metas[0].styles()[0].id(); - - let result = synthesizer - .tts(text, *model_id, &Default::default()) - .unwrap(); - - general_purpose::STANDARD.encode(result) -} diff --git a/crates/voicevox_core_wasm_api/src/commands/system.rs b/crates/voicevox_core_wasm_api/src/commands/system.rs deleted file mode 100644 index 691f6960a..000000000 --- a/crates/voicevox_core_wasm_api/src/commands/system.rs +++ /dev/null @@ -1,35 +0,0 @@ -use crate::constants::get_dict_path; -use log::info; - -static OPEN_JTALK_DICT: &[u8] = include_bytes!("../openjtalk_dict.zip"); - -fn init_logger() { - std::env::set_var("RUST_LOG", "info"); - env_logger::Builder::from_default_env() - .target(env_logger::Target::Stderr) - .init(); - info!("Initialized logger"); -} - -fn init_dict() { - let dict_file = std::io::Cursor::new(OPEN_JTALK_DICT); - info!("Extracting openjtalk dict"); - zip::ZipArchive::new(dict_file) - .unwrap() - .extract(&get_dict_path()) - .unwrap(); -} - -pub fn init() -> String { - init_logger(); - - init_dict(); - - info!("Initialized"); - - "".to_string() -} - -pub fn get_version() -> String { - env!("CARGO_PKG_VERSION").to_string() -} diff --git a/crates/voicevox_core_wasm_api/src/constants.rs b/crates/voicevox_core_wasm_api/src/constants.rs deleted file mode 100644 index d3a4cdc9c..000000000 --- a/crates/voicevox_core_wasm_api/src/constants.rs +++ /dev/null @@ -1,5 +0,0 @@ -use std::path::PathBuf; - -pub fn get_dict_path() -> PathBuf { - std::env::temp_dir().join("openjtalk_dict") -} diff --git a/crates/voicevox_core_wasm_api/src/lib.rs b/crates/voicevox_core_wasm_api/src/lib.rs deleted file mode 100644 index b900da638..000000000 --- a/crates/voicevox_core_wasm_api/src/lib.rs +++ /dev/null @@ -1,71 +0,0 @@ -mod commands; -mod constants; -use base64::{engine::general_purpose, Engine as _}; -use log::info; -use serde::{Deserialize, Serialize}; -use std::ffi::{c_char, CStr, CString}; -use strum::Display; - -#[derive(Debug, Serialize, Deserialize, Display)] -#[serde(tag = "type")] -#[serde(rename_all = "camelCase")] -enum Message { - Init {}, - GetVersion {}, - ModelLoad { - payload: ModelLoadPayload, - }, - SynthesizerCreate {}, - SynthesizerLoadModel { - payload: SynthesizerLoadModelPayload, - }, - SynthesizerTts { - payload: SynthesizerTtsPayload, - }, -} - -#[derive(Debug, Serialize, Deserialize)] -struct ModelLoadPayload { - base64: String, -} - -#[derive(Debug, Serialize, Deserialize)] -struct SynthesizerLoadModelPayload { - synthesizer: usize, - model: usize, -} - -#[derive(Debug, Serialize, Deserialize)] -struct SynthesizerTtsPayload { - synthesizer: usize, - text: String, -} - -#[no_mangle] -pub fn message(message: *const c_char) -> *mut c_char { - std::env::set_var("RUST_BACKTRACE", "full"); - let message = unsafe { CStr::from_ptr(message as *mut c_char) }; - let message = message.to_str().unwrap().to_string(); - let message = serde_json::from_str::(&message).unwrap(); - info!("Received: {}", message); - let result = match message { - Message::Init {} => commands::system::init(), - Message::GetVersion {} => commands::system::get_version(), - Message::ModelLoad { payload } => commands::model::load( - &general_purpose::STANDARD - .decode(payload.base64) - .expect("base64 decode error"), - ), - Message::SynthesizerCreate {} => commands::synthesizer::create(), - Message::SynthesizerLoadModel { payload } => { - commands::synthesizer::load_model(payload.synthesizer, payload.model) - } - Message::SynthesizerTts { payload } => { - commands::synthesizer::tts(payload.synthesizer, &payload.text) - } - }; - info!("Done."); - let result = CString::new(result).unwrap(); - - result.into_raw() -} diff --git a/crates/voicevox_core_wasm_api/web/src/voicevoxCore.ts b/crates/voicevox_core_wasm_api/web/src/voicevoxCore.ts deleted file mode 100644 index 836f888ff..000000000 --- a/crates/voicevox_core_wasm_api/web/src/voicevoxCore.ts +++ /dev/null @@ -1,95 +0,0 @@ -let core: undefined | any = undefined; - -declare const RawVoicevoxCore: () => Promise; -RawVoicevoxCore().then(async (m: any) => { - console.log("WASM loaded"); - core = m; - await message("init", {}); - console.log("Initialized"); -}); - -type Messages = { - init: {}; - getVersion: {}; - modelLoad: { - base64: string; - }; - synthesizerCreate: {}; - synthesizerLoadModel: { - synthesizer: number; - model: number; - }; - synthesizerTts: { - synthesizer: number; - text: string; - }; -}; - -async function message( - type: T, - payload: Messages[T] -): Promise { - if (core === undefined) { - throw new Error("core is not loaded"); - } - console.log("Sending message:", type); - const result = await core.ccall( - "message", - "string", - ["string"], - [ - JSON.stringify({ - type, - payload, - }), - ] - ); - console.log("Received."); - return result; -} - -export async function init(): Promise { - return message("init", {}); -} - -export async function getVersion(): Promise { - return message("getVersion", {}); -} - -export class Model { - pointer: number; - constructor(pointer: number) { - this.pointer = pointer; - } - - static async load(base64: string): Promise { - const pointer = await message("modelLoad", { base64 }); - return new Model(parseInt(pointer)); - } -} - -export class Synthesizer { - pointer: number; - constructor(pointer: number) { - this.pointer = pointer; - } - - static async create(): Promise { - const pointer = await message("synthesizerCreate", {}); - return new Synthesizer(parseInt(pointer)); - } - - async loadModel(model: Model): Promise { - await message("synthesizerLoadModel", { - synthesizer: this.pointer, - model: model.pointer, - }); - } - - async tts(text: string): Promise { - return message("synthesizerTts", { - synthesizer: this.pointer, - text, - }); - } -} diff --git a/crates/voicevox_core_wasm_api/web/vite.config.ts b/crates/voicevox_core_wasm_api/web/vite.config.ts deleted file mode 100644 index e18795165..000000000 --- a/crates/voicevox_core_wasm_api/web/vite.config.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { defineConfig } from "vite"; -import vue from "@vitejs/plugin-vue"; -import fs from "node:fs/promises"; - -// https://vitejs.dev/config/ -export default defineConfig(async () => { - const releaseFiles = await fs.readdir( - "../../../target/wasm32-unknown-emscripten/release" - ); - const wasmFile = releaseFiles.find((file) => file.endsWith(".wasm")); - const jsFile = releaseFiles.find((file) => file.endsWith(".js")); - const wasmPath = `../../../target/wasm32-unknown-emscripten/release/${wasmFile}`; - const jsPath = `../../../target/wasm32-unknown-emscripten/release/${jsFile}`; - await fs.copyFile(wasmPath, "./public/voicevox_core_wasm_api.wasm"); - await fs.copyFile(jsPath, "./public/voicevox_core_wasm_api.js"); - - return { plugins: [vue()] }; -}); diff --git a/crates/voicevox_core_wasm_api/web/.gitignore b/example/wasm/.gitignore similarity index 85% rename from crates/voicevox_core_wasm_api/web/.gitignore rename to example/wasm/.gitignore index a547bf36d..6dce82b68 100644 --- a/crates/voicevox_core_wasm_api/web/.gitignore +++ b/example/wasm/.gitignore @@ -22,3 +22,6 @@ dist-ssr *.njsproj *.sln *.sw? + +src/artifacts/*.mjs +src/artifacts/*.wasm diff --git a/example/wasm/README.md b/example/wasm/README.md new file mode 100644 index 000000000..ef72fd524 --- /dev/null +++ b/example/wasm/README.md @@ -0,0 +1,18 @@ +# Vue 3 + TypeScript + Vite + +This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 ` diff --git a/crates/voicevox_core_wasm_api/web/package.json b/example/wasm/package.json similarity index 100% rename from crates/voicevox_core_wasm_api/web/package.json rename to example/wasm/package.json diff --git a/crates/voicevox_core_wasm_api/web/pnpm-lock.yaml b/example/wasm/pnpm-lock.yaml similarity index 100% rename from crates/voicevox_core_wasm_api/web/pnpm-lock.yaml rename to example/wasm/pnpm-lock.yaml diff --git a/crates/voicevox_core_wasm_api/web/src/App.vue b/example/wasm/src/App.vue similarity index 100% rename from crates/voicevox_core_wasm_api/web/src/App.vue rename to example/wasm/src/App.vue diff --git a/example/wasm/src/artifacts/voicevox_core_wasm_api.d.ts b/example/wasm/src/artifacts/voicevox_core_wasm_api.d.ts new file mode 100644 index 000000000..42076ddea --- /dev/null +++ b/example/wasm/src/artifacts/voicevox_core_wasm_api.d.ts @@ -0,0 +1,18 @@ +declare module "voicevox_core_wasm_api"; +type Functions = { + voicevox_get_version: () => string; +}; +type Ccall = ( + name: T, + returnType: ReturnType, + argTypes: ("number" | "string" | "array" | "boolean")[], + args: Parameters +) => ReturnType; +export type VoicevoxCore = { + ccall: Ccall; +}; +export default function (): Promise< + VoicevoxCore & { + ready: Promise; + } +>; diff --git a/crates/voicevox_core_wasm_api/web/src/assets/vue.svg b/example/wasm/src/assets/vue.svg similarity index 100% rename from crates/voicevox_core_wasm_api/web/src/assets/vue.svg rename to example/wasm/src/assets/vue.svg diff --git a/crates/voicevox_core_wasm_api/web/src/components/HelloWorld.vue b/example/wasm/src/components/HelloWorld.vue similarity index 100% rename from crates/voicevox_core_wasm_api/web/src/components/HelloWorld.vue rename to example/wasm/src/components/HelloWorld.vue diff --git a/crates/voicevox_core_wasm_api/web/src/main.ts b/example/wasm/src/main.ts similarity index 100% rename from crates/voicevox_core_wasm_api/web/src/main.ts rename to example/wasm/src/main.ts diff --git a/crates/voicevox_core_wasm_api/web/src/style.css b/example/wasm/src/style.css similarity index 100% rename from crates/voicevox_core_wasm_api/web/src/style.css rename to example/wasm/src/style.css diff --git a/crates/voicevox_core_wasm_api/web/src/vite-env.d.ts b/example/wasm/src/vite-env.d.ts similarity index 100% rename from crates/voicevox_core_wasm_api/web/src/vite-env.d.ts rename to example/wasm/src/vite-env.d.ts diff --git a/example/wasm/src/voicevoxCore.ts b/example/wasm/src/voicevoxCore.ts new file mode 100644 index 000000000..2d55c0aa1 --- /dev/null +++ b/example/wasm/src/voicevoxCore.ts @@ -0,0 +1,28 @@ +import raw, { VoicevoxCore } from "./artifacts/voicevox_core_wasm_api"; + +let _voicevoxCore: VoicevoxCore | undefined; +let resolvers: ((vvc: VoicevoxCore) => void)[] = []; +const voicevoxCore = (): Promise => { + if (_voicevoxCore) { + return Promise.resolve(_voicevoxCore); + } + return new Promise((resolve) => { + resolvers.push(resolve); + }); +}; +raw().then((vvc) => { + vvc.ready.then(() => { + console.log("VoicevoxCore is ready"); + _voicevoxCore = vvc; + for (const resolve of resolvers) { + resolve(vvc); + } + }); +}); +export async function getVersion() { + const vvc = await voicevoxCore(); + return vvc.ccall("voicevox_get_version", "string", [], []); +} + +export class Synthesizer {} +export class Model {} diff --git a/crates/voicevox_core_wasm_api/web/tsconfig.json b/example/wasm/tsconfig.json similarity index 100% rename from crates/voicevox_core_wasm_api/web/tsconfig.json rename to example/wasm/tsconfig.json diff --git a/crates/voicevox_core_wasm_api/web/tsconfig.node.json b/example/wasm/tsconfig.node.json similarity index 100% rename from crates/voicevox_core_wasm_api/web/tsconfig.node.json rename to example/wasm/tsconfig.node.json diff --git a/example/wasm/vite.config.ts b/example/wasm/vite.config.ts new file mode 100644 index 000000000..ee3a2506e --- /dev/null +++ b/example/wasm/vite.config.ts @@ -0,0 +1,8 @@ +import { defineConfig } from "vite"; +import vue from "@vitejs/plugin-vue"; +import fs from "node:fs/promises"; + +// https://vitejs.dev/config/ +export default defineConfig(async () => { + return { plugins: [vue()] }; +});