Skip to content

Commit

Permalink
整理: CoreSpeakerdataclass へ変更 + リネーム (#1409)
Browse files Browse the repository at this point in the history
* refactor: `CoreSpeaker` BaseModel → `CoreCharacter` dataclass へ移行

* refactor: `CoreSpeakerStyle` BaseModel → `CoreCharacterStyle` dataclass へ移行

* fix: `Field` 削除による移行追従 & lint

* fix: lint

* refactor: `CoreAdapter.speakers` → `CoreAdapter.characters` へリネーム
  • Loading branch information
tarepan authored Jun 19, 2024
1 parent f43f696 commit c5e5f3a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
4 changes: 2 additions & 2 deletions voicevox_engine/app/routers/morphing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def morphable_targets(
version = core_version or core_manager.latest_version()
core = core_manager.get_core(version)

speakers = metas_store.load_combined_metas(core.speakers)
speakers = metas_store.load_combined_metas(core.characters)
try:
morphable_targets = get_morphable_targets(speakers, base_style_ids)
except StyleIdNotFoundError as e:
Expand Down Expand Up @@ -97,7 +97,7 @@ def _synthesis_morphing(
core = core_manager.get_core(version)

# モーフィングが許可されないキャラクターペアを拒否する
speakers = metas_store.load_combined_metas(core.speakers)
speakers = metas_store.load_combined_metas(core.characters)
try:
morphable = is_morphable(speakers, base_style_id, target_style_id)
except StyleIdNotFoundError as e:
Expand Down
8 changes: 4 additions & 4 deletions voicevox_engine/app/routers/speaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def speakers(core_version: str | SkipJsonSchema[None] = None) -> list[Speaker]:
"""話者情報の一覧を取得します。"""
version = core_version or core_manager.latest_version()
core = core_manager.get_core(version)
speakers = metas_store.load_combined_metas(core.speakers)
speakers = metas_store.load_combined_metas(core.characters)
return filter_speakers_and_styles(speakers, "speaker")

@router.get("/speaker_info")
Expand Down Expand Up @@ -78,8 +78,8 @@ def _speaker_info(
version = core_version or core_manager.latest_version()

# 該当話者を検索する
core_speakers = core_manager.get_core(version).speakers
speakers = metas_store.load_combined_metas(core_speakers)
core_characters = core_manager.get_core(version).characters
speakers = metas_store.load_combined_metas(core_characters)
speakers = filter_speakers_and_styles(speakers, speaker_or_singer)
speaker = next(
filter(lambda spk: spk.speaker_uuid == speaker_uuid, speakers), None
Expand Down Expand Up @@ -143,7 +143,7 @@ def singers(core_version: str | SkipJsonSchema[None] = None) -> list[Speaker]:
"""歌手情報の一覧を取得します"""
version = core_version or core_manager.latest_version()
core = core_manager.get_core(version)
singers = metas_store.load_combined_metas(core.speakers)
singers = metas_store.load_combined_metas(core.characters)
return filter_speakers_and_styles(singers, "singer")

@router.get("/singer_info")
Expand Down
35 changes: 18 additions & 17 deletions voicevox_engine/core/core_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import json
import threading
from dataclasses import dataclass
from typing import Literal, NewType
from typing import Any, Literal, NewType

import numpy as np
from numpy.typing import NDArray
from pydantic import BaseModel, Field
from pydantic import TypeAdapter

from ..metas.Metas import StyleId
from .core_wrapper import CoreWrapper, OldCoreError
Expand All @@ -16,25 +16,26 @@
CoreStyleType = Literal["talk", "singing_teacher", "frame_decode", "sing"]


class CoreSpeakerStyle(BaseModel):
"""
話者のスタイル情報
"""
@dataclass(frozen=True)
class CoreCharacterStyle:
"""コアに含まれるキャラクターのスタイル情報"""

name: str
id: CoreStyleId
type: CoreStyleType | None = Field(default="talk")
type: CoreStyleType | None = "talk"


class CoreSpeaker(BaseModel):
"""
コアに含まれる話者情報
"""
@dataclass(frozen=True)
class CoreCharacter:
"""コアに含まれるキャラクター情報"""

name: str
speaker_uuid: str
styles: list[CoreSpeakerStyle]
version: str = Field(title="話者のバージョン")
styles: list[CoreCharacterStyle]
version: str # 話者のバージョン


_core_character_adapter = TypeAdapter(CoreCharacter)


@dataclass(frozen=True)
Expand Down Expand Up @@ -62,10 +63,10 @@ def default_sampling_rate(self) -> int:
return self.core.default_sampling_rate

@property
def speakers(self) -> list[CoreSpeaker]:
"""話者情報"""
metas = self.core.metas()
return [CoreSpeaker(**speaker) for speaker in json.loads(metas)]
def characters(self) -> list[CoreCharacter]:
"""キャラクター情報"""
metas: list[Any] = json.loads(self.core.metas())
return list(map(_core_character_adapter.validate_python, metas))

@property
def supported_devices(self) -> DeviceSupport | None:
Expand Down
6 changes: 3 additions & 3 deletions voicevox_engine/metas/MetasStore.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pydantic import BaseModel, Field

from voicevox_engine.core.core_adapter import CoreSpeaker, CoreSpeakerStyle
from voicevox_engine.core.core_adapter import CoreCharacter, CoreCharacterStyle
from voicevox_engine.metas.Metas import (
Speaker,
SpeakerStyle,
Expand All @@ -15,7 +15,7 @@
)


def cast_styles(cores: list[CoreSpeakerStyle]) -> list[SpeakerStyle]:
def cast_styles(cores: list[CoreCharacterStyle]) -> list[SpeakerStyle]:
"""コアから取得したスタイル情報をエンジン形式へキャストする。"""
return [
SpeakerStyle(name=core.name, id=StyleId(core.id), type=core.type)
Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(self, engine_speakers_path: Path) -> None:
for folder in engine_speakers_path.iterdir()
}

def load_combined_metas(self, core_metas: list[CoreSpeaker]) -> list[Speaker]:
def load_combined_metas(self, core_metas: list[CoreCharacter]) -> list[Speaker]:
"""コアとエンジンのメタ情報を統合する。"""
return [
Speaker(
Expand Down

0 comments on commit c5e5f3a

Please sign in to comment.