From 1d42ecb6e97aafcc80788f4761b968a944857997 Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Mon, 23 Nov 2026 00:15:34 -0500 Subject: [PATCH] Optionally turn off the displayed text when messages are spoken. --- __init__.py | 6 +++++- speech.py | 48 +++++++++++++++++++++++++++++++++++++++--- tests/test_speech.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) diff --git a/__init__.py b/__init__.py index 4759845..720c433 100755 --- a/__init__.py +++ b/__init__.py @@ -40,7 +40,8 @@ from .sound import ( ) # Import Speech class and functions -from .speech import messagebox, speak, Speech, speak_previous, speak_current, speak_next +from .speech import (messagebox, speak, Speech, speak_previous, speak_current, speak_next, set_text_display, + add_speech_listener, remove_speech_listener) # Import Scoreboard from .scoreboard import Scoreboard @@ -106,6 +107,9 @@ __all__ = [ 'speak_previous', 'speak_current', 'speak_next', + 'set_text_display', + 'add_speech_listener', + 'remove_speech_listener', # Scoreboard 'Scoreboard', diff --git a/speech.py b/speech.py index 78ee248..cbfd9b3 100644 --- a/speech.py +++ b/speech.py @@ -35,6 +35,12 @@ class Speech: self.lastSpoken = {"text": None, "time": 0} self.speechDelay = 250 # ms + # Whether speak() also shows the text full screen. A game that shows + # spoken text itself turns this off with set_text_display(False). + self.displayText = True + # Functions told about every message, even when no speech provider was found. + self.listeners = [] + # Try to initialize a speech provider self.provider = None self.providerName = None @@ -71,9 +77,6 @@ class Speech: priority (str): Speech priority - "important", "normal", or "notification" add_to_history (bool): Whether to add this message to speech history (default: True) """ - if not self.provider: - return - currentTime = pygame.time.get_ticks() # Check if this is the same text within the delay window @@ -94,6 +97,13 @@ class Speech: except ImportError: pass + # Tell listeners before speaking, so a game can show the text even with no speech. + for listener in list(self.listeners): + listener(text) + + if not self.provider: + return + # Proceed with speech based on provider and priority if self.providerName == "speechd": if interrupt: @@ -127,6 +137,8 @@ class Speech: self.ao2.speak(text, interrupt=use_interrupt) # Display the text on screen + if not self.displayText: + return screen = pygame.display.get_surface() if not screen: return @@ -171,6 +183,36 @@ def speak(text, interrupt=True, priority="normal", add_to_history=True): _speechInstance = Speech.get_instance() _speechInstance.speak(text, interrupt, priority, add_to_history) +def set_text_display(enabled): + """Choose whether speak() shows spoken text full screen (on by default). + + A game that draws spoken text itself, such as in a message box, turns this + off so the screen is not blanked for each message. + + Args: + enabled (bool): True to show spoken text full screen, False to only speak it + """ + Speech.get_instance().displayText = enabled + + +def add_speech_listener(callback): + """Call callback(text) for every message speak() is given, even with no speech provider. + + Messages repeated within the speech delay are skipped, as they are for speech. + + Args: + callback (callable): Function taking the message text + """ + Speech.get_instance().listeners.append(callback) + + +def remove_speech_listener(callback): + """Stop calling a function added with add_speech_listener.""" + listeners = Speech.get_instance().listeners + if callback in listeners: + listeners.remove(callback) + + def messagebox(text, sounds=None): """Show a message or dialog with background music temporarily suspended. diff --git a/tests/test_speech.py b/tests/test_speech.py index 9795386..cc02f26 100644 --- a/tests/test_speech.py +++ b/tests/test_speech.py @@ -168,5 +168,55 @@ class MessageMusicTests(unittest.TestCase): self.assert_music_restored(music, state) +class SpeechListenerTests(unittest.TestCase): + def make_speech(self): + module = load_speech() + module.pygame = Mock() + module.pygame.time.get_ticks.return_value = 10000 + speech = module.Speech.__new__(module.Speech) + speech.lastSpoken = {"text": None, "time": 0} + speech.speechDelay = 250 + speech.displayText = True + speech.listeners = [] + speech.provider = None + speech.providerName = None + return module, speech + + def test_listener_hears_messages_without_a_speech_provider(self): + module, speech = self.make_speech() + heard = [] + speech.listeners.append(heard.append) + speech.speak("Lap 2.", add_to_history=False) + self.assertEqual(heard, ["Lap 2."]) + + def test_repeat_within_delay_is_not_heard_twice(self): + module, speech = self.make_speech() + heard = [] + speech.listeners.append(heard.append) + speech.speak("Lap 2.", add_to_history=False) + speech.speak("Lap 2.", add_to_history=False) + self.assertEqual(heard, ["Lap 2."]) + + def test_text_display_off_leaves_the_screen_alone(self): + module, speech = self.make_speech() + speech.provider = Mock() + speech.providerName = "accessible_output2" + speech.ao2 = speech.provider + speech.displayText = False + speech.speak("Lap 2.", add_to_history=False) + speech.ao2.speak.assert_called_once_with("Lap 2.", interrupt=True) + module.pygame.display.get_surface.assert_not_called() + module.pygame.display.flip.assert_not_called() + + def test_text_display_on_by_default_still_draws(self): + module, speech = self.make_speech() + speech.provider = Mock() + speech.providerName = "accessible_output2" + speech.ao2 = speech.provider + module.pygame.display.get_surface.return_value = None + speech.speak("Lap 2.", add_to_history=False) + module.pygame.display.get_surface.assert_called_once_with() + + if __name__ == "__main__": unittest.main()