mirror of
https://github.com/neodyland/sbv2-api.git
synced 2026-05-18 14:50:38 +00:00
add python binding
This commit is contained in:
@@ -11,9 +11,7 @@ def main():
|
||||
|
||||
style_vector = model.get_style_vector("amitaro", 0, 1.0)
|
||||
with open("output.wav", "wb") as f:
|
||||
data = model.synthesize("こんにちは", "amitaro", style_vector, 0.0, 0.5)
|
||||
print(data)
|
||||
f.write(data)
|
||||
f.write(model.synthesize("おはようございます。", "amitaro", style_vector, 0.0, 0.5))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -2,16 +2,10 @@ use pyo3::prelude::*;
|
||||
mod sbv2;
|
||||
pub mod style;
|
||||
|
||||
/// Formats the sum of two numbers as string.
|
||||
#[pyfunction]
|
||||
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
|
||||
Ok((a + b).to_string())
|
||||
}
|
||||
|
||||
/// A Python module implemented in Rust.
|
||||
/// sbv2 bindings module
|
||||
#[pymodule]
|
||||
fn sbv2_bindings(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
|
||||
m.add_class::<sbv2::TTSModel>()?;
|
||||
m.add_class::<style::StyleVector>()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -6,6 +6,16 @@ use crate::style::StyleVector;
|
||||
|
||||
use std::fs;
|
||||
|
||||
/// TTSModel class
|
||||
///
|
||||
/// 音声合成するために使うクラス
|
||||
///
|
||||
/// Parameters
|
||||
/// ----------
|
||||
/// bert_model_bytes : bytes
|
||||
/// BERTモデルのバイナリデータ
|
||||
/// tokenizer_bytes : bytes
|
||||
/// トークナイザーのバイナリデータ
|
||||
#[pyclass]
|
||||
pub struct TTSModel {
|
||||
pub model: TTSModelHolder,
|
||||
@@ -20,26 +30,66 @@ impl TTSModel {
|
||||
})
|
||||
}
|
||||
|
||||
/// パスからTTSModelインスタンスを生成する
|
||||
///
|
||||
/// Parameters
|
||||
/// ----------
|
||||
/// bert_model_path : str
|
||||
/// BERTモデルのパス
|
||||
/// tokenizer_path : str
|
||||
/// トークナイザーのパス
|
||||
#[staticmethod]
|
||||
fn from_path(bert_model_path: String, tokenizer_path: String) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
model: TTSModelHolder::new(
|
||||
fs::read(bert_model_path)?,
|
||||
fs::read(tokenizer_path)?,
|
||||
)?,
|
||||
model: TTSModelHolder::new(fs::read(bert_model_path)?, fs::read(tokenizer_path)?)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// SBV2ファイルを読み込む
|
||||
///
|
||||
/// Parameters
|
||||
/// ----------
|
||||
/// ident : str
|
||||
/// 識別子
|
||||
/// sbv2file_bytes : bytes
|
||||
/// SBV2ファイルのバイナリデータ
|
||||
fn load_sbv2file(&mut self, ident: String, sbv2file_bytes: Vec<u8>) -> anyhow::Result<()> {
|
||||
self.model.load_sbv2file(ident, sbv2file_bytes)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_sbv2file_from_path(&mut self, ident: String, sbv2file_path: String) -> anyhow::Result<()> {
|
||||
/// パスからSBV2ファイルを読み込む
|
||||
///
|
||||
/// Parameters
|
||||
/// ----------
|
||||
/// ident : str
|
||||
/// 識別子
|
||||
/// sbv2file_path : str
|
||||
/// SBV2ファイルのパス
|
||||
fn load_sbv2file_from_path(
|
||||
&mut self,
|
||||
ident: String,
|
||||
sbv2file_path: String,
|
||||
) -> anyhow::Result<()> {
|
||||
self.model.load_sbv2file(ident, fs::read(sbv2file_path)?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// スタイルベクトルを取得する
|
||||
///
|
||||
/// Parameters
|
||||
/// ----------
|
||||
/// ident : str
|
||||
/// 識別子
|
||||
/// style_id : int
|
||||
/// スタイルID
|
||||
/// weight : float
|
||||
/// 重み
|
||||
///
|
||||
/// Returns
|
||||
/// -------
|
||||
/// style_vector : StyleVector
|
||||
/// スタイルベクトル
|
||||
fn get_style_vector(
|
||||
&self,
|
||||
ident: String,
|
||||
@@ -51,6 +101,25 @@ impl TTSModel {
|
||||
))
|
||||
}
|
||||
|
||||
/// テキストから音声を合成する
|
||||
///
|
||||
/// Parameters
|
||||
/// ----------
|
||||
/// text : str
|
||||
/// テキスト
|
||||
/// ident : str
|
||||
/// 識別子
|
||||
/// style_vector : StyleVector
|
||||
/// スタイルベクトル
|
||||
/// sdp_ratio : float
|
||||
/// SDP比率
|
||||
/// length_scale : float
|
||||
/// 音声の長さのスケール
|
||||
///
|
||||
/// Returns
|
||||
/// -------
|
||||
/// voice_data : bytes
|
||||
/// 音声データ
|
||||
fn synthesize<'p>(
|
||||
&'p self,
|
||||
py: Python<'p>,
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use ndarray::Array1;
|
||||
use pyo3::prelude::*;
|
||||
|
||||
/// StyleVector class
|
||||
///
|
||||
/// スタイルベクトルを表すクラス
|
||||
#[pyclass]
|
||||
#[derive(Clone)]
|
||||
pub struct StyleVector(Array1<f32>);
|
||||
|
||||
Reference in New Issue
Block a user