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