Skip to content

Commit

Permalink
refactor: MetasStoreCoreManager の接点を最小化
Browse files Browse the repository at this point in the history
  • Loading branch information
tarepan committed Jun 24, 2024
1 parent a388e07 commit a0e5c90
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 6 additions & 2 deletions voicevox_engine/app/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from voicevox_engine.core.core_initializer import CoreManager
from voicevox_engine.engine_manifest import EngineManifest
from voicevox_engine.library.library_manager import LibraryManager
from voicevox_engine.metas.MetasStore import MetasStore
from voicevox_engine.metas.MetasStore import MetasStore, generate_core_characters_getter
from voicevox_engine.preset.preset_manager import PresetManager
from voicevox_engine.resource_manager import ResourceManager
from voicevox_engine.setting.model import CorsPolicyMode
Expand Down Expand Up @@ -66,7 +66,11 @@ def generate_app(

resource_manager = ResourceManager(is_development())
resource_manager.register_dir(speaker_info_dir)
metas_store = MetasStore(speaker_info_dir, core_manager, resource_manager)
metas_store = MetasStore(
speaker_info_dir,
generate_core_characters_getter(core_manager),
resource_manager,
)

app.include_router(
generate_tts_pipeline_router(
Expand Down
28 changes: 21 additions & 7 deletions voicevox_engine/metas/MetasStore.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

from dataclasses import dataclass
from pathlib import Path
from typing import Final, Literal, TypeAlias
from typing import Callable, Final, Literal, TypeAlias

from fastapi import HTTPException
from pydantic import BaseModel, Field

from voicevox_engine.core.core_adapter import CoreCharacterStyle
from voicevox_engine.core.core_adapter import CoreCharacter, CoreCharacterStyle
from voicevox_engine.core.core_initializer import CoreManager
from voicevox_engine.metas.Metas import (
Speaker,
Expand Down Expand Up @@ -69,6 +69,22 @@ class _EngineSpeaker(BaseModel):
)


GetCoreCharacters: TypeAlias = Callable[[str | None], list[CoreCharacter]]


def generate_core_characters_getter(core_manager: CoreManager) -> GetCoreCharacters:
"""コアマネージャーを基に `get_core_characters()` 関数を生成する。"""

def get_core_characters(version: str | None) -> list[CoreCharacter]:
"""バージョンで指定されたコアからキャラクター一覧を取得する。"""
# NOTE: CoreManager へ直接触れずにキャラクター情報を取得するために関数化している
version = version or core_manager.latest_version()
core = core_manager.get_core(version)
return core.characters

return get_core_characters


class MetasStore:
"""
話者やスタイルのメタ情報を管理する
Expand All @@ -77,7 +93,7 @@ class MetasStore:
def __init__(
self,
engine_speakers_path: Path,
core_manager: CoreManager,
get_core_characters: GetCoreCharacters,
resource_manager: ResourceManager,
) -> None:
"""
Expand All @@ -89,7 +105,7 @@ def __init__(
コアマネージャー
"""
self._speakers_path = engine_speakers_path
self._core_manager = core_manager
self._get_core_characters = get_core_characters
self._resource_manager = resource_manager
# エンジンに含まれる各話者のメタ情報
self._loaded_metas: dict[str, _EngineSpeaker] = {
Expand All @@ -102,12 +118,10 @@ def __init__(

def characters(self, core_version: str | None) -> list[Character]:
"""キャラクターの情報の一覧を取得する。"""
version = core_version or self._core_manager.latest_version()
core = self._core_manager.get_core(version)

# エンジンとコアのキャラクター情報を統合する
characters: list[Character] = []
for core_character in core.characters:
for core_character in self._get_core_characters(core_version):
character_uuid = core_character.speaker_uuid
engine_character = self._loaded_metas[character_uuid]
styles = cast_styles(core_character.styles)
Expand Down

0 comments on commit a0e5c90

Please sign in to comment.