diff --git a/qt/aqt/tts.py b/qt/aqt/tts.py index 6e3bc9044..feba27d8e 100644 --- a/qt/aqt/tts.py +++ b/qt/aqt/tts.py @@ -33,7 +33,7 @@ from dataclasses import dataclass from typing import List, Optional, Any, cast from anki.sound import AVTag, TTSTag -from anki.utils import tmpdir, isWin +from anki.utils import tmpdir, isWin, checksum from aqt.sound import OnDoneCallback, PlayerInterrupted, SimpleProcessPlayer @@ -82,6 +82,15 @@ class TTSPlayer: return None + def temp_file_for_tag_and_voice(self, tag: AVTag, voice: TTSVoice) -> str: + """Return a hashed filename, to allow for caching generated files. + + No file extension is included.""" + assert isinstance(tag, TTSTag) + buf = f"{voice.name}-{voice.lang}-{tag.field_text}" + return os.path.join(tmpdir(), + f"tts-{checksum(buf)}") + class TTSProcessPlayer(SimpleProcessPlayer, TTSPlayer): # mypy gets confused if rank_for_tag is defined in TTSPlayer