Another attempt at fixing speech processing for non-English characters.

This commit is contained in:
Storm Dragon 2025-01-19 12:10:15 -05:00
parent 6e9aaa9751
commit ad55390801

View File

@ -223,43 +223,38 @@ class SpeechHandler:
"""Thread to handle speech processing""" """Thread to handle speech processing"""
startSpeech = False # Don't start speaking until after initial output startSpeech = False # Don't start speaking until after initial output
while True: try:
try: # Read directly from stdout, no need for buffer attribute
# Read raw bytes from stdout for line in process.stdout:
rawLine = process.stdout.buffer.readline() try:
if not rawLine: # Handle encoding by trying utf-8 first, then fallback to latin1
break
# Try different encodings
for encoding in ['utf-8', 'latin1', 'cp1252']:
try: try:
line = rawLine.decode(encoding) if isinstance(line, bytes):
break lineStr = line.decode('utf-8').strip()
else:
lineStr = line.strip()
except UnicodeDecodeError: except UnicodeDecodeError:
lineStr = line.decode('latin1').strip()
# Keep gzdoom's existing functionality of lines being printed to the console
print(lineStr)
# Wait for the initial separator before starting speech
if not startSpeech:
if lineStr and all(c == '-' for c in lineStr):
startSpeech = True
continue continue
else:
# If all encodings fail, skip this line processedLine = self.process_line(lineStr)
print(f"Warning: Could not decode line: {rawLine}", file=sys.stderr) if processedLine:
continue self.speak(processedLine)
# Keep gzdoom's existing functionality of lines being printed to the console
print(line, end='')
lineStr = line.strip()
# Wait for the initial separator before starting speech
if not startSpeech:
if lineStr and all(c == '-' for c in lineStr):
startSpeech = True
continue
processedLine = self.process_line(lineStr)
if processedLine:
self.speak(processedLine)
except Exception as e: except Exception as e:
print(f"Error processing game output: {e}", file=sys.stderr) print(f"Error processing line: {e}", file=sys.stderr)
continue # Continue processing instead of breaking continue # Skip this line and continue with the next
except Exception as e:
print(f"Error in speech thread: {e}", file=sys.stderr)
class MenuDialog(QDialog): class MenuDialog(QDialog):
@ -1688,26 +1683,12 @@ class DoomLauncher(QMainWindow):
cwd=str(self.gamePath), cwd=str(self.gamePath),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
bufsize=1, bufsize=1, # Line buffered
universal_newlines=True, # This handles text encoding
env=dict(os.environ, PYTHONUNBUFFERED="1"), env=dict(os.environ, PYTHONUNBUFFERED="1"),
startupinfo=startupinfo startupinfo=startupinfo
) )
# Start speech processing thread for Windows
speechThread = threading.Thread(
target=self.speechHandler.speak_thread,
args=(process,),
daemon=True
)
speechThread.start()
# Monitor thread
monitorThread = threading.Thread(
target=self.monitor_game_process,
args=(process,),
daemon=True
)
monitorThread.start()
else: else:
# For Linux/Mac, use stdbuf to unbuffer output # For Linux/Mac, use stdbuf to unbuffer output
cmdLine = ["stdbuf", "-oL", gzdoomPath, "-stdout", cmdLine = ["stdbuf", "-oL", gzdoomPath, "-stdout",
@ -1720,26 +1701,26 @@ class DoomLauncher(QMainWindow):
cwd=str(self.gamePath), cwd=str(self.gamePath),
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
bufsize=1, bufsize=1, # Line buffered
text=True, universal_newlines=True, # This handles text encoding
env=dict(os.environ, PYTHONUNBUFFERED="1") env=dict(os.environ, PYTHONUNBUFFERED="1")
) )
# Start speech processing thread # Start speech processing thread
speechThread = threading.Thread( speechThread = threading.Thread(
target=self.speechHandler.speak_thread, target=self.speechHandler.speak_thread,
args=(process,), args=(process,),
daemon=True daemon=True
) )
speechThread.start() speechThread.start()
# Start process monitor thread # Start process monitor thread
monitorThread = threading.Thread( monitorThread = threading.Thread(
target=self.monitor_game_process, target=self.monitor_game_process,
args=(process,), args=(process,),
daemon=True daemon=True
) )
monitorThread.start() monitorThread.start()
# Hide the window # Hide the window
self.hide() self.hide()