@lobehub/tts
Version:
A high-quality & reliable TTS React Hooks library
28 lines (26 loc) • 1.04 kB
JavaScript
import locales_default from "../data/locales.mjs";
import { SpeechSynthesis } from "../const/polyfill.mjs";
import voiceList_default from "./voiceList.mjs";
import { flatten } from "es-toolkit/compat";
//#region src/core/SpeechSynthesisTTS/options.ts
const genSpeechSynthesisVoiceList = () => {
if (!SpeechSynthesis) return voiceList_default;
const data = SpeechSynthesis?.getVoices();
if (!data) return voiceList_default;
const localeKeys = Object.keys(locales_default);
const list = {};
for (const voice of data) if (localeKeys.includes(voice.lang)) {
if (!list[voice.lang]) list[voice.lang] = [];
list[voice.lang].push(voice.name);
}
return Object.keys(list).length > 0 ? list : voiceList_default;
};
const getSpeechSynthesisVoiceOptions = (locale) => {
const voiceList = genSpeechSynthesisVoiceList();
return (locale && voiceList?.[locale] ? voiceList?.[locale] || [] : flatten(Object.values(voiceList))).map((voice) => ({
label: voice,
value: voice
}));
};
//#endregion
export { getSpeechSynthesisVoiceOptions };