Skip to content

Commit

Permalink
SpeakerIdの型を作成 (VOICEVOX#1196)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroshiba authored Feb 16, 2023
1 parent 1b1c345 commit ab5b664
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 43 deletions.
6 changes: 3 additions & 3 deletions src/components/CharacterButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
<script lang="ts">
import { base64ImageToUri } from "@/helpers/imageHelper";
import { useStore } from "@/store";
import { CharacterInfo, Voice } from "@/type/preload";
import { CharacterInfo, SpeakerId, Voice } from "@/type/preload";
import { debounce } from "quasar";
import { computed, defineComponent, PropType, ref } from "vue";
Expand Down Expand Up @@ -256,7 +256,7 @@ export default defineComponent({
)
);
const getDefaultStyle = (speakerUuid: string) => {
const getDefaultStyle = (speakerUuid: SpeakerId) => {
// FIXME: 同一キャラが複数エンジンにまたがっているとき、順番が先のエンジンが必ず選択される
const characterInfo = props.characterInfos.find(
(info) => info.metas.speakerUuid === speakerUuid
Expand All @@ -276,7 +276,7 @@ export default defineComponent({
return defaultStyle;
};
const onSelectSpeaker = (speakerUuid: string) => {
const onSelectSpeaker = (speakerUuid: SpeakerId) => {
const style = getDefaultStyle(speakerUuid);
emit("update:selectedVoice", {
engineId: style.engineId,
Expand Down
18 changes: 9 additions & 9 deletions src/components/CharacterOrderDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@
import { defineComponent, computed, ref, PropType, watch } from "vue";
import draggable from "vuedraggable";
import { useStore } from "@/store";
import { CharacterInfo, StyleInfo } from "@/type/preload";
import { CharacterInfo, SpeakerId, StyleInfo } from "@/type/preload";
export default defineComponent({
name: "CharacterOrderDialog",
Expand Down Expand Up @@ -233,14 +233,14 @@ export default defineComponent({
});
// 新しいキャラクター
const newCharacters = ref<string[]>([]);
const newCharacters = ref<SpeakerId[]>([]);
const hasNewCharacter = computed(() => newCharacters.value.length > 0);
// サンプルボイス一覧のキャラクター順番
const sampleCharacterOrder = ref<string[]>([]);
const sampleCharacterOrder = ref<SpeakerId[]>([]);
// 選択中のスタイル
const selectedStyleIndexes = ref<Record<string, number>>({});
const selectedStyleIndexes = ref<Record<SpeakerId, number>>({});
const selectedStyles = computed(() => {
const map: { [key: string]: StyleInfo } = {};
props.characterInfos.forEach((characterInfo) => {
Expand All @@ -254,7 +254,7 @@ export default defineComponent({
// 選択中のキャラクター
const selectedCharacter = ref(props.characterInfos[0].metas.speakerUuid);
const selectCharacter = (speakerUuid: string) => {
const selectCharacter = (speakerUuid: SpeakerId) => {
selectedCharacter.value = speakerUuid;
};
Expand Down Expand Up @@ -314,14 +314,14 @@ export default defineComponent({
// 音声再生
const playing =
ref<{ speakerUuid: string; styleId: number; index: number }>();
ref<{ speakerUuid: SpeakerId; styleId: number; index: number }>();
const audio = new Audio();
audio.volume = 0.5;
audio.onended = () => stop();
const play = (
speakerUuid: string,
speakerUuid: SpeakerId,
{ styleId, voiceSamplePaths }: StyleInfo,
index: number
) => {
Expand All @@ -341,7 +341,7 @@ export default defineComponent({
// 再生していたら停止、再生していなかったら再生
const togglePlayOrStop = (
speakerUuid: string,
speakerUuid: SpeakerId,
styleInfo: StyleInfo,
index: number
) => {
Expand All @@ -358,7 +358,7 @@ export default defineComponent({
};
// スタイル番号をずらす
const rollStyleIndex = (speakerUuid: string, diff: number) => {
const rollStyleIndex = (speakerUuid: SpeakerId, diff: number) => {
// 0 <= index <= length に収める
const length = characterInfosMap.value[speakerUuid].metas.styles.length;
const selectedStyleIndex: number | undefined =
Expand Down
26 changes: 15 additions & 11 deletions src/components/DefaultStyleListDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { useStore } from "@/store";
import { CharacterInfo, StyleInfo } from "@/type/preload";
import { CharacterInfo, SpeakerId, StyleInfo } from "@/type/preload";
import DefaultStyleSelectDialog from "@/components/DefaultStyleSelectDialog.vue";
const props =
Expand Down Expand Up @@ -131,7 +131,7 @@ const selectedCharacterInfo = computed(() => {
});
const characterInfosMap = computed(() => {
const map: { [key: string]: CharacterInfo } = {};
const map: { [key: SpeakerId]: CharacterInfo } = {};
props.characterInfos.forEach((characterInfo) => {
map[characterInfo.metas.speakerUuid] = characterInfo;
});
Expand All @@ -142,7 +142,7 @@ const characterInfosMap = computed(() => {
const speakerWithMultipleStyles = ref<CharacterInfo[]>([]);
// 選択中のスタイル
const selectedStyleIndexes = ref<Record<string, number>>({});
const selectedStyleIndexes = ref<Record<SpeakerId, number>>({});
const selectedStyles = computed(() => {
const map: { [key: string]: StyleInfo } = {};
props.characterInfos.forEach((characterInfo) => {
Expand Down Expand Up @@ -194,14 +194,18 @@ const closeDialog = () => {
store.dispatch(
"SET_DEFAULT_STYLE_IDS",
Object.entries(selectedStyleIndexes.value).map(
([speakerUuid, styleIndex]) => ({
speakerUuid,
defaultStyleId:
characterInfosMap.value[speakerUuid].metas.styles[styleIndex].styleId,
engineId:
characterInfosMap.value[speakerUuid].metas.styles[styleIndex]
.engineId,
})
([speakerUuidStr, styleIndex]) => {
const speakerUuid = SpeakerId(speakerUuidStr);
return {
speakerUuid,
defaultStyleId:
characterInfosMap.value[speakerUuid].metas.styles[styleIndex]
.styleId,
engineId:
characterInfosMap.value[speakerUuid].metas.styles[styleIndex]
.engineId,
};
}
)
);
stop();
Expand Down
9 changes: 7 additions & 2 deletions src/components/DefaultStyleSelectDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@
<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { useStore } from "@/store";
import { CharacterInfo, DefaultStyleId, StyleInfo } from "@/type/preload";
import {
CharacterInfo,
DefaultStyleId,
SpeakerId,
StyleInfo,
} from "@/type/preload";
const props =
defineProps<{
Expand Down Expand Up @@ -188,7 +193,7 @@ audio.volume = 0.5;
audio.onended = () => stop();
const play = (
speakerUuid: string,
speakerUuid: SpeakerId,
{ styleId, voiceSamplePaths }: StyleInfo,
index: number
) => {
Expand Down
3 changes: 2 additions & 1 deletion src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
EngineId,
MoraDataType,
MorphingInfo,
SpeakerId,
StyleInfo,
Voice,
WriteFileErrorResult,
Expand Down Expand Up @@ -283,7 +284,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
const characterInfo: CharacterInfo = {
portraitPath: base64ImageToUri(speakerInfo.portrait),
metas: {
speakerUuid: speaker.speakerUuid,
speakerUuid: SpeakerId(speaker.speakerUuid),
speakerName: speaker.name,
styles,
policy: speakerInfo.policy,
Expand Down
6 changes: 3 additions & 3 deletions src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { createPartialStore } from "./vuex";

import { AccentPhrase } from "@/openapi";
import { z } from "zod";
import { EngineId, engineIdSchema } from "@/type/preload";
import { EngineId, engineIdSchema, speakerIdSchema } from "@/type/preload";

const DEFAULT_SAMPLING_RATE = 24000;

Expand Down Expand Up @@ -473,15 +473,15 @@ const audioQuerySchema = z.object({
const morphingInfoSchema = z.object({
rate: z.number(),
targetEngineId: engineIdSchema,
targetSpeakerId: z.string(),
targetSpeakerId: speakerIdSchema,
targetStyleId: z.number(),
});

const audioItemSchema = z.object({
text: z.string(),
voice: z.object({
engineId: engineIdSchema,
speakerId: z.string().uuid(),
speakerId: speakerIdSchema,
styleId: z.number(),
}),
query: audioQuerySchema.optional(),
Expand Down
13 changes: 7 additions & 6 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
EngineSetting,
Voice,
EngineId,
SpeakerId,
} from "@/type/preload";
import { IEngineConnectorFactory } from "@/infrastructures/EngineConnector";
import { QVueGlobals } from "quasar";
Expand Down Expand Up @@ -112,7 +113,7 @@ export type QuasarDialog = QVueGlobals["dialog"];

export type AudioStoreState = {
characterInfos: Record<EngineId, CharacterInfo[]>;
morphableTargetsInfo: Record<string, MorphableTargetInfoTable>;
morphableTargetsInfo: Record<EngineId, MorphableTargetInfoTable>;
audioKeyInitializingSpeaker?: string;
audioItems: Record<string, AudioItem>;
audioKeys: string[];
Expand Down Expand Up @@ -823,13 +824,13 @@ export type EngineStoreTypes = {

export type IndexStoreState = {
defaultStyleIds: DefaultStyleId[];
userCharacterOrder: string[];
userCharacterOrder: SpeakerId[];
isMultiEngineOffMode: boolean;
};

export type IndexStoreTypes = {
GET_ALL_CHARACTER_INFOS: {
getter: Map<string, CharacterInfo>;
getter: Map<SpeakerId, CharacterInfo>;
};

GET_ORDERED_ALL_CHARACTER_INFOS: {
Expand Down Expand Up @@ -882,12 +883,12 @@ export type IndexStoreTypes = {
};

SET_USER_CHARACTER_ORDER: {
mutation: { userCharacterOrder: string[] };
action(payload: string[]): void;
mutation: { userCharacterOrder: SpeakerId[] };
action(payload: SpeakerId[]): void;
};

GET_NEW_CHARACTERS: {
action(): string[];
action(): SpeakerId[];
};

LOG_ERROR: {
Expand Down
20 changes: 12 additions & 8 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ export const engineIdSchema = z.string().uuid().brand<"EngineId">();
export type EngineId = z.infer<typeof engineIdSchema>;
export const EngineId = (id: string): EngineId => engineIdSchema.parse(id);

export const speakerIdSchema = z.string().uuid().brand<"SpeakerId">();
export type SpeakerId = z.infer<typeof speakerIdSchema>;
export const SpeakerId = (id: string): SpeakerId => speakerIdSchema.parse(id);

// ホットキーを追加したときは設定のマイグレーションが必要
export const defaultHotkeySettings: HotkeySetting[] = [
{
Expand Down Expand Up @@ -214,14 +218,14 @@ export type StyleInfo = {

export type MetasJson = {
speakerName: string;
speakerUuid: string;
speakerUuid: SpeakerId;
styles: Pick<StyleInfo, "styleName" | "styleId">[];
};

export type CharacterInfo = {
portraitPath: string;
metas: {
speakerUuid: string;
speakerUuid: SpeakerId;
speakerName: string;
styles: StyleInfo[];
policy: string;
Expand All @@ -236,7 +240,7 @@ export type UpdateInfo = {

export type Voice = {
engineId: EngineId;
speakerId: string;
speakerId: SpeakerId;
styleId: number;
};

Expand Down Expand Up @@ -281,7 +285,7 @@ export type EngineSetting = z.infer<typeof engineSettingSchema>;

export type DefaultStyleId = {
engineId: EngineId;
speakerUuid: string;
speakerUuid: SpeakerId;
defaultStyleId: number;
};

Expand Down Expand Up @@ -332,7 +336,7 @@ export type Preset = {
export type MorphingInfo = {
rate: number;
targetEngineId: EngineId;
targetSpeakerId: string;
targetSpeakerId: SpeakerId;
targetStyleId: number;
};

Expand Down Expand Up @@ -492,14 +496,14 @@ export const electronStoreSchema = z
.array()
.default(defaultToolbarButtonSetting),
engineSettings: z.record(engineIdSchema, engineSettingSchema).default({}),
userCharacterOrder: z.string().array().default([]),
userCharacterOrder: speakerIdSchema.array().default([]),
defaultStyleIds: z
.object({
// FIXME: マイグレーション前にバリテーションされてしまう問題に対処したら.or(z.literal)を外す
engineId: engineIdSchema
.or(z.literal(EngineId("00000000-0000-0000-0000-000000000000")))
.default(EngineId("00000000-0000-0000-0000-000000000000")),
speakerUuid: z.string().uuid(),
speakerUuid: speakerIdSchema,
defaultStyleId: z.number(),
})
.passthrough()
Expand All @@ -523,7 +527,7 @@ export const electronStoreSchema = z
.object({
rate: z.number(),
targetEngineId: engineIdSchema,
targetSpeakerId: z.string().uuid(),
targetSpeakerId: speakerIdSchema,
targetStyleId: z.number(),
})
.passthrough()
Expand Down

0 comments on commit ab5b664

Please sign in to comment.