From b4b12f16422ff98d135e36901beb7c60ab1ee51c Mon Sep 17 00:00:00 2001 From: ANH Date: Thu, 30 Jul 2020 21:06:16 +0300 Subject: [PATCH 1/4] add reviewer_will_play_question_sounds and reviewer_will_play_answer_sounds hooks --- qt/aqt/gui_hooks.py | 54 ++++++++++++++++++++++++++++++++++++++++ qt/aqt/reviewer.py | 16 +++++++++--- qt/tools/genhooks_gui.py | 8 ++++++ 3 files changed, 74 insertions(+), 4 deletions(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 2f30d22e4..8c8ec5fd9 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -2112,6 +2112,60 @@ class _ReviewerWillEndHook: reviewer_will_end = _ReviewerWillEndHook() +class _ReviewerWillPlayAnswerSoundsHook: + _hooks: List[Callable[[Card, "List[anki.sound.AVTag]"], None]] = [] + + def append(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None: + """(card: Card, tags: List[anki.sound.AVTag])""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self, card: Card, tags: List[anki.sound.AVTag]) -> None: + for hook in self._hooks: + try: + hook(card, tags) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +reviewer_will_play_answer_sounds = _ReviewerWillPlayAnswerSoundsHook() + + +class _ReviewerWillPlayQuestionSoundsHook: + _hooks: List[Callable[[Card, "List[anki.sound.AVTag]"], None]] = [] + + def append(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None: + """(card: Card, tags: List[anki.sound.AVTag])""" + self._hooks.append(cb) + + def remove(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None: + if cb in self._hooks: + self._hooks.remove(cb) + + def count(self) -> int: + return len(self._hooks) + + def __call__(self, card: Card, tags: List[anki.sound.AVTag]) -> None: + for hook in self._hooks: + try: + hook(card, tags) + except: + # if the hook fails, remove it + self._hooks.remove(hook) + raise + + +reviewer_will_play_question_sounds = _ReviewerWillPlayQuestionSoundsHook() + + class _ReviewerWillShowContextMenuHook: _hooks: List[Callable[["aqt.reviewer.Reviewer", QMenu], None]] = [] diff --git a/qt/aqt/reviewer.py b/qt/aqt/reviewer.py index 3bc85352f..01d847afe 100644 --- a/qt/aqt/reviewer.py +++ b/qt/aqt/reviewer.py @@ -183,10 +183,14 @@ class Reviewer: q = c.q() # play audio? if c.autoplay(): - av_player.play_tags(c.question_av_tags()) + sounds = c.question_av_tags() + gui_hooks.reviewer_will_play_question_sounds(c, sounds) + av_player.play_tags(sounds) else: av_player.clear_queue_and_maybe_interrupt() - + sounds = [] + gui_hooks.reviewer_will_play_question_sounds(c, sounds) + av_player.play_tags(sounds) # render & update bottom q = self._mungeQA(q) q = gui_hooks.card_will_show(q, c, "reviewQuestion") @@ -225,10 +229,14 @@ class Reviewer: a = c.a() # play audio? if c.autoplay(): - av_player.play_tags(c.answer_av_tags()) + sounds = c.answer_av_tags() + gui_hooks.reviewer_will_play_answer_sounds(c, sounds) + av_player.play_tags(sounds) else: av_player.clear_queue_and_maybe_interrupt() - + sounds = [] + gui_hooks.reviewer_will_play_answer_sounds(c, sounds) + av_player.play_tags(sounds) a = self._mungeQA(a) a = gui_hooks.card_will_show(a, c, "reviewAnswer") # render and update bottom diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index f5db67f6f..ea7fe384a 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -90,6 +90,14 @@ hooks = [ legacy_hook="reviewCleanup", doc="Called before Anki transitions from the review screen to another screen.", ), + Hook( + name="reviewer_will_play_question_sounds", + args=["card: Card", "tags: List[anki.sound.AVTag]"], + ), + Hook( + name="reviewer_will_play_answer_sounds", + args=["card: Card", "tags: List[anki.sound.AVTag]"], + ), # Debug ################### Hook( From b4604873c49a29a70cf503e7552a567b525fe2b2 Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 31 Jul 2020 03:06:13 +0300 Subject: [PATCH 2/4] document hooks --- qt/aqt/gui_hooks.py | 24 ++++++++++++++++++++++++ qt/tools/genhooks_gui.py | 22 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 8c8ec5fd9..8fd74230a 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -2113,6 +2113,18 @@ reviewer_will_end = _ReviewerWillEndHook() class _ReviewerWillPlayAnswerSoundsHook: + """Called before showing the answer/back side. + + `tags` can be used to inspect and manipulate the sounds + that will be played (if any). + + This won't be called when the user manually plays sounds + using `Replay Audio`. + + Not that this hook is called even when the `Automatically play audio` + option is unchecked; This is so as to allow playing custom + sounds regardless of that option.""" + _hooks: List[Callable[[Card, "List[anki.sound.AVTag]"], None]] = [] def append(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None: @@ -2140,6 +2152,18 @@ reviewer_will_play_answer_sounds = _ReviewerWillPlayAnswerSoundsHook() class _ReviewerWillPlayQuestionSoundsHook: + """Called before showing the question/front side. + + `tags` can be used to inspect and manipulate the sounds + that will be played (if any). + + This won't be called when the user manually plays sounds + using `Replay Audio`. + + Not that this hook is called even when the `Automatically play audio` + option is unchecked; This is so as to allow playing custom + sounds regardless of that option.""" + _hooks: List[Callable[[Card, "List[anki.sound.AVTag]"], None]] = [] def append(self, cb: Callable[[Card, "List[anki.sound.AVTag]"], None]) -> None: diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index ea7fe384a..2fc9d5024 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -93,10 +93,32 @@ hooks = [ Hook( name="reviewer_will_play_question_sounds", args=["card: Card", "tags: List[anki.sound.AVTag]"], + doc="""Called before showing the question/front side. + + `tags` can be used to inspect and manipulate the sounds + that will be played (if any). + + This won't be called when the user manually plays sounds + using `Replay Audio`. + + Not that this hook is called even when the `Automatically play audio` + option is unchecked; This is so as to allow playing custom + sounds regardless of that option.""", ), Hook( name="reviewer_will_play_answer_sounds", args=["card: Card", "tags: List[anki.sound.AVTag]"], + doc="""Called before showing the answer/back side. + + `tags` can be used to inspect and manipulate the sounds + that will be played (if any). + + This won't be called when the user manually plays sounds + using `Replay Audio`. + + Not that this hook is called even when the `Automatically play audio` + option is unchecked; This is so as to allow playing custom + sounds regardless of that option.""", ), # Debug ################### From 6df4cf765e5d24ed5f40a16675868248f6de1cb0 Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 31 Jul 2020 04:41:49 +0300 Subject: [PATCH 3/4] fix typo --- qt/tools/genhooks_gui.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qt/tools/genhooks_gui.py b/qt/tools/genhooks_gui.py index 2fc9d5024..1548d18ee 100644 --- a/qt/tools/genhooks_gui.py +++ b/qt/tools/genhooks_gui.py @@ -101,7 +101,7 @@ hooks = [ This won't be called when the user manually plays sounds using `Replay Audio`. - Not that this hook is called even when the `Automatically play audio` + Note that this hook is called even when the `Automatically play audio` option is unchecked; This is so as to allow playing custom sounds regardless of that option.""", ), @@ -116,7 +116,7 @@ hooks = [ This won't be called when the user manually plays sounds using `Replay Audio`. - Not that this hook is called even when the `Automatically play audio` + Note that this hook is called even when the `Automatically play audio` option is unchecked; This is so as to allow playing custom sounds regardless of that option.""", ), From 9b0d509e744c80f2487a26260d5ba72878ee0a2a Mon Sep 17 00:00:00 2001 From: ANH Date: Fri, 31 Jul 2020 04:46:39 +0300 Subject: [PATCH 4/4] just forgot to regenerate gui_hooks.py to fix typo --- qt/aqt/gui_hooks.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qt/aqt/gui_hooks.py b/qt/aqt/gui_hooks.py index 8fd74230a..6f87684b8 100644 --- a/qt/aqt/gui_hooks.py +++ b/qt/aqt/gui_hooks.py @@ -2121,7 +2121,7 @@ class _ReviewerWillPlayAnswerSoundsHook: This won't be called when the user manually plays sounds using `Replay Audio`. - Not that this hook is called even when the `Automatically play audio` + Note that this hook is called even when the `Automatically play audio` option is unchecked; This is so as to allow playing custom sounds regardless of that option.""" @@ -2160,7 +2160,7 @@ class _ReviewerWillPlayQuestionSoundsHook: This won't be called when the user manually plays sounds using `Replay Audio`. - Not that this hook is called even when the `Automatically play audio` + Note that this hook is called even when the `Automatically play audio` option is unchecked; This is so as to allow playing custom sounds regardless of that option."""