Skip to content

Commit

Permalink
Merge branch 'main' into change-liberate-voicevox-core
Browse files Browse the repository at this point in the history
 #891 で入れたtodoも解決する。
  • Loading branch information
qryxip committed Dec 12, 2024
2 parents c5617c8 + 5c01e9c commit 9fde4e7
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 144 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/voicevox_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ camino.workspace = true
const_format.workspace = true
derive-getters.workspace = true
derive-new.workspace = true
derive_more = { workspace = true, features = ["add", "deref", "display", "from", "from_str"] }
derive_more = { workspace = true, features = ["add", "deref", "display", "from", "from_str", "index"] }
duplicate.workspace = true
easy-ext.workspace = true
educe.workspace = true
enum-map.workspace = true
enum-map = { workspace = true, features = ["serde"] }
fs-err.workspace = true
futures-io.workspace = true
futures-lite.workspace = true
Expand Down
4 changes: 3 additions & 1 deletion crates/voicevox_core/src/infer/domains/talk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{collections::BTreeSet, sync::LazyLock};
use enum_map::Enum;
use macros::{InferenceInputSignature, InferenceOperation, InferenceOutputSignature};
use ndarray::{Array0, Array1, Array2};
use serde::Deserialize;

use crate::{manifest::TalkManifest, StyleType};

Expand All @@ -23,7 +24,8 @@ impl InferenceDomain for TalkDomain {
}
}

#[derive(Clone, Copy, Enum, InferenceOperation)]
#[derive(Clone, Copy, Deserialize, Enum, InferenceOperation)]
#[serde(rename_all = "snake_case")]
#[inference_operation(
type Domain = TalkDomain;
)]
Expand Down
21 changes: 6 additions & 15 deletions crates/voicevox_core/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{
};

use derive_getters::Getters;
use derive_more::Deref;
use derive_more::{Deref, Index};
use derive_new::new;
use macros::IndexForFields;
use enum_map::EnumMap;
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_with::{serde_as, DisplayFromStr};

Expand Down Expand Up @@ -81,21 +81,12 @@ pub struct Manifest {

pub(crate) type ManifestDomains = inference_domain_map_values!(for<D> Option<D::Manifest>);

#[derive(Deserialize, IndexForFields)]
#[derive(Index, Deserialize)]
#[cfg_attr(test, derive(Default))]
#[index_for_fields(TalkOperation)]
pub(crate) struct TalkManifest {
#[index_for_fields(TalkOperation::PredictDuration)]
pub(crate) predict_duration: ModelFile,

#[index_for_fields(TalkOperation::PredictIntonation)]
pub(crate) predict_intonation: ModelFile,

#[index_for_fields(TalkOperation::GenerateFullIntermediate)]
pub(crate) generate_full_intermediate: ModelFile,

#[index_for_fields(TalkOperation::RenderAudioSegment)]
pub(crate) render_audio_segment: ModelFile,
#[index]
#[serde(flatten)]
filenames: EnumMap<TalkOperation, ModelFile>,

#[serde(default)]
pub(crate) style_id_to_inner_voice_id: StyleIdToInnerVoiceId,
Expand Down
47 changes: 26 additions & 21 deletions crates/voicevox_core/src/voice_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{
use anyhow::{anyhow, Context as _};
use derive_more::From;
use easy_ext::ext;
use enum_map::{enum_map, EnumMap};
use enum_map::{Enum, EnumMap};
use futures_io::{AsyncBufRead, AsyncRead, AsyncSeek};
use futures_util::future::{OptionFuture, TryFutureExt as _};
use itertools::Itertools as _;
Expand All @@ -23,7 +23,7 @@ use crate::{
asyncs::{Async, Mutex as _},
error::{LoadModelError, LoadModelErrorKind, LoadModelResult},
infer::{
domains::{inference_domain_map_values, InferenceDomainMap, TalkDomain, TalkOperation},
domains::{inference_domain_map_values, InferenceDomainMap, TalkDomain},
InferenceDomain,
},
manifest::{Manifest, ManifestDomains, ModelFile, ModelFileType, StyleIdToInnerVoiceId},
Expand Down Expand Up @@ -128,7 +128,7 @@ impl<A: Async> Inner<A> {

let header = VoiceModelHeader::new(manifest, metas, path)?.into();

InnerTryBuilder {
return InnerTryBuilder {
header,
inference_model_entries_builder: |header| {
let VoiceModelHeader { manifest, .. } = &**header;
Expand All @@ -139,23 +139,9 @@ impl<A: Async> Inner<A> {
talk: |talk| {
talk.as_ref()
.map(|manifest| {
let indices = enum_map! {
TalkOperation::PredictDuration => {
find_entry_index(&manifest.predict_duration.filename)?
}
TalkOperation::PredictIntonation => {
find_entry_index(&manifest.predict_intonation.filename)?
}
TalkOperation::GenerateFullIntermediate => {
find_entry_index(
&manifest.generate_full_intermediate.filename,
)?
}
TalkOperation::RenderAudioSegment => find_entry_index(
&manifest.render_audio_segment.filename,
)?,
};

let indices = EnumMap::from_fn(|k| &manifest[k]).try_map(
|_, ModelFile { filename, .. }| find_entry_index(filename),
)?;
Ok(InferenceModelEntry { indices, manifest })
})
.transpose()
Expand All @@ -174,7 +160,26 @@ impl<A: Async> Inner<A> {
},
zip: zip.into_inner().into_inner().into(),
}
.try_build()
.try_build();

#[ext]
impl<K: Enum, V> EnumMap<K, V> {
fn try_map<V2, E>(
self,
f: impl FnMut(K, V) -> Result<V2, E>,
) -> Result<EnumMap<K, V2>, E> {
let mut elems = self
.map(f)
.into_iter()
.map(|(_, r)| r.map(Some))
.collect::<Result<Vec<_>, _>>()?;

Ok(EnumMap::<K, _>::from_fn(|key| {
let key = key.into_usize();
elems[key].take().expect("each `key` should be distinct")
}))
}
}
}

fn id(&self) -> VoiceModelId {
Expand Down
33 changes: 0 additions & 33 deletions crates/voicevox_core_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod extract;
mod inference_domain;
mod inference_domains;
mod manifest;

use syn::parse_macro_input;

Expand Down Expand Up @@ -103,38 +102,6 @@ pub fn derive_inference_output_signature(
from_syn(inference_domain::derive_inference_output_signature(input))
}

/// 構造体のフィールドを取得できる`std::ops::Index`の実装を導出する。
///
/// # Example
///
/// ```
/// use macros::IndexForFields;
///
/// #[derive(IndexForFields)]
/// #[index_for_fields(TalkOperation)]
/// pub(crate) struct TalkManifest {
/// #[index_for_fields(TalkOperation::PredictDuration)]
/// pub(crate) predict_duration_filename: Arc<str>,
///
/// #[index_for_fields(TalkOperation::PredictIntonation)]
/// pub(crate) predict_intonation_filename: Arc<str>,
///
/// #[index_for_fields(TalkOperation::GenerateFullIntermediate)]
/// pub(crate) generate_full_intermediate_filename: Arc<str>,
///
/// #[index_for_fields(TalkOperation::RenderAudioSegment)]
/// pub(crate) render_audio_segment_filename: Arc<str>,
///
/// // …
/// }
/// ```
#[cfg(not(doctest))]
#[proc_macro_derive(IndexForFields, attributes(index_for_fields))]
pub fn derive_index_for_fields(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = &parse_macro_input!(input);
from_syn(manifest::derive_index_for_fields(input))
}

/// # Example
///
/// ```
Expand Down
72 changes: 0 additions & 72 deletions crates/voicevox_core_macros/src/manifest.rs

This file was deleted.

0 comments on commit 9fde4e7

Please sign in to comment.