forked from VOICEVOX/voicevox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preset.ts
296 lines (264 loc) · 8.48 KB
/
preset.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import { ref, computed } from "vue";
import { defineStore, storeToRefs } from "pinia";
import { v4 as uuidv4 } from "uuid";
import { useStoreAsState } from "./store-helper";
import { useStore as useVuexStore } from "@/store";
import {
ExperimentalSetting,
Preset,
PresetKey,
Voice,
VoiceId,
} from "@/type/preload";
/**
* configを参照して割り当てるべきpresetKeyとそのPresetを適用すべきかどうかを返す
*
* generate: プロジェクト新規作成時、空のAudioItemを作成する場合
* copy: 元となるAudioItemがある場合(+ボタンで作成したとき)
* changeVoice: ボイス切り替え時
*/
export function determineNextPresetKey(
defaultPresetKeys: State["defaultPresetKeys"],
experimentalSetting: ExperimentalSetting,
inheritAudioInfo: boolean,
voice: Voice,
presetKeyCandidate: PresetKey | undefined,
operation: "generate" | "copy" | "changeVoice"
): {
nextPresetKey: PresetKey | undefined;
shouldApplyPreset: boolean;
} {
const defaultPresetKeyForCurrentVoice = defaultPresetKeys[VoiceId(voice)];
switch (operation) {
case "generate": {
// 初回作成時
return {
nextPresetKey: defaultPresetKeyForCurrentVoice,
shouldApplyPreset: experimentalSetting.enablePreset,
};
}
case "copy": {
// 元となるAudioItemがある場合
if (inheritAudioInfo) {
// パラメータ引継ぎがONならそのまま引き継ぐ
return {
nextPresetKey: presetKeyCandidate,
shouldApplyPreset: false,
};
}
// それ以外はデフォルトプリセットを割り当て、適用するかはプリセットのON/OFFに依存
return {
nextPresetKey: defaultPresetKeyForCurrentVoice,
shouldApplyPreset: experimentalSetting.enablePreset,
};
}
case "changeVoice": {
// ボイス切り替え時
if (experimentalSetting.shouldApplyDefaultPresetOnVoiceChanged) {
// デフォルトプリセットを適用する
return {
nextPresetKey: defaultPresetKeyForCurrentVoice,
shouldApplyPreset: true,
};
}
const isDefaultPreset = Object.values(defaultPresetKeys).some(
(key) => key === presetKeyCandidate
);
// 引き継ぎ元が他スタイルのデフォルトプリセットだった場合
// 別キャラのデフォルトプリセットを引き継がないようにする
// それ以外は指定そのまま
return {
nextPresetKey: isDefaultPreset
? defaultPresetKeyForCurrentVoice
: presetKeyCandidate,
shouldApplyPreset: false,
};
}
}
}
type State = ReturnType<typeof usePresetState>;
const usePresetState = defineStore("preset/state", () => {
const presetKeys = ref<PresetKey[]>([]);
const presetItems = ref<Record<PresetKey, Preset>>({});
const defaultPresetKeys = ref<Record<VoiceId, PresetKey>>({});
return {
presetKeys,
presetItems,
defaultPresetKeys,
};
});
export const usePresetStore = defineStore("preset", () => {
const { state, defMut, defAct } = useStoreAsState(usePresetState);
const vuexStore = useVuexStore();
const defaultPresetKeySets = computed(() => {
return new Set(Object.values(state.defaultPresetKeys));
});
const setPresetItems = defMut(
(state, { presetItems }: { presetItems: Record<PresetKey, Preset> }) => {
state.presetItems = presetItems;
}
);
const setPresetKeys = defMut(
(state, { presetKeys }: { presetKeys: PresetKey[] }) => {
state.presetKeys = presetKeys;
}
);
const setDefaultPresetMap = defAct(
async ({
defaultPresetKeys,
}: {
defaultPresetKeys: Record<VoiceId, PresetKey>;
}) => {
window.electron.setSetting("defaultPresetKeys", defaultPresetKeys);
setDefaultPresetMapMut.act({ defaultPresetKeys });
}
);
const setDefaultPresetMapMut = defMut(
(
state,
{ defaultPresetKeys }: { defaultPresetKeys: Record<VoiceId, PresetKey> }
) => {
state.defaultPresetKeys = defaultPresetKeys;
}
);
const hydratePresetStore = defAct(async () => {
const defaultPresetKeys = (await window.electron.getSetting(
"defaultPresetKeys"
// z.BRAND型のRecordはPartialになる仕様なのでasで型を変換
// TODO: 将来的にzodのバージョンを上げてasを消す https://github.com/colinhacks/zod/pull/2097
)) as Record<VoiceId, PresetKey>;
setDefaultPresetMapMut.act({
defaultPresetKeys,
});
const presetConfig = await window.electron.getSetting("presets");
if (
presetConfig === undefined ||
presetConfig.items === undefined ||
presetConfig.keys === undefined
)
return;
setPresetItems.act({
// z.BRAND型のRecordはPartialになる仕様なのでasで型を変換
// TODO: 将来的にzodのバージョンを上げてasを消す https://github.com/colinhacks/zod/pull/2097
presetItems: presetConfig.items as Record<PresetKey, Preset>,
});
setPresetKeys.act({
presetKeys: presetConfig.keys,
});
});
const savePresetOrder = defAct(
async ({ presetKeys }: { presetKeys: PresetKey[] }) => {
return savePresetConfig({
presetItems: state.presetItems,
presetKeys,
});
}
);
const savePresetConfig = defAct(
async ({
presetItems,
presetKeys,
}: {
presetItems: Record<PresetKey, Preset>;
presetKeys: PresetKey[];
}) => {
const result = await window.electron.setSetting("presets", {
items: JSON.parse(JSON.stringify(presetItems)),
keys: JSON.parse(JSON.stringify(presetKeys)),
});
setPresetItems.act({
// z.BRAND型のRecordはPartialになる仕様なのでasで型を変換
// TODO: 将来的にzodのバージョンを上げてasを消す https://github.com/colinhacks/zod/pull/2097
presetItems: result.items as Record<PresetKey, Preset>,
});
setPresetKeys.act({ presetKeys: result.keys });
}
);
const addPreset = defAct(async ({ presetData }: { presetData: Preset }) => {
const newKey = PresetKey(uuidv4());
const newPresetItems = {
...state.presetItems,
[newKey]: presetData,
};
const newPresetKeys = [newKey, ...state.presetKeys];
await savePresetConfig({
presetItems: newPresetItems,
presetKeys: newPresetKeys,
});
return newKey;
});
const createAllDefaultPreset = defAct(async () => {
const voices = vuexStore.getters.GET_ALL_VOICES;
for (const voice of voices) {
const voiceId = VoiceId(voice);
const defaultPresetKey = state.defaultPresetKeys[voiceId];
if (state.presetKeys.includes(defaultPresetKey)) {
continue;
}
const characterName = vuexStore.getters.VOICE_NAME(voice);
const presetData: Preset = {
name: `デフォルト:${characterName}`,
speedScale: 1.0,
pitchScale: 0.0,
intonationScale: 1.0,
volumeScale: 1.0,
prePhonemeLength: 0.1,
postPhonemeLength: 0.1,
};
const newPresetKey = await addPreset({ presetData });
await setDefaultPresetMap({
defaultPresetKeys: {
...state.defaultPresetKeys,
[voiceId]: newPresetKey,
},
});
}
});
const updatePreset = defAct(
async ({
presetKey,
presetData,
}: {
presetData: Preset;
presetKey: PresetKey;
}) => {
const newPresetItems = {
...state.presetItems,
[presetKey]: presetData,
};
const newPresetKeys = state.presetKeys.includes(presetKey)
? [...state.presetKeys]
: [presetKey, ...state.presetKeys];
await savePresetConfig({
presetItems: newPresetItems,
presetKeys: newPresetKeys,
});
}
);
const deletePreset = defAct(
async ({ presetKey }: { presetKey: PresetKey }) => {
const newPresetKeys = state.presetKeys.filter((key) => key != presetKey);
// Filter the `presetKey` properties from presetItems.
const { [presetKey]: _, ...newPresetItems } = state.presetItems;
await savePresetConfig({
presetItems: newPresetItems,
presetKeys: newPresetKeys,
});
}
);
return {
...storeToRefs(state),
defaultPresetKeySets,
setPresetItems,
setPresetKeys,
setDefaultPresetMap,
setDefaultPresetMapMut,
hydratePresetStore,
savePresetOrder,
savePresetConfig,
addPreset,
createAllDefaultPreset,
updatePreset,
deletePreset,
};
});