ここまで

This commit is contained in:
mc_fdc
2023-04-07 16:32:00 +00:00
parent 391fb34df1
commit d05235ae89
6 changed files with 85 additions and 0 deletions

View File

@ -6,3 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
bytes = "1.4.0"
reqwest = { version = "0.11.16", features = ["json"] }
serde = { version = "1.0.159", features = ["derive"] }

13
src/client.rs Normal file
View File

@ -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),
}
}
}

View File

@ -1,3 +1,5 @@
mod types;
pub fn add(left: usize, right: usize) -> usize { pub fn add(left: usize, right: usize) -> usize {
left + right left + right
} }

44
src/restapi.rs Normal file
View File

@ -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<AudioQueryType> {
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(&params)
.send()
.await?
.json()
.await?
}
pub async fn synthesis(&self, audio_query: &AudioQueryType) -> Result<Bytes> {
self.request("POST", "/synthesis")
.json(audio_query)
.send()
.await?
.bytes()
.await?
}
}

22
src/types/audio_query.rs Normal file
View File

@ -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,
}

1
src/types/mod.rs Normal file
View File

@ -0,0 +1 @@
mod audio_query;