First pass at implementing a speech history option for games.
This commit is contained in:
88
speech.py
88
speech.py
@@ -61,12 +61,13 @@ class Speech:
|
||||
# No speech providers found
|
||||
print("No speech providers found.")
|
||||
|
||||
def speak(self, text, interrupt=True):
|
||||
def speak(self, text, interrupt=True, priority="normal"):
|
||||
"""Speak text using the configured speech provider and display on screen.
|
||||
|
||||
Args:
|
||||
text (str): Text to speak and display
|
||||
interrupt (bool): Whether to interrupt current speech (default: True)
|
||||
priority (str): Speech priority - "important", "normal", or "notification"
|
||||
"""
|
||||
if not self.provider:
|
||||
return
|
||||
@@ -74,7 +75,7 @@ class Speech:
|
||||
currentTime = pygame.time.get_ticks()
|
||||
|
||||
# Check if this is the same text within the delay window
|
||||
if (self.lastSpoken["text"] == text and
|
||||
if (self.lastSpoken["text"] == text and
|
||||
currentTime - self.lastSpoken["time"] < self.speechDelay):
|
||||
return
|
||||
|
||||
@@ -82,13 +83,45 @@ class Speech:
|
||||
self.lastSpoken["text"] = text
|
||||
self.lastSpoken["time"] = currentTime
|
||||
|
||||
# Proceed with speech
|
||||
# Add to speech history (import here to avoid circular imports)
|
||||
try:
|
||||
from .services import SpeechHistoryService
|
||||
history_service = SpeechHistoryService.get_instance()
|
||||
history_service.add_message(text)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Proceed with speech based on provider and priority
|
||||
if self.providerName == "speechd":
|
||||
if interrupt:
|
||||
self.spd.cancel()
|
||||
|
||||
# Set priority for speechd
|
||||
if priority == "important":
|
||||
try:
|
||||
import speechd
|
||||
self.spd.set_priority(speechd.Priority.IMPORTANT)
|
||||
except (ImportError, AttributeError):
|
||||
pass
|
||||
elif priority == "notification":
|
||||
try:
|
||||
import speechd
|
||||
self.spd.set_priority(speechd.Priority.NOTIFICATION)
|
||||
except (ImportError, AttributeError):
|
||||
pass
|
||||
else: # normal
|
||||
try:
|
||||
import speechd
|
||||
self.spd.set_priority(speechd.Priority.TEXT)
|
||||
except (ImportError, AttributeError):
|
||||
pass
|
||||
|
||||
self.spd.say(text)
|
||||
|
||||
elif self.providerName == "accessible_output2":
|
||||
self.ao2.speak(text, interrupt=interrupt)
|
||||
# For accessible_output2, use interrupt for important messages
|
||||
use_interrupt = interrupt or (priority == "important")
|
||||
self.ao2.speak(text, interrupt=use_interrupt)
|
||||
|
||||
# Display the text on screen
|
||||
screen = pygame.display.get_surface()
|
||||
@@ -121,17 +154,18 @@ class Speech:
|
||||
# Global instance for backward compatibility
|
||||
_speechInstance = None
|
||||
|
||||
def speak(text, interrupt=True):
|
||||
def speak(text, interrupt=True, priority="normal"):
|
||||
"""Speak text using the global speech instance.
|
||||
|
||||
Args:
|
||||
text (str): Text to speak and display
|
||||
interrupt (bool): Whether to interrupt current speech (default: True)
|
||||
priority (str): Speech priority - "important", "normal", or "notification"
|
||||
"""
|
||||
global _speechInstance
|
||||
if _speechInstance is None:
|
||||
_speechInstance = Speech.get_instance()
|
||||
_speechInstance.speak(text, interrupt)
|
||||
_speechInstance.speak(text, interrupt, priority)
|
||||
|
||||
def messagebox(text, sounds=None):
|
||||
"""Enhanced messagebox with dialog support.
|
||||
@@ -313,3 +347,45 @@ def _play_dialog_sound(entry, dialog_config, sounds):
|
||||
except Exception:
|
||||
# Sound missing or error - continue silently without crashing
|
||||
pass
|
||||
|
||||
|
||||
def speak_previous():
|
||||
"""Navigate to and speak the previous message in speech history."""
|
||||
try:
|
||||
from .services import SpeechHistoryService
|
||||
history_service = SpeechHistoryService.get_instance()
|
||||
message = history_service.move_previous()
|
||||
if message:
|
||||
speak(message, interrupt=True, priority="important")
|
||||
else:
|
||||
speak("No previous messages", interrupt=True, priority="important")
|
||||
except ImportError:
|
||||
speak("Speech history not available", interrupt=True, priority="important")
|
||||
|
||||
|
||||
def speak_current():
|
||||
"""Repeat the current message in speech history."""
|
||||
try:
|
||||
from .services import SpeechHistoryService
|
||||
history_service = SpeechHistoryService.get_instance()
|
||||
message = history_service.get_current()
|
||||
if message:
|
||||
speak(message, interrupt=True, priority="important")
|
||||
else:
|
||||
speak("No current message", interrupt=True, priority="important")
|
||||
except ImportError:
|
||||
speak("Speech history not available", interrupt=True, priority="important")
|
||||
|
||||
|
||||
def speak_next():
|
||||
"""Navigate to and speak the next message in speech history."""
|
||||
try:
|
||||
from .services import SpeechHistoryService
|
||||
history_service = SpeechHistoryService.get_instance()
|
||||
message = history_service.move_next()
|
||||
if message:
|
||||
speak(message, interrupt=True, priority="important")
|
||||
else:
|
||||
speak("No next messages", interrupt=True, priority="important")
|
||||
except ImportError:
|
||||
speak("Speech history not available", interrupt=True, priority="important")
|
||||
|
Reference in New Issue
Block a user