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,29 +223,21 @@ 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 raw bytes from stdout # Read directly from stdout, no need for buffer attribute
rawLine = process.stdout.buffer.readline() for line in process.stdout:
if not rawLine:
break
# Try different encodings
for encoding in ['utf-8', 'latin1', 'cp1252']:
try: try:
line = rawLine.decode(encoding) # Handle encoding by trying utf-8 first, then fallback to latin1
break try:
except UnicodeDecodeError: if isinstance(line, bytes):
continue lineStr = line.decode('utf-8').strip()
else: else:
# If all encodings fail, skip this line lineStr = line.strip()
print(f"Warning: Could not decode line: {rawLine}", file=sys.stderr) except UnicodeDecodeError:
continue lineStr = line.decode('latin1').strip()
# Keep gzdoom's existing functionality of lines being printed to the console # Keep gzdoom's existing functionality of lines being printed to the console
print(line, end='') print(lineStr)
lineStr = line.strip()
# Wait for the initial separator before starting speech # Wait for the initial separator before starting speech
if not startSpeech: if not startSpeech:
@ -258,8 +250,11 @@ class SpeechHandler:
self.speak(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,8 +1701,8 @@ 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")
) )