2019-10-09 21:10:14 +00:00
|
|
|
tool
|
2019-09-24 19:21:05 +00:00
|
|
|
extends Node
|
2019-09-17 15:12:17 +00:00
|
|
|
|
2019-12-31 14:54:13 +00:00
|
|
|
var TTS
|
2019-09-17 15:12:17 +00:00
|
|
|
|
2019-10-12 22:03:26 +00:00
|
|
|
var tts = null
|
|
|
|
|
|
|
|
func _ready():
|
2019-12-31 14:54:13 +00:00
|
|
|
if not OS.has_feature('JavaScript') and not Engine.has_singleton("AndroidTTS"):
|
|
|
|
TTS = preload("godot-tts.gdns")
|
|
|
|
if OS.has_feature('JavaScript'):
|
|
|
|
pass
|
2019-12-30 18:57:19 +00:00
|
|
|
elif Engine.has_singleton("AndroidTTS"):
|
|
|
|
tts = Engine.get_singleton("AndroidTTS")
|
2019-12-31 14:54:13 +00:00
|
|
|
elif TTS.can_instance() or Engine.editor_hint:
|
|
|
|
tts = TTS.new()
|
2019-10-12 22:03:26 +00:00
|
|
|
else:
|
2019-10-16 13:01:34 +00:00
|
|
|
print_debug("TTS not available!")
|
2019-09-17 15:12:17 +00:00
|
|
|
|
2019-12-31 14:54:13 +00:00
|
|
|
var javascript_rate = 50
|
|
|
|
|
2019-09-27 18:07:35 +00:00
|
|
|
func set_rate(rate):
|
2019-10-12 22:03:26 +00:00
|
|
|
if tts != null:
|
|
|
|
tts.rate = rate
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
javascript_rate = rate
|
2019-09-27 18:07:35 +00:00
|
|
|
|
|
|
|
func get_rate():
|
2019-10-12 22:03:26 +00:00
|
|
|
if tts != null:
|
|
|
|
return tts.rate
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
return javascript_rate
|
2019-10-12 22:03:26 +00:00
|
|
|
else:
|
|
|
|
return 0
|
2019-09-27 18:07:35 +00:00
|
|
|
|
|
|
|
var rate setget set_rate, get_rate
|
2019-10-09 21:10:14 +00:00
|
|
|
|
2019-09-17 15:12:17 +00:00
|
|
|
func speak(text, interrupt := true):
|
2019-10-16 13:01:34 +00:00
|
|
|
print_debug("%s: %s" % [text, interrupt])
|
2019-10-12 22:03:26 +00:00
|
|
|
if tts != null:
|
|
|
|
tts.speak(text, interrupt)
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
var scaled_rate = javascript_rate / 2
|
|
|
|
var code = """
|
|
|
|
let utterance = new SpeechSynthesisUtterance("%s")
|
|
|
|
utterance.rate = %s
|
|
|
|
""" % [text, scaled_rate]
|
|
|
|
if interrupt:
|
|
|
|
code += """
|
|
|
|
window.speechSynthesis.cancel()
|
|
|
|
"""
|
|
|
|
code += "window.speechSynthesis.speak(utterance)"
|
|
|
|
JavaScript.eval(code)
|
2019-09-17 15:12:17 +00:00
|
|
|
|
|
|
|
func stop():
|
2019-10-12 22:03:26 +00:00
|
|
|
if tts != null:
|
|
|
|
tts.stop()
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
JavaScript.eval("window.speechSynthesis.cancel()")
|
2019-09-24 23:06:57 +00:00
|
|
|
|
2019-10-09 21:10:14 +00:00
|
|
|
func get_is_rate_supported():
|
2019-12-30 18:57:19 +00:00
|
|
|
if Engine.get_singleton("AndroidTTS"):
|
|
|
|
return false
|
2019-12-31 14:54:13 +00:00
|
|
|
elif OS.has_feature('JavaScript'):
|
|
|
|
return true
|
2019-12-30 18:57:19 +00:00
|
|
|
elif tts != null:
|
2019-10-12 22:03:26 +00:00
|
|
|
return tts.is_rate_supported()
|
|
|
|
else:
|
|
|
|
return false
|
2019-10-09 21:10:14 +00:00
|
|
|
|
|
|
|
var is_rate_supported setget , get_is_rate_supported
|
|
|
|
|
2019-09-24 23:06:57 +00:00
|
|
|
func singular_or_plural(count, singular, plural):
|
|
|
|
if count == 1:
|
|
|
|
return singular
|
|
|
|
else:
|
|
|
|
return plural
|
2019-12-23 00:09:40 +00:00
|
|
|
|
|
|
|
func _exit_tree():
|
|
|
|
tts.free()
|