forked from yy4382/tts-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoiceList.ts
47 lines (46 loc) · 1.48 KB
/
voiceList.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
import { defineStore } from "pinia";
import type { VoiceAttr } from "~/utils/types";
import { useSettingsStore } from "./settings";
export const useVoiceListStore = defineStore("tts-i:voiceList", {
state: () => ({
voiceList: [] as VoiceAttr[],
}),
actions: {
async updateVoiceList(
successCb?: ({ listLength }: { listLength: number }) => void,
errCb?: (error: Error) => void,
) {
const settings = useSettingsStore();
if (!settings.notEmpty) {
if (errCb) errCb(new Error("settings not valid"));
return;
}
const res = await $fetch(
`https://${settings.region}.tts.speech.microsoft.com/cognitiveservices/voices/list`,
{
headers: {
"Ocp-Apim-Subscription-Key": settings.key,
},
},
);
if (!Array.isArray(res)) {
if (errCb) errCb(new Error("Invalid response, should be an array"));
else throw new Error("Invalid response, should be an array");
return;
}
const zhVoices = res
.filter((voice) => voice.Locale.startsWith("zh"))
.map((voice) => {
const styles: Array<string> | null = voice.StyleList || null;
return {
LocalName: voice.LocalName,
ShortName: voice.ShortName,
StyleNames: styles,
} satisfies VoiceAttr as VoiceAttr;
});
this.voiceList = zhVoices;
if (successCb) successCb({ listLength: zhVoices.length });
},
},
persist: true,
});