mirror of
https://github.com/ankitects/anki.git
synced 2025-09-18 14:02:21 -04:00
add a proof of concept TTS implementation on Mac
This commit is contained in:
parent
707ac587ec
commit
caac4527e9
2 changed files with 46 additions and 0 deletions
|
@ -647,6 +647,10 @@ def setup_audio(base_folder: str) -> None:
|
|||
mpv.args.append("--include=" + base_folder)
|
||||
av_player.players.append(mpv)
|
||||
|
||||
if isMac:
|
||||
from aqt.tts import MacTTSPlayer
|
||||
|
||||
av_player.players.append(MacTTSPlayer())
|
||||
|
||||
# Legacy audio interface
|
||||
##########################################################################
|
||||
|
|
42
qt/aqt/tts.py
Normal file
42
qt/aqt/tts.py
Normal file
|
@ -0,0 +1,42 @@
|
|||
"""
|
||||
todo
|
||||
"""
|
||||
|
||||
import subprocess
|
||||
from concurrent.futures import Future
|
||||
from typing import Callable, cast
|
||||
|
||||
from anki.sound import AVTag, TTSTag
|
||||
from aqt.sound import OnDoneCallback, Player
|
||||
from aqt.taskman import TaskManager
|
||||
|
||||
|
||||
class TTSPlayer(Player): # pylint: disable=abstract-method
|
||||
def can_play(self, tag: AVTag) -> bool:
|
||||
return isinstance(tag, TTSTag)
|
||||
|
||||
|
||||
class MacTTSPlayer(TTSPlayer):
|
||||
_taskman = TaskManager()
|
||||
|
||||
def play(self, tag: AVTag, on_done: Callable[[], None]) -> None:
|
||||
ttag = cast(TTSTag, tag)
|
||||
self._taskman.run(
|
||||
lambda: self._play(ttag), lambda ret: self._on_done(ret, on_done)
|
||||
)
|
||||
|
||||
def _play(self, tag: TTSTag) -> None:
|
||||
ret = subprocess.run(
|
||||
["say", "-v", "Alex", "-f", "-"],
|
||||
input=tag.text,
|
||||
encoding="utf8",
|
||||
check=True,
|
||||
)
|
||||
|
||||
def _on_done(self, ret: Future, cb: OnDoneCallback) -> None:
|
||||
# will raise on error
|
||||
ret.result()
|
||||
cb()
|
||||
|
||||
def stop(self):
|
||||
pass
|
Loading…
Reference in a new issue