How better to use TTS (Text to Speech...)

Godot Version

Godot 4.6.1

Question

Having tried out Godot TTS, it works very well for my modest usage, but could be so much better if there was a way of identifying the end of the speech, with a signal, for instance, in similar fashion to animation endings. I suppose that this is rather more difficult, as the speech itself is not coming ‘native’ from Godot, but is there any work-around that others use..? Maybe a ‘safe word’ to be detected and used to Stop the TTS..? An ASCII code to append to a text..? Find the length of the speech and set off a Timer..? For the moment, I have a generic 10-second Timer which cuts it off by declaring an empty ““ text, followed by Stop, but the voice repeats the message until the time-out cuts in. Is there a better method..?

You can register a callback to know when it starts or finishes using DisplayServer.tts_set_utterance_callback() like:

func _ready() -> void:
	DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_STARTED, func(id: int): print("Started speaking"))
	DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_ENDED, func(id: int): print("Ended speaking"))
3 Likes

Excellent..! It took a little fettling to get to work as a ‘one-shot’ speech, without losing future speech, but I got there in the end. Here’s my (clumsy…) code, inside a Canvas _Process for my HUD…

~~~

if GlobalVariables.gv_mess != "":
	DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_STARTED, func(_id: int): DisplayServer.tts_get_voices_for_language("en"))
	DisplayServer.tts_speak(GlobalVariables.gv_mess, voice_id)
	DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_ENDED, func(_id: int): DisplayServer.tts_stop())
	GlobalVariables.gv_mess = ""
else:
	DisplayServer.tts_set_utterance_callback(DisplayServer.TTS_UTTERANCE_STARTED, func(_id: int): DisplayServer.tts_get_voices_for_language("en"))

~~~