From cfb90496b6d5582d7b6dea22a1140b56777208de Mon Sep 17 00:00:00 2001 From: tarepan Date: Fri, 24 May 2024 13:00:47 +0000 Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20`Character`=20=E5=86=85?= =?UTF-8?q?=E9=83=A8=E5=9E=8B=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voicevox_engine/metas/MetasStore.py | 90 ++++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 16 deletions(-) diff --git a/voicevox_engine/metas/MetasStore.py b/voicevox_engine/metas/MetasStore.py index d5655a6c2..a15d966ae 100644 --- a/voicevox_engine/metas/MetasStore.py +++ b/voicevox_engine/metas/MetasStore.py @@ -1,7 +1,7 @@ import json -from copy import deepcopy +from dataclasses import dataclass from pathlib import Path -from typing import TYPE_CHECKING, Literal, NewType +from typing import TYPE_CHECKING, Final, Iterable, Literal, NewType from pydantic import BaseModel, Field @@ -10,7 +10,6 @@ SpeakerStyle, SpeakerSupportedFeatures, StyleId, - StyleType, ) if TYPE_CHECKING: @@ -132,24 +131,83 @@ def construct_lookup( return lookup_table +@dataclass +class Character: + """キャラクター""" + + name: str + uuid: str + talk_styles: list[SpeakerStyle] + sing_styles: list[SpeakerStyle] + version: str + supported_features: SpeakerSupportedFeatures + + +TALK_STYLE_TYPES: Final = ["talk"] +SING_STYLE_TYPES: Final = ["singing_teacher", "frame_decode", "sing"] + + +def cast_as_talk_speakers(talkers: Iterable[Character]) -> list[Speaker]: + """talkers を talk 系スタイルからのみなる `Speaker` リストへキャストする。""" + talk_speakers = map( + lambda talker: Speaker( + name=talker.name, + speaker_uuid=talker.uuid, + styles=talker.talk_styles, + version=talker.version, + supported_features=talker.supported_features, + ), + talkers, + ) + return list(talk_speakers) + + +def cast_as_sing_speakers(singers: Iterable[Character]) -> list[Speaker]: + """singers を sing 系スタイルからのみなる `Speaker` リストへキャストする。""" + sing_speakers = map( + lambda singer: Speaker( + name=singer.name, + speaker_uuid=singer.uuid, + styles=singer.sing_styles, + version=singer.version, + supported_features=singer.supported_features, + ), + singers, + ) + return list(sing_speakers) + + def filter_speakers_and_styles( speakers: list[Speaker], speaker_or_singer: Literal["speaker", "singer"], ) -> list[Speaker]: """ - 話者・スタイルをフィルタリングする。 - speakerの場合はトーク系スタイルのみ、singerの場合はソング系スタイルのみを残す。 - スタイル数が0になった話者は除外する。 + speakers から talkers あるいは singers を取り出す。 + talker はスタイル情報が talk 系のみにフィルタリングされている。 + singer はスタイル情報が sing 系のみにフィルタリングされている。 """ - style_types: list[StyleType] + + characters = map( + lambda speaker: Character( + name=speaker.name, + uuid=speaker.speaker_uuid, + talk_styles=list( + filter(lambda style: style.type in TALK_STYLE_TYPES, speaker.styles) + ), + sing_styles=list( + filter(lambda style: style.type in SING_STYLE_TYPES, speaker.styles) + ), + version=speaker.version, + supported_features=speaker.supported_features, + ), + speakers, + ) + if speaker_or_singer == "speaker": - style_types = ["talk"] + talkers = filter(lambda character: len(character.talk_styles) > 0, characters) + return cast_as_talk_speakers(talkers) elif speaker_or_singer == "singer": - style_types = ["singing_teacher", "frame_decode", "sing"] - - speakers = deepcopy(speakers) - for speaker in speakers: - speaker.styles = [ - style for style in speaker.styles if style.type in style_types - ] - return [speaker for speaker in speakers if len(speaker.styles) > 0] + singers = filter(lambda character: len(character.sing_styles) > 0, characters) + return cast_as_sing_speakers(singers) + else: + raise Exception(f"'{speaker_or_singer}' は不正な style_type です") From 117695d244369320b075b72b276a7d486ee2f7e9 Mon Sep 17 00:00:00 2001 From: tarepan Date: Fri, 24 May 2024 13:09:46 +0000 Subject: [PATCH 2/5] fix: typo --- voicevox_engine/metas/MetasStore.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/voicevox_engine/metas/MetasStore.py b/voicevox_engine/metas/MetasStore.py index a15d966ae..fdf5e5e13 100644 --- a/voicevox_engine/metas/MetasStore.py +++ b/voicevox_engine/metas/MetasStore.py @@ -148,7 +148,7 @@ class Character: def cast_as_talk_speakers(talkers: Iterable[Character]) -> list[Speaker]: - """talkers を talk 系スタイルからのみなる `Speaker` リストへキャストする。""" + """talkers を talk 系スタイルのみからなる `Speaker` リストへキャストする。""" talk_speakers = map( lambda talker: Speaker( name=talker.name, @@ -163,7 +163,7 @@ def cast_as_talk_speakers(talkers: Iterable[Character]) -> list[Speaker]: def cast_as_sing_speakers(singers: Iterable[Character]) -> list[Speaker]: - """singers を sing 系スタイルからのみなる `Speaker` リストへキャストする。""" + """singers を sing 系スタイルのみからなる `Speaker` リストへキャストする。""" sing_speakers = map( lambda singer: Speaker( name=singer.name, From e947ce2e2da27826dd10c2320e1e1ad81a60aa5d Mon Sep 17 00:00:00 2001 From: tarepan Date: Tue, 28 May 2024 05:57:57 +0000 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20docstring=20=E3=82=92=E7=B0=A1?= =?UTF-8?q?=E7=95=A5=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voicevox_engine/metas/MetasStore.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/voicevox_engine/metas/MetasStore.py b/voicevox_engine/metas/MetasStore.py index fdf5e5e13..c629a30ff 100644 --- a/voicevox_engine/metas/MetasStore.py +++ b/voicevox_engine/metas/MetasStore.py @@ -181,11 +181,7 @@ def filter_speakers_and_styles( speakers: list[Speaker], speaker_or_singer: Literal["speaker", "singer"], ) -> list[Speaker]: - """ - speakers から talkers あるいは singers を取り出す。 - talker はスタイル情報が talk 系のみにフィルタリングされている。 - singer はスタイル情報が sing 系のみにフィルタリングされている。 - """ + """キャラクター内のスタイルをtalk系・sing系のみにする。スタイル数が0になったキャラクターは除外する。""" characters = map( lambda speaker: Character( From d14631490ee1e8502f970bd2023afa9758c75de5 Mon Sep 17 00:00:00 2001 From: tarepan Date: Tue, 28 May 2024 06:10:29 +0000 Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=E3=82=AD=E3=83=A3=E3=82=B9?= =?UTF-8?q?=E3=83=88=E9=96=A2=E6=95=B0=E3=82=92=E5=89=8A=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voicevox_engine/metas/MetasStore.py | 62 +++++++++++++---------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/voicevox_engine/metas/MetasStore.py b/voicevox_engine/metas/MetasStore.py index c629a30ff..1f4a90792 100644 --- a/voicevox_engine/metas/MetasStore.py +++ b/voicevox_engine/metas/MetasStore.py @@ -147,36 +147,6 @@ class Character: SING_STYLE_TYPES: Final = ["singing_teacher", "frame_decode", "sing"] -def cast_as_talk_speakers(talkers: Iterable[Character]) -> list[Speaker]: - """talkers を talk 系スタイルのみからなる `Speaker` リストへキャストする。""" - talk_speakers = map( - lambda talker: Speaker( - name=talker.name, - speaker_uuid=talker.uuid, - styles=talker.talk_styles, - version=talker.version, - supported_features=talker.supported_features, - ), - talkers, - ) - return list(talk_speakers) - - -def cast_as_sing_speakers(singers: Iterable[Character]) -> list[Speaker]: - """singers を sing 系スタイルのみからなる `Speaker` リストへキャストする。""" - sing_speakers = map( - lambda singer: Speaker( - name=singer.name, - speaker_uuid=singer.uuid, - styles=singer.sing_styles, - version=singer.version, - supported_features=singer.supported_features, - ), - singers, - ) - return list(sing_speakers) - - def filter_speakers_and_styles( speakers: list[Speaker], speaker_or_singer: Literal["speaker", "singer"], @@ -200,10 +170,34 @@ def filter_speakers_and_styles( ) if speaker_or_singer == "speaker": - talkers = filter(lambda character: len(character.talk_styles) > 0, characters) - return cast_as_talk_speakers(talkers) + # talk 系スタイルを持たないキャラクターを除外する + talk_characters = filter(lambda character: len(character.talk_styles) > 0, characters) + # キャラクター内のスタイルを talk 系のみにしたうえでキャストする + talk_speakers = map( + lambda talker: Speaker( + name=talker.name, + speaker_uuid=talker.uuid, + styles=talker.talk_styles, + version=talker.version, + supported_features=talker.supported_features, + ), + talk_characters, + ) + return list(talk_speakers) elif speaker_or_singer == "singer": - singers = filter(lambda character: len(character.sing_styles) > 0, characters) - return cast_as_sing_speakers(singers) + # sing 系スタイルを持たないキャラクターを除外する + sing_characters = filter(lambda character: len(character.sing_styles) > 0, characters) + # キャラクター内のスタイルを sing 系のみにしたうえでキャストする + sing_speakers = map( + lambda singer: Speaker( + name=singer.name, + speaker_uuid=singer.uuid, + styles=singer.sing_styles, + version=singer.version, + supported_features=singer.supported_features, + ), + sing_characters, + ) + return list(sing_speakers) else: raise Exception(f"'{speaker_or_singer}' は不正な style_type です") From 537a275a1bdf04a2441651504724010a39387192 Mon Sep 17 00:00:00 2001 From: tarepan Date: Tue, 28 May 2024 06:11:52 +0000 Subject: [PATCH 5/5] fix: lint --- voicevox_engine/metas/MetasStore.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/voicevox_engine/metas/MetasStore.py b/voicevox_engine/metas/MetasStore.py index 1f4a90792..3efba014a 100644 --- a/voicevox_engine/metas/MetasStore.py +++ b/voicevox_engine/metas/MetasStore.py @@ -1,7 +1,7 @@ import json from dataclasses import dataclass from pathlib import Path -from typing import TYPE_CHECKING, Final, Iterable, Literal, NewType +from typing import TYPE_CHECKING, Final, Literal, NewType from pydantic import BaseModel, Field @@ -171,7 +171,9 @@ def filter_speakers_and_styles( if speaker_or_singer == "speaker": # talk 系スタイルを持たないキャラクターを除外する - talk_characters = filter(lambda character: len(character.talk_styles) > 0, characters) + talk_characters = filter( + lambda character: len(character.talk_styles) > 0, characters + ) # キャラクター内のスタイルを talk 系のみにしたうえでキャストする talk_speakers = map( lambda talker: Speaker( @@ -186,7 +188,9 @@ def filter_speakers_and_styles( return list(talk_speakers) elif speaker_or_singer == "singer": # sing 系スタイルを持たないキャラクターを除外する - sing_characters = filter(lambda character: len(character.sing_styles) > 0, characters) + sing_characters = filter( + lambda character: len(character.sing_styles) > 0, characters + ) # キャラクター内のスタイルを sing 系のみにしたうえでキャストする sing_speakers = map( lambda singer: Speaker(