From d05235ae891d689cf3a4820f982d37373114da13 Mon Sep 17 00:00:00 2001 From: mc_fdc Date: Fri, 7 Apr 2023 16:32:00 +0000 Subject: [PATCH] =?UTF-8?q?=E3=81=93=E3=81=93=E3=81=BE=E3=81=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 3 +++ src/client.rs | 13 ++++++++++++ src/lib.rs | 2 ++ src/restapi.rs | 44 ++++++++++++++++++++++++++++++++++++++++ src/types/audio_query.rs | 22 ++++++++++++++++++++ src/types/mod.rs | 1 + 6 files changed, 85 insertions(+) create mode 100644 src/client.rs create mode 100644 src/restapi.rs create mode 100644 src/types/audio_query.rs create mode 100644 src/types/mod.rs diff --git a/Cargo.toml b/Cargo.toml index efa8eda..cf6ff60 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +bytes = "1.4.0" +reqwest = { version = "0.11.16", features = ["json"] } +serde = { version = "1.0.159", features = ["derive"] } diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..6b7af9e --- /dev/null +++ b/src/client.rs @@ -0,0 +1,13 @@ +use crate::restapi::RestAPI; + +pub struct Client { + restapi: RestAPI, +} + +impl Client { + pub fn new(base_path: String) -> Self { + Self { + restapi: RestAPI::new(base_path), + } + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 7d12d9a..281d2af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +mod types; + pub fn add(left: usize, right: usize) -> usize { left + right } diff --git a/src/restapi.rs b/src/restapi.rs new file mode 100644 index 0000000..481d336 --- /dev/null +++ b/src/restapi.rs @@ -0,0 +1,44 @@ +use reqwest::{Client, RequestBuilder, Result}; +use crate::types::audio_query::AudioQueryType; +use bytes::Bytes; + +pub struct RestAPI { + base_path: String, + client: Client, +} + +impl RestAPI { + pub fn new(base_path: String) -> Self { + Self { + base_path, + client: Client::new(), + } + } + + pub fn request(&self, method: &str, path: &str) -> RequestBuilder { + self.client + .request(method.parse().unwrap(), &format!("{}{}", self.base_path, path)) + } + + pub async fn create_audio_query(&self, text: &str, core_version: Option<&str>) -> Result { + let mut params = vec![("text", text)]; + if let Some(core_version) = core_version { + params.push(("core_version", core_version)) + } + self.request("POST", "/audio_query") + .param(¶ms) + .send() + .await? + .json() + .await? + } + + pub async fn synthesis(&self, audio_query: &AudioQueryType) -> Result { + self.request("POST", "/synthesis") + .json(audio_query) + .send() + .await? + .bytes() + .await? + } +} \ No newline at end of file diff --git a/src/types/audio_query.rs b/src/types/audio_query.rs new file mode 100644 index 0000000..954303d --- /dev/null +++ b/src/types/audio_query.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct AudioQueryType { + #[serde(rename = "speedScale")] + speed_scale: i32, + #[serde(rename = "pitchScale")] + pitch_scale: i32, + #[serde(rename = "intonationScale")] + intonation_scale: i32, + #[serde(rename = "volumeScale")] + volume_scale: i32, + #[serde(rename = "prePhonemeLength")] + pre_phoneme_length: i32, + #[serde(rename = "postPhonemeLength")] + post_phoneme_length: i32, + #[serde(rename = "outputSamplingRate")] + output_sampling_rate: i32, + #[serde(rename = "outputStereo")] + output_stereo: bool, + kana: String, +} diff --git a/src/types/mod.rs b/src/types/mod.rs new file mode 100644 index 0000000..f938474 --- /dev/null +++ b/src/types/mod.rs @@ -0,0 +1 @@ +mod audio_query;