Code cleanup and sound consolidation.

This commit is contained in:
Storm Dragon
2025-03-22 17:34:35 -04:00
parent 3a478d15d5
commit a17a4c6f15
10 changed files with 732 additions and 986 deletions

View File

@ -15,26 +15,26 @@ from sys import exit
class Speech:
"""Handles text-to-speech functionality."""
_instance = None
@classmethod
def get_instance(cls):
"""Get or create the singleton instance."""
if cls._instance is None:
cls._instance = Speech()
return cls._instance
def __init__(self):
"""Initialize speech system with available provider."""
# Handle speech delays so we don't get stuttering
self.lastSpoken = {"text": None, "time": 0}
self.speechDelay = 250 # ms
# Try to initialize a speech provider
self.provider = None
self.providerName = None
# Try speechd first
try:
import speechd
@ -44,7 +44,7 @@ class Speech:
return
except ImportError:
pass
# Try accessible_output2 next
try:
import accessible_output2.outputs.auto
@ -54,31 +54,31 @@ class Speech:
return
except ImportError:
pass
# No speech providers found
print("No speech providers found.")
def speak(self, text, interrupt=True):
"""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)
"""
if not self.provider:
return
currentTime = pygame.time.get_ticks()
# Check if this is the same text within the delay window
if (self.lastSpoken["text"] == text and
currentTime - self.lastSpoken["time"] < self.speechDelay):
return
# Update last spoken tracking
self.lastSpoken["text"] = text
self.lastSpoken["time"] = currentTime
# Proceed with speech
if self.providerName == "speechd":
if interrupt:
@ -86,12 +86,12 @@ class Speech:
self.spd.say(text)
elif self.providerName == "accessible_output2":
self.ao2.speak(text, interrupt=interrupt)
# Display the text on screen
screen = pygame.display.get_surface()
if not screen:
return
font = pygame.font.Font(None, 36)
# Wrap the text
maxWidth = screen.get_width() - 40 # Leave a 20-pixel margin on each side
@ -109,7 +109,7 @@ class Speech:
screen.blit(surface, textRect)
currentY += surface.get_height()
pygame.display.flip()
def close(self):
"""Clean up speech resources."""
if self.providerName == "speechd":
@ -120,7 +120,7 @@ _speechInstance = None
def speak(text, interrupt=True):
"""Speak text using the global speech instance.
Args:
text (str): Text to speak and display
interrupt (bool): Whether to interrupt current speech (default: True)