Files
gaim/Interact/dialogue/text_to_speech.gd
T
2026-08-31 13:03:24 +02:00

49 lines
1.1 KiB
GDScript

extends Node
var voice_id
var _tts_locale := ""
func _ready():
_update_voice_for_locale()
DialogueManager.got_dialogue.connect(_on_dialogue)
func _notification(what: int) -> void:
if what == NOTIFICATION_TRANSLATION_CHANGED:
_update_voice_for_locale()
func _update_voice_for_locale() -> void:
var locale := TranslationServer.get_locale()
var lang := locale.substr(0, 2)
if lang == _tts_locale and voice_id != null:
return
_tts_locale = lang
var voices = DisplayServer.tts_get_voices_for_language(lang)
if voices.size() > 0:
# Prefer a known German robotic voice when available; otherwise first matching voice
if lang == "de" and "German+Robosoft6" in voices:
voice_id = "German+Robosoft6"
else:
voice_id = voices[0]
else:
# Fallback to English if current language has no TTS voices
voices = DisplayServer.tts_get_voices_for_language("en")
if voices.size() > 0:
voice_id = voices[0]
else:
voice_id = null
func _on_dialogue(line):
if voice_id == null:
return
DisplayServer.tts_stop()
DisplayServer.tts_speak(line.text, voice_id, 100)
func _stop_dialogue():
DisplayServer.tts_stop()