From ce0904511c32c9e39f542e159149e145ecf67ec0 Mon Sep 17 00:00:00 2001 From: Ryo Yamashita Date: Thu, 16 Nov 2023 22:30:12 +0900 Subject: [PATCH] =?UTF-8?q?Rust=20API=E3=81=AEdoctest=E3=81=AE=E3=83=9C?= =?UTF-8?q?=E3=82=A4=E3=83=A9=E3=83=BC=E3=83=97=E3=83=AC=E3=83=BC=E3=83=88?= =?UTF-8?q?=E3=82=92fixture=E3=81=A8=E3=81=97=E3=81=A6=E5=88=86=E9=9B=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/voicevox_core/src/__internal.rs | 4 +- .../src/__internal/doctest_fixtures.rs | 24 ++++ crates/voicevox_core/src/synthesizer.rs | 120 +++--------------- 3 files changed, 47 insertions(+), 101 deletions(-) create mode 100644 crates/voicevox_core/src/__internal/doctest_fixtures.rs diff --git a/crates/voicevox_core/src/__internal.rs b/crates/voicevox_core/src/__internal.rs index ee9ce212e..ff9f5ce3c 100644 --- a/crates/voicevox_core/src/__internal.rs +++ b/crates/voicevox_core/src/__internal.rs @@ -1,4 +1,6 @@ -//! VOICEVOX CORE内のラッパー向けの実装 +pub mod doctest_fixtures; + +// VOICEVOX CORE内のラッパー向けの実装 // FIXME: 要議論: https://github.com/VOICEVOX/voicevox_core/issues/595 pub fn to_zenkaku(surface: &str) -> String { diff --git a/crates/voicevox_core/src/__internal/doctest_fixtures.rs b/crates/voicevox_core/src/__internal/doctest_fixtures.rs new file mode 100644 index 000000000..9df517720 --- /dev/null +++ b/crates/voicevox_core/src/__internal/doctest_fixtures.rs @@ -0,0 +1,24 @@ +use std::{path::Path, sync::Arc}; + +use crate::{AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel}; + +pub async fn synthesizer_with_sample_voice_model( + open_jtalk_dic_dir: impl AsRef, +) -> anyhow::Result { + let syntesizer = Synthesizer::new( + Arc::new(OpenJtalk::new(open_jtalk_dic_dir).unwrap()), + &InitializeOptions { + acceleration_mode: AccelerationMode::Cpu, + ..Default::default() + }, + )?; + + let model = &VoiceModel::from_path(concat!( + env!("CARGO_MANIFEST_DIR"), + "/../../model/sample.vvm", + )) + .await?; + syntesizer.load_voice_model(model).await?; + + Ok(syntesizer) +} diff --git a/crates/voicevox_core/src/synthesizer.rs b/crates/voicevox_core/src/synthesizer.rs index 594cfd856..47ac0997a 100644 --- a/crates/voicevox_core/src/synthesizer.rs +++ b/crates/voicevox_core/src/synthesizer.rs @@ -251,35 +251,15 @@ impl Synthesizer { #[cfg_attr(not(windows), doc = "```")] /// # #[tokio::main] /// # async fn main() -> anyhow::Result<()> { - /// # let syntesizer = { - /// # use std::sync::Arc; - /// # - /// # use test_util::OPEN_JTALK_DIC_DIR; - /// # use voicevox_core::{ - /// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel, - /// # }; - /// # - /// # let mut syntesizer = Synthesizer::new( - /// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()), - /// # &InitializeOptions { - /// # acceleration_mode: AccelerationMode::Cpu, - /// # ..Default::default() - /// # }, - /// # )?; - /// # - /// # let model = &VoiceModel::from_path(concat!( - /// # env!("CARGO_MANIFEST_DIR"), - /// # "/../../model/sample.vvm", - /// # )) + /// # let synthesizer = + /// # voicevox_core::__internal::doctest_fixtures::synthesizer_with_sample_voice_model( + /// # test_util::OPEN_JTALK_DIC_DIR, + /// # ) /// # .await?; - /// # syntesizer.load_voice_model(model).await?; - /// # - /// # syntesizer - /// # }; /// # /// use voicevox_core::StyleId; /// - /// let accent_phrases = syntesizer + /// let accent_phrases = synthesizer /// .create_accent_phrases_from_kana("コンニチワ'", StyleId::new(302)) /// .await?; /// # @@ -304,35 +284,15 @@ impl Synthesizer { #[cfg_attr(not(windows), doc = "```")] /// # #[tokio::main] /// # async fn main() -> anyhow::Result<()> { - /// # let syntesizer = { - /// # use std::sync::Arc; - /// # - /// # use test_util::OPEN_JTALK_DIC_DIR; - /// # use voicevox_core::{ - /// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel, - /// # }; - /// # - /// # let mut syntesizer = Synthesizer::new( - /// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()), - /// # &InitializeOptions { - /// # acceleration_mode: AccelerationMode::Cpu, - /// # ..Default::default() - /// # }, - /// # )?; - /// # - /// # let model = &VoiceModel::from_path(concat!( - /// # env!("CARGO_MANIFEST_DIR"), - /// # "/../../model/sample.vvm", - /// # )) + /// # let synthesizer = + /// # voicevox_core::__internal::doctest_fixtures::synthesizer_with_sample_voice_model( + /// # test_util::OPEN_JTALK_DIC_DIR, + /// # ) /// # .await?; - /// # syntesizer.load_voice_model(model).await?; - /// # - /// # syntesizer - /// # }; /// # /// use voicevox_core::StyleId; /// - /// let accent_phrases = syntesizer + /// let accent_phrases = synthesizer /// .create_accent_phrases("こんにちは", StyleId::new(302)) /// .await?; /// # @@ -393,35 +353,15 @@ impl Synthesizer { #[cfg_attr(not(windows), doc = "```")] /// # #[tokio::main] /// # async fn main() -> anyhow::Result<()> { - /// # let syntesizer = { - /// # use std::sync::Arc; - /// # - /// # use test_util::OPEN_JTALK_DIC_DIR; - /// # use voicevox_core::{ - /// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel, - /// # }; - /// # - /// # let mut syntesizer = Synthesizer::new( - /// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()), - /// # &InitializeOptions { - /// # acceleration_mode: AccelerationMode::Cpu, - /// # ..Default::default() - /// # }, - /// # )?; - /// # - /// # let model = &VoiceModel::from_path(concat!( - /// # env!("CARGO_MANIFEST_DIR"), - /// # "/../../model/sample.vvm", - /// # )) + /// # let synthesizer = + /// # voicevox_core::__internal::doctest_fixtures::synthesizer_with_sample_voice_model( + /// # test_util::OPEN_JTALK_DIC_DIR, + /// # ) /// # .await?; - /// # syntesizer.load_voice_model(model).await?; - /// # - /// # syntesizer - /// # }; /// # /// use voicevox_core::StyleId; /// - /// let audio_query = syntesizer + /// let audio_query = synthesizer /// .audio_query_from_kana("コンニチワ'", StyleId::new(302)) /// .await?; /// # @@ -447,35 +387,15 @@ impl Synthesizer { #[cfg_attr(not(windows), doc = "```")] /// # #[tokio::main] /// # async fn main() -> anyhow::Result<()> { - /// # let syntesizer = { - /// # use std::sync::Arc; - /// # - /// # use test_util::OPEN_JTALK_DIC_DIR; - /// # use voicevox_core::{ - /// # AccelerationMode, InitializeOptions, OpenJtalk, Synthesizer, VoiceModel, - /// # }; - /// # - /// # let mut syntesizer = Synthesizer::new( - /// # Arc::new(OpenJtalk::new(OPEN_JTALK_DIC_DIR).unwrap()), - /// # &InitializeOptions { - /// # acceleration_mode: AccelerationMode::Cpu, - /// # ..Default::default() - /// # }, - /// # )?; - /// # - /// # let model = &VoiceModel::from_path(concat!( - /// # env!("CARGO_MANIFEST_DIR"), - /// # "/../../model/sample.vvm", - /// # )) + /// # let synthesizer = + /// # voicevox_core::__internal::doctest_fixtures::synthesizer_with_sample_voice_model( + /// # test_util::OPEN_JTALK_DIC_DIR, + /// # ) /// # .await?; - /// # syntesizer.load_voice_model(model).await?; - /// # - /// # syntesizer - /// # }; /// # /// use voicevox_core::StyleId; /// - /// let audio_query = syntesizer + /// let audio_query = synthesizer /// .audio_query("こんにちは", StyleId::new(302)) /// .await?; /// #