Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: rework VoiceModel #830

Merged
merged 18 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 106 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ anstream = { version = "0.5.0", default-features = false }
anstyle-query = "1.0.0"
anyhow = "1.0.65"
assert_cmd = "2.0.8"
async-fs = "2.1.2"
async_zip = "=0.0.16"
bindgen = "0.69.4"
binstall-tar = "0.4.39"
Expand All @@ -33,10 +34,10 @@ enum-map = "3.0.0-beta.1"
eyre = "0.6.8"
flate2 = "1.0.25"
fs-err = "2.11.0"
futures = "0.3.26"
futures-core = "0.3.25"
futures-util = "0.3.25"
futures-lite = "2.2.0"
futures-io = "0.3.28"
heck = "0.4.1"
humansize = "2.1.2"
indexmap = "2.0.0"
Expand Down
7 changes: 4 additions & 3 deletions crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ link-onnxruntime = []

[dependencies]
anyhow.workspace = true
async-fs.workspace = true
async_zip = { workspace = true, features = ["deflate"] }
camino.workspace = true
const_format.workspace = true
Expand All @@ -27,14 +28,15 @@ easy-ext.workspace = true
educe.workspace = true
enum-map.workspace = true
fs-err = { workspace = true, features = ["tokio"] }
futures.workspace = true
futures-io.workspace = true
futures-lite.workspace = true
futures-util = { workspace = true, features = ["io"] }
indexmap = { workspace = true, features = ["serde"] }
itertools.workspace = true
jlabel.workspace = true
ndarray.workspace = true
open_jtalk.workspace = true
ouroboros.workspace = true
rayon.workspace = true
ref-cast.workspace = true
regex.workspace = true
serde = { workspace = true, features = ["derive", "rc"] }
Expand All @@ -49,7 +51,6 @@ tracing.workspace = true
uuid = { workspace = true, features = ["v4", "serde"] }
voicevox-ort = { workspace = true, features = ["download-binaries", "__init-for-voicevox"] }
voicevox_core_macros = { path = "../voicevox_core_macros" }
zip.workspace = true

[dev-dependencies]
heck.workspace = true
Expand Down
54 changes: 54 additions & 0 deletions crates/voicevox_core/src/asyncs.rs
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use std::{
io::{self, Read as _, Seek as _, SeekFrom},
path::Path,
pin::Pin,
task::{self, Poll},
};

use futures_io::{AsyncRead, AsyncSeek};

pub(crate) trait Async: 'static {
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin>;
}

/// "async"としての責務を放棄し、すべてをブロックする。
pub(crate) enum Unstoppable {}

impl Async for Unstoppable {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin> {
return std::fs::File::open(path).map(UnstoppableFile);

struct UnstoppableFile(std::fs::File);

impl AsyncRead for UnstoppableFile {
fn poll_read(
mut self: Pin<&mut Self>,
_: &mut task::Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
Poll::Ready(self.0.read(buf))
}
}

impl AsyncSeek for UnstoppableFile {
fn poll_seek(
mut self: Pin<&mut Self>,
_: &mut task::Context<'_>,
pos: SeekFrom,
) -> Poll<io::Result<u64>> {
Poll::Ready(self.0.seek(pos))
}
}
}
}

/// [blocking]クレートで駆動する。
///
/// [blocking](https://docs.rs/crate/blocking)
pub(crate) enum SmolBlocking {}

impl Async for SmolBlocking {
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved
async fn open_file(path: impl AsRef<Path>) -> io::Result<impl AsyncRead + AsyncSeek + Unpin> {
async_fs::File::open(path).await
}
}
56 changes: 56 additions & 0 deletions crates/voicevox_core/src/infer/domains.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,70 @@
mod talk;

use std::future::Future;

use educe::Educe;
use serde::{Deserialize, Deserializer};

pub(crate) use self::talk::{
DecodeInput, DecodeOutput, PredictDurationInput, PredictDurationOutput, PredictIntonationInput,
PredictIntonationOutput, TalkDomain, TalkOperation,
};

#[derive(Educe)]
// TODO: `bounds`に`V: ?Sized`も入れようとすると、よくわからない理由で弾かれる。最新版のeduce
// でもそうなのか?また最新版でも駄目だとしたら、弾いている理由は何なのか?
#[educe(Clone(bound = "V: InferenceDomainMapValues, V::Talk: Clone"))]
pub(crate) struct InferenceDomainMap<V: InferenceDomainMapValues + ?Sized> {
pub(crate) talk: V::Talk,
}

impl<T> InferenceDomainMap<(T,)> {
pub(crate) fn each_ref(&self) -> InferenceDomainMap<(&T,)> {
InferenceDomainMap { talk: &self.talk }
}

pub(crate) fn map<T2, Ft: FnOnce(T) -> T2>(
self,
fs: InferenceDomainMap<(Ft,)>,
) -> InferenceDomainMap<(T2,)> {
InferenceDomainMap {
talk: (fs.talk)(self.talk),
}
}
}

impl<T, E> InferenceDomainMap<(Result<T, E>,)> {
pub(crate) fn collect(self) -> Result<InferenceDomainMap<(T,)>, E> {
let talk = self.talk?;
Ok(InferenceDomainMap { talk })
}
}

impl<T: Future> InferenceDomainMap<(T,)> {
pub(crate) async fn join(self) -> InferenceDomainMap<(T::Output,)> {
let talk = self.talk.await;
InferenceDomainMap { talk }
}
}
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

impl<'de, V: InferenceDomainMapValues + ?Sized> Deserialize<'de> for InferenceDomainMap<V>
where
V::Talk: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let Repr { talk } = Repr::deserialize(deserializer)?;
return Ok(Self { talk });

#[derive(Deserialize)]
struct Repr<T> {
talk: T,
}
}
}

pub(crate) trait InferenceDomainMapValues {
type Talk;
}
Expand Down
1 change: 1 addition & 0 deletions crates/voicevox_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const _: () = {
);
};

mod asyncs;
mod devices;
/// cbindgen:ignore
mod engine;
Expand Down
Loading
Loading