From 42527d0b08859a36189a2c6e3e49f397c276ca3e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 26 Jan 2020 17:35:07 +1000 Subject: [PATCH] add a (perhaps temporary) tts-voices: filter to show all available engines --- qt/aqt/tts.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/qt/aqt/tts.py b/qt/aqt/tts.py index e06e67a81..1825cafe8 100644 --- a/qt/aqt/tts.py +++ b/qt/aqt/tts.py @@ -30,8 +30,10 @@ import re import subprocess from concurrent.futures import Future from dataclasses import dataclass +from operator import attrgetter from typing import Any, List, Optional, cast +from anki import hooks from anki.sound import AVTag, TTSTag from anki.utils import checksum, isWin, tmpdir from aqt import gui_hooks @@ -105,6 +107,36 @@ class TTSProcessPlayer(SimpleProcessPlayer, TTSPlayer): return None +# tts-voices filter +########################################################################## + + +def all_tts_voices() -> List[TTSVoice]: + from aqt.sound import av_player + + all_voices: List[TTSVoice] = [] + for p in av_player.players: + getter = getattr(p, "voices", None) + if not getter: + continue + all_voices.extend(getter()) + return all_voices + + +def on_tts_voices(text: str, field, filter: str, ctx) -> str: + if filter != "tts-voices": + return text + voices = all_tts_voices() + voices.sort(key=attrgetter("name")) + voices.sort(key=attrgetter("lang")) + + buf = "
TTS voices available:
" + buf += "
".join(f"{{{{tts {v.lang} voices={v.name}}}}}" for v in voices) + return buf + "
" + + +hooks.field_filter.append(on_tts_voices) + # Mac support ##########################################################################