Specifically set the encoding to UTF-8. Simplify speak_thread.

This commit is contained in:
Storm Dragon 2025-01-25 08:41:11 -05:00
parent db40032d3d
commit a0e539d3ce

View File

@ -244,51 +244,30 @@ class SpeechHandler:
def speak_thread(self, process: subprocess.Popen): def speak_thread(self, process: subprocess.Popen):
"""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
try: while True:
# Read directly from stdout, no need for buffer attribute try:
for line in process.stdout: line = process.stdout.readline()
try: # Keep gzdoom's existing functionality of lines being printed to the console.
# Try multiple encodings in order print(line, end='')
encodings = ['utf-8', 'gbk', 'big5', 'latin1'] if not line:
lineStr = None break
for encoding in encodings: lineStr = line.strip()
try:
if isinstance(line, bytes): # Wait for the initial separator before starting speech
lineStr = line.decode(encoding).strip() if not startSpeech:
else: if lineStr and all(c == '-' for c in lineStr):
lineStr = line.strip() startSpeech = True
break # Successfully decoded continue
except UnicodeDecodeError:
continue processedLine = self.process_line(lineStr)
if processedLine:
if lineStr is None: self.speak(processedLine)
print(f"Failed to decode line with any encoding", file=sys.stderr)
continue except Exception as e:
print(f"Error processing game output: {e}", file=sys.stderr)
# Keep gzdoom's existing functionality of lines being printed to the console break
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
processedLine = self.process_line(lineStr)
if processedLine:
try:
self.speak(processedLine)
except Exception as e:
print(f"Error speaking line: {e}", file=sys.stderr)
except Exception as e:
print(f"Error processing line: {e}", file=sys.stderr)
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):