From 8ca2fa64762e6b758b497a06513ffc1277cb0e7e Mon Sep 17 00:00:00 2001 From: RumovZ Date: Sun, 13 Feb 2022 04:51:59 +0100 Subject: [PATCH] Handle multiple languages in TTS code on Windows (#1663) Closes #1615. --- qt/aqt/tts.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/qt/aqt/tts.py b/qt/aqt/tts.py index 4298c53af..7f242d6d6 100644 --- a/qt/aqt/tts.py +++ b/qt/aqt/tts.py @@ -474,9 +474,10 @@ if is_win: "31748": "zh_CHT", } - def lcid_hex_str_to_lang_code(hex: str) -> str: - dec_str = str(int(hex, 16)) - return LCIDS.get(dec_str, "unknown") + def lcid_hex_str_to_lang_codes(hex_codes: str) -> list[str]: + return [ + LCIDS.get(str(int(code, 16)), "unknown") for code in hex_codes.split(";") + ] class WindowsTTSPlayer(TTSProcessPlayer): default_rank = -1 @@ -488,13 +489,17 @@ if is_win: def get_available_voices(self) -> list[TTSVoice]: if self.speaker is None: return [] - return list(map(self._voice_to_object, self.speaker.GetVoices())) + return [ + obj + for voice in self.speaker.GetVoices() + for obj in self._voice_to_objects(voice) + ] - def _voice_to_object(self, voice: Any) -> WindowsVoice: - lang = voice.GetAttribute("language") - lang = lcid_hex_str_to_lang_code(lang) + def _voice_to_objects(self, voice: Any) -> list[WindowsVoice]: + langs = voice.GetAttribute("language") + langs = lcid_hex_str_to_lang_codes(langs) name = self._tidy_name(voice.GetAttribute("name")) - return WindowsVoice(name=name, lang=lang, handle=voice) + return [WindowsVoice(name=name, lang=lang, handle=voice) for lang in langs] def _play(self, tag: AVTag) -> None: assert isinstance(tag, TTSTag)